Skip to content

Commit 1c56c60

Browse files
committed
Merge remote-tracking branch 'origin/master' into update_smol_tutorial
2 parents 7ec86bb + 670c5be commit 1c56c60

File tree

6 files changed

+177
-78
lines changed

6 files changed

+177
-78
lines changed

src/reaction.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ end
163163
function Reaction(rate, subs, prods, substoich, prodstoich;
164164
netstoich = nothing, metadata = Pair{Symbol, Any}[],
165165
only_use_rate = metadata_only_use_rate_check(metadata), kwargs...)
166+
# Handles empty/nothing vectors.
167+
isnothing(subs) || isempty(subs) && (subs = nothing)
168+
isnothing(prods) || isempty(prods) && (prods = nothing)
169+
isnothing(substoich) || isempty(substoich) && (substoich = nothing)
170+
isnothing(prodstoich) || isempty(prodstoich) && (prodstoich = nothing)
171+
166172
(isnothing(prods) && isnothing(subs)) &&
167173
throw(ArgumentError("A reaction requires a non-nothing substrate or product vector."))
168174
(isnothing(prodstoich) && isnothing(substoich)) &&

src/reactionsystem_serialisation/serialisation_support.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function push_field(file_text::String, rn::ReactionSystem, annotate::Bool, top_l
3636
has_component, get_comp_string, get_comp_annotation = comp_funcs
3737
has_component(rn) || (return (file_text, false))
3838

39-
# Prepares the text creating the field. For non-top level systems, adds `local `. Observables
39+
# Prepares the text creating the field. For non-top level systems, add `local `. Observables
4040
# must be handled differently (as the declaration is not at the beginning of the code for these).
4141
# The independent variables is not declared as a variable, and also should not have a `1ocal `.
4242
write_string = get_comp_string(rn)
@@ -54,7 +54,7 @@ function push_field(file_text::String, rn::ReactionSystem, annotate::Bool, top_l
5454
return (file_text * write_string, true)
5555
end
5656

57-
# Generic function for creating an string for an unsupported argument.
57+
# Generic function for creating an string for a unsupported argument.
5858
function get_unsupported_comp_string(component::String)
5959
@warn "Writing ReactionSystem models with $(component) is currently not supported. This field is not written to the file."
6060
return ""
@@ -82,9 +82,10 @@ function syms_2_strings(syms)
8282
return get_substring_end("$(convert(Vector{Any}, strip_called_syms))", 4, 0)
8383
end
8484

85-
# Converts a vector of symbolics (e.g. the species or parameter vectors) to a string corresponding to
86-
# the code required to declare them (potential @parameters or @species commands must still be added).
87-
# The `multiline_format` option formats it with a `begin ... end` block and declarations on separate lines.
85+
# Converts a vector of symbolic variables (e.g. the species or parameter vectors) to a string
86+
# corresponding to the code required to declare them (potential @parameters or @species commands
87+
# must still be added). The `multiline_format` option formats it with a `begin ... end` block
88+
# and declarations on separate lines.
8889
function syms_2_declaration_string(syms; multiline_format = false)
8990
decs_string = (multiline_format ? " begin" : "")
9091
for sym in syms
@@ -102,7 +103,7 @@ function sym_2_declaration_string(sym; multiline_format = false)
102103
# Creates the basic symbol. The `"$(sym)"` ensures that we get e.g. "X(t)" and not "X".
103104
dec_string = "$(sym)"
104105

105-
# If the symbol have a non-default type, appends the declaration of this.
106+
# If the symbol has a non-default type, appends the declaration of this.
106107
# Assumes that the type is on the form `SymbolicUtils.BasicSymbolic{X}`. Contain error checks
107108
# to ensure that this is the case.
108109
if !(sym isa SymbolicUtils.BasicSymbolic{Real})
@@ -136,7 +137,7 @@ end
136137

137138
# Converts a generic value to a String. Handles each type of value separately. Unsupported values might
138139
# not necessarily generate valid code, and hence throw errors. Primarily used to write default values
139-
# and metadata values (which hopefully almost exclusively) has simple, supported, types. Ideally,
140+
# and metadata values (which hopefully almost exclusively) have simple, supported, types. Ideally,
140141
# more supported types can be added here.
141142
x_2_string(x::Num) = expression_2_string(x)
142143
x_2_string(x::SymbolicUtils.BasicSymbolic{<:Real}) = expression_2_string(x)
@@ -261,7 +262,7 @@ function get_dep_syms(sym)
261262
return Symbolics.get_variables(ModelingToolkit.getdefault(sym))
262263
end
263264

264-
# Checks if a symbolic depends on an symbolics in a vector being declared.
265+
# Checks if a symbolic depends on a symbolics in a vector being declared.
265266
# Because Symbolics has to utilise `isequal`, the `isdisjoint` function cannot be used.
266267
function depends_on(sym, syms)
267268
dep_syms = get_dep_syms(sym)
@@ -275,8 +276,8 @@ end
275276

276277
# For a set of remaining parameters/species/variables (remaining_syms), return this split into
277278
# two sets:
278-
# One with those that does not depend on any sym in `all_remaining_syms`.
279-
# One with those that does depend on at least one sym in `all_remaining_syms`.
279+
# One with those that do not depend on any sym in `all_remaining_syms`.
280+
# One with those that do depend on at least one sym in `all_remaining_syms`.
280281
# The first set is returned. Next `remaining_syms` is updated to be the second set.
281282
function dependency_split!(remaining_syms, all_remaining_syms)
282283
writable_syms = filter(sym -> !depends_on(sym, all_remaining_syms), remaining_syms)
@@ -289,7 +290,7 @@ end
289290

290291

291292
# Checks if a symbolic's declaration is "complicated". The declaration is considered complicated
292-
# if it have metadata, default value, or type designation that must be declared.
293+
# if it has metadata, default value, or type designation that must be declared.
293294
function complicated_declaration(sym)
294295
isempty(get_metadata_to_declare(sym)) || (return true)
295296
ModelingToolkit.hasdefault(sym) && (return true)

src/reactionsystem_serialisation/serialise_fields.jl

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### Handles Independent Variables ###
22

3-
# Checks if the reaction system have any independent variable. True for all valid reaction systems.
4-
function has_iv(rn::ReactionSystem)
3+
# Checks if the reaction system has any independent variable. True for all valid reaction systems.
4+
function seri_has_iv(rn::ReactionSystem)
55
return true
66
end
77

@@ -17,13 +17,13 @@ function get_iv_annotation(rn::ReactionSystem)
1717
end
1818

1919
# Combines the 3 independent variable-related functions in a constant tuple.
20-
IV_FS = (has_iv, get_iv_string, get_iv_annotation)
20+
IV_FS = (seri_has_iv, get_iv_string, get_iv_annotation)
2121

2222

2323
### Handles Spatial Independent Variables ###
2424

25-
# Checks if the reaction system have any spatial independent variables.
26-
function has_sivs(rn::ReactionSystem)
25+
# Checks if the reaction system has any spatial independent variables.
26+
function seri_has_sivs(rn::ReactionSystem)
2727
return !isempty(get_sivs(rn))
2828
end
2929

@@ -38,23 +38,23 @@ function get_sivs_annotation(rn::ReactionSystem)
3838
end
3939

4040
# Combines the 3 independent variables-related functions in a constant tuple.
41-
SIVS_FS = (has_sivs, get_sivs_string, get_sivs_annotation)
41+
SIVS_FS = (seri_has_sivs, get_sivs_string, get_sivs_annotation)
4242

4343

4444
### Handles Species, Variables, and Parameters ###
4545

4646
# Function which handles the addition of species, variable, and parameter declarations to the file
4747
# text. These must be handled as a unity in case there are default value dependencies between these.
4848
function handle_us_n_ps(file_text::String, rn::ReactionSystem, annotate::Bool, top_level::Bool)
49-
# Fetches the systems parameters, species, and variables. Computes the `has_` `Bool`s.
49+
# Fetches the system's parameters, species, and variables. Computes the `has_` `Bool`s.
5050
ps_all = get_ps(rn)
5151
sps_all = get_species(rn)
5252
vars_all = filter(!isspecies, get_unknowns(rn))
53-
has_ps = has_parameters(rn)
54-
has_sps = has_species(rn)
55-
has_vars = has_variables(rn)
53+
has_ps = seri_has_parameters(rn)
54+
has_sps = seri_has_species(rn)
55+
has_vars = seri_has_variables(rn)
5656

57-
# Checks which sets have dependencies which requires managing.
57+
# Checks which sets have dependencies which require managing.
5858
p_deps = any(depends_on(p, [ps_all; sps_all; vars_all]) for p in ps_all)
5959
sp_deps = any(depends_on(sp, [sps_all; vars_all]) for sp in sps_all)
6060
var_deps = any(depends_on(var, vars_all) for var in vars_all)
@@ -93,7 +93,7 @@ function handle_us_n_ps(file_text::String, rn::ReactionSystem, annotate::Bool, t
9393

9494
# Pre-declares the sets with written/remaining parameters/species/variables.
9595
# Whenever all/none are written depends on whether there were any initial dependencies.
96-
# `deepcopy` is required as these gets mutated by `dependency_split!`.
96+
# `deepcopy` is required as these get mutated by `dependency_split!`.
9797
remaining_ps = (p_deps ? deepcopy(ps_all) : [])
9898
remaining_sps = (sp_deps ? deepcopy(sps_all) : [])
9999
remaining_vars = (var_deps ? deepcopy(vars_all) : [])
@@ -113,7 +113,7 @@ function handle_us_n_ps(file_text::String, rn::ReactionSystem, annotate::Bool, t
113113
isempty(writable_vars) || @string_append! us_n_ps_string get_variables_string(writable_vars) "\n"
114114
end
115115

116-
# For parameters, species, and/or variables with dependencies, creates final vectors.
116+
# For parameters, species, and/or variables with dependencies, create final vectors.
117117
p_deps && (@string_append! us_n_ps_string "ps = " syms_2_strings(ps_all) "\n")
118118
sp_deps && (@string_append! us_n_ps_string "sps = " syms_2_strings(sps_all) "\n")
119119
var_deps && (@string_append! us_n_ps_string "vars = " syms_2_strings(vars_all) "\n")
@@ -127,17 +127,17 @@ function handle_us_n_ps(file_text::String, rn::ReactionSystem, annotate::Bool, t
127127
us_n_ps_string = replace(us_n_ps_string, "\nvars = " => "\nlocal vars = ")
128128
end
129129

130-
# Merges the file text with `us_n_ps_string` and return the final outputs.
130+
# Merges the file text with `us_n_ps_string` and returns the final outputs.
131131
return file_text * us_n_ps_string, has_ps, has_sps, has_vars
132132
end
133133

134134

135135
### Handles Parameters ###
136-
# Unlike most other fields, there are not called via `push_field`, but rather via `handle_us_n_ps`.
136+
# Unlike most other fields, these are not called via `push_field`, but rather via `handle_us_n_ps`.
137137
# Hence they work slightly differently.
138138

139-
# Checks if the reaction system have any parameters.
140-
function has_parameters(rn::ReactionSystem)
139+
# Checks if the reaction system has any parameters.
140+
function seri_has_parameters(rn::ReactionSystem)
141141
return !isempty(get_ps(rn))
142142
end
143143

@@ -156,11 +156,11 @@ end
156156

157157

158158
### Handles Species ###
159-
# Unlike most other fields, there are not called via `push_field`, but rather via `handle_us_n_ps`.
159+
# Unlike most other fields, these are not called via `push_field`, but rather via `handle_us_n_ps`.
160160
# Hence they work slightly differently.
161161

162-
# Checks if the reaction system have any species.
163-
function has_species(rn::ReactionSystem)
162+
# Checks if the reaction system has any species.
163+
function seri_has_species(rn::ReactionSystem)
164164
return !isempty(get_species(rn))
165165
end
166166

@@ -179,11 +179,11 @@ end
179179

180180

181181
### Handles Variables ###
182-
# Unlike most other fields, there are not called via `push_field`, but rather via `handle_us_n_ps`.
182+
# Unlike most other fields, these are not called via `push_field`, but rather via `handle_us_n_ps`.
183183
# Hence they work slightly differently.
184184

185-
# Checks if the reaction system have any variables.
186-
function has_variables(rn::ReactionSystem)
185+
# Checks if the reaction system has any variables.
186+
function seri_has_variables(rn::ReactionSystem)
187187
return length(get_unknowns(rn)) > length(get_species(rn))
188188
end
189189

@@ -201,13 +201,13 @@ function get_variables_annotation(rn::ReactionSystem)
201201
end
202202

203203
# Combines the 3 variables-related functions in a constant tuple.
204-
VARIABLES_FS = (has_variables, get_variables_string, get_variables_annotation)
204+
VARIABLES_FS = (seri_has_variables, get_variables_string, get_variables_annotation)
205205

206206

207207
### Handles Reactions ###
208208

209-
# Checks if the reaction system have any reactions.
210-
function has_reactions(rn::ReactionSystem)
209+
# Checks if the reaction system has any reactions.
210+
function seri_has_reactions(rn::ReactionSystem)
211211
return length(reactions(rn)) != 0
212212
end
213213

@@ -265,14 +265,14 @@ function get_reactions_annotation(rn::ReactionSystem)
265265
return "Reactions:"
266266
end
267267

268-
# Combines the 3 reactions-related functions in a constant tuple.
269-
REACTIONS_FS = (has_reactions, get_reactions_string, get_reactions_annotation)
268+
# Combines the 3 reaction-related functions in a constant tuple.
269+
REACTIONS_FS = (seri_has_reactions, get_reactions_string, get_reactions_annotation)
270270

271271

272272
### Handles Equations ###
273273

274-
# Checks if the reaction system have any equations.
275-
function has_equations(rn::ReactionSystem)
274+
# Checks if the reaction system has any equations.
275+
function seri_has_equations(rn::ReactionSystem)
276276
return length(get_eqs(rn)) > length(get_rxs(rn))
277277
end
278278

@@ -302,13 +302,13 @@ function get_equations_annotation(rn::ReactionSystem)
302302
end
303303

304304
# Combines the 3 equations-related functions in a constant tuple.
305-
EQUATIONS_FS = (has_equations, get_equations_string, get_equations_annotation)
305+
EQUATIONS_FS = (seri_has_equations, get_equations_string, get_equations_annotation)
306306

307307

308308
### Handles Observables ###
309309

310-
# Checks if the reaction system have any observables.
311-
function has_observed(rn::ReactionSystem)
310+
# Checks if the reaction system has any observables.
311+
function seri_has_observed(rn::ReactionSystem)
312312
return !isempty(observed(rn))
313313
end
314314

@@ -353,13 +353,13 @@ function get_observed_annotation(rn::ReactionSystem)
353353
end
354354

355355
# Combines the 3 -related functions in a constant tuple.
356-
OBSERVED_FS = (has_observed, get_observed_string, get_observed_annotation)
356+
OBSERVED_FS = (seri_has_observed, get_observed_string, get_observed_annotation)
357357

358358

359359
### Handles Continuous Events ###
360360

361-
# Checks if the reaction system have any continuous events.
362-
function has_continuous_events(rn::ReactionSystem)
361+
# Checks if the reaction system have has continuous events.
362+
function seri_has_continuous_events(rn::ReactionSystem)
363363
return !isempty(MT.get_continuous_events(rn))
364364
end
365365

@@ -410,13 +410,13 @@ function get_continuous_events_annotation(rn::ReactionSystem)
410410
end
411411

412412
# Combines the 3 -related functions in a constant tuple.
413-
CONTINUOUS_EVENTS_FS = (has_continuous_events, get_continuous_events_string, get_continuous_events_annotation)
413+
CONTINUOUS_EVENTS_FS = (seri_has_continuous_events, get_continuous_events_string, get_continuous_events_annotation)
414414

415415

416416
### Handles Discrete Events ###
417417

418-
# Checks if the reaction system have any discrete events.
419-
function has_discrete_events(rn::ReactionSystem)
418+
# Checks if the reaction system has any discrete events.
419+
function seri_has_discrete_events(rn::ReactionSystem)
420420
return !isempty(MT.get_discrete_events(rn))
421421
end
422422

@@ -466,17 +466,17 @@ function get_discrete_events_annotation(rn::ReactionSystem)
466466
end
467467

468468
# Combines the 3 -related functions in a constant tuple.
469-
DISCRETE_EVENTS_FS = (has_discrete_events, get_discrete_events_string, get_discrete_events_annotation)
469+
DISCRETE_EVENTS_FS = (seri_has_discrete_events, get_discrete_events_string, get_discrete_events_annotation)
470470

471471

472472
### Handles Systems ###
473473

474474
# Specific `push_field` function, which is used for the system field (where the annotation option
475475
# must be passed to the `get_component_string` function). Since non-ReactionSystem systems cannot be
476-
# written to file, this functions throws an error if any such systems are encountered.
476+
# written to file, this function throws an error if any such systems are encountered.
477477
function push_systems_field(file_text::String, rn::ReactionSystem, annotate::Bool, top_level::Bool)
478-
# Checks whther there are any subsystems, and if these are ReactionSystems.
479-
has_systems(rn) || (return (file_text, false))
478+
# Checks whether there are any subsystems, and if these are ReactionSystems.
479+
seri_has_systems(rn) || (return (file_text, false))
480480
if any(!(system isa ReactionSystem) for system in MT.get_systems(rn))
481481
error("Tries to write a ReactionSystem to file which have non-ReactionSystem subs-systems. This is currently not possible.")
482482
end
@@ -489,8 +489,8 @@ function push_systems_field(file_text::String, rn::ReactionSystem, annotate::Boo
489489
return (file_text * write_string, true)
490490
end
491491

492-
# Checks if the reaction system have any systems.
493-
function has_systems(rn::ReactionSystem)
492+
# Checks if the reaction system has any systems.
493+
function seri_has_systems(rn::ReactionSystem)
494494
return !isempty(MT.get_systems(rn))
495495
end
496496

@@ -519,13 +519,13 @@ function get_systems_annotation(rn::ReactionSystem)
519519
end
520520

521521
# Combines the 3 systems-related functions in a constant tuple.
522-
SYSTEMS_FS = (has_systems, get_systems_string, get_systems_annotation)
522+
SYSTEMS_FS = (seri_has_systems, get_systems_string, get_systems_annotation)
523523

524524

525525
### Handles Connection Types ###
526526

527-
# Checks if the reaction system have any connection types.
528-
function has_connection_type(rn::ReactionSystem)
527+
# Checks if the reaction system has any connection types.
528+
function seri_has_connection_type(rn::ReactionSystem)
529529
return false
530530
end
531531

@@ -540,4 +540,4 @@ function get_connection_type_annotation(rn::ReactionSystem)
540540
end
541541

542542
# Combines the 3 connection types-related functions in a constant tuple.
543-
CONNECTION_TYPE_FS = (has_connection_type, get_connection_type_string, get_connection_type_annotation)
543+
CONNECTION_TYPE_FS = (seri_has_connection_type, get_connection_type_string, get_connection_type_annotation)

src/reactionsystem_serialisation/serialise_reactionsystem.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ function get_full_system_string(rn::ReactionSystem, annotate::Bool, top_level::B
5353
file_text = ""
5454

5555
# Goes through each type of system component, potentially adding it to the string.
56-
# Species, variables, and parameters must be handled differently in case there is default-values
56+
# Species, variables, and parameters must be handled differently in case there are default values
5757
# dependencies between them.
58-
# Systems uses custom `push_field` function as these requires the annotation `Bool`to be passed
58+
# Systems use custom `push_field` function as these require the annotation `Bool`to be passed
5959
# to the function that creates the next sub-system declarations.
6060
file_text, _ = push_field(file_text, rn, annotate, top_level, IV_FS)
6161
file_text, has_sivs = push_field(file_text, rn, annotate, top_level, SIVS_FS)
@@ -68,8 +68,8 @@ function get_full_system_string(rn::ReactionSystem, annotate::Bool, top_level::B
6868
file_text, has_systems = push_systems_field(file_text, rn, annotate, top_level)
6969
file_text, has_connection_type = push_field(file_text, rn, annotate, top_level, CONNECTION_TYPE_FS)
7070

71-
# Finalises the system. Creates the final `ReactionSystem` call.
72-
# Enclose everything ing a `let ... end` block.
71+
# Finalise the system. Creates the final `ReactionSystem` call.
72+
# Enclose everything in a `let ... end` block.
7373
rs_creation_code = make_reaction_system_call(rn, annotate, top_level, has_sivs, has_species,
7474
has_variables, has_parameters, has_reactions,
7575
has_equations, has_observed, has_continuous_events,
@@ -82,7 +82,7 @@ function get_full_system_string(rn::ReactionSystem, annotate::Bool, top_level::B
8282
end
8383

8484
# Creates a ReactionSystem call for creating the model. Adds all the correct inputs to it. The input
85-
# `has_` `Bool`s described which inputs are used. If model is `complete`, this is handled here.
85+
# `has_` `Bool`s described which inputs are used. If the model is `complete`, this is handled here.
8686
function make_reaction_system_call(rs::ReactionSystem, annotate, top_level, has_sivs, has_species,
8787
has_variables, has_parameters, has_reactions, has_equations,
8888
has_observed, has_continuous_events, has_discrete_events,

0 commit comments

Comments
 (0)