Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 795a54a

Browse files
Merge pull request #179 from JuliaDiffEq/MOL
MOL discretization start
2 parents a680382 + acba340 commit 795a54a

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1111
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
1212
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1313
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
14+
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1415
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1516
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1617
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
1718

1819
[compat]
19-
DiffEqBase = ">= 5.19.0"
20+
DiffEqBase = "6"
21+
ModelingToolkit = "0.8.0"
2022
julia = "1"
2123

2224
[extras]

src/DiffEqOperators.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using DiffEqBase, StaticArrays, LinearAlgebra
55
import LinearAlgebra: mul!, ldiv!, lmul!, rmul!, axpy!, opnorm, factorize, I
66
import DiffEqBase: AbstractDiffEqLinearOperator, update_coefficients!, is_constant
77
using SparseArrays, ForwardDiff, BandedMatrices, NNlib, LazyArrays, BlockBandedMatrices
8+
using ModelingToolkit
89

910
abstract type AbstractDerivativeOperator{T} <: AbstractDiffEqLinearOperator{T} end
1011
abstract type AbstractDiffEqCompositeOperator{T} <: AbstractDiffEqLinearOperator{T} end
@@ -33,10 +34,11 @@ include("derivative_operators/concretization.jl")
3334
include("derivative_operators/ghost_derivative_operator.jl")
3435
include("derivative_operators/derivative_operator_functions.jl")
3536

36-
3737
### Composite Operators
3838
include("composite_operators.jl")
3939

40+
include("MOL_discretization.jl")
41+
4042
# The (u,p,t) and (du,u,p,t) interface
4143
for T in [DiffEqScaledOperator, DiffEqOperatorCombination, DiffEqOperatorComposition]
4244
(L::T)(u,p,t) = (update_coefficients!(L,u,p,t); L * u)
@@ -50,6 +52,6 @@ export AbstractDerivativeOperator, DerivativeOperator,
5052
export DirichletBC, Dirichlet0BC, NeumannBC, Neumann0BC, RobinBC, GeneralBC, MultiDimBC, PeriodicBC,
5153
MultiDimDirectionalBC, ComposedMultiDimBC,
5254
compose, decompose, perpsize
53-
5455
export GhostDerivativeOperator
56+
export MOLFiniteDifference
5557
end # module

src/MOL_discretization.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct MOLFiniteDifference{T} <: DiffEqBase.AbstractDiscretization
2+
dxs::T
3+
order::Int
4+
end
5+
MOLFiniteDifference(args...;order=2) = MOLFiniteDifference(args,order)
6+
7+
function DiffEqBase.discretize(pdesys::PDESystem,discretization::MOLFiniteDifference)
8+
tdomain = pdesys.domain[1].domain
9+
domain = pdesys.domain[2].domain
10+
@assert domain isa IntervalDomain
11+
len = domain.upper - domain.lower
12+
dx = discretization.dxs[1]
13+
interior = domain.lower+dx:dx:domain.upper-dx
14+
X = domain.lower:dx:domain.upper
15+
L = CenteredDifference(2,2,dx,Int(len/dx)-2)
16+
Q = DirichletBC(0.0,0.0)
17+
function f(du,u,p,t)
18+
mul!(du,L,Array(Q*u))
19+
end
20+
u0 = @. - interior * (interior - 1) * sin(interior)
21+
PDEProblem(ODEProblem(f,u0,(tdomain.lower,tdomain.upper),nothing),Q,X)
22+
end

test/MOLtest.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using ModelingToolkit, DiffEqOperators, DiffEqBase, LinearAlgebra
2+
3+
# Define some variables
4+
@parameters t x
5+
@variables u(..)
6+
@derivatives Dt'~t
7+
@derivatives Dxx''~x
8+
eq = Dt(u(t,x)) ~ Dxx(u(t,x))
9+
bcs = [u(0,x) ~ - x * (x-1) * sin(x),
10+
u(t,0) ~ 0, u(t,1) ~ 0]
11+
12+
domains = [t IntervalDomain(0.0,1.0),
13+
x IntervalDomain(0.0,1.0)]
14+
15+
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
16+
discretization = MOLFiniteDifference(0.1)
17+
prob = discretize(pdesys,discretization) # This gives an ODEProblem since it's time-dependent
18+
19+
using OrdinaryDiffEq
20+
sol = solve(prob,Tsit5(),saveat=0.1)

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ import Base: isapprox
1717
@time @safetestset "Differentiation Dimension" begin include("differentiation_dimension.jl") end
1818
@time @safetestset "2D and 3D fast multiplication" begin include("2D_3D_fast_multiplication.jl") end
1919
@time @safetestset "Higher Dimensional Concretization" begin include("concretization.jl") end
20-
@time @safetestset "Upwind Operator Interface" begin include("upwind_operators_interface.jl") end
20+
@time @safetestset "Upwind Operator Interface" begin include("upwind_operators_interface.jl") end
21+
@time @safetestset "MOLFiniteDifference Interface" begin include("MOLtest.jl") end

0 commit comments

Comments
 (0)