Skip to content

Commit 064254f

Browse files
authored
Small enhancements (#10)
* better error messages for dupliacte processes * add equation function * version * even better error message
1 parent 77a24ea commit 064254f

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
ProcessBasedModelling.jl follows semver 2.0.
44
Changelog is kept with respect to v1 release.
55

6+
## 1.3
7+
8+
- Better error messages for duplicate processes given to `process_to_mtkmodel`.
9+
- New exported function `equation(p::Process)` that returns `lhs(p) ~ rhs(p)`.
10+
611
## 1.2
712

813
The API for default processes has been drastically improved.

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.2.4"
4+
version = "1.3.0"
55

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

src/API.jl

+7
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,10 @@ function lhs_variable(x) # basically x is SymbolicUtils.BasicSymbolic{Real}
110110
throw(ArgumentError("We analyzed the LHS `$(x)` "*
111111
"but could not extract a single variable it represents."))
112112
end
113+
114+
"""
115+
equation(process)
116+
117+
Return the `Equation` represented by the given process.
118+
"""
119+
equation(proc) = lhs(proc) ~ rhs(proc)

src/ProcessBasedModelling.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export processes_to_mtkmodel
2323
export new_derived_named_parameter
2424
export has_symbolic_var, default_value
2525
export @convert_to_parameters, LiteralParameter
26-
# export lhs_variable, rhs, lhs # I am not sure whether these should be exported.
27-
export all_equations
26+
export all_equations, equation
2827

2928
end

src/make.jl

+17-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function processes_to_mtkmodel(_processes::Vector, default::Dict{Num, Any};
6767
# Setup: obtain lhs-variables so we can track new variables that are not
6868
# in this vector. The vector has to be of type `Num`
6969
lhs_vars = Num[lhs_variable(p) for p in processes]
70-
ensure_unique_vars(lhs_vars)
70+
ensure_unique_vars(lhs_vars, processes)
7171
# First pass: generate equations from given processes
7272
# and collect variables without equations
7373
incomplete = Num[]
@@ -76,7 +76,7 @@ function processes_to_mtkmodel(_processes::Vector, default::Dict{Num, Any};
7676
for proc in processes
7777
append_incomplete_variables!(incomplete, introduced, lhs_vars, proc)
7878
# add the generated equation in the pool of equations
79-
push!(eqs, lhs(proc) ~ rhs(proc))
79+
push!(eqs, equation(proc))
8080
end
8181
# Second pass: attempt to add default processes to incomplete variables
8282
# throw an error if default does not exist
@@ -169,9 +169,22 @@ function append_incomplete_variables!(incomplete, introduced, lhs_vars, process)
169169
return
170170
end
171171

172-
function ensure_unique_vars(lhs_vars)
172+
function ensure_unique_vars(lhs_vars, processes)
173173
nonun = nonunique(lhs_vars)
174-
isempty(nonun) || error("The following variables have more than one processes assigned to them: $(nonun)")
174+
isempty(nonun) && return # all good!
175+
dupprocs = []
176+
for var in nonun
177+
idxs = findall(p -> Symbol(lhs_variable(p)) == Symbol(var), processes)
178+
append!(dupprocs, processes[idxs])
179+
end
180+
errormsg = """
181+
The following variables have more than one processes assigned to them: $(nonun).
182+
The duplicate processes are:
183+
"""
184+
for p in dupprocs
185+
errormsg *= "\n"*sprint(show, p)
186+
end
187+
throw(ArgumentError(errormsg))
175188
return
176189
end
177190

test/runtests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ end
160160
@test p == 0.5
161161
end
162162

163+
@testset "equation" begin
164+
@variables x(t)
165+
@test equation(ParameterProcess(x, LiteralParameter(0.5))) == (x ~ 0.5)
166+
end
167+
163168
end
164169

165170
@testset "default processes, has_thing" begin

0 commit comments

Comments
 (0)