Skip to content

Commit 2dba007

Browse files
committed
fix(core): extract start values from dae attributes, pass experiment annotations to fmu xml
1 parent 94319ea commit 2dba007

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

packages/cli/src/commands/export-fmu.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ export const ExportFmu: CommandModule<{}, ExportFmuArgs> = {
159159
const simulator = new ModelicaSimulator(dae);
160160
simulator.prepare();
161161

162+
// Extract experiment annotation from the DAE as fallback for CLI flags
163+
const exp = dae.experiment;
164+
const startTime = args.startTime ?? args["start-time"] ?? exp.startTime;
165+
const stopTime = args.stopTime ?? args["stop-time"] ?? exp.stopTime;
166+
const stepSize = args.stepSize ?? args["step-size"] ?? exp.interval;
167+
162168
// FMU type flags
163169
const fmuType = {
164170
modelExchange: args.type === "me" || args.type === "both",
@@ -175,9 +181,9 @@ export const ExportFmu: CommandModule<{}, ExportFmuArgs> = {
175181
modelIdentifier,
176182
description: args.description,
177183
generationTool: "ModelScript CLI",
178-
startTime: args.startTime ?? args["start-time"],
179-
stopTime: args.stopTime ?? args["stop-time"],
180-
stepSize: args.stepSize ?? args["step-size"],
184+
startTime,
185+
stopTime,
186+
stepSize,
181187
fmuType,
182188
},
183189
simulator.stateVars,
@@ -197,9 +203,9 @@ export const ExportFmu: CommandModule<{}, ExportFmuArgs> = {
197203
modelIdentifier,
198204
description: args.description,
199205
generationTool: "ModelScript CLI",
200-
startTime: args.startTime ?? args["start-time"],
201-
stopTime: args.stopTime ?? args["stop-time"],
202-
stepSize: args.stepSize ?? args["step-size"],
206+
startTime,
207+
stopTime,
208+
stepSize,
203209
fmuType,
204210
includeSources: args.source !== false,
205211
includeModelJson: true,

packages/core/src/compiler/modelica/fmi.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,13 @@ function mapVariable(v: ModelicaVariable, valueRef: number): FmiScalarVariable {
207207
};
208208
if (v.description) sv.description = v.description;
209209

210-
// Extract start value from the binding expression (if it's a literal)
211-
if (v.expression) {
210+
// Extract start value: first check the 'start' attribute, then the binding expression
211+
const startAttr = v.attributes.get("start");
212+
if (startAttr) {
213+
const startVal = extractNumericLiteral(startAttr);
214+
if (startVal !== null) sv.start = startVal;
215+
}
216+
if (sv.start === undefined && v.expression) {
212217
const startVal = extractNumericLiteral(v.expression);
213218
if (startVal !== null) sv.start = startVal;
214219
}

packages/core/src/compiler/modelica/fmu-codegen.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,18 @@ function generateModelC(id: string, dae: ModelicaDAE, result: FmuResult): string
298298
}
299299
}
300300
}
301-
// Set start values for continuous variables
301+
// Set start values for continuous variables (from start attribute or binding)
302302
for (const v of dae.variables) {
303303
if (v.variability === null || v.variability === undefined) {
304304
const ref = vrMap.get(v.name);
305-
if (ref !== undefined && v.expression) {
306-
const cExpr = exprToC(v.expression);
307-
lines.push(` inst->vars[${ref}] = ${cExpr}; /* ${v.name} */`);
305+
if (ref !== undefined) {
306+
// Prefer the 'start' attribute over the binding expression
307+
const startAttr = v.attributes.get("start");
308+
const initExpr = startAttr ?? v.expression;
309+
if (initExpr) {
310+
const cExpr = exprToC(initExpr);
311+
lines.push(` inst->vars[${ref}] = ${cExpr}; /* ${v.name} */`);
312+
}
308313
}
309314
}
310315
}

0 commit comments

Comments
 (0)