-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_solver.m
278 lines (264 loc) · 12 KB
/
test_solver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
classdef test_solver < matlab.perftest.TestCase
properties(TestParameter)
useSubCSD = struct('UseSubCSD', true, 'NoSubCSD', false);
solver = struct('Upwind', @upwind_solver, 'HRFVM', @hrfvm_solver);
end
methods(Test)
function test_default_growth(testCase, useSubCSD, solver)
props = parameterized_system_properties();
options = make_options();
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
options.timeStepScale = 0.1;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 300;
nextState = s.step(ic, in, tSpan);
% consistency test
% Note, different solver, different options will result in
% different final concentration. This is only for testing the
% consistency but not ensuring the accuracy.
% Note: Values hard-coded on 6/19/2020
solverFcn = functions(solver);
switch (solverFcn.function)
case 'hrfvm_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.0270545060780174, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.02649, 'RelTol', 1e-3);
end
case 'upwind_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.027965276284955, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.025437263363723, 'RelTol', 1e-3);
end
otherwise
msg = sprintf('Solver %s does not have assertion case', solverFcn.function);
testCase.assertFail(msg);
end
end
function test_multistep(testCase, useSubCSD, solver)
% Note, this test uses higher allowed relative error because
% multistep seems introduces accumulating error.
props = parameterized_system_properties();
options = make_options();
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 100;
nextState = s.step(ic, in, tSpan);
nextState = s.step(nextState, in, tSpan);
nextState = s.step(nextState, in, tSpan);
solverFcn = functions(solver);
switch (solverFcn.function)
case 'hrfvm_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.0270545060780174, 'RelTol', 1e-2);
case 0
testCase.assertEqual(nextState.conc, 0.02649, 'RelTol', 1e-2);
end
case 'upwind_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.027965276284955, 'RelTol', 1e-2);
case 0
testCase.assertEqual(nextState.conc, 0.025437263363723, 'RelTol', 1e-2);
end
otherwise
msg = sprintf('Solver %s does not have assertion case', solverFcn.function);
testCase.assertFail(msg);
end
end
function test_early_stop(testCase, useSubCSD, solver)
props = parameterized_system_properties();
options = make_options();
options.earlyStopThreshold = 1e-5;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 2000;
nextState = s.step(ic, in, tSpan);
testCase.assertEqual(nextState.conc, 0.01059, 'RelTol', 1e-3);
end
function test_polymorphism(testCase, useSubCSD, solver)
props1 = parameterized_system_properties();
props2 = parameterized_system_properties();
props2.solubilityPoly = [6.222e-3; -1.165e-4; 7.644e-6];
props = [props1, props2]; % TODO: props must be col vec!
options = make_options();
options.useSubCSD = useSubCSD;
options.earlyStopThreshold = 0;
ic = [
make_states(props1.solubility(60), 0, props1),...
make_states(nan, 0, props2)
];
in = make_inputs(25);
s = solver(options, props);
tSpan = 2000;
nextState = s.step(ic, in, tSpan);
testCase.assertEqual(nextState(1).conc, 0.008087, 'RelTol', 1e-3);
end
function test_stateful_single_step(testCase, useSubCSD, solver)
props = parameterized_system_properties();
options = make_options();
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 300;
s.set_initial_states(ic);
nextState = s.step([], in, tSpan);
conc = nextState.conc;
prevConc = s.prevStates.conc;
testCase.assertEqual(conc, prevConc);
solverFcn = functions(solver);
switch (solverFcn.function)
case 'hrfvm_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.0270545060780174, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.02649, 'RelTol', 1e-3);
end
case 'upwind_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.027965276284955, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.025437263363723, 'RelTol', 1e-3);
end
otherwise
msg = sprintf('Solver %s does not have assertion case', solverFcn.function);
testCase.assertFail(msg);
end
end
function test_size_dependent_growth(testCase, useSubCSD, solver)
props = parameterized_system_properties();
props.sizeGrids = size_grid(299, 300);
props.gBeta = -2e-3;
options = make_options();
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 300;
nextState = s.step(ic, in, tSpan);
solverFcn = functions(solver);
switch (solverFcn.function)
case 'hrfvm_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.032541771168378, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.032447564014222, 'RelTol', 1e-3);
end
case 'upwind_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.0289843825219405, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.026680361271864, 'RelTol', 1e-3);
end
otherwise
msg = sprintf('Solver %s does not have assertion case', solverFcn.function);
testCase.assertFail(msg);
end
end
function test_size_dependent_growth_increasing_faster(testCase, useSubCSD, solver)
% Warning: this case needs attention. See TODO
% size_dependent_mismatch.
props = parameterized_system_properties();
props.gBeta = 1e-3;
options = make_options();
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
options.timeStepScale = 1e-1;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(25);
tSpan = 300;
nextState = s.step(ic, in, tSpan);
solverFcn = functions(solver);
switch (solverFcn.function)
case 'hrfvm_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.031472683985705, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.0329554700906034, 'RelTol', 1e-3);
end
case 'upwind_solver'
switch (useSubCSD)
case 1
testCase.assertEqual(nextState.conc, 0.027425509727680, 'RelTol', 1e-3);
case 0
testCase.assertEqual(nextState.conc, 0.024804926193285, 'RelTol', 1e-3);
end
otherwise
msg = sprintf('Solver %s does not have assertion case', solverFcn.function);
testCase.assertFail(msg);
end
end
function test_disallow_bad_growth_beta(testCase)
props = parameterized_system_properties();
props.sizeGrids = size_grid(999,1000);
props.gBeta = -1e-3;
function assign_beta()
props.gBeta = -1e-2;
end
testCase.verifyError(@assign_beta, '');
end
function test_cstr_wash_away(testCase, solver, useSubCSD)
props = parameterized_system_properties();
props.snKb = 0;
props.pnKp = 0;
options = make_options();
options.isMSMPR = true;
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(props.solubility(60), 0, props);
in = make_inputs(60, 300, 0, {0});
tSpan = 300;
nextState = s.step(ic, in, tSpan);
testCase.assertEqual(nextState.conc, ic.conc/exp(1), 'RelTol', 1e-2);
end
function test_cstr_concentrating(testCase, solver, useSubCSD)
props = parameterized_system_properties();
props.snKb = 0;
props.pnKp = 0;
options = make_options();
options.isMSMPR = true;
options.earlyStopThreshold = 0;
options.useSubCSD = useSubCSD;
s = solver(options, props);
ic = make_states(0, 0, props);
in = make_inputs(60, 300, props.solubility(60), {0});
tSpan = 300;
nextState = s.step(ic, in, tSpan);
testCase.assertEqual(nextState.conc, in.inConc * (1-1/exp(1)), 'RelTol', 1e-2);
end
% function test_default_msmpr(testCase, solver, useSubCSD)
% props = parameterized_system_properties();
% options = make_options();
% options.isMSMPR = true;
% options.earlyStopThreshold = 0;
% options.useSubCSD = useSubCSD;
% s = solver(options, props);
% ic = make_states(props.solubility(60), 0, props);
% in = make_inputs(25, 600, 0, 0);
% tSpan = 500;
% nextState = s.step(ic, in, tSpan);
% end
end
end