Skip to content

Commit

Permalink
add further daeMode option
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi committed Jul 5, 2016
1 parent f2eb970 commit 3d60086
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Compiler/SimCode/SimCode.mo
Expand Up @@ -264,13 +264,19 @@ uniontype VarInfo "Number of variables of various types in a Modelica model."
end VARINFO;
end VarInfo;

uniontype DaeModeConfig
record ALL_EQUATIONS end ALL_EQUATIONS;
record DYNAMIC_EQUATIONS end DYNAMIC_EQUATIONS;
end DaeModeConfig;

uniontype DaeModeData
"contains data that belongs to the dae mode"
record DAEMODEDATA
list<list<SimEqSystem>> daeEquations "daeModel residuals equations";
Option<JacobianMatrix> sparsityPattern "contains the sparsity pattern for the daeMode";
list<SimCodeVar.SimVar> residualVars; // variable used to calculate residuals of a DAE form, they are real
list<SimCodeVar.SimVar> algebraicDAEVars; // variable used to calculate residuals of a DAE form, they are real
DaeModeConfig modeCreated; // indicates the mode in which
end DAEMODEDATA;
end DaeModeData;

Expand Down
14 changes: 9 additions & 5 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -241,6 +241,7 @@ protected
BackendDAE.EquationArray bdaeModeEqns;
Option<SimCode.JacobianMatrix> daeModeSP;
Option<SimCode.DaeModeData> daeModeData;
SimCode.DaeModeConfig daeModeConf;
list<SimCodeVar.SimVar> sensitivityVars;
Integer countSenParams;
list<tuple<Integer, Integer>> equationSccMapping, eqBackendSimCodeMapping;
Expand Down Expand Up @@ -340,7 +341,7 @@ algorithm
allEquations := List.append_reverse(allEquations, removedEquations);

// create residuals equations from ode equations for daeMode
if Flags.getConfigBool(Flags.DAE_MODE) then
if Flags.getConfigEnum(Flags.DAE_MODE)>1 then
(daeEquations, residualVars, algebraicVars, uniqueEqIndex, tempvars,
bdaeModeVars, bdaeModeEqns) := createDAEEquations(contSysts, shared, uniqueEqIndex, tempvars);
else
Expand Down Expand Up @@ -437,11 +438,12 @@ algorithm
end if;

// add residuals vars from DAE creation
if Flags.getConfigBool(Flags.DAE_MODE) then
if Flags.getConfigEnum(Flags.DAE_MODE)>1 then
daeModeSP := createDaeModeSparsePattern(bdaeModeVars, bdaeModeEqns, shared, crefToSimVarHT);
residualVars := rewriteIndex(residualVars, 0);
algebraicVars := sortSimVarsAndWriteIndex(algebraicVars, crefToSimVarHT);
daeModeData := SOME(SimCode.DAEMODEDATA(daeEquations, daeModeSP, residualVars, algebraicVars));
daeModeConf := match Flags.getConfigEnum(Flags.DAE_MODE) case 2 then SimCode.ALL_EQUATIONS(); case 3 then SimCode.DYNAMIC_EQUATIONS(); end match;
daeModeData := SOME(SimCode.DAEMODEDATA(daeEquations, daeModeSP, residualVars, algebraicVars, daeModeConf));
else
daeModeData := NONE();
end if;
Expand Down Expand Up @@ -1901,7 +1903,7 @@ protected
array<Integer> stateeqnsmark;
Integer uniqueEqIndex;
String message;
Boolean skip;
Boolean skip, skipEquations;

list<SimCodeVar.SimVar> tempvars, resVars, algVars;
list<list<SimCode.SimEqSystem>> daeEquations;
Expand Down Expand Up @@ -1933,9 +1935,11 @@ algorithm
skip := Util.boolAndList(List.map(eqnlst, BackendEquation.isWhenEquation));
// skip is discrete
skip := Util.boolAndList(List.map(varlst, BackendVariable.isVarDiscrete)) or skip;
// skip algebraic equation if dynamic option is selected
skipEquations := match Flags.getConfigEnum(Flags.DAE_MODE) case 2 then true; case 3 then BackendDAEUtil.blockIsDynamic(eqnNums, stateeqnsmark); end match;

