Skip to content

Commit fd9bd50

Browse files
finish and test SDESystem
1 parent c2bc515 commit fd9bd50

File tree

16 files changed

+125
-34
lines changed

16 files changed

+125
-34
lines changed

src/ModelingToolkit.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
module ModelingToolkit
22

3-
export Operation, Expression
4-
export calculate_jacobian, generate_jacobian, generate_function
5-
export independent_variables, dependent_variables, parameters
6-
export simplified_expr, eval_function
7-
export @register, @I
8-
export modelingtoolkitize
9-
10-
113
using DiffEqBase, Distributed
124
using StaticArrays, LinearAlgebra, SparseArrays
135
using Latexify
6+
using MacroTools
147

158
using MacroTools
169
import MacroTools: splitdef, combinedef, postwalk, striplines
@@ -92,6 +85,7 @@ include("utils.jl")
9285
include("direct.jl")
9386
include("domains.jl")
9487
include("systems/diffeqs/odesystem.jl")
88+
include("systems/diffeqs/sdesystem.jl")
9589
include("systems/diffeqs/abstractodesystem.jl")
9690
include("systems/diffeqs/first_order_transform.jl")
9791
include("systems/diffeqs/modelingtoolkitize.jl")
@@ -100,4 +94,22 @@ include("systems/pde/pdesystem.jl")
10094
include("latexify_recipes.jl")
10195
include("build_function.jl")
10296

97+
export ODESystem, ODEFunction
98+
export SDESystem, SDEFunction
99+
export NonlinearSystem
100+
export ode_order_lowering
101+
export PDESystem
102+
export Differential, expand_derivatives, @derivatives
103+
export IntervalDomain, ProductDomain, , CircleDomain
104+
export Equation, ConstrainedEquation
105+
export simplify_constants
106+
107+
export Operation, Expression
108+
export calculate_jacobian, generate_jacobian, generate_function, generate_diffusion_function
109+
export independent_variables, dependent_variables, parameters
110+
export simplified_expr, eval_function
111+
export @register, @I
112+
export modelingtoolkitize
113+
export Variable, @variables, @parameters
114+
103115
end # module

src/differentials.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export Differential, expand_derivatives, @derivatives
2-
3-
41
"""
52
$(TYPEDEF)
63

src/domains.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export IntervalDomain, ProductDomain, , CircleDomain
2-
31
abstract type AbstractDomain{T,N} end
42

53
struct VarDomainPairing

src/equations.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export Equation, ConstrainedEquation
2-
3-
41
"""
52
$(TYPEDEF)
63

src/simplify.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export simplify_constants
2-
3-
41
function simplify_constants(O::Operation, shorten_tree)
52
while true
63
O′ = _simplify_constants(O, shorten_tree)

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function calculate_jacobian(sys::AbstractODESystem)
2121
end
2222

2323
struct ODEToExpr
24-
sys::ODESystem
24+
sys::AbstractODESystem
2525
end
2626
function (f::ODEToExpr)(O::Operation)
2727
if isa(O.op, Variable)

src/systems/diffeqs/first_order_transform.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export ode_order_lowering
2-
3-
41
function lower_varname(var::Variable, idv, order)
52
order == 0 && return var
63
name = Symbol(var.name, :_, string(idv.name)^order)

src/systems/diffeqs/odesystem.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export ODESystem, ODEFunction
2-
31
isintermediate(eq::Equation) = !(isa(eq.lhs, Operation) && isa(eq.lhs.op, Differential))
42

53
function flatten_differential(O::Operation)

src/systems/diffeqs/sdesystem.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,60 @@ function generate_diffusion_function(sys::SDESystem, dvs = sys.dvs, ps = sys.ps,
5151
ps′ = [clean(p) for p ps]
5252
return build_function(sys.noiseeqs, dvs′, ps′, (sys.iv.name,), ODEToExpr(sys), expression; kwargs...)
5353
end
54+
55+
"""
56+
$(SIGNATURES)
57+
58+
Create an `SDEFunction` from the [`SDESystem`](@ref). The arguments `dvs` and `ps`
59+
are used to set the order of the dependent variable and parameter vectors,
60+
respectively.
61+
"""
62+
function DiffEqBase.SDEFunction{iip}(sys::SDESystem, dvs = sys.dvs, ps = sys.ps;
63+
version = nothing, tgrad=false,
64+
jac = false, Wfact = false) where {iip}
65+
f_oop,f_iip = generate_function(sys, dvs, ps, Val{false})
66+
g_oop,g_iip = generate_diffusion_function(sys, dvs, ps, Val{false})
67+
68+
f(u,p,t) = f_oop(u,p,t)
69+
f(du,u,p,t) = f_iip(du,u,p,t)
70+
g(u,p,t) = g_oop(u,p,t)
71+
g(du,u,p,t) = g_iip(du,u,p,t)
72+
73+
if tgrad
74+
tgrad_oop,tgrad_iip = generate_tgrad(sys, dvs, ps, Val{false})
75+
_tgrad(u,p,t) = tgrad_oop(u,p,t)
76+
_tgrad(J,u,p,t) = tgrad_iip(J,u,p,t)
77+
else
78+
_tgrad = nothing
79+
end
80+
81+
if jac
82+
jac_oop,jac_iip = generate_jacobian(sys, dvs, ps, Val{false})
83+
_jac(u,p,t) = jac_oop(u,p,t)
84+
_jac(J,u,p,t) = jac_iip(J,u,p,t)
85+
else
86+
_jac = nothing
87+
end
88+
89+
if Wfact
90+
tmp_Wfact,tmp_Wfact_t = generate_factorized_W(sys, dvs, ps, true, Val{false})
91+
Wfact_oop, Wfact_iip = tmp_Wfact
92+
Wfact_oop_t, Wfact_iip_t = tmp_Wfact_t
93+
_Wfact(u,p,dtgamma,t) = Wfact_oop(u,p,dtgamma,t)
94+
_Wfact(W,u,p,dtgamma,t) = Wfact_iip(W,u,p,dtgamma,t)
95+
_Wfact_t(u,p,dtgamma,t) = Wfact_oop_t(u,p,dtgamma,t)
96+
_Wfact_t(W,u,p,dtgamma,t) = Wfact_iip_t(W,u,p,dtgamma,t)
97+
else
98+
_Wfact,_Wfact_t = nothing,nothing
99+
end
100+
101+
SDEFunction{iip}(f,g,jac=_jac,
102+
tgrad = _tgrad,
103+
Wfact = _Wfact,
104+
Wfact_t = _Wfact_t,
105+
syms = Symbol.(sys.dvs))
106+
end
107+
108+
function DiffEqBase.SDEFunction(sys::SDESystem, args...; kwargs...)
109+
SDEFunction{true}(sys, args...; kwargs...)
110+
end

src/systems/nonlinear/nonlinear_system.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export NonlinearSystem
2-
31
struct NLEq
42
rhs::Expression
53
end

0 commit comments

Comments
 (0)