diff --git a/Project.toml b/Project.toml index ed98911..6c5527e 100644 --- a/Project.toml +++ b/Project.toml @@ -16,7 +16,7 @@ TSVD = "9449cd9e-2762-5aa3-a617-5413e99d722e" [compat] DocStringExtensions = "0.9.5" LinearAlgebra = "1" -ModelingToolkit = "11.42" +ModelingToolkit = "11.44" PrecompileTools = "1.2.1" SciMLBase = "3.48" SparseArrays = "1" diff --git a/src/deim.jl b/src/deim.jl index c1a38b0..0194579 100644 --- a/src/deim.jl +++ b/src/deim.jl @@ -346,7 +346,7 @@ function _deim( name::Symbol; snapshot_times = nothing, training_parameters = Dict{Any, Any}(), tspan = nothing, kwargs... ) - rows = length(ModelingToolkit.unknowns(sys)) + rows = length(scalar_unknowns(sys)) size(snapshot, 1) == rows || throw( DimensionMismatch( "the snapshot has $(size(snapshot, 1)) rows, but the source system has $rows unknowns" @@ -383,10 +383,10 @@ end Reduce a first-order `ModelingToolkit.System` with Proper Orthogonal Decomposition (POD) and the Discrete Empirical Interpolation Method (DEIM). -The rows of `snapshot` follow `ModelingToolkit.unknowns(sys)` and each column is one -time instance. The state basis is the POD basis of `snapshot`, and the DEIM basis is the -POD basis of the nonlinear terms evaluated at the snapshot columns. Both bases are -computed with [`TSVD`](@ref). +The rows of `snapshot` follow the scalar unknowns of `sys` (array-valued unknowns expanded +in column-major order) and each column is one time instance. The state basis is the POD +basis of `snapshot`, and the DEIM basis is the POD basis of the nonlinear terms evaluated +at the snapshot columns. Both bases are computed with [`TSVD`](@ref). `sys` may contain algebraic equations or array equations. It is structurally simplified as needed, which may scalarize the equations offline, but the returned system always has @@ -516,7 +516,7 @@ function deim( first(sol.t) == first(prob.tspan) || throw(ArgumentError("the solution must save the state at the start of the problem")) snapshot = Array(sol) - size(snapshot, 1) == length(ModelingToolkit.unknowns(sys)) || + size(snapshot, 1) == length(scalar_unknowns(sys)) || throw(DimensionMismatch("solution states must match the system unknowns")) training_parameters = Dict{Any, Any}() diff --git a/src/full_order.jl b/src/full_order.jl index 316e374..8c727d2 100644 --- a/src/full_order.jl +++ b/src/full_order.jl @@ -59,11 +59,11 @@ struct FullOrderModel{S, T} end function FullOrderModel(source::System; training_parameters = Dict{Any, Any}()) - source_unknowns = Any[Symbolics.unwrap(unknown) for unknown in ModelingToolkit.unknowns(source)] + source_unknowns = scalar_unknowns(source) fields = _source_fields(source_unknowns) sys = _explicit_ode(source) iv = ModelingToolkit.independent_variable(sys) - unknowns = Any[Symbolics.unwrap(unknown) for unknown in ModelingToolkit.unknowns(sys)] + unknowns = scalar_unknowns(sys) differential, algebraic = _split_equations(ModelingToolkit.full_equations(sys)) isempty(algebraic) || throw( @@ -121,6 +121,28 @@ function FullOrderModel(source::System; training_parameters = Dict{Any, Any}()) ) end +""" +$(TYPEDSIGNATURES) + +Scalar unknowns of `sys` in snapshot row order. + +Array-valued unknowns are expanded into their elements in column-major order, the order +`ModelingToolkit` uses for `u0` and the solution rows. Scalar unknowns are kept as they are. +""" +function scalar_unknowns(sys::System) + unknowns = Any[] + for unknown in ModelingToolkit.unknowns(sys) + value = Symbolics.unwrap(unknown) + if SymbolicUtils.symtype(value) <: AbstractArray + elements = vec(Symbolics.scalarize(Symbolics.wrap(value))) + append!(unknowns, Symbolics.unwrap.(elements)) + else + push!(unknowns, value) + end + end + return unknowns +end + _is_differential(equation::Equation) = ( lhs = Symbolics.unwrap(equation.lhs); SymbolicUtils.iscall(lhs) && SymbolicUtils.operation(lhs) isa Differential @@ -134,7 +156,7 @@ end function _explicit_ode(source::System) differential, algebraic = _split_equations(ModelingToolkit.equations(source)) - if isempty(algebraic) && length(differential) == length(ModelingToolkit.unknowns(source)) + if isempty(algebraic) && length(differential) == length(scalar_unknowns(source)) return source end return mtkcompile(source) diff --git a/src/pod.jl b/src/pod.jl index 36ef3cf..f764176 100644 --- a/src/pod.jl +++ b/src/pod.jl @@ -310,7 +310,7 @@ function _pod( snapshot_times = nothing, training_parameters = Dict{Any, Any}(), tspan = nothing, kwargs... ) - rows = length(ModelingToolkit.unknowns(sys)) + rows = length(scalar_unknowns(sys)) size(snapshot, 1) == rows || throw( DimensionMismatch( "the snapshot has $(size(snapshot, 1)) rows, but the source system has $rows unknowns" @@ -371,8 +371,9 @@ end Reduce a first-order `ModelingToolkit.System` with Proper Orthogonal Decomposition (POD) Galerkin projection, without Discrete Empirical Interpolation (DEIM). -The rows of `snapshot` follow `ModelingToolkit.unknowns(sys)` and each column is one time -instance. The state basis is the POD basis of `snapshot`, computed with [`TSVD`](@ref). +The rows of `snapshot` follow the scalar unknowns of `sys` (array-valued unknowns expanded +in column-major order) and each column is one time instance. The state basis is the POD +basis of `snapshot`, computed with [`TSVD`](@ref). When the source system is an explicit first-order model whose differential equations are written in terms of the source field variables (including array equations), the reduced @@ -495,7 +496,7 @@ function pod( first(sol.t) == first(prob.tspan) || throw(ArgumentError("the solution must save the state at the start of the problem")) snapshot = Array(sol) - size(snapshot, 1) == length(ModelingToolkit.unknowns(sys)) || + size(snapshot, 1) == length(scalar_unknowns(sys)) || throw(DimensionMismatch("solution states must match the system unknowns")) training_parameters = Dict{Any, Any}() diff --git a/test/deim.jl b/test/deim.jl index abaaf53..494b508 100644 --- a/test/deim.jl +++ b/test/deim.jl @@ -237,7 +237,9 @@ function reduced_system_size(intervals) [x => local_dx], t; approx_order = order ) local_sys, _ = symbolic_discretize(pde_sys, local_discretization) - rows = length(ModelingToolkit.get_unknowns(local_sys)) + rows = sum(ModelingToolkit.get_unknowns(local_sys); init = 0) do unknown + length(ModelingToolkit.Symbolics.scalarize(unknown)) + end local_snapshot = [ sin(0.13 * i * j) + cos(0.07 * i * (j + 1)) for i in 1:rows, j in 1:8 ] @@ -333,6 +335,64 @@ end @test norm(scalar_reconstruction - dae_reconstruction) / norm(truth) < 1.0e-6 end +@testset "array-valued unknown sources" begin + @variables r(t)[1:4] + r0 = [0.4, 0.3, 0.2, 0.1] + @named array_source = System( + [Dt(r) ~ -r - r .^ 3], t, [r], []; + initial_conditions = [r => r0, Dt(r) => -r0 - r0 .^ 3], + ) + full_problem = DAEProblem( + complete(array_source), nothing, (0.0, 0.5); build_initializeprob = false + ) + full_solution = solve(full_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(full_solution) + + rom = deim(full_problem, full_solution, 2) + @test length(ModelingToolkit.get_eqs(rom)) == 1 + rom_problem = DAEProblem(rom, nothing, full_problem.tspan; build_initializeprob = false) + rom_solution = solve(rom_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(rom_solution) + truth = reduce(hcat, full_solution[r]) + reconstruction = reduce( + hcat, [ + SII.observed(rom, r)(state, rom_problem.p, time) + for (state, time) in zip(rom_solution.u, rom_solution.t) + ] + ) + @test norm(reconstruction - truth) / norm(truth) < 0.01 +end + +@testset "mixed scalar and array unknowns with an array reduction" begin + @variables a(t) u(t)[1:2, 1:2] v(t) w(t)[1:2, 1:3, 1:2] + a0 = 0.9; u0 = [0.1 0.3; 0.2 0.4]; v0 = 0.5; w0 = reshape(collect(1:12) ./ 100, 2, 3, 2) + @named mixed = System( + [Dt(a) ~ -a + v, Dt(u) ~ -u .^ 2 .+ a, Dt(v) ~ v + u[1, 1] - sum(w), Dt(w) ~ -w .+ v], t, + [a, u, v, w], []; + initial_conditions = [ + a => a0, u => u0, v => v0, w => w0, Dt(a) => -a0 + v0, Dt(u) => -u0 .^ 2 .+ a0, + Dt(v) => v0 + u0[1, 1] - sum(w0), Dt(w) => -w0 .+ v0, + ], + ) + full_problem = DAEProblem(complete(mixed), nothing, (0.0, 0.3); build_initializeprob = false) + full_solution = solve(full_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(full_solution) + rom = deim(full_problem, full_solution, 4) + rom_problem = DAEProblem(rom, nothing, full_problem.tspan; build_initializeprob = false) + rom_solution = solve(rom_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(rom_solution) + for variable in (a, u, v, w) + truth = reduce(hcat, [vcat(x...) for x in full_solution[variable]]) + reconstruction = reduce( + hcat, [ + vcat(SII.observed(rom, variable)(state, rom_problem.p, time)...) + for (state, time) in zip(rom_solution.u, rom_solution.t) + ] + ) + @test norm(reconstruction - truth) / norm(truth) < 1.0e-4 + end +end + @testset "nonautonomous linear terms" begin # Two distinct decay rates give rank-two state and nonlinear snapshots, so two POD # and DEIM modes reproduce the full model up to solver tolerance. diff --git a/test/full_order.jl b/test/full_order.jl index 992046a..66c07c8 100644 --- a/test/full_order.jl +++ b/test/full_order.jl @@ -1,6 +1,7 @@ using Test, ModelOrderReduction using ModelingToolkit using ModelingToolkit: Symbolics +using SciMLBase: DAEProblem @independent_variables t @variables w(t) x(t) y(t) z(t) @@ -74,3 +75,27 @@ end ) @test collect(values(fom.parameter_values)) == [5.0] end + +@testset "FullOrderModel with array-valued unknowns" begin + @variables u(t)[1:2, 1:2] v(t) + u_scalars = Symbolics.unwrap.(vec(Symbolics.scalarize(u))) + u0 = [0.4 0.2; 0.3 0.1] + @named sys = System( + [D(u) ~ -u .^ 2 .+ sin(t), D(v) ~ v + u[1, 1]], t, [u, v], []; + initial_conditions = [u => u0, v => 0.5, D(u) => -u0 .^ 2, D(v) => 0.5 + u0[1, 1]], + ) + problem = DAEProblem(complete(sys), nothing, (0.0, 1.0); build_initializeprob = false) + @test problem.u0 == [vec(u0); 0.5] + + fom = ModelOrderReduction.FullOrderModel(complete(sys)) + @test all(isequal.(fom.source_unknowns, [u_scalars; Symbolics.unwrap(v)])) + @test length(fom.unknowns) == 5 + @test all(isequal.(fom.source_unknowns[fom.rows], fom.unknowns)) + @test fom.fields[1].shape == (2, 2) + @test all(isequal.(fom.source_unknowns[fom.fields[1].rows], u_scalars)) + + row_of(variable) = findfirst(isequal(Symbolics.unwrap(variable)), fom.unknowns) + @test fom.linear[row_of(v), row_of(u[1, 1])] == 1 + @test all(symbolically_zero(fom.forcing[row_of(u[i, j])] - sin(t)) for i in 1:2, j in 1:2) + @test all(symbolically_zero(fom.nonlinear[row_of(u[i, j])] + u[i, j]^2) for i in 1:2, j in 1:2) +end diff --git a/test/pod.jl b/test/pod.jl index 9836f77..15051b9 100644 --- a/test/pod.jl +++ b/test/pod.jl @@ -57,6 +57,34 @@ D = Differential(t) @test norm(reconstruction - truth) / norm(truth) < 0.01 end +@testset "array-valued unknown POD Galerkin" begin + @variables z(t)[1:4] + z0 = [0.4, 0.3, 0.2, 0.1] + @named array_system = System( + [D(z) ~ -z - z .^ 3], t, [z], []; + initial_conditions = [z => z0, D(z) => -z0 - z0 .^ 3], + ) + full_problem = DAEProblem( + complete(array_system), nothing, (0.0, 0.5); build_initializeprob = false + ) + full_solution = solve(full_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(full_solution) + + rom = pod(full_problem, full_solution, 2) + @test length(ModelingToolkit.get_eqs(rom)) == 1 + rom_problem = DAEProblem(rom, nothing, full_problem.tspan; build_initializeprob = false) + rom_solution = solve(rom_problem; saveat = 0.05, abstol = 1.0e-10, reltol = 1.0e-10) + @test successful_retcode(rom_solution) + truth = reduce(hcat, full_solution[z]) + reconstruction = reduce( + hcat, [ + SII.observed(rom, z)(state, rom_problem.p, time) + for (state, time) in zip(rom_solution.u, rom_solution.t) + ] + ) + @test norm(reconstruction - truth) / norm(truth) < 0.01 +end + @testset "POD graph size is grid-independent for array equations" begin function graph_size(n) @variables q(t)[1:n]