{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Element dependent environment\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom opendrift.models.oceandrift import OceanDrift\nimport trajan as ta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OpenDrift elements have properties such as lon, lat, z, size etc.\nThese elements are moved and changed based on environment properties such as current, wind, temperature etc.\nIn principle the element properties do not affect the environment, however,\nfor sensitivity studies it can be of interest to let different elements of \nthe same simulation be exposed to different environment.\nThis is made possible by specifying the (constant) environment values\nfor all elements of a seed call as illustrated below\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First an example with two different seedings with two different environments\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o = OceanDrift(loglevel=20)\n# First seeding 200 elements that will be exposed to eastward current and a horizontal diffusivity of 10 m2/s\nnumber = 200\no.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10,\n environment={'horizontal_diffusivity': 10,\n 'x_sea_water_velocity': 1,\n 'y_sea_water_velocity': 0})\n# Then seeding 100 elements that will be exposed to northward current and less diffusivity (1 m2/s)\nnumber = 100\no.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10,\n environment={'horizontal_diffusivity': 1,\n 'x_sea_water_velocity': 0,\n 'y_sea_water_velocity': .5})\no.run(steps=10)\no.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second example with a single seeding where each element will be exposed to different diffusivity values\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o = OceanDrift(loglevel=20)\n# Seeding 1000 elements that will be exposed to north-eastward current with diffusivities ranging from 0 to 50 m2/s\nnumber = 1000\ndiffusivity_values = [0, 1, 5, 10, 50]\n# Repeating values so that 200 elements get each diffusivity\ndiffusivities = np.repeat(diffusivity_values, number/len(diffusivity_values))\n\no.seed_elements(lon=-60, lat=40, time=datetime(2022,1,1), number=number, radius=10,\n environment={'horizontal_diffusivity': diffusivities,\n 'x_sea_water_velocity': .2,\n 'y_sea_water_velocity': .2})\nds = o.run(steps=10)\nds.traj.plot(land=None, margin=0)\n# Plotting the convex hull around end positions separately for each diffusivity value\nds = ds.isel(time=-1)\ncolors = plt.cm.jet(np.linspace(0, 1, 5))\nfor d, color in zip(diffusivity_values, colors):\n ds.where(ds.horizontal_diffusivity==d).traj.plot.convex_hull(label=f'Diffusivity {d} m2/s', color=color)\nplt.legend()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above could also be achieved by performing separate simulations for each value of diffusivity,\nbut with more computational overhead/time and more complexity\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 0 }