1
+ # ## Fetch Packages ###
2
+
3
+ # Fetch packages.
4
+ using Catalyst, NonlinearSolve, OrdinaryDiffEq, SteadyStateDiffEq
5
+ using Random, Test
6
+
7
+ # Sets rnd number.
8
+ using StableRNGs
9
+ rng = StableRNG (12345 )
10
+
11
+ # ## Run Tests ###
12
+
13
+ # Creates a simple problem and find steady states just different approaches. Compares to analytic solution.
14
+ let
15
+ # Model with easily computable steady states.
16
+ steady_state_network_1 = @reaction_network begin
17
+ (k1, k2), ∅ ↔ X1
18
+ (k3, k4), ∅ ↔ 3 X2
19
+ (k5, k6), ∅ ↔ X3 + X4
20
+ end
21
+
22
+ # Creates NonlinearProblem.
23
+ u0 = rand (rng, length (states (steady_state_network_1)))
24
+ p = rand (rng, length (parameters (steady_state_network_1)))
25
+ nl_prob = NonlinearProblem (steady_state_network_1, u0, p)
26
+
27
+ # Solves it using standard algorithm and simulation based algorithm.
28
+ sol1 = solve (nl_prob; abstol= 1e-15 , reltol= 1e-15 ). u
29
+ sol2 = solve (nl_prob, DynamicSS (Tsit5 (); abstol= 1e-15 , reltol= 1e-15 ); abstol= 1e-15 , reltol= 1e-15 ). u
30
+
31
+ # Tests solutions are correct.
32
+ @test abs .(sol1[1 ] - p[1 ] / p[2 ]) < 1e-8
33
+ @test abs .(sol1[2 ]^ 3 / factorial (3 ) - p[3 ] / p[4 ]) < 1e-8
34
+ @test abs .(sol1[3 ] * sol1[4 ] - p[5 ] / p[6 ]) < 1e-8
35
+ @test sol1 ≈ sol2
36
+ end
37
+
38
+ # Creates a system with multiple steady states.
39
+ # Checks that corresponding ODEFunction return 0.0 in both cases. Checks for manually computed function as well.
40
+ # Checks with SYmbol `u0` and Symbolic `p`.
41
+ let
42
+ # Creates steady state network, unpack the parameter values.
43
+ steady_state_network_2 = @reaction_network begin
44
+ v / 10 + hill (X, v, K, n), ∅ → X
45
+ d, X → ∅
46
+ end
47
+ @unpack v, K, n, d = steady_state_network_2
48
+
49
+ # Creates NonlinearProblem.
50
+ u0 = [:X => 1.0 ]
51
+ p = [v => 1.0 + rand (rng), K => 0.8 , n => 3 , d => 1.0 ]
52
+ nl_prob = NonlinearProblem (steady_state_network_2, u0, p)
53
+
54
+ # Solves it using standard algorithm and simulation based algorithm.
55
+ sol1 = solve (nl_prob; abstol= 1e-18 , reltol= 1e-18 ). u
56
+ sol2 = solve (nl_prob, DynamicSS (Tsit5 (); abstol= 1e-18 , reltol= 1e-18 ); abstol= 1e-18 , reltol= 1e-18 ). u
57
+
58
+ # Computes NonlinearFunction (manually and automatically).
59
+ nfunc = NonlinearFunction (convert (NonlinearSystem, steady_state_network_2))
60
+ function nf_manual (u,p)
61
+ X = u[1 ]
62
+ v, K, n, d = p
63
+ return v/ 10 + v * X^ n/ (X^ n + K^ n) - d* X
64
+ end
65
+
66
+ # Tests solutions are correct.
67
+ @test nfunc (sol1, last .(p))[1 ] ≈ 0.0
68
+ @test nfunc (sol2, last .(p))[1 ] ≈ 0.0
69
+ @test nf_manual (sol1, last .(p)) ≈ 0.0
70
+ @test nf_manual (sol2, last .(p)) ≈ 0.0
71
+ end
72
+
73
+ # Checks for system with conservation laws.
74
+ # Checks using interfacing with output solution.
75
+ let
76
+ # Creates steady state network, unpack the parameter values.
77
+ steady_state_network_3 = complete (@reaction_network begin
78
+ (p,d), 0 <--> X
79
+ (k1, k2), 2 Y <--> Y2
80
+ (k3, k4), X + Y2 <--> XY2
81
+ end )
82
+ @unpack X, Y, Y2, XY2 = steady_state_network_3
83
+
84
+ # Creates NonlinearProblem.
85
+ u0 = [steady_state_network_3. X => rand (), steady_state_network_3. Y => rand () + 1.0 , steady_state_network_3. Y2 => rand () + 3.0 , steady_state_network_3. XY2 => 0.0 ]
86
+ p = [:p => rand ()+ 1.0 , :d => 0.5 , :k1 => 1.0 , :k2 => 2.0 , :k3 => 3.0 , :k4 => 4.0 ]
87
+ nl_prob_1 = NonlinearProblem (steady_state_network_3, u0, p; remove_conserved = true )
88
+ nl_prob_2 = NonlinearProblem (steady_state_network_3, u0, p)
89
+
90
+ # Solves it using standard algorithm and simulation based algorithm.
91
+ sol1 = solve (nl_prob_1; abstol= 1e-18 , reltol= 1e-18 )
92
+ sol2 = solve (nl_prob_2, DynamicSS (Tsit5 (); abstol= 1e-18 , reltol= 1e-18 ); abstol= 1e-18 , reltol= 1e-18 )
93
+
94
+ # Checks output using NonlinearFunction.
95
+ nfunc = NonlinearFunction (convert (NonlinearSystem, steady_state_network_3))
96
+ @test all (nfunc ([sol1[X], sol1[Y], sol1[Y2], sol1[XY2]], last .(p)) .≈ 0.0 )
97
+ @test all (nfunc ([sol2[X], sol2[Y], sol2[Y2], sol2[XY2]], last .(p)) .≈ 0.0 )
98
+ end
0 commit comments