Skip to content

Commit afa425c

Browse files
authored
add error and test for #255 (#258)
1 parent c1550d4 commit afa425c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/types.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,28 @@ mutable struct DifferentialEquation
4646
return DifferentialEquation(exprs .~ Int(0), vars)
4747
end
4848

49-
function DifferentialEquation(arg1, arg2)
50-
return DifferentialEquation(
51-
arg1 isa Vector ? arg1 : [arg1], arg2 isa Vector ? arg2 : [arg2]
49+
function DifferentialEquation(eq::Equation, var::Num)
50+
typerhs = typeof(eq.rhs)
51+
typelhs = typeof(eq.lhs)
52+
if eq.rhs isa AbstractVector || eq.lhs isa AbstractVector
53+
throw(
54+
ArgumentError(
55+
"The equation is of the form $(typerhs)~$(typelhs) is not supported. Commenly one forgot to broadcast the equation symbol `~`.",
56+
),
57+
)
58+
end
59+
return DifferentialEquation([eq], [var])
60+
end
61+
function DifferentialEquation(eq::Equation, vars::Vector{Num})
62+
typerhs = typeof(eq.rhs)
63+
typelhs = typeof(eq.lhs)
64+
throw(
65+
ArgumentError(
66+
"The variables are of type $(typeof(vars)). Whereas you gave one equation of type $(typerhs)~$(typelhs). Commenly one forgot to broadcast the equation symbol `~`.",
67+
),
5268
)
5369
end
70+
DifferentialEquation(lhs::Num, var::Num) = DifferentialEquation([lhs ~ Int(0)], [var])
5471
end
5572

5673
function Base.show(io::IO, diff_eq::DifferentialEquation)

test/API.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using HarmonicBalance
2+
3+
# define equation of motion
4+
@variables ω1, ω2, t, ω, F, γ, α1, α2, k, x(t), y(t);
5+
rhs = [
6+
d(x, t, 2) + ω1^2 * x + γ * d(x, t) + α1 * x^3 - k * y,
7+
d(d(y, t), t) + ω2^2 * y + γ * d(y, t) + α2 * y^3 - k * x,
8+
]
9+
eqs = rhs .~ [F * cos* t), 0]
10+
11+
@test_throws ArgumentError DifferentialEquation(rhs ~ [F * cos* t), 0], [x, y])

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ end
3232
JET.test_package(HarmonicBalance; target_defined_modules=true)
3333
end
3434

35+
@testset "Symbolics customised" begin
36+
include("API.jl")
37+
end
38+
3539
@testset "Symbolics customised" begin
3640
include("powers.jl")
3741
include("harmonics.jl")

0 commit comments

Comments
 (0)