I am trying to automate my openModelica simulations. My three goals are as follows:
- Run a for loop to run the different simulations
- Change the different parameters needed for each simulation with each parameter is described an array
- Save the output of the siumlation as a csv file, but with only a few parameters selected (Filesize would be too large if all parameters were selected)
This is the current OMS I have:
loadFile("scenario_1_fists_paper_microgrid_no_bess_no_solar_l2_l3_no_ev_charging.mo"); getErrorString();
parameter Real BESSCapacity[3] = {1620000000, 3240000000, 16200000000}; getErrorString();
parameter String BESSFunction[3] = {"demandResponse", "demandResponse", "peakShaving"}; getErrorString();
equation
for i in 1:4 loop
Buildings.Electrical.AC.ThreePhasesBalanced.Storage.Battery BESS(EMax = BESSCapacity[i], SOC_start = 0.5, pf = 0.9) annotation(
Placement(visible = true, transformation(origin = {-10, -68}, extent = {{10, -10}, {-10, 10}}, rotation = 90)));
Buildings.Utilities.IO.Python_3_8.Real_Real level_2_ev_charger_random_generator(functionName = BESSFunction[i] ,moduleName = "pythonFile",nDblRea = 1, nDblWri = 1, samplePeriod = 900) annotation(
Placement(visible = true, transformation(origin = {89, -51}, extent = {{9, -9}, {-9, 9}}, rotation = 0)));
simulate(conf_paper_microgrid, fileNamePrefix = "run_1", outputFormat = "csv"); getErrorString();
end for;
I am not sure if for loops in openmodelica script are the same as in openmodelica. Also, how to I save the simulation’s csv file with only select parameters from the simualtion (building_load.P, BESS.P, etc.) I am new to openmodelica scripting, so apologies if some of these questions are trivial. Thank you for the help.
Edit suggested from comments:
loadFile("scenario_1_fists_paper_microgrid_no_bess_no_solar_l2_l3_no_ev_charging.mo"); getErrorString();
parameter Real BESSCapacity[3] = {1620000000, 3240000000, 16200000000}; getErrorString();
parameter String BESSFunction[3] = {"demandResponse", "demandResponse", "peakShaving"}; getErrorString();
equation
for i in {1, 2, 3} loop
\ Do something for parameter sweep
simulate(conf_paper_microgrid, fileNamePrefix = "run_1", outputFormat = "csv"); getErrorString();
end for;
6
This can be done in two ways, using loadString or setComponentModifierValue:
loadFile("scenario_1_fists_paper_microgrid_no_bess_no_solar_l2_l3_no_ev_charging.mo"); getErrorString();
BESSCapacity := {1620000000, 3240000000, 16200000000}; getErrorString();
BESSFunction := {"demandResponse", "demandResponse", "peakShaving"}; getErrorString();
// using loadString to build a model on the fly
loadFile("scenario_1_fists_paper_microgrid_no_bess_no_solar_l2_l3_no_ev_charging.mo"); getErrorString();
for i in 1:4 loop
loadString(
"model conf_paper_microgrid_" + String(i) + "
extends conf_paper_microgrid(
BESS(EMax = " + String(BESSCapacity[i]) + ", SOC_start = 0.5, pf = 0.9),
level_2_ev_charger_random_generator(functionName = " + BESSFunction[i] + ", moduleName = "pythonFile", nDblRea = 1, nDblWri = 1, samplePeriod = 900));
end conf_paper_microgrid_" + String(i) + ";"); getErrorString();
simulate(stringTypeName("conf_paper_microgrid" + String(i)), fileNamePrefix = "run_" + String(i), outputFormat = "csv"); getErrorString();
end for;
/* second approach, use setComponentModifierValue, not sure if it works
for i in 1:4 loop
setComponentModifierValue(conf_paper_microgrid, BESS, $Code((EMax = BESSCapacity[i], SOC_start = 0.5, pf = 0.9))); getErrorString();
setComponentModifierValue(conf_paper_microgrid, level_2_ev_charger_random_generator, $Code((functionName = BESSFunction[i], moduleName = "pythonFile", nDblRea = 1, nDblWri = 1, samplePeriod = 900));
simulate(conf_paper_microgrid, fileNamePrefix = "run_" + String(i), outputFormat = "csv"); getErrorString();
end for;
*/
6