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

Fixing sol interface tests #357

Merged
merged 13 commits into from
Jan 25, 2024
Merged
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
16 changes: 10 additions & 6 deletions test/components/solution_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ using ModelingToolkit: Differential

solu = sol.original_sol[grid[u(t, x, y)]]

for i in 1:length(sol.t)
for j in 1:9
for k in 1:9
traditional_sol[i, j, k] = solu[i][j, k]
for t_i in 1:length(sol.t)
for x_i in 2:9
for y_i in 2:9
traditional_sol[t_i, x_i, y_i] = solu[t_i][x_i, y_i]
end
end
end

#periodic BCs are unknown to ODE, and thus to sol.original_sol
# so replace here
traditional_sol[:, :, 1] = traditional_sol[:, :, end];
traditional_sol[:, 1, :] = traditional_sol[:, end, :];
traditional_sol[:, 1, 1] .= 0.0; #corners pinned to zero

@test sol[u(t, x, y)] == traditional_sol

Expand Down Expand Up @@ -99,7 +103,7 @@ end

solu = map(d -> sol.original_sol[d], grid[u(x, y)])

@test sol[u(x, y)] == solu
@test_broken sol[u(x, y)] == solu

@test sol(pi / 4, pi / 4, dv=u(x, y)) isa Float64
@test sol(pi / 4, :)[1] isa Vector{Float64}
Expand Down
13 changes: 7 additions & 6 deletions test/pde_systems/MOL_1D_HigherOrder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using ModelingToolkit: Differential

# Beam Equation
#! Broken due to overlapping inner corner bc eqs - determine state sharing heuristic; sort by dorder and give precedence to higher
@test_broken begin #@testset "Test 00: Beam Equation" begin
@testset "Test 00: Beam Equation" begin
@parameters x, t
@variables u(..)
Dt = Differential(t)
Expand Down Expand Up @@ -36,13 +36,14 @@ using ModelingToolkit: Differential

@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
discretization = MOLFiniteDifference([x=>dx],t, approx_order=4)
prob = discretize(pdesys,discretization)
@test_broken discretize(pdesys,discretization) isa ODEProblem

sol = solve(prob, FBDF())
# prob = discretize(pdesys,discretization)
# sol = solve(prob, FBDF())
end

# Beam Equation with Velocity
@test_broken begin#@testset "Test 01: Beam Equation with velocity" begin
@testset "Test 01: Beam Equation with velocity" begin
@parameters x, t
@variables u(..), v(..)
Dt = Differential(t)
Expand Down Expand Up @@ -79,7 +80,7 @@ end
sol = solve(prob, FBDF())
end

@test_broken begin#@testset "KdV Single Soliton equation" begin
@testset "KdV Single Soliton equation" begin
@parameters x, t
@variables u(..)
Dt = Differential(t)
Expand Down Expand Up @@ -114,7 +115,7 @@ end

sol = solve(prob,FBDF(),saveat=0.1)

@test sol.retcode == SciMLBase.ReturnCode.Success
@test SciMLBase.successful_retcode(sol)

xs = sol[x]
ts = sol[t]
Expand Down
38 changes: 19 additions & 19 deletions test/pde_systems/MOL_1D_Integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ using MethodOfLines, ModelingToolkit, LinearAlgebra, Test, OrdinaryDiffEq, Domai
@test cumuSumsol ≈ exact atol = 0.36
end

@test_broken begin #@testset "Test 00: Test simple integration case (0 .. x), with sys transformation" begin
@testset "Test 00: Test simple integration case (0 .. x), with sys transformation" begin
# test integrals
@parameters t, x
@variables cumuSum(..)
@variables integrand(..) cumuSum(..)
Dt = Differential(t)
Dx = Differential(x)
xmin = 0.0
Expand All @@ -64,18 +64,18 @@ end

disc = MOLFiniteDifference([x => 120], t)

prob = discretize(pde_system, disc)

sol = solve(prob, Tsit5())
@test_broken (discretize(pde_system, disc) isa ODEProblem)
# prob = discretize(pde_system, disc)
# sol = solve(prob, Tsit5())

xdisc = sol[x]
tdisc = sol[t]
# xdisc = sol[x]
# tdisc = sol[t]

cumuSumsol = sol[cumuSum(t, x)]
# cumuSumsol = sol[cumuSum(t, x)]

exact = [asf(t_, x_) for t_ in tdisc, x_ in xdisc]
# exact = [asf(t_, x_) for t_ in tdisc, x_ in xdisc]

@test cumuSumsol ≈ exact atol = 0.36
# @test cumuSumsol ≈ exact atol = 0.36
end

@testset "Test 01: Test integration over whole domain, (xmin .. xmax)" begin
Expand Down Expand Up @@ -119,7 +119,7 @@ end
@test cumuSumsol ≈ exact atol = 0.3
end

