|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Element dependent environment |
| 4 | +============================= |
| 5 | +""" |
| 6 | + |
| 7 | +from datetime import datetime |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import numpy as np |
| 10 | +from opendrift.models.oceandrift import OceanDrift |
| 11 | +import trajan as ta |
| 12 | + |
| 13 | +#%% |
| 14 | +# OpenDrift elements have properties such as lon, lat, z, size etc. |
| 15 | +# These elements are moved and changed based on environment properties such as current, wind, temperature etc. |
| 16 | +# In principle the element properties do not affect the environment, however, |
| 17 | +# for sensitivity studies it can be of interest to let different elements of |
| 18 | +# the same simulation be exposed to different environment. |
| 19 | +# This is made possible by specifying the (constant) environment values |
| 20 | +# for all elements of a seed call as illustrated below |
| 21 | + |
| 22 | + |
| 23 | +#%% |
| 24 | +# First an example with two different seedings with two different environments |
| 25 | +o = OceanDrift(loglevel=20) |
| 26 | +# First seeding 200 elements that will be exposed to eastward current and a horizontal diffusivity of 10 m2/s |
| 27 | +number = 200 |
| 28 | +o.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10, |
| 29 | + environment={'horizontal_diffusivity': 10, |
| 30 | + 'x_sea_water_velocity': 1, |
| 31 | + 'y_sea_water_velocity': 0}) |
| 32 | +# Then seeding 100 elements that will be exposed to northward current and less diffusivity (1 m2/s) |
| 33 | +number = 100 |
| 34 | +o.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10, |
| 35 | + environment={'horizontal_diffusivity': 1, |
| 36 | + 'x_sea_water_velocity': 0, |
| 37 | + 'y_sea_water_velocity': .5}) |
| 38 | +o.run(steps=10) |
| 39 | +o.plot() |
| 40 | + |
| 41 | +#%% |
| 42 | +# Second example with a single seeding where each element will be exposed to different diffusivity values |
| 43 | +o = OceanDrift(loglevel=20) |
| 44 | +# Seeding 1000 elements that will be exposed to north-eastward current with diffusivities ranging from 0 to 50 m2/s |
| 45 | +number = 1000 |
| 46 | +diffusivity_values = [0, 1, 5, 10, 50] |
| 47 | +# Repeating values so that 200 elements get each diffusivity |
| 48 | +diffusivities = np.repeat(diffusivity_values, number/len(diffusivity_values)) |
| 49 | + |
| 50 | +o.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10, |
| 51 | + environment={'horizontal_diffusivity': diffusivities, |
| 52 | + 'x_sea_water_velocity': .2, |
| 53 | + 'y_sea_water_velocity': .2}) |
| 54 | +ds = o.run(steps=10) |
| 55 | +ds.traj.plot(land=None, margin=0) |
| 56 | +# Plotting the convex hull around end positions separately for each diffusivity value |
| 57 | +ds = ds.isel(time=-1) |
| 58 | +colors = plt.cm.jet(np.linspace(0, 1, 5)) |
| 59 | +for d, color in zip(diffusivity_values, colors): |
| 60 | + ds.where(ds.horizontal_diffusivity==d).traj.plot.convex_hull(label=f'Diffusivity {d} m2/s', color=color) |
| 61 | +plt.legend() |
| 62 | +plt.show() |
| 63 | + |
| 64 | +#%% |
| 65 | +# The above could also be achieved by performing separate simulations for each value of diffusivity, |
| 66 | +# but with more computational overhead/time and more complexity |
0 commit comments