Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit b528b72

Browse files
niklworsOpenModelica-Hudson
authored andcommitted
Fatemeh Davoudi implementation of the symbolic model reduction algorithm
For detailed description see http://www.ep.liu.se/ecp/043/090/ecp09430136.pdf The algorithm is implemented in the cpp runtime in Core/ReduceDAE At the moment for the ranking method the residuen ranking is implement in class Core/ReduceDAE/Ranking.cpp The reduction mehtods cancel terms, linearize terms and substitute terms are implemented in class Core/ReduceDAE/Reduction.cpp The label algorithms for cancel terms, linearize terms and substitute terms are implemented in OMCompiler/Compiler/SimCode/ReduceDAE.mo The method can be used with: setCommandLineOptions("+simCodeTarget=Cpp --labeledReduction --disableExtraLabeling -d=writeToBuffer"); loadFile("your model file"); simulate(yourmodel); added omc c-api communication project to the cpp folder. The c- api is build with the cpp runtime for mingw and gcc. The c-api is used by the reduce dae algorithm to start labeling and reduce terms adapted reduce dae build for cpp runtime build added omc C-API library to cpp runtime added build for C-API library to cpp runtime build fixed includes removed unused files reorganized some code parts added test for c-api moved in LoadCompilerSources the include order for ReduceDAE.mo to the backend part fix in Flags.mo, two Flags used the same id only build reduce dae for mingw merged in cpp template model reduction code generation added missing interface methods fix for compiler errors whem getDimRHS return nothing change formtat ReduceDAE.mo to utf8 moved function getNumContinuousEquations to SimCodeUtil use std chrono instead of boost time library added sundials dependicies to runtimeCPP make target fix for wrong type for timeout in SimSettings fix for wrong reduceDAE includes, if reduceDAE flag is disabled fix for some tests, where der variables are filtered in outputfike fix for missing IDEAReduce include in generated code Belonging to [master]: - #1973
1 parent fb52840 commit b528b72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5451
-224
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,43 @@ external "builtin";
25812581
annotation(preferredView="text");
25822582
end buildModel;
25832583

2584+
function buildLabel "builds Lable."
2585+
input TypeName className "the class that should be built";
2586+
input Real startTime = 0.0 "the start time of the simulation. <default> = 0.0";
2587+
input Real stopTime = 1.0 "the stop time of the simulation. <default> = 1.0";
2588+
input Integer numberOfIntervals = 500 "number of intervals in the result file. <default> = 500";
2589+
input Real tolerance = 1e-6 "tolerance used by the integration method. <default> = 1e-6";
2590+
input String method = "dassl" "integration method used for simulation. <default> = dassl";
2591+
input String fileNamePrefix = "" "fileNamePrefix. <default> = \"\"";
2592+
input String options = "" "options. <default> = \"\"";
2593+
input String outputFormat = "mat" "Format for the result file. <default> = \"mat\"";
2594+
input String variableFilter = ".*" "Filter for variables that should store in result file. <default> = \".*\"";
2595+
input String cflags = "" "cflags. <default> = \"\"";
2596+
input String simflags = "" "simflags. <default> = \"\"";
2597+
output String[2] buildModelResults;
2598+
external "builtin";
2599+
annotation(preferredView="text");
2600+
end buildLabel;
2601+
2602+
function reduceTerms "reduce terms."
2603+
input TypeName className "the class that should be built";
2604+
input Real startTime = 0.0 "the start time of the simulation. <default> = 0.0";
2605+
input Real stopTime = 1.0 "the stop time of the simulation. <default> = 1.0";
2606+
input Integer numberOfIntervals = 500 "number of intervals in the result file. <default> = 500";
2607+
input Real tolerance = 1e-6 "tolerance used by the integration method. <default> = 1e-6";
2608+
input String method = "dassl" "integration method used for simulation. <default> = dassl";
2609+
input String fileNamePrefix = "" "fileNamePrefix. <default> = \"\"";
2610+
input String options = "" "options. <default> = \"\"";
2611+
input String outputFormat = "mat" "Format for the result file. <default> = \"mat\"";
2612+
input String variableFilter = ".*" "Filter for variables that should store in result file. <default> = \".*\"";
2613+
input String cflags = "" "cflags. <default> = \"\"";
2614+
input String simflags = "" "simflags. <default> = \"\"";
2615+
input String labelstoCancel="";
2616+
output String[2] buildModelResults;
2617+
external "builtin";
2618+
annotation(preferredView="text");
2619+
end reduceTerms;
2620+
25842621
function moveClass
25852622
"Moves a class up or down depending on the given offset, where a positive
25862623
offset moves the class down and a negative offset up. The offset is truncated

