Skip to content

Commit

Permalink
fix: fix "mathtt{}" symbolic bug (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
oameye authored Oct 27, 2024
1 parent b2068db commit dbc22df
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Plots = "1.39"
PrecompileTools = "1.2"
ProgressMeter = "1.7.2"
SteadyStateDiffEq = "2.3.2"
SymbolicUtils = "3.7"
Symbolics = "~6.14"
SymbolicUtils = "3.5"
Symbolics = "6.4"
julia = "1.10.0"

[extras]
Expand Down
7 changes: 4 additions & 3 deletions src/HarmonicVariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ function declare_variable(name::String, independent_variable::Num)
end

"Return the name of a variable (excluding independent variables)"
function var_name(x::Num)
function var_name(x::Num)::String
var = Symbolics._toexpr(x)
return var isa Expr ? String(var.args[1]) : String(var)
var = var isa Expr ? String(var.args[1]) : String(var)
return String(replace(var, r"\\mathtt\{([^}]*)\}" => s"\1"))
# ^ remove "\\mathtt{}" from the variable name coming from Symbolics since Symbolics v6.14.1 (Symbolics#1305)
end
# var_name(x::Term) = String(Symbolics._toexpr(x).args[1])
var_name(x::SymbolicUtils.Sym) = String(x.name)
71 changes: 6 additions & 65 deletions test/HarmonicVariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,10 @@ using HarmonicBalance
diff_eq = DifferentialEquation(
d(x, t, 2) + ω0 * x + α * x^3 + γ * d(x, t) + η * x^2 * d(x, t) ~ F * cos* t), x
) # define ODE
add_harmonic!(diff_eq, x, ω)
harmonic_eq = get_harmonic_equations(diff_eq)

add_harmonic!(diff_eq, x, ω) # specify the ansatz x = u(T) cos(ωt) + v(T) sin(ωt)

harmonic_eq = get_harmonic_equations(diff_eq) # implement ansatz to get harmonic equations
get_variables(harmonic_eq)

# @testset "HarmonicVariable Tests" begin
# # Test display function
# @testset "Display" begin
# var = HarmonicVariable(:x, "x", "u", 1.0, 2.0)
# @test display(var) == :x
# vars = [
# HarmonicVariable(:x, "x", "u", 1.0, 2.0),
# HarmonicVariable(:y, "y", "v", 2.0, 3.0),
# ]
# @test display(vars) == [:x, :y]
# end

# # Test _coordinate_transform function
# @testset "_coordinate_transform" begin
# @test _coordinate_transform(2.0, 1.0, 0.0, "u") == 2.0
# @test _coordinate_transform(2.0, 1.0, 0.0, "v") == 0.0
# @test _coordinate_transform(2.0, 1.0, 0.0, "a") == 2.0
# end

# # Test _create_harmonic_variable function
# @testset "_create_harmonic_variable" begin
# rule, hvar = _create_harmonic_variable(2.0, 1.0, 0.0, "u"; new_symbol="x")
# @test rule == 2.0
# @test hvar == HarmonicVariable(:x, "u_{2.0}", "u", 1.0, 2.0)
# end

# # Test substitute_all function
# @testset "substitute_all" begin
# eq = 2.0 * :x + 3.0 * :y
# rules = Dict(:x => 1.0, :y => 2.0)
# @test substitute_all(eq, rules) == 2.0 + 6.0
# var = HarmonicVariable(:x, "x", "u", 1.0, 2.0)
# @test substitute_all(var, rules) == HarmonicVariable(1.0, "x", "u", 1.0, 2.0)
# vars = [
# HarmonicVariable(:x, "x", "u", 1.0, 2.0),
# HarmonicVariable(:y, "y", "v", 2.0, 3.0),
# ]
# @test substitute_all(vars, rules) == [
# HarmonicVariable(1.0, "x", "u", 1.0, 2.0),
# HarmonicVariable(2.0, "y", "v", 2.0, 3.0),
# ]
# end

# # Test Symbolics.get_variables function
# @testset "Symbolics.get_variables" begin
# vars = [1.0, 2.0, 3.0]
# @test Symbolics.get_variables(vars) == [1.0, 2.0, 3.0]
# var = HarmonicVariable(:x, "x", "u", 1.0, 2.0)
# @test Symbolics.get_variables(var) == [:x]
# end

# # Test isequal function
# @testset "isequal" begin
# var1 = HarmonicVariable(:x, "x", "u", 1.0, 2.0)
# var2 = HarmonicVariable(:x, "x", "u", 1.0, 2.0)
# var3 = HarmonicVariable(:y, "y", "v", 2.0, 3.0)
# @test isequal(var1, var2) == true
# @test isequal(var1, var3) == false
# end
# end
@testset "" begin
vars = get_variables(harmonic_eq)
@test HarmonicBalance.var_name.(vars) == ["u1", "v1"]
end

0 comments on commit dbc22df

Please sign in to comment.