1
1
module ModelingToolkit
2
2
3
3
using DiffEqBase, SciMLBase
4
- using Distributed
5
4
using StaticArrays, LinearAlgebra, SparseArrays, LabelledArrays
6
5
using Latexify, Unitful, ArrayInterface
7
6
using MacroTools
@@ -25,159 +24,29 @@ RuntimeGeneratedFunctions.init(@__MODULE__)
25
24
using RecursiveArrayTools
26
25
27
26
import SymbolicUtils
28
- import SymbolicUtils: Term, Add, Mul, Pow, Sym, FnType ,
29
- @rule , Rewriters, substitute, similarterm ,
30
- promote_symtype
31
-
27
+ import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype ,
28
+ Symbolic, Term, Add, Mul, Pow, Sym, FnType ,
29
+ @rule , Rewriters, substitute
30
+ using SymbolicUtils . Code
32
31
import SymbolicUtils. Code: toexpr
33
-
34
32
import SymbolicUtils. Rewriters: Chain, Postwalk, Prewalk, Fixpoint
33
+
34
+ using Reexport
35
+ @reexport using Symbolics
36
+ export @derivatives
37
+ using Symbolics: _parse_vars, value, makesym, @derivatives , get_variables,
38
+ exprs_occur_in
39
+ import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
40
+ jacobian_sparsity, islinear
41
+
35
42
import DiffEqBase: @add_kwonly
36
- using LinearAlgebra: LU, BlasInt
37
43
38
44
import LightGraphs: SimpleDiGraph, add_edge!
39
45
40
46
import TreeViews
41
47
42
48
using Requires
43
49
44
- export Num, Variable
45
- """
46
- $(TYPEDEF)
47
-
48
- Wrap anything in a type that is a subtype of Real
49
- """
50
- struct Num <: Real
51
- val
52
- end
53
-
54
- const show_numwrap = Ref (false )
55
-
56
- Num (x:: Num ) = x # ideally this should never be called
57
- (n:: Num )(args... ) = Num (value (n)(map (value,args)... ))
58
- value (x) = x
59
- value (x:: Num ) = x. val
60
-
61
- SciMLBase. issymbollike (:: Num ) = true
62
- SciMLBase. issymbollike (:: SymbolicUtils.Symbolic ) = true
63
-
64
- SymbolicUtils. @number_methods (Num,
65
- Num (f (value (a))),
66
- Num (f (value (a), value (b))))
67
-
68
- for C in [Complex, Complex{Bool}]
69
- @eval begin
70
- Base.:* (x:: Num , z:: $C ) = Complex (x * real (z), x * imag (z))
71
- Base.:* (z:: $C , x:: Num ) = Complex (real (z) * x, imag (z) * x)
72
- end
73
- end
74
-
75
- Base.:+ (x:: Num , z:: Complex ) = Complex (x + real (z), imag (z))
76
- Base.:+ (z:: Complex , x:: Num ) = Complex (real (z) + x, imag (z))
77
- Base.:- (x:: Num , z:: Complex ) = Complex (x - real (z), - imag (z))
78
- Base.:- (z:: Complex , x:: Num ) = Complex (real (z) - x, imag (z))
79
-
80
- function Base. inv (z:: Complex{Num} )
81
- a, b = reim (z)
82
- den = a^ 2 + b^ 2
83
- Complex (a/ den, - b/ den)
84
- end
85
- function Base.:/ (x:: Complex{Num} , y:: Complex{Num} )
86
- a, b = reim (x)
87
- c, d = reim (y)
88
- den = c^ 2 + d^ 2
89
- Complex ((a* c + b* d)/ den, (b* c - a* d)/ den)
90
- end
91
-
92
- function Base. show (io:: IO , z:: Complex{<:Num} )
93
- r, i = reim (z)
94
- compact = get (io, :compact , false )
95
- show (io, r)
96
- print (io, (compact ? " +" : " + " ) * " (" )
97
- show (io, i)
98
- print (io, " )*im" )
99
- end
100
-
101
- SymbolicUtils. simplify (n:: Num ; kw... ) = Num (SymbolicUtils. simplify (value (n); kw... ))
102
-
103
- SymbolicUtils. symtype (n:: Num ) = symtype (n. val)
104
-
105
- function Base. iszero (x:: Num )
106
- _x = SymbolicUtils. to_mpoly (value (x))[1 ]
107
- return (_x isa Number || _x isa SymbolicUtils. MPoly) && iszero (_x)
108
- end
109
-
110
- import SymbolicUtils: < ₑ, Symbolic, Term, operation, arguments
111
-
112
- Base. show (io:: IO , n:: Num ) = show_numwrap[] ? print (io, :(Num ($ (value (n))))) : Base. show (io, value (n))
113
-
114
- Base. promote_rule (:: Type{<:Number} , :: Type{<:Num} ) = Num
115
- Base. promote_rule (:: Type{<:Symbolic{<:Number}} , :: Type{<:Num} ) = Num
116
- function Base. getproperty (t:: Union{Add, Mul, Pow, Term} , f:: Symbol )
117
- if f === :op
118
- Base. depwarn (" `x.op` is deprecated, use `operation(x)` instead" , :getproperty , force= true )
119
- operation (t)
120
- elseif f === :args
121
- Base. depwarn (" `x.args` is deprecated, use `arguments(x)` instead" , :getproperty , force= true )
122
- arguments (t)
123
- else
124
- getfield (t, f)
125
- end
126
- end
127
- < ₑ (s:: Num , x) = value (s) < ₑ value (x)
128
- < ₑ (s, x:: Num ) = value (s) < ₑ value (x)
129
- < ₑ (s:: Num , x:: Num ) = value (s) < ₑ value (x)
130
-
131
- for T in (Integer, Rational)
132
- @eval Base.:(^ )(n:: Num , i:: $T ) = Num (value (n)^ i)
133
- end
134
-
135
- macro num_method (f, expr, Ts= nothing )
136
- if Ts === nothing
137
- Ts = [Any]
138
- else
139
- @assert Ts. head == :tuple
140
- # e.g. a tuple or vector
141
- Ts = Ts. args
142
- end
143
-
144
- ms = [quote
145
- $ f (a:: $T , b:: $Num ) = $ expr
146
- $ f (a:: $Num , b:: $T ) = $ expr
147
- end for T in Ts]
148
- quote
149
- $ f (a:: $Num , b:: $Num ) = $ expr
150
- $ (ms... )
151
- end |> esc
152
- end
153
-
154
- """
155
- tosymbolic(a::Union{Sym,Num}) -> Sym{Real}
156
- tosymbolic(a::T) -> T
157
- """
158
- tosymbolic (a:: Num ) = tosymbolic (value (a))
159
- tosymbolic (a:: Sym ) = tovar (a)
160
- tosymbolic (a) = a
161
- @num_method Base. isless (val = isless (tosymbolic (a), tosymbolic (b)); val isa Bool ? val : Num (val)) (Real,)
162
- @num_method Base.:(< ) (val = tosymbolic (a) < tosymbolic (b) ; val isa Bool ? val : Num (val)) (Real,)
163
- @num_method Base.:(<= ) (val = tosymbolic (a) <= tosymbolic (b) ; val isa Bool ? val : Num (val)) (Real,)
164
- @num_method Base.:(> ) (val = tosymbolic (a) > tosymbolic (b) ; val isa Bool ? val : Num (val)) (Real,)
165
- @num_method Base.:(>= ) (val = tosymbolic (a) >= tosymbolic (b) ; val isa Bool ? val : Num (val)) (Real,)
166
- @num_method Base.:(== ) (val = tosymbolic (a) == tosymbolic (b) ; val isa Bool ? val : Num (val)) (AbstractFloat,Number)
167
- @num_method Base. isequal isequal (tosymbolic (a), tosymbolic (b)) (AbstractFloat, Number, Symbolic)
168
-
169
- Base. hash (x:: Num , h:: UInt ) = hash (value (x), h)
170
-
171
- Base. convert (:: Type{Num} , x:: Symbolic{<:Number} ) = Num (x)
172
- Base. convert (:: Type{Num} , x:: Number ) = Num (x)
173
- Base. convert (:: Type{Num} , x:: Num ) = x
174
-
175
- Base. convert (:: Type{<:Array{Num}} , x:: AbstractArray ) = map (Num, x)
176
- Base. convert (:: Type{<:Array{Num}} , x:: AbstractArray{Num} ) = x
177
- Base. convert (:: Type{Sym} , x:: Num ) = value (x) isa Sym ? value (x) : error (" cannot convert $x to Sym" )
178
-
179
- LinearAlgebra. lu (x:: Array{Num} ; check= true , kw... ) = sym_lu (x; check= check)
180
-
181
50
"""
182
51
$(TYPEDEF)
183
52
@@ -211,16 +80,11 @@ include("bipartite_graph.jl")
211
80
using . BipartiteGraphs
212
81
213
82
include (" variables.jl" )
214
- include (" context_dsl.jl" )
215
- include (" differentials.jl" )
83
+ include (" parameters.jl" )
216
84
217
- include (" equations.jl" )
218
85
include (" utils.jl" )
219
- include (" linearity.jl" )
220
- include (" solve.jl" )
221
86
include (" direct.jl" )
222
87
include (" domains.jl" )
223
- include (" register_function.jl" )
224
88
225
89
include (" systems/abstractsystem.jl" )
226
90
@@ -252,7 +116,6 @@ include("systems/reduction.jl")
252
116
253
117
include (" latexify_recipes.jl" )
254
118
include (" build_function.jl" )
255
- include (" extra_functions.jl" )
256
119
257
120
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr
258
121
export SDESystem, SDEFunction, SDEFunctionExpr, SDESystemExpr
@@ -292,7 +155,6 @@ export asgraph, asdigraph
292
155
export toexpr, get_variables
293
156
export simplify, substitute
294
157
export build_function
295
- export @register
296
158
export modelingtoolkitize
297
159
export @variables , @parameters
298
160
0 commit comments