ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SBMStateManager.H
Go to the documentation of this file.
1 #ifndef ERF_SBM_STATE_MANAGER_H_
2 #define ERF_SBM_STATE_MANAGER_H_
3 
5 
6 #include <AMReX_MultiFab.H>
7 
8 #include <memory>
9 #include <optional>
10 #include <stdexcept>
11 #include <string>
12 #include <vector>
13 
14 namespace erf_sbm {
15 
16 /** Owns the authoritative old/new spectral state and its semantic times. */
18 public:
20 
21  [[nodiscard]] const SBMLayout& layout () const noexcept { return m_layout; }
22  [[nodiscard]] int nlevels () const noexcept { return static_cast<int>(m_levels.size()); }
23  [[nodiscard]] bool is_defined (int lev) const;
24 
25  void define (int lev, const amrex::BoxArray& grids,
26  const amrex::DistributionMapping& mapping,
27  double current_time, int ngrow = 0);
28  void destroy (int lev);
29 
30  /** Accepted state at the beginning of an active physical step. */
31  [[nodiscard]] const amrex::MultiFab& old_state (int lev) const;
32  [[nodiscard]] double old_time (int lev) const;
33  [[nodiscard]] bool old_valid (int lev) const;
34 
35  /** Accepted/current state. Throws while the target buffer is invalid. */
36  [[nodiscard]] const amrex::MultiFab& new_state (int lev) const;
37  [[nodiscard]] double new_time (int lev) const;
38  [[nodiscard]] bool new_valid (int lev) const;
39 
40  /** Mutable accepted state for initialization and synchronized restart I/O. */
41  [[nodiscard]] amrex::MultiFab& new_state_for_initialization (int lev);
42 
43  /** Mutable target storage for transport; semantic reads remain invalid until accepted. */
44  [[nodiscard]] amrex::MultiFab& new_target_storage (int lev);
45 
46  /** Rotate accepted new state into old state at the ERF::Advance seam. */
47  /** A fine-level caller may supply its actual dt to admit bounded
48  * scheduler-roundoff between consecutive substep endpoint expressions.
49  * Without it, owner time identity remains exact.
50  */
51  bool begin_step (int lev, double step_old_time, std::string& diagnostic,
52  std::optional<double> dt_min = std::nullopt);
53 
54  /** Publish a candidate accepted by the host stage sequence. */
55  bool accept_stage_target (int lev, double target_time,
56  bool physical_step_complete,
57  std::string& diagnostic);
58  /** Publish an admitted same-time AMR synchronization candidate. */
59  bool accept_synchronized_state (int lev,
60  const amrex::MultiFab& candidate,
61  double semantic_time,
62  std::string& diagnostic);
63  bool can_accept_synchronized_state (int lev,
64  const amrex::MultiFab& candidate,
65  double semantic_time,
66  std::string& diagnostic) const;
67  /** Commit a candidate after collective can_accept_synchronized_state succeeds. */
68  void publish_synchronized_state (int lev,
69  const amrex::MultiFab& candidate);
70  /** Collective check that a regrid candidate on new grids may replace a
71  * synchronized level: same component count and ghost width, same time. */
72  bool can_replace_level (int lev, const amrex::MultiFab& candidate,
73  double semantic_time, std::string& diagnostic) const;
74  /** Adopt an admitted regrid candidate as the level's accepted state on its
75  * new grids. The old view stays invalid until the next begin_step. */
76  void replace_level (int lev, amrex::MultiFab&& candidate,
77  double semantic_time);
78  [[nodiscard]] bool step_active (int lev) const;
79 
80  /** One-way projection from the spectrum into ERF's compact qc and qr lanes. */
81  void project_to_core (int lev, amrex::MultiFab& core,
82  int qc_component, int qr_component) const;
83 
84 private:
85  struct LevelState {
86  std::unique_ptr<amrex::MultiFab> old_state;
87  std::unique_ptr<amrex::MultiFab> new_state;
88  double old_time{0.0};
89  double new_time{0.0};
90  bool old_valid{false};
91  bool new_valid{false};
92  bool step_active{false};
93  };
94 
95  [[nodiscard]] LevelState& level (int lev);
96  [[nodiscard]] const LevelState& level (int lev) const;
97 
100  std::vector<LevelState> m_levels;
101 };
102 
103 } // namespace erf_sbm
104 
105 #endif
Definition: ERF_SBMBulkProjection.H:18
Definition: ERF_SBMLayout.H:94
Definition: ERF_SBMStateManager.H:17
bool old_valid(int lev) const
Definition: ERF_SBMStateManager.cpp:89
bool is_defined(int lev) const
Definition: ERF_SBMStateManager.cpp:35
double new_time(int lev) const
Definition: ERF_SBMStateManager.cpp:103
std::vector< LevelState > m_levels
Definition: ERF_SBMStateManager.H:100
SBMBulkProjection m_projection
Definition: ERF_SBMStateManager.H:99
void publish_synchronized_state(int lev, const amrex::MultiFab &candidate)
Definition: ERF_SBMStateManager.cpp:242
void project_to_core(int lev, amrex::MultiFab &core, int qc_component, int qr_component) const
Definition: ERF_SBMStateManager.cpp:313
void destroy(int lev)
Definition: ERF_SBMStateManager.cpp:69
SBMStateManager(SBMLayout layout, int nlevels)
Definition: ERF_SBMStateManager.cpp:16
bool new_valid(int lev) const
Definition: ERF_SBMStateManager.cpp:109
SBMLayout m_layout
Definition: ERF_SBMStateManager.H:98
bool accept_synchronized_state(int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic)
Definition: ERF_SBMStateManager.cpp:204
const SBMLayout & layout() const noexcept
Definition: ERF_SBMStateManager.H:21
int nlevels() const noexcept
Definition: ERF_SBMStateManager.H:22
bool step_active(int lev) const
Definition: ERF_SBMStateManager.cpp:308
const amrex::MultiFab & new_state(int lev) const
Definition: ERF_SBMStateManager.cpp:94
amrex::MultiFab & new_target_storage(int lev)
Definition: ERF_SBMStateManager.cpp:124
LevelState & level(int lev)
Definition: ERF_SBMStateManager.cpp:23
bool begin_step(int lev, double step_old_time, std::string &diagnostic, std::optional< double > dt_min=std::nullopt)
Definition: ERF_SBMStateManager.cpp:133
void replace_level(int lev, amrex::MultiFab &&candidate, double semantic_time)
Definition: ERF_SBMStateManager.cpp:283
bool accept_stage_target(int lev, double target_time, bool physical_step_complete, std::string &diagnostic)
Definition: ERF_SBMStateManager.cpp:184
void define(int lev, const amrex::BoxArray &grids, const amrex::DistributionMapping &mapping, double current_time, int ngrow=0)
Definition: ERF_SBMStateManager.cpp:41
bool can_replace_level(int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic) const
Definition: ERF_SBMStateManager.cpp:255
amrex::MultiFab & new_state_for_initialization(int lev)
Definition: ERF_SBMStateManager.cpp:114
const amrex::MultiFab & old_state(int lev) const
Definition: ERF_SBMStateManager.cpp:74
bool can_accept_synchronized_state(int lev, const amrex::MultiFab &candidate, double semantic_time, std::string &diagnostic) const
Definition: ERF_SBMStateManager.cpp:216
double old_time(int lev) const
Definition: ERF_SBMStateManager.cpp:83
Definition: ERF.H:128
Definition: ERF_SBMStateManager.H:85
std::unique_ptr< amrex::MultiFab > new_state
Definition: ERF_SBMStateManager.H:87
std::unique_ptr< amrex::MultiFab > old_state
Definition: ERF_SBMStateManager.H:86
bool new_valid
Definition: ERF_SBMStateManager.H:91
double new_time
Definition: ERF_SBMStateManager.H:89
bool old_valid
Definition: ERF_SBMStateManager.H:90
bool step_active
Definition: ERF_SBMStateManager.H:92
double old_time
Definition: ERF_SBMStateManager.H:88