- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Best scripting workflow for building...
Best scripting workflow for building and executing models
Best scripting workflow for building and executing models
Good day.
I am new to OM and would like to know what workflow to follow to build and execute models through scripting.
I use Ubuntu and my workflow in other languages is to code in Vim and then have another tab open in the terminal where I run the script. Is there a similar way in OM?
Lets say I want to run the model given in the tutorial:
Code:
model HelloWorld "A simple equation"
Real x(start=1);
parameter Real a = -1;
equation
der(x)= a*x;
end HelloWorld;
simulate(HelloWorld, stopTime = 2)
plot(x)
Can I put all this code in a file and run the script with some terminal command?
I have tried stuff like
Code:
omc file.oml
and
Code:
omshell file.oml
where the above code is stored in file.oml, to no avail.
Thank you in advance.
Re: Best scripting workflow for building and executing models
See here, session example 3:
https://openmodelica.org/doc/OpenModeli … mmand-line
You can just make a script.mos containing:
Code:
loadModel(Modelica); getErrorString(); // load Modelica if you need it
loadFile("Model.mo"); getErrorString(); // load Model.mo that you edit
simulate(Model); getErrorString(); // simulate the model
plot({x, y}); getErrorString(); // plot any variables you need
then run omc from command line:
Code:
> omc script.mos
- adrpo
- 885 Posts
Re: Best scripting workflow for building and executing models
You can also have the model in the .mos:
script.mos:
Code:
// load the model as string, just make sure you escape " with \" inside the string.
loadString("
model HelloWorld \"A simple equation\"
Real x(start=1);
parameter Real a = -1;
equation
der(x)= a*x;
end HelloWorld;"); getErrorString();
simulate(HelloWorld, stopTime = 2); getErrorString();
plot(x); getErrorString();
Then run omc from command line:
Code:
> omc +d=showStatement script.mos
- adrpo
- 885 Posts
Re: Best scripting workflow for building and executing models
Thank you.
I understand the process, but I am getting the output:
Code:
true
""
true
""
record SimulationResult
resultFile = "",
simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
messages = "Simulation Failed. Model: Model does not exist! Please load it first before simulation.",
timeFrontend = 0.0,
timeBackend = 0.0,
timeSimCode = 0.0,
timeTemplates = 0.0,
timeCompile = 0.0,
timeSimulation = 0.0,
timeTotal = 0.0
end SimulationResult;
""
false
""
I'm interpreting this as Modelica is loaded successfully (First True) and the file Model.mo is loaded successfully (second True), but the simulation can't be run.
The content of my Model.mo file is:
Code:
model HelloWorld "A simple equation"
Real x(start=1);
parameter Real a = -1;
equation
der(x)= a*x;
end HelloWorld;
Is this correct?
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Best scripting workflow for building...