CoreModelCoupled.jl 5.24 KB
Newer Older
Sylvain Arreckx's avatar
Sylvain Arreckx committed
1
@testset "Coupling constraints" begin
2
    cp = convert(CoreModelCoupled, test_LP())
3
    @test size(cp.lm.S) == (4, 3)
4
    @test size(stoichiometry(convert(CoreModel, cp))) == (4, 3)
5
6
    new_cp = add_coupling_constraints(cp, stoichiometry(cp)[end, :], -1.0, 1.0)
    @test n_coupling_constraints(cp) + 1 == n_coupling_constraints(new_cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
7

8
9
10
    new_cp =
        add_coupling_constraints(cp, stoichiometry(cp)[1:2, :], [-1.0; -1.0], [1.0; 1.0])
    @test n_coupling_constraints(cp) + 2 == n_coupling_constraints(new_cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
11

12
13
14
15
16
    n_c = n_coupling_constraints(cp)
    add_coupling_constraints!(cp, stoichiometry(cp)[end, :], -1.0, 1.0)
    @test n_c + 1 == n_coupling_constraints(cp)
    add_coupling_constraints!(cp, stoichiometry(cp)[1:2, :], [-1.0; -1.0], [1.0; 1.0])
    @test n_c + 3 == n_coupling_constraints(cp)
Sylvain Arreckx's avatar
Sylvain Arreckx committed
17

18
19
20
21
22
23
    n_c = n_coupling_constraints(cp)
    remove_coupling_constraints!(cp, 1)
    @test n_c - 1 == n_coupling_constraints(cp)
    remove_coupling_constraints!(cp, [1, 2])
    @test n_c - 3 == n_coupling_constraints(cp)
    @test n_coupling_constraints(cp) == 0
Sylvain Arreckx's avatar
Sylvain Arreckx committed
24
25

    cp = test_coupledLP()
26
27
28
29
30
31
32
    n_c = n_coupling_constraints(cp)
    new_cp = remove_coupling_constraints(cp, 1)
    @test size(coupling(cp)) == (n_c, n_reactions(cp))
    @test n_c - 1 == n_coupling_constraints(new_cp)
    @test n_coupling_constraints(cp) == n_c
    new_cp = remove_coupling_constraints(cp, [1, 2])
    @test n_c - 2 == n_coupling_constraints(new_cp)
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
33
    new_cp = remove_coupling_constraints(cp, Vector(1:n_coupling_constraints(cp)))
34
35
    @test n_coupling_constraints(new_cp) == 0
    @test n_coupling_constraints(cp) == n_c
Sylvain Arreckx's avatar
Sylvain Arreckx committed
36
37

    cp = test_coupledLP()
38
39
    change_coupling_bounds!(cp, [3, 1], cl = [-10.0, -20], cu = [10.0, 20])
    cl, cu = coupling_bounds(cp)
40
41
    @test cl[[1, 3]] == [-20, -10]
    @test cu[[1, 3]] == [20, 10]
42
43
    change_coupling_bounds!(cp, [1000, 1001], cl = [-50.0, -60.0])
    cl, cu = coupling_bounds(cp)
44
    @test cl[[1000, 1001]] == [-50.0, -60.0]
Sylvain Arreckx's avatar
Sylvain Arreckx committed
45
end
46
47
48
49
50
51
52
53
54
55
56

@testset "Add reactions" begin
    cp = convert(CoreModelCoupled, test_LP())
    cp = add_coupling_constraints(cp, stoichiometry(cp)[end, :], -1.0, 1.0)

    new_cp = add_reactions(cp, 2.0 * ones(4), 3 .* ones(4), 2.0, -1.0, 1.0)
    @test new_cp isa CoreModelCoupled
    @test cp.C == new_cp.C[:, 1:end-1]
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
57
58
59
60
61
62
63
64
65
66
    new_cp = add_reactions(
        cp,
        2.0 * ones(4),
        3 .* ones(4),
        2.0,
        -1.0,
        1.0,
        "r4",
        ["m$i" for i = 1:4],
    )
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    @test cp.C == new_cp.C[:, 1:end-1]
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu

    new_cp = add_reactions(
        cp,
        2.0 * ones(4, 10),
        3 .* ones(4),
        2 .* ones(10),
        -ones(10),
        ones(10),
    )
    @test cp.C == new_cp.C[:, 1:end-10]
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu

    new_cp = add_reactions(
        cp,
        2.0 * ones(4, 10),
        3 .* ones(4),
        2 .* ones(10),
        -ones(10),
        ones(10),
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
90
91
        ["r$i" for i = 1:10],
        ["m$i" for i = 1:4],
92
93
94
95
96
    )
    @test cp.C == new_cp.C[:, 1:end-7] # 3 reactions were already present
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
97
98
    new_cp =
        add_reactions(cp, 2.0 * sprand(4000, 0.5), 3 .* sprand(4000, 0.5), 2.0, -1.0, 1.0)
99
100
101
102
103
    @test cp.C == new_cp.C[:, 1:end-1]
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu

    cm = CoreModel(
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
104
105
106
107
108
109
110
111
        2.0 * ones(4, 10),
        3 .* ones(4),
        2 .* ones(10),
        -ones(10),
        ones(10),
        ["r$i" for i = 1:10],
        ["m$i" for i = 1:4],
    )
112
113
114
115
116
    new_cp = add_reactions(cp, cm)
    @test cp.C == new_cp.C[:, 1:end-7] # 3 reactions were already present
    @test cp.cl == new_cp.cl
    @test cp.cu == new_cp.cu
end
117
118
119

@testset "Remove reactions" begin
    cp = convert(CoreModelCoupled, test_LP())
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
120
    cp = add_coupling_constraints(cp, 1.0 .* collect(1:n_reactions(cp)), -1.0, 1.0)
121

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
122
    new_cp = remove_reactions(cp, [3; 2])
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    @test new_cp isa CoreModelCoupled
    @test new_cp.C[:] == cp.C[:, 1] # because cp.C[:, 1] comes out as a Vector
    @test new_cp.cl == cp.cl
    @test new_cp.cu == cp.cu

    new_cp = remove_reactions(cp, 2)
    @test new_cp.C == cp.C[:, [1; 3]]
    @test new_cp.cl == cp.cl
    @test new_cp.cu == cp.cu

    new_cp = remove_reactions(cp, "r1")
    @test new_cp.C == cp.C[:, 2:3]
    @test new_cp.cl == cp.cl
    @test new_cp.cu == cp.cu

    new_cp = remove_reactions(cp, ["r1"; "r3"])
    @test new_cp.C[:] == cp.C[:, 2]
    @test new_cp.cl == cp.cl
    @test new_cp.cu == cp.cu

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
143
    new_cp = remove_reactions(cp, [1; 4])
144
145
146
147
148
    @test new_cp.C == cp.C[:, 2:3]

    new_cp = remove_reactions(cp, "r4")
    @test new_cp.C == cp.C

Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
149
    new_cp = remove_reactions(cp, [1; 1; 2])
150
151
    @test new_cp.C[:] == cp.C[:, 3]
end
152

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
@testset "Change bounds" begin
    cp = convert(CoreModelCoupled, test_LP())
    change_bounds!(cp, [3; 1], xl = [-10.0; -20], xu = [10.0; 20])
    @test cp isa CoreModelCoupled
    @test cp.lm.xl == [-20; 1; -10]
    @test cp.lm.xu == [20; 1; 10]
    change_bounds!(
        cp,
        ["gibberish1"; "r3"; "r1"; "gibberish2"],
        xl = [0; -30.0; -40; 0],
        xu = [0; 30.0; 40; 0],
    )
    @test cp.lm.xl == [-40; 1; -30]
    @test cp.lm.xu == [40; 1; 30]
    change_bounds!(cp, ["r1"; "r3"], xl = [-50.0; -60])
    @test cp.lm.xl == [-50; 1; -60]
end