Skip to content

Commit 0e928f7

Browse files
committed
init
1 parent a649acc commit 0e928f7

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

docs/src/model_creation/chemistry_related_functionality.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,23 @@ balanced_reaction = balance_reaction(unbalanced_reaction)[1]
136136
Reactions declared as a part of a `ReactionSystem` (e.g. using the DSL) can be retrieved for balancing using the [`reactions`](@ref) function. Please note that balancing these will not mutate the `ReactionSystem`, but a new reaction system will need to be created using the balanced reactions.
137137

138138
!!! note
139-
Reaction balancing is currently not supported for reactions involving compounds of compounds.
139+
Reaction balancing is currently not supported for reactions involving compounds of compounds.
140+
141+
### Balancing full systems
142+
It is possible to balance all the reactions of a reaction system simultaneously using the `balance_system` function. Here, the output is a new system, where all reactions are balanced. E.g. We can use it to balance this system of Methane formation/combustion:
143+
```@example chem2
144+
rs = @reaction_network begin
145+
@species C(t) O(t) H(t)
146+
@compounds begin
147+
H2(t) = 2H
148+
CH4(t) = C + 4H
149+
O2(t) = 2O
150+
CO2(t) = C + 2O
151+
H2O(t) = 2H + O
152+
end
153+
1.0, C + H2 --> CH4
154+
2.0, CH4 + O2 --> CO2 + H2O
155+
end
156+
rs_balanced = balance_system(rs)
157+
```
158+
Except for the modified reaction stoichiometries, the new system is identical to the previous one.

src/chemistry_functionality.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,16 @@ function create_matrix(reaction::Catalyst.Reaction)
346346

347347
return A
348348
end
349+
350+
"""
351+
balance_system(rs::ReactionSystem)
352+
353+
From a system, creates a new system where each reaction is a balanced version of the corresponding
354+
reaction of the original system. For more information, consider the `balance_reaction` function
355+
(which is internally applied to each system reaction).
356+
"""
357+
function balance_system(rs::ReactionSystem)
358+
@set! rs.eqs = [(eq isa Reaction) ? balance_reaction(eq) : eq for eq in get_eqs(rs)]
359+
@set! rs.rxs = [balance_reaction(rx) for rx in get_rxs(rs)]
360+
return rs
361+
end

test/miscellaneous_tests/reaction_balancing.jl

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,5 +425,58 @@ let
425425
@test isequal([rn.CO2, rn.H2O], brxs.substrates)
426426
@test isequal([rn.C6H12O6, rn.O2], brxs.products)
427427
@test isequal([6, 6], brxs.substoich)
428-
@test isequal([1, 6], brxs.prodstoich)
428+
@test isequal([1, 6], brxs.prodstoich)
429+
end
430+
431+
432+
### Reaction System Balancing ###
433+
434+
# Simple example with multiple reactions.
435+
let
436+
# Creates a reaction system and tis balanced version.
437+
rs = @reaction_network begin
438+
@species C(t) O(t) H(t)
439+
@compounds begin
440+
H2(t) = 2H
441+
CH4(t) = C + 4H
442+
O2(t) = 2O
443+
CO2(t) = C + 2O
444+
H2O(t) = 2H + O
445+
end
446+
1.0, C + H2 --> CH4
447+
2.0, CH4 + O2 --> CO2 + H2O
448+
end
449+
rs_balanced = balance_system(rs)
450+
451+
# Checks that the system is correctly balanced (while not taking order of reactions or of
452+
# substrates/products for granted).
453+
rx1 = filter(rx -> isequal(rx.rate, 1.0), reactions(rs_balanced))[1]
454+
rx2 = filter(rx -> isequal(rx.rate, 2.0), reactions(rs_balanced))[1]
455+
@test issetequal(rx1.substoich, [1,2])
456+
@test issetequal(rx1.prodstoich, [1])
457+
@test issetequal(rx2.substoich, [1,2])
458+
@test issetequal(rx2.prodstoich, [1,2])
459+
end
460+
461+
# Checks for system with non-reaction equations.
462+
let
463+
# Creates a reaction system and tis balanced version.
464+
rs = @reaction_network begin
465+
@species O(t) H(t)
466+
@compounds begin
467+
H2(t) = 2H
468+
O2(t) = 2O
469+
H2O(t) = 2H + O
470+
end
471+
@equation begin
472+
D(V) ~ 1 - V
473+
end
474+
1.0, H2 + O2 --> H2O
475+
end
476+
rs_balanced = balance_system(rs)
477+
478+
# Checks that the system is correctly balanced (while not taking order of reactions or of
479+
# substrates/products for granted).
480+
@test issetequal(reactions(rs_balanced)[1].substoich, [2,1])
481+
@test issetequal(reactions(rs_balanced)[1].prodstoich, [2])
429482
end

0 commit comments

Comments
 (0)