ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_sbm::SBMStateManager Class Reference

#include <ERF_SBMStateManager.H>

Collaboration diagram for erf_sbm::SBMStateManager:

Classes

struct  LevelState
 

Public Member Functions

 SBMStateManager (SBMLayout layout, int nlevels)
 
const SBMLayout & layout () const noexcept
 
int nlevels () const noexcept
 
bool is_defined (int lev) const
 
void define (int lev, const amrex::BoxArray &grids, const amrex::DistributionMapping &mapping, double current_time, int ngrow=0)
 
void destroy (int lev)
 
const amrex::MultiFab & old_state (int lev) const
 
double old_time (int lev) const
 
bool old_valid (int lev) const
 
const amrex::MultiFab & new_state (int lev) const
 
double new_time (int lev) const
 
bool new_valid (int lev) const
 
amrex::MultiFab & new_state_for_initialization (int lev)
 
amrex::MultiFab & new_target_storage (int lev)
 
bool begin_step (int lev, double step_old_time, std::string &diagnostic, std::optional< double > dt_min=std::nullopt)
 
bool accept_stage_target (int lev, double target_time, bool physical_step_complete, std::string &diagnostic)
 
bool accept_synchronized_state (int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic)
 
bool can_accept_synchronized_state (int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic) const
 
void publish_synchronized_state (int lev, const amrex::MultiFab &candidate)
 
bool can_replace_level (int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic) const
 
void replace_level (int lev, amrex::MultiFab &&candidate, double semantic_time)
 
bool step_active (int lev) const
 
void project_to_core (int lev, amrex::MultiFab &core, int qc_component, int qr_component) const
 

Private Member Functions

LevelState & level (int lev)
 
const LevelState & level (int lev) const
 

Private Attributes

SBMLayout m_layout
 
SBMBulkProjection m_projection
 
std::vector< LevelState > m_levels
 

Detailed Description

Owns the authoritative old/new spectral state and its semantic times.

Constructor & Destructor Documentation

◆ SBMStateManager()

erf_sbm::SBMStateManager::SBMStateManager ( SBMLayout  layout,
int  nlevels 
)
17  : m_layout(std::move(layout)), m_projection(m_layout)
18 {
19  if (nlevels <= 0) throw std::invalid_argument("SBM state manager needs at least one level");
20  m_levels.resize(static_cast<std::size_t>(nlevels));
21 }
std::vector< LevelState > m_levels
Definition: ERF_SBMStateManager.H:100
SBMBulkProjection m_projection
Definition: ERF_SBMStateManager.H:99
SBMLayout m_layout
Definition: ERF_SBMStateManager.H:98
const SBMLayout & layout() const noexcept
Definition: ERF_SBMStateManager.H:21
int nlevels() const noexcept
Definition: ERF_SBMStateManager.H:22
Here is the call graph for this function:

Member Function Documentation

◆ accept_stage_target()

bool erf_sbm::SBMStateManager::accept_stage_target ( int  lev,
double  target_time,
bool  physical_step_complete,
std::string &  diagnostic 
)

Publish a candidate accepted by the host stage sequence.

187 {
188  diagnostic.clear();
189  auto& state = level(lev);
190  if (!state.old_state || !state.new_state || !state.step_active || !state.old_valid) {
191  diagnostic = "SBM stage target cannot be accepted without an active old/new lifecycle";
192  return false;
193  }
194  if (!std::isfinite(target_time)) {
195  diagnostic = "SBM stage target semantic time is nonfinite";
196  return false;
197  }
198  state.new_time = target_time;
199  state.new_valid = true;
200  if (physical_step_complete) state.step_active = false;
201  return true;
202 }
LevelState & level(int lev)
Definition: ERF_SBMStateManager.cpp:23

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ accept_synchronized_state()

bool erf_sbm::SBMStateManager::accept_synchronized_state ( int  lev,
const amrex::MultiFab &  candidate,
double  semantic_time,
std::string &  diagnostic 
)

Publish an admitted same-time AMR synchronization candidate.

