diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 24bedc5d..622a12e7 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -71,21 +71,56 @@ jobs: version: ${{ matrix.version }} arch: ${{ matrix.arch }} + # Master (pushes, the nightly) restores and saves the depot cache: the + # artifact store, package store, registries and the compiled-modules + # directory. The compiled cache is the biggest warm-run win - without it + # precompile reruns from scratch. - name: Cache Julia depot + if: github.event_name != 'pull_request' uses: julia-actions/cache@v2 with: - # Cache the artifact store, package store, registries and the - # compiled-modules directory. The compiled cache is the biggest - # warm-run win - without it precompile reruns from scratch. cache-name: ombackend include-matrix: true + # A pull request restores master's cache and saves none: each PR run + # saved 0.8-2.6 GB per OS, the repository went over its 10 GB and GitHub + # evicted master's caches. The paths and the key prefix are those of + # julia-actions/cache above (a cache is found only under the same paths). + - name: Depot cache paths (pull request) + if: github.event_name == 'pull_request' + id: depot + shell: bash + run: | + if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then + depot="${USERPROFILE//\\//}/.julia" + else + depot="$HOME/.julia" + fi + { + echo "paths<> "$GITHUB_OUTPUT" + echo "key=ombackend;os=${{ runner.os }};os=${{ matrix.os }};arch=${{ matrix.arch }};version=${{ matrix.version }};" >> "$GITHUB_OUTPUT" + + - name: Restore Julia depot from master (pull request) + if: github.event_name == 'pull_request' + uses: actions/cache/restore@v4 + with: + path: ${{ steps.depot.outputs.paths }} + key: ${{ steps.depot.outputs.key }} + restore-keys: ${{ steps.depot.outputs.key }} + enableCrossOsArchive: false + - name: Add OpenModelica Julia registry shell: julia --color=yes {0} run: | import Pkg Pkg.Registry.add("General") Pkg.Registry.add(Pkg.RegistrySpec(url = "https://github.com/OpenModelica/OpenModelicaRegistry.git")) + # a restored registry is as old as its cache (julia-actions/cache updates it on master; a PR's restore does not) + Pkg.Registry.update() - name: Develop the sibling checkouts shell: julia --color=yes --project=OMBackend.jl {0} @@ -100,6 +135,22 @@ jobs: with: project: OMBackend.jl + # Which of our packages the restored depot has precompiled for the test + # run (its flags: coverage on Linux), and why a cache is rejected + # (JULIA_DEBUG=loading: "Rejecting ... because ..."). Unchanged siblings + # should come from the cache; OMBackend itself is rebuilt when it changed. + - name: Precompile cache report + shell: bash + env: + JULIA_DEBUG: loading + COVERAGE_FLAG: ${{ runner.os == 'Linux' && format('--code-coverage=@{0}/OMBackend.jl', github.workspace) || '' }} + run: | + julia --color=yes --project=OMBackend.jl --depwarn=yes $COVERAGE_FLAG -e ' + for name in ("ImmutableList", "MetaModelica", "OMParser", "OMFrontend", "OMRuntimeExternalC", "OMBackend") + local id = Base.identify_package(name) + println(name, ": ", Base.isprecompiled(id; ignore_loaded = true) ? "cached" : "to precompile") + end' + - name: Run tests uses: julia-actions/julia-runtest@v1 env: diff --git a/Project.toml b/Project.toml index 9622acc0..8bbefdb3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OMBackend" uuid = "9400bd5f-7561-42f6-8760-03756753da50" -version = "0.11.2" +version = "0.11.3" authors = ["John Tinnerholm ", "Karim Abdelhak ", "AnHeuermann "] [deps] diff --git a/ext/OMBackendOMFrontendExt/translate.jl b/ext/OMBackendOMFrontendExt/translate.jl index e75487f8..116a1594 100644 --- a/ext/OMBackendOMFrontendExt/translate.jl +++ b/ext/OMBackendOMFrontendExt/translate.jl @@ -194,7 +194,7 @@ function _translateKept(fm::F.FlatModel, model::OMJLIR.Model, path::String, subj local result::Tuple{String, Expr} = translate(fm; functionList, BackendMode, warnMissingStartValues, eliminateNonDynamic, observedFilter, checkSimCode, omjlir = model) - (BackendMode == IMTK_MODE && !haskey(IMTKGen.BUILT, name)) || return result + (BackendMode == IMTK_MODE && !haskey(IMTKGen.BUILT, name) && !(name in IMTKGen.BUILT_AT_SIMULATE)) || return result reason = "does not build" catch err isFatal(err) && rethrow() diff --git a/src/Backend/Passes/IndexReduction/arrays.jl b/src/Backend/Passes/IndexReduction/arrays.jl index 7d602f2e..730af01d 100644 --- a/src/Backend/Passes/IndexReduction/arrays.jl +++ b/src/Backend/Passes/IndexReduction/arrays.jl @@ -47,13 +47,15 @@ element's reference as the scalarized model names it (`c[2].p.T` of the array `c record array's; `\$DER.c[2].T` of the backend's `\$DER.c.T`). `passesFollow`: the view is of a model as lowered, for the scalar passes (`scalarizeArrays(model; passesFollow = true)`), and keeps what they take as the frontend's scalarization does: a reference by a subscript known at run time only (`chooseRuntimeIndices!`), a record equation -of a call (`fieldEquations`). The view the array form's index reduction plans on does not. +of a call (`fieldEquations`). The view the array form's index reduction plans on does not. `structural[p]` the value +of the structural Integer parameter `p` (`structuralValues`): a range over it (`1:N`) has its translated value. """ struct Elements elements::Dict{Symbol, Vector{Symbol}} sizes::Dict{Symbol, Vector{Int}} array::Dict{Symbol, Tuple{Symbol, Vector{Int}}} crefs::Dict{Symbol, IR.ComponentRef} + structural::Dict{Symbol, Int} passesFollow::Bool end @@ -175,7 +177,7 @@ already (a record field array of a parameter, `expandRecordFieldArrays`). `passe """ function elements(variables::IR.VariableSet; passesFollow::Bool = false)::Elements local E = Elements(Dict{Symbol, Vector{Symbol}}(), Dict{Symbol, Vector{Int}}(), Dict{Symbol, Tuple{Symbol, Vector{Int}}}(), - Dict{Symbol, IR.ComponentRef}(), passesFollow) + Dict{Symbol, IR.ComponentRef}(), structuralValues(variables), passesFollow) local derived::Vector{Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}} = Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}[] #= the arrays of rolled components first (their elements may be another array's, kept beside them: rollComponents!) =# local rolledFirst::Vector{IR.Variable} = IR.Variable[v for v in variables if IR.isRolled(v.cref.parts[1].ty)] @@ -201,6 +203,50 @@ function elements(variables::IR.VariableSet; passesFollow::Bool = false)::Elemen return E end +""" + structuralValues(variables) -> Dict{Symbol, Int} + +The values of the structural Integer parameters of `variables` (`IR.PARAMETER(true)`: the frontend evaluated them, +the generated code holds their translated values) whose bindings fold to an Integer, also through other structural +parameters (`M = 2 * N`). +""" +function structuralValues(variables::IR.VariableSet)::Dict{Symbol, Int} + local values::Dict{Symbol, Int} = Dict{Symbol, Int}() + local todo::Vector{IR.Variable} = IR.Variable[v for v in variables + if v.kind isa IR.PARAMETER && v.kind.structural && v.binding !== nothing] + local progress::Bool = true + while progress && !isempty(todo) + progress = false + for k in length(todo):-1:1 + local value::Union{Nothing, Int} = foldInteger(withStructural(todo[k].binding, values)) + value === nothing && continue + values[todo[k].name] = value + deleteat!(todo, k) + progress = true + end + end + return values +end + +"`e` with the references to the structural parameters of `values` (`structuralValues`) replaced by their values." +function withStructural(e::IR.Expression, values::Dict{Symbol, Int})::IR.Expression + isempty(values) && return e + local replace = (@nospecialize(x::IR.Expression)) -> begin + (x isa IR.CREF && !IR.hasSubscripts(x.cref) && haskey(values, x.cref.name)) || return x + return IR.INTEGER(values[x.cref.name]) + end + return IR.mapExp(replace, e) +end + +""" + iterationRange(range, values, E) -> Union{Nothing, StepRange{Int, Int}} + +The values of a loop's `range` at the iterators `values`: literal once the iterators and the structural parameters +(`E.structural`) are replaced (`1:N`); `nothing` where it is not. +""" +iterationRange(range::IR.Expression, values::Dict{Symbol, Int}, E::Elements)::Union{Nothing, StepRange{Int, Int}} = + literalRange(withStructural(scalarExpression(range, values, E), E.structural)) + """ addDerivedElements!(E, v, operators, of) @@ -233,7 +279,7 @@ end made of), a new `Elements`; `E` not changed. NotHandled for an added array that is no backend-made one. """ function extendedElements(E::Elements, added::Vector{IR.Variable}, variables::IR.VariableSet)::Elements - local out = Elements(copy(E.elements), copy(E.sizes), copy(E.array), copy(E.crefs), E.passesFollow) + local out = Elements(copy(E.elements), copy(E.sizes), copy(E.array), copy(E.crefs), E.structural, E.passesFollow) local derived::Vector{Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}} = Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}[] for v in added v.ty isa IR.T_ARRAY || continue @@ -883,7 +929,7 @@ by `scalarExpression`). NotHandled for a for-equation over a range that is not c """ function scalarEquations!(out::Vector{IR.Equation}, origins::Vector{EquationOrigin}, eq::IR.FOR_EQUATION, origin::EquationOrigin, values::Dict{Symbol, Int}, E::Elements)::Nothing - local range = literalRange(scalarExpression(eq.iterator.range, values, E)) + local range::Union{Nothing, StepRange{Int, Int}} = iterationRange(eq.iterator.range, values, E) range === nothing && throw(NotHandled("a for-equation over a range that is not constant ($(IR.toString(eq.iterator.range)))")) local inner = copy(values) local paths = Vector{Int}[vcat(origin.path, b) for b in eachindex(eq.body)] @@ -980,7 +1026,7 @@ function scalarEquations!(out::Vector{IR.Equation}, origins::Vector{EquationOrig origin::EquationOrigin, values::Dict{Symbol, Int}, E::Elements)::Nothing local loop::Union{Nothing, IR.FOR_STATEMENT} = E.passesFollow ? independentLoop(eq.statements) : nothing local range::Union{Nothing, StepRange{Int, Int}} = - loop === nothing ? nothing : literalRange(scalarExpression(loop.iterator.range, values, E)) + loop === nothing ? nothing : iterationRange(loop.iterator.range, values, E) if loop !== nothing && range !== nothing #= one algorithm per iteration, as the frontend's scalarization has one per component =# local inner::Dict{Symbol, Int} = copy(values) @@ -1285,27 +1331,60 @@ function scalarizeArrays(model::IR.Model; passesFollow::Bool = false, cache::Union{Nothing, ScalarCache} = nothing)::Scalarized (length(model.continuous) == 1 && isempty(model.clocked) && isempty(model.modes)) || throw(NotHandled("arrays in a model of several partitions or structural modes")) - local E = elements(model.variables; passesFollow) - local variables = scalarVariables(model.variables, E) - local p = model.continuous[1] - local equations = IR.Equation[] - local origins = EquationOrigin[] + local E::Elements = elements(model.variables; passesFollow) + local (partition::IR.Partition, origins::Vector{EquationOrigin}) = scalarPartition(model.continuous[1], E, cache) + local scalar::IR.Model = IR.Model(model.name, scalarVariables(model.variables, E), IR.Partition[partition], model.clocked, + model.baseClocks, model.functions, model.initialMode, model.modes, model.transitions, + model.frontendModel) + return Scalarized(scalar, origins, E) +end + +""" + scalarPartition(p, E, cache) -> (partition, origins) + +The partition `p` with its equations and initial equations as their elements (`scalarEquationsOf!`) and its array +unknowns as their elements (`E`), and the origins of its element equations. +""" +function scalarPartition(p::IR.Partition, E::Elements, + cache::Union{Nothing, ScalarCache})::Tuple{IR.Partition, Vector{EquationOrigin}} + local equations::Vector{IR.Equation} = IR.Equation[] + local origins::Vector{EquationOrigin} = EquationOrigin[] for (i, eq) in enumerate(p.equations) scalarEquationsOf!(equations, origins, eq, i, E, cache) end - local initialEquations = IR.Equation[] - local initialOrigins = EquationOrigin[] + local initialEquations::Vector{IR.Equation} = IR.Equation[] + local initialOrigins::Vector{EquationOrigin} = EquationOrigin[] for (i, eq) in enumerate(p.initialEquations) scalarEquationsOf!(initialEquations, initialOrigins, eq, i, E, cache) end - local unknowns = Symbol[] + local unknowns::Vector{Symbol} = Symbol[] for n in p.unknowns haskey(E.elements, n) ? append!(unknowns, E.elements[n]) : push!(unknowns, n) end - local partition = IR.Partition(p.index, p.association, unknowns, equations, initialEquations) - local scalar = IR.Model(model.name, variables, IR.Partition[partition], model.clocked, model.baseClocks, model.functions, - model.initialMode, model.modes, model.transitions, model.frontendModel) - return Scalarized(scalar, origins, E) + return (IR.Partition(p.index, p.association, unknowns, equations, initialEquations), origins) +end + +""" + scalarizeModel(model) -> IR.Model + +`model` as lowered with its arrays as their elements, for the scalar passes (`scalarizeArrays` with `passesFollow`) +in every part of it: its continuous and clocked partitions, and each structural mode with its own variables. The +backend's scalarization (`backendScalarizedModel`) of a model the array form does not take. The model is not +changed. +""" +function scalarizeModel(model::IR.Model)::IR.Model + local E::Elements = elements(model.variables; passesFollow = true) + return IR.Model(model.name, scalarVariables(model.variables, E), + IR.Partition[first(scalarPartition(p, E, nothing)) for p in model.continuous], + IR.Partition[first(scalarPartition(p, E, nothing)) for p in model.clocked], model.baseClocks, + model.functions, model.initialMode, IR.StructuralMode[scalarMode(m) for m in model.modes], + model.transitions, model.frontendModel) +end + +"The structural mode `m` with its variables and its system as their elements (`scalarizeModel`)." +function scalarMode(m::IR.StructuralMode)::IR.StructuralMode + local E::Elements = elements(m.variables; passesFollow = true) + return IR.StructuralMode(m.name, scalarVariables(m.variables, E), first(scalarPartition(m.system, E, nothing))) end """ diff --git a/src/CodeGeneration/IMTKGen.jl b/src/CodeGeneration/IMTKGen.jl index dbd63c83..bb86f819 100644 --- a/src/CodeGeneration/IMTKGen.jl +++ b/src/CodeGeneration/IMTKGen.jl @@ -74,6 +74,9 @@ const DUMP_ENABLED = Ref(false) by the generated `Model(tspan)`: (problem, callbacks, ivs, _ivs_all, reducedSystem, tspan, pars, vars, irreducibleSyms). =# const BUILT = Dict{String, Tuple}() +#= The models whose build is their module's own simulate (structural transitions, sub-models: `generateIMTKCode` + skips the build at translate), so no `BUILT` entry: translated all the same. =# +const BUILT_AT_SIMULATE = Set{String}() #= Hash of (modelCode, build-affecting flags) for each cached build. A repeat translate (e.g. overwriteCache) whose regenerated code and flags are unchanged reuses the live module + cached build instead of re-eval'ing, which @@ -100,7 +103,7 @@ another mode): an older build must not answer for the current one. """ function forgetBuild(cname::String) _forgetReinit(cname) - for cache in (BUILT, BUILT_HASH, REDUCED_SYSTEMS, PRISTINE_P, TUNABLE_SETS) + for cache in (BUILT, BUILT_AT_SIMULATE, BUILT_HASH, REDUCED_SYSTEMS, PRISTINE_P, TUNABLE_SETS) delete!(cache, cname) end #= and its external objects (tables): kept, every translate of the model left its tables @@ -147,6 +150,7 @@ function generateIMTKCode(simCode::SC.SIM_CODE) _buildAndCache(modelName, modelCode) else forgetBuild(cname) + push!(BUILT_AT_SIMULATE, cname) @info "[IMTK GEN] structural / sub-model path; build-cache skipped (iMTK delegates to MTK simulate)" model = modelName end return (modelName, modelCode) diff --git a/src/Runtime/Runtime.jl b/src/Runtime/Runtime.jl index de1e6dc7..a47ca210 100644 --- a/src/Runtime/Runtime.jl +++ b/src/Runtime/Runtime.jl @@ -391,11 +391,11 @@ function solve(omProblem::OM_ProblemStructural, tspan, alg; kwargs...) local u0Dict = _buildTransferU0Dict(newSystem, oldSystem, integrator, cb.name, activeModeName, omProblem.topVariables) + #= MTK 10+: one operating point, the inherited unknowns and the new mode's parameter values =# local daeProb = ModelingToolkit.ODEProblem( newSystem.f.sys, - u0Dict, - (i.t, tspan[2]), - newSystem.p; + merge(u0Dict, _parameterValues(newSystem)), + (i.t, tspan[2]); build_initializeprob = true, warn_initialize_determined = false, callback = CallbackSet(cb.systemSpecificCallbacks, omProblem.callbackConditions...) @@ -501,6 +501,17 @@ function _isDAETransferTarget(newSystem)::Bool end end +""" + _parameterValues(problem::ODEProblem) -> Dict{Any, Any} + +The values of the parameters of the MTK system of `problem` in `problem.p` (symbolic indexing), for the operating +point of a problem rebuilt from that system (`ODEProblem(sys, op, tspan)`). +""" +function _parameterValues(problem::ODEProblem)::Dict{Any, Any} + local sys = problem.f.sys + return Dict{Any, Any}(p => problem.ps[p] for p in ModelingToolkit.parameters(sys)) +end + """ Build a Dict mapping MTK symbolic variables of the new mode to inherited numeric values pulled from the old mode's integrator (state first, then @@ -676,8 +687,9 @@ function _switchMode(change::AbstractStructuralChange, problem::ODEProblem, inte _fill_observed_u0!(newU0, oldSyms, newProblem, change.solutionAtChange) @VSS_DEBUG @info "[solve OM_ProblemRecompilation] new u0" newU0 anyNaN = any(isnan, newU0) #= In the latest world, as the steps: init runs the new mode's callbacks' initialization (a table's time events). =# + #= the solver options (`kwargs`: tolerances) go to init; reinit! takes none of them =# local newIntegrator::DEIntegrator = Base.invokelatest(init, newProblem, alg; force_dtmin = true, kwargs...) - Base.invokelatest(reinit!, newIntegrator, newU0; t0 = tChange, tf = last(tspan), reset_dt = true, kwargs...) + Base.invokelatest(reinit!, newIntegrator, newU0; t0 = tChange, tf = last(tspan), reset_dt = true) #= The structural event may trigger again in this step: reset after it. =# newIntegrator.dt = min(newIntegrator.dt, 1e-6) Base.invokelatest(step!, newIntegrator, newIntegrator.dt, true) diff --git a/src/backendAPI.jl b/src/backendAPI.jl index 02622989..630eb189 100644 --- a/src/backendAPI.jl +++ b/src/backendAPI.jl @@ -348,7 +348,7 @@ function clearCaches!(; models::Bool=true, cleared = String[] models && (CodeGeneration.freeAllExternalObjects!(); empty!(COMPILED_MODELS_MTK); - empty!(IMTKGen.BUILT); empty!(IMTKGen.BUILT_HASH); + empty!(IMTKGen.BUILT); empty!(IMTKGen.BUILT_AT_SIMULATE); empty!(IMTKGen.BUILT_HASH); empty!(IMTKGen.REDUCED_SYSTEMS); empty!(IMTKGen.PRISTINE_P); empty!(CodeGeneration.DAE_REINIT); empty!(Runtime.RECOMPILED_MODES); @@ -586,8 +586,8 @@ end """ backendScalarizedModel(lower::Function, name::String) -> Union{Nothing, OMJLIR.Model} -The model `name` (canonical) that `lower()` lowers to OMJLIR with its arrays kept, scalarized by the backend's -element view (`Passes.IndexReduction.scalarizeArrays`, the view for the scalar passes) and through the scalar +The model `name` (canonical) that `lower()` lowers to OMJLIR with its arrays kept, scalarized by the backend +(`Passes.IndexReduction.scalarizeModel`: its partitions and structural modes) and through the scalar passes with index reduction (`Passes.runPasses!`): what the frontend's scalarizing flatten, lowered and through the passes, gives, without flattening again. For the models the array form does not take (`reducedArrayModel`). `nothing` where the element view does not take the model (switched off, OMBACKEND_BACKEND_SCALARIZATION; what it @@ -601,7 +601,7 @@ function backendScalarizedModel(lower::Function, name::String)::Union{Nothing, O return nothing end return _arrayReductionStep(name, decline; route = "the backend's scalarization") do - Passes.runPasses!(Passes.IndexReduction.scalarizeArrays(lower(); passesFollow = true).model) + Passes.runPasses!(Passes.IndexReduction.scalarizeModel(lower())) end end diff --git a/test/runtests.jl b/test/runtests.jl index 7d48c317..7d840da4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -505,6 +505,9 @@ import .ExampleModels #= ── 20z. A failed build's simulate fails on the build's error ── =# include("failedBuildTests.jl") + #= ── 20z2. Variable structure systems on the arrays-kept route, scalarized by the backend ── =# + include("vssScalarizationTests.jl") + #= ── 21. The backend reads the frontend's flat model only ──────── =# @testset "No DAE in the backend" begin #= The frontend's DAE, the previous backend's BDAE and its SimulationCode are gone: the diff --git a/test/vssScalarizationTests.jl b/test/vssScalarizationTests.jl new file mode 100644 index 00000000..0174f384 --- /dev/null +++ b/test/vssScalarizationTests.jl @@ -0,0 +1,79 @@ +#= Variable structure systems on the arrays-kept route (OM's translate: `translateReducedArrays`): the backend + scalarizes the models the array form does not take, also their structural modes and a for-equation over a + structural parameter (`1:N`, N the target of a recompilation). Analytic values. =# + +"The model `model` of the Modelica text `program` with its arrays kept, translated as OM's translate does; its name." +function _vssTranslate(program, model::String)::String + local scode = OMFrontend.translateToSCode(program) + local (fm, cache) = OMFrontend.instantiateSCodeToFM(model, scode; scalarize = false) + @test OMBackend.translateReducedArrays(fm; functionList = OMFrontend.cacheToFunctionList(cache)) !== nothing + return OMBackend.canonicalName(model) +end + +"The value of `name` at the end of the solution `sol` of a mode." +_vssValue(sol, name::String)::Float64 = OMBackend.getVariableValues(sol, name)[end] + +@testset "Variable structure systems, scalarized by the backend" begin + @testset "Structural modes with arrays: the state carried over" begin + local program = OMFrontend.OMParser.Modelica""" + model VSSModes + model A + Real x[2](start = {1.0, 2.0}); + equation + for i in 1:2 loop + der(x[i]) = -x[i]; + end for; + end A; + model B + Real x[2](start = {1.0, 2.0}); + equation + der(x) = {1.0, 2.0}; + end B; + structuralmode A a; + structuralmode B b; + equation + initialStructuralState(a); + structuralTransition(a, b, time >= 0.5); + end VSSModes; + """ + local name::String = _vssTranslate(program, "VSSModes") + @test name in OMBackend.BACKEND_SCALARIZED_MODELS + OMBackend.resetFallbacks!() + local sols = OMBackend.simulateModel(name; tspan = (0.0, 1.0), reltol = 1e-10, abstol = 1e-10) + @test length(sols) == 2 + #= b's problem rebuilt from its system at the transition (MTK's initialization with the carried values): not the + vector u0 fallback, whose problem starts at 0 =# + @test !haskey(OMBackend.FALLBACKS, :vssTransferDAEInit) + @test sols[2].prob.tspan == (0.5, 1.0) + @test _vssValue(sols[1], "a_x[2]") ≈ 2 * exp(-0.5) atol = 1e-8 + #= x(0.5) carried into b, then x(1) = x(0.5) + {1, 2} * 0.5 =# + @test _vssValue(sols[2], "b_x[1]") ≈ exp(-0.5) + 0.5 atol = 1e-8 + @test _vssValue(sols[2], "b_x[2]") ≈ 2 * exp(-0.5) + 1.0 atol = 1e-8 + end + + @testset "Recompilation of an array size: a for-equation over 1:N" begin + local program = OMFrontend.OMParser.Modelica""" + model VSSGrow + parameter Integer N = 2; + Real x[N](start = {i for i in 1:N}); + equation + for i in 1:N loop + der(x[i]) = -x[i]; + end for; + when time > 0.5 then + recompilation(N, 3); + end when; + end VSSGrow; + """ + local name::String = _vssTranslate(program, "VSSGrow") + @test name in OMBackend.BACKEND_SCALARIZED_MODELS + #= solver options reach the recompiled mode's integrator (reinit! takes none of them) =# + local sols = OMBackend.simulateModel(name; tspan = (0.0, 1.0), reltol = 1e-10, abstol = 1e-10) + @test length(sols) == 2 + @test length(sols[1].u[end]) == 2 && length(sols[2].u[end]) == 3 + #= x[1], x[2] carried over; x[3] from its start value 3 at the recompilation =# + @test _vssValue(sols[2], "x[1]") ≈ exp(-1.0) atol = 1e-8 + @test _vssValue(sols[2], "x[2]") ≈ 2 * exp(-1.0) atol = 1e-8 + @test _vssValue(sols[2], "x[3]") ≈ 3 * exp(-0.5) atol = 1e-8 + end +end