Skip to content

Commit

Permalink
Add getSimulationOptions as typed API call, and call this from OMEdit…
Browse files Browse the repository at this point in the history
…. A default tolerance, startTime, stopTime may be provided to prefer these over OpenModelica defaults if the experiment annotation is not present

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17877 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 24, 2013
1 parent da38ee7 commit 2beb8d7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/OpenTURNS.mo
Expand Up @@ -93,7 +93,7 @@ algorithm

simSettings := CevalScript.convertSimulationOptionsToSimCode(
CevalScript.buildSimulationOptionsFromModelExperimentAnnotation(
Interactive.setSymbolTableAST(GlobalScript.emptySymboltable,inProgram),inPath,fileNamePrefix)
Interactive.setSymbolTableAST(GlobalScript.emptySymboltable,inProgram),inPath,fileNamePrefix,NONE())
);
// correlation matrix form (vector of records) currently not supported by OpenModelica backend, remove it .
//print("enter dae:");
Expand Down
14 changes: 14 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2731,6 +2731,20 @@ function isExperiment "An experiment is defined as having annotation Experiment(
external "builtin";
end isExperiment;

function getSimulationOptions
input TypeName name;
input Real defaultStartTime := 0.0;
input Real defaultStopTime := 1.0;
input Real defaultTolerance := 1e-6;
output Real startTime;
output Real stopTime;
output Real tolerance;
external "builtin";
annotation(Documentation(info="<html>
<p>Returns the startTime, stopTime, interval and tolerance based on the experiment annotation.</p>
</html>"));
end getSimulationOptions;

function classAnnotationExists "Check if annotation exists"
input TypeName className;
input TypeName annotationName;
Expand Down
31 changes: 25 additions & 6 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -111,6 +111,7 @@ protected import SCodeSimplify;
protected import SimCodeMain;
protected import System;
protected import Static;
protected import StaticScript;
protected import SCode;
protected import SCodeUtil;
protected import Settings;
Expand Down Expand Up @@ -427,18 +428,19 @@ public function buildSimulationOptionsFromModelExperimentAnnotation
input GlobalScript.SymbolTable inSymTab;
input Absyn.Path inModelPath;
input String inFileNamePrefix;
input Option<GlobalScript.SimulationOptions> defaultOption;
output GlobalScript.SimulationOptions outSimOpt;
algorithm
outSimOpt := matchcontinue (inSymTab, inModelPath, inFileNamePrefix)
outSimOpt := matchcontinue (inSymTab, inModelPath, inFileNamePrefix, defaultOption)
local
GlobalScript.SimulationOptions defaults, simOpt;
String experimentAnnotationStr;
list<Absyn.NamedArg> named;

// search inside annotation(experiment(...))
case (_, _, _)
case (_, _, _, _)
equation
defaults = setFileNamePrefixInSimulationOptions(defaultSimulationOptions, inFileNamePrefix);
defaults = Util.getOptionOrDefault(defaultOption, setFileNamePrefixInSimulationOptions(defaultSimulationOptions, inFileNamePrefix));

experimentAnnotationStr =
Interactive.getNamedAnnotation(
Expand All @@ -464,11 +466,10 @@ algorithm
simOpt;

// if we fail, just use the defaults
case (_, _, _)
else
equation
defaults = setFileNamePrefixInSimulationOptions(defaultSimulationOptions, inFileNamePrefix);
then
defaults;
then defaults;
end matchcontinue;
end buildSimulationOptionsFromModelExperimentAnnotation;

Expand Down Expand Up @@ -825,6 +826,7 @@ algorithm
platform,usercflags,senddata,res,workdir,gcc,confcmd,touch_file,uname,filenameprefix,compileDir,libDir,exeDir,configDir,from,to,
gridStr, logXStr, logYStr, x1Str, x2Str, y1Str, y2Str, curveWidthStr, curveStyleStr, legendPosition,scriptFile,logFile, simflags2, outputFile,
systemPath, gccVersion, gd, strlinearizeTime, direction, suffix;
list<DAE.Exp> simOptions;
list<Values.Value> vals;
Absyn.Path path,classpath,className,baseClassPath;
SCode.Program scodeP,sp;
Expand All @@ -835,6 +837,9 @@ algorithm
list<GlobalScript.InstantiatedClass> ic,ic_1;
list<GlobalScript.Variable> iv;
list<GlobalScript.CompiledCFunction> cf;
GlobalScript.SimulationOptions simOpt;
Real startTime,stopTime,tolerance;
DAE.Exp startTimeExp,stopTimeExp,toleranceExp;
DAE.Type tp;
Absyn.Class absynClass;
Absyn.ClassDef cdef;
Expand Down Expand Up @@ -2220,6 +2225,20 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache,env,"getSimulationOptions",{Values.CODE(Absyn.C_TYPENAME(classpath)),Values.REAL(startTime),Values.REAL(stopTime),Values.REAL(tolerance)},st as GlobalScript.SYMBOLTABLE(ast = p),_)
equation
cr_1 = Absyn.pathToCref(classpath);
// ignore the name of the model
ErrorExt.setCheckpoint("getSimulationOptions");
simOpt = GlobalScript.SIMULATION_OPTIONS(DAE.RCONST(startTime),DAE.RCONST(stopTime),DAE.ICONST(0),DAE.RCONST(0.0),DAE.RCONST(tolerance),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.BCONST(false),DAE.SCONST(""),DAE.SCONST(""));
ErrorExt.rollBack("getSimulationOptions");
(_, _::startTimeExp::stopTimeExp::_::toleranceExp::_) = StaticScript.getSimulationArguments(Env.emptyCache(), {},{Absyn.CREF(cr_1)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo,SOME(simOpt));
startTime = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,startTimeExp,Values.REAL(startTime)));
stopTime = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,stopTimeExp,Values.REAL(stopTime)));
tolerance = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,toleranceExp,Values.REAL(tolerance)));
then
(cache,Values.TUPLE({Values.REAL(startTime),Values.REAL(stopTime),Values.REAL(tolerance)}),st);

