-
-
Notifications
You must be signed in to change notification settings - Fork 32
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 #331 from xtalax/callbacks
Add discrete callbacks
- Loading branch information
Showing
12 changed files
with
77 additions
and
7 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
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,5 @@ | ||
function generate_cb_rules(II, s, depvars, derivweights, bmap, indexmap, terms) | ||
cbs = derivweights.callbacks | ||
cb_rules = [cb.sym => cb(s) for cb in cbs] | ||
return cb_rules | ||
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
struct MOLDiscCallback | ||
f::Function | ||
p | ||
sym | ||
end | ||
|
||
function MOLDiscCallback(f, disc_ps) | ||
symh = hash(f) | ||
sym = unwrap(first(@parameters(Symbol("MOLDiscCallback_$symh")))) | ||
return MOLDiscCallback(f, disc_ps, sym) | ||
end | ||
|
||
(M::MOLDiscCallback)(s) = M.f(s, M.p) |
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,38 @@ | ||
using ModelingToolkit, MethodOfLines, DomainSets, Test, Symbolics, SymbolicUtils, OrdinaryDiffEq | ||
|
||
@testset "Discrete callback" begin | ||
@parameters x, t | ||
@variables u(..) | ||
Dt = Differential(t) | ||
Dxx = Differential(x)^2 | ||
t_min = 0.0 | ||
t_max = 2.0 | ||
x_min = 0.0 | ||
x_max = 2.0 | ||
|
||
a = 0.1*pi | ||
|
||
eq = Dt(u(t, x)) ~ a*Dxx(u(t, x)) | ||
|
||
bcs = [u(t_min, x) ~ sinpi(x), u(t, x_min) ~ sinpi(t+x), u(t, x_max) ~ sinpi(t+x)] | ||
|
||
domains = [t ∈ Interval(t_min, t_max), x ∈ Interval(x_min, x_max)] | ||
|
||
@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x)]) | ||
|
||
cb = MOLDiscCallback((s, p) -> prod(p), [0.1, pi]) | ||
a_cb = cb.sym | ||
cbeq = Dt(u(t, x)) ~ a_cb*Dxx(u(t, x)) | ||
|
||
@named cbpdesys = PDESystem(cbeq, bcs, domains, [t, x], [u(t, x)]) | ||
|
||
disc = MOLFiniteDifference([x => 0.1], t; approx_order=2, callbacks = [cb]) | ||
|
||
prob = discretize(pdesys, disc) | ||
cbprob = discretize(cbpdesys, disc) | ||
|
||
sol1 = solve(prob, Tsit5(), saveat=0.1) | ||
sol2 = solve(cbprob, Tsit5(), saveat=0.1) | ||
|
||
@test sol1[u(t, x)] ≈ sol2[u(t, x)] | ||
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