3

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

Justin Shultz
  • 199
  • 1
  • 11
  • 1
    Do you want to perform a simulation where each model is a run in a separate process or is it ok that they are run in the process and just exchange information between them? Have you considered to just use the do_step method on the co-simulation FMU? – Christian Winther Sep 03 '16 at 09:05
  • Hi @ChristianAndersson, thank you for your rely. I believe do_step may be what I'm looking for. I've used help(do_step) to get some info but I was unfamiliar with this command, is there somewhere I can get additional documentation on these commands? Additionally, I notice the set and get of variables to be acting strangely. When I "set" a variable I notice the change in the FMU results but when I "get" the same variable the return is not the value that was set. Is this common behavior? I am going to attempt to repeat this behavior with another FMU but was curious if you could provide insight. – Justin Shultz Sep 06 '16 at 14:04
  • 1
    I'd recommend to have a look at the standard specification (https://www.fmi-standard.org/downloads ) and look at the method do_step there. Note also that there are version 1.0 and version 2.0 of the standard so you'll have to check which version your FMU follow. Regarding the behaviour with set/get, it's according to the standard, the values will not be updated unless a call to the do_step method has been made. – Christian Winther Sep 06 '16 at 18:57

1 Answers1

1

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.

  • Hi Joakim, very interesting answer. I can definitely see a lot of usefulness for this code. However, I found out there is a built in function call `do_step` that iterates in a similar method. However, I see your code to be unique and potential more powerful in other situations. – Justin Shultz May 25 '17 at 14:25
  • It is very convenient, as it potentially also allows for altering the state in a loop. Another possibility is using the ME2 methods: `get_fmu_state()` and `set_fmu_state()`. See: http://www.jmodelica.org/assimulo_home/pyfmi_1.0/pyfmi.html#pyfmi.fmi.FMUModelBase2.get_fmu_state – Joakim B Petersen May 26 '17 at 08:05