Skip to content

Commit 23e01ab

Browse files
committed
Fix dumping of flat model with NB
- Return the OB function tree that's used in `NSimCode.SimCode.create` so it can be reused when dumping the flat model, since the conversion is destructive and can fail if done twice.
1 parent 729822b commit 23e01ab

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ protected
8080
DAE.Element class_elem;
8181
algorithm
8282
daeFunctions := convertFunctionTree(functions);
83+
dae := convertModel(flatModel);
84+
execStat(getInstanceName());
85+
end convert;
8386

87+
function convertModel
88+
input FlatModel flatModel;
89+
output DAE.DAElist dae;
90+
protected
91+
list<DAE.Element> elems;
92+
DAE.Element class_elem;
93+
algorithm
8494
elems := convertVariables(flatModel.variables, {});
8595
elems := convertEquations(flatModel.equations, elems);
8696
elems := convertInitialEquations(flatModel.initialEquations, elems);
@@ -89,9 +99,7 @@ algorithm
8999

90100
class_elem := DAE.COMP(FlatModel.fullName(flatModel), elems, flatModel.source, ElementSource.getOptComment(flatModel.source));
91101
dae := DAE.DAE({class_elem});
92-
93-
execStat(getInstanceName());
94-
end convert;
102+
end convertModel;
95103

96104
function convertStatements
97105
input list<Statement> statements;

OMCompiler/Compiler/NSimCode/NSimCode.mo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public
270270
input String fileNamePrefix;
271271
input Option<OldSimCode.SimulationSettings> simSettingsOpt;
272272
output SimCode simCode;
273+
output DAE.FunctionTree oldFunctionTree;
273274
protected
274275
partial function mapExp
275276
input output Expression exp;
@@ -400,7 +401,10 @@ public
400401
// Will probably be mostly the same in all other regards
401402
program := SymbolTable.getAbsyn();
402403
directory := CevalScriptBackend.getFileDir(AbsynUtil.pathToCref(name), program);
403-
(libs, libPaths, _, includeDirs, recordDecls, functions, _) := OldSimCodeUtil.createFunctions(program, ConvertDAE.convertFunctionTree(funcTree));
404+
// The OB function tree is needed both here and when dumping the flat model,
405+
// but converting it is destructive so return it to avoid doing it again.
406+
oldFunctionTree := ConvertDAE.convertFunctionTree(funcTree);
407+
(libs, libPaths, _, includeDirs, recordDecls, functions, _) := OldSimCodeUtil.createFunctions(program, oldFunctionTree);
404408
makefileParams := OldSimCodeFunctionUtil.createMakefileParams(includeDirs, libs, libPaths, false, false);
405409

406410
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := collectAlgebraicLoops(init, init_0, ode, algebraic, daeModeData, simCodeIndices, simcode_map);

OMCompiler/Compiler/SimCode/SimCodeMain.mo

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ function generateModelCodeNewBackend
394394
output String fileDir;
395395
output Real timeSimCode = 0.0;
396396
output Real timeTemplates = 0.0;
397+
output DAE.FunctionTree oldFunctionTree;
397398
protected
398399
Integer numCheckpoints;
399400
NSimCode.SimCode simCode;
@@ -403,7 +404,7 @@ algorithm
403404
StackOverflow.clearStacktraceMessages();
404405
try
405406
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMCODE);
406-
simCode := NSimCode.SimCode.create(bdae, className, fileNamePrefix, simSettingsOpt);
407+
(simCode, oldFunctionTree) := NSimCode.SimCode.create(bdae, className, fileNamePrefix, simSettingsOpt);
407408
if Flags.isSet(Flags.DUMP_SIMCODE) then
408409
print(NSimCode.SimCode.toString(simCode));
409410
end if;
@@ -1172,15 +1173,15 @@ algorithm
11721173
ExecStat.execStat("FrontEnd");
11731174

11741175
if runBackend then
1175-
(outLibs, outFileDir, resultValues) := translateModelCallBackendNB(flatModel, funcTree, className, inFileNamePrefix, inSimSettingsOpt);
1176+
(outLibs, outFileDir, resultValues, funcs) := translateModelCallBackendNB(flatModel, funcTree, className, inFileNamePrefix, inSimSettingsOpt);
11761177
end if;
11771178

11781179
// This must be done after calling the backend since it uses the FlatModel,
11791180
// and converting it to DAE is destructive.
11801181
if dumpValidFlatModelicaNF then
11811182
flatString := NFFlatString;
11821183
elseif not runSilent then
1183-
(dae, funcs) := NFConvertDAE.convert(flatModel, funcTree);
1184+
dae := NFConvertDAE.convertModel(flatModel);
11841185
flatString := DAEDump.dumpStr(dae, funcs);
11851186
end if;
11861187

@@ -1476,6 +1477,7 @@ protected function translateModelCallBackendNB
14761477
output list<String> outLibs;
14771478
output String outFileDir;
14781479
output list<tuple<String, Values.Value>> resultValues;
1480+
output DAE.FunctionTree oldFunctionTree;
14791481
protected
14801482
Real timeSimCode=0.0, timeTemplates=0.0, timeBackend=0.0;
14811483
NBackendDAE bdae;
@@ -1493,7 +1495,7 @@ algorithm
14931495
timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
14941496
ExecStat.execStat("backend");
14951497

1496-
(outLibs, outFileDir, timeSimCode, timeTemplates) := generateModelCodeNewBackend(bdae, inClassName, inFileNamePrefix, inSimSettingsOpt);
1498+
(outLibs, outFileDir, timeSimCode, timeTemplates, oldFunctionTree) := generateModelCodeNewBackend(bdae, inClassName, inFileNamePrefix, inSimSettingsOpt);
14971499

14981500
resultValues := {("timeTemplates", Values.REAL(timeTemplates)),
14991501
("timeSimCode", Values.REAL(timeSimCode)),

0 commit comments

Comments
 (0)