Skip to content

Commit

Permalink
Extracted capsizing illustration from example_leeway to new example_l…
Browse files Browse the repository at this point in the history
…eeway_capsizing
  • Loading branch information
knutfrode committed May 23, 2024
1 parent c389060 commit 3b6dd02
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
34 changes: 6 additions & 28 deletions examples/example_leeway.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,16 @@
reader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() +
'16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')

#%%
# Adding readers successively, and specifying which variables they
# shall provide. This way, order of adding readers does not matter
o.add_reader(reader_norkyst,
variables=['x_sea_water_velocity', 'y_sea_water_velocity'])
o.add_reader(reader_arome, variables=['x_wind', 'y_wind'])
o.add_reader(reader_norkyst)
o.add_reader(reader_arome)
o.set_config('environment:fallback:x_sea_water_velocity', 0)
o.set_config('environment:fallback:y_sea_water_velocity', 0)

#%%
# Activating capsizing for high winds, with probability per hour given by
# p(windspeed) = 0.5 + 0.5*tanh((windspeed-wind_threshold)/sigma)
o.set_config('capsizing', True)
o.set_config('capsizing:wind_threshold', 30)
o.set_config('capsizing:wind_threshold_sigma', 5)
o.set_config('capsizing:leeway_fraction', 0.4) # Reducing leeway coefficients to 40% of original after capsize

o.plot_capsize_probability()

#%%
# Seed leeway elements at defined position and time
object_type = 26 # 26 = Life-raft, no ballast
o.seed_elements(lon=4.5, lat=59.6, radius=100, number=1000,
time=reader_arome.start_time, object_type=object_type)
time=reader_arome.start_time, object_type=object_type)

#%%
# Running model
Expand All @@ -55,14 +41,6 @@
# Print and plot results
print(o)

#%%
# Animation illustrating effect of capsizing
from matplotlib.colors import ListedColormap
o.animation(color='capsized', cmap=ListedColormap(['black','red']), fast=True)

#%%
# .. image:: /gallery/animations/example_leeway_0.gif

#%%
# Animation with current as background.
# Note that drift is also depending on wind, which is not shown.
Expand All @@ -71,7 +49,7 @@
cmap=cmocean.cm.speed, vmin=0, vmax=.8, bgalpha=.7, land_color='#666666', fast=True)

#%%
# .. image:: /gallery/animations/example_leeway_1.gif
# .. image:: /gallery/animations/example_leeway_0.gif

o.plot(fast=True)

Expand All @@ -80,5 +58,5 @@
d, dsub, dstr, lon, lat = o.get_density_array(pixelsize_m=3000)
strand_density = xr.DataArray(dstr[-1,:,:], coords={'lon_bin': lon[0:-1], 'lat_bin': lat[0:-1]})
o.plot(fast=True, background=strand_density.where(strand_density>0),
vmin=0, vmax=20, clabel='Density of stranded elements',
show_elements=False, linewidth=0)
vmin=0, vmax=20, cmap=cmocean.cm.dense, clabel='Density of stranded elements',
show_elements=False, linewidth=0)
73 changes: 73 additions & 0 deletions examples/example_leeway_capsizing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
"""
Leeway capsizing
==================================
"""

from datetime import timedelta
import cmocean
import xarray as xr
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.models.leeway import Leeway

o = Leeway(loglevel=0) # Set loglevel to 0 for debug information

# Atmospheric model for wind
#reader_arome = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')
reader_arome = reader_netCDF_CF_generic.Reader(o.test_data_folder() +
'16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc')

# Ocean model for current
#reader_norkyst = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/mepslatest/meps_lagged_6_h_latest_2_5km_latest.nc')
reader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() +
'16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')

o.add_reader(reader_norkyst)
o.add_reader(reader_arome)

#%%
# Activating capsizing for high winds, with probability per hour given by
# p(windspeed) = 0.5 + 0.5*tanh((windspeed-wind_threshold)/sigma)
o.set_config('capsizing', True)
o.set_config('capsizing:wind_threshold', 30)
o.set_config('capsizing:wind_threshold_sigma', 5)
o.set_config('capsizing:leeway_fraction', 0.4) # Reducing leeway coefficients to 40% of original after capsize

o.plot_capsize_probability()

#%%
# Seed leeway elements at defined position and time
object_type = 26 # 26 = Life-raft, no ballast
o.seed_elements(lon=4.5, lat=59.6, radius=100, number=1000,
time=reader_arome.start_time, object_type=object_type)

#%%
# Running model
o.run(duration=timedelta(hours=48), time_step=900, time_step_output=3600)

#%%
# Print and plot results
print(o)

#%%
# Animation illustrating effect of capsizing
from matplotlib.colors import ListedColormap
o.animation(color='capsized', cmap=ListedColormap(['black','red']), fast=True)

#%%
# .. image:: /gallery/animations/example_leeway_capsizing_0.gif

#%%
# Reverse run, also with probability of (un)-capsizing
o = Leeway()
o.add_reader(reader_norkyst)
o.add_reader(reader_arome)
o.set_config('capsizing', True)
o.seed_elements(lon=4.4, lat=61.0, radius=100, number=1000,
capsized=1, # now we seed all objects as already capsized
time=reader_arome.end_time, object_type=object_type)
o.run(time_step=-900, duration=timedelta(hours=48), time_step_output=timedelta(hours=1))
o.animation(color='capsized', cmap=ListedColormap(['black','red']), fast=True)

#%%
# .. image:: /gallery/animations/example_leeway_capsizing_1.gif

0 comments on commit 3b6dd02

Please sign in to comment.