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

Commit f10312d

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
[DAEmode] added several minor things
- added DAE_CONTEXT - disable compiler optimization for some dae functions - preparation to use eqAttr in the code generation Belonging to [master]: - #2309
1 parent 90d01e9 commit f10312d

File tree

6 files changed

+138
-38
lines changed

6 files changed

+138
-38
lines changed

Compiler/SimCode/SimCodeFunction.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ uniontype Context
222222

223223
record FMI_CONTEXT
224224
end FMI_CONTEXT;
225+
226+
record DAE_MODE_CONTEXT
227+
end DAE_MODE_CONTEXT;
225228
end Context;
226229

227230
public constant Context contextSimulationNonDiscrete = SIMULATION_CONTEXT(false);
@@ -236,6 +239,7 @@ public constant Context contextParallelFunction = PARALLEL_FUNCTION_CONTEX
236239
public constant Context contextZeroCross = ZEROCROSSINGS_CONTEXT();
237240
public constant Context contextOptimization = OPTIMIZATION_CONTEXT();
238241
public constant Context contextFMI = FMI_CONTEXT();
242+
public constant Context contextDAEmode = DAE_MODE_CONTEXT();
239243

240244
constant list<DAE.Exp> listExpLength1 = {DAE.ICONST(0)} "For CodegenC.tpl";
241245
constant list<Variable> boxedRecordOutVars = VARIABLE(DAE.CREF_IDENT("",DAE.T_COMPLEX_DEFAULT_RECORD,{}),DAE.T_COMPLEX_DEFAULT_RECORD,NONE(),{},DAE.NON_PARALLEL(),DAE.VARIABLE())::{} "For CodegenC.tpl";

Compiler/SimCode/SimCodeMain.mo

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ protected
10681068
Integer maxDelayedExpIndex;
10691069
Integer uniqueEqIndex = 1;
10701070
Integer nStates;
1071-
Integer numberofEqns, numStateSets, numberofLinearSys, numberofNonLinearSys,
1071+
Integer numberofEqns, numberofLinearSys, numberofNonLinearSys,
10721072
numberofMixedSys, numberOfJacobians, numberofFixedParameters;
10731073
Boolean tmpB;
10741074

@@ -1086,9 +1086,9 @@ protected
10861086
Option<SimCode.JacobianMatrix> daeModeSP;
10871087
Option<SimCode.DaeModeData> daeModeData;
10881088
SimCode.DaeModeConfig daeModeConf;
1089-
list<SimCode.SimEqSystem> daeEquations, removedEquations;
1089+
1090+
list<list<SimCode.SimEqSystem>> daeEquations;
10901091
list<SimCodeVar.SimVar> residualVars, algebraicStateVars, auxiliaryVars;
1091-
list<SimCode.StateSet> stateSets;
10921092

10931093
tuple<Option<BackendDAE.SymbolicJacobian>, BackendDAE.SparsePattern, BackendDAE.SparseColoring> daeModeJac;
10941094
SimCode.JacobianMatrix symDAESparsPattern;
@@ -1155,19 +1155,8 @@ algorithm
11551155

11561156
discreteModelVars := BackendDAEUtil.foldEqSystem(inBackendDAE, SimCodeUtil.extractDiscreteModelVars, {});
11571157

