ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_TerrainPoisson.H
Go to the documentation of this file.
1 /**
2  * \file ERF_TerrainPoisson.H
3  */
4 #ifndef ERF_TERRAIN_POISSON_H_
5 #define ERF_TERRAIN_POISSON_H_
6 
7 #include <AMReX_Geometry.H>
8 #include <AMReX_MultiFab.H>
9 #include <AMReX_BCRec.H>
10 
11 #ifdef ERF_USE_FFT
12 
14 #include <AMReX_FFT_Poisson.H>
15 
16 /**
17  * Matrix-vector interface for the terrain Poisson operator used by GMRES.
18  */
19 class TerrainPoisson
20 {
21 public:
22 
23  using RT = amrex::Real;
24 
25  /**
26  * Construct a terrain Poisson operator and its FFT preconditioner metadata.
27  *
28  * @param geom Geometry for the subdomain being solved
29  * @param ba BoxArray for operator and vector allocation
30  * @param dm DistributionMapping for operator and vector allocation
31  * @param domain_bcs_type Domain boundary-condition names
32  * @param stretched_dz_lev_d Device stretched-dz spacing for the level
33  * @param ax Terrain metric coefficient on x-faces
34  * @param ay Terrain metric coefficient on y-faces
35  * @param az Terrain metric coefficient on z-faces
36  * @param dJ Cell-centered Jacobian determinant
37  * @param z_phys_nd Node-centered physical height field
38  * @param use_real_bcs Whether real lateral boundary conditions are active
39  * @param mf_ux Map factor m_x on x-faces, for the slope terms of the z-face fluxes
40  * (null: one)
41  * @param mf_vy Map factor m_y on y-faces (null: one)
42  */
43  TerrainPoisson (amrex::Geometry const& geom, amrex::Geometry const& lev_geom,
44  amrex::BoxArray const& ba,
45  amrex::DistributionMapping const& dm,
46  amrex::Array<std::string,2*AMREX_SPACEDIM>& domain_bcs_type,
47  amrex::Gpu::DeviceVector<amrex::Real>& stretched_dz_lev_d,
48  const amrex::MultiFab& ax, const amrex::MultiFab& ay,
49  const amrex::MultiFab& az, const amrex::MultiFab& dJ,
50  amrex::MultiFab const* z_phys_nd,
51  bool use_real_bcs,
52  amrex::MultiFab const* mf_ux = nullptr,
53  amrex::MultiFab const* mf_vy = nullptr);
54 
55  /**
56  * Apply the terrain Poisson operator.
57  *
58  * @param lhs Destination for the operator result
59  * @param rhs Input vector with ghost cells available for boundary fills
60  */
61  void apply (amrex::MultiFab& lhs, amrex::MultiFab const& rhs);
62 
63  /**
64  * Fill ghost cells according to the configured Poisson boundary conditions.
65  *
66  * @param phi Field whose ghost cells are updated
67  */
68  void apply_bcs (amrex::MultiFab& phi);
69 
70  /**
71  * Enable or disable the FFT preconditioner.
72  *
73  * @param precond_flag True to use the FFT preconditioner
74  */
75  void usePrecond (bool precond_flag);
76 
77  /**
78  * Faces of the solve region whose flux is zero (its Neumann faces), as a sum of
79  * TerrainPoissonFaceBit values.
80  *
81  * @return The face mask
82  */
83  [[nodiscard]] int zeroFluxFaces () const { return m_zero_flux_faces; }
84 
85  /**
86  * Compute face-centered terrain Poisson fluxes from a solution field.
87  *
88  * @param phi Solution field used to compute gradients
89  * @param fluxes Face-centered fluxes to fill
90  */
91  void getFluxes (amrex::MultiFab& phi, amrex::Array<amrex::MultiFab,AMREX_SPACEDIM>& fluxes);
92 
93  /**
94  * Copy one vector into another.
95  *
96  * @param lhs Destination vector
97  * @param rhs Source vector
98  */
99  void assign (amrex::MultiFab& lhs, amrex::MultiFab const& rhs);
100 
101  /**
102  * Scale a vector in place.
103  *
104  * @param lhs Vector to scale
105  * @param fac Multiplicative scale factor
106  */
107  void scale (amrex::MultiFab& lhs, amrex::Real fac);
108 
109  /**
110  * Compute the vector dot product.
111  *
112  * @param v1 First input vector
113  * @param v2 Second input vector
114  * @return Dot product of v1 and v2
115  */
116  amrex::Real dotProduct (amrex::MultiFab const& v1, amrex::MultiFab const& v2);
117 
118  /**
119  * Add a scaled vector into another vector.
120  *
121  * @param lhs Destination vector to increment
122  * @param rhs Source vector
123  * @param a Scale factor applied to rhs
124  */
125  void increment (amrex::MultiFab& lhs, amrex::MultiFab const& rhs, amrex::Real a);
126 
127  /**
128  * Compute a linear combination of two source vectors.
129  *
130  * @param lhs Destination vector
131  * @param a Scale factor applied to rhs_a
132  * @param rhs_a First source vector
133  * @param b Scale factor applied to rhs_b
134  * @param rhs_b Second source vector
135  */
136  void linComb (amrex::MultiFab& lhs, amrex::Real a, amrex::MultiFab const& rhs_a,
137  amrex::Real b, amrex::MultiFab const& rhs_b);
138 
139  /**
140  * Allocate a vector with the right-hand-side ghost-cell layout.
141  *
142  * @return Newly allocated right-hand-side vector
143  */
144  amrex::MultiFab makeVecRHS ();
145 
146  /**
147  * Allocate a vector with the solution ghost-cell layout.
148  *
149  * @return Newly allocated solution vector
150  */
151  amrex::MultiFab makeVecLHS ();
152 
153  /**
154  * Compute the L2 norm of a vector.
155  *
156  * @param v Input vector
157  * @return L2 norm of v
158  */
159  amrex::Real norm2 (amrex::MultiFab const& v);
160 
161  /**
162  * Apply the configured preconditioner.
163  *
164  * @param lhs Destination for the preconditioned result
165  * @param rhs Input vector to precondition
166  */
167  void precond (amrex::MultiFab& lhs, amrex::MultiFab const& rhs);
168 
169  /**
170  * Set a vector to zero.
171  *
172  * @param v Vector to zero
173  */
174  void setToZero (amrex::MultiFab& v);
175 
176 private:
177  bool m_use_precond = false;
178  amrex::Geometry m_geom;
179  amrex::BoxArray m_grids;
180  amrex::DistributionMapping m_dmap;
181  amrex::Array<std::string,2*AMREX_SPACEDIM> m_domain_bcs_type;
182  amrex::Gpu::DeviceVector<amrex::Real> m_stretched_dz_d;
183  const amrex::MultiFab& m_ax;
184  const amrex::MultiFab& m_ay;
185  const amrex::MultiFab& m_az;
186  const amrex::MultiFab& m_dJ;
187  const amrex::MultiFab* m_zphys;
188  const amrex::MultiFab* m_mf_ux;
189  const amrex::MultiFab* m_mf_vy;
190 
191  std::unique_ptr<amrex::FFT::PoissonHybrid<amrex::MultiFab>> m_2D_fft_precond;
192  amrex::Array<std::pair<amrex::FFT::Boundary,amrex::FFT::Boundary>,AMREX_SPACEDIM> bc_fft;
193 
194  //! Faces of the solve region with zero flux (TerrainPoissonFaceBit values)
195  int m_zero_flux_faces = 0;
196 };
197 
198 #endif
199 #endif
auto apply_bcs
Definition: ERF_TI_utils.H:46
void apply(const amrex::Box &bx, const amrex::Box &domain, const int quantity, const int flux_comp, const amrex::Array4< const amrex::Real > &state, const amrex::Array4< const amrex::Real > &prim, const amrex::Array4< const amrex::Real > &base_state, const amrex::Array4< const amrex::Real > &u, const amrex::Array4< const amrex::Real > &v, const amrex::Array4< const amrex::Real > &w, const amrex::Array4< amrex::Real > &rhs, const amrex::Array4< amrex::Real > &xflux, const amrex::Array4< amrex::Real > &yflux, const amrex::Array4< amrex::Real > &zflux, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > &dx_inv, const erf_wall_thermodynamics::Boundary &walls, const amrex::Real alpha_T, const amrex::Real alpha_C, const amrex::Real rdOcp, const bool cloudy=false, const erf_wall_thermodynamics::SidewallBandViews &bands={})
Definition: ERF_CloudChamberWallFlux.H:1278
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real phi(amrex::Real d, amrex::Real z0, amrex::Real psi) noexcept
Definition: ERF_ImmersedWallCell.H:176