Repository navigation
ModelicaSystem.simulate() does not handle 'outputFormat' and 'variableFilter' options correctly. #151
Description
Overview
The argument '-override' of (model).exe has the following syntax
(model).exe -override=startTime=0,stopTime=5,stepSize=0.01,tolerance=1e-06,solver=dassl,mdl.param1=0.5,mdl.param2=2.0,outputFormat=csv,variableFilter=. *
ModelicaSystems.simulate() will give the '-override' option if parameters or simulation options are set using setParameters() or setSimulationOptions().
In this case, 'outputFormat' and 'variableFilter' cannot be specified.
To specify 'outputFormat' and 'variableFilter', I added options to the 'simflags' argument as below, but after using setParameters() or/and setSimulationOptions(), '-override' is double argument and simulate() does not work properly.
mod.simulate(simflags='-override=startTime=0,stopTime=5,stepSize=0.01,tolerance=1e-06,solver=dassl,outputFormat=csv,variableFilter=.*')
Operating Environment
Windows 10 Enterprise ver 20H2
OpenModelica 1.16.2 MSL3.2.3
Python 3.9.6
OMPython 3.3.0
Tentative measures
Do not use setParameters(), setSimulationOptions(), and specify all 'override' options using simFlags.
simulate() improvement suggestions
Add arguments to specify 'outputFormat' and 'variableFilter' and add code to append them to the '-override' statement.
The tested 'simulate()' modifications are as follows.
--- C:/venv39/jupyter-lab/Lib/site-packages/OMPython/__init__.py.org Thu Mar 17 15:14:01 2022
+++ C:/venv39/jupyter-lab/Lib/site-packages/OMPython/__init__patch.py Fri Apr 1 18:02:55 2022
@@ -1197,13 +1197,30 @@
return ([self.optimizeOptions.get(x,"NotExist") for x in names])
# to simulate or re-simulate model
- def simulate(self,resultfile=None,simflags=None): # 11
+ ## override option add
+ # -def simulate(self,resultfile=None,simflags=None): # 11
+ def simulate(self,resultfile=None,simflags=None,overrideaux=None): # 11
"""
This method simulates model according to the simulation options.
+
+ Parameters
+ ----------
+ resultfile : str or None
+ Output file name
+
+ simflags : str or None
+ Other simulation options not '-override' parameters
+
+ overrideaux : str or None
+ Specify 'outputFormat' and 'variableFilter
+
usage
+ -----
>>> simulate()
>>> simulate(resultfile="a.mat")
>>> simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10) set runtime simulation flags
+ >>> simulate(simflags="-noEventEmit -noRestart" ,overrideaux="outputFormat=csv,variableFilter=.*") # set runtime simulation flags
"""
if(resultfile is None):
r=""
@@ -1225,6 +1242,13 @@
override =" -override=" + values1
else:
override =""
+
+ # add override flags not parameters or simulation options
+ if overrideaux:
+ if override:
+ override = override + "," + overrideaux
+ else:
+ override = " -override=" + overrideaux
if (self.inputFlag): # if model has input quantities
for i in self.inputlist:
Activity
adeas31 commentedon Apr 14, 2022
You should use
ModelicaSystem.resultfileto set a new output format. As forvariableFilter, I am not sure how to set this.@arun3688 is it possible to set this in ModelicaSystem?
@sjoelund is it possible to set
variableFiltervia simulation flags?arun3688 commentedon Apr 14, 2022
@Nobby-n To set the result file format when using ModelicaSystem you can set the result file via simulate API, something like below
You cannot use
variable filterwith ModelicaSystem class, but i will add this featureNobby-n commentedon Apr 16, 2022
@adeas31 san, @arun3688 san
Thank you for your comment.
I understand how to use 'simulate ()' as in your comment.
I think 'method-3' is a good way to use all the '-override' options.
However, in this case I cannot use 'setParameters ()' and 'setSimulationOptions ()' at the same time to specify 'outputFormat' and 'variableFilter'.
I would be happy if 'simulate ()' could be improved in the future.
arun3688 commentedon Apr 18, 2022
@Nobby-n It is not possible to use variable filter as a runtime simulation flags, but however you can use the
getSolutionsAPI to filter the results, something like belowNobby-n commentedon Apr 20, 2022
@arun3688 san
I'd confirmed that I can get only the variables I need using
getSolutions()API.One of the reasons I want to use
variableFilterinsimulate()API is that I can reduce the file size of the mat file.For example, in the
Modelica.Fluid.Examples.PumpingSystemincluded in the MSL, I'd confirmed that by specifyingvariableFilteras follows, the variables output to the mat file can be reduced.arun3688 commentedon Jun 15, 2022
@Nobby-n Now the variable filter option is added to
ModelicaSystemconstructor with this commit 2284229, you can use it in following ways ,You can directly use it in
ModelicaSystem()Constructor or you can use it inbuildModel()joewa commentedon Sep 11, 2022
The approach from @Nobby-n applies the customization (variableFilter or overrides in general) when calling the pre-built model executable whereas the current implementation of @arun3688 applies the variableFilter before when calling buildModel.
I think both approaches are interesting! There might be use cases where calling
buildModeleach time ModelicaSystem is instantiated is not efficient, e.g. when only the parameters or the input data changes between different simulation runs.It would be necessary to tell ModelicaSystem not to call
buildModelthen, e.g. by providing the xmlFileName of an existing model.I would appreciate if we can have this in OMPython.
I have already integrated this proposal in my fork of OMPython and I am ready to send a pull request.
What do you think?
arun3688 commentedon Sep 12, 2022
@Nobby-n Please make a PR
Nobby-n commentedon Sep 12, 2022
@joewa, @arun3688
Thank you for comments.
I'd saw @joewa 's fork.
@joewa, Please send a pull request.
I agree your proposal.
joewa commentedon Sep 13, 2022
Please note my pull request #165 @arun3688 @Nobby-n
(I don"t know if you are notified by default)
arun3688 commentedon Sep 14, 2022
@joewa yes your PR is noted
Nobby-n commentedon Sep 14, 2022
@joewa Me too, your pull request is notified.
adeas31 commentedon Dec 2, 2025
Closing this as it is possible to set output format and result filters on ModelicaSystem.