@test_broken begin #@testset "Test 02: Test integration with arbitrary limits, (a .. b)" begin
@testset "Test 02: Test integration with arbitrary limits, (a .. b)" begin
# test integrals
@parameters t, x
@variables integrand(..) cumuSum(..)
Expand All @@ -145,16 +145,16 @@ end

disc = MOLFiniteDifference([x => 120], t)

prob = discretize(pde_system, disc)
@test_broken (discretize(pde_system, disc) isa ODEProblem)
# prob = discretize(pde_system, disc)
# sol = solve(prob, Tsit5(), saveat=0.1)

sol = solve(prob, Tsit5(), saveat=0.1)
# xdisc = sol[x]
# tdisc = sol[t]

xdisc = sol[x]
tdisc = sol[t]
# cumuSumsol = sol[cumuSum(t)]

cumuSumsol = sol[cumuSum(t)]

exact = [asf(t_, 3.0) - asf(t_, 0.5) for t_ in tdisc]
# exact = [asf(t_, 3.0) - asf(t_, 0.5) for t_ in tdisc]

@test cumuSumsol ≈ exact atol = 0.36
# @test cumuSumsol ≈ exact atol = 0.36
end
96 changes: 49 additions & 47 deletions test/pde_systems/MOL_1D_Linear_Convection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ end

@test sol[u(t, x)][end, 2:end] ≈ utrue atol = 0.1
end
@test_broken begin#@testset "Test 03: Dt(u(t,x)) ~ -Dx(v(t,x))*u(t,x)-v(t,x)*Dx(u(t,x)) with v(t,x)=1" begin

@testset "Test 03: Dt(u(t,x)) ~ -Dx(v(t,x))*u(t,x)-v(t,x)*Dx(u(t,x)) with v(t,x)=1" begin
# Parameters, variables, and derivatives
@parameters t x
@variables v(..) u(..)
Expand Down Expand Up @@ -409,49 +410,50 @@ end
end


# @test_broken begin #@testset "Test 05: Dt(u(t,x)) ~ -Dx(v(t,x)*u(t,x)) with v(t, x) ~ 1.0" begin
# # Parameters, variables, and derivatives
# @parameters t x
# @variables v(..) u(..)
# Dt = Differential(t)
# Dx = Differential(x)

# # 1D PDE and boundary conditions
# eq = [Dt(u(t, x)) ~ -Dx(v(t, x) * u(t, x)),
# v(t, x) ~ 1.0 ]
# asf(x) = (0.5 / (0.2 * sqrt(2.0 * 3.1415))) * exp(-(x - 1.0)^2 / (2.0 * 0.2^2))

# bcs = [u(0, x) ~ asf(x),
# u(t, 0) ~ u(t, 2),
# v(0, x) ~ v(t, 2)]

# # Space and time domains
# domains = [t ∈ Interval(0.0, 2.0),
# x ∈ Interval(0.0, 2.0)]

# # PDE system
# @named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x), v(t, x)])

# # Method of lines discretization
# dx = 2 / 80
# order = 1
# discretization = MOLFiniteDifference([x => dx], t)

# # Convert the PDE problem into an ODE problem
# prob = discretize(pdesys, discretization)

# # Solve ODE problem
# using OrdinaryDiffEq
# sol = solve(prob, FBDF(), saveat=0.1)
# x_interval = sol[x][2:end]
# utrue = asf.(x_interval)
# # Plot and save results
# # plot(x_sol, u, seriestype = :scatter,label="Analytic solution")
# # plot!(x_sol, sol.u[end], label="Numeric solution")
# # plot!(x_sol, u.-sol.u[end], label="Differential Error")

# # savefig("plots/MOL_Linear_Convection_Test00.png")

# @test_broken sol[u(t, x)][end, 2:end] ≈ utrue atol = 0.1
# # Too dispersive ^
# end
@testset "Test 05: Dt(u(t,x)) ~ -Dx(v(t,x)*u(t,x)) with v(t, x) ~ 1.0" begin
# Parameters, variables, and derivatives
@parameters t x
@variables v(..) u(..)
Dt = Differential(t)
Dx = Differential(x)

# 1D PDE and boundary conditions
eq = [Dt(u(t, x)) ~ -Dx(v(t, x) * u(t, x)),
v(t, x) ~ 1.0 ]
asf(x) = (0.5 / (0.2 * sqrt(2.0 * 3.1415))) * exp(-(x - 1.0)^2 / (2.0 * 0.2^2))

bcs = [u(0, x) ~ asf(x),
u(t, 0) ~ u(t, 2),
v(0, x) ~ v(t, 2)]

# Space and time domains
domains = [t ∈ Interval(0.0, 2.0),
x ∈ Interval(0.0, 2.0)]

# PDE system
@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x), v(t, x)])

# Method of lines discretization
dx = 2 / 80
order = 1
discretization = MOLFiniteDifference([x => dx], t)

# Convert the PDE problem into an ODE problem
@test_broken (discretize(pdesys, discretization) isa ODEProblem)

