From dbc22df76aa7d9e294bcafeeaecec2080b8a3b21 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Sun, 27 Oct 2024 10:30:20 +0000 Subject: [PATCH] fix: fix "mathtt{}" symbolic bug (#292) --- Project.toml | 4 +-- src/HarmonicVariable.jl | 7 ++-- test/HarmonicVariable.jl | 71 ++++------------------------------------ 3 files changed, 12 insertions(+), 70 deletions(-) diff --git a/Project.toml b/Project.toml index da1a3ea5..59a8a3da 100644 --- a/Project.toml +++ b/Project.toml @@ -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] diff --git a/src/HarmonicVariable.jl b/src/HarmonicVariable.jl index 149c8c8e..332f987a 100644 --- a/src/HarmonicVariable.jl +++ b/src/HarmonicVariable.jl @@ -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) diff --git a/test/HarmonicVariable.jl b/test/HarmonicVariable.jl index 58937dec..88797607 100644 --- a/test/HarmonicVariable.jl +++ b/test/HarmonicVariable.jl @@ -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