Skip to content

Commit ca662b3

Browse files
authored
Fix dumping of flat model with NB (#14627)
- 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 ca662b3

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-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: 8 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,17 @@ 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);
1177+
else
1178+
funcs := NFConvertDAE.convertFunctionTree(funcTree);
11761179
end if;
11771180

11781181
// This must be done after calling the backend since it uses the FlatModel,
11791182
// and converting it to DAE is destructive.
11801183
if dumpValidFlatModelicaNF then
11811184
flatString := NFFlatString;
11821185
elseif not runSilent then
1183-
(dae, funcs) := NFConvertDAE.convert(flatModel, funcTree);
1186+
dae := NFConvertDAE.convertModel(flatModel);
11841187
flatString := DAEDump.dumpStr(dae, funcs);
11851188
end if;
11861189

@@ -1476,6 +1479,7 @@ protected function translateModelCallBackendNB
14761479
output list<String> outLibs;
14771480
output String outFileDir;
14781481
output list<tuple<String, Values.Value>> resultValues;
1482+
output DAE.FunctionTree oldFunctionTree;
14791483
protected
14801484
Real timeSimCode=0.0, timeTemplates=0.0, timeBackend=0.0;
14811485
NBackendDAE bdae;
@@ -1493,7 +1497,7 @@ algorithm
14931497
timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
14941498
ExecStat.execStat("backend");
14951499

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

14981502
resultValues := {("timeTemplates", Values.REAL(timeTemplates)),
14991503
("timeSimCode", Values.REAL(timeSimCode)),

0 commit comments

Comments
 (0)