1

I am working on a simulation tool for a certain heat exchange system. I first design the system using a certain routine, and then assess it's off-design performance. For the off-design performance I use a single gekko model instance that I want to run at different off-design operating points. To set the the initial values in the off-design model close to a reasonable value for the off-design simulations, I run the off-design model at the design point. After this, I run the off-design points. For some off-design points, the simulation converges, but for others it doesn't. Th

ough, most of the unconverged off-design points do converge when I run the simulation without first running the design point in the off-design model.

Is there some way I can hard reset the gekko model to a state before the first solve? In that way I can control when initial guesses from the design simulation are used. I've tried numerous things, but none seem to work. Unfortunately I can't post the exact script I'm using since it is too large. I've tried the following to reset the model though:

  • Setting any combination of the following solver options: COLDSTART, CSV_READ, CSV_WRITE, AUTO_COLD, BAD_CYCLES, DBS_READ, DBS_WRITE, SPECS
  • Manually resetting the variables values using:
# Save initial variable values before initial solve:
if init_variable_vals = None:
    init_variable_vals = {deepcopy(vi.name): deepcopy(float(str(vi.value)))
    for vi in m._variables}

...  # Calling solve, handling exception


for vi in m._variables:
    vi.value = init_variable_vals[vi.name]

... # Setting off-design point and attempting solve

I also tried this in combination with setting vi.value.change to 'True', 'False', and its value before the first solve (not sure why this changes between solves).

  • Replacing the entire model object:
# Save model object before solving it for the first time.
init_m = deepcopy(m)

... # Calling solve, handling exception

m = init_m

... # Setting off-design point and attempting solve

None of these seem to work well. Is there some way of forcing the variables to go back to their initial state before a previous solve?

1 Answer 1

0

As you've noticed, Gekko uses prior solve information to help subsequent solve requests. The best way to return to the initial state is to delete the old model and create a new model.

from gekko import GEKKO

def build_model(design_params):
    m = GEKKO(remote=False)
    # build full model structure using design_params and set options
    # ...
    # ...
    return m

m = build_model(design_params)
m.solve()

m.cleanup()
m = build_model(design_params)
m.solve()

If you don't want to re-generate the model, you can try various levels of removing files:

  • m.clear_data() removes only data and initial condition files
  • m.clear() deletes all files in the model working folder (m._path is the run directory) but keeps the folder. The Python model (variables, equations, options in memory) remains. The next m.solve() rewrites the files.
  • m.cleanup() removes the entire working folder (temp directory) for that model.

You can also ignore individuals files although new options or data will also not be read.

  • m.options.DBS_READ=0 (skip options)
  • m.options.CSV_READ=0 (skip data)

Warm-start files are the .t0 text files in the run directory m._path. You can selectively remove those with this code segment after every solve:

# remove only the .t0 warm-start files in the Gekko run directory
import os, glob

t0_files = glob.glob(os.path.join(m._path, '*.t0'))
for f in t0_files:
    try:
        os.remove(f)
        # print(f"Removed: {f}")  # uncomment if you want confirmation
    except OSError as e:
        print(f"Could not remove {f}: {e}")

Change the '*.t0' to another extension such as '*.csv' to add additional files extensions. Here is another way to remove both:

import os, glob

# extensions you want to remove (add more if needed)
exts = ('.t0', '.csv')

for ext in exts:
    for f in glob.glob(os.path.join(m._path, f'*{ext}')):
        try:
            os.remove(f)
            # print(f"Removed: {f}")  # <- optional confirmation
        except OSError as e:
            print(f"Could not remove {f}: {e}")

Make sure you are in local mode m=GEKKO(remote=False) so that all of your restart files are in the local run directory and not on the public server.

Sign up to request clarification or add additional context in comments.

3 Comments

Dear prof. Hedengren,
Thank you for your response! Unfortunately it is very unpractical for my problem to rebuild the model, so I would really like to find a different solution before resorting to that. I've also tried the other methods you suggested. Unfortunately using the 'clear_data' method removes some parameter values that I set for the different off-design considerations. That essentially returns the model back to a design-simulation. Using the 'clear' method also removes files that are used for interpolation in the model. As a result, an error is thrown.
The warm-start solution is stored in the t0 files in the run directory m._path. You could try removing the t0 files. I'll add a code snippet to try that or other files that you'd like to selectively remove. Open the run directory with m.open_folder().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.