SciML / DiffEqOperators.jl

Linear operators for discretizations of differential equations and scientific machine learning (SciML)
https://docs.sciml.ai/DiffEqOperators/stable/
Other
283 stars 74 forks source link

how to write a linear combination of operators? #275

Open JianghuiDu opened 4 years ago

JianghuiDu commented 4 years ago
L = 50.0 # domain in depth cm
N = 5000 # number of grid cells, all species are defined at the center of the cells,
h = L/(N+1) # spatial steps
x = range(h, step=h, length=N)

u=0.25
D=0.0

Pe=u.*h/(D);
σ=1.0./tanh.(Pe).- 1.0./Pe;

Δ1 = CenteredDifference(1, 2,h, N);
Δ_up = UpwindDifference(1, 1,h, N,1.0); #forward 
Δ_down = UpwindDifference(1, 1,h, N,-1.0); #backward

bcAge = RobinBC((u,-D,0.0),(0.0,1.0,4.0),h,1);

I want a weighted average of centered and backward differences based on σ. This new operator should equal the centered difference when σ=0 and backward difference when σ=1 This works

#σ=1
Age = exp.(x)
# should equal backward difference
Δ1*bcAge*Age + -σ/2.0*(Δ_up*bcAge*Age) + -σ/2.0*(Δ_down*bcAge*Age)

but not

# I thought this would be the same as above
Δ1*bcAge*Age .+ -σ/2.0*Δ_up*bcAge*Age .+ -σ/2.0*Δ_down*bcAge*Age

Why do the brackets matter? I thought I could write in a format like this

Δ=Δ1 + -σ/2.0*Δ_up+ -σ/2.0*Δ_down

and do Δ*bcAge*Age but that doesn't work.

I also noticed that sometimes . is needed before "+/- etc. but other times not... What are the format required to do DiffEqOperatorCombination and DiffEqScaledOperator?

ChrisRackauckas commented 4 years ago

I think the brackets matter because after applying the coefficient it cannot do the composition, which is a little odd and something we should fix. Thanks for making note of this.