Skip to content

Commit

Permalink
fix literal value
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Feb 22, 2024
1 parent 31e61d9 commit 02c9fa6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ but rather keep it as a numeric literal in the generated equations.
struct LiteralParameter{P}
p::P
end
# necessary for the macro
_literalvalue(x) = x
_literalvalue(p::LiteralParameter) = p.p

"""
has_variable(eq, var)
Expand Down Expand Up @@ -63,7 +66,7 @@ If `value isa Num` return `value`.
If `value isa `[`LiteralParameter`](@ref), replace it with its literal value.
Otherwise, create a new MTK `@parameter`
whose name is created from `variable` by adding the `extra` string.
If `suffix = true` the extra is added at the end after a `_`. Otherwise
If `suffix == true` the extra is added at the end after a `_`. Otherwise
it is added at the start, then a `_` and then the variable name.
For example,
Expand Down Expand Up @@ -133,7 +136,7 @@ macro convert_to_parameters(vars...)
varname = QuoteNode(var)
push!(expr.args,
:($binding = ifelse(
$binding isa LiteralParameter, $(binding).p, ifelse(
$binding isa LiteralParameter, _literalvalue($(binding)), ifelse(
# don't do anyting if this is already a Num
$binding isa Num, $binding,
# Else, convert to modeling toolkit param.
Expand Down
49 changes: 30 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,38 @@ end
end

@testset "utility functions" begin
@variables x(t) = 0.5
p = new_derived_named_parameter(x, 0.2, "t")
@test ModelingToolkit.getname(p) == :x_t
@test default_value(p) == 0.2
p = new_derived_named_parameter(x, p, "lala")
@test ModelingToolkit.getname(p) == :x_t

A, B = 0.5, 0.5
C = first(@parameters X = 0.5)
@convert_to_parameters A B C
@test A isa Num
@test default_value(A) == 0.5
@test ModelingToolkit.getname(C) == :X
# Test an untested clause:
@test default_value(0.5) == 0.5

p = LiteralParameter(0.5)
p = new_derived_named_parameter(x, p, "t")
@test p == 0.5
@testset "derived" begin
@variables x(t) = 0.5
p = new_derived_named_parameter(x, 0.2, "t")
@test ModelingToolkit.getname(p) == :x_t
@test default_value(p) == 0.2
p = new_derived_named_parameter(x, p, "lala")
@test ModelingToolkit.getname(p) == :x_t
end

@testset "convert" begin
A, B = 0.5, 0.5
C = first(@parameters X = 0.5)
@convert_to_parameters A B C
@test A isa Num
@test default_value(A) == 0.5
@test ModelingToolkit.getname(C) == :X

end

@testset "literal in derived" begin
p = LiteralParameter(0.5)
p = new_derived_named_parameter(x, p, "t")
@test p == 0.5
end

@testset "literal in covert" begin
p = LiteralParameter(0.5)
@convert_to_parameters p
@test p == 0.5
end

p = LiteralParameter(0.5)
@convert_to_parameters p
@test p == 0.5
end

0 comments on commit 02c9fa6

Please sign in to comment.