-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_concentration_tracker_DDD.py
295 lines (231 loc) · 11.1 KB
/
test_concentration_tracker_DDD.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 12:25:27 2023
@author: LaurentRoberge
"""
import pytest
import numpy as np
from landlab import FieldError, RasterModelGrid
from landlab.components import DepthDependentDiffuser
#from landlab.components import ConcentrationTrackerForDiffusion
from concentration_tracker_DDD import ConcentrationTrackerDDD as ConcentrationTrackerForDiffusion
def test_input_soil_flux_from_diffuser():
"""
ConcentrationTrackerForDiffusion should throw an error when output fields
from a diffusion component do not exist (soil__flux)
"""
# Make a raster model grid
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
_ = mg.add_zeros("topographic__elevation", at="node")
# Instantiate the component
with pytest.raises(FieldError):
_ = ConcentrationTrackerForDiffusion(mg)
def test_input_fields_soil():
"""
ConcentrationTrackerForDiffusion should throw an error when input fields
are not provided (soil__depth, soil_production__rate, topographic__elevation)
"""
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
fields_to_add = ["soil__depth",
"soil_production__rate",
"topographic__elevation"]
# Instantiate the component
for field in fields_to_add:
with pytest.raises(FieldError):
_ = ConcentrationTrackerForDiffusion(mg)
_ = mg.add_zeros(field, at="node")
def test_field_instantiation():
"""
ConcentrationTrackerForDiffusion should instantiate the following fields
when they do not already exist ('sed_property_mass__flux',
'bedrock_property__concentration', 'sed_property__concentration',
'sed_property__decay_rate', 'sed_property__production_rate')
"""
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
_ = mg.add_zeros("topographic__elevation", at="node")
_ = ConcentrationTrackerForDiffusion(mg)
node_fields = ['bedrock_property__concentration',
'sed_property__concentration',
'sed_property__decay_rate',
'sed_property__production_rate'
]
assert "sed_property_mass__flux" in mg.at_link
for node_field in node_fields:
assert node_field in mg.at_node
# Test that default input produces correct fields with no pre-existing fields
def test_fields_for_default_input():
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
_ = ConcentrationTrackerForDiffusion(mg)
link_field = mg.at_link["sed_property_mass__flux"]
node_fields = [mg.at_node["sed_property__concentration"],
mg.at_node["bedrock_property__concentration"],
mg.at_node["sed_property__production_rate"],
mg.at_node["sed_property__decay_rate"]
]
link_check = np.array([0., 0.,
0., 0., 0.,
0., 0.,
0., 0., 0.,
0., 0.])
node_check = np.array([0., 0., 0.,
0., 0., 0.,
0., 0., 0.])
np.testing.assert_equal(link_field, link_check)
for node_field in node_fields:
np.testing.assert_equal(node_field, node_check)
# Test that default input uses correct fields with pre-existing fields
def test_fields_for_default_input_with_preexisting_fields():
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
_ = mg.add_ones('sed_property_mass__flux', at='link')
_ = mg.add_ones('sed_property__concentration', at='node')
_ = mg.add_ones('bedrock_property__concentration', at='node')
_ = mg.add_ones('sed_property__production_rate', at='node')
_ = mg.add_ones('sed_property__decay_rate', at='node')
_ = ConcentrationTrackerForDiffusion(mg)
link_field = mg.at_link["sed_property_mass__flux"]
node_fields = [mg.at_node["sed_property__concentration"],
mg.at_node["bedrock_property__concentration"],
mg.at_node["sed_property__production_rate"],
mg.at_node["sed_property__decay_rate"]
]
link_check = np.array([1., 1.,
1., 1., 1.,
1., 1.,
1., 1., 1.,
1., 1.])
node_check = np.array([1., 1., 1.,
1., 1., 1.,
1., 1., 1.])
np.testing.assert_equal(link_field, link_check)
for node_field in node_fields:
np.testing.assert_equal(node_field, node_check)
# Test that user input of single values produces the correct fields
def test_fields_for_user_value_input():
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
_ = ConcentrationTrackerForDiffusion(mg,
concentration_initial=1,
concentration_in_bedrock=1,
local_production_rate=1,
local_decay_rate=1,)
link_field = mg.at_link["sed_property_mass__flux"]
node_fields = [mg.at_node["sed_property__concentration"],
mg.at_node["bedrock_property__concentration"],
mg.at_node["sed_property__production_rate"],
mg.at_node["sed_property__decay_rate"]
]
link_check = np.array([0., 0.,
0., 0., 0.,
0., 0.,
0., 0., 0.,
0., 0.])
node_check = np.array([1., 1., 1.,
1., 1., 1.,
1., 1., 1.])
np.testing.assert_equal(link_field, link_check)
for node_field in node_fields:
np.testing.assert_equal(node_field, node_check)
# Test that user input of arrays produces the correct fields
def test_fields_for_user_array_input():
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
c_sed = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
c_br = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
p = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
d = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1.])
_ = ConcentrationTrackerForDiffusion(mg,
concentration_initial=c_sed,
concentration_in_bedrock=c_br,
local_production_rate=p,
local_decay_rate=d,)
link_field = mg.at_link["sed_property_mass__flux"]
node_fields = [mg.at_node["sed_property__concentration"],
mg.at_node["bedrock_property__concentration"],
mg.at_node["sed_property__production_rate"],
mg.at_node["sed_property__decay_rate"]
]
link_check = np.array([0., 0.,
0., 0., 0.,
0., 0.,
0., 0., 0.,
0., 0.])
node_check = np.array([1., 1., 1.,
1., 1., 1.,
1., 1., 1.])
np.testing.assert_equal(link_field, link_check)
for node_field in node_fields:
np.testing.assert_equal(node_field, node_check)
# Test that user input of grid fields produces the correct fields
def test_fields_for_user_field_input():
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
c_sed = mg.add_ones('sed_property__concentration', at='node')
c_br = mg.add_ones('bedrock_property__concentration', at='node')
p = mg.add_ones('sed_property__production_rate', at='node')
d = mg.add_ones('sed_property__decay_rate', at='node')
_ = ConcentrationTrackerForDiffusion(mg,
concentration_initial=c_sed,
concentration_in_bedrock=c_br,
local_production_rate=p,
local_decay_rate=d,)
link_field = mg.at_link["sed_property_mass__flux"]
node_fields = [mg.at_node["sed_property__concentration"],
mg.at_node["bedrock_property__concentration"],
mg.at_node["sed_property__production_rate"],
mg.at_node["sed_property__decay_rate"]
]
link_check = np.array([0., 0.,
0., 0., 0.,
0., 0.,
0., 0., 0.,
0., 0.])
node_check = np.array([1., 1., 1.,
1., 1., 1.,
1., 1., 1.])
np.testing.assert_equal(link_field, link_check)
for node_field in node_fields:
np.testing.assert_equal(node_field, node_check)
# Test that physically impossible inputs raise correct errors
def test_properties_concentrations():
"""
ConcentrationTrackerForDiffusion should throw an error when input
concentration values are negative.
"""
mg = RasterModelGrid((3, 3))
_ = mg.add_zeros('soil__flux', at='link')
_ = mg.add_zeros("soil__depth", at="node")
_ = mg.add_zeros("topographic__elevation", at="node")
_ = mg.add_zeros('soil_production__rate', at='node')
# Instantiate the component
with pytest.raises(ValueError):
_ = ConcentrationTrackerForDiffusion(mg, concentration_initial=-1)
# Instantiate the component
with pytest.raises(ValueError):
_ = ConcentrationTrackerForDiffusion(mg, concentration_in_bedrock=-1)
# PLACEHOLDER: Test results against 1-D analytical solution (for DepthDependentDiffuser)
# (I think this is covered by the docstring tests, so I haven't added it here)
# PLACEHOLDER: Test results against 1-D analytical solution (for DepthDependentTaylorDiffuser)
# (I think this is covered by the docstring tests, so I haven't added it here)