Tsunami Project Lab
Loading...
Searching...
No Matches
NetCdf.h
Go to the documentation of this file.
1
23#ifndef TSUNAMI_LAB_IO_NETCDF_H
24#define TSUNAMI_LAB_IO_NETCDF_H
25
26#include "../constants.h"
27#include <cstddef>
28#include <string>
29#include <vector>
30
31namespace tsunami_lab {
32 namespace io {
33 class NetCdf;
34 }
35}
36
38 private:
40 int m_ncId = -1;
41
43 int m_dimTime = -1;
44 int m_dimY = -1;
45 int m_dimX = -1;
46
48 int m_varTime = -1;
49 int m_varY = -1;
50 int m_varX = -1;
51 int m_varH = -1;
52 int m_varHu = -1;
53 int m_varHv = -1;
54 int m_varB = -1;
55
57 t_idx m_nx = 0;
58 t_idx m_ny = 0;
59 t_idx m_stride = 0;
60
62 t_real m_dxy = 0;
63
65 std::size_t m_timeIdx = 0;
66
69 t_idx m_k = 1;
70
75 static void check( int i_status, char const * i_what );
76
82 void writeSlice( int i_varId,
83 t_real const * i_data );
84
89 static t_idx fast_div_ceil(t_idx a, t_idx b) {
90 return (a + b - 1) / b;
91 }
92 public:
113 NetCdf( std::string i_path,
114 t_real i_dxy,
115 t_idx i_nx,
116 t_idx i_ny,
117 t_idx i_stride,
118 t_real const * i_bathymetry,
119 t_idx i_k );
120
122 struct Append {};
123
139 NetCdf( Append,
140 std::string i_path,
141 t_real i_dxy,
142 t_idx i_nx,
143 t_idx i_ny,
144 t_idx i_stride,
145 t_idx i_k );
146
151 ~NetCdf();
152
153 NetCdf( NetCdf const & ) = delete;
154 NetCdf & operator=( NetCdf const & ) = delete;
155
166 void writeTimeStep( t_real i_simTime,
167 t_real const * i_h,
168 t_real const * i_hu,
169 t_real const * i_hv );
170
174 std::size_t records() const { return m_timeIdx; }
175
176 // ---------------- input side (assignment 5.2) ----------------------
177
184 struct Grid {
185 t_idx nx = 0;
186 t_idx ny = 0;
187 std::vector<t_real> x;
188 std::vector<t_real> y;
189 std::vector<t_real> z;
190
197 bool inBounds( t_real i_x, t_real i_y ) const {
198 return !x.empty() && !y.empty()
199 && i_x >= x.front() && i_x <= x.back()
200 && i_y >= y.front() && i_y <= y.back();
201 }
202
213 t_real sampleNearest( t_real i_x, t_real i_y ) const;
214 };
215
225 static Grid read( std::string i_path );
226
227 // -------------- checkpoint restart side (assignment 7.1) -----------
228
247 void writeCheckpoint( std::string i_path,
248 t_real const * i_h,
249 t_real const * i_hu,
250 t_real const * i_hv,
251 t_real const * i_bathymetry,
252 t_real i_simTime,
253 t_idx i_timeStep,
254 t_real i_dt );
255
262 struct Checkpoint {
269 t_real dt = 0;
270
271 t_idx nx() const { return b.nx; }
272 t_idx ny() const { return b.ny; }
273 t_real dxy() const { return b.x.size() > 1 ? b.x[1] - b.x[0] : 0; }
274 };
275
279 static bool hasCheckpoint( std::string i_path );
280
289 static Checkpoint readCheckpoint( std::string i_path );
290};
291
292#endif
Definition NetCdf.h:37
~NetCdf()
Definition NetCdf.cpp:151
static Checkpoint readCheckpoint(std::string i_path)
Definition NetCdf.cpp:369
NetCdf & operator=(NetCdf const &)=delete
std::size_t records() const
Definition NetCdf.h:174
static Grid read(std::string i_path)
Definition NetCdf.cpp:226
void writeTimeStep(t_real i_simTime, t_real const *i_h, t_real const *i_hu, t_real const *i_hv)
Definition NetCdf.cpp:187
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)
Definition NetCdf.cpp:294
static bool hasCheckpoint(std::string i_path)
Definition NetCdf.cpp:361
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)
Definition NetCdf.cpp:28
NetCdf(NetCdf const &)=delete
Definition constants.h:12
float t_real
floating point type
Definition constants.h:17
std::size_t t_idx
integral type for cell-ids, pointer arithmetic, etc.
Definition constants.h:14
tag type selecting the append constructor (assignment 7.1 resume)
Definition NetCdf.h:122
Grid hv
y-momentum
Definition NetCdf.h:266
Grid h
water height
Definition NetCdf.h:264
t_real dxy() const
cell size (m)
Definition NetCdf.h:273
t_idx timeStep
time-step counter of the checkpoint
Definition NetCdf.h:268
Grid b
bathymetry
Definition NetCdf.h:263
t_idx nx() const
interior cells in x
Definition NetCdf.h:271
t_idx ny() const
interior cells in y
Definition NetCdf.h:272
t_real simTime
simulation time of the checkpoint (s)
Definition NetCdf.h:267
t_real dt
constant time step of the run (s)
Definition NetCdf.h:269
Grid hu
x-momentum
Definition NetCdf.h:265
Definition NetCdf.h:184
bool inBounds(t_real i_x, t_real i_y) const
Definition NetCdf.h:197
std::vector< t_real > x
monotonically ascending x (size nx)
Definition NetCdf.h:187
t_idx nx
number of x-coordinates
Definition NetCdf.h:185
std::vector< t_real > z
2d field in (y, x) order (size nx * ny)
Definition NetCdf.h:189
t_idx ny
number of y-coordinates
Definition NetCdf.h:186
t_real sampleNearest(t_real i_x, t_real i_y) const
Definition NetCdf.cpp:206
std::vector< t_real > y
monotonically ascending y (size ny)
Definition NetCdf.h:188