207 {
208  if (!can_accept_synchronized_state(lev, candidate, semantic_time,
209  diagnostic)) {
210  return false;
211  }
212  publish_synchronized_state(lev, candidate);
213  return true;
214 }
void publish_synchronized_state(int lev, const amrex::MultiFab &candidate)
Definition: ERF_SBMStateManager.cpp:242
bool can_accept_synchronized_state(int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic) const
Definition: ERF_SBMStateManager.cpp:216
Here is the call graph for this function:

◆ begin_step()

bool erf_sbm::SBMStateManager::begin_step ( int  lev,
double  step_old_time,
std::string &  diagnostic,
std::optional< double >  dt_min = std::nullopt 
)

Rotate accepted new state into old state at the ERF::Advance seam. A fine-level caller may supply its actual dt to admit bounded scheduler-roundoff between consecutive substep endpoint expressions. Without it, owner time identity remains exact.

136 {
137  diagnostic.clear();
138  auto& state = level(lev);
139  if (!state.old_state || !state.new_state) {
140  diagnostic = "SBM begin-step requested for an undefined level";
141  return false;
142  }
143  if (!std::isfinite(step_old_time)) {
144  diagnostic = "SBM begin-step time is nonfinite";
145  return false;
146  }
147  if (state.step_active) {
148  diagnostic = "SBM begin-step requested while the prior physical step is active";
149  return false;
150  }
151  if (!state.new_valid) {
152  diagnostic = "SBM begin-step has no accepted current/new spectrum";
153  return false;
154  }
155  const bool same_time = dt_min.has_value()
156  ? semantic_time_matches(state.new_time, step_old_time, *dt_min)
157  : semantic_time_matches_exact(state.new_time, step_old_time);
158  if (!same_time) {
159  std::ostringstream message;
160  message.precision(17);
161  message << "SBM begin-step time mismatch at level " << lev
162  << ": expected host step-old time " << step_old_time
163  << ", accepted spectral time " << state.new_time
164  << ", lifecycle=accepted new state, step inactive";
165  diagnostic = message.str();
166  return false;
167  }
168 
169  // Match ERF::Advance: rotate storage by swapping ownership, never by
170  // copying the full spectral state. When the host scheduler's current
171  // step-start expression was admitted as the same endpoint within the
172  // bounded fine-step roundoff test above, the rotated old FAB now owns
173  // that step-start semantic label. The spectrum values are unchanged;
174  // this only keeps the owner timestamp in lockstep with S_old/t_old at
175  // stage 0. With no dt_min, the exact-time path above still applies.
176  std::swap(state.old_state, state.new_state);
177  state.old_time = step_old_time;
178  state.old_valid = true;
179  state.new_valid = false;
180  state.step_active = true;
181  return true;
182 }
std::string message
Definition: ERF_InputPreflight.cpp:25
double new_time(int lev) const
Definition: ERF_SBMStateManager.cpp:103
bool semantic_time_matches(const double lhs, const double rhs, const double dt_min) noexcept
Definition: ERF_SBMAMRTime.H:14
bool semantic_time_matches_exact(const double lhs, const double rhs) noexcept
Definition: ERF_SBMAMRTime.H:32
Here is the call graph for this function:

◆ can_accept_synchronized_state()

bool erf_sbm::SBMStateManager::can_accept_synchronized_state ( int  lev,
const amrex::MultiFab &  candidate,
double  semantic_time,
std::string &  diagnostic 
) const
219 {
220  diagnostic.clear();
221  const auto& state = level(lev);
222  const double scale = std::max({1.0, std::abs(semantic_time),
223  std::abs(state.new_time)});
224  const bool time_matches = std::isfinite(semantic_time) &&
225  std::abs(semantic_time - state.new_time) <=
226  64.0 * std::numeric_limits<double>::epsilon() * scale;
227  const bool local_ok = state.new_state != nullptr && state.new_valid &&
228  !state.step_active && time_matches &&
229  candidate.nComp() == m_layout.ncomp() &&
230  erf_auxiliary::SameCellLayout(candidate, *state.new_state);
231  int vote = local_ok ? 1 : 0;
232  amrex::ParallelDescriptor::ReduceIntMin(vote);
233  if (vote == 0) {
234  diagnostic = local_ok
235  ? "SBM synchronized candidate is invalid on another MPI rank"
236  : "SBM synchronized candidate must match the accepted level layout and time";
237  return false;
238  }
239  return true;
240 }
int ncomp() const noexcept
Definition: ERF_SBMLayout.H:103
bool SameCellLayout(const amrex::MultiFab &lhs, const amrex::MultiFab &rhs)
Definition: ERF_AuxiliaryMappedTransport.cpp:33
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12

