@@ -2,7 +2,7 @@ using ModelingToolkit, StaticArrays, LinearAlgebra
2
2
using DiffEqBase
3
3
using Test
4
4
5
- # Define some variables
5
+ # Calculus
6
6
@parameters t σ ρ β
7
7
@variables x y z
8
8
29
29
@test all (isequal .(ModelingToolkit. gradient (eqs[1 ],[x,y,z]),[σ * - 1 ,σ,0 ]))
30
30
@test all (isequal .(ModelingToolkit. hessian (eqs[1 ],[x,y,z]),0 ))
31
31
32
+ # Function building
33
+
32
34
@parameters σ () ρ () β ()
33
35
@variables x y z
34
-
35
36
eqs = [σ* (y- x),
36
37
x* (ρ- z)- y,
37
38
x* y - β* z]
39
+ f = eval (ModelingToolkit. build_function (eqs,[x,y,z],[σ,ρ,β]))
40
+ out = [1.0 ,2 ,3 ]
41
+ o1 = f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
42
+ f (out,[1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
43
+ @test all (o1 .== out)
38
44
39
- ModelingToolkit. build_function (eqs,[x,y,z],[σ,ρ,β])
45
+ function test_worldage ()
46
+ @parameters σ () ρ () β ()
47
+ @variables x y z
48
+ eqs = [σ* (y- x),
49
+ x* (ρ- z)- y,
50
+ x* y - β* z]
51
+ _f = eval (ModelingToolkit. build_function (eqs,[x,y,z],[σ,ρ,β]))
52
+ f (u,p) = ModelingToolkit. fast_invokelatest (_f,typeof (u),u,p)
53
+ f (du,u,p) = ModelingToolkit. fast_invokelatest (_f,Nothing,du,u,p)
54
+ out = [1.0 ,2 ,3 ]
55
+ o1 = f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
56
+ f (out,[1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
57
+ end
58
+ test_worldage ()
40
59
41
60
mac = @I begin
42
61
@parameters σ () ρ () β ()
@@ -45,17 +64,17 @@ mac = @I begin
45
64
eqs = [σ* (y- x),
46
65
x* (ρ- z)- y,
47
66
x* y - β* z]
48
-
49
67
ModelingToolkit. build_function (eqs,[x,y,z],[σ,ρ,β])
50
68
end
51
69
f = @ICompile
52
70
out = [1.0 ,2 ,3 ]
53
- f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
71
+ o1 = f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
54
72
f (out,[1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
73
+ @test all (o1 .== out)
55
74
56
75
mac = @I begin
57
- @parameters σ () ρ () β ()
58
- @variables x () y () z ()
76
+ @parameters σ ρ β
77
+ @variables x y z
59
78
60
79
eqs = [σ* (y- x),
61
80
x* (ρ- z)- y,
@@ -65,6 +84,44 @@ mac = @I begin
65
84
end
66
85
f = @ICompile
67
86
out = zeros (3 ,3 )
68
- f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
87
+ o1 = f ([1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
69
88
f (out,[1.0 ,2 ,3 ],[1.0 ,2 ,3 ])
70
- out
89
+ @test all (out .== o1)
90
+
91
+ # # No parameters
92
+ @variables x y z
93
+ eqs = [(y- x)^ 2 ,
94
+ x* (x- z)- y,
95
+ x* y - y* z]
96
+ f = eval (ModelingToolkit. build_function (eqs,[x,y,z]))
97
+ out = zeros (3 )
98
+ o1 = f ([1.0 ,2 ,3 ])
99
+ f (out,[1.0 ,2 ,3 ])
100
+ @test all (out .== o1)
101
+
102
+ function test_worldage ()
103
+ @variables x y z
104
+ eqs = [(y- x)^ 2 ,
105
+ x* (x- z)- y,
106
+ x* y - y* z]
107
+ _f = eval (ModelingToolkit. build_function (eqs,[x,y,z]))
108
+ f (u) = ModelingToolkit. fast_invokelatest (_f,typeof (u),u)
109
+ f (du,u) = ModelingToolkit. fast_invokelatest (_f,Nothing,du,u)
110
+ out = zeros (3 )
111
+ o1 = f ([1.0 ,2 ,3 ])
112
+ f (out,[1.0 ,2 ,3 ])
113
+ end
114
+ test_worldage ()
115
+
116
+ mac = @I begin
117
+ @variables x y z
118
+ eqs = [(y- x)^ 2 ,
119
+ x* (x- z)- y,
120
+ x* y - y* z]
121
+ ModelingToolkit. build_function (eqs,[x,y,z])
122
+ end
123
+ f = @ICompile
124
+ out = zeros (3 )
125
+ o1 = f ([1.0 ,2 ,3 ])
126
+ f (out,[1.0 ,2 ,3 ])
127
+ @test all (out .== o1)
0 commit comments