Skip to content

Commit a85ccc7

Browse files
committed
HarmonicEquation tests
1 parent 7e83671 commit a85ccc7

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

src/HarmonicEquation.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,13 @@ function is_rearranged(eom::HarmonicEquation)
175175
return HB_bool || hopf_bool || MF_bool
176176
end
177177

178+
"""
179+
$(TYPEDSIGNATURES)
180+
181+
(Re)-declare the variables of `eom` in module scope.
182+
"""
178183
function declare_variables(eom::HarmonicEquation)
179184
vars_orig = get_variables(eom)
180185
return declare_variable.(var_name.(vars_orig))
181186
end
187+
# TODO should this be the variable with independent_variable or without independent_variable?

src/HarmonicVariable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function d(f::Num, x::Num, deg=1)::Num
113113
end
114114
d(funcs::Vector{Num}, x::Num, deg=1) = Num[d(f, x, deg) for f in funcs]
115115

116-
"Declare a variable in the the currently active namespace"
116+
"Declare a variable in the the currently active Module namespace"
117117
function declare_variable(name::String)
118118
var_sym = Symbol(name)
119119
@eval($(var_sym) = first(Symbolics.@variables $var_sym))
@@ -122,7 +122,7 @@ end
122122

123123
declare_variable(x::Num) = declare_variable(string(x))
124124

125-
"Declare a variable that is a function of another variable in the the current namespace"
125+
"Declare a variable that is a function of another variable in the Module namespace"
126126
function declare_variable(name::String, independent_variable::Num)
127127
# independent_variable = declare_variable(independent_variable) convert string into Num
128128
var_sym = Symbol(name)

test/HarmonicEquation.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ end
1212
@testset "Symbolics customised" begin
1313
include("symbolics.jl")
1414
end
15+
16+
@testset "HarmonicEquation" begin
17+
include("HarmonicEquation.jl")
18+
end

0 commit comments

Comments
 (0)