case (cache,env,"classAnnotationExists",{Values.CODE(Absyn.C_TYPENAME(classpath)),Values.CODE(Absyn.C_TYPENAME(path))},st as GlobalScript.SYMBOLTABLE(ast=p),_)
equation
b = Interactive.getNamedAnnotation(classpath, p, path, SOME(false), Util.isSome);
Expand Down
70 changes: 0 additions & 70 deletions Compiler/Script/Interactive.mo
Expand Up @@ -1658,22 +1658,6 @@ algorithm
then
(resstr,st);

case (istmts, st as GlobalScript.SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getSimulationOptions");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
modelpath = Absyn.crefToPath(cr);
ErrorExt.setCheckpoint("getSimulationOptions");
// ignore the name of the model
(_, _::simOptions) = StaticScript.getSimulationArguments(Env.emptyCache(), {},{Absyn.CREF(cr)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo);
resstr =
"{" +&
ExpressionDump.printExpListStr(simOptions) +&
"} /* startTime, stopTime, numberOfIntervals, tolerance, method, fileNamePrefix, noClean, options, outputFormat */";
ErrorExt.rollBack("getSimulationOptions");
then
(resstr,st);

case (istmts, st as GlobalScript.SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "getNamedAnnotation");
Expand Down Expand Up @@ -2188,60 +2172,6 @@ algorithm
then
(resstr,st);

// builds model and writes results to buffer
case (istmts, st)
equation
matchApiFunction(istmts, "writeToBuffer");
{Absyn.CREF(componentRef = cr)} = getApiFunctionArgs(istmts);
_ = Flags.enableDebug(Flags.WRITE_TO_BUFFER);
(cache,simOptions) = StaticScript.getSimulationArguments(Env.emptyCache(),{},{Absyn.CREF(cr)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo);
(_,_,_) = CevalScript.ceval(cache,{},DAE.CALL(Absyn.IDENT("buildModel"),simOptions,DAE.callAttrBuiltinOther),true,SOME(st),Absyn.NO_MSG(),0);
then
("true",st);

//starts label DAE algorithm
case (istmts, st)
equation
matchApiFunction(istmts, "generateLabeledSimCode");
{Absyn.CREF(componentRef = cr),Absyn.STRING(value = reductionMethod), Absyn.ARRAY(arrayExp = exp_list)} = getApiFunctionArgs(istmts);
Flags.setConfigBool(Flags.GENERATE_LABELED_SIMCODE,true);
Flags.setConfigString(Flags.REDUCTION_METHOD,reductionMethod);
// (cache,simOptions) = StaticScript.getSimulationArguments(Env.emptyCache(),{},{Absyn.CREF(cr)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo);
// (_,_,_) = CevalScript.ceval(cache,{},DAE.CALL(Absyn.IDENT("buildModel"),simOptions,DAE.callAttrBuiltinOther),true,SOME(st),Absyn.NO_MSG());
modelpath = Absyn.crefToPath(cr);
filenameprefix = Absyn.pathLastIdent(modelpath);
(env,st) = buildEnvFromSymboltable(st);
(_,{_,_,_,_,_,DAE.SCONST(method),_,_,_,_,_,_,_,_}) = StaticScript.getSimulationArguments(Env.emptyCache(),{},{Absyn.CREF(cr)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo);
(_,_,_,libs,file_dir,_) = SimCodeMain.translateModel(Env.emptyCache(),env,modelpath,st,filenameprefix,true,NONE(),
Absyn.FUNCTIONARGS({Absyn.CREF(cr), Absyn.ARRAY(exp_list)},{}));
CevalScript.compileModel(filenameprefix,libs,file_dir,method);
then
("true",st);

case (istmts, st)
equation
matchApiFunction(istmts, "generateLabeledSimCode");
then
("No such reduction method!",st);

//starts remove terms from DAE algorithm
case (istmts, st as GlobalScript.SYMBOLTABLE(ast = p))
equation
matchApiFunction(istmts, "reduceTerms");
{Absyn.CREF(componentRef = cr), Absyn.ARRAY(arrayExp = exp_list),Absyn.ARRAY(arrayExp = exp_list2)} = getApiFunctionArgs(istmts);
modelpath = Absyn.crefToPath(cr);
filenameprefix = Absyn.pathLastIdent(modelpath);
(env,st) = buildEnvFromSymboltable(st);
Flags.setConfigBool(Flags.REDUCE_TERMS,true);
Flags.setConfigBool(Flags.GENERATE_LABELED_SIMCODE,false);
_ = Flags.disableDebug(Flags.WRITE_TO_BUFFER);
(_,{_,_,_,_,_,DAE.SCONST(method),_,_,_,_,_,_,_,_}) = StaticScript.getSimulationArguments(Env.emptyCache(),{},{Absyn.CREF(cr)},{},false,SOME(st),Prefix.NOPRE(),Absyn.dummyInfo);
(_,_,_,libs,file_dir,_) = SimCodeMain.translateModel(Env.emptyCache(),env,modelpath,st,filenameprefix,true,NONE(),
Absyn.FUNCTIONARGS({Absyn.CREF(cr), Absyn.ARRAY(exp_list), Absyn.ARRAY(exp_list2)}, {}));
CevalScript.compileModel(filenameprefix,libs,file_dir,method);
then
("true",st);

end matchcontinue;
end evaluateGraphicalApi_dispatch;

Expand Down
21 changes: 11 additions & 10 deletions Compiler/Script/StaticScript.mo
Expand Up @@ -115,11 +115,12 @@ public function getSimulationArguments
input Option<GlobalScript.SymbolTable> inInteractiveInteractiveSymbolTableOption;
input Prefix.Prefix inPrefix;
input Absyn.Info inInfo;
input Option<GlobalScript.SimulationOptions> defaultOption;
output Env.Cache outCache;
output list<DAE.Exp> outSimulationArguments;
algorithm
(outCache, outSimulationArguments) :=
match (inCache, inEnv, inAbsynExpLst, inAbsynNamedArgLst, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, inInfo)
match (inCache, inEnv, inAbsynExpLst, inAbsynNamedArgLst, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, inInfo, defaultOption)
local
Absyn.Exp crexp;
list<Absyn.NamedArg> args;
Expand All @@ -137,15 +138,15 @@ algorithm
Values.Value v;

// fill in defaults
case (cache,env,{crexp},args,impl,SOME(st),pre,info)
case (cache,env,{crexp},args,impl,SOME(st),pre,info,_)
equation
exp = Static.elabCodeExp(crexp,cache,env,DAE.C_TYPENAME(),info);
// We need to force eval in order to get the correct prefix
(cache,v,SOME(st)) = Ceval.ceval(cache,env,exp,true,SOME(st),Absyn.MSG(info),0);
Values.CODE(Absyn.C_TYPENAME(className)) = CevalScript.evalCodeTypeName(v,env);

cname_str = Absyn.pathString(Absyn.unqotePathIdents(className)) "easier than checking if the file system supports UTF-8...";
defaulSimOpt = CevalScript.buildSimulationOptionsFromModelExperimentAnnotation(st, className, cname_str);
defaulSimOpt = CevalScript.buildSimulationOptionsFromModelExperimentAnnotation(st, className, cname_str, defaultOption);

(cache, startTime, stopTime, numberOfIntervals) =
calculateSimulationTimes(inCache, inEnv, inAbsynExpLst, inAbsynNamedArgLst, impl, inInteractiveInteractiveSymbolTableOption, inPrefix, inInfo, defaulSimOpt);
Expand Down Expand Up @@ -267,7 +268,7 @@ public function elabCallInteractive "This function elaborates the functions defi

case (cache,env,Absyn.CREF_IDENT(name = "translateModel"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
then
(cache,Expression.makeBuiltinCall("translateModel",simulationArgs,DAE.T_STRING_DEFAULT),DAE.PROP(DAE.T_STRING_DEFAULT,DAE.C_VAR()),SOME(st));

Expand Down Expand Up @@ -342,42 +343,42 @@ public function elabCallInteractive "This function elaborates the functions defi

case (cache,env,Absyn.CREF_IDENT(name = "buildModel"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
then
(cache,Expression.makeBuiltinCall("buildModel",simulationArgs,DAE.T_UNKNOWN_DEFAULT),
DAE.PROP(DAE.T_ARRAY(DAE.T_STRING_DEFAULT,{DAE.DIM_INTEGER(2)},DAE.emptyTypeSource),DAE.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "buildModelBeast"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
then
(cache,Expression.makeBuiltinCall("buildModelBeast",simulationArgs,DAE.T_UNKNOWN_DEFAULT),
DAE.PROP(DAE.T_ARRAY(DAE.T_STRING_DEFAULT,{DAE.DIM_INTEGER(2)},DAE.emptyTypeSource),DAE.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "simulate"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
recordtype = CevalScript.getSimulationResultType();
then
(cache,Expression.makeBuiltinCall("simulate",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "simulation"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
recordtype = CevalScript.getDrModelicaSimulationResultType();
then
(cache,Expression.makeBuiltinCall("simulation",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "residualCMP"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
recordtype = CevalScript.getSimulationResultType();
then
(cache,Expression.makeBuiltinCall("residualCMP",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()),SOME(st));

case (cache,env,Absyn.CREF_IDENT(name = "linearize"),{Absyn.CREF(componentRef = cr)},args,impl,SOME(st),pre,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info);
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inBoolean, inInteractiveInteractiveSymbolTableOption, inPrefix, info, NONE());
recordtype = CevalScript.getSimulationResultType();
then
(cache,Expression.makeBuiltinCall("linearize",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()),SOME(st));
Expand Down

0 comments on commit 2beb8d7

Please sign in to comment.