Referenced by accept_synchronized_state().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ can_replace_level()

bool erf_sbm::SBMStateManager::can_replace_level ( int  lev,
const amrex::MultiFab &  candidate,
double  semantic_time,
std::string &  diagnostic 
) const

Collective check that a regrid candidate on new grids may replace a synchronized level: same component count and ghost width, same time.

258 {
259  diagnostic.clear();
260  const auto& state = level(lev);
261  const double scale = std::max({1.0, std::abs(semantic_time),
262  std::abs(state.new_time)});
263  const bool time_matches = std::isfinite(semantic_time) &&
264  std::abs(semantic_time - state.new_time) <=
265  64.0 * std::numeric_limits<double>::epsilon() * scale;
266  const bool local_ok = state.old_state != nullptr &&
267  state.new_state != nullptr && state.new_valid && !state.step_active &&
268  time_matches && candidate.ok() &&
269  candidate.nComp() == m_layout.ncomp() &&
270  candidate.nGrowVect() == state.new_state->nGrowVect();
271  int vote = local_ok ? 1 : 0;
272  amrex::ParallelDescriptor::ReduceIntMin(vote);
273  if (vote == 0) {
274  diagnostic = local_ok
275  ? "SBM regrid candidate is invalid on another MPI rank"
276  : "SBM regrid candidate must replace a synchronized accepted level "
277  "with the same components, ghost width, and semantic time";
278  return false;
279  }
280  return true;
281 }
Here is the call graph for this function:

◆ define()

void erf_sbm::SBMStateManager::define ( int  lev,
const amrex::BoxArray &  grids,
const amrex::DistributionMapping &  mapping,
double  current_time,
int  ngrow = 0 
)
44 {
45  if (ngrow < 0) throw std::invalid_argument("SBM ghost width cannot be negative");
46  if (!std::isfinite(current_time)) {
47  throw std::invalid_argument("SBM current semantic time must be finite");
48  }
49  auto& state = level(lev);
50  if (state.old_state || state.new_state) {
51  throw std::logic_error("SBM level state is already defined");
52  }
53 
54  state.old_state = std::make_unique<amrex::MultiFab>(
55  grids, mapping, m_layout.ncomp(), ngrow);
56  state.new_state = std::make_unique<amrex::MultiFab>(
57  grids, mapping, m_layout.ncomp(), ngrow);
58  // Both buffers are deterministic. Zero is a valid physical spectrum;
59  // validity is carried only by the semantic flags below.
60  state.old_state->setVal(amrex::Real(0.0));
61  state.new_state->setVal(amrex::Real(0.0));
62  state.old_valid = false;
63  state.new_valid = true;
64  state.old_time = 0.0;
65  state.new_time = current_time;
66  state.step_active = false;
67 }
Here is the call graph for this function:

◆ destroy()

void erf_sbm::SBMStateManager::destroy ( int  lev)
70 {
71  level(lev) = LevelState{};
72 }
Here is the call graph for this function:

◆ is_defined()

bool erf_sbm::SBMStateManager::is_defined ( int  lev) const
36 {
37  const auto& state = level(lev);
38  return state.old_state != nullptr && state.new_state != nullptr;
39 }

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout()

const SBMLayout& erf_sbm::SBMStateManager::layout ( ) const
inlinenoexcept
21 { return m_layout; }

◆ level() [1/2]

SBMStateManager::LevelState & erf_sbm::SBMStateManager::level ( int  lev)
private
24 {
25  if (lev < 0 || lev >= nlevels()) throw std::out_of_range("SBM level is outside the manager");
26  return m_levels[static_cast<std::size_t>(lev)];
27 }

