|
| 1 | +# test/test_HarmonicVariable.jl |
| 2 | +using Test |
| 3 | +using Symbolics |
| 4 | +using QuestBase: declare_variable, declare_variables, substitute_all, var_name, HarmonicVariable, _show_ansatz, _coordinate_transform, @eqtest, _create_harmonic_variable, get_variables_nums |
| 5 | + |
| 6 | +# Setup |
| 7 | +@variables t T |
| 8 | +nat_var = declare_variable("x", t) |
| 9 | +rot_var = declare_variable("u", T) |
| 10 | +ω = declare_variable("ω") |
| 11 | + |
| 12 | +@testset "Constructor" begin |
| 13 | + hv = HarmonicVariable(rot_var, "test_name", "u", ω, nat_var) |
| 14 | + @eqtest hv.symbol == rot_var |
| 15 | + @test hv.name == "test_name" |
| 16 | + @test hv.type == "u" |
| 17 | + @eqtest hv.ω == ω |
| 18 | + @eqtest hv.natural_variable == nat_var |
| 19 | +end |
| 20 | + |
| 21 | +@testset "Show and Display" begin |
| 22 | + hv = HarmonicVariable(rot_var, "test_name", "u", ω, nat_var) |
| 23 | + # Test show output |
| 24 | + io = IOBuffer() |
| 25 | + show(io, hv) |
| 26 | + @test String(take!(io)) == "Harmonic variable u(T) for harmonic ω of x(t)\n" |
| 27 | + |
| 28 | + # Test _show_ansatz |
| 29 | + @test contains(_show_ansatz(hv), "cos(ωt)") |
| 30 | +end |
| 31 | + |
| 32 | +@testset "Coordinate Transforms" begin |
| 33 | + new_var = declare_variable("y") |
| 34 | + # Test u-type transform |
| 35 | + @eqtest _coordinate_transform(new_var, ω, t, "u") == new_var * cos(ω * t) |
| 36 | + # Test v-type transform |
| 37 | + @eqtest _coordinate_transform(new_var, ω, t, "v") == new_var * sin(ω * t) |
| 38 | + # Test a-type transform |
| 39 | + @eqtest _coordinate_transform(new_var, ω, t, "a") == new_var |
| 40 | +end |
| 41 | + |
| 42 | +@testset "Create Harmonic Variable" begin |
| 43 | + rule, hvar = _create_harmonic_variable(nat_var, ω, t, "u"; new_symbol="a") |
| 44 | + a = declare_variable("a", t) |
| 45 | + @eqtest rule == a * cos(ω * t) |
| 46 | + @test hvar isa HarmonicVariable |
| 47 | + @test rule isa Num |
| 48 | + @test hvar.type == "u" |
| 49 | + @eqtest hvar.ω == ω |
| 50 | +end |
| 51 | + |
| 52 | +@testset "Substitution" begin |
| 53 | + hv = HarmonicVariable(nat_var, "test_name", "u", ω, nat_var) |
| 54 | + new_var = declare_variable("z") |
| 55 | + rules = Dict(hv => new_var) |
| 56 | + |
| 57 | + # Test equation substitution |
| 58 | + eq = nat_var^2 + ω |
| 59 | + result = substitute_all(eq, rules) |
| 60 | + @eqtest result == new_var^2 + ω |
| 61 | + |
| 62 | + # Test variable substitution |
| 63 | + new_hv = substitute_all(hv, Dict(ω => 2)) |
| 64 | + @test new_hv.ω == 2 |
| 65 | +end |
0 commit comments