Compiler/Script/CevalScriptBackend.mo

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,28 @@ algorithm
14161416
then
14171417
(cache,ValuesUtil.makeArray({Values.STRING(executable),Values.STRING(initfilename)}),st);
14181418

1419+
case (cache,env,"buildLabel",vals,st,_)
1420+
equation
1421+
Flags.setConfigBool(Flags.GENERATE_LABELED_SIMCODE, true);
1422+
//Flags.set(Flags.WRITE_TO_BUFFER,true);
1423+
List.map_0(ClockIndexes.buildModelClocks,System.realtimeClear);
1424+
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMULATE_TOTAL);
1425+
(cache,st,compileDir,executable,_,_,initfilename,_,_) = buildModel(cache,env, vals, st, msg);
1426+
then
1427+
(cache,ValuesUtil.makeArray({Values.STRING(executable),Values.STRING(initfilename)}),st);
1428+
1429+
case (cache,env,"reduceTerms",vals,st,_)
1430+
equation
1431+
Flags.setConfigBool(Flags.REDUCE_TERMS, true);
1432+
// Flags.setConfigBool(Flags.DISABLE_EXTRA_LABELING, true);
1433+
Flags.setConfigBool(Flags.GENERATE_LABELED_SIMCODE, false);
1434+
_=Flags.disableDebug(Flags.WRITE_TO_BUFFER);
1435+
List.map_0(ClockIndexes.buildModelClocks,System.realtimeClear);
1436+
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMULATE_TOTAL);
1437+
1438+
(cache,st,compileDir,executable,_,_,initfilename,_,_) = buildLabeledModel(cache,env, vals, st, msg);
1439+
then
1440+
(cache,ValuesUtil.makeArray({Values.STRING(executable),Values.STRING(initfilename)}),st);
14191441
case(cache,env,"buildOpenTURNSInterface",vals,st,_)
14201442
equation
14211443
(cache,scriptFile,st) = buildOpenTURNSInterface(cache,env,vals,st,msg);
@@ -3139,6 +3161,72 @@ algorithm
31393161
end match;
31403162
end translateModel;
31413163

