Skip to content

Commit

Permalink
add LiteralParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Feb 22, 2024
1 parent 6a88a88 commit 31e61d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,5 @@ default_value
has_variable
new_derived_named_parameter
@convert_to_parameters
LiteralParameter
```
2 changes: 1 addition & 1 deletion src/ProcessBasedModelling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export Process, ParameterProcess, TimeDerivative, ExpRelaxation
export processes_to_mtkmodel
export new_derived_named_parameter
export has_variable, default_value
export @convert_to_parameters
export @convert_to_parameters, LiteralParameter
export lhs_variable, rhs, lhs

end
23 changes: 20 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""
LiteralParameter(p)
A wrapper around a value `p` to indicate to
[`new_derived_named_parameter`](@ref) or [`@convert_to_parameters`](@ref)
to _not_ convert the given parameter `p` into a named `@parameters` instance,
but rather keep it as a numeric literal in the generated equations.
"""
struct LiteralParameter{P}
p::P
end

"""
has_variable(eq, var)
Expand Down Expand Up @@ -47,7 +59,9 @@ end
"""
new_derived_named_parameter(variable, value, extra::String, suffix = true)
If `value isa Num`, return `value`. Otherwise, create a new MTK `@parameter`
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
it is added at the start, then a `_` and then the variable name.
Expand All @@ -60,7 +74,8 @@ 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, extra, suffix = true) = value
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, suffix = true)
n = string(ModelingToolkit.getname(v))
newstring = if suffix
Expand All @@ -84,6 +99,7 @@ end
Convert all variables `vars` into `@parameters` with name the same as `vars`
and default value the same as the value of `vars`. The macro leaves unaltered
inputs that are of type `Num`, assumming they are already parameters.
It also replaces [`LiteralParameter`](@ref) inputs with its literal values.
This macro is extremely useful to convert e.g., keyword arguments into named parameters,
while also allowing the user to give custom parameter names.
Expand Down Expand Up @@ -117,12 +133,13 @@ macro convert_to_parameters(vars...)
varname = QuoteNode(var)
push!(expr.args,
:($binding = ifelse(
$binding isa LiteralParameter, $(binding).p, ifelse(
# don't do anyting if this is already a Num
$binding isa Num, $binding,
# Else, convert to modeling toolkit param.
# This syntax was obtained by doing @macroexpand @parameters A = 0.5
(ModelingToolkit.toparam)((Symbolics.wrap)((SymbolicUtils.setmetadata)((Symbolics.setdefaultval)((Sym){Real}($varname), $binding), Symbolics.VariableSource, (:parameters, $varname))))
)
))
)
)
end
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ end
@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

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

0 comments on commit 31e61d9

Please sign in to comment.