Skip to content

Commit 6dd49e6

Browse files
authored
[NB] find homotopy calls in algebraic loops (#13754)
* [NB] find homotopy calls in algebraic loops * [NSim] collect algebraic loops and jacobians from lambda_0 systems
1 parent d32ddf7 commit 6dd49e6

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ protected
5656
import Causalize = NBCausalize;
5757
import BVariable = NBVariable;
5858
import NBEquation.{Equation, EquationPointer, EquationPointers, EquationAttributes, Iterator};
59+
import Initialization = NBInitialization;
5960
import NBJacobian.JacobianType;
6061
import Matching = NBMatching;
6162
import Resizable = NBResizable;
@@ -153,6 +154,7 @@ public
153154
Option<Tearing> casual;
154155
Boolean linear "true if the loop is linear";
155156
Boolean mixed "true for systems that have discrete variables";
157+
Boolean homotopy "true if contains homotopy()";
156158
Solve.Status status;
157159
end ALGEBRAIC_LOOP;
158160

@@ -210,7 +212,7 @@ public
210212
then str;
211213

212214
case ALGEBRAIC_LOOP() algorithm
213-
str := StringUtil.headline_3("BLOCK" + indexStr + ": Algebraic Loop (Linear = " + boolString(comp.linear) + ", Mixed = " + boolString(comp.mixed) + ")");
215+
str := StringUtil.headline_3("BLOCK" + indexStr + ": Algebraic Loop (Linear = " + boolString(comp.linear) + ", Mixed = " + boolString(comp.mixed) + ", Homotopy = " + boolString(comp.homotopy) + ")");
214216
str := str + Tearing.toString(comp.strict, "Strict Tearing Set");
215217
if isSome(comp.casual) then
216218
str := str + Tearing.toString(Util.getOption(comp.casual), "Casual Tearing Set");
@@ -771,7 +773,7 @@ public
771773
case RESIZABLE_COMPONENT()then Equation.isDiscrete(Slice.getT(comp.eqn));
772774
case ENTWINED_COMPONENT() then List.all(list(isDiscrete(c) for c in comp.entwined_slices), bool_ident);
773775
case GENERIC_COMPONENT() then Equation.isDiscrete(Slice.getT(comp.eqn));
774-
case ALGEBRAIC_LOOP() then not comp.mixed;
776+
case ALGEBRAIC_LOOP() then comp.mixed;
775777
case ALIAS() then isDiscrete(comp.original);
776778
else algorithm
777779
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of wrong component: " + toString(comp)});
@@ -818,6 +820,7 @@ public
818820
Tearing tearingSet;
819821
Slice<VariablePointer> var_slice;
820822
Slice<EquationPointer> eqn_slice;
823+
Pointer<Boolean> homotopy = Pointer.create(false);
821824

822825
// Size 1 strong component
823826
// - case 1: sliced equation because of for-equation
@@ -880,12 +883,16 @@ public
880883
residual_eqns = comp_eqns,
881884
innerEquations = listArray({}),
882885
jac = NONE());
886+
for eqn in comp_eqns loop
887+
Equation.map(Pointer.access(Slice.getT(eqn)), function Initialization.containsHomotopyCall(b = homotopy));
888+
end for;
883889
then ALGEBRAIC_LOOP(
884890
idx = -1,
885891
strict = tearingSet,
886892
casual = NONE(),
887893
linear = false,
888894
mixed = false,
895+
homotopy = Pointer.access(homotopy),
889896
status = NBSolve.Status.IMPLICIT);
890897
end match;
891898
then comp;

OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBInitialization.mo

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -632,24 +632,29 @@ public
632632
input Boolean init "if init then replace with simplified, else replace with actual";
633633
input Pointer<Boolean> hasHom "output, determines if partition contains homotopy()";
634634
algorithm
635-
exp := match exp
636-
local
637-
Expression e;
638-
String name;
639-
Call call;
640-
case Expression.CALL(call = call as Call.TYPED_CALL()) algorithm
641-
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
642-
e := match name
643-
case "homotopy" algorithm
644-
Pointer.update(hasHom, true);
645-
then listGet(Call.arguments(exp.call), if init then 2 else 1);
646-
else exp;
647-
end match;
648-
then e;
649-
else exp;
650-
end match;
635+
if Expression.isCallNamed(exp, "homotopy") then
636+
exp := match exp
637+
local
638+
Call call;
639+
640+
case Expression.CALL(call = call as Call.TYPED_CALL()) algorithm
641+
Pointer.update(hasHom, true);
642+
then listGet(Call.arguments(exp.call), if init then 2 else 1);
643+
644+
else exp;
645+
end match;
646+
end if;
651647
end cleanupHomotopy;
652648

649+
function containsHomotopyCall
650+
input output Expression exp;
651+
input Pointer<Boolean> b;
652+
algorithm
653+
if not Pointer.access(b) then
654+
Pointer.update(b, Expression.isCallNamed(exp, "homotopy"));
655+
end if;
656+
end containsHomotopyCall;
657+
653658
function removeWhenEquation
654659
"this function checks if an equation has to be removed before initialization.
655660
true for: when branch without condition initial()"

OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ protected
5555
// Backend imports
5656
import Adjacency = NBAdjacency;
5757
import NBAdjacency.Solvability;
58+
import Causalize = NBCausalize;
5859
import BEquation = NBEquation;
60+
import Initialization = NBInitialization;
5961
import BJacobian = NBJacobian;
6062
import BVariable = NBVariable;
61-
import Causalize = NBCausalize;
6263
import Differentiate = NBDifferentiate;
6364
import Inline = NBInline;
6465
import Jacobian = NBackendDAE.BackendDAE;
@@ -163,36 +164,43 @@ public
163164
// dummy adjacency matrix, don't need it for implicit
164165
Adjacency.Matrix dummy = Adjacency.EMPTY(NBAdjacency.MatrixStrictness.FULL);
165166
StrongComponent new_comp;
167+
Pointer<Boolean> homotopy = Pointer.create(false);
166168
algorithm
167169
(comp, dummy, funcTree, index) := match comp
168170
// create implicit equations
169171
case StrongComponent.SINGLE_COMPONENT() algorithm
172+
Equation.map(Pointer.access(comp.eqn), function Initialization.containsHomotopyCall(b = homotopy));
170173
new_comp := StrongComponent.ALGEBRAIC_LOOP(
171174
idx = index,
172175
strict = singleImplicit(comp.var, comp.eqn),
173176
casual = NONE(),
174177
linear = false,
175178
mixed = false,
179+
homotopy = Pointer.access(homotopy),
176180
status = NBSolve.Status.IMPLICIT);
177181
then finalize(new_comp, dummy, funcTree, index, VariablePointers.empty(), EquationPointers.empty(), Pointer.create(0), kind);
178182

179183
case StrongComponent.MULTI_COMPONENT() algorithm
184+
Equation.map(Pointer.access(Slice.getT(comp.eqn)), function Initialization.containsHomotopyCall(b = homotopy));
180185
new_comp := StrongComponent.ALGEBRAIC_LOOP(
181186
idx = index,
182187
strict = singleImplicit(Slice.getT(listHead(comp.vars)), Slice.getT(comp.eqn)), // this is wrong! need to take all vars
183188
casual = NONE(),
184189
linear = false,
185190
mixed = false,
191+
homotopy = Pointer.access(homotopy),
186192
status = NBSolve.Status.IMPLICIT);
187193
then finalize(new_comp, dummy, funcTree, index, VariablePointers.empty(), EquationPointers.empty(), Pointer.create(0), kind);
188194

189195
case StrongComponent.RESIZABLE_COMPONENT() algorithm
196+
Equation.map(Pointer.access(Slice.getT(comp.eqn)), function Initialization.containsHomotopyCall(b = homotopy));
190197
new_comp := StrongComponent.ALGEBRAIC_LOOP(
191198
idx = index,
192199
strict = singleImplicit(Slice.getT(comp.var), Slice.getT(comp.eqn)),
193200
casual = NONE(),
194201
linear = false,
195202
mixed = false,
203+
homotopy = Pointer.access(homotopy),
196204
status = NBSolve.Status.IMPLICIT);
197205
then finalize(new_comp, dummy, funcTree, index, VariablePointers.empty(), EquationPointers.empty(), Pointer.create(0), kind);
198206

OMCompiler/Compiler/NSimCode/NSimCode.mo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ public
409409
//(daeModeData, modelInfo, jacA, simcode_map, simCodeIndices) := DaeModeData.createSparsityJacobian(daeModeData, modelInfo, Util.getOption(bdae.dae), simcode_map, simCodeIndices);
410410
//else
411411

412-
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := collectAlgebraicLoops(init, ode, algebraic, daeModeData, simCodeIndices, simcode_map);
412+
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := collectAlgebraicLoops(init, init_0, ode, algebraic, daeModeData, simCodeIndices, simcode_map);
413+
413414
for jac in jacobians loop
414415
if Util.isSome(jac.jac_map) then
415416
vars := SimVars.addSeedAndJacobianVars(vars, UnorderedMap.toList(Util.getOption(jac.jac_map)));
@@ -577,6 +578,7 @@ public
577578
"Collects algebraic loops from all systems (ode, init, init_0, dae, ...).
578579
ToDo: Add other systems once implemented!"
579580
input list<SimStrongComponent.Block> init;
581+
input list<SimStrongComponent.Block> init_0;
580582
input list<list<SimStrongComponent.Block>> ode;
581583
input list<list<SimStrongComponent.Block>> algebraic;
582584
input Option<DaeModeData> daeModeData;
@@ -588,7 +590,7 @@ public
588590
protected
589591
list<list<SimStrongComponent.Block>> dae_mode_blcks;
590592
algorithm
591-
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops({init}, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
593+
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops({init, init_0}, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
592594
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops(ode, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
593595
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops(algebraic, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
594596
if isSome(daeModeData) then

OMCompiler/Compiler/NSimCode/NSimStrongComponent.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ public
728728
indexSystem = simCodeIndices.nonlinearSystemIndex,
729729
size = listLength(crefs),
730730
jacobian = Pointer.create(jacobian),
731-
homotopy = false,
731+
homotopy = comp.homotopy,
732732
mixed = comp.mixed,
733733
torn = true
734734
);

OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSystem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ void initializeNonlinearSystemData(DATA *data, threadData_t *threadData, NONLINE
403403
nonlinsys->numberOfIterations = 0;
404404

405405
/* check if residual function pointer are valid */
406-
assertStreamPrint(threadData, ((0 != nonlinsys->residualFunc)) || ((nonlinsys->strictTearingFunctionCall != NULL) ? (0 != nonlinsys->strictTearingFunctionCall) : 0), "residual function pointer is invalid" );
406+
assertStreamPrint(threadData, (nonlinsys->residualFunc != NULL) || (nonlinsys->strictTearingFunctionCall != NULL), "residual function pointer is invalid");
407407

408408
/* check if analytical jacobian is created */
409409
if(nonlinsys->jacobianIndex != -1)

0 commit comments

Comments
 (0)