- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » File Operations in Interactive Shell
File Operations in Interactive Shell
File Operations in Interactive Shell
Hi,
I have been using OpenModelica to build a reactor model. I am simulating the system using a .mos script which loads and simulates the model. Could anyone please let me know if there are any scripting commands available to do file operations like opening an excel file, copying or extracting data from it?
Thank You.
Re: File Operations in Interactive Shell
No. We don't have such facilities.
If you have such an external program that can extract data from an Excel file,
you could use
Code:
system("program excel_file");
Can you tell us a bit more why do you need such facilities in the scripting environment?
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
Hi,
Thank you for the reply. This question is part of a bigger problem that I am trying to solve. Please find a description of the problem below:
I am using DASSL solver to solve a DAE system in OpenModelica. Until now, I was using 'Simulation Setup' to modify the solver options like step size etc but it had little effect on the system because of the events involved. So, we are trying to give increasing time intervals to the solver, starting from a very low value (~ 1e-10). By time interval, I mean the difference between 'Start Time' and 'Stop Time' and not the step size that solver considers. So, I was thinking of a script which might look similar to the one below:
loadFile("name_of_model.mo");
my_start_time := 0;
my_stop_time := 1e-10;
for i in 1:33 loop
simulate(name_of_model, startTime = my_start_time, stopTime = my_stop_time, 'other options');
my_start_time := my_stop_time;
my_stop_time := 2*my_stop_time; // After every loop the start time is equated to the stop time of previous loop and the stop time is multiplied by a factor of 2 //
end for;
In essence, I am trying to call the solver multiple times, store the result after each loop and use this result as the initial condition for the next loop(the way it can be done in FORTRAN). So, if at all I need to use this methodology then I need an efficient way of storing the results and handling them.
This was an alternative that I could think of. Please let me know if there are any efficient ways of doing the same in OpenModelica, and if this methodology would work.
Thank You.
Re: File Operations in Interactive Shell
You could do:
Code:
buildModel(Model);
to get the Model.exe executable and then use from some external program of your choice or using system in .mos:
Code:
Model.exe -override var1=val1,var2=val2,startTime=x,stopTime=y -output oval1,oval2 -r unique_file_name.mat
if you don't want the .mat file you could use -noemit
Alternatively you could generate and FMU and then use your own dassl.
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
Hi,
I have been trying to implement the first methodology that you suggested using a system call in a .mos file. But, the script returns a value of 127 which I guess represents failure, as success returns 0(according to OpenModelica.scripting documentation). I have attached a snippet of the script that I am using (assume that the model I am building is BouncingBall). My PC runs on Ubuntu. So, in the system command, I can't write BouncingBall.exe.
Also, in the second methodology that you had suggested, by own DASSL, do you mean from some other software or program or is there a way to access the solver directly from the scripting environment?
Code:
loadFile("/.../BouncingBall.mo");
buildModel(Bouncing Ball);
system("BouncingBall -override startTime = 2, stopTime = 10");
Thank You. Any help on this front would be greatly appreciated.
Re: File Operations in Interactive Shell
Use getErrorString() after the loadFile and buildModel commands. Use ./BouncingBall as I expect your PATH does not contain ./ in it.
- sjoelund.se
- 1700 Posts
Re: File Operations in Interactive Shell
This is wrong:
Code:
system("BouncingBall -override startTime = 2, stopTime = 10");
It should be WITH NO spaces:
Code:
system("BouncingBall -override startTime=2, stopTime=10");
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
You also seem to have a space here:
Code:
buildModel(Bouncing Ball);
should be:
Code:
buildModel(BouncingBall);
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
Hi,
The spaces in code were a typo error. I apologize for that. Nevertheless, the problem arose because of not using './' before the name of executable. The script works fine now. I have been trying to save the output as a csv file instead of a mat file, so that I could open it in Excel. Could you suggest a way to do that?
Thank You.
Re: File Operations in Interactive Shell
I guess it should work with: ./Model -override outputFormat=csv
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
Yeah that works. But a couple of issues that I am facing here are:
1. If I call the system command in a for loop wherein I manipulate the values of startTime, stopTime and other variables, depending on the progression of loop, I don't understand how to store the values generated from various loops in a single output file. Could you please shed more light on that?
2. When overriding the start or stop time, if I use a variable to assign the start or stop time, the script generates an output csv file but the system doesn't simulate. The command looks like this:
Code:
system("./BouncingBall -override stopTime=myStop, outputFormat = csv");
In this case, myStop := 3 assignment has been done before the system command. If I instead use stopTime=3, the system seems to simulate.
Thank You.
Re: File Operations in Interactive Shell
Because you need to append the variable as string:
Code:
system("./BouncingBall -override stopTime=" + String(myStop) + ",outputFormat = csv");
Also, you again have a space after comma and before outputFormat. Be careful.
You can generate new result files using:
Code:
system("./BouncingBall -override stopTime=" + String(myStop) + ",outputFormat = csv -r file_" + String(i) + ".csv");
where i is an index variable for example.
At the end you could concatenate the csv files i guess.
- adrpo
- 885 Posts
Re: File Operations in Interactive Shell
Hi,
Yeah I followed the code that you had suggested and it works fine. There are a few last minute issues that I am facing. Please take a look at the following code and kindly address the issues that follow.
Code:
loadFile("/.../BouncingBall.mo");
myStart := 0;
myStop := 2;
tempHt := 10;
tempV := 0;
buildModel(BouncingBall);
for i in 1:3 loop
system("./BouncingBall -override h="+String(tempHt)+",v="+String(tempV)+",startTime="+String(myStart)+",stopTime="+String(myStop)+",outputFormat=csv,numberOfIntervals=10 -r sample_out_"+String(i)+".csv");
tempHt := val(h,myStop);
tempV := val(v,myStop);
myStart := myStop;
myStop := 2*myStop;
end for;
(Excuse any typos like extra spaces in the code. I have ensured that they are in place.)
Here, tempHt and tempV are the variables that are used to store the values of velocity and height at the end of every iteration, to be used as initial values for the next iteration.
Issue1: I am unable to use the val() function if my output format is csv(works with mat and plt). So, would you suggest an alternative way of storing the values of variables at the last time instant after every iteration? I don't need the complete output vectors which is given by the -output option.
Issue2: I am unable to manipulate the number of intervals. I am using this option as 'numberOfIntervals=10'. Kindly suggest how this needs to be done.
Regards,
Bhargava
Re: File Operations in Interactive Shell
Hi,
I have overcome the issues stated in the previous post, by sticking to .mat format and, converting the .mat files to a consolidated .csv file using Octave. But, when I try to run the script that simulates the system, I get an error saying 'Internal error BackendDAE.adjacencyRowEnhanced failed for eqn:' and it points to the code in algorithm section of my master model in which two component models have been connected. I have also attached a screenshot of the error statement. Kindly let me know what this error means and, if there is any possible way out.
Regards,
Bhargava
Re: File Operations in Interactive Shell
This seems like a bug in the OpenModelica compiler. If your model is public you could create a ticket in our Trac and attach it there:
https://trac.openmodelica.org/OpenModelica (New Ticket)
If the model is not public you can send it to us privately via OpenModelica@ida.liu.se and we'll only use it for
debugging and then delete it.
If you cannot send your model then maybe you can do a minimal model which has this issue which is public.
- adrpo
- 885 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » File Operations in Interactive Shell