Skip to content

Commit 2e66d1d

Browse files
committed
update
1 parent 0929f7f commit 2e66d1d

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/Catalyst.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ using JumpProcesses: JumpProcesses,
1313
# ModelingToolkit imports and convenience functions we use
1414
using ModelingToolkit
1515
const MT = ModelingToolkit
16-
using DynamicQuantities# , Unitful # Having Unitful here as well currently gives an error.
16+
using DynamicQuantities#, Unitful # Having Unitful here as well currently gives an error.
17+
18+
1719
@reexport using ModelingToolkit
1820
using Symbolics
1921

src/networkapi.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,12 +1624,21 @@ function validate(rs::ReactionSystem, info::String = "")
16241624
rxunits *= get_unit(sub)^rx.substoich[i]
16251625
end
16261626

1627-
if !isequal(rxunits, rateunits)
1628-
validated = false
1629-
@warn(string("Reaction rate laws are expected to have units of ", rateunits,
1630-
" however, ", rx, " has units of ", rxunits, "."))
1627+
# Checks that the reaction's combined units is correct, if not, throws a warning.
1628+
# Needs additional checks because for cases: (1.0^n) and (1.0^n1)*(1.0^n2).
1629+
# These are not considered (be default) considered equal to `1.0` for unitless reactions.
1630+
isequal(rxunits, rateunits) && continue
1631+
if istree(rxunits)
1632+
unitless_exp(rxunits) && continue
1633+
(operation(rxunits) == *) && all(unitless_exp(arg) for arg in arguments(rxunits)) && continue
16311634
end
1635+
validated = false
1636+
@warn(string("Reaction rate laws are expected to have units of ", rateunits, " however, ",
1637+
rx, " has units of ", rxunits, "."))
16321638
end
16331639

16341640
validated
16351641
end
1642+
1643+
# Checks if a unit consist of exponents with base 1 (and is this unitless).
1644+
unitless_exp(u) = istree(u) && (operation(u) == ^) && (arguments(u)[1] == 1)

test/miscellaneous_tests/units.jl

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ let
1717
rxs = [Reaction(k1, nothing, [A]),
1818
Reaction(k2, [A], [B]),
1919
Reaction(k3, [A, B], [B], [1, 1], [2])]
20-
@test_nowarn @named rs = ReactionSystem(rxs, t, [A, B, C], [k1, k2, k3])
20+
@test_nowarn ReactionSystem(rxs, t, [A, B, C], [k1, k2, k3]; name = :rs)
21+
@named rs = ReactionSystem(rxs, t, [A, B, C], [k1, k2, k3])
2122
rs = complete(rs)
2223

2324
# Test that all reactions have the correct unit.
@@ -149,46 +150,43 @@ end
149150
# Checks that units works for various non-trivial systems.
150151
let
151152
# Parametric rates (no units).
152-
@test_broken false # This yields a warning (and it shouldn't).
153-
rn = @reaction_network begin
153+
@test_nowarn @reaction_network begin
154154
k1, 2X1 --> Z1
155155
k2, n2*X2 --> m2*Z2
156156
k3, n3*X3 + m3*Y3--> Z3
157157
end
158158

159159
# Parametric rates with units is not possible, since the unit depends on the parameter values.
160160

161-
# Non-constant rates (no units).
162-
@test_nowarn rn = @reaction_network begin
161+
# Non-trivial rates (no units).
162+
@test_nowarn @reaction_network begin
163163
k1*X1, 2X1 --> Z1
164164
mm(X2, v2, K2), X2 --> Z2
165165
hill(X3, v3, K3, n3), X3 + Y3--> Z3
166166
end
167167

168-
# Non-constant rates (units).
169-
@test_broken false # The below expression generates an error on compile. Really no idea why, have
170-
# managed to make a really small example of it, but not figured it out yet.
171-
# rn = @reaction_network begin
172-
# @ivs t [unit=u"s"]
173-
# @species begin
174-
# X1(t), [unit=u"m"]
175-
# Z1(t), [unit=u"m"]
176-
# X2(t), [unit=u"m"]
177-
# Z2(t), [unit=u"m"]
178-
# X3(t), [unit=u"m"]
179-
# Y3(t), [unit=u"m"]
180-
# Z3(t), [unit=u"m"]
181-
# end
182-
# @parameters begin
183-
# k1, [unit=u"m^(-4)/s"]
184-
# v2, [unit=u"m^(-4)/s"]
185-
# K2, [unit=u"m"]
186-
# v3, [unit=u"m^(-3)/s"]
187-
# K3, [unit=u"m"]
188-
# n3
189-
# end
190-
# k1*X1, 2X1 --> Z1
191-
# mm(X2, v, K), 3X2 --> Z2
192-
# hill(X3, v3, K3, n3), X3 + Y3--> Z3
193-
# end
194-
end
168+
# Non-trivial rates (units).
169+
@test_nowarn @reaction_network begin
170+
@ivs t [unit=u"s"]
171+
@species begin
172+
X1(t), [unit=u"m"]
173+
Z1(t), [unit=u"m"]
174+
X2(t), [unit=u"m"]
175+
Z2(t), [unit=u"m"]
176+
X3(t), [unit=u"m"]
177+
Y3(t), [unit=u"m"]
178+
Z3(t), [unit=u"m"]
179+
end
180+
@parameters begin
181+
k1, [unit=u"m^(-2)/s"]
182+
v2, [unit=u"m^(-2)/s"]
183+
K2, [unit=u"m"]
184+
v3, [unit=u"m^(-1)/s"]
185+
K3, [unit=u"m"]
186+
n3
187+
end
188+
k1*X1, 2X1 --> Z1
189+
mm(X2, v2, K2), 3X2 --> Z2
190+
hill(X3, v3, K3, n3), X3 + Y3--> Z3
191+
end
192+
end

0 commit comments

Comments
 (0)