Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

energies: adding energies to PDESystems #2256

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(DocStringExtensions.README)
"""
module ModelingToolkit
using PrecompileTools, Reexport
@recompile_invalidations begin
@recompile_invalidations begin
using DocStringExtensions
using Compat
using AbstractTrees
Expand Down
1 change: 1 addition & 0 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function complete(sys::AbstractSystem)
end

for prop in [:eqs
:energies
:tag
:noiseeqs
:iv
Expand Down
8 changes: 7 additions & 1 deletion src/systems/pde/pdesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ domains = [t ∈ (0.0,1.0),
struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
"The equations which define the PDE"
eqs::Any
"The energy functions"
energies::Any
"The boundary conditions"
bcs::Any
"The domain for the independent variables."
Expand Down Expand Up @@ -88,6 +90,7 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
@add_kwonly function PDESystem(eqs, bcs, domain, ivs, dvs,
ps = SciMLBase.NullParameters();
defaults = Dict(),
energies = [],
systems = [],
connector_type = nothing,
metadata = nothing,
Expand All @@ -101,6 +104,7 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
end

eqs = eqs isa Vector ? eqs : [eqs]
energies = energies isa Vector ? energies : [energies]

if !isnothing(analytic)
analytic = analytic isa Vector ? analytic : [analytic]
Expand All @@ -123,7 +127,8 @@ struct PDESystem <: ModelingToolkit.AbstractMultivariateSystem
analytic_func = analytic_func isa Dict ? analytic_func : analytic_func |> Dict
end

new(eqs, bcs, domain, ivs, dvs, ps, defaults, connector_type, systems, analytic,
new(eqs, energies, bcs, domain, ivs, dvs, ps, defaults, connector_type, systems,
analytic,
analytic_func, name, metadata, gui_metadata)
end
end
Expand All @@ -149,6 +154,7 @@ function Base.show(io::IO, ::MIME"text/plain", sys::PDESystem)
println(io, summary(sys))
println(io, "Equations: ", get_eqs(sys))
println(io, "Boundary Conditions: ", get_bcs(sys))
println(io, "Energies: ", get_energies(sys))
println(io, "Domain: ", get_domain(sys))
println(io, "Dependent Variables: ", get_dvs(sys))
println(io, "Independent Variables: ", get_ivs(sys))
Expand Down
30 changes: 29 additions & 1 deletion test/pde.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ using ModelingToolkit, DiffEqBase, LinearAlgebra, Test
@constants h = 1
@variables u(..)
Dt = Differential(t)
Dxx = Differential(x)^2
Dx = Differential(x)
Dxx = (Dx)^2
eq = Dt(u(t, x)) ~ h * Dxx(u(t, x))
bcs = [u(0, x) ~ -h * x * (x - 1) * sin(x),
u(t, 0) ~ 0, u(t, 1) ~ 0]
energies = [(Dx(u(t, x)))^2]

domains = [t ∈ (0.0, 1.0),
x ∈ (0.0, 1.0)]
Expand All @@ -27,3 +29,29 @@ dt = 0:0.1:1
# Test generated analytic_func
@test all(pdesys.analytic_func[u(t, x)]([2], disct, discx) ≈
analytic_function([2], disct, discx) for disct in dt, discx in dx)

# Test with energies
@named pdesys = PDESystem(eq,
bcs,
domains,
[t, x],
[u],
[h => 1],
energies = energies,
analytic = analytic)
@show pdesys

@test all(isequal.(independent_variables(pdesys), [t, x]))

# Test with explicit empty energies
@named pdesys = PDESystem(eq,
bcs,
domains,
[t, x],
[u],
[h => 1],
energies = [],
analytic = analytic)
@show pdesys

@test all(isequal.(independent_variables(pdesys), [t, x]))