Skip to content

Commit

Permalink
connector conveniencie
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Feb 26, 2024
1 parent 082a39e commit a428e54
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ function is_variable(x)
end

"""
new_derived_named_parameter(variable, value, extra::String, prefix = true)
new_derived_named_parameter(variable, value, extra::String; kw...)
If `value isa Num` return `value`.
If `value isa LiteralParameter`, replace it with its literal value.
Otherwise, create a new MTK `@parameter`
whose name is created from `variable` (which could also be just a `Symbol`) by adding the `extra` string.
If `prefix == false` the `extra` is added at the end after a `_`. Otherwise
If the keyword `prefix == false` the `extra` is added at the end after a `_`. Otherwise
it is added at the start, then a `_` and then the variable name.
The keyword `connector = "_"` is what connects the `extra` with the name.
For example,
Expand All @@ -77,14 +79,14 @@ p = new_derived_named_parameter(x, 0.5, "τ")
```
Now `p` will be a parameter with name `:τ_x` and default value `0.5`.
"""
new_derived_named_parameter(v, value::Num, args...) = value
new_derived_named_parameter(v, value::LiteralParameter, args...) = value.p
function new_derived_named_parameter(v, value::Real, extra, prefix = true)
new_derived_named_parameter(v, value::Num, extra::String; kw...) = value
new_derived_named_parameter(v, value::LiteralParameter, extra::String; kw...) = value.p
function new_derived_named_parameter(v, value::Real, extra; connector = "_", prefix = true)
n = string(ModelingToolkit.getname(v))
newstring = if !(prefix)
n*"_"*extra
n*connector*extra
else
extra*"_"*n
extra*connector*n
end
new_derived_named_parameter(newstring, value)
end
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ end
p = new_derived_named_parameter(x, 0.2, "t")
@test ModelingToolkit.getname(p) == :t_x
@test default_value(p) == 0.2
p = new_derived_named_parameter(x, 0.2, "t", false)
@test ModelingToolkit.getname(p) == :x_t
p = new_derived_named_parameter(x, 0.2, "t"; prefix = false, connector = "")
@test ModelingToolkit.getname(p) == :xt
end

@testset "convert" begin
Expand Down

0 comments on commit a428e54

Please sign in to comment.