Skip to content

Commit

Permalink
Invent nominal values for derivatives (#7819)
Browse files Browse the repository at this point in the history
A new heuristic for estimating the nominal value of derivatives is
to take the nominal value from the corresponding state and divide
that by the simulation interval.

  der(x).nominal := x.nominal/Interval

See issue #7794 for the discussion.
  • Loading branch information
phannebohm committed Sep 1, 2021
1 parent f946c53 commit c80f58e
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 51 deletions.
15 changes: 8 additions & 7 deletions OMCompiler/Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -79,7 +79,7 @@ uniontype EqSystem "An independent system of equations (and their corresponding
StateSets stateSets "the state sets of the system";
BaseClockPartitionKind partitionKind;
EquationArray removedEqs "these are equations that cannot solve for a variable.
e.g. assertions, external function calls, algorithm sections without effect";
e.g. assertions, external function calls, algorithm sections without effect";
end EQSYSTEM;
end EqSystem;

Expand Down Expand Up @@ -121,19 +121,20 @@ uniontype Shared "Data shared for all equation-systems"
pointer to their values (trajectories).";
EquationArray initialEqs "Initial equations";
EquationArray removedEqs "these are equations that cannot solve for a variable. for example assertions, external function calls, algorithm sections without effect";
list< .DAE.Constraint> constraints "constraints (Optimica extension)";
list< .DAE.ClassAttributes> classAttrs "class attributes (Optimica extension)";
list< .DAE.Constraint> constraints "constraints (Optimica extension)";
list< .DAE.ClassAttributes> classAttrs "class attributes (Optimica extension)";
FCore.Cache cache;
FCore.Graph graph;
.DAE.FunctionTree functionTree "functions for Backend";
EventInfo eventInfo "eventInfo";
ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
SymbolicJacobians symjacs "Symbolic Jacobians";
ExtraInfo info "contains extra info that we send around like the model name";
ExtraInfo info "contains extra info that we send around like the model name";
PartitionsInfo partitionsInfo;
BackendDAEModeData daeModeData "DAEMode Data";
BackendDAEModeData daeModeData "DAEMode Data";
Option<DataReconciliationData> dataReconciliationData;
Option<.DAE.Exp> timeInterval "from experiment annotation Interval, used for derivative nominal guesswork";
end SHARED;
end Shared;

Expand Down Expand Up @@ -167,8 +168,8 @@ uniontype PartitionsInfo
end PartitionsInfo;

uniontype ExtraInfo "extra information that we should send around with the DAE"
record EXTRA_INFO "extra information that we should send around with the DAE"
String description "the model description string";
record EXTRA_INFO
String description "the model description string";
String fileNamePrefix "the model name to be used in the dumps";
end EXTRA_INFO;
end ExtraInfo;
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -170,6 +170,7 @@ algorithm
symjacs,inExtraInfo,
BackendDAEUtil.emptyPartitionsInfo(),
BackendDAE.emptyDAEModeData,
NONE(),
NONE()
));
BackendDAEUtil.checkBackendDAEWithErrorMsg(outBackendDAE);
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -9179,6 +9179,7 @@ algorithm
ei,
emptyPartitionsInfo(),
BackendDAE.emptyDAEModeData,
NONE(),
NONE()
);
end createEmptyShared;
Expand Down
27 changes: 10 additions & 17 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -1266,7 +1266,7 @@ algorithm

case (cache,env,"translateModel",vals as {Values.CODE(Absyn.C_TYPENAME(className)),_,_,_,_,_,Values.STRING(filenameprefix),_,_,_,_,_},_)
equation
(cache,simSettings) = calculateSimulationSettings(cache,env,vals,msg);
(cache,simSettings) = calculateSimulationSettings(cache, vals);
(b,cache,_,_,_) = translateModel(cache, env, className, filenameprefix, true, SOME(simSettings));
then
(cache,Values.BOOL(b));
Expand Down Expand Up @@ -1499,7 +1499,7 @@ algorithm
if Config.simCodeTarget() == "omsicpp" then

filenameprefix := AbsynUtil.pathString(className);
(cache,simSettings) := calculateSimulationSettings(cache, env, vals, msg);
(cache,simSettings) := calculateSimulationSettings(cache, vals);
try
(cache, Values.STRING(str)) := buildModelFMU(cache, env, className, "2.0", "me", "<default>", true, {"static"},SOME(simSettings));
if stringEmpty(str) then
Expand All @@ -1524,7 +1524,7 @@ algorithm

