From be19c989df87804b3187cc419aee377a9f17dd53 Mon Sep 17 00:00:00 2001 From: JKRT Date: Sun, 14 Jun 2026 19:32:24 +0200 Subject: [PATCH] Minor performance updates --- src/Backend/Causalize.jl | 46 +++++++++++++++++-------------- src/Runtime/Runtime.jl | 8 ------ src/SimulationCode/simCodeUtil.jl | 4 +-- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/Backend/Causalize.jl b/src/Backend/Causalize.jl index ed1b0de2..247d5c07 100644 --- a/src/Backend/Causalize.jl +++ b/src/Backend/Causalize.jl @@ -1595,54 +1595,60 @@ function _resolveIntVarsInSystem!(syst::BDAE.EQSYSTEM) local removedDefiners = OrderedSet{String}() for (i, eq) in enumerate(syst.orderedEqs) if eq isa BDAE.EQUATION - lhsIsInt = eq.lhs isa DAE.CREF && string(eq.lhs.componentRef) in intVarNames - rhsIsInt = eq.rhs isa DAE.CREF && string(eq.rhs.componentRef) in intVarNames + lhsName = eq.lhs isa DAE.CREF ? string(eq.lhs.componentRef) : nothing + rhsName = eq.rhs isa DAE.CREF ? string(eq.rhs.componentRef) : nothing + lhsIsInt = lhsName !== nothing && lhsName in intVarNames + rhsIsInt = rhsName !== nothing && rhsName in intVarNames #= If this integer is actually when-driven discrete state, leave the equation alone (it might be a continuous default / initial value assignment). =# - isWhenDriven = (lhsIsInt && string(eq.lhs.componentRef) in intVarsWrittenInWhen) || - (rhsIsInt && string(eq.rhs.componentRef) in intVarsWrittenInWhen) + isWhenDriven = (lhsIsInt && lhsName in intVarsWrittenInWhen) || + (rhsIsInt && rhsName in intVarsWrittenInWhen) if (lhsIsInt || rhsIsInt) && !isWhenDriven #= Drop a `intvar = literal` binding outright; capture the value. =# if lhsIsInt && _isLiteral(eq.rhs) keepEq[i] = false - push!(removedDefiners, string(eq.lhs.componentRef)) - valueMap[string(eq.lhs.componentRef)] = eq.rhs + push!(removedDefiners, lhsName) + valueMap[lhsName] = eq.rhs elseif rhsIsInt && _isLiteral(eq.lhs) keepEq[i] = false - push!(removedDefiners, string(eq.rhs.componentRef)) - valueMap[string(eq.rhs.componentRef)] = eq.lhs + push!(removedDefiners, rhsName) + valueMap[rhsName] = eq.lhs end #= int-to-int aliases (e.g. `auxiliary_n = auxiliary[2]`) are handled in the alias-chain follow-up pass below — we cannot decide here because the RHS may not yet be in removedDefiners. =# end elseif eq isa BDAE.ARRAY_EQUATION - leftIsInt = eq.left isa DAE.CREF && string(eq.left.componentRef) in allIntNames - rightIsInt = eq.right isa DAE.CREF && string(eq.right.componentRef) in allIntNames - isWhenDriven = (leftIsInt && string(eq.left.componentRef) in intVarsWrittenInWhen) || - (rightIsInt && string(eq.right.componentRef) in intVarsWrittenInWhen) + leftName = eq.left isa DAE.CREF ? string(eq.left.componentRef) : nothing + rightName = eq.right isa DAE.CREF ? string(eq.right.componentRef) : nothing + leftIsInt = leftName !== nothing && leftName in allIntNames + rightIsInt = rightName !== nothing && rightName in allIntNames + isWhenDriven = (leftIsInt && leftName in intVarsWrittenInWhen) || + (rightIsInt && rightName in intVarsWrittenInWhen) if (leftIsInt || rightIsInt) && !isWhenDriven if leftIsInt && _isLiteral(eq.right) keepEq[i] = false - push!(removedDefiners, string(eq.left.componentRef)) + push!(removedDefiners, leftName) elseif rightIsInt && _isLiteral(eq.left) keepEq[i] = false - push!(removedDefiners, string(eq.right.componentRef)) + push!(removedDefiners, rightName) end end elseif eq isa BDAE.COMPLEX_EQUATION - leftIsInt = eq.left isa DAE.CREF && string(eq.left.componentRef) in allIntNames - rightIsInt = eq.right isa DAE.CREF && string(eq.right.componentRef) in allIntNames - isWhenDriven = (leftIsInt && string(eq.left.componentRef) in intVarsWrittenInWhen) || - (rightIsInt && string(eq.right.componentRef) in intVarsWrittenInWhen) + leftName = eq.left isa DAE.CREF ? string(eq.left.componentRef) : nothing + rightName = eq.right isa DAE.CREF ? string(eq.right.componentRef) : nothing + leftIsInt = leftName !== nothing && leftName in allIntNames + rightIsInt = rightName !== nothing && rightName in allIntNames + isWhenDriven = (leftIsInt && leftName in intVarsWrittenInWhen) || + (rightIsInt && rightName in intVarsWrittenInWhen) if (leftIsInt || rightIsInt) && !isWhenDriven if leftIsInt && _isLiteral(eq.right) keepEq[i] = false - push!(removedDefiners, string(eq.left.componentRef)) + push!(removedDefiners, leftName) elseif rightIsInt && _isLiteral(eq.left) keepEq[i] = false - push!(removedDefiners, string(eq.right.componentRef)) + push!(removedDefiners, rightName) end end end diff --git a/src/Runtime/Runtime.jl b/src/Runtime/Runtime.jl index 94d65308..651eb98d 100644 --- a/src/Runtime/Runtime.jl +++ b/src/Runtime/Runtime.jl @@ -255,13 +255,6 @@ function solve(omProblem::OM_ProblemStructural, tspan, alg; kwargs...) @BACKEND_LOGGING @info "u values at Δt $(integrator.dt) & t = $(integrator.t)" integrator.u #= Check structural callbacks in order =# @BACKEND_LOGGING @info "Stepping at:" i.t - #= AUDIT (ombackend-bug-audit-2026-06-05 #13): retCode is computed but never - branched on. Harmless here (inside `for i in integrator`, which - self-terminates on integrator failure); the concerning twin site is the - OM_ProblemRecompilation `while true` loop, which has no retcode / - dt-collapse break and relies solely on sol.retcode. VSS path; add a break - when the VSS-recompilation pipeline is hardened. =# - retCode = check_error(integrator) for cb in structuralCallbacks if cb.structureChanged && cb.name != activeModeName @VSS_DEBUG @info "Structure changed at $(i.t) transition to $(cb.name) => $(cb.structureChanged)" @@ -522,7 +515,6 @@ function solve(omProblem::OM_ProblemRecompilation, tspan::Tuple, alg; kwargs...) local i = integrator local old_t = i.t #= Check structural callbacks in order =# - retCode = check_error(integrator) for j in 1:length(structuralCallbacks) local cb = structuralCallbacks[j] @VSS_DEBUG @info "Structure Changed? $(cb.structureChanged)" diff --git a/src/SimulationCode/simCodeUtil.jl b/src/SimulationCode/simCodeUtil.jl index 66f02a78..761b834d 100644 --- a/src/SimulationCode/simCodeUtil.jl +++ b/src/SimulationCode/simCodeUtil.jl @@ -602,7 +602,7 @@ function createEquationVariableBidirectionGraph(equations::AbstractVector, allBackendVars::VARS, stringToSimVarHT)::OrderedDict where{IF_EQS, WHEN_EQS, VARS} local eqCounter::Int = 0 - local variableEqMapping = OrderedDict() + local variableEqMapping = OrderedDict{String, Vector{Int}}() local unknownVariables = filter((x) -> BDAEUtil.isVariable(x.varKind), allBackendVars) #=TODO: The set of discrete variables are currently not in use. =# local discreteVariables = filter((x) -> BDAEUtil.isDiscrete(x.varKind), allBackendVars) @@ -696,7 +696,7 @@ function createEquationVariableBidirectionGraph(equations::RES_T, allBackendVars::VECTOR_VAR, stringToSimVarHT)::OrderedDict where{RES_T, VECTOR_VAR} local eqCounter::Int = 0 - local variableEqMapping = OrderedDict() + local variableEqMapping = OrderedDict{String, Vector{Int}}() local unknownVariables = filter((x) -> BDAEUtil.isVariable(x.varKind), allBackendVars) local discreteVariables = filter((x) -> BDAEUtil.isDiscrete(x.varKind), allBackendVars) local stateVariables = filter((x) -> BDAEUtil.isState(x.varKind), allBackendVars)