-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
scimlfunctions.jl
4867 lines (4243 loc) · 196 KB
/
scimlfunctions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const RECOMPILE_BY_DEFAULT = true
"""
$(TYPEDEF)
Supertype for the specialization types. Controls the compilation and
function specialization behavior of SciMLFunctions, ultimately controlling
the runtime vs compile-time trade-off.
"""
abstract type AbstractSpecialization end
"""
$(TYPEDEF)
The default specialization level for problem functions. `AutoSpecialize`
works by applying a function wrap just-in-time before the solve process
to disable just-in-time re-specialization of the solver to the specific
choice of model `f` and thus allow for using a cached solver compilation
from a different `f`. This wrapping process can lead to a small decreased
runtime performance with a benefit of a greatly decreased compile-time.
## Note About Benchmarking and Runtime Optimality
It is recommended that `AutoSpecialize` is not used in any benchmarking
due to the potential effect of function wrapping on runtimes. `AutoSpecialize`'s
use case is targeted at decreased latency for REPL performance and
not for cases where where top runtime performance is required (such as in
optimization loops). Generally, for non-stiff equations the cost will be minimal
and potentially not even measurable. For stiff equations, function wrapping
has the limitation that only chunk sized 1 Dual numbers are allowed, which
can decrease Jacobian construction performance.
## Limitations of `AutoSpecialize`
The following limitations are not fundamental to the implementation of `AutoSpecialize`,
but are instead chosen as a compromise between default precompilation times and
ease of maintenance. Please open an issue to discuss lifting any potential
limitations.
* `AutoSpecialize` is only setup to wrap the functions from in-place ODEs. Other
cases are excluded for the time being due to time limitations.
* `AutoSpecialize` will only lead to compilation reuse if the ODEFunction's other
functions (such as jac and tgrad) are the default `nothing`. These could be
JIT wrapped as well in a future version.
* `AutoSpecialize`'d functions are only compatible with Jacobian calculations
performed with chunk size 1, and only with tag `DiffEqBase.OrdinaryDiffEqTag()`.
Thus ODE solvers written on the common interface must be careful to detect
the `AutoSpecialize` case and perform differentiation under these constraints,
use finite differencing, or manually unwrap before solving. This will lead
to decreased runtime performance for sufficiently large Jacobians.
* `AutoSpecialize` only wraps on Julia v1.8 and higher.
* `AutoSpecialize` does not handle cases with units. If unitful values are detected,
wrapping is automatically disabled.
* `AutoSpecialize` only wraps cases for which `promote_rule` is defined between `u0`
and dual numbers, `u0` and `t`, and for which `ArrayInterface.promote_eltype`
is defined on `u0` to dual numbers.
* `AutoSpecialize` only wraps cases for which `f.mass_matrix isa UniformScaling`, the
default.
* `AutoSpecialize` does not wrap cases where `f isa AbstractSciMLOperator`
* By default, only the `u0 isa Vector{Float64}`, `eltype(tspan) isa Float64`, and
`typeof(p) isa Union{Vector{Float64},SciMLBase.NullParameters}` are specialized
by the solver libraries. Other forms can be specialized with
`AutoSpecialize`, but must be done in the precompilation of downstream libraries.
* `AutoSpecialize`d functions are manually unwrapped in adjoint methods in
SciMLSensitivity.jl in order to allow compiler support for automatic differentiation.
Improved versions of adjoints which decrease the recompilation surface will come
in non-breaking updates.
Cases where automatic wrapping is disabled are equivalent to `FullSpecialize`.
## Example
```julia
f(du,u,p,t) = (du .= u)
# Note this is the same as ODEProblem(f, [1.0], (0.0,1.0))
# If no preferences are set
ODEProblem{true, SciMLBase.AutoSpecialize}(f, [1.0], (0.0,1.0))
```
"""
struct AutoSpecialize <: AbstractSpecialization end
"""
$(TYPEDEF)
`NoSpecialize` forces SciMLFunctions to not specialize on the types
of functions wrapped within it. This ultimately contributes to a
form such that every `prob.f` type is the same, meaning compilation
caches are fully reused, with the downside of losing runtime performance.
`NoSpecialize` is the form that most fully trades off runtime for compile
time. Unlike `AutoSpecialize`, `NoSpecialize` can be used with any
`SciMLFunction`.
## Example
```julia
f(du,u,p,t) = (du .= u)
ODEProblem{true, SciMLBase.NoSpecialize}(f, [1.0], (0.0,1.0))
```
"""
struct NoSpecialize <: AbstractSpecialization end
"""
$(TYPEDEF)
`FunctionWrapperSpecialize` is an eager wrapping choice which
performs a function wrapping during the `ODEProblem` construction.
This performs the function wrapping at the earliest possible point,
giving the best compile-time vs runtime performance, but with the
difficulty that any usage of `prob.f` needs to account for the
function wrapper's presence. While optimal in a performance sense,
this method has many usability issues with nonstandard solvers
and analyses as it requires unwrapping before re-wrapping for any
type changes. Thus this method is not used by default. Given that
the compile-time different is almost undetectable from AutoSpecialize,
this method is mostly used as a benchmarking reference for speed
of light for `AutoSpecialize`.
## Limitations of `FunctionWrapperSpecialize`
`FunctionWrapperSpecialize` has all of the limitations of `AutoSpecialize`,
but also includes the limitations:
* `prob.f` is directly specialized to the types of `(u,p,t)`, and any usage
of `prob.f` on other types first requires using
`SciMLBase.unwrapped_f(prob.f)` to remove the function wrapper.
* `FunctionWrapperSpecialize` can only be used by the `ODEProblem` constructor.
If an `ODEFunction` is being constructed, the user must manually use
`DiffEqBase.wrap_iip` on `f` before calling
`ODEFunction{true,FunctionWrapperSpecialize}(f)`. This is a fundamental
limitation of the approach as the types of `(u,p,t)` are required in the
construction process and not accessible in the `AbstractSciMLFunction` constructors.
## Example
```julia
f(du,u,p,t) = (du .= u)
ODEProblem{true, SciMLBase.FunctionWrapperSpecialize}(f, [1.0], (0.0,1.0))
```
"""
struct FunctionWrapperSpecialize <: AbstractSpecialization end
"""
$(TYPEDEF)
`FullSpecialize` is an eager specialization choice which
directly types the `AbstractSciMLFunction` struct to match the type
of the model `f`. This forces recompilation of the solver on each
new function type `f`, leading to the most compile times with the
benefit of having the best runtime performance.
`FullSpecialize` should be used in all cases where top runtime performance
is required, such as in long-running simulations and benchmarking.
## Example
```julia
f(du,u,p,t) = (du .= u)
ODEProblem{true, SciMLBase.FullSpecialize}(f, [1.0], (0.0,1.0))
```
"""
struct FullSpecialize <: AbstractSpecialization end
specstring = Preferences.@load_preference("SpecializationLevel", "AutoSpecialize")
if specstring ∉
("NoSpecialize", "FullSpecialize", "AutoSpecialize", "FunctionWrapperSpecialize")
error("SpecializationLevel preference $specstring is not in the allowed set of choices (NoSpecialize, FullSpecialize, AutoSpecialize, FunctionWrapperSpecialize).")
end
const DEFAULT_SPECIALIZATION = getproperty(SciMLBase, Symbol(specstring))
function DEFAULT_OBSERVED(sym, u, p, t)
error("Indexing symbol $sym is unknown.")
end
function DEFAULT_OBSERVED_NO_TIME(sym, u, p)
error("Indexing symbol $sym is unknown.")
end
function DEFAULT_POLYNOMIALIZE(u, p)
return u
end
function DEFAULT_UNPOLYNOMIALIZE(u, p)
return (u,)
end
function Base.summary(io::IO, prob::AbstractSciMLFunction)
type_color, no_color = get_colorizers(io)
print(io,
type_color, nameof(typeof(prob)),
no_color, ". In-place: ",
type_color, isinplace(prob),
no_color)
end
const NONCONFORMING_FUNCTIONS_ERROR_MESSAGE = """
Nonconforming functions detected. If a model function `f` is defined
as in-place, then all constituent functions like `jac` and `paramjac`
must be in-place (and vice versa with out-of-place). Detected that
some overloads did not conform to the same convention as `f`.
"""
struct NonconformingFunctionsError <: Exception
nonconforming::Vector{String}
end
function Base.showerror(io::IO, e::NonconformingFunctionsError)
println(io, NONCONFORMING_FUNCTIONS_ERROR_MESSAGE)
print(io, "Nonconforming functions: ")
printstyled(io, e.nonconforming; bold = true, color = :red)
end
const INTEGRAND_MISMATCH_FUNCTIONS_ERROR_MESSAGE = """
Nonconforming functions detected. If an integrand function `f` is defined
as out-of-place (`f(u,p)`), then no integrand_prototype can be passed into the
function constructor. Likewise if `f` is defined as in-place (`f(out,u,p)`), then
an integrand_prototype is required. Either change the use of the function
constructor or define the appropriate dispatch for `f`.
"""
struct IntegrandMismatchFunctionError <: Exception
iip::Bool
integrand_passed::Bool
end
function Base.showerror(io::IO, e::IntegrandMismatchFunctionError)
println(io, INTEGRAND_MISMATCH_FUNCTIONS_ERROR_MESSAGE)
print(io, "Mismatch: IIP=")
printstyled(io, e.iip; bold = true, color = :red)
print(io, ", Integrand passed=")
printstyled(io, e.integrand_passed; bold = true, color = :red)
end
"""
$(TYPEDEF)
"""
abstract type AbstractODEFunction{iip} <: AbstractDiffEqFunction{iip} end
@doc doc"""
$(TYPEDEF)
A representation of an ODE function `f`, defined by:
```math
M \frac{du}{dt} = f(u,p,t)
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
## Constructor
```julia
ODEFunction{iip,specialize}(f;
mass_matrix = __has_mass_matrix(f) ? f.mass_matrix : I,
analytic = __has_analytic(f) ? f.analytic : nothing,
tgrad= __has_tgrad(f) ? f.tgrad : nothing,
jac = __has_jac(f) ? f.jac : nothing,
jvp = __has_jvp(f) ? f.jvp : nothing,
vjp = __has_vjp(f) ? f.vjp : nothing,
jac_prototype = __has_jac_prototype(f) ? f.jac_prototype : nothing,
sparsity = __has_sparsity(f) ? f.sparsity : jac_prototype,
paramjac = __has_paramjac(f) ? f.paramjac : nothing,
colorvec = __has_colorvec(f) ? f.colorvec : nothing,
sys = __has_sys(f) ? f.sys : nothing)
```
Note that only the function `f` itself is required. This function should
be given as `f!(du,u,p,t)` or `du = f(u,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `mass_matrix`: the mass matrix `M` represented in the ODE function. Can be used
to determine that the equation is actually a differential-algebraic equation (DAE)
if `M` is singular. Note that in this case special solvers are required, see the
DAE solver page for more details: https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/.
Must be an AbstractArray or an AbstractSciMLOperator.
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
- `tgrad(dT,u,p,t)` or dT=tgrad(u,p,t): returns ``\frac{\partial f(u,p,t)}{\partial t}``
- `jac(J,u,p,t)` or `J=jac(u,p,t)`: returns ``\frac{df}{du}``
- `jvp(Jv,v,u,p,t)` or `Jv=jvp(v,u,p,t)`: returns the directional derivative``\frac{df}{du} v``
- `vjp(Jv,v,u,p,t)` or `Jv=vjp(v,u,p,t)`: returns the adjoint derivative``\frac{df}{du}^\ast v``
- `jac_prototype`: a prototype matrix matching the type that matches the Jacobian. For example,
if the Jacobian is tridiagonal, then an appropriately sized `Tridiagonal` matrix can be used
as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the Jacobian.
The default is `nothing`, which means a dense Jacobian.
- `paramjac(pJ,u,p,t)`: returns the parameter Jacobian ``\frac{df}{dp}``.
- `colorvec`: a color vector according to the SparseDiffTools.jl definition for the sparsity
pattern of the `jac_prototype`. This specializes the Jacobian construction when using
finite differences and automatic differentiation to be computed in an accelerated manner
based on the sparsity pattern. Defaults to `nothing`, which means a color vector will be
internally computed on demand when required. The cost of this operation is highly dependent
on the sparsity pattern.
## iip: In-Place vs Out-Of-Place
`iip` is the optional boolean for determining whether a given function is written to
be used in-place or out-of-place. In-place functions are `f!(du,u,p,t)` where the return
is ignored, and the result is expected to be mutated into the value of `du`. Out-of-place
functions are `du=f(u,p,t)`.
Normally, this is determined automatically by looking at the method table for `f` and seeing
the maximum number of arguments in available dispatches. For this reason, the constructor
`ODEFunction(f)` generally works (but is type-unstable). However, for type-stability or
to enforce correctness, this option is passed via `ODEFunction{true}(f)`.
## specialize: Controlling Compilation and Specialization
The `specialize` parameter controls the specialization level of the ODEFunction
on the function `f`. This allows for a trade-off between compile and run time performance.
The available specialization levels are:
* `SciMLBase.AutoSpecialize`: this form performs a lazy function wrapping on the
functions of the ODE in order to stop recompilation of the ODE solver, but allow
for the `prob.f` to stay unwrapped for normal usage. This is the default specialization
level and strikes a balance in compile time vs runtime performance.
* `SciMLBase.FullSpecialize`: this form fully specializes the `ODEFunction` on the
constituent functions that make its fields. As such, each `ODEFunction` in this
form is uniquely typed, requiring re-specialization and compilation for each new
ODE definition. This form has the highest compile-time at the cost of being the
most optimal in runtime. This form should be preferred for long-running calculations
(such as within optimization loops) and for benchmarking.
* `SciMLBase.NoSpecialize`: this form fully unspecializes the function types in the ODEFunction
definition by using an `Any` type declaration. As a result, it can result in reduced runtime
performance, but is the form that induces the least compile-time.
* `SciMLBase.FunctionWrapperSpecialize`: this is an eager function wrapping form. It is
unsafe with many solvers, and thus is mostly used for development testing.
For more details, see the
[specialization levels section of the SciMLBase documentation](https://docs.sciml.ai/SciMLBase/stable/interfaces/Problems/#Specialization-Levels).
## Fields
The fields of the ODEFunction type directly match the names of the inputs.
## More Details on Jacobians
The following example creates an inplace `ODEFunction` whose Jacobian is a `Diagonal`:
```julia
using LinearAlgebra
f = (du,u,p,t) -> du .= t .* u
jac = (J,u,p,t) -> (J[1,1] = t; J[2,2] = t; J)
jp = Diagonal(zeros(2))
fun = ODEFunction(f; jac=jac, jac_prototype=jp)
```
Note that the integrators will always make a deep copy of `fun.jac_prototype`, so
there's no worry of aliasing.
In general, the Jacobian prototype can be anything that has `mul!` defined, in
particular sparse matrices or custom lazy types that support `mul!`. A special case
is when the `jac_prototype` is a `AbstractSciMLOperator`, in which case you
do not need to supply `jac` as it is automatically set to `update_coefficients!`.
Refer to the AbstractSciMLOperators documentation for more information
on setting up time/parameter dependent operators.
## Examples
### Declaring Explicit Jacobians for ODEs
The most standard case, declaring a function for a Jacobian is done by overloading
the function `f(du,u,p,t)` with an in-place updating function for the Jacobian:
`f_jac(J,u,p,t)` where the value type is used for dispatch. For example,
take the Lotka-Volterra model:
```julia
function f(du,u,p,t)
du[1] = 2.0 * u[1] - 1.2 * u[1]*u[2]
du[2] = -3 * u[2] + u[1]*u[2]
end
```
To declare the Jacobian, we simply add the dispatch:
```julia
function f_jac(J,u,p,t)
J[1,1] = 2.0 - 1.2 * u[2]
J[1,2] = -1.2 * u[1]
J[2,1] = 1 * u[2]
J[2,2] = -3 + u[1]
nothing
end
```
Then we can supply the Jacobian with our ODE as:
```julia
ff = ODEFunction(f;jac=f_jac)
```
and use this in an `ODEProblem`:
```julia
prob = ODEProblem(ff,ones(2),(0.0,10.0))
```
## Symbolically Generating the Functions
See the `modelingtoolkitize` function from
[ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/) for
automatically symbolically generating the Jacobian and more from the
numerically-defined functions.
"""
struct ODEFunction{iip, specialize, F, TMM, Ta, Tt, TJ, JVP, VJP, JP, SP, TW, TWt, WP, TPJ,
O, TCV,
SYS, ID <: Union{Nothing, OverrideInitData}, NLP <: Union{Nothing, ODE_NLProbData}} <:
AbstractODEFunction{iip}
f::F
mass_matrix::TMM
analytic::Ta
tgrad::Tt
jac::TJ
jvp::JVP
vjp::VJP
jac_prototype::JP
sparsity::SP
Wfact::TW
Wfact_t::TWt
W_prototype::WP
paramjac::TPJ
observed::O
colorvec::TCV
sys::SYS
initialization_data::ID
nlprob_data::NLP
end
@doc doc"""
$(TYPEDEF)
A representation of a split ODE function `f`, defined by:
```math
M \frac{du}{dt} = f_1(u,p,t) + f_2(u,p,t)
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
Generally, for ODE integrators the `f_1` portion should be considered the
"stiff portion of the model" with larger timescale separation, while the
`f_2` portion should be considered the "non-stiff portion". This interpretation
is directly used in integrators like IMEX (implicit-explicit integrators)
and exponential integrators.
## Constructor
```julia
SplitFunction{iip,specialize}(f1,f2;
mass_matrix = __has_mass_matrix(f1) ? f1.mass_matrix : I,
analytic = __has_analytic(f1) ? f1.analytic : nothing,
tgrad= __has_tgrad(f1) ? f1.tgrad : nothing,
jac = __has_jac(f1) ? f1.jac : nothing,
jvp = __has_jvp(f1) ? f1.jvp : nothing,
vjp = __has_vjp(f1) ? f1.vjp : nothing,
jac_prototype = __has_jac_prototype(f1) ? f1.jac_prototype : nothing,
W_prototype = __has_W_prototype(f1) ? f1.W_prototype : nothing,
sparsity = __has_sparsity(f1) ? f1.sparsity : jac_prototype,
paramjac = __has_paramjac(f1) ? f1.paramjac : nothing,
colorvec = __has_colorvec(f1) ? f1.colorvec : nothing,
sys = __has_sys(f1) ? f1.sys : nothing)
```
Note that only the functions `f_i` themselves are required. These functions should
be given as `f_i!(du,u,p,t)` or `du = f_i(u,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling.
All of the remaining functions are optional for improving or accelerating
the usage of the `SplitFunction`. These include:
- `mass_matrix`: the mass matrix `M` represented in the ODE function. Can be used
to determine that the equation is actually a differential-algebraic equation (DAE)
if `M` is singular. Note that in this case special solvers are required, see the
DAE solver page for more details: https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/.
Must be an AbstractArray or an AbstractSciMLOperator.
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
- `tgrad(dT,u,p,t)` or dT=tgrad(u,p,t): returns ``\frac{\partial f_1(u,p,t)}{\partial t}``
- `jac(J,u,p,t)` or `J=jac(u,p,t)`: returns ``\frac{df_1}{du}``
- `jvp(Jv,v,u,p,t)` or `Jv=jvp(v,u,p,t)`: returns the directional derivative ``\frac{df_1}{du} v``
- `vjp(Jv,v,u,p,t)` or `Jv=vjp(v,u,p,t)`: returns the adjoint derivative ``\frac{df_1}{du}^\ast v``
- `jac_prototype`: a prototype matrix matching the type that matches the Jacobian. For example,
if the Jacobian is tridiagonal, then an appropriately sized `Tridiagonal` matrix can be used
as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the Jacobian.
The default is `nothing`, which means a dense Jacobian.
- `W_prototype`: a prototype matrix matching the type that matches the W matrix. For example,
if the Jacobian is tridiagonal, and the mass_matrix is diagonal, then an appropriately sized `Tridiagonal`
matrix can be used as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the W matrix.
The default is `nothing`, which means a W of appropriate type for the jacobian and linear solver
- `paramjac(pJ,u,p,t)`: returns the parameter Jacobian ``\frac{df_1}{dp}``.
- `colorvec`: a color vector according to the SparseDiffTools.jl definition for the sparsity
pattern of the `jac_prototype`. This specializes the Jacobian construction when using
finite differences and automatic differentiation to be computed in an accelerated manner
based on the sparsity pattern. Defaults to `nothing`, which means a color vector will be
internally computed on demand when required. The cost of this operation is highly dependent
on the sparsity pattern.
## Note on the Derivative Definition
The derivatives, such as the Jacobian, are only defined on the `f1` portion of the split ODE.
This is used to treat the `f1` implicit while keeping the `f2` portion explicit.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the SplitFunction type directly match the names of the inputs.
## Symbolically Generating the Functions
See the `modelingtoolkitize` function from
[ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/) for
automatically symbolically generating the Jacobian and more from the
numerically-defined functions. See `ModelingToolkit.SplitODEProblem` for
information on generating the SplitFunction from this symbolic engine.
"""
struct SplitFunction{
iip, specialize, F1, F2, TMM, C, Ta, Tt, TJ, JVP, VJP, JP, WP, SP, TW, TWt,
TPJ, O, TCV, SYS, ID <: Union{Nothing, OverrideInitData},
NLP <: Union{Nothing, ODE_NLProbData}} <: AbstractODEFunction{iip}
f1::F1
f2::F2
mass_matrix::TMM
cache::C
analytic::Ta
tgrad::Tt
jac::TJ
jvp::JVP
vjp::VJP
jac_prototype::JP
W_prototype::WP
sparsity::SP
Wfact::TW
Wfact_t::TWt
paramjac::TPJ
observed::O
colorvec::TCV
sys::SYS
initialization_data::ID
nlprob_data::NLP
end
@doc doc"""
$(TYPEDEF)
A representation of an ODE function `f`, defined by:
```math
M \frac{du}{dt} = f(u,p,t)
```
as a partitioned ODE:
```math
\begin{align}
M_1 \frac{du}{dt} = f_1(u,p,t) \\
M_2 \frac{du}{dt} = f_2(u,p,t)
\end{align}
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
## Constructor
```julia
DynamicalODEFunction{iip,specialize}(f1,f2;
mass_matrix = __has_mass_matrix(f) ? f.mass_matrix : I,
analytic = __has_analytic(f) ? f.analytic : nothing,
tgrad= __has_tgrad(f) ? f.tgrad : nothing,
jac = __has_jac(f) ? f.jac : nothing,
jvp = __has_jvp(f) ? f.jvp : nothing,
vjp = __has_vjp(f) ? f.vjp : nothing,
jac_prototype = __has_jac_prototype(f) ? f.jac_prototype : nothing,
sparsity = __has_sparsity(f) ? f.sparsity : jac_prototype,
paramjac = __has_paramjac(f) ? f.paramjac : nothing,
colorvec = __has_colorvec(f) ? f.colorvec : nothing,
sys = __has_sys(f) ? f.sys : nothing)
```
Note that only the functions `f_i` themselves are required. These functions should
be given as `f_i!(du,u,p,t)` or `du = f_i(u,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `mass_matrix`: the mass matrix `M_i` represented in the ODE function. Can be used
to determine that the equation is actually a differential-algebraic equation (DAE)
if `M` is singular. Note that in this case special solvers are required, see the
DAE solver page for more details: <https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/>.
Must be an AbstractArray or an AbstractSciMLOperator. Should be given as a tuple
of mass matrices, i.e. `(M_1, M_2)` for the mass matrices of equations 1 and 2
respectively.
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
- `tgrad(dT,u,p,t)` or dT=tgrad(u,p,t): returns ``\frac{\partial f(u,p,t)}{\partial t}``
- `jac(J,u,p,t)` or `J=jac(u,p,t)`: returns ``\frac{df}{du}``
- `jvp(Jv,v,u,p,t)` or `Jv=jvp(v,u,p,t)`: returns the directional derivative ``\frac{df}{du} v``
- `vjp(Jv,v,u,p,t)` or `Jv=vjp(v,u,p,t)`: returns the adjoint derivative ``\frac{df}{du}^\ast v``
- `jac_prototype`: a prototype matrix matching the type that matches the Jacobian. For example,
if the Jacobian is tridiagonal, then an appropriately sized `Tridiagonal` matrix can be used
as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the Jacobian.
The default is `nothing`, which means a dense Jacobian.
- `paramjac(pJ,u,p,t)`: returns the parameter Jacobian ``\frac{df}{dp}``.
- `colorvec`: a color vector according to the SparseDiffTools.jl definition for the sparsity
pattern of the `jac_prototype`. This specializes the Jacobian construction when using
finite differences and automatic differentiation to be computed in an accelerated manner
based on the sparsity pattern. Defaults to `nothing`, which means a color vector will be
internally computed on demand when required. The cost of this operation is highly dependent
on the sparsity pattern.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the DynamicalODEFunction type directly match the names of the inputs.
"""
struct DynamicalODEFunction{iip, specialize, F1, F2, TMM, Ta, Tt, TJ, JVP, VJP, JP, SP, TW,
TWt, TPJ,
O, TCV, SYS, ID} <: AbstractODEFunction{iip}
f1::F1
f2::F2
mass_matrix::TMM
analytic::Ta
tgrad::Tt
jac::TJ
jvp::JVP
vjp::VJP
jac_prototype::JP
sparsity::SP
Wfact::TW
Wfact_t::TWt
paramjac::TPJ
observed::O
colorvec::TCV
sys::SYS
initialization_data::ID
end
"""
$(TYPEDEF)
"""
abstract type AbstractDDEFunction{iip} <: AbstractDiffEqFunction{iip} end
@doc doc"""
$(TYPEDEF)
A representation of a DDE function `f`, defined by:
```math
M \frac{du}{dt} = f(u,h,p,t)
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
## Constructor
```julia
DDEFunction{iip,specialize}(f;
mass_matrix = __has_mass_matrix(f) ? f.mass_matrix : I,
analytic = __has_analytic(f) ? f.analytic : nothing,
tgrad= __has_tgrad(f) ? f.tgrad : nothing,
jac = __has_jac(f) ? f.jac : nothing,
jvp = __has_jvp(f) ? f.jvp : nothing,
vjp = __has_vjp(f) ? f.vjp : nothing,
jac_prototype = __has_jac_prototype(f) ? f.jac_prototype : nothing,
sparsity = __has_sparsity(f) ? f.sparsity : jac_prototype,
paramjac = __has_paramjac(f) ? f.paramjac : nothing,
colorvec = __has_colorvec(f) ? f.colorvec : nothing,
sys = __has_sys(f) ? f.sys : nothing)
```
Note that only the function `f` itself is required. This function should
be given as `f!(du,u,h,p,t)` or `du = f(u,h,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling. The history function
`h` acts as an interpolator over time, i.e. `h(t)` with options matching
the solution interface, i.e. `h(t; save_idxs = 2)`.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `mass_matrix`: the mass matrix `M` represented in the ODE function. Can be used
to determine that the equation is actually a differential-algebraic equation (DAE)
if `M` is singular. Note that in this case special solvers are required, see the
DAE solver page for more details: https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/.
Must be an AbstractArray or an AbstractSciMLOperator.
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
- `tgrad(dT,u,h,p,t)` or dT=tgrad(u,p,t): returns ``\frac{\partial f(u,p,t)}{\partial t}``
- `jac(J,u,h,p,t)` or `J=jac(u,p,t)`: returns ``\frac{df}{du}``
- `jvp(Jv,v,h,u,p,t)` or `Jv=jvp(v,u,p,t)`: returns the directional derivative ``\frac{df}{du} v``
- `vjp(Jv,v,h,u,p,t)` or `Jv=vjp(v,u,p,t)`: returns the adjoint derivative ``\frac{df}{du}^\ast v``
- `jac_prototype`: a prototype matrix matching the type that matches the Jacobian. For example,
if the Jacobian is tridiagonal, then an appropriately sized `Tridiagonal` matrix can be used
as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the Jacobian.
The default is `nothing`, which means a dense Jacobian.
- `paramjac(pJ,h,u,p,t)`: returns the parameter Jacobian ``\frac{df}{dp}``.
- `colorvec`: a color vector according to the SparseDiffTools.jl definition for the sparsity
pattern of the `jac_prototype`. This specializes the Jacobian construction when using
finite differences and automatic differentiation to be computed in an accelerated manner
based on the sparsity pattern. Defaults to `nothing`, which means a color vector will be
internally computed on demand when required. The cost of this operation is highly dependent
on the sparsity pattern.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the DDEFunction type directly match the names of the inputs.
"""
struct DDEFunction{
iip, specialize, F, TMM, Ta, Tt, TJ, JVP, VJP, JP, SP, TW, TWt, TPJ, O, TCV, SYS, ID} <:
AbstractDDEFunction{iip}
f::F
mass_matrix::TMM
analytic::Ta
tgrad::Tt
jac::TJ
jvp::JVP
vjp::VJP
jac_prototype::JP
sparsity::SP
Wfact::TW
Wfact_t::TWt
paramjac::TPJ
observed::O
colorvec::TCV
sys::SYS
initialization_data::ID
end
@doc doc"""
$(TYPEDEF)
A representation of a DDE function `f`, defined by:
```math
M \frac{du}{dt} = f(u,h,p,t)
```
as a partitioned ODE:
```math
\begin{align}
M_1 \frac{du}{dt} = f_1(u,h,p,t) \\
M_2 \frac{du}{dt} = f_2(u,h,p,t)
\end{align}
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
## Constructor
```julia
DynamicalDDEFunction{iip,specialize}(f1,f2;
mass_matrix = __has_mass_matrix(f) ? f.mass_matrix : I,
analytic = __has_analytic(f) ? f.analytic : nothing,
tgrad= __has_tgrad(f) ? f.tgrad : nothing,
jac = __has_jac(f) ? f.jac : nothing,
jvp = __has_jvp(f) ? f.jvp : nothing,
vjp = __has_vjp(f) ? f.vjp : nothing,
jac_prototype = __has_jac_prototype(f) ? f.jac_prototype : nothing,
sparsity = __has_sparsity(f) ? f.sparsity : jac_prototype,
paramjac = __has_paramjac(f) ? f.paramjac : nothing,
colorvec = __has_colorvec(f) ? f.colorvec : nothing,
sys = __has_sys(f) ? f.sys : nothing)
```
Note that only the functions `f_i` themselves are required. These functions should
be given as `f_i!(du,u,h,p,t)` or `du = f_i(u,h,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling. The history function
`h` acts as an interpolator over time, i.e. `h(t)` with options matching
the solution interface, i.e. `h(t; save_idxs = 2)`.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `mass_matrix`: the mass matrix `M_i` represented in the ODE function. Can be used
to determine that the equation is actually a differential-algebraic equation (DAE)
if `M` is singular. Note that in this case special solvers are required, see the
DAE solver page for more details: https://docs.sciml.ai/DiffEqDocs/stable/solvers/dae_solve/.
Must be an AbstractArray or an AbstractSciMLOperator. Should be given as a tuple
of mass matrices, i.e. `(M_1, M_2)` for the mass matrices of equations 1 and 2
respectively.
- `analytic(u0,h,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
- `tgrad(dT,u,h,p,t)` or dT=tgrad(u,h,p,t): returns ``\frac{\partial f(u,p,t)}{\partial t}``
- `jac(J,u,h,p,t)` or `J=jac(u,h,p,t)`: returns ``\frac{df}{du}``
- `jvp(Jv,v,u,h,p,t)` or `Jv=jvp(v,u,h,p,t)`: returns the directional derivative ``\frac{df}{du} v``
- `vjp(Jv,v,u,h,p,t)` or `Jv=vjp(v,u,h,p,t)`: returns the adjoint derivative ``\frac{df}{du}^\ast v``
- `jac_prototype`: a prototype matrix matching the type that matches the Jacobian. For example,
if the Jacobian is tridiagonal, then an appropriately sized `Tridiagonal` matrix can be used
as the prototype and integrators will specialize on this structure where possible. Non-structured
sparsity patterns should use a `SparseMatrixCSC` with a correct sparsity pattern for the Jacobian.
The default is `nothing`, which means a dense Jacobian.
- `paramjac(pJ,u,h,p,t)`: returns the parameter Jacobian ``\frac{df}{dp}``.
- `colorvec`: a color vector according to the SparseDiffTools.jl definition for the sparsity
pattern of the `jac_prototype`. This specializes the Jacobian construction when using
finite differences and automatic differentiation to be computed in an accelerated manner
based on the sparsity pattern. Defaults to `nothing`, which means a color vector will be
internally computed on demand when required. The cost of this operation is highly dependent
on the sparsity pattern.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the DynamicalDDEFunction type directly match the names of the inputs.
"""
struct DynamicalDDEFunction{iip, specialize, F1, F2, TMM, Ta, Tt, TJ, JVP, VJP, JP, SP, TW,
TWt, TPJ,
O, TCV, SYS, ID} <: AbstractDDEFunction{iip}
f1::F1
f2::F2
mass_matrix::TMM
analytic::Ta
tgrad::Tt
jac::TJ
jvp::JVP
vjp::VJP
jac_prototype::JP
sparsity::SP
Wfact::TW
Wfact_t::TWt
paramjac::TPJ
observed::O
colorvec::TCV
sys::SYS
initialization_data::ID
end
"""
$(TYPEDEF)
"""
abstract type AbstractDiscreteFunction{iip} <:
AbstractDiffEqFunction{iip} end
@doc doc"""
$(TYPEDEF)
A representation of a discrete dynamical system `f`, defined by:
```math
u_{n+1} = f(u,p,t_{n+1})
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
## Constructor
```julia
DiscreteFunction{iip,specialize}(f;
analytic = __has_analytic(f) ? f.analytic : nothing)
```
Note that only the function `f` itself is required. This function should
be given as `f!(du,u,p,t)` or `du = f(u,p,t)`. See the section on `iip`
for more details on in-place vs out-of-place handling.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the DiscreteFunction type directly match the names of the inputs.
"""
struct DiscreteFunction{iip, specialize, F, Ta, O, SYS, ID} <:
AbstractDiscreteFunction{iip}
f::F
analytic::Ta
observed::O
sys::SYS
initialization_data::ID
end
@doc doc"""
$(TYPEDEF)
A representation of an discrete dynamical system `f`, defined by:
```math
0 = f(u_{n+1}, u_{n}, p, t_{n+1}, integ)
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,
`p` are the parameters, and `t` is the independent variable.
`integ` contains the fields:
```julia
dt: the time step
```
## Constructor
```julia
ImplicitDiscreteFunction{iip,specialize}(f;
analytic = __has_analytic(f) ? f.analytic : nothing)
```
Note that only the function `f` itself is required. This function should
be given as `f!(residual, u_next, u, p, t)` or `residual = f(u_next, u, p, t)`. See the section on `iip`
for more details on in-place vs out-of-place handling.
All of the remaining functions are optional for improving or accelerating
the usage of `f`. These include:
- `analytic(u0,p,t)`: used to pass an analytical solution function for the analytical
solution of the ODE. Generally only used for testing and development of the solvers.
## iip: In-Place vs Out-Of-Place
For more details on this argument, see the ODEFunction documentation.
## specialize: Controlling Compilation and Specialization
For more details on this argument, see the ODEFunction documentation.
## Fields
The fields of the ImplicitDiscreteFunction type directly match the names of the inputs.
"""
struct ImplicitDiscreteFunction{iip, specialize, F, Ta, O, SYS, ID} <:
AbstractDiscreteFunction{iip}
f::F
analytic::Ta
observed::O
sys::SYS
initialization_data::ID
end
"""
$(TYPEDEF)
"""
abstract type AbstractSDEFunction{iip} <: AbstractDiffEqFunction{iip} end
@doc doc"""
$(TYPEDEF)
A representation of an SDE function `f`, defined by:
```math
M du = f(u,p,t)dt + g(u,p,t) dW
```
and all of its related functions, such as the Jacobian of `f`, its gradient
with respect to time, and more. For all cases, `u0` is the initial condition,