Reputation: 63
I have one FMU to simulate using PyFMI. How do I create an input function that depends on an output of the simulated FMU? The documentation provides only examples for input functions that depend on external variables, e.g.:
# Generate input
t = N.linspace(0.,10.,100)
u = N.cos(t)
u_traj = N.transpose(N.vstack((t,u)))
# Create input object
input_object = ('u', u_traj)
# Simulate
res = model.simulate(final_time=30, input=input_object, options={'ncp':3000})
How do I do if I want my input function u_traj to depend on the output 'y' of model instead of t?
Upvotes: 1
Views: 649
Reputation: 1123
It is possible. In PyFMI it is allowed to specify the input as a function instead of a data matrix.
model = load_fmu(...)
def input_function(t):
global model
#Get values from the model using e.g. "model.get("my_parameter")"
p = model.get("my_paramater")
return t*p
input_object = ("u", input_function)
res = model.simulate(final_time=30, input=input_object, options={'ncp':3000})
But as I said in my comment, this has to be done with care as it is possible to create loops and make the problem unsolvable. It might also be so that you may need protect (in input_function) the first call to it as the model may not have been initialized and thus the values that you need to retrieve may not be available.
Upvotes: 1