Skip to content

Commit 1ec8b6b

Browse files
committed
fix(core): extract start values from dae attributes, emit only referenced variable aliases in c codegen
1 parent 101f6c4 commit 1ec8b6b

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,25 @@ function generateModelC(id: string, dae: ModelicaDAE, result: FmuResult): string
318318

319319
// ── getDerivatives function ──
320320
lines.push(`void ${id}_getDerivatives(${id}_Instance* inst) {`);
321-
lines.push(" double time = inst->time;");
322321

323-
// Create local aliases for readability
322+
// Collect all variable names referenced in derivative equations
323+
const referencedNames = new Set<string>();
324+
for (const eq of dae.equations) {
325+
if (!("expression1" in eq && "expression2" in eq)) continue;
326+
const simpleEq = eq as { expression1: ModelicaExpression; expression2: ModelicaExpression };
327+
collectReferencedNames(simpleEq.expression1, referencedNames);
328+
collectReferencedNames(simpleEq.expression2, referencedNames);
329+
}
330+
331+
// Emit time alias only if referenced
332+
if (referencedNames.has("time")) {
333+
lines.push(" double time = inst->time;");
334+
}
335+
336+
// Create local aliases only for referenced variables
324337
for (const sv of result.scalarVariables) {
325338
if (sv.causality === "independent") continue;
339+
if (!referencedNames.has(sv.name)) continue;
326340
const cName = varToC(sv.name);
327341
lines.push(` double ${cName} = inst->vars[${sv.valueReference}];`);
328342
}
@@ -692,3 +706,39 @@ function extractDerName(expr: unknown): string | null {
692706
}
693707
return null;
694708
}
709+
710+
/** Recursively collect all variable names referenced in an expression. */
711+
function collectReferencedNames(expr: unknown, names: Set<string>): void {
712+
if (!expr || typeof expr !== "object") return;
713+
if ("name" in expr) {
714+
const nameVal = (expr as { name: unknown }).name;
715+
if (typeof nameVal === "string") {
716+
if (nameVal.startsWith("der(") && nameVal.endsWith(")")) {
717+
names.add(nameVal.substring(4, nameVal.length - 1));
718+
} else {
719+
names.add(nameVal);
720+
}
721+
}
722+
}
723+
if ("operand" in expr) collectReferencedNames((expr as { operand: unknown }).operand, names);
724+
if ("operand1" in expr) collectReferencedNames((expr as { operand1: unknown }).operand1, names);
725+
if ("operand2" in expr) collectReferencedNames((expr as { operand2: unknown }).operand2, names);
726+
if ("condition" in expr) collectReferencedNames((expr as { condition: unknown }).condition, names);
727+
if ("thenExpression" in expr) collectReferencedNames((expr as { thenExpression: unknown }).thenExpression, names);
728+
if ("elseExpression" in expr) collectReferencedNames((expr as { elseExpression: unknown }).elseExpression, names);
729+
if ("args" in expr) {
730+
const args = (expr as { args: unknown[] }).args;
731+
if (Array.isArray(args)) {
732+
for (const arg of args) collectReferencedNames(arg, names);
733+
}
734+
}
735+
if ("elseIfClauses" in expr) {
736+
const clauses = (expr as { elseIfClauses: { condition: unknown; expression: unknown }[] }).elseIfClauses;
737+
if (Array.isArray(clauses)) {
738+
for (const clause of clauses) {
739+
collectReferencedNames(clause.condition, names);
740+
collectReferencedNames(clause.expression, names);
741+
}
742+
}
743+
}
744+
}

0 commit comments

Comments
 (0)