|
| 1 | +import parcels.application_kernels |
| 2 | +import plasticparcels as pp |
| 3 | +import parcels |
| 4 | +from datetime import datetime, timedelta |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +def make_simple_fieldset(): |
| 9 | + fieldset = parcels.FieldSet.from_data({'U': 0, 'V': 0}, |
| 10 | + {'lon': 0, 'lat': 0}, |
| 11 | + mesh='spherical') |
| 12 | + return fieldset |
| 13 | + |
| 14 | + |
| 15 | +def make_standard_simulation_settings(): |
| 16 | + simulation_settings = {'startdate': datetime.strptime('2020-01-04-00:00:00', '%Y-%m-%d-%H:%M:%S'), |
| 17 | + 'runtime': timedelta(days=2), |
| 18 | + 'outputdt': timedelta(hours=1), |
| 19 | + 'dt': timedelta(minutes=20), |
| 20 | + } |
| 21 | + return simulation_settings |
| 22 | + |
| 23 | + |
| 24 | +def make_standard_plastictype_settings(): |
| 25 | + plastictype_settings = {'wind_coefficient': 0.01, # Percentage of wind to apply to particles |
| 26 | + 'plastic_diameter': 0.01, # Plastic particle diameter (m) |
| 27 | + 'plastic_density': 1030., # Plastic particle density (kg/m^3) |
| 28 | + } |
| 29 | + return plastictype_settings |
| 30 | + |
| 31 | + |
| 32 | +# Test the create_hydrodynamic_fieldset() function |
| 33 | +def test_create_hydrodynamic_fieldset(): |
| 34 | + settings_file = 'tests/test_data/test_settings.json' |
| 35 | + settings = pp.utils.load_settings(settings_file) |
| 36 | + settings['simulation'] = make_standard_simulation_settings() |
| 37 | + |
| 38 | + fieldset = pp.constructors.create_hydrodynamic_fieldset(settings) |
| 39 | + |
| 40 | + assert isinstance(fieldset, parcels.FieldSet) |
| 41 | + |
| 42 | + |
| 43 | +# Test the create_fieldset() function |
| 44 | +@pytest.mark.parametrize('use_3D', [True, False]) |
| 45 | +@pytest.mark.parametrize('use_biofouling', [True, False]) |
| 46 | +@pytest.mark.parametrize('use_stokes', [True, False]) |
| 47 | +@pytest.mark.parametrize('use_wind', [True, False]) |
| 48 | +@pytest.mark.parametrize('use_mixing', [True, False]) |
| 49 | +def test_create_fieldset(use_3D, use_biofouling, use_stokes, use_wind, use_mixing): |
| 50 | + settings_file = 'tests/test_data/test_settings.json' |
| 51 | + settings = pp.utils.load_settings(settings_file) |
| 52 | + settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data') |
| 53 | + |
| 54 | + settings['simulation'] = make_standard_simulation_settings() |
| 55 | + |
| 56 | + settings['use_3D'] = use_3D |
| 57 | + settings['use_biofouling'] = use_biofouling |
| 58 | + settings['use_stokes'] = use_stokes |
| 59 | + settings['use_wind'] = use_wind |
| 60 | + settings['use_mixing'] = use_mixing |
| 61 | + |
| 62 | + fieldset = pp.constructors.create_fieldset(settings) |
| 63 | + |
| 64 | + assert isinstance(fieldset, parcels.FieldSet) |
| 65 | + |
| 66 | + |
| 67 | +# Test three different initialisation maps |
| 68 | +@pytest.mark.parametrize('initialisation_map', ['coastal', 'fisheries', 'rivers']) |
| 69 | +def test_create_particleset_from_map(initialisation_map): |
| 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 | + settings['release'] = {'initialisation_type': initialisation_map, |
| 79 | + 'country': 'Italy', |
| 80 | + } |
| 81 | + |
| 82 | + fieldset = make_simple_fieldset() |
| 83 | + pset = pp.constructors.create_particleset_from_map(fieldset, settings) |
| 84 | + |
| 85 | + assert isinstance(pset, parcels.ParticleSet) |
| 86 | + |
| 87 | + |
| 88 | +# Test the two global concentration release map types |
| 89 | +@pytest.mark.parametrize('concentration_type', ['Beach', 'Ocean']) |
| 90 | +def test_create_particleset_from_map_concentrations(concentration_type): |
| 91 | + settings_file = 'tests/test_data/test_settings.json' |
| 92 | + settings = pp.utils.load_settings(settings_file) |
| 93 | + settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data') |
| 94 | + |
| 95 | + settings['simulation'] = make_standard_simulation_settings() |
| 96 | + |
| 97 | + settings['plastictype'] = make_standard_plastictype_settings() |
| 98 | + |
| 99 | + settings['release'] = {'initialisation_type': 'global_concentrations', |
| 100 | + 'concentration_type': concentration_type, |
| 101 | + } |
| 102 | + |
| 103 | + fieldset = make_simple_fieldset() |
| 104 | + pset = pp.constructors.create_particleset_from_map(fieldset, settings) |
| 105 | + |
| 106 | + assert isinstance(pset, parcels.ParticleSet) |
| 107 | + |
| 108 | + |
| 109 | +# Test three different initialisation maps |
| 110 | +@pytest.mark.parametrize('use_3D', [True, False]) |
| 111 | +@pytest.mark.parametrize('use_biofouling', [True, False]) |
| 112 | +@pytest.mark.parametrize('use_stokes', [True, False]) |
| 113 | +@pytest.mark.parametrize('use_wind', [True, False]) |
| 114 | +@pytest.mark.parametrize('use_mixing', [True, False]) |
| 115 | +def test_create_kernel(use_3D, use_biofouling, use_stokes, use_wind, use_mixing): |
| 116 | + settings_file = 'tests/test_data/test_settings.json' |
| 117 | + settings = pp.utils.load_settings(settings_file) |
| 118 | + settings = pp.utils.download_plasticparcels_dataset('NEMO0083', settings, 'input_data') |
| 119 | + |
| 120 | + settings['simulation'] = make_standard_simulation_settings() |
| 121 | + |
| 122 | + settings['use_3D'] = use_3D |
| 123 | + settings['use_biofouling'] = use_biofouling |
| 124 | + settings['use_stokes'] = use_stokes |
| 125 | + settings['use_wind'] = use_wind |
| 126 | + settings['use_mixing'] = use_mixing |
| 127 | + |
| 128 | + fieldset = pp.constructors.create_fieldset(settings) |
| 129 | + kernels = pp.constructors.create_kernel(fieldset) |
| 130 | + |
| 131 | + if use_3D: |
| 132 | + assert parcels.application_kernels.AdvectionRK4_3D in kernels |
| 133 | + assert pp.kernels.checkThroughBathymetry in kernels |
| 134 | + assert pp.kernels.checkErrorThroughSurface in kernels |
| 135 | + else: |
| 136 | + assert parcels.application_kernels.AdvectionRK4 in kernels |
| 137 | + |
| 138 | + if use_3D and use_biofouling: |
| 139 | + assert pp.kernels.Biofouling in kernels |
| 140 | + elif use_3D and not use_biofouling: |
| 141 | + assert pp.kernels.SettlingVelocity in kernels |
| 142 | + |
| 143 | + if use_mixing: |
| 144 | + assert pp.kernels.VerticalMixing in kernels |
| 145 | + |
| 146 | + if use_stokes: |
| 147 | + assert pp.kernels.StokesDrift in kernels |
| 148 | + assert pp.kernels.unbeaching in kernels |
| 149 | + |
| 150 | + if use_wind: |
| 151 | + assert pp.kernels.WindageDrift in kernels |
| 152 | + assert pp.kernels.unbeaching in kernels |
| 153 | + |
| 154 | + assert pp.kernels.deleteParticle in kernels |
| 155 | + assert pp.kernels.periodicBC in kernels |
| 156 | + assert pp.kernels.PolyTEOS10_bsq in kernels |
0 commit comments