Skip to content

Commit

Permalink
Merge pull request #1350 from knutfrode/dev
Browse files Browse the repository at this point in the history
[run-ex] Config general:coastline_approximation_precision is now acti…
  • Loading branch information
knutfrode authored Jul 3, 2024
2 parents b2149bf + f0a4d40 commit 52913f0
Show file tree
Hide file tree
Showing 7 changed files with 2,629 additions and 23 deletions.
3 changes: 2 additions & 1 deletion examples/example_coastline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
o.add_reader([reader_osc])
o.set_config('environment:fallback:y_sea_water_velocity', .2)
o.set_config('general:coastline_action', 'stranding')
o.set_config('general:coastline_approximation_precision', None)
o.seed_elements(lon=12.2, lat=67.7, radius=5000, number=number, time=reader_osc.zero_time)
o.run(duration=duration, time_step=time_step)
print(f'Calculation time: {o.timing["total time"]}')
Expand All @@ -41,7 +42,7 @@
# Coastline option "stranding" with higher precision
# ==================================================
#
# By setting config "general:coastline_approximation_precision" to desired accuracy in degrees,
# By setting config "general:coastline_approximation_precision" to desired accuracy in degrees (default is 0.01 as in this example),
# a more exact coastline crossing is calculated by the deactivated particles.
# Note that with a (too) large compuation time step, particles may still "jump" over islands.
# An alternative to avoid this possibility is to use a smaller timestep for the simulation, though at a larger computational cost.
Expand Down
20 changes: 8 additions & 12 deletions opendrift/models/basemodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,22 +319,18 @@ def __init__(self,
'default': '', 'level': CONFIG_LEVEL_BASIC,
'description': 'Name of simulation'},
'general:coastline_action': {
'type':
'enum',
'type': 'enum',
'enum': ['none', 'stranding', 'previous'],
'default':
'stranding',
'level':
CONFIG_LEVEL_BASIC,
'description':
'None means that objects may also move over land. '
'stranding means that objects are deactivated if they hit land. '
'previous means that objects will move back to the previous location '
'if they hit land'
'default': 'stranding',
'level': CONFIG_LEVEL_BASIC,
'description': 'None means that objects may also move over land. '
'stranding means that objects are deactivated if they hit land. '
'previous means that objects will move back to the previous location '
'if they hit land'
},
'general:coastline_approximation_precision': {
'type': 'float',
'default': None,
'default': 0.001,
'min': 0.0001,
'max': 0.005,
'units': 'degrees',
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ grib = ["cfgrib", "pygrib"]
[tool.poetry.dependencies]
python = ">=3.9,<3.13"
matplotlib = ">=3.5"
numpy = ">=1.23"
numpy = "<=1.26.4"
scipy = ">=1.9"
netCDF4 = ">=1.6"
pyproj = ">=2.3"
Expand All @@ -43,7 +43,7 @@ utm = ">=0.7"
roaring-landmask = ">=0.7"
requests = ">=2.28"
pykdtree = ">=1.3"
trajan = ">=0.1.3"
trajan = ">=0.6.3"
xhistogram = ">=0.3"
#adios_db = "<1.2"
copernicusmarine = ">=1.1.1"
Expand Down
6 changes: 5 additions & 1 deletion tests/models/test_leeway.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_leewayrun(tmpdir, test_data):
object_type = 50 # FISHING-VESSEL-1
reader_landmask = reader_global_landmask.Reader()
lee.add_reader([reader_landmask])
lee.set_config('general:coastline_approximation_precision', None)
lee.set_config('environment:fallback:x_wind', 0)
lee.set_config('environment:fallback:y_wind', 10)
lee.set_config('environment:fallback:x_sea_water_velocity', 0)
Expand All @@ -70,13 +71,16 @@ def test_leewayrun(tmpdir, test_data):
asciif = tmpdir + '/leeway_ascii.txt'
lee.export_ascii(asciif)
asciitarget = test_data + "/generated/test_leewayrun_export_ascii.txt"
asciitarget2 = test_data + "/generated/test_leewayrun_export_ascii_v2.txt"
from difflib import Differ
with open(asciif) as file_1, open(asciitarget) as file_2:
differ = Differ()
for line in differ.compare(file_1.readlines(), file_2.readlines()):
print(line)
import filecmp
assert filecmp.cmp(asciif, asciitarget)
if not filecmp.cmp(asciif, asciitarget):
# Comparing with second versio of ASCII file, with slight numerical differences
assert filecmp.cmp(asciif, asciitarget2)

def test_capsize():
o = Leeway(loglevel=20)
Expand Down
17 changes: 16 additions & 1 deletion tests/models/test_stranding.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_stranding_3d(self):
o.add_reader(reader_nordic)
o.set_config('environment:fallback:y_wind', 10) # Some wind for mixing
o.set_config('general:coastline_action', 'stranding')
o.set_config('general:coastline_approximation_precision', None)
o.set_config('vertical_mixing:timestep', 120)
o.set_config('drift:max_speed', .1)
o.seed_elements(lon=14.0, lat=68.15, radius=2000, number=100,
Expand Down Expand Up @@ -110,6 +111,7 @@ def test_stranding_options(self):
time_step = -900
o = OceanDrift(loglevel=50)
o.set_config('general:coastline_action', option)
o.set_config('general:coastline_approximation_precision', None)
o.add_reader([reader_osc, reader_global])
# Adding northwards drift
o.set_config('environment:constant:y_sea_water_velocity', .1)
Expand All @@ -128,7 +130,6 @@ def test_stranding_options(self):
self.assertIsNone(np.testing.assert_array_almost_equal(
el.lon, lons[i], 3))


def test_interact_coastline_global(self):
reader_global = reader_global_landmask.Reader()

Expand All @@ -144,6 +145,20 @@ def test_interact_coastline_global(self):
self.assertAlmostEqual(lons[-2], 5.092, 2)
self.assertAlmostEqual(lons[-1], 5.092, 2)

def test_stranding_approximation(self):
o = OceanDrift(loglevel=0)
o.set_config('environment:constant:x_sea_water_velocity', 1)
o.set_config('general:coastline_approximation_precision', None)
o.seed_elements(lon=4.55, lat=60, time=datetime.now())
o.run(steps=10)
o2 = OceanDrift(loglevel=0)
o2.set_config('environment:constant:x_sea_water_velocity', 1)
o2.set_config('general:coastline_approximation_precision', .001)
o2.seed_elements(lon=4.55, lat=60, time=datetime.now())
o2.run(steps=10)
self.assertAlmostEqual(o.elements_deactivated.lon[0], 5.066, 3)
self.assertAlmostEqual(o2.elements_deactivated.lon[0], 5.051, 3)


if __name__ == '__main__':
unittest.main()
21 changes: 15 additions & 6 deletions tests/readers/test_shyfem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
from opendrift.models.oceandrift import OceanDrift

ismar = 'https://iws.ismar.cnr.it/thredds/dodsC/emerge/shyfem_unstructured_adriatic.nc'


def test_open():
try:
r = shyfem.Reader(ismar)
print(r)
# r.plot_mesh()
# plt.show()
shyfem_available = True
except:
shyfem_available = False

#def test_open():
# r = shyfem.Reader(ismar)
# print(r)
# # r.plot_mesh()
# # plt.show()

need_shyfem = pytest.mark.skipif(shyfem_available == False,
reason='Shyfem sample file not available/readable from iws.ismar.cnr.it/thredds')

@need_shyfem
def test_get_variables(benchmark):
r = shyfem.Reader(ismar)

Expand All @@ -28,11 +35,13 @@ def test_get_variables(benchmark):
assert u.shape == (len(x),)
assert len(u) == len(x)

@need_shyfem
def test_z_polarity():
r = shyfem.Reader(ismar)
assert r.zmax > r.zmin
assert (r.z < 0).all()

@need_shyfem
def test_get_values():
r = shyfem.Reader(ismar)

Expand Down
Loading

0 comments on commit 52913f0

Please sign in to comment.