ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ReadCheckpointMF.H
Go to the documentation of this file.
1 #ifndef ERF_READ_CHECKPOINT_MF_H_
2 #define ERF_READ_CHECKPOINT_MF_H_
3 
4 #include <string>
5 
6 #include <AMReX_Algorithm.H>
7 #include <AMReX_Geometry.H>
8 #include <AMReX_IntVect.H>
9 #include <AMReX_MultiFab.H>
10 #include <AMReX_Periodicity.H>
11 #include <AMReX_VisMF.H>
12 
13 namespace erf_checkpoint {
14 
15 /**
16  * The periodicity of geom in x and y only. For planar fields, and for the terrain heights,
17  * which are not periodic in z even when the domain is: in a domain periodic in z, the top
18  * node of a column is the image of the bottom one but its height is not.
19  */
20 inline amrex::Periodicity
21 lateral_periodicity (const amrex::Geometry& geom)
22 {
23  const amrex::Box& dom = geom.Domain();
24  return amrex::Periodicity(amrex::IntVect(AMREX_D_DECL(geom.isPeriodic(0) ? dom.length(0) : 0,
25  geom.isPeriodic(1) ? dom.length(1) : 0,
26  0)));
27 }
28 
29 /**
30  * Fill the ghost layers of dst between ng_filled and ng_dst, which a checkpoint written with
31  * fewer ghost cells (for example before a change of advection scheme) could not supply.
32  *
33  * Every such cell first takes the value of the nearest cell within ng_filled of its own valid
34  * box, so that none is left undefined: outside the domain that is the outermost layer the file
35  * did hold. FillBoundary then gives the cells that lie over another box's valid region, or its
36  * periodic image, the value from there.
37  */
38 inline void
39 fill_unread_ghosts (amrex::MultiFab& dst, int dcomp, int ncomp,
40  const amrex::IntVect& ng_filled, const amrex::IntVect& ng_dst,
41  const amrex::Periodicity& period)
42 {
43  if (ng_filled.allGE(ng_dst)) { return; }
44 
45  for (amrex::MFIter mfi(dst); mfi.isValid(); ++mfi) {
46  const amrex::Box& vbx = mfi.validbox();
47  const amrex::Box gbx = amrex::grow(vbx, ng_dst);
48  const amrex::Box fbx = amrex::grow(vbx, ng_filled);
49  const amrex::Dim3 flo = amrex::lbound(fbx);
50  const amrex::Dim3 fhi = amrex::ubound(fbx);
51  const amrex::Array4<amrex::Real>& a = dst.array(mfi);
52  amrex::ParallelFor(gbx, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
53  {
54  const int ii = amrex::Clamp(i, flo.x, fhi.x);
55  const int jj = amrex::Clamp(j, flo.y, fhi.y);
56  const int kk = amrex::Clamp(k, flo.z, fhi.z);
57  if (ii != i || jj != j || kk != k) {
58  a(i,j,k,dcomp+n) = a(ii,jj,kk,dcomp+n);
59  }
60  });
61  }
62 
63  dst.FillBoundary(dcomp, ncomp, ng_dst, period);
64 }
65 
66 /**
67  * Read one checkpoint MultiFab and deposit it into dst, which may live on different grids.
68  *
69  * The source is read into an undefined MultiFab, which VisMF::Read then defines on the
70  * BoxArray recorded in the file, so no caller needs to know the checkpoint's grids -- which
71  * is what lets a restart land a component's state on level-0 grids that moved (issue 4225).
72  *
73  * Only the VALID cells of the source are offered by default: a file's ghost cells are not
74  * guaranteed to agree with its valid data, and ParallelCopy intersects the grown source with
75  * the grown destination without any ordering guarantee between overlapping sources, so
76  * offering them can land stale values in valid cells. Pass src_ghosts = true for a field
77  * whose halo the checkpoint carries and whose ordinary restart has always restored it; the
78  * valid cells are then laid over the top in a second pass, so they win wherever they reach.
79  * If the file holds fewer ghost cells than that, the layers it cannot supply are filled by
80  * fill_unread_ghosts rather than left undefined.
81  *
82  * @param[inout] dst destination, on the grids the run uses
83  * @param[in] mf_name the checkpoint MultiFab, as for VisMF::Read
84  * @param[in] scomp first component of the source to read
85  * @param[in] dcomp first component of dst to fill
86  * @param[in] ncomp number of components
87  * @param[in] dst_ghosts ghost cells of dst to fill (capped at what dst holds)
88  * @param[in] src_ghosts offer the file's ghost cells as well as its valid cells
89  * @param[in] period periodicity of the domain dst lives in
90  */
91 inline void
92 read_checkpoint_multifab (amrex::MultiFab& dst,
93  const std::string& mf_name,
94  int scomp, int dcomp, int ncomp,
95  const amrex::IntVect& dst_ghosts,
96  bool src_ghosts,
97  const amrex::Periodicity& period)
98 {
99  //
100  // Undefined on purpose: VisMF::Read defines it on the BoxArray the file records, which is
101  // the decomposition the checkpoint was written with and need not be this run's.
102  //
103  amrex::MultiFab src;
104  amrex::VisMF::Read(src, mf_name);
105 
106  //
107  // A file written with fewer ghost cells than the destination holds cannot fill more than
108  // it has, and the destination cannot take more than it holds.
109  //
110  const amrex::IntVect ng_dst = amrex::min(dst_ghosts, dst.nGrowVect());
111 
112  //
113  // The overwhelmingly common case: this level's grids ARE the grids the checkpoint was
114  // written on, so nothing has to move between boxes. Copy box to box, exactly as the
115  // reader did before the grids could differ, so that an ordinary restart stays
116  // byte-for-byte what it always was -- no periodic images, and no overlapping sources to
117  // be ambiguous about.
118  //
119  // The distribution still has to be matched: VisMF::Read gives the MultiFab it defines a
120  // DistributionMapping of its own, which is not this run's even when the BoxArray is.
121  // Redistribute is the box-to-box move for exactly that case.
122  //
123  if (src.boxArray() == dst.boxArray())
124  {
125  const amrex::IntVect ng_copy = src_ghosts ? amrex::min(ng_dst, src.nGrowVect())
126  : amrex::IntVect(0);
127 
128  if (src.DistributionMap() == dst.DistributionMap()) {
129  amrex::MultiFab::Copy(dst, src, scomp, dcomp, ncomp, ng_copy);
130  } else {
131  amrex::MultiFab aligned(src.boxArray(), dst.DistributionMap(), src.nComp(), src.nGrowVect());
132  aligned.Redistribute(src, 0, 0, src.nComp(), src.nGrowVect());
133  amrex::MultiFab::Copy(dst, aligned, scomp, dcomp, ncomp, ng_copy);
134  }
135  if (src_ghosts) { fill_unread_ghosts(dst, dcomp, ncomp, ng_copy, ng_dst, period); }
136  return;
137  }
138 
139  if (src_ghosts) {
140  //
141  // Two passes. The first offers the file's halo so that destination ghost cells OUTSIDE
142  // the domain -- which no valid cell can reach -- get the values the checkpoint recorded
143  // for them. But a file's halo is not guaranteed to agree with its valid data, and
144  // offering it alone would let a stale ghost cell land in a valid cell through a box
145  // overlap or a periodic image, with no ordering guarantee between overlapping sources.
146  // So the second pass copies the valid cells on top, and they win wherever they reach.
147  //
148  const amrex::IntVect ng_src = amrex::min(ng_dst, src.nGrowVect());
149  dst.ParallelCopy(src, scomp, dcomp, ncomp, ng_src, ng_dst, period);
150  dst.ParallelCopy(src, scomp, dcomp, ncomp, amrex::IntVect(0), ng_dst, period);
151  fill_unread_ghosts(dst, dcomp, ncomp, ng_src, ng_dst, period);
152  } else {
153  dst.ParallelCopy(src, scomp, dcomp, ncomp, amrex::IntVect(0), ng_dst, period);
154  }
155 }
156 
157 /**
158  * As read_checkpoint_multifab, for an integer field the checkpoint stores as a MultiFab,
159  * valid cells only. dst is (re)defined on ba/dm, which are the grids the run uses.
160  */
161 inline void
162 read_checkpoint_imultifab (amrex::iMultiFab& dst,
163  const amrex::BoxArray& ba,
164  const amrex::DistributionMapping& dm,
165  const std::string& mf_name,
166  const amrex::Periodicity& period)
167 {
168  amrex::MultiFab tmp(ba, dm, 1, 0);
169  read_checkpoint_multifab(tmp, mf_name, 0, 0, 1, amrex::IntVect(0), false, period);
170  dst = amrex::cast<amrex::iMultiFab>(tmp);
171 }
172 
173 } // namespace erf_checkpoint
174 
175 #endif
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
@ tmp
Definition: ERF_AdvanceWSM6.cpp:116
Definition: ERF_ReadCheckpointMF.H:13
void fill_unread_ghosts(amrex::MultiFab &dst, int dcomp, int ncomp, const amrex::IntVect &ng_filled, const amrex::IntVect &ng_dst, const amrex::Periodicity &period)
Definition: ERF_ReadCheckpointMF.H:39
void read_checkpoint_imultifab(amrex::iMultiFab &dst, const amrex::BoxArray &ba, const amrex::DistributionMapping &dm, const std::string &mf_name, const amrex::Periodicity &period)
Definition: ERF_ReadCheckpointMF.H:162
amrex::Periodicity lateral_periodicity(const amrex::Geometry &geom)
Definition: ERF_ReadCheckpointMF.H:21
void read_checkpoint_multifab(amrex::MultiFab &dst, const std::string &mf_name, int scomp, int dcomp, int ncomp, const amrex::IntVect &dst_ghosts, bool src_ghosts, const amrex::Periodicity &period)
Definition: ERF_ReadCheckpointMF.H:92