Justin Shultz
Justin Shultz

Reputation: 309

Pause JModelica and Pass Incremental Inputs During Simulation

Hi Modelica Community,

I would like to run two models in parallel in JModelica but I'm not sure how to pass variables between the models. One model is a python model and the other is an EnergyPlusToFMU model.

The examples in the JModelica documentation has the full simulation period inputs defined prior to the simulation of the model. I don't understand how one would configure a model that pauses for inputs, which is a key feature of FMUs and co-simulation.

Can someone provide me with an example or piece of code that shows how this could be implemented in JModelica?

Do I put the simulate command in a loop? If so, how do I handle warm up periods and initialization without losing data at prior timesteps?

Thank you for your time,

Justin

Upvotes: 3

Views: 477

Answers (1)

Joakim B Petersen
Joakim B Petersen

Reputation: 31

Late answer, but in case it is picked up by others...

You can indeed put the simulation into a loop, you just need to keep track of the state of your system, such that you can re-init it at every iteration. Consider the following example:

Ts = 100
x_k = x_0

for k in range(100):
    # Do whatever you need to get your input here
    u_k = ...

    FMU.reset()
    FMU.set(x_k.keys(), x_k.values())

    sim_res = FMU.simulate(
        start_time=k*Ts,
        final_time=(k+1)*Ts,
        input=u_k
    )

    x_k = get_state(sim_res)

Now, I have written a small function to grab the state, x_k, of the system:

# Get state names and their values at given index
def get_state(fmu, results, index):
    # Identify states as variables with a _start_ value
    identifier = "_start_"
    keys = fmu.get_model_variables(filter=identifier + "*").keys()
    # Now, loop through all states, get their value and put it in x
    x = {}
    for name in keys:
        x[name] = results[name[len(identifier):]][index]
    # Return state
    return x

This relies on setting "state_initial_equations": True compile option.

Upvotes: 1

Related Questions