Skip to content

Commit 7fb4ba4

Browse files
committed
feat(core): extend AD, IA, and McCormick to support preserved array mode
1 parent 980eabc commit 7fb4ba4

4 files changed

Lines changed: 6 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ export function generateModelEvaluateJacobian(id: string, dae: ModelicaDAE, vars
670670

671671
// Gather target equations (der(x) = f(x,u))
672672
const derEqs: { state: string; rhs: ModelicaExpression }[] = [];
673-
for (const eq of dae.equations) {
673+
for (const eq of dae.sortedEquations.length > 0 ? dae.sortedEquations : dae.equations) {
674674
if (!("expression1" in eq && "expression2" in eq)) continue;
675675
const se = eq as { expression1: ModelicaExpression; expression2: ModelicaExpression };
676676
const ld = extractDer(se.expression1);
@@ -819,7 +819,7 @@ export function generateModelEvaluateHessian(id: string, dae: ModelicaDAE, vars:
819819
varMap.set("time", `inst->time`);
820820

821821
const derEqs: { state: string; rhs: ModelicaExpression }[] = [];
822-
for (const eq of dae.equations) {
822+
for (const eq of dae.sortedEquations.length > 0 ? dae.sortedEquations : dae.equations) {
823823
if (eq instanceof ModelicaArrayEquation) continue;
824824
if (!("expression1" in eq && "expression2" in eq)) continue;
825825
const se = eq as { expression1: ModelicaExpression; expression2: ModelicaExpression };

packages/core/src/compiler/modelica/ad-jacobian.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function evaluateTapeReverse(ops: TapeOp[], t: Float64Array, outputIndex:
239239
export function buildAdJacobian(dae: ModelicaDAE): ((t: number, y: number[]) => number[][]) | null {
240240
// Gather derivative equations: der(x) = f(x, u)
241241
const derEqs: { state: string; rhs: ModelicaExpression }[] = [];
242-
for (const eq of dae.equations) {
242+
for (const eq of dae.sortedEquations.length > 0 ? dae.sortedEquations : dae.equations) {
243243
if (!("expression1" in eq && "expression2" in eq)) continue;
244244
const se = eq as { expression1: ModelicaExpression; expression2: ModelicaExpression };
245245
const ld = extractDer(se.expression1);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class ModelicaDAE {
1818
description: string | null;
1919
classKind = "class";
2020
equations: ModelicaEquation[] = [];
21+
/** Equations sorted by BLT transformation for simulation. */
22+
sortedEquations: ModelicaEquation[] = [];
2123
algorithms: ModelicaStatement[][] = [];
2224
/** Equations from `initial equation` sections. */
2325
initialEquations: ModelicaEquation[] = [];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7907,7 +7907,7 @@ function tryFoldBuiltinFunction(functionName: string, args: ModelicaExpression[]
79077907
*/
79087908
export function findAlgebraicLoops(dae: ModelicaDAE): void {
79097909
const { sortedEquations, algebraicLoops } = performBltTransformation(dae);
7910-
dae.equations = sortedEquations;
7910+
dae.sortedEquations = sortedEquations;
79117911

79127912
if (algebraicLoops.length > 0) {
79137913
console.log(`[DAE] Found ${algebraicLoops.length} algebraic loop(s) in ${dae.name}`);

0 commit comments

Comments
 (0)