I'm using pyfmi for loading the Modelica generated model in Python. Once I load the model I want to perform an optimization and parameter estimation. The issue is that for every parameter estimation (optimization iteration) the FMU needs to be loaded ususally it needs around 300-400 itterations but it is not converging because of binary loading error.Where should I look for soultions? Any hints are welcome.
def fun2optim(theta):## Funtion to optimize with the initial guess of paramameter values theta
model = load_fmu("MOdel_0IV_0curves.fmu")## LOAD THE FMU
res = model.simulate(input=foo(theta),final_time=1)
results_VV=np.array([]) ###SAVE THE OUTPUT IN ARRAY
for i in range(200,400):
out=(res[output_IV[i]])
results=out[0::5] #Dymola FMU has 5 same IV curve points
results_VV=np.append(results_VV,results)
return(results_VV)
def RMSE (theta): ## results_V are the ideal values
tt=sum(np.sqrt((fun2optim(theta)-results_V)**2).mean())
return(tt)
from scipy import optimize
res11=optimize.minimize(RMSE,thetaInit,method='nelder-mead', options={'xtol': 1e-4, 'disp': True})
After 50-60 itterations I got an error:
FMUException: Error loading the binary. Could not load the DLL: A dynamic link library (DLL) initialization routine failed.
I've experienced similar problems before with Dymola FMUs and my best guess is that something is not unloaded correctly which in the end leads to the problem.
I'd suggest to change your code to:
model = load_fmu("MOdel_0IV_0curves.fmu")## LOAD THE FMU
def fun2optim(theta):## Funtion to optimize with the initial guess of paramameter values theta
global model
model.reset()
res = model.simulate(input=foo(theta),final_time=1)
results_VV=np.array([]) ###SAVE THE OUTPUT IN ARRAY
for i in range(200,400):
out=(res[output_IV[i]])
results=out[0::5] #Dymola FMU has 5 same IV curve points
results_VV=np.append(results_VV,results)
return(results_VV)
In this way you do not reload the FMU each time (you just reset it) which also will lead to a performance boost.