Tsunami Project Lab
Loading...
Searching...
No Matches
tsunami_lab::io::NetCdf Class Reference

#include <NetCdf.h>

Classes

struct  Append
 tag type selecting the append constructor (assignment 7.1 resume) More...
 
struct  Checkpoint
 
struct  Grid
 

Public Member Functions

 NetCdf (std::string i_path, t_real i_dxy, t_idx i_nx, t_idx i_ny, t_idx i_stride, t_real const *i_bathymetry, t_idx i_k)
 
 NetCdf (Append, std::string i_path, t_real i_dxy, t_idx i_nx, t_idx i_ny, t_idx i_stride, t_idx i_k)
 
 ~NetCdf ()
 
 NetCdf (NetCdf const &)=delete
 
NetCdfoperator= (NetCdf const &)=delete
 
void writeTimeStep (t_real i_simTime, t_real const *i_h, t_real const *i_hu, t_real const *i_hv)
 
std::size_t records () const
 
void writeCheckpoint (std::string i_path, t_real const *i_h, t_real const *i_hu, t_real const *i_hv, t_real const *i_bathymetry, t_real i_simTime, t_idx i_timeStep, t_real i_dt)
 

Static Public Member Functions

static Grid read (std::string i_path)
 
static bool hasCheckpoint (std::string i_path)
 
static Checkpoint readCheckpoint (std::string i_path)
 

Constructor & Destructor Documentation

◆ NetCdf() [1/3]

tsunami_lab::io::NetCdf::NetCdf ( std::string i_path,
t_real i_dxy,
t_idx i_nx,
t_idx i_ny,
t_idx i_stride,
t_real const * i_bathymetry,
t_idx i_k )

Creates a fresh netCDF file at i_path (overwriting any existing file), defines time/y/x dimensions, the coordinate variables and the time-independent bathymetry, and writes them once. Cell centres are at ((ix + 0.5) * dxy, (iy + 0.5) * dxy) so the values align with the convention used by io::Csv.

Parameters
i_pathoutput file path.
i_dxyuniform cell width/height (m).
i_nxnumber of interior cells in x-direction.
i_nynumber of interior cells in y-direction.
i_striderow stride of the data arrays passed to writeTimeStep().
i_bathymetrybathymetry values at the interior origin (length stride * ny). Optional: nullptr writes a zero-filled bathymetry variable.
i_kcoarse-output factor: each k x k block of interior cells is averaged into one output cell, yielding a ceil(nx/k) x ceil(ny/k) grid. Pass 1 for full-resolution output.

◆ NetCdf() [2/3]

tsunami_lab::io::NetCdf::NetCdf ( Append ,
std::string i_path,
t_real i_dxy,
t_idx i_nx,
t_idx i_ny,
t_idx i_stride,
t_idx i_k )

Opens an existing output file (written by the create constructor) for appending: looks up the time/height/momentum variable ids and positions the next record after the last one already present, so a resumed run continues the same solution.nc instead of overwriting it. The grid geometry (full-resolution nx/ny, stride, dxy) and the coarse factor must match the original run.

Parameters
i_pathexisting output file to append to.
i_dxyuniform cell width/height (m).
i_nxnumber of interior cells in x-direction.
i_nynumber of interior cells in y-direction.
i_striderow stride of the data arrays passed to writeTimeStep().
i_kcoarse-output factor used by the original run.

◆ ~NetCdf()

tsunami_lab::io::NetCdf::~NetCdf ( )

Closes the netCDF file (if open). Errors during close are reported on stderr but not thrown — destructors must not throw.

◆ NetCdf() [3/3]

tsunami_lab::io::NetCdf::NetCdf ( NetCdf const & )
delete

Member Function Documentation

◆ hasCheckpoint()

bool tsunami_lab::io::NetCdf::hasCheckpoint ( std::string i_path)
static

True if i_path names a readable checkpoint file (never throws).

◆ operator=()

NetCdf & tsunami_lab::io::NetCdf::operator= ( NetCdf const & )
delete

◆ read()

tsunami_lab::io::NetCdf::Grid tsunami_lab::io::NetCdf::read ( std::string i_path)
static

Reads a netCDF file with coordinate variables x, y and a 2d data variable z. Both (y, x) and (x, y) dim layouts of z are accepted; the result is always returned in canonical (y, x) row-major order.

Parameters
i_pathpath to the input file.
Returns
the parsed Grid.

◆ readCheckpoint()

tsunami_lab::io::NetCdf::Checkpoint tsunami_lab::io::NetCdf::readCheckpoint ( std::string i_path)
static

Reads a checkpoint written by writeCheckpoint(), returning the four fields plus the simulation time, step counter and constant dt. Throws if the file cannot be opened or is incomplete.

Parameters
i_pathpath to the checkpoint file.
Returns
the recovered restart state.

◆ records()

std::size_t tsunami_lab::io::NetCdf::records ( ) const
inline

Number of records written so far (mainly for tests).

◆ writeCheckpoint()

void tsunami_lab::io::NetCdf::writeCheckpoint ( std::string i_path,
t_real const * i_h,
t_real const * i_hu,
t_real const * i_hv,
t_real const * i_bathymetry,
t_real i_simTime,
t_idx i_timeStep,
t_real i_dt )

Writes a full, self-contained restart checkpoint to i_path: the four fields (height, momentum_x, momentum_y, bathymetry) at full resolution plus the x/y coordinate variables and the scalar metadata needed to resume the run identically (simulation time, time-step counter and the constant time step dt). The file is created, fully written and closed in one call, so a reader always sees a complete checkpoint.

Parameters
i_pathoutput path for the checkpoint file.
i_hwater heights at the interior origin.
i_hux-momenta at the interior origin.
i_hvy-momenta at the interior origin.
i_bathymetrybathymetry at the interior origin.
i_simTimecurrent simulation time (seconds).
i_timeStepcurrent time-step counter.
i_dtconstant time step of the run (seconds).

◆ writeTimeStep()

void tsunami_lab::io::NetCdf::writeTimeStep ( t_real i_simTime,
t_real const * i_h,
t_real const * i_hu,
t_real const * i_hv )

Appends one record to the unlimited time dimension. Each of i_h, i_hu, i_hv must point at the interior origin of an array with the row stride passed to the constructor.

Parameters
i_simTimecurrent simulation time (seconds).
i_hwater heights at the interior origin.
i_hux-momenta at the interior origin.
i_hvy-momenta at the interior origin.

The documentation for this class was generated from the following files: