OpenModelica / OMMatlab

Matlab scripting OpenModelica interface
12 stars 4 forks source link

initialization #15

Closed MohammadHadiAlizadeh closed 2 years ago

MohammadHadiAlizadeh commented 2 years ago

Hi, I'm trying to implement a Model Predictive Control on an Open Modelica written model. I want to enforce the control algorithm on Matlab and pass the optimal values of parameters to OM with OMMatlab. I need to simulate the model in multiple separate but simultaneous time intervals. For example, I'm going to simulate the model from 0 to 10s and then do some calculations in Matlab, and change some parameters of the model, and then I want to start the simulation from time=10 to 20. So I need to pass the simulator's state values of time =10 as the initial conditions in the second interval. I have around 2000 variables(2000 initial states), and I'm not sure how to enter the values of past time steps to the OM as the initial conditions. Is there any effective method to handle this problem?

casella commented 2 years ago

@MohammadHadiAlizadeh, I see two options for your use case.

One option is to export your model as an FMU, and then import it in Simulink, using the Control System toolbox to represent the MPC controller. In order for this to work, you need to ensure that the initial equations of your model produce the right initial conditions, so that the Simulink simulation starts in the right place.

If you want to use OMMatlab instead, then you need a way to enforce the initial conditions at each step of the MPC. This can be done with some suitable simulation flags, as explained here. In fact, I plan to move this explanation to the User's Guide, because otherwise it's not very accessible where it is now 😅 . Basically, at each MPC step you run a simulation that produces a .mat file, then you use it to initialize the next MPC step by setting the simulation flags to load it and enforce it

Please let me know if you can deal with that or if you need further instructions. Eventually, if everything works it would be nice if you could contribute a short section to the User's Guide, explaining how you did that.

MohammadHadiAlizadeh commented 2 years ago

@casella Thanks for your helpful explanation. I want to use the MPC cascade with the PI controller and implement the control algorithm in Matlab without the MPC toolbox. To be more precise, the set point of the PI controller is changed with MPC at the end of each sample time(or the beginning of the new sample time). Therefore, the value of manipulated variable jumps and changes too. However, the ".mat" file contains the old value of the manipulated variable. How can I edit the mat file before feeding it to the model at the beginning of the new sample time?

casella commented 2 years ago

I think you can use CSV-file input for the top inputs of your Modelica model. This feature is not properly documented, I'm collecting info on that, will get back to you here ASAP.

arun3688 commented 2 years ago

@MohammadHadiAlizadeh As casella said you can use csv input file for your inputs and do the simulation, An example of such a csv file with inputs is attached SeborgCSTR.ModSeborgCSTRorg.csv, and in the simulate command you can use it like this

omc.sendExpression("simulate(SeborgCSTR.ModSeborgCSTRorg, simflags="-csvinput=\"SeborgCSTR.ModSeborgCSTRorg.csv\"")")

otherwise if you are using ModelicaSystem constructor you can use setInputs() API some thing like below

>>>omc.setInputs(["Tc=300","Ti=350"])
for different time steps 
>>>omc.setInputs ("Tc = [(0,350),(15,350),(15,340),(20,340),(20,360),(30,360)]")

The setInputs() API will automatically create a csv file in the working directory and you can simple use the simulate API something like below

>>> omc = OMMatlab()
>>> omc.ModelicaSystem("a.mo", "a")
>>> omc.getInputs()
>>> omc.setInputs("Tc=300")
>>> omc.simulate()
MohammadHadiAlizadeh commented 2 years ago

@arun3688 Thank you so much for your helpful guidance. I'm closing this issue.

MohammadHadiAlizadeh commented 2 years ago

@casella Many Thanks for your persistent support. I'd be looking forward to hearing from you about more info on CSV flag.

casella commented 2 years ago

@MohammadHadiAlizadeh see OpenModelica/OpenModelica#9472.

casella commented 2 years ago

@MohammadHadiAlizadeh As casella said you can use csv input file for your inputs and do the simulation, An example of such a csv file with inputs is attached SeborgCSTR.ModSeborgCSTRorg.csv, and in the simulate command you can use it like this

omc.sendExpression("simulate(SeborgCSTR.ModSeborgCSTRorg, simflags="-csvinput=\"SeborgCSTR.ModSeborgCSTRorg.csv\"")")

I guess it needs to be -csvInput with camel-case capital 'I'.

casella commented 2 years ago

@MohammadHadiAlizadeh we finally managed to get the CSV-file input feature working again (thanks to @mahge's fixes) and properly documented.

You can get it in the latest nightly build and in the forthcoming 1.20.0 release.

casella commented 2 years ago

The different ways you can use previous simulation results for initialization are now explained properly in the User's Guide.