|
| 1 | +# test/test_HarmonicEquation.jl |
| 2 | +using Test |
| 3 | +using Symbolics |
| 4 | +using SymbolicUtils |
| 5 | +using QuestBase |
| 6 | +using QuestBase: |
| 7 | + DifferentialEquation, |
| 8 | + HarmonicVariable, |
| 9 | + HarmonicEquation, |
| 10 | + _parameters, |
| 11 | + get_variables, |
| 12 | + get_independent_variables, |
| 13 | + rearrange_standard, |
| 14 | + substitute_all, |
| 15 | + is_rearranged, |
| 16 | + _remove_brackets, |
| 17 | + declare_variables, |
| 18 | + dummy_symbolic_Jacobian, |
| 19 | + @eqtest, |
| 20 | + get_all_terms, |
| 21 | + get_independent |
| 22 | + |
| 23 | +# Setup common test variables |
| 24 | +@variables t, T |
| 25 | +@variables x(t) y(t) u(T) v(T) |
| 26 | +D = Differential(T) |
| 27 | + |
| 28 | +# Create simple test equation |
| 29 | +eq1 = D(u) ~ u + v |
| 30 | +eq2 = D(v) ~ -u + v |
| 31 | +nat_eq = DifferentialEquation([eq1, eq2], [x, y]) |
| 32 | + |
| 33 | +# Create test harmonic variables |
| 34 | +hv1 = HarmonicVariable(u, "test", "u", Num(1.0), x) |
| 35 | +hv2 = HarmonicVariable(v, "test", "v", Num(1.0), y) |
| 36 | + |
| 37 | +# Test constructor |
| 38 | +@testset "Construction" begin |
| 39 | + heq1 = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) |
| 40 | + heq2 = HarmonicEquation([eq1, eq2], [hv1, hv2], Num[], nat_eq) |
| 41 | + for heq in [heq1, heq2] |
| 42 | + heq = heq1 |
| 43 | + @test heq.equations == [eq1, eq2] |
| 44 | + @test heq.variables == [hv1, hv2] |
| 45 | + @test heq.natural_equation == nat_eq |
| 46 | + @test heq.parameters == Num[] |
| 47 | + @test heq.jacobian isa Matrix{Num} |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +@testset "Parameter handling" begin |
| 52 | + parvar = @variables p q |
| 53 | + eq_with_params = D(u) ~ p * v + q * v |
| 54 | + heq = HarmonicEquation([eq_with_params, eq2], [hv1, hv2], nat_eq) |
| 55 | + params = _parameters(heq) |
| 56 | + @eqtest params == parvar |
| 57 | +end |
| 58 | + |
| 59 | +@testset "Variable handling" begin |
| 60 | + heq = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) |
| 61 | + vars = get_variables(heq) |
| 62 | + @test length(vars) == 2 |
| 63 | + @eqtest [T] == get_independent_variables(heq) |
| 64 | +end |
| 65 | + |
| 66 | +@testset "Equation manipulation" begin |
| 67 | + heq = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) |
| 68 | + |
| 69 | + # Test rearrange |
| 70 | + rearranged = rearrange_standard(heq) |
| 71 | + @test is_rearranged(rearranged) |
| 72 | + |
| 73 | + # Test substitute_all |
| 74 | + @variables a |
| 75 | + rules = Dict(u => a) |
| 76 | + subbed = substitute_all(heq, rules) |
| 77 | + @test !isequal(subbed.equations, heq.equations) |
| 78 | +end |
| 79 | + |
| 80 | +@testset "Utility functions" begin |
| 81 | + heq = HarmonicEquation([eq1, eq2], [hv1, hv2], nat_eq) |
| 82 | + |
| 83 | + # Test _remove_brackets |
| 84 | + no_brackets = _remove_brackets(heq) |
| 85 | + |
| 86 | + list = get_all_terms.(Symbolics.expand_derivatives.(no_brackets)) |
| 87 | + list = unique(filter(x -> !(x isa Real), Symbolics.unwrap.(reduce(vcat, list)))) |
| 88 | + @test all(map(x -> !hasproperty(x, :arguments), list)) |
| 89 | + |
| 90 | + # Test declare_variables |
| 91 | + declared = declare_variables(heq) |
| 92 | + list = get_all_terms.(Symbolics.expand_derivatives.(declared)) |
| 93 | + list = unique(filter(x -> !(x isa Real), Symbolics.unwrap.(reduce(vcat, list)))) |
| 94 | + @test all(map(x -> !hasproperty(x, :arguments), list)) |
| 95 | +end |
0 commit comments