This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from akashkgarg/dev/issue-353
Generalizing MOLFiniteDifference to N-order PDEs
- Loading branch information
Showing
7 changed files
with
325 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# 1D diffusion problem | ||
|
||
# Packages and inclusions | ||
using ModelingToolkit,DiffEqOperators,LinearAlgebra,Test,OrdinaryDiffEq | ||
using ModelingToolkit: Differential | ||
|
||
# Beam Equation | ||
@test_broken begin | ||
@parameters x, t | ||
@variables u(..) | ||
Dt = Differential(t) | ||
Dtt = Differential(t)^2 | ||
Dx = Differential(x) | ||
Dxx = Differential(x)^2 | ||
Dx3 = Differential(x)^3 | ||
Dx4 = Differential(x)^4 | ||
|
||
g = -9.81 | ||
EI = 1 | ||
mu = 1 | ||
L = 10.0 | ||
dx = 0.4 | ||
|
||
eq = Dtt(u(t,x)) ~ -mu*EI*Dx4(u(t,x)) + mu*g | ||
|
||
bcs = [u(0, x) ~ 0, | ||
u(t,0) ~ 0, | ||
Dx(u(t,0)) ~ 0, | ||
Dxx(u(t, L)) ~ 0, | ||
Dx3(u(t, L)) ~ 0] | ||
|
||
# Space and time domains | ||
domains = [t ∈ IntervalDomain(0.0,1.0), | ||
x ∈ IntervalDomain(0.0,L)] | ||
|
||
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)]) | ||
discretization = MOLFiniteDifference([x=>dx],t, centered_order=4) | ||
prob = discretize(pdesys,discretization) | ||
end | ||
|
||
# Beam Equation with Velocity | ||
@test_broken begin | ||
@parameters x, t | ||
@variables u(..), v(..) | ||
Dt = Differential(t) | ||
Dtt = Differential(t)^2 | ||
Dx = Differential(x) | ||
Dxx = Differential(x)^2 | ||
Dx3 = Differential(x)^3 | ||
Dx4 = Differential(x)^4 | ||
|
||
g = -9.81 | ||
EI = 1 | ||
mu = 1 | ||
L = 10.0 | ||
dx = 0.4 | ||
|
||
eqs = [v(t, x) ~ Dt(u(t,x)), | ||
Dt(v(t,x)) ~ -mu*EI*Dx4(u(t,x)) + mu*g] | ||
|
||
bcs = [u(0, x) ~ 0, | ||
v(0, x) ~ 0, | ||
u(t,0) ~ 0, | ||
v(t,0) ~ 0, | ||
Dxx(u(t, L)) ~ 0, | ||
Dx3(u(t, L)) ~ 0] | ||
|
||
# Space and time domains | ||
domains = [t ∈ IntervalDomain(0.0,1.0), | ||
x ∈ IntervalDomain(0.0,L)] | ||
|
||
pdesys = PDESystem(eqs,bcs,domains,[t,x],[u(t,x),v(t,x)]) | ||
discretization = MOLFiniteDifference([x=>dx],t, centered_order=4) | ||
prob = discretize(pdesys,discretization) | ||
end | ||
|
||
@testset "Kuramoto–Sivashinsky equation" begin | ||
@parameters x, t | ||
@variables u(..) | ||
Dt = Differential(t) | ||
Dx = Differential(x) | ||
Dx2 = Differential(x)^2 | ||
Dx3 = Differential(x)^3 | ||
Dx4 = Differential(x)^4 | ||
|
||
α = 1 | ||
β = 4 | ||
γ = 1 | ||
eq = Dt(u(x,t)) ~ -u(x,t)*Dx(u(x,t)) - α*Dx2(u(x,t)) - β*Dx3(u(x,t)) - γ*Dx4(u(x,t)) | ||
|
||
u_analytic(x,t;z = -x/2+t) = 11 + 15*tanh(z) -15*tanh(z)^2 - 15*tanh(z)^3 | ||
du(x,t;z = -x/2+t) = 15/2*(tanh(z) + 1)*(3*tanh(z) - 1)*sech(z)^2 | ||
|
||
bcs = [u(x,0) ~ u_analytic(x,0), | ||
u(-10,t) ~ u_analytic(-10,t), | ||
u(10,t) ~ u_analytic(10,t), | ||
Dx(u(-10,t)) ~ du(-10,t), | ||
Dx(u(10,t)) ~ du(10,t)] | ||
|
||
# Space and time domains | ||
domains = [x ∈ IntervalDomain(-10.0,10.0), | ||
t ∈ IntervalDomain(0.0,1.0)] | ||
# Discretization | ||
dx = 0.4; dt = 0.2 | ||
|
||
discretization = MOLFiniteDifference([x=>dx],t;centered_order=4,grid_align=center_align) | ||
pdesys = PDESystem(eq,bcs,domains,[x,t],[u(x,t)]) | ||
prob = discretize(pdesys,discretization) | ||
|
||
sol = solve(prob,Tsit5(),saveat=0.1,dt=dt) | ||
|
||
@test sol.retcode == :Success | ||
|
||
xs = domains[1].domain.lower+dx+dx:dx:domains[1].domain.upper-dx-dx | ||
ts = sol.t | ||
|
||
u_predict = sol.u | ||
u_real = [[u_analytic(x, t) for x in xs] for t in ts] | ||
u_diff = u_real - u_predict | ||
@test_broken u_diff[:] ≈ zeros(length(u_diff)) atol=0.01; | ||
#plot(xs, u_diff) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,83 @@ | ||
using Test, LinearAlgebra | ||
using DiffEqOperators | ||
using ModelingToolkit | ||
|
||
@testset "utility functions" begin | ||
@test DiffEqOperators.unit_indices(2) == (CartesianIndex(1,0), CartesianIndex(0,1)) | ||
@test DiffEqOperators.add_dims(zeros(2,2), ndims(zeros(2,2)) + 2) == [6. 6.; 0. 0.; 0. 0.] | ||
@test DiffEqOperators.perpindex(collect(1:5), 3) == [1, 2, 4, 5] | ||
@test DiffEqOperators.perpsize(zeros(2,2,3,2), 3) == (2, 2, 2) | ||
end | ||
|
||
@testset "count differentials 1D" begin | ||
@parameters t x | ||
@variables u(..) | ||
Dt = Differential(t) | ||
|
||
Dx = Differential(x) | ||
eq = Dt(u(t,x)) ~ -Dx(u(t,x)) | ||
@test first(DiffEqOperators.differential_order(eq.rhs, x.val)) == 1 | ||
@test isempty(DiffEqOperators.differential_order(eq.rhs, t.val)) | ||
@test first(DiffEqOperators.differential_order(eq.lhs, t.val)) == 1 | ||
@test isempty(DiffEqOperators.differential_order(eq.lhs, x.val)) | ||
|
||
Dxx = Differential(x)^2 | ||
eq = Dt(u(t,x)) ~ Dxx(u(t,x)) | ||
@test first(DiffEqOperators.differential_order(eq.rhs, x.val)) == 2 | ||
@test isempty(DiffEqOperators.differential_order(eq.rhs, t.val)) | ||
@test first(DiffEqOperators.differential_order(eq.lhs, t.val)) == 1 | ||
@test isempty(DiffEqOperators.differential_order(eq.lhs, x.val)) | ||
|
||
Dxxxx = Differential(x)^4 | ||
eq = Dt(u(t,x)) ~ -Dxxxx(u(t,x)) | ||
@test first(DiffEqOperators.differential_order(eq.rhs, x.val)) == 4 | ||
@test isempty(DiffEqOperators.differential_order(eq.rhs, t.val)) | ||
@test first(DiffEqOperators.differential_order(eq.lhs, t.val)) == 1 | ||
@test isempty(DiffEqOperators.differential_order(eq.lhs, x.val)) | ||
end | ||
|
||
@testset "count differentials 2D" begin | ||
@parameters t x y | ||
@variables u(..) | ||
Dxx = Differential(x)^2 | ||
Dyy = Differential(y)^2 | ||
Dt = Differential(t) | ||
|
||
eq = Dt(u(t,x,y)) ~ Dxx(u(t,x,y)) + Dyy(u(t,x,y)) | ||
@test first(DiffEqOperators.differential_order(eq.rhs, x.val)) == 2 | ||
@test first(DiffEqOperators.differential_order(eq.rhs, y.val)) == 2 | ||
@test isempty(DiffEqOperators.differential_order(eq.rhs, t.val)) | ||
@test first(DiffEqOperators.differential_order(eq.lhs, t.val)) == 1 | ||
@test isempty(DiffEqOperators.differential_order(eq.lhs, x.val)) | ||
@test isempty(DiffEqOperators.differential_order(eq.lhs, y.val)) | ||
end | ||
|
||
@testset "count with mixed terms" begin | ||
@parameters t x y | ||
@variables u(..) | ||
Dxx = Differential(x)^2 | ||
Dyy = Differential(y)^2 | ||
Dx = Differential(x) | ||
Dy = Differential(y) | ||
Dt = Differential(t) | ||
|
||
eq = Dt(u(t,x,y)) ~ Dxx(u(t,x,y)) + Dyy(u(t,x,y)) + Dx(Dy(u(t,x,y))) | ||
@test DiffEqOperators.differential_order(eq.rhs, x.val) == Set([2, 1]) | ||
@test DiffEqOperators.differential_order(eq.rhs, y.val) == Set([2, 1]) | ||
end | ||
|
||
@testset "Kuramoto–Sivashinsky equation" begin | ||
@parameters x, t | ||
@variables u(..) | ||
Dt = Differential(t) | ||
Dx = Differential(x) | ||
Dx2 = Differential(x)^2 | ||
Dx3 = Differential(x)^3 | ||
Dx4 = Differential(x)^4 | ||
|
||
α = 1 | ||
β = 4 | ||
γ = 1 | ||
eq = Dt(u(x,t)) + u(x,t)*Dx(u(x,t)) + α*Dx2(u(x,t)) + β*Dx3(u(x,t)) + γ*Dx4(u(x,t)) ~ 0 | ||
@test DiffEqOperators.differential_order(eq.lhs, x.val) == Set([4, 3, 2, 1]) | ||
end |
Oops, something went wrong.