@@ -69,6 +69,16 @@ struct ODESystem <: AbstractSystem
69
69
[`calculate_jacobian`](@ref) is called on the system.
70
70
"""
71
71
jac:: RefValue{Matrix{Expression}}
72
+ """
73
+ Wfact matrix. Note: this field will not be defined until
74
+ [`generate_factorized_W`](@ref) is called on the system.
75
+ """
76
+ Wfact:: RefValue{Matrix{Expression}}
77
+ """
78
+ Wfact_t matrix. Note: this field will not be defined until
79
+ [`generate_factorized_W`](@ref) is called on the system.
80
+ """
81
+ Wfact_t:: RefValue{Matrix{Expression}}
72
82
end
73
83
74
84
function ODESystem (eqs)
@@ -89,7 +99,9 @@ function ODESystem(eqs)
89
99
end
90
100
function ODESystem (deqs, iv, dvs, ps)
91
101
jac = RefValue (Matrix {Expression} (undef, 0 , 0 ))
92
- ODESystem (deqs, iv, dvs, ps, jac)
102
+ Wfact = RefValue (Matrix {Expression} (undef, 0 , 0 ))
103
+ Wfact_t = RefValue (Matrix {Expression} (undef, 0 , 0 ))
104
+ ODESystem (deqs, iv, dvs, ps, jac, Wfact, Wfact_t)
93
105
end
94
106
95
107
function _eq_unordered (a, b)
@@ -152,10 +164,10 @@ function generate_function(sys::ODESystem, dvs, ps; version::FunctionVersion = A
152
164
return build_function (rhss, dvs′, ps′, (sys. iv. name,), ODEToExpr (sys); version = version)
153
165
end
154
166
167
+ function calculate_factorized_W (sys:: ODESystem , simplify= true )
168
+ isempty (sys. Wfact[]) || return (sys. Wfact[],sys. Wfact_t[])
155
169
156
- function generate_factorized_W (sys:: ODESystem , simplify= true ; version:: FunctionVersion = ArrayFunction)
157
170
jac = calculate_jacobian (sys)
158
-
159
171
gam = Variable (:gam ; known = true )()
160
172
161
173
W = - LinearAlgebra. I + gam* jac
@@ -170,6 +182,14 @@ function generate_factorized_W(sys::ODESystem, simplify=true; version::FunctionV
170
182
if simplify
171
183
Wfact_t = simplify_constants .(Wfact_t)
172
184
end
185
+ sys. Wfact[] = Wfact
186
+ sys. Wfact_t[] = Wfact_t
187
+
188
+ (Wfact,Wfact_t)
189
+ end
190
+
191
+ function generate_factorized_W (sys:: ODESystem , simplify= true ; version:: FunctionVersion = ArrayFunction)
192
+ (Wfact,Wfact_t) = calculate_factorized_W (sys,simplify)
173
193
174
194
if version === SArrayFunction
175
195
siz = size (Wfact)
@@ -195,11 +215,16 @@ Create an `ODEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and `p
195
215
are used to set the order of the dependent variable and parameter vectors,
196
216
respectively.
197
217
"""
198
- function DiffEqBase. ODEFunction (sys:: ODESystem , dvs, ps; version:: FunctionVersion = ArrayFunction)
199
- expr = generate_function (sys, dvs, ps; version = version)
218
+ function DiffEqBase. ODEFunction (sys:: ODESystem , dvs, ps; version:: FunctionVersion = ArrayFunction,
219
+ jac = false , Wfact = false )
220
+ expr = eval (generate_function (sys, dvs, ps; version = version))
221
+ jac_expr = jac ? nothing : eval (generate_jacobian (sys))
222
+ Wfact_expr,Wfact_t_expr = Wfact ? (nothing ,nothing ) : eval .(calculate_factorized_W (sys))
200
223
if version === ArrayFunction
201
- ODEFunction {true} (eval (expr))
224
+ ODEFunction {true} (eval (expr),jac= jac_expr,
225
+ Wfact = Wfact_expr, Wfact_t = Wfact_t_expr)
202
226
elseif version === SArrayFunction
203
- ODEFunction {false} (eval (expr))
227
+ ODEFunction {false} (eval (expr),jac= jac_expr,
228
+ Wfact = Wfact_expr, Wfact_t = Wfact_t_expr)
204
229
end
205
230
end
0 commit comments