Referenced by accept_stage_target(), begin_step(), can_accept_synchronized_state(), can_replace_level(), define(), destroy(), is_defined(), new_state(), new_state_for_initialization(), new_target_storage(), new_time(), new_valid(), old_state(), old_time(), old_valid(), publish_synchronized_state(), replace_level(), and step_active().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ level() [2/2]

const SBMStateManager::LevelState & erf_sbm::SBMStateManager::level ( int  lev) const
private
30 {
31  if (lev < 0 || lev >= nlevels()) throw std::out_of_range("SBM level is outside the manager");
32  return m_levels[static_cast<std::size_t>(lev)];
33 }
Here is the call graph for this function:

◆ new_state()

const amrex::MultiFab & erf_sbm::SBMStateManager::new_state ( int  lev) const

Accepted/current state. Throws while the target buffer is invalid.

95 {
96  const auto& state = level(lev);
97  if (!state.new_state || !state.new_valid) {
98  throw std::logic_error("SBM new spectral view is invalid until a stage is accepted");
99  }
100  return *state.new_state;
101 }

Referenced by erf_sbm::SBMTransport::advance_stage(), new_time(), and project_to_core().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_state_for_initialization()

amrex::MultiFab & erf_sbm::SBMStateManager::new_state_for_initialization ( int  lev)

Mutable accepted state for initialization and synchronized restart I/O.

115 {
116  auto& state = level(lev);
117  if (!state.new_state || !state.new_valid || state.old_valid || state.step_active) {
118  throw std::logic_error(
119  "SBM accepted new state can be edited only before the first physical step");
120  }
121  return *state.new_state;
122 }
Here is the call graph for this function:

◆ new_target_storage()

amrex::MultiFab & erf_sbm::SBMStateManager::new_target_storage ( int  lev)

Mutable target storage for transport; semantic reads remain invalid until accepted.

125 {
126  auto& state = level(lev);
127  if (!state.new_state || !state.step_active) {
128  throw std::logic_error("SBM new target storage requires an active physical step");
129  }
130  return *state.new_state;
131 }

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_time()

double erf_sbm::SBMStateManager::new_time ( int  lev) const
104 {
105  static_cast<void>(new_state(lev));
106  return level(lev).new_time;
107 }
const amrex::MultiFab & new_state(int lev) const
Definition: ERF_SBMStateManager.cpp:94
double new_time
Definition: ERF_SBMStateManager.H:89

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_valid()

bool erf_sbm::SBMStateManager::new_valid ( int  lev) const
110 {
111  return level(lev).new_state != nullptr && level(lev).new_valid;
112 }
std::unique_ptr< amrex::MultiFab > new_state
Definition: ERF_SBMStateManager.H:87
bool new_valid
Definition: ERF_SBMStateManager.H:91

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nlevels()

int erf_sbm::SBMStateManager::nlevels ( ) const
inlinenoexcept
22 { return static_cast<int>(m_levels.size()); }

Referenced by level(), and SBMStateManager().

Here is the caller graph for this function:

◆ old_state()

const amrex::MultiFab & erf_sbm::SBMStateManager::old_state ( int  lev) const

Accepted state at the beginning of an active physical step.

75 {
76  const auto& state = level(lev);
77  if (!state.old_state || !state.old_valid) {
78  throw std::logic_error("SBM old spectral view is invalid at this lifecycle point");
79  }
80  return *state.old_state;
81 }

Referenced by erf_sbm::SBMTransport::advance_stage(), and old_time().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ old_time()

double erf_sbm::SBMStateManager::old_time ( int  lev) const
84 {
85  static_cast<void>(old_state(lev));
86  return level(lev).old_time;
87 }
const amrex::MultiFab & old_state(int lev) const
Definition: ERF_SBMStateManager.cpp:74
double old_time
Definition: ERF_SBMStateManager.H:88

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ old_valid()

