|
| 1 | +using ModelingToolkit, StaticArrays, LinearAlgebra |
| 2 | +using DiffEqBase |
| 3 | +using Test |
| 4 | + |
| 5 | +# Calculus |
| 6 | +@parameters t σ ρ β |
| 7 | +@variables x y z |
| 8 | + |
| 9 | +eqs = [σ*(y-x), |
| 10 | + x*(ρ-z)-y, |
| 11 | + x*y - β*z] |
| 12 | + |
| 13 | +simpexpr = [ |
| 14 | + :(σ * (y - x)) |
| 15 | + :(x * (ρ - z) - y) |
| 16 | + :(x * y - β * z) |
| 17 | + ] |
| 18 | + |
| 19 | +for i in 1:3 |
| 20 | + @test ModelingToolkit.simplified_expr.(eqs)[i] == simpexpr[i] |
| 21 | + @test ModelingToolkit.simplified_expr.(eqs)[i] == simpexpr[i] |
| 22 | +end |
| 23 | + |
| 24 | +∂ = ModelingToolkit.jacobian(eqs,[x,y,z]) |
| 25 | +for i in 1:3 |
| 26 | + ∇ = ModelingToolkit.gradient(eqs[i],[x,y,z]) |
| 27 | + @test isequal(∂[i,:],∇) |
| 28 | +end |
| 29 | +@test all(isequal.(ModelingToolkit.gradient(eqs[1],[x,y,z]),[σ * -1,σ,0])) |
| 30 | +@test all(isequal.(ModelingToolkit.hessian(eqs[1],[x,y,z]),0)) |
| 31 | + |
| 32 | +# Function building |
| 33 | + |
| 34 | +@parameters σ() ρ() β() |
| 35 | +@variables x y z |
| 36 | +eqs = [σ*(y-x), |
| 37 | + x*(ρ-z)-y, |
| 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) |
| 44 | + |
| 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() |
| 59 | + |
| 60 | +mac = @I begin |
| 61 | + @parameters σ() ρ() β() |
| 62 | + @variables x() y() z() |
| 63 | + |
| 64 | + eqs = [σ*(y-x), |
| 65 | + x*(ρ-z)-y, |
| 66 | + x*y - β*z] |
| 67 | + ModelingToolkit.build_function(eqs,[x,y,z],[σ,ρ,β]) |
| 68 | +end |
| 69 | +f = @ICompile |
| 70 | +out = [1.0,2,3] |
| 71 | +o1 = f([1.0,2,3],[1.0,2,3]) |
| 72 | +f(out,[1.0,2,3],[1.0,2,3]) |
| 73 | +@test all(o1 .== out) |
| 74 | + |
| 75 | +mac = @I begin |
| 76 | + @parameters σ ρ β |
| 77 | + @variables x y z |
| 78 | + |
| 79 | + eqs = [σ*(y-x), |
| 80 | + x*(ρ-z)-y, |
| 81 | + x*y - β*z] |
| 82 | + ∂ = ModelingToolkit.jacobian(eqs,[x,y,z]) |
| 83 | + ModelingToolkit.build_function(∂,[x,y,z],[σ,ρ,β]) |
| 84 | +end |
| 85 | +f = @ICompile |
| 86 | +out = zeros(3,3) |
| 87 | +o1 = f([1.0,2,3],[1.0,2,3]) |
| 88 | +f(out,[1.0,2,3],[1.0,2,3]) |
| 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