Beginners question: why does model.get() throw an error?

3 posts / 0 new
Last post
steffenkaminski1
Offline
Joined: 2017-04-06
Beginners question: why does model.get() throw an error?

As the title says I'm wondering why <code>model.get('resistor.R')</code> throws an error. Ive compiled the fmu with dymola using attached dymola 2017 32bit model. The python code is following:

 

import os as O
import numpy as N
import matplotlib.pyplot as P

P.switch_backend('TkAgg')
import numpy as N

from pyfmi import load_fmu, fmi

if __name__ == "__main__":
curr_dir = O.path.dirname(O.path.abspath(__file__));
path_to_fmus = O.path.join(curr_dir, 'files', 'FMUs') # , 'ME1.0')
model = load_fmu(O.path.join(path_to_fmus, 'motor.fmu'))

vars=model.get_model_variables()

opts = model.simulate_options() # load solver options
opts["CVode_options"]["atol"] = 1e-5 # Change the absolute tolerance
opts["ncp"] = 500 # change number of output points

#change model parameter:
r=model.get('resistor.R')
r=2.
model.set('resistor.R', 3)

t = N.linspace(0., 10., 100) # Create one hundred evenly spaced points
temp = N.ones((1, 100), dtype=bool)
control1=temp[0]
control1[20:]=False
u = N.sin(t) # Create the input vector

u_traj = N.transpose(N.vstack((t, u, control1))) # Create the data matrix and transpose
# it to the correct form

input_object = (['v1','control1'], u_traj)

res = model.simulate(final_time=10, input=input_object, options=opts)

fig = P.figure()
P.clf()
P.subplot(2, 1, 1)
P.plot(res["time"], res['inductor.i'])
P.subplot(2, 1, 2)
P.plot(t, u, label='input_python1')
P.plot(t, control1, label='input_python2')
P.plot(res["time"], res['v1'], label='model_input1')
P.plot(res["time"], res['control1'], label='model_input2')
P.legend()

P.show()
print('done')

 

The error is:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\pydevd.py", line 1580, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\pydevd.py", line 964, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/jsc-ska/Py_Workspace/CloudControl/Build_opt/_tray/test_circut.py", line 22, in <module>
    r=model.get('resistor.R')
  File "pyfmi\fmi.pyx", line 202, in pyfmi.fmi.ModelBase.get (src\pyfmi\fmi.c:3829)
  File "pyfmi\fmi.pyx", line 3351, in pyfmi.fmi.FMUModelBase2._get (src\pyfmi\fmi.c:31986)
  File "pyfmi\fmi.pyx", line 3044, in pyfmi.fmi.FMUModelBase2.get_real (src\pyfmi\fmi.c:29596)
pyfmi.fmi.FMUException: Failed to get the Real values.

Process finished with exit code 1

 

I would be really glad if somebody could help me with this!

 

Best regards

Steffen

umutcanuguz
Offline
Joined: 2017-05-18
fmi2GetReal Illegal Call Sequence

I have been getting a similar error with my ME2 model. What is more interesting is when I run my script through python shell line by line. As in your example, the following line gives an error:

asd = model.get("Some_Parameter")

FMUException: Failed to get the Real values.

When I run model.get_log, what I get is as below:

FMIL: module = Model, log level 2:[logStatusError][FMU status:Error] fmi2GetReal: Illegal call sequence. fmi2GetReal is not allowed in Instantiated state.

So the interesting thing is, I write the last line again jsut after I get the error, without doing anything else:

asd = model.get("Some_Parameter")

I get my parameter without any problem at all. Error is somewhat related to call sequence but just getting the error any running the line that causes the error solves the problem.  I would be glad if anyone could help me to understand the underlying problem so that I wouldn't be stucked at such a preliminary point of FMU simulations.

 

 

 

 

AttachmentSize
model_get_error.jpg 146.63 KB
umutcanuguz
Offline
Joined: 2017-05-18
Initialization before fmi2GetReal

After few trials I understood that the model has to be initialized to use get method of the model, which uses fmi2GetReal function. Calling this function before the initialization phase causes an sequence error. To overcome this problem, one can initialize the model as following:

 

model.setup_experiment() # this is required for initialization
model.initialize() # does initialization (both enter and exit initialization mode)
model.get("whatever_you_want") # now it should work

The reason for these stuff is, as far as I get, that the model has to go into event mode for the interaction with model parameters, please feel free to refer chapter 3.2.3 of this beautiful document. When someone directly runs a simulation as following,

from pyfmi import load_fmu
model = load_fmu("myModel.mo")
model.set("myParameter", 3)
model.set("myParameter2", 5)
results = model.simulate()

then one wouldn't notice the problem of getting parameters out of the model(interestingly you can set parameters, although you cannot retrieve them. Yes, life is full of misteries). But the new problem you may be getting now is during the simulation. simulate method of the model makes an automatic initialization and it is going to give an error as you already initialized your model, please see the attachment.

As a new solution you may use reset or instantiate methods of the model. Since these methods are going to revert everthing to its default, you may want to store your parameters etc and set them again after you revert your model back to its initial state. That is, unfortunately, only solution I could come up with. In fact, reset method does not work for me, as I mentioned in this topic.

AttachmentSize
get_set.png 44.9 KB
Login or register to post comments