1158-
//prepare DAEmode stuff
1159-
eqnsLst := BackendEquation.equationSystemsEqnsLst(inBackendDAE.eqs);
1160-
varsLst := BackendVariable.equationSystemsVarsLst(inBackendDAE.eqs);
1161-
daeVars := BackendVariable.listVar(varsLst);
1162-
daeEqns := BackendEquation.listEquation(eqnsLst);
1163-
// create SimCode residual equation
1164-
(daeEquations, uniqueEqIndex, tempVars) := SimCodeUtil.createEquationsfromList(listReverse(eqnsLst), varsLst, uniqueEqIndex, tempVars, inBackendDAE.shared.info, true, true);
1165-
1166-
(uniqueEqIndex, removedEquations) := BackendEquation.traverseEquationArray(BackendDAEUtil.collapseRemovedEqs(inBackendDAE), SimCodeUtil.traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
1167-
daeEquations := listAppend(daeEquations, removedEquations);
1168-
// state set stuff
1169-
(_, stateSets, uniqueEqIndex, tempVars, numStateSets) := SimCodeUtil.createStateSets(inBackendDAE, {}, uniqueEqIndex, tempVars);
1170-
if debug then ExecStat.execStat("simCode: createStateSets"); end if;
1158+
// create DAEmode equations
1159+
(daeEquations, uniqueEqIndex, tempVars) := SimCodeUtil.createEquationsfromBackendDAE(inBackendDAE, uniqueEqIndex, tempVars, true, true);
11711160

11721161
// create model info
11731162
// create dummy system where all original variables are created
@@ -1177,14 +1166,12 @@ algorithm
11771166
// disable start value calculation, it's only helpful in case of algebraic loops
11781167
// and they are not present in DAEmode
11791168
tmpB := Flags.set(Flags.NO_START_CALC, true);
1180-
modelInfo := SimCodeUtil.createModelInfo(className, p, emptyBDAE, inInitDAE, functions, {}, numStateSets, fileDir, 0, tempVars);
1169+
modelInfo := SimCodeUtil.createModelInfo(className, p, emptyBDAE, inInitDAE, functions, {}, 0, fileDir, 0, tempVars);
11811170
Flags.set(Flags.NO_START_CALC, tmpB);
11821171

11831172
//create hash table
11841173
crefToSimVarHT := SimCodeUtil.createCrefToSimVarHT(modelInfo);
11851174

1186-
// collect symbolic jacobians from state selection
1187-
(stateSets, modelInfo, SymbolicJacsStateSelect) := SimCodeUtil.addAlgebraicLoopsModelInfoStateSets(stateSets, modelInfo);
11881175
// collect symbolic jacobians in initialization loops of the overall jacobians
11891176
SymbolicJacsNLS := {};
11901177
(initialEquations, modelInfo, SymbolicJacsTemp) := SimCodeUtil.addAlgebraicLoopsModelInfo(initialEquations, modelInfo);
@@ -1193,7 +1180,7 @@ algorithm
11931180
SymbolicJacsNLS := listAppend(SymbolicJacsTemp, SymbolicJacsNLS);
11941181
(symJacs, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({}, crefToSimVarHT, uniqueEqIndex, {"A", "B", "C", "D"}, {});
11951182
(SymbolicJacs, modelInfo, SymbolicJacsTemp) := SimCodeUtil.addAlgebraicLoopsModelInfoSymJacs(symJacs, modelInfo);
1196-
SymbolicJacs := listAppend(SymbolicJacs, SymbolicJacsStateSelect);
1183+
11971184
// collect jacobian equation only for equantion info file
11981185
jacobianEquations := SimCodeUtil.collectAllJacobianEquations(SymbolicJacs);
11991186
if debug then ExecStat.execStat("simCode: create Jacobian linear code"); end if;
@@ -1205,8 +1192,10 @@ algorithm
12051192
seedVars := SimCodeUtil.collectAllSeedVars(SymbolicJacs);
12061193
modelInfo := SimCodeUtil.setSeedVars(seedVars, modelInfo);
12071194

1208-
12091195
// create dae SimVars: residual and algebraic
1196+
varsLst := BackendVariable.equationSystemsVarsLst(inBackendDAE.eqs);
1197+
daeVars := BackendVariable.listVar(varsLst);
1198+
12101199
// create residual variables, set index and push them SimCode HashTable
12111200
((_, resVars)) := BackendVariable.traverseBackendDAEVars(daeVars, BackendVariable.collectVarKindVarinVariables, (BackendVariable.isDAEmodeResVar, BackendVariable.emptyVars()));
12121201
((residualVars, _)) := BackendVariable.traverseBackendDAEVars(resVars, SimCodeUtil.traversingdlowvarToSimvar, ({}, BackendVariable.emptyVars()));
@@ -1237,7 +1226,7 @@ algorithm
12371226
({symDAESparsPattern}, uniqueEqIndex) := SimCodeUtil.createSymbolicJacobianssSimCode({daeModeJac}, crefToSimVarHT, uniqueEqIndex, {"daeMode"}, {});
12381227
daeModeSP := SOME(symDAESparsPattern);
12391228
daeModeConf := SimCode.ALL_EQUATIONS();
1240-
daeModeData := SOME(SimCode.DAEMODEDATA({daeEquations}, daeModeSP, residualVars, algebraicStateVars, auxiliaryVars, daeModeConf));
1229+
daeModeData := SOME(SimCode.DAEMODEDATA(daeEquations, daeModeSP, residualVars, algebraicStateVars, auxiliaryVars, daeModeConf));
12411230

12421231
/* This is a *much* better estimate than the guessed number of equations */
12431232
modelInfo := SimCodeUtil.addNumEqns(modelInfo, uniqueEqIndex-listLength(jacobianEquations));
@@ -1262,11 +1251,11 @@ algorithm
12621251
minValueEquations,
12631252
maxValueEquations,
12641253
parameterEquations,
1265-
removedEquations,
12661254
{},
12671255
{},
12681256
{},
1269-
stateSets,
1257+
{},
1258+
{},
12701259
{},
12711260
{},
12721261
zeroCrossings,

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,39 @@ algorithm
49904990
end matchcontinue;
49914991
end createEquationsIfBranch;
49924992

4993+
public function createEquationsfromBackendDAE
4994+
input BackendDAE.BackendDAE inBDAE;
4995+
input Integer iuniqueEqIndex;
4996+
input list<SimCodeVar.SimVar> itempvars;
4997+
input Boolean genDiscrete = false;
4998+
input Boolean includeWhen = false;
4999+
input Boolean skipDiscInZc = false;
5000+
input Boolean skipDiscInAlgorithm = false;
5001+
output list<list<SimCode.SimEqSystem>> outEquations = {};
5002+
output Integer uniqueEqIndex = iuniqueEqIndex;
5003+
output list<SimCodeVar.SimVar> tempvars = itempvars;
5004+
protected
5005+
BackendDAE.StrongComponents comps;
5006+
list<SimCode.SimEqSystem> simEqs, zeroVarEquations;
5007+
algorithm
5008+
for syst in inBDAE.eqs loop
5009+
// get components
5010+
comps := BackendDAEUtil.getCompsOfMatching(syst.matching);
5011+
5012+
// create SimCode equatinos
5013+
(simEqs, _, uniqueEqIndex, tempvars) := createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, syst, inBDAE.shared, comps, uniqueEqIndex, tempvars);
5014+
5015+
// process also the zeroVariable equations
5016+
(uniqueEqIndex, zeroVarEquations) := BackendEquation.traverseEquationArray(syst.removedEqs, SimCodeUtil.traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
5017+
5018+
// add the zero variable equations
5019+
simEqs := listAppend(simEqs, zeroVarEquations);
5020+
5021+
// add equations
5022+
outEquations := listAppend(outEquations, {simEqs});
5023+
end for;
5024+
end createEquationsfromBackendDAE;
5025+
49935026
public function createEquationsfromList
49945027
input list<BackendDAE.Equation> inEquations;
49955028
input list<BackendDAE.Var> inVars;

Compiler/Template/CodegenC.tpl

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3873,6 +3873,18 @@ template functionXXX_systems(list<list<SimEqSystem>> eqs, String name, Text &loo
38733873
>>
38743874
end functionXXX_systems;
38753875
3876+
template createEquationsAndCalls(list<list<SimEqSystem>> systems, String name, Context context, String modelNamePrefixStr, Text eqCalls, Text eqFuncs)
3877+
::=
3878+
let _ = (systems |> equations => (
3879+
equations |> eq => (
3880+
let &eqFuncs += equation_impl(-1, eq, context, modelNamePrefixStr)
3881+
let &eqCalls += equationNames_(eq, context, modelNamePrefixStr)
3882+
<<>>
3883+
)
3884+
)
3885+
)
3886+
<<>>
3887+
end createEquationsAndCalls;
38763888
38773889
template equationNamesArrayFormat(SimEqSystem eq, Context context, String name, Integer arrayIndex, Text &arrayEqs, Text &forwardEqs, String modelNamePrefixStr)
38783890
"Generates an equation.
@@ -4017,30 +4029,21 @@ template evaluateDAEResiduals(list<list<SimEqSystem>> resEquations, String model
40174029
"Generates function in simulation file."
40184030
::=
40194031
let &eqFuncs = buffer ""
4020-
let &nrfuncs = buffer ""
4021-
let &fncalls = buffer ""
4032+
let &eqCalls = buffer ""
40224033
4023-
let systems = if Flags.isSet(Flags.PARMODAUTO) then
4024-
(functionXXX_systems_arrayFormat(resEquations, "DAERes", &fncalls, &nrfuncs, &eqFuncs, modelNamePrefix))
4025-
else
4026-
(functionXXX_systems(resEquations, "DAERes", &fncalls, &eqFuncs, modelNamePrefix))
4034+
let systems = createEquationsAndCalls(resEquations, "DAERes", contextDAEmode, modelNamePrefix, &eqCalls, &eqFuncs)
40274035
<<
40284036
/*residual equations*/
40294037
<%eqFuncs%>
4030-
<%(resEquations |> eqLst => (eqLst |> eq hasindex i0 =>
4031-
equation_impl(-1, eq, contextSimulationDiscrete, modelNamePrefix)
4032-
;separator="\n");separator="\n")%>
4033-
<%systems%>
4038+
40344039
/* for residuals DAE variables */
4040+
OMC_DISABLE_OPT
40354041
int <%symbolName(modelNamePrefix,"evaluateDAEResiduals")%>(DATA *data, threadData_t *threadData)
40364042
{
40374043
TRACE_PUSH
40384044
data->simulationInfo->callStatistics.functionEvalDAE++;
40394045
4040-
<%if Flags.isSet(Flags.PARMODAUTO) then 'PM_functionDAERes(<%nrfuncs%>, data, threadData, functionDAERes_systems);'
4041-
else fncalls %>
4042-
4043-
<%symbolName(modelNamePrefix,"function_savePreSynchronous")%>(data, threadData);
4046+
<%eqCalls%>
40444047
40454048
TRACE_POP
40464049
return 0;
@@ -4075,6 +4078,7 @@ template algebraicDAEVar(list<SimVar> algVars, String modelNamePrefix)
40754078
40764079
<<
40774080
/* algebraic nominal values */
4081+
OMC_DISABLE_OPT
40784082
int <%symbolName(modelNamePrefix,"getAlgebraicDAEVarNominals")%>(DATA *data, threadData_t *threadData, double* algebraicNominal)
40794083
{
40804084
TRACE_PUSH
@@ -4085,6 +4089,7 @@ template algebraicDAEVar(list<SimVar> algVars, String modelNamePrefix)
40854089
}
40864090
40874091
/* forward algebraic variables */
4092+
OMC_DISABLE_OPT
40884093
int <%symbolName(modelNamePrefix,"setAlgebraicDAEVars")%>(DATA *data, threadData_t *threadData, double* algebraic)
40894094
{
40904095
TRACE_PUSH
@@ -4093,6 +4098,7 @@ template algebraicDAEVar(list<SimVar> algVars, String modelNamePrefix)
40934098
return 0;
40944099
}
40954100
/* get algebraic variables */
4101+
OMC_DISABLE_OPT
40964102
int <%symbolName(modelNamePrefix,"getAlgebraicDAEVars")%>(DATA *data, threadData_t *threadData, double* algebraic)
40974103
{
40984104
TRACE_PUSH
@@ -4114,6 +4120,7 @@ template initializeDAEmodeData(Integer nResVars, Integer nAlgVars, Integer nAuxV
41144120
let maxColorStr = '<%maxColor%>'
41154121
<<
41164122
/* initialize the daeMode variables */
4123+
OMC_DISABLE_OPT
41174124
int <%symbolName(modelNamePrefix,"initializeDAEmodeData")%>(DATA *inData, DAEMODE_DATA* daeModeData)
41184125
{
41194126
TRACE_PUSH
@@ -5101,7 +5108,9 @@ template equationNames_(SimEqSystem eq, Context context, String modelNamePrefixS
51015108
else
51025109
equationIndex(eq)
51035110
end match
5111+
let simEqAttr = match context case(DAE_MODE_CONTEXT()) then '/* eqAttr: <%simEqAttr(eq)%> */'
51045112
<<
5113+
<%simEqAttr%>
51055114
<% if profileAll() then 'SIM_PROF_TICK_EQ(<%ix%>);' %>
51065115
<%symbolName(modelNamePrefixStr,"eqFunction")%>_<%ix%>(data, threadData);
51075116
<% if profileAll() then 'SIM_PROF_ACC_EQ(<%ix%>);' %>
@@ -5826,6 +5835,42 @@ template crefM(ComponentRef cr)
58265835
else "P" + crefToMStr(cr)
58275836
end crefM;
58285837

5838+
template simEqAttr(SimEqSystem eq)
5839+
::=
5840+
match eq
5841+
case SES_RESIDUAL(__)
5842+
case SES_SIMPLE_ASSIGN(__)
5843+
case SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
5844+
case SES_ARRAY_CALL_ASSIGN(__)
5845+
case SES_IFEQUATION(__)
5846+
case SES_ALGORITHM(__)
5847+
case SES_LINEAR(__)
5848+
case SES_NONLINEAR(__)
5849+
case SES_MIXED(__)
5850+
case SES_WHEN(__)
5851+
case SES_FOR_LOOP(__)
5852+
then eqAttributes(eqAttr)
5853+
end simEqAttr;
5854+
5855+
template eqAttributes(EquationAttributes eqAtt)
5856+
::=
5857+
match eqAtt
5858+
case EQUATION_ATTRIBUTES(kind=kind) then '<%eqKind(kind)%>'
5859+
else ''
5860+
end eqAttributes;
5861+
5862+
template eqKind(EquationKind eqKind)
5863+
::=
5864+
match eqKind
5865+
case DYNAMIC_EQUATION() then 'DYNAMIC_EQN_KIND'
5866+
case BINDING_EQUATION() then 'BINDING_EQN_KIND'
5867+
case INITIAL_EQUATION() then 'INITAL_EQN_KIND'
5868+
case CLOCKED_EQUATION() then 'CLOCKED_EQN_KIND'
5869+
case DISCRETE_EQUATION() then 'DISCRETE_EQN_KIND'
5870+
case AUX_EQUATION() then 'AUX_EQN_KIND'
5871+
case UNKNOWN_EQUATION_KIND() then 'UNKNOWN_EQN_KIND'
5872+
end eqKind;
5873+
58295874
/*****************************************************************************
58305875
* SECTION: GENERATE OPTIMIZATION IN SIMULATION FILE
58315876
*****************************************************************************/

Compiler/Template/CodegenCFunctions.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,7 @@ template algStmtWhen(DAE.Statement when, Context context, Text &varDecls, Text &
31843184
"Generates a when algorithm statement."
31853185
::=
31863186
match context
3187+
case DAE_MODE_CONTEXT(__)
31873188
case SIMULATION_CONTEXT(__) then
31883189
match when
31893190
case STMT_WHEN(__) then
@@ -5178,6 +5179,7 @@ template daeExpRelationSim(Exp exp, Context context, Text &preExp,
51785179
match exp
51795180
case rel as RELATION(__) then
51805181
match context
5182+
case DAE_MODE_CONTEXT(__)
51815183
case SIMULATION_CONTEXT(__) then
51825184
match rel.optionExpisASUB
51835185
case NONE() then

Compiler/Template/SimCodeTV.mo

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ package SimCodeFunction
830830
end OPTIMIZATION_CONTEXT;
831831
record FMI_CONTEXT
832832
end FMI_CONTEXT;
833+
record DAE_MODE_CONTEXT
834+
end DAE_MODE_CONTEXT;
833835
end Context;
834836

835837
constant Context contextSimulationNonDiscrete;
@@ -844,6 +846,7 @@ package SimCodeFunction
844846
constant Context contextZeroCross;
845847
constant Context contextOptimization;
846848
constant Context contextFMI;
849+
constant Context contextDAEmode;
847850
constant list<DAE.Exp> listExpLength1;
848851
constant list<SimCodeFunction.Variable> boxedRecordOutVars;
849852
end SimCodeFunction;
@@ -1321,6 +1324,30 @@ package BackendDAE
13211324

13221325
type Constraints = list<DAE.Constraint> "Constraints on the solvability of the (casual) tearing set; needed for proper Dynamic Tearing";
13231326

1327+
uniontype EquationKind "equation kind"
1328+
record BINDING_EQUATION
1329+
end BINDING_EQUATION;
1330+
record DYNAMIC_EQUATION
1331+
end DYNAMIC_EQUATION;
1332+
record INITIAL_EQUATION
1333+
end INITIAL_EQUATION;
1334+
record CLOCKED_EQUATION
1335+
Integer clk;
1336+
end CLOCKED_EQUATION;
1337+
record DISCRETE_EQUATION
1338+
end DISCRETE_EQUATION;
1339+
record AUX_EQUATION
1340+
end AUX_EQUATION;
1341+
record UNKNOWN_EQUATION_KIND
1342+
end UNKNOWN_EQUATION_KIND;
1343+
end EquationKind;
1344+
1345+
uniontype EquationAttributes
1346+
record EQUATION_ATTRIBUTES
1347+
Boolean differentiated "true if the equation was differentiated, and should not differentiated again to avoid equal equations";
1348+
EquationKind kind;
1349+
end EQUATION_ATTRIBUTES;
1350+
end EquationAttributes;
13241351
end BackendDAE;
13251352

13261353
package System

0 commit comments

Comments
 (0)