3164+
protected function translateLabeledModel " author: Fatima
3165+
translates a labeled model into cpp code and writes also a makefile"
3166+
input FCore.Cache inCache;
3167+
input FCore.Graph inEnv;
3168+
input Absyn.Path className "path for the model";
3169+
input GlobalScript.SymbolTable inInteractiveSymbolTable;
3170+
input String inFileNamePrefix;
3171+
input Boolean addDummy "if true, add a dummy state";
3172+
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
3173+
input list<Absyn.NamedArg> inLabelstoCancel;
3174+
output FCore.Cache outCache;
3175+
output GlobalScript.SymbolTable outInteractiveSymbolTable;
3176+
output BackendDAE.BackendDAE outBackendDAE;
3177+
output list<String> outStringLst;
3178+
output String outFileDir;
3179+
output list<tuple<String,Values.Value>> resultValues;
3180+
algorithm
3181+
(outCache,outInteractiveSymbolTable,outBackendDAE,outStringLst,outFileDir,resultValues):=
3182+
match (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix,addDummy,inSimSettingsOpt,inLabelstoCancel)
3183+
local
3184+
FCore.Cache cache;
3185+
FCore.Graph env;
3186+
BackendDAE.BackendDAE indexed_dlow;
3187+
GlobalScript.SymbolTable st;
3188+
list<String> libs;
3189+
String file_dir, fileNamePrefix;
3190+
Absyn.Program p;
3191+
Flags.Flags flags;
3192+
String commandLineOptions;
3193+
list<String> args;
3194+
Boolean haveAnnotation;
3195+
list<Absyn.NamedArg> labelstoCancel;
3196+
3197+
case (cache,env,_,st as GlobalScript.SYMBOLTABLE(),fileNamePrefix,_,_,labelstoCancel)
3198+
algorithm
3199+
3200+
if Config.ignoreCommandLineOptionsAnnotation() then
3201+
(cache, st, indexed_dlow, libs, file_dir, resultValues) :=
3202+
SimCodeMain.translateModel(cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
3203+
else
3204+
// read the __OpenModelica_commandLineOptions
3205+
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, st.ast, Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
3206+
haveAnnotation := boolNot(stringEq(commandLineOptions, ""));
3207+
// backup the flags.
3208+
flags := if haveAnnotation then Flags.backupFlags() else Flags.loadFlags();
3209+
try
3210+
// apply if there are any new flags
3211+
if haveAnnotation then
3212+
args := System.strtok(commandLineOptions, " ");
3213+
_ := Flags.readArgs(args);
3214+
end if;
3215+
3216+
(cache, st, indexed_dlow, libs, file_dir, resultValues) :=
3217+
SimCodeMain.translateModel(cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
3218+
// reset to the original flags
3219+
Flags.saveFlags(flags);
3220+
else
3221+
Flags.saveFlags(flags);
3222+
fail();
3223+
end try;
3224+
end if;
3225+
then
3226+
(cache,st,indexed_dlow,libs,file_dir,resultValues);
3227+
3228+
end match;
3229+
end translateLabeledModel;
31423230
/*protected function translateModelCPP " author: x02lucpo
31433231
translates a model into cpp code and writes also a makefile"
31443232
input FCore.Cache inCache;
@@ -4821,6 +4909,116 @@ algorithm
48214909
end matchcontinue;
48224910
end buildModel;
48234911

4912+
protected function buildLabeledModel "translates and builds the labeled model by running compiler script on the generated makefile"
4913+
input FCore.Cache inCache;
4914+
input FCore.Graph inEnv;
4915+
input list<Values.Value> inValues;
4916+
input GlobalScript.SymbolTable inInteractiveSymbolTable;
4917+
input Absyn.Msg inMsg;
4918+
output FCore.Cache outCache;
4919+
output GlobalScript.SymbolTable outInteractiveSymbolTable3;
4920+
output String compileDir;
4921+
output String outString1 "className";
4922+
output String outString2 "method";
4923+
output String outputFormat_str;
4924+
output String outInitFileName "initFileName";
4925+
output String outSimFlags;
4926+
output list<tuple<String,Values.Value>> resultValues;
4927+
protected
4928+
Values.Value labelstoCancel;
4929+
list<Absyn.NamedArg> named_args1={},named_args2={};
4930+
list<Values.Value> valuesList;
4931+
String labelstoCancelStr;
4932+
//Absyn.NamedArg named_arg;
4933+
algorithm
4934+
if listLength(inValues)==13 then
4935+
labelstoCancel:=listGet(inValues,13);
4936+
valuesList:=listDelete(inValues,13);
4937+
end if;
4938+
labelstoCancelStr:=System.stringReplace(ValuesUtil.valString(labelstoCancel), "\"", "");
4939+
4940+
named_args2:=Absyn.NAMEDARG("labelstoCancel", Absyn.STRING(labelstoCancelStr))::named_args1;
4941+
4942+
(outCache,outInteractiveSymbolTable3,compileDir,outString1,outString2,outputFormat_str,outInitFileName,outSimFlags,resultValues):=
4943+
matchcontinue (inCache,inEnv,valuesList,inInteractiveSymbolTable,inMsg)
4944+
local
4945+
GlobalScript.SymbolTable st,st_1,st2;
4946+
BackendDAE.BackendDAE indexed_dlow_1;
4947+
list<String> libs;
4948+
String file_dir,init_filename,method_str,filenameprefix,exeFile,s3,simflags,optionStr,filterStr,cflagsStr;
4949+
Absyn.Path classname;
4950+
Absyn.Program p;
4951+
Absyn.Class cdef;
4952+
Real edit,build,globalEdit,globalBuild,timeCompile;
4953+
FCore.Graph env;
4954+
SimCode.SimulationSettings simSettings;
4955+
Values.Value starttime,stoptime,interval,tolerance,method,options,outputFormat,variableFilter;
4956+
list<Values.Value> vals, values;
4957+
Absyn.Msg msg;
4958+
FCore.Cache cache;
4959+
Boolean existFile;
4960+
4961+
// compile the model
4962+
case (cache,env,vals,st,msg)
4963+
equation
4964+
4965+
// buildModel expects these arguments:
4966+
// className, startTime, stopTime, numberOfIntervals, tolerance, method, fileNamePrefix,
4967+
// options, outputFormat, variableFilter, cflags, simflags
4968+
values = vals;
4969+
4970+
(Values.CODE(Absyn.C_TYPENAME(classname)),vals) = getListFirstShowError(vals, "while retreaving the className (1 arg) from the buildModel arguments");
4971+
(_,vals) = getListFirstShowError(vals, "while retreaving the startTime (2 arg) from the buildModel arguments");
4972+
(_,vals) = getListFirstShowError(vals, "while retreaving the stopTime (3 arg) from the buildModel arguments");
4973+
(_,vals) = getListFirstShowError(vals, "while retreaving the numberOfIntervals (4 arg) from the buildModel arguments");
4974+
(_,vals) = getListFirstShowError(vals, "while retreaving the tolerance (5 arg) from the buildModel arguments");
4975+
(_,vals) = getListFirstShowError(vals, "while retreaving the method (6 arg) from the buildModel arguments");
4976+
(Values.STRING(filenameprefix),vals) = getListFirstShowError(vals, "while retreaving the fileNamePrefix (7 arg) from the buildModel arguments");
4977+
(_,vals) = getListFirstShowError(vals, "while retreaving the options (8 arg) from the buildModel arguments");
4978+
(_,vals) = getListFirstShowError(vals, "while retreaving the outputFormat (9 arg) from the buildModel arguments");
4979+
(_,vals) = getListFirstShowError(vals, "while retreaving the variableFilter (10 arg) from the buildModel arguments");
4980+
(_,vals) = getListFirstShowError(vals, "while retreaving the cflags (11 arg) from the buildModel arguments");
4981+
(Values.STRING(simflags),vals) = getListFirstShowError(vals, "while retreaving the simflags (12 arg) from the buildModel arguments");
4982+
4983+
Error.clearMessages() "Clear messages";
4984+
compileDir = System.pwd() + System.pathDelimiter();
4985+
(cache,simSettings) = calculateSimulationSettings(cache, env, values, st, msg);
4986+
4987+
SimCode.SIMULATION_SETTINGS(method = method_str,options=optionStr, outputFormat = outputFormat_str,variableFilter=filterStr,cflags=cflagsStr)
4988+
= simSettings;
4989+
4990+
(cache,st as GlobalScript.SYMBOLTABLE(),_,libs,file_dir,resultValues) = translateLabeledModel(cache,env, classname, st, filenameprefix,true, SOME(simSettings),named_args2);
4991+
4992+
//cname_str = Absyn.pathString(classname);
4993+
//SimCodeUtil.generateInitData(indexed_dlow_1, classname, filenameprefix, init_filename,
4994+
// starttime_r, stoptime_r, interval_r, tolerance_r, method_str,options_str,outputFormat_str);
4995+
4996+
System.realtimeTick(ClockIndexes.RT_CLOCK_BUILD_MODEL);
4997+
init_filename = filenameprefix + "_init.xml"; //a hack ? should be at one place somewhere
4998+
//win1 = getWithinStatement(classname);
4999+
5000+
if Flags.isSet(Flags.DYN_LOAD) then
5001+
Debug.traceln("buildModel: about to compile model " + filenameprefix + ", " + file_dir);
5002+
end if;
5003+
CevalScript.compileModel(filenameprefix, libs);
5004+
print("after compilemodel in buildLabeledModel \n");
5005+
if Flags.isSet(Flags.DYN_LOAD) then
5006+
Debug.trace("buildModel: Compiling done.\n");
5007+
end if;
5008+
// p = setBuildTime(p,classname);
5009+
st2 = st;// Interactive.replaceSymbolTableProgram(st,p);
5010+
timeCompile = System.realtimeTock(ClockIndexes.RT_CLOCK_BUILD_MODEL);
5011+
resultValues = ("timeCompile",Values.REAL(timeCompile)) :: resultValues;
5012+
then
5013+
(cache,st2,compileDir,filenameprefix,method_str,outputFormat_str,init_filename,simflags,resultValues);
5014+
5015+
// failure
5016+
else
5017+
equation
5018+
Error.assertion(listLength(valuesList) == 12, "buildModel failure, length = " + intString(listLength(valuesList)), Absyn.dummyInfo);
5019+
then fail();
5020+
end matchcontinue;
5021+
end buildLabeledModel;
48245022
protected function createSimulationResultFromcallModelExecutable
48255023
"This function calls the compiled simulation executable."
48265024
input Integer callRet;

0 commit comments

Comments
 (0)