bool erf_sbm::SBMStateManager::old_valid ( int  lev) const
90 {
91  return level(lev).old_state != nullptr && level(lev).old_valid;
92 }
std::unique_ptr< amrex::MultiFab > old_state
Definition: ERF_SBMStateManager.H:86
bool old_valid
Definition: ERF_SBMStateManager.H:90

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ project_to_core()

void erf_sbm::SBMStateManager::project_to_core ( int  lev,
amrex::MultiFab &  core,
int  qc_component,
int  qr_component 
) const

One-way projection from the spectrum into ERF's compact qc and qr lanes.

316 {
317  const auto& spectrum = new_state(lev);
318  if (qc_component < 0 || qr_component < 0 ||
319  qc_component >= core.nComp() || qr_component >= core.nComp()) {
320  throw std::invalid_argument("SBM compact projection target is outside core state");
321  }
322  if (!(spectrum.boxArray() == core.boxArray()) ||
323  !(spectrum.DistributionMap() == core.DistributionMap())) {
324  throw std::invalid_argument("SBM spectrum and core state must share a level layout");
325  }
326  for (amrex::MFIter mfi(core); mfi.isValid(); ++mfi) {
327  m_projection.apply_to_core(mfi.validbox(), spectrum.const_array(mfi),
328  core.array(mfi), qc_component, qr_component);
329  }
330 }
void apply_to_core(const amrex::Box &box, const amrex::Array4< const amrex::Real > &auxiliary, const amrex::Array4< amrex::Real > &core, int qc_component, int qr_component) const
Definition: ERF_SBMBulkProjection.cpp:43

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ publish_synchronized_state()

void erf_sbm::SBMStateManager::publish_synchronized_state ( int  lev,
const amrex::MultiFab &  candidate 
)

Commit a candidate after collective can_accept_synchronized_state succeeds.

244 {
245  auto& state = level(lev);
246  AMREX_ALWAYS_ASSERT(state.new_state != nullptr && state.new_valid &&
247  !state.step_active &&
248  candidate.nComp() == m_layout.ncomp() &&
250  *state.new_state));
251  amrex::MultiFab::Copy(*state.new_state, candidate, 0, 0,
252  m_layout.ncomp(), 0);
253 }
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)

Referenced by accept_synchronized_state().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replace_level()

void erf_sbm::SBMStateManager::replace_level ( int  lev,
amrex::MultiFab &&  candidate,
double  semantic_time 
)

Adopt an admitted regrid candidate as the level's accepted state on its new grids. The old view stays invalid until the next begin_step.

286 {
287  auto& state = level(lev);
288  AMREX_ALWAYS_ASSERT(state.old_state != nullptr &&
289  state.new_state != nullptr && state.new_valid &&
290  !state.step_active && candidate.ok() &&
291  candidate.nComp() == m_layout.ncomp() &&
292  candidate.nGrowVect() == state.new_state->nGrowVect() &&
293  std::isfinite(semantic_time));
294  // Release the old-grid storage before allocating the new old buffer.
295  state.old_state.reset();
296  state.new_state = std::make_unique<amrex::MultiFab>(std::move(candidate));
297  state.old_state = std::make_unique<amrex::MultiFab>(
298  state.new_state->boxArray(), state.new_state->DistributionMap(),
299  m_layout.ncomp(), state.new_state->nGrowVect());
300  state.old_state->setVal(amrex::Real(0.0));
301  state.old_valid = false;
302  state.old_time = 0.0;
303  state.new_valid = true;
304  state.new_time = semantic_time;
305  state.step_active = false;
306 }
Here is the call graph for this function:

◆ step_active()

bool erf_sbm::SBMStateManager::step_active ( int  lev) const
309 {
310  return level(lev).step_active;
311 }
bool step_active
Definition: ERF_SBMStateManager.H:92

Referenced by erf_sbm::SBMTransport::advance_stage().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_layout

◆ m_levels

std::vector<LevelState> erf_sbm::SBMStateManager::m_levels
private

Referenced by level(), nlevels(), and SBMStateManager().

◆ m_projection

SBMBulkProjection erf_sbm::SBMStateManager::m_projection
private

Referenced by project_to_core().


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