Oceananigans.jl
repository·main·Indexed 23 days ago
https://github.com/clima/oceananigans.jlA high-performance Julia package for finite volume simulations of incompressible fluid dynamics (Boussinesq equations). It supports nonhydrostatic and hydrostatic models in Cartesian and spherical shell domains, with native acceleration for both CPUs and GPUs.
What's inside Oceananigans.jl
- Oceananigans.jl is a fast, flexible software package for finite volume simulations of nonhydrostatic and hydrostatic Boussinesq equations. It supports both Cartesian and spherical shell domains and is optimized for execution on both CPUs and GPUs.
Overview of Oceananigans output writers
mainOceananigans provides three primary ways to save simulation data to disk using
AbstractOutputWriters:NetCDFWriter: Best for saving arrays and scalars to NetCDF files (usesNCDatasets.jl). Ideal for standard scientific data formats.JLD2Writer: Best for saving arbitrary Julia data structures (usesJLD2.jl). These files are HDF5-compatible and can be opened in Julia withJLD2.jlor in Python withh5py.Checkpointer: Automatically saves as much model data as possible for simulation recovery (usesJLD2.jl).
To use a writer, create the instance and add it to the
simulation.output_writersdictionary before callingrun!(simulation).Impose boundary conditions in Oceananigans.jl
mainIn bounded, non-periodic directions, you can impose several types of boundary conditions. Note that for a velocity field, the only available boundary condition for the component normal to the bounded direction is no-penetration.
Available boundary condition types:
- Flux: Prescribes the flux of a quantity normal to the boundary.
- Gradient (Neumann): Prescribes the gradient of a field normal to the boundary.
- Value (Dirichlet): Prescribes the specific value of a field on the boundary.
- No penetration: Prescribes the velocity component normal to the boundary to be 0.
Explore the Oceananigans.jl public API modules
mainThe Oceananigans.jl library is organized into several specialized modules that cover the full spectrum of ocean modeling. Key functional areas include:
- Core Simulation & Models:
Oceananigans.Models(including Non-hydrostatic, Hydrostatic free-surface, Shallow-water, and Lagrangian particle tracking),Oceananigans.Simulations, andOceananigans.TimeSteppers. - Physics & Dynamics:
Oceananigans.Advection,Oceananigans.BuoyancyFormulations,Oceananigans.Coriolis,Oceananigans.TurbulenceClosures, andOceananigans.StokesDrifts. - Numerical Components:
Oceananigans.Grids,Oceananigans.Fields,Oceananigans.Solvers,Oceananigans.Operators, andOceananigans.AbstractOperations. - Boundary & Forcing:
Oceananigans.BoundaryConditions,Oceananigans.Forcings, andOceananigans.ImmersedBoundaries. - Data & I/O:
Oceananigans.OutputWriters,Oceananigans.OutputReaders, andOceananigansNCDatasetsExt. - Infrastructure:
Oceananigans.DistributedComputations,Oceananigans.Logger, andOceananigans.Utils.
- Core Simulation & Models:
What is a FieldTimeSeries?
mainAFieldTimeSeriesis a data structure that implements aFieldwith time dependence. It allows you to store and access field data across multiple time steps. It can be constructed manually on an existing grid or generated from simulation output files (e.g., JLD2 files).Use Flux boundary conditions
mainA flux boundary condition prescribes the flux of a quantity normal to the boundary. For a tracer $c$, this corresponds to prescribing the normal flux $q_c$ at the boundary $\partial \Omega_b$.Available time steppers by model type
mainOceananigans provides different explicit time-stepping schemes depending on the model being used. The
TimeSteppersmodule handles the time integration of prognostic fields, while the model itself is responsible for computing tendencies.Model Type Supported Time Steppers NonhydrostaticModel:QuasiAdamsBashforth2,:RungeKutta3(default)ShallowWaterModel:QuasiAdamsBashforth2,:RungeKutta3(default)HydrostaticFreeSurfaceModel:QuasiAdamsBashforth2(default),:SplitRungeKuttaUnderstand Cartesian coordinate systems
mainIn Oceananigans,
RectilinearGriduses a Cartesian coordinate system. This provides a local, flat (Euclidean) representation of space using coordinates $(x, y, z)$ and unit vectors $\boldsymbol{\hat x}$, $\boldsymbol{\hat y}$, and $\boldsymbol{\hat z}$.Conventions:
- $\boldsymbol{\hat x}$: Points East.
- $\boldsymbol{\hat y}$: Points North.
- $\boldsymbol{\hat z}$: Points "upward" (opposite to the direction of gravitational acceleration).
Velocity Representation: Three-dimensional velocity is $\boldsymbol{v} = u \boldsymbol{\hat x} + v \boldsymbol{\hat y} + w \boldsymbol{\hat z}$, where $u, v, w$ are the velocity components in the $x, y, z$ directions respectively. Horizontal velocity is denoted as $\boldsymbol{u} = u \boldsymbol{\hat x} + v \boldsymbol{\hat y}$.
Understand momentum and tracer conservation equations
mainMomentum Conservation
The model solves for momentum in a rotating fluid with buoyancy via the Boussinesq approximation. The momentum equation includes:
- Momentum advection:
(v · ∇) u - Coriolis force:
f × u - Baroclinic kinematic pressure gradient:
∇ p - Barotropic kinematic pressure gradient:
∇ (g η) - Viscous stress:
∇ · τ - Internal forcing:
F_u
Note: For curvilinear grids like
LatitudeLongitudeGrid, the advection term is implemented in vector-invariant form using vertical relative vorticityζ.Tracer Conservation
Tracer equations follow the form:
∂t c = - v · ∇ c - ∇ · q_c + F_cWhere:
v · ∇ cis tracer advection∇ · q_cis molecular or turbulent diffusionF_cis an arbitrary internal source term
- Momentum advection:
Choose the appropriate grid type for your domain
mainThe choice of grid depends on the physical shape of your domain:
RectilinearGrid: Used for lines, rectangles, and boxes (Cartesian coordinatesx,y,z).LatitudeLongitudeGrid: Used for sectors of thin spherical shells with cells bounded by constant latitude and longitude (Geographic coordinatesλ,φ,z).OrthogonalSphericalShellGrid: Used for sectors of thin spherical shells with arbitrary mesh lines that intersect at right angles.
Note that
LatitudeLongitudeGridandOrthogonalSphericalShellGriduse geographic coordinates(λ, φ, z), whereλis longitude,φis latitude, andzis height. You can inspect these usingλnodes,φnodes,λspacings, andφspacings.Finite volume discretization of generalized vertical coordinates
mainOceananigans uses a finite volume staggered C-grid framework to discretize the governing equations in generalized vertical coordinates. The discretization is performed by integrating over the cell volume $V_r = \Delta x \Delta y \Delta r$, where $\Delta z = \sigma \Delta r$. Indices $i, j, k$ correspond to the $x, y,$ and vertical directions respectively.
Key aspects of the discretization include:
- Mass Conservation: The vertical velocity $\omega$ (in $r$ space) is diagnosed using the grid velocity and the horizontal velocity divergence. The discrete mass conservation equation relates the change in cell-averaged $\sigma$ to the horizontal fluxes $\mathcal{U} = A_x u$ and $\mathcal{V} = A_y v$, and the vertical flux $\mathcal{W} = A_z \omega$.
- Tracer Equation: The tracer equation is discretized by integrating the tendency over the cell volume. For an explicit formulation (e.g., forward Euler), the tracer value at the next time step is calculated as:
$$c^{n+1} = \frac{\sigma^n}{\sigma^{n+1}}(c^n + \Delta t , G^n)$$
where $G^n$ is the tendency computed on the $z$-grid.
- Multi-step Methods: For methods like second-order Adams-Bashforth, the time-discretization accounts for the changing grid at different time-steps:
$$c^{n+1} = \frac{1}{\sigma^{n+1}} \left[\sigma^n c^n + \Delta t \left(\frac{3}{2} \sigma^n G^n - \frac{1}{2} \sigma^{n-1} G^{n-1}\right)\right]$$
To support this, Oceananigans stores tendencies pre-multiplied by $\sigma$ at their current time-level.
- Implicit Diffusion: When using implicit discretization for diffusive fluxes, the code first computes $c^{n+1}$ using the explicit formula (excluding diffusive fluxes), then performs the implicit step on a $z$-grid using the grid at $n+1$ (including $\sigma^{n+1}$), treating the grid as static during that step.
How Split Runge-Kutta works
mainThe
SplitRungeKuttaTimeStepperis designed for split-explicit computations (e.g., treating barotropic and baroclinic modes separately in aHydrostaticFreeSurfaceModel).At the start of each time step, prognostic fields are cached. Subsequent substeps then compute the update using the cached initial state and stage coefficients $\beta$. The default three-stage scheme uses $\beta = (3, 2, 1)$.