# prob = discretize(pdesys, discretization)
# Solve ODE problem
# using OrdinaryDiffEq
# sol = solve(prob, FBDF(), saveat=0.1)
# x_interval = sol[x][2:end]
# utrue = asf.(x_interval)
# # Plot and save results
# # plot(x_sol, u, seriestype = :scatter,label="Analytic solution")
# # plot!(x_sol, sol.u[end], label="Numeric solution")
# # plot!(x_sol, u.-sol.u[end], label="Differential Error")

# # savefig("plots/MOL_Linear_Convection_Test00.png")

# @test_broken sol[u(t, x)][end, 2:end] ≈ utrue atol = 0.1
# Too dispersive ^
end
2 changes: 1 addition & 1 deletion test/pde_systems/MOL_1D_Linear_Convection_WENO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ end

@test sol[u(t, x)][end, 2:end] ≈ utrue atol = 0.1
end
@test_broken begin #@testset "Test 03: Dt(u(t,x)) ~ -Dx(v(t,x))*u(t,x)-v(t,x)*Dx(u(t,x)) with v(t,x)=1" begin
@testset "Test 03: Dt(u(t,x)) ~ -Dx(v(t,x))*u(t,x)-v(t,x)*Dx(u(t,x)) with v(t,x)=1" begin
# Parameters, variables, and derivatives
@parameters t x
@variables v(..) u(..)
Expand Down
1 change: 0 additions & 1 deletion test/pde_systems/MOL_1D_Linear_Diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ end
end

@testset "Test 02: Dt(u(t,x)) ~ Dx(D(t,x))*Dx(u(t,x))+D(t,x)*Dxx(u(t,x))" begin
#@test_broken begin
# Parameters, variables, and derivatives
@parameters t x
@variables u(..) D(..)
Expand Down
30 changes: 15 additions & 15 deletions test/pde_systems/MOL_1D_Linear_Diffusion_NonUniform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -690,37 +690,37 @@ end
end
end

@test_broken begin #@testset "Test 12: linear diffusion, two variables, mixed BCs, different independent variables in a vector Order 2" begin
@testset "Test 12: linear diffusion, two variables, mixed BCs, different independent variables in a vector Order 2" begin
# Method of Manufactured Solutions
u_exact = (x, t) -> exp.(-t) * cos.(x)
v_exact = (y, t) -> exp.(-t) * sin.(y)

# Parameters, variables, and derivatives
@parameters t x y
@variables u(..)[1:2]
@variables u(..) v(..)
Dt = Differential(t)
Dx = Differential(x)
Dxx = Dx^2
Dy = Differential(y)
Dyy = Dy^2

# 1D PDE and boundary conditions
eqs = [Dt(u[1](t, x)) ~ Dxx(u[1](t, x)),
Dt(u[2](t, y)) ~ Dyy(u[2](t, y))]
bcs = [u[1](0, x) ~ cos(x),
u[2](0, y) ~ sin(y),
u[1](t, 0) ~ exp(-t),
Dx(u[1](t, 1)) ~ -exp(-t) * sin(1),
Dy(u[2](t, 0)) ~ exp(-t),
u[2](t, 2) ~ exp(-t) * sin(2)]
eqs = [Dt(u(t, x)) ~ Dxx(u(t, x)),
Dt(v(t, y)) ~ Dyy(v(t, y))]
bcs = [u(0, x) ~ cos(x),
v(0, y) ~ sin(y),
u(t, 0) ~ exp(-t),
Dx(u(t, 1)) ~ -exp(-t) * sin(1),
Dy(v(t, 0)) ~ exp(-t),
v(t, 2) ~ exp(-t) * sin(2)]

# Space and time domains
domains = [t ∈ Interval(0.0, 1.0),
x ∈ Interval(0.0, 1.0),
y ∈ Interval(0.0, 2.0)]

# PDE system
@named pdesys = PDESystem(eqs, bcs, domains, [t, x, y], [u[1](t, x), u[2](t, y)])
@named pdesys = PDESystem(eqs, bcs, domains, [t, x, y], [u(t, x), v(t, y)])

# Method of lines discretization
l = 100
Expand All @@ -740,17 +740,17 @@ end
# Solve ODE problem
sol = solve(prob, Tsit5(), saveat=0.1)

solu1 = sol[u[1](t, x)]
solu2 = sol[u[2](t, x)]
solu1 = sol[u(t, x)]
solu2 = sol[v(t, y)]

x_sol = sol[x]
y_sol = sol[y]
t_sol = sol[t]

# Test against exact solution
for i in 1:length(t_sol)
@test_broken all(isapprox.(u_exact(x_sol, t_sol[i]), solu1[i, :], atol=0.01))
@test_broken all(isapprox.(v_exact(y_sol, t_sol[i]), solu2[i, :], atol=0.01))
@test all(isapprox.(u_exact(x_sol, t_sol[i]), solu1[i, :], atol=0.01))
@test all(isapprox.(v_exact(y_sol, t_sol[i]), solu2[i, :], atol=0.01))
end
end

Expand Down
Loading
Loading