Skip to content

Commit 920fd8e

Browse files
authored
Merge pull request #42 from OceanParcels/improving_unit_tests
Updating and improving unit testing
2 parents 4dc18c0 + 8326777 commit 920fd8e

3 files changed

Lines changed: 33 additions & 14 deletions

File tree

plasticparcels/constructors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ def create_particleset(fieldset, settings, release_locations):
221221
"""
222222

223223
# Set the longitude, latitude, and plastic amount per particle
224-
lons = release_locations['lons']
225-
lats = release_locations['lats']
224+
lons = np.array(release_locations['lons'])
225+
lats = np.array(release_locations['lats'])
226226
if 'plastic_amount' in release_locations.keys():
227227
plastic_amounts = release_locations['plastic_amount']
228228
else:
229-
plastic_amounts = np.nan_like(lons)
229+
plastic_amounts = np.full_like(lons, np.nan)
230230

231231
# Set particle properties
232232
plastic_densities = np.full(lons.shape, settings['plastictype']['plastic_density'])

tests/test_constructors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ def test_create_fieldset(use_3D, use_biofouling, use_stokes, use_wind, use_mixin
6464
assert isinstance(fieldset, parcels.FieldSet)
6565

6666

67+
# Test the base create_particleset() function
68+
@pytest.mark.parametrize('plastic_amount', [None, 1])
69+
def test_create_particleset(plastic_amount):
70+
settings_file = 'tests/test_data/test_settings.json'
71+
settings = pp.utils.load_settings(settings_file)
72+
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
73+
74+
settings['simulation'] = make_standard_simulation_settings()
75+
76+
settings['plastictype'] = make_standard_plastictype_settings()
77+
78+
if plastic_amount is None:
79+
release_locations = {'lons': [18], 'lats': [35]}
80+
else:
81+
release_locations = {'lons': [18], 'lats': [35], 'plastic_amount': [plastic_amount]}
82+
83+
fieldset = make_simple_fieldset()
84+
pset = pp.constructors.create_particleset(fieldset, settings, release_locations)
85+
86+
assert isinstance(pset, parcels.ParticleSet)
87+
88+
6789
# Test three different initialisation maps
6890
@pytest.mark.parametrize('initialisation_map', ['coastal', 'fisheries', 'rivers'])
6991
def test_create_particleset_from_map(initialisation_map):

tests/test_kernels.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ def make_standard_plastictype_settings():
2626

2727
def make_standard_particleset(fieldset, settings):
2828
# Generate a particleset that has particles in the test domain
29-
settings['release'] = {'initialisation_type': 'fisheries', 'country': 'Malta'}
30-
pset = pp.constructors.create_particleset_from_map(fieldset, settings)
31-
32-
# Only keep particles in the test domain
33-
keep_particles = (pset.lon > 17) & (pset.lon < 20) & (pset.lat < 36) & (pset.lat > 34)
34-
pset.remove_booleanvector(~keep_particles)
29+
release_locations = {'lons': [18, 18.25, 18.5], 'lats': [35, 35, 35],
30+
'plastic_amount': [1, 1, 1]}
31+
pset = pp.constructors.create_particleset(fieldset, settings, release_locations)
3532

3633
return pset
3734

@@ -40,7 +37,6 @@ def make_standard_particleset(fieldset, settings):
4037
def test_advection_only(use_3D):
4138
settings_file = 'tests/test_data/test_settings.json'
4239
settings = pp.utils.load_settings(settings_file)
43-
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
4440

4541
settings['simulation'] = make_standard_simulation_settings()
4642
settings['plastictype'] = make_standard_plastictype_settings()
@@ -75,7 +71,6 @@ def test_advection_only(use_3D):
7571
def test_settling_velocity():
7672
settings_file = 'tests/test_data/test_settings.json'
7773
settings = pp.utils.load_settings(settings_file)
78-
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
7974

8075
settings['simulation'] = make_standard_simulation_settings()
8176
settings['plastictype'] = make_standard_plastictype_settings()
@@ -108,7 +103,6 @@ def test_settling_velocity():
108103
def test_biofouling():
109104
settings_file = 'tests/test_data/test_settings.json'
110105
settings = pp.utils.load_settings(settings_file)
111-
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
112106

113107
settings['simulation'] = make_standard_simulation_settings()
114108
settings['plastictype'] = make_standard_plastictype_settings()
@@ -141,6 +135,8 @@ def test_biofouling():
141135
def test_Stokes():
142136
settings_file = 'tests/test_data/test_settings.json'
143137
settings = pp.utils.load_settings(settings_file)
138+
139+
# Required for the unbeaching kernel
144140
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
145141

146142
settings['simulation'] = make_standard_simulation_settings()
@@ -157,7 +153,7 @@ def test_Stokes():
157153

158154
fieldset = pp.constructors.create_fieldset(settings)
159155

160-
kernels = [pp.kernels.StokesDrift, pp.kernels.deleteParticle]
156+
kernels = [pp.kernels.StokesDrift, pp.kernels.unbeaching, pp.kernels.periodicBC, pp.kernels.deleteParticle]
161157

162158
pset = make_standard_particleset(fieldset, settings)
163159

@@ -173,6 +169,8 @@ def test_Stokes():
173169
def test_wind():
174170
settings_file = 'tests/test_data/test_settings.json'
175171
settings = pp.utils.load_settings(settings_file)
172+
173+
# Required for the unbeaching kernel
176174
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
177175

178176
settings['simulation'] = make_standard_simulation_settings()
@@ -205,7 +203,6 @@ def test_wind():
205203
def test_mixing():
206204
settings_file = 'tests/test_data/test_settings.json'
207205
settings = pp.utils.load_settings(settings_file)
208-
settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data')
209206

210207
settings['simulation'] = make_standard_simulation_settings()
211208
settings['plastictype'] = make_standard_plastictype_settings()

0 commit comments

Comments
 (0)