Skip to content

Commit aab62c2

Browse files
committed
allow AdditionProcess to accept arbitrarily many adders
1 parent 8cedcb6 commit aab62c2

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ProcessBasedModelling"
22
uuid = "ca969041-2cf3-4b10-bc21-86f4417093eb"
33
authors = ["Datseris <[email protected]>"]
4-
version = "1.0.3"
4+
version = "1.0.4"
55

66
[deps]
77
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"

src/processes_basic.jl

+19-16
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,37 @@ function rhs(e::ExpRelaxation)
9191
end
9292

9393
"""
94-
AdditionProcess(process, added)
94+
AdditionProcess(process, added...)
9595
96-
A convenience process for adding `added` to the `rhs` of the given `process`.
97-
`added` can be a `Process` or `Equation`, in which case it is checked that
98-
the `lhs_variable` matches. Otherwise, it can be an arbitrary expression.
96+
A convenience process for adding processes `added` to the `rhs` of the given `process`.
97+
`added` can be a single symbolic expression. Otherwise,
98+
`added` can be a `Process` or `Equation`, or multitude of them, in which case it is checked
99+
that the `lhs_variable` across all added components matches the `process`.
99100
"""
100101
struct AdditionProcess <: Process
101102
process
102-
added
103-
function AdditionProcess(process, added)
104-
if typeof(added) <: Union{Process, Equation}
105-
if ModelingToolkit.getname(lhs_variable(process)) ModelingToolkit.getname(lhs_variable(added))
106-
@show lhs_variable(process), lhs_variable(added)
107-
throw(ArgumentError("Added component does not have the same lhs variable."))
103+
added::Vector
104+
function AdditionProcess(process, added::Vector)
105+
for add in added
106+
if typeof(add) <: Union{Process, Equation}
107+
v1, v2 = ModelingToolkit.getname(lhs_variable(process)), ModelingToolkit.getname(lhs_variable(add))
108+
if v1 v2
109+
throw(ArgumentError(
110+
"Added processes do not have the same lhs variable. Got: $(v1), $(v2)"
111+
))
112+
end
108113
end
109114
end
110115
return new(process, added)
111116
end
112117
end
118+
AdditionProcess(process, added...) = AdditionProcess(process, collect(added))
119+
AdditionProcess(process, added::Num) = AdditionProcess(process, lhs(process) ~ added)
113120

114121
lhs_variable(a::AdditionProcess) = lhs_variable(a.process)
115122
timescale(a::AdditionProcess) = timescale(a.process)
116123

117124
function rhs(a::AdditionProcess)
118-
if typeof(a.added) <: Union{Process, Equation}
119-
plus = rhs(a.added)
120-
else
121-
plus = a.added
122-
end
123-
return rhs(a.process) + plus
125+
exprs = [rhs(p) for p in a.added]
126+
return +(rhs(a.process), exprs...)
124127
end

test/runtests.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ end
173173
ParameterProcess(y),
174174
ExpRelaxation(z, x^2, 0.5),
175175
AdditionProcess(ParameterProcess(w), x^2),
176-
AdditionProcess(TimeDerivative(q, x^2, 1.2), ExpRelaxation(q, x^2))
176+
AdditionProcess(TimeDerivative(q, x^2, 1.2), ExpRelaxation(q, x^2), q ~ y*x)
177177
]
178178
mtk = processes_to_mtkmodel(processes)
179179
mtk = structural_simplify(mtk)
@@ -186,4 +186,9 @@ end
186186
@test has_symbolic_var(mtk, q)
187187
@test has_symbolic_var(eqs, mtk.τ_z)
188188
@test has_symbolic_var(eqs, :w_0)
189+
end
190+
191+
@testset "addition process error" begin
192+
@variables x(t) y(t) z(t)
193+
@test_throws ArgumentError AdditionProcess(x ~ 0.1z, y ~ x^2)
189194
end

0 commit comments

Comments
 (0)