if b then
exeDir := compileDir;
(cache,simSettings) := calculateSimulationSettings(cache,env,vals,msg);
(cache,simSettings) := calculateSimulationSettings(cache, vals);
SimCode.SIMULATION_SETTINGS(outputFormat = outputFormat_str) := simSettings;
result_file := stringAppendList(List.consOnTrue(not Testsuite.isRunning(),compileDir,{executable,"_res.",outputFormat_str}));
// result file might have been set by simflags (-r ...)
Expand Down Expand Up @@ -1696,7 +1696,7 @@ algorithm
(b,cache,compileDir,executable,_,outputFormat_str,_,simflags,resultValues,vals) = buildModel(cache,env,vals,msg);
if b then
exeDir=compileDir;
(cache,simSettings) = calculateSimulationSettings(cache,env,vals,msg);
(cache,simSettings) = calculateSimulationSettings(cache, vals);
SimCode.SIMULATION_SETTINGS(outputFormat = outputFormat_str) = simSettings;
result_file = stringAppendList(List.consOnTrue(not Testsuite.isRunning(),compileDir,{executable,"_res.",outputFormat_str}));
executableSuffixedExe = stringAppend(executable, getSimulationExtension(Config.simCodeTarget(),Autoconf.platform));
Expand Down Expand Up @@ -4103,36 +4103,29 @@ end translateGraphics;
protected function calculateSimulationSettings " author: x02lucpo
calculates the start,end,interval,stepsize, method and initFileName"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input list<Values.Value> vals;
input Absyn.Msg inMsg;
output FCore.Cache outCache;
output SimCode.SimulationSettings outSimSettings;
algorithm
(outCache,outSimSettings) := match (inCache,inEnv,vals,inMsg)
(outCache,outSimSettings) := match (inCache,vals)
local
String method_str,options_str,outputFormat_str,variableFilter_str,s;
String method_str,options_str,outputFormat_str,variableFilter_str;
Values.Value starttime_v,stoptime_v,tolerance_v;
Integer interval_i;
Real starttime_r,stoptime_r,tolerance_r;
FCore.Graph env;
Absyn.Msg msg;
FCore.Cache cache;
String cflags,simflags;
case (cache,_,{Values.CODE(Absyn.C_TYPENAME(_)),starttime_v,stoptime_v,Values.INTEGER(interval_i),tolerance_v,Values.STRING(method_str),_,Values.STRING(options_str),Values.STRING(outputFormat_str),Values.STRING(variableFilter_str),Values.STRING(cflags),Values.STRING(_)},_)
String cflags;
case (cache, {Values.CODE(Absyn.C_TYPENAME(_)),starttime_v,stoptime_v,Values.INTEGER(interval_i),tolerance_v,Values.STRING(method_str),_,Values.STRING(options_str),Values.STRING(outputFormat_str),Values.STRING(variableFilter_str),Values.STRING(cflags),Values.STRING(_)})
equation
starttime_r = ValuesUtil.valueReal(starttime_v);
stoptime_r = ValuesUtil.valueReal(stoptime_v);
tolerance_r = ValuesUtil.valueReal(tolerance_v);
outSimSettings = SimCodeMain.createSimulationSettings(starttime_r,stoptime_r,interval_i,tolerance_r,method_str,options_str,outputFormat_str,variableFilter_str,cflags);


then
(cache, outSimSettings);
else
equation
s = "CevalScript.calculateSimulationSettings failed: " + ValuesUtil.valString(Values.TUPLE(vals));
Error.addMessage(Error.INTERNAL_ERROR, {s});
Error.addMessage(Error.INTERNAL_ERROR, {"CevalScript.calculateSimulationSettings failed: " + ValuesUtil.valString(Values.TUPLE(vals))});
then
fail();
end match;
Expand Down Expand Up @@ -5459,7 +5452,7 @@ algorithm
end if;

compileDir := System.pwd() + Autoconf.pathDelimiter;
(cache,simSettings) := calculateSimulationSettings(cache, env, values, msg);
(cache,simSettings) := calculateSimulationSettings(cache, values);
SimCode.SIMULATION_SETTINGS(method = method_str, outputFormat = outputFormat_str) := simSettings;

(success,cache,libs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix,true, SOME(simSettings));
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -336,7 +336,8 @@ protected function createSimCode "
input BackendDAE.SymbolicJacobians inFMIDer = {};
output SimCode.SimCode simCode;
algorithm
simCode := matchcontinue(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, libPaths, program,simSettingsOpt, recordDecls, literals, args)
// FIXME This matchcontinue is extremely horrible! Please remove it
simCode := matchcontinue(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, libPaths, program, simSettingsOpt, recordDecls, literals, args)
local
Integer numProc;
SimCode.SimCode tmpSimCode;
Expand Down

0 comments on commit c80f58e

Please sign in to comment.