{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Back and forth\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport opendrift\nfrom opendrift.readers import reader_netCDF_CF_generic\nfrom opendrift.models.oceandrift import OceanDrift\n\nncfile = 'backandforth.nc'\n\no = OceanDrift(loglevel=20) # Set loglevel to 0 for debug information\nreader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() +\n '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')\n#reader_norkyst = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_be')\n\no.add_reader(reader_norkyst)\no.set_config('environment:fallback:land_binary_mask', 0)\no.disable_vertical_motion()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Forward run\nSeeding some particles\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lon = 4.2; lat = 60.1;\ntime = reader_norkyst.start_time\no.seed_elements(lon, lat, radius=1000, number=100, time=time)\n\no.run(steps=50*4, time_step=900, time_step_output=3600, outfile=ncfile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print and plot results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(o)\no.plot(buffer=.2, fast=True)\n\n##%%\n# Backward run:\n# Import forward run, and seed elements at final positions and time\no = opendrift.open(ncfile)\nelements_final = o.elements\ntime_final = o.time\ndel o\no = OceanDrift(loglevel=20) # Set loglevel to 0 for debug information\no.set_config('environment:fallback:land_binary_mask', 0)\no.disable_vertical_motion()\no.add_reader(reader_norkyst)\no.schedule_elements(elements_final, time_final)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running model backwards from end of forward simulation\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o.run(steps=50*4, time_step=-900, time_step_output=3600)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check if backwards simulation brings elements back to their starting position. Some numerical error is expected.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(o)\no.plot(buffer=.2, fast=True)\nos.remove(ncfile)\n\n##%%\n# Compare plots forward and backward" ] } ], "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.11.6" } }, "nbformat": 4, "nbformat_minor": 0 }