82
82
83
83
# Creates model by extending a `ReactionSystem` with a ODESystem.
84
84
rn_extended = @network_component begin
85
- ($ k1* $ A, $ k2* $ B), X1 <--> X2
85
+ ($ k1* $ A, $ k2* $ B), X1 <--> X2
86
86
end
87
87
eqs_extended = [
88
88
D (A) ~ X1 + a - A
98
98
D (A) ~ X1 + a - A
99
99
D (B) ~ X2 + b - B
100
100
end
101
- (k1* A, k2* B), X1 <--> X2
101
+ (k1* A, k2* B), X1 <--> X2
102
102
end
103
103
104
104
# Checks that models are equivalent and contain the correct stuff.
126
126
# Tests coupled CRN/algebraic equation. Checks that known steady state is reached using ODE solve.
127
127
# Check that steady state can be found using NonlinearSolve and SteadyStateDiffEq.
128
128
# Checks that errors are given if `structural_simplify = true` argument is not given.
129
- let
129
+ let
130
130
# Creates a simple coupled model with an algebraic equation.
131
131
@parameters p d a b
132
132
@species X (t)
216
216
# Checks that coupled systems contain the correct species, variables, and parameters.
217
217
# Checks that species, variables, and parameters are inferred correctly from equations.
218
218
# Checks that non-default iv is inferred correctly from reactions/equations.
219
- let
219
+ let
220
220
# Create coupled model.
221
221
@variables τ A (τ) B (τ)
222
222
@species X (τ) X2 (τ)
@@ -431,22 +431,22 @@ end
431
431
# Checks the algebraic equation holds.
432
432
sprob = SDEProblem (coupled_rs, u0, tspan, ps; structural_simplify = true )
433
433
ssol = solve (sprob, ImplicitEM ())
434
- @test 2 .+ ps[:k1 ] * ssol[:A ] == 3 .+ ps[:k2 ] * ssol[:X ]
434
+ @test 2 .+ ps[:k1 ] * ssol[:A ] == 3 .+ ps[:k2 ] * ssol[:X ]
435
435
end
436
436
437
437
438
438
# ## Coupled NonlinearSystems Tests ###
439
439
440
440
# Checks that systems with weird differential equations yield errors.
441
- let
441
+ let
442
442
# This one is normal, and should not yield an error.
443
443
begin
444
444
rs = @reaction_network begin
445
445
@equations D (V) ~ 1.0 - V
446
446
end
447
447
@test_nowarn convert (NonlinearSystem, rs)
448
448
end
449
-
449
+
450
450
# Higher-order differential on the lhs, should yield an error.
451
451
begin
452
452
rs = @reaction_network begin
457
457
end
458
458
@test_throws Exception convert (NonlinearSystem, rs)
459
459
end
460
-
460
+
461
461
# Differential on the rhs, should yield an error.
462
462
begin
463
463
rs = @reaction_network begin
467
467
end
468
468
@test_throws Exception convert (NonlinearSystem, rs)
469
469
end
470
-
470
+
471
471
# Non-differential term on the lhs, should yield an error.
472
472
begin
473
473
rs = @reaction_network begin
485
485
486
486
# Tests that coupled CRN/DAEs with higher order differentials can be created.
487
487
# Tests that these can be solved using ODEs, nonlinear solving, and steady state simulations.
488
- let
488
+ let
489
489
# Create coupled model.
490
490
@species X (t)
491
491
@variables A (t) B (t)
494
494
Reaction (p, nothing , [X]),
495
495
Reaction (d, [X], nothing ),
496
496
D (D (A)) + 2 ω* D (A) + (ω^ 2 )* A ~ 0 ,
497
- A + k* (B + D (A)) ~ X
497
+ A + k* (B + D (A)) ~ X
498
498
]
499
499
@named coupled_rs = ReactionSystem (eqs, t)
500
500
coupled_rs = complete (coupled_rs)
521
521
end
522
522
523
523
# Checks that the steady state can be found by solving a nonlinear problem.
524
- # Here `B => 0.1` has to be provided as well (and it shouldn't for the 2nd order ODE), hence the
524
+ # Here `B => 0.1` has to be provided as well (and it shouldn't for the 2nd order ODE), hence the
525
525
# separate `u0` declaration.
526
526
u0 = [X => 1.0 , A => 2.0 , D (A) => 1.0 , B => 0.1 ]
527
527
nlprob = NonlinearProblem (coupled_rs, u0, ps; structural_simplify = true , all_differentials_permitted = true )
536
536
# differentials, not necessarily on the same side).
537
537
# Checks with non-default iv, and parameters/initial conditions given using Symbols.
538
538
# Checks with default value for algebraic variable.
539
- let
539
+ let
540
540
# Prepares stuff common to both simulations.
541
541
@parameters i r m1 m2 h_max
542
542
u0 = [:S => 999.0 , :I => 1.0 , :R => 0.0 , :M => 1000.0 ]
589
589
# ## DSL Tests ###
590
590
591
591
# Check that a coupled CRN/DAE created programmatically and via the DSL are identical.
592
- # Checks where variables are implied from differential equations, and with variables/parameter
592
+ # Checks where variables are implied from differential equations, and with variables/parameter
593
593
# default values, types, and metadata.
594
594
# Checks that generated system contents are correct, and ODE simulations are identical.
595
595
let
685
685
end
686
686
687
687
# Checks that lhs variable is correctly inferred from differential equations.
688
- let
688
+ let
689
689
# Checks for system with a differential equation and an algebraic equation.
690
690
# Here, `H` is defined using `@variables`, but M should be inferred.
691
691
rs_1 = @reaction_network begin
700
700
issetequal (species (rs_1), [rs_1. S, rs_1. I, rs_1. R])
701
701
issetequal (unknowns (rs_1)[4 : 5 ], [rs_1. H, rs_1. M])
702
702
703
- # Checks for system with two differential equations, and which do not use `@variables`,
703
+ # Checks for system with two differential equations, and which do not use `@variables`,
704
704
rs_2 = @reaction_network coupled_rs begin
705
705
@equations begin
706
706
D (V) ~ X/ (1 + X) - V
@@ -735,11 +735,11 @@ let
735
735
@test getdescription (rs. V) == " A variable"
736
736
end
737
737
738
- # Checks that equations can be formatted in various ways. Tries e.g. isolating a single number on
739
- # either side of the equality.
738
+ # Checks that equations can be formatted in various ways. Tries e.g. isolating a single number on
739
+ # either side of the equality.
740
740
# Checks that various weird function can be used within equations.
741
741
# Checks that special symbols, like π and t can be used within equations.
742
- let
742
+ let
743
743
# Declares models with a single equation, formatted in various ways.
744
744
rs_1 = @reaction_network rs begin
745
745
@parameters p q
@@ -751,13 +751,13 @@ let
751
751
@parameters p q
752
752
@species X (t)
753
753
@variables A (t) B (t)
754
- @equations X^ 2 + log (A+ X) + sqrt (B) - sin (p + X + π)/ exp (A/ (1 + t)) - q ~ 1
754
+ @equations X^ 2 + log (A+ X) + sqrt (B) - sin (p + X + π)/ exp (A/ (1 + t)) - q ~ 1
755
755
end
756
756
rs_3 = @reaction_network rs begin
757
757
@parameters p q
758
758
@species X (t)
759
759
@variables A (t) B (t)
760
- @equations X^ 2 + log (A+ X) + sqrt (B) - sin (p + X + π)/ exp (A/ (1 + t)) - 1 - q ~ 0
760
+ @equations X^ 2 + log (A+ X) + sqrt (B) - sin (p + X + π)/ exp (A/ (1 + t)) - 1 - q ~ 0
761
761
end
762
762
rs_4 = @reaction_network rs begin
763
763
@parameters p q
@@ -795,17 +795,17 @@ end
795
795
796
796
# Checks that the default differential (`D`) uses a declared, non-default, independent variable.
797
797
# Check that inferred variables depends on declared time independent variables.
798
- let
798
+ let
799
799
# Declares model.
800
800
rs = @reaction_network begin
801
801
@ivs τ
802
802
@equations D (V) ~ - 1.0
803
803
end
804
-
804
+
805
805
# Checks that the default differential uses τ iv.
806
806
Ds = Differential (ModelingToolkit. get_iv (rs))
807
807
@test isequal (operation (equations (rs)[1 ]. lhs), Ds)
808
-
808
+
809
809
# Checks that the inferred variable depends on τ iv.
810
810
@variables V ($ (ModelingToolkit. get_iv (rs)))
811
811
@test isequal (V, rs. V)
@@ -851,27 +851,27 @@ let
851
851
end
852
852
853
853
# Checks that various misformatted declarations yield errors.
854
- let
854
+ let
855
855
# Symbol in equation not appearing elsewhere (1).
856
856
@test_throws Exception @eval @reaction_network begin
857
857
@equations D (V) ~ - X
858
858
end
859
-
859
+
860
860
# Symbol in equation not appearing elsewhere (2).
861
861
@test_throws Exception @eval @reaction_network begin
862
862
@equations 1 + log (x) ~ 2 X
863
863
end
864
-
864
+
865
865
# Attempting to infer differential variable not isolated on lhs (1).
866
866
@test_throws Exception @eval @reaction_network begin
867
867
@equations D (V) + 1 ~ 0
868
868
end
869
-
869
+
870
870
# Attempting to infer differential variable not isolated on lhs (2).
871
871
@test_throws Exception @eval @reaction_network begin
872
872
@equations - 1.0 ~ D (V)
873
873
end
874
-
874
+
875
875
# Attempting to infer differential operator not isolated on lhs (1).
876
876
@test_throws Exception @eval @reaction_network begin
877
877
@variables V (t)
883
883
@differentials Δ = Differential (t)
884
884
@equations Δ (V) ~ - 1 ,0
885
885
end
886
-
886
+
887
887
# Attempting to create a new differential from an unknown iv.
888
888
@test_throws Exception @eval @reaction_network begin
889
889
@differentials D = Differential (τ)
@@ -894,13 +894,13 @@ let
894
894
@variables D
895
895
@differentials d ~ D
896
896
end
897
-
897
+
898
898
# Several equations without `begin ... end` block.
899
899
@test_throws Exception @eval @reaction_network begin
900
900
@variables V (t)
901
901
@equations D (V) + 1 ~ - 1.0
902
902
end
903
-
903
+
904
904
# Undeclared differential.
905
905
@test_throws Exception @eval @reaction_network begin
906
906
@species V
@@ -918,18 +918,18 @@ let
918
918
@equations begin
919
919
δ (n) ~ - n
920
920
Δ (N) ~ - N
921
- end
921
+ end
922
922
end
923
923
end
924
924
925
925
926
926
# ## Error Tests ###
927
927
928
928
# Checks that various erroneous coupled system declarations yield errors.
929
- let
929
+ let
930
930
@parameters p1 p2
931
931
@variables τ U1 (τ) V1 (t)
932
- @species R1 (τ) R2 (τ) S1 (t) S2 (t)
932
+ @species R1 (τ) R2 (τ) S1 (t) S2 (t)
933
933
E = Differential (τ)
934
934
935
935
# Variables as reaction reactants.
942
942
Reaction (p1, [R1], [R2])
943
943
], t; name = :rs )
944
944
945
- # Equation with variable using non-declared independent variable.
945
+ # Equation with variable using non-declared independent variable.
946
946
@test_throws Exception ReactionSystem ([
947
947
Reaction (p1, [S1], [S2]),
948
948
U1 ~ S1 + p2
@@ -956,10 +956,10 @@ let
956
956
end
957
957
958
958
# Checks that various attempts to create `ODEProblem`s from faulty systems generate errors.
959
- let
959
+ let
960
960
@parameters p1 p2
961
961
@variables V1 (t)
962
- @species S1 (t) S2 (t)
962
+ @species S1 (t) S2 (t)
963
963
964
964
# Coupled system with additional differential equation for species.
965
965
eqs = [
0 commit comments