From a3789ae17a9f67df87b98179483e044c33e1fe44 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Dec 2024 14:20:27 +0100 Subject: [PATCH] test with split true and false --- test/input_output_handling.jl | 113 ++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/test/input_output_handling.jl b/test/input_output_handling.jl index 25434e3dfb..de6fc92b5c 100644 --- a/test/input_output_handling.jl +++ b/test/input_output_handling.jl @@ -153,61 +153,66 @@ if VERSION >= v"1.8" # :opaque_closure not supported before end ## Code generation with unbound inputs +@testset "generate_control_function with disturbance inputs" begin + for split in [true, false] + simplify = true + + @variables x(t)=0 u(t)=0 [input = true] + eqs = [ + D(x) ~ -x + u + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys; simplify, split) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + @test f[1](x, u, p, 1) == -x + u + + # With disturbance inputs + @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + eqs = [ + D(x) ~ -x + u + d^2 + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d]; simplify, split) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + @test f[1](x, u, p, 1) == -x + u + + ## With added d argument + @variables x(t)=0 u(t)=0 [input = true] d(t)=0 + eqs = [ + D(x) ~ -x + u + d^2 + ] + + @named sys = ODESystem(eqs, t) + f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( + sys, [u], [d]; simplify, split, disturbance_argument = true) + + @test isequal(dvs[], x) + @test isempty(ps) + + p = nothing + x = [rand()] + u = [rand()] + d = [rand()] + @test f[1](x, u, p, t, d) == -x + u + [d[]^2] + end +end -@variables x(t)=0 u(t)=0 [input = true] -eqs = [ - D(x) ~ -x + u -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function(sys, simplify = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -@test f[1](x, u, p, 1) == -x + u - -# With disturbance inputs -@variables x(t)=0 u(t)=0 [input = true] d(t)=0 -eqs = [ - D(x) ~ -x + u + d^2 -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d], simplify = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -@test f[1](x, u, p, 1) == -x + u - -# With added d argument -@variables x(t)=0 u(t)=0 [input = true] d(t)=0 -eqs = [ - D(x) ~ -x + u + d^2 -] - -@named sys = ODESystem(eqs, t) -f, dvs, ps, io_sys = ModelingToolkit.generate_control_function( - sys, [u], [d], simplify = true, disturbance_argument = true) - -@test isequal(dvs[], x) -@test isempty(ps) - -p = nothing -x = [rand()] -u = [rand()] -d = [rand()] -@test f[1](x, u, p, 1, d) == -x + u + [d[]^2] - -# more complicated system +## more complicated system @variables u(t) [input = true]