// convert only dynamic block here
if BackendDAEUtil.blockIsDynamic(eqnNums, stateeqnsmark) and not skip then
if skipEquations and not skip then

// try as is should fallback case is a hack for complex record equations
try
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenC.tpl
Expand Up @@ -1091,7 +1091,7 @@ template simulationFile(SimCode simCode, String guid, Boolean isModelExchangeFMU
data.modelData = &modelData;
data.simulationInfo = &simInfo;
measure_time_flag = <% if profileHtml() then "5" else if profileSome() then "1" else if profileAll() then "2" else "0" /* Would be good if this was not a global variable...*/ %>;
compiledInDAEMode = <% if Flags.getConfigBool(Flags.DAE_MODE) then '1' else '0' %>;
compiledInDAEMode = <%intSub(Flags.getConfigEnum(Flags.DAE_MODE), 1)%>;
<%mainInit%>
<%mainTop(mainBody,"https://trac.openmodelica.org/OpenModelica/newticket")%>
Expand Down
10 changes: 10 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -613,6 +613,11 @@ package SimCode
end VARINFO;
end VarInfo;

uniontype DaeModeConfig
record ALL_EQUATIONS end ALL_EQUATIONS;
record DYNAMIC_EQUATIONS end DYNAMIC_EQUATIONS;
end DaeModeConfig;

uniontype DaeModeData
"contains data that belongs to the dae mode"
record DAEMODEDATA
Expand Down Expand Up @@ -3322,6 +3327,11 @@ package Flags
output String outValue;
end getConfigString;

function getConfigEnum
input ConfigFlag inFlag;
output Integer outValue;
end getConfigEnum;

function getConfigStringList
input ConfigFlag inFlag;
output list<String> outValue;
Expand Down
10 changes: 8 additions & 2 deletions Compiler/Util/Flags.mo
Expand Up @@ -1281,9 +1281,15 @@ constant ConfigFlag NO_TEARING_FOR_COMPONENT = CONFIG_FLAG(93, "noTearingForComp
constant ConfigFlag CT_STATE_MACHINES = CONFIG_FLAG(94, "ctStateMachines",
NONE(), INTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Experimental: Enable continuous-time state machine prototype"));

constant ConfigFlag DAE_MODE = CONFIG_FLAG(95, "daeMode",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Generates additional code for DAE mode, where dynamic equations are not causelized."));
NONE(), EXTERNAL(), ENUM_FLAG(1, {("none", 1), ("all",2), ("dynamic",3)}),
SOME(STRING_OPTION({"none", "all", "dynamic"})),
Util.gettext("Generates additional code for DAE mode, where the equations are not causelized, when one of the following option is selected:\n"+
"all : In this mode all equations are passed to the integrator.\n"+
"dynamic : In this mode only the equation for the dynamic part of the system are passed to the integrator.")
);

constant ConfigFlag INLINE_METHOD = CONFIG_FLAG(96, "inlineMethod",
NONE(), EXTERNAL(), ENUM_FLAG(1, {("replace",1), ("append",2)}),
SOME(STRING_OPTION({"replace", "append"})),
Expand Down
6 changes: 5 additions & 1 deletion SimulationRuntime/c/simulation/solver/ida_solver.c
Expand Up @@ -1051,7 +1051,11 @@ int rootsFunctionIDA(double time, N_Vector yy, N_Vector yp, double *gout, void*
externalInputUpdate(data);
data->callback->input_function(data, threadData);
/* eval needed equations*/
data->callback->function_ZeroCrossingsEquations(data, threadData);
if (idaData->daeMode && compiledInDAEMode == 1){}
else
{
data->callback->function_ZeroCrossingsEquations(data, threadData);
}

data->callback->function_ZeroCrossings(data, threadData, gout);

Expand Down

0 comments on commit 3d60086

Please sign in to comment.