Loading...
Searching...
No Matches
laplace_simple.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "../../quadrature/quadrature.hpp"
6#include "dense/vec.hpp"
9#include "grid/grid_types.hpp"
11#include "linalg/operator.hpp"
12#include "linalg/vector.hpp"
13#include "linalg/vector_q1.hpp"
14
16
17template < typename ScalarT >
19{
20 public:
23 using ScalarType = ScalarT;
25
26 static constexpr int LocalMatrixDim = 6;
27
28 private:
29 bool storeLMatrices_ =
30 false; // set to let apply_impl() know, that it should store the local matrices after assembling them
31 bool applyStoredLMatrices_ =
32 false; // set to make apply_impl() load and use the stored LMatrices for the operator application
33 Grid4DDataLocalMatrices lmatrices_;
34
36
39
40 bool treat_boundary_;
41 bool diagonal_;
42 bool single_quadpoint_ = false;
43
44 linalg::OperatorApplyMode operator_apply_mode_;
45 linalg::OperatorCommunicationMode operator_communication_mode_;
46
49
52
54 ScalarT quad_weights_3x2_[quadrature::quad_felippa_3x2_num_quad_points];
56 ScalarT quad_weights_1x1_[quadrature::quad_felippa_1x1_num_quad_points];
57
58 public:
63 bool treat_boundary,
64 bool diagonal,
65 bool single_quadpoint = false,
67 linalg::OperatorCommunicationMode operator_communication_mode =
69 : domain_( domain )
70 , grid_( grid )
71 , radii_( radii )
72 , treat_boundary_( treat_boundary )
73 , diagonal_( diagonal )
74 , single_quadpoint_( single_quadpoint )
75 , operator_apply_mode_( operator_apply_mode )
76 , operator_communication_mode_( operator_communication_mode )
77 // TODO: we can reuse the send and recv buffers and pass in from the outside somehow
78 , send_buffers_( domain )
79 , recv_buffers_( domain )
80 {
85 }
86
87 /// @brief Getter for domain member
88 const grid::shell::DistributedDomain& get_domain() const { return domain_; }
89
90 /// @brief Getter for radii member
92
93 /// @brief Getter for grid member
95
96 /// @brief S/Getter for diagonal member
97 void set_diagonal( bool v ) { diagonal_ = v; }
98
99 /// @brief S/Getter for quadpoint member
100 void set_single_quadpoint( bool v ) { single_quadpoint_ = v; }
101
102 /// @brief Retrives the local matrix stored in the operator
103 KOKKOS_INLINE_FUNCTION
105 const int local_subdomain_id,
106 const int x_cell,
107 const int y_cell,
108 const int r_cell,
109 const int wedge ) const
110 {
111 assert( lmatrices_.data() != nullptr );
112
113 return lmatrices_( local_subdomain_id, x_cell, y_cell, r_cell, wedge );
114 }
115
116 KOKKOS_INLINE_FUNCTION
118 const int local_subdomain_id,
119 const int x_cell,
120 const int y_cell,
121 const int r_cell,
122 const int wedge,
124 {
125 Kokkos::abort( "Not implemented." );
126 }
127
128 /// @brief Setter/Getter for app applyStoredLMatrices_: usage of stored local matrices during apply
129 void setApplyStoredLMatrices( bool v ) { applyStoredLMatrices_ = v; }
130
131 /// @brief
132 /// allocates memory for the local matrices
133 /// calls kernel with storeLMatrices_ = true to assemble and store the local matrices
134 /// sets applyStoredLMatrices_, such that future applies use the stored local matrices
136 {
137 storeLMatrices_ = true;
138 if ( lmatrices_.data() == nullptr )
139 {
140 lmatrices_ = Grid4DDataLocalMatrices(
141 "LaplaceSimple::lmatrices_",
142 domain_.subdomains().size(),
146 Kokkos::parallel_for(
147 "assemble_store_lmatrices", grid::shell::local_domain_md_range_policy_cells( domain_ ), *this );
148 Kokkos::fence();
149 }
150 storeLMatrices_ = false;
151 applyStoredLMatrices_ = true;
152 }
153
154 void apply_impl( const SrcVectorType& src, DstVectorType& dst )
155 {
156 if ( storeLMatrices_ or applyStoredLMatrices_ )
157 assert( lmatrices_.data() != nullptr );
158
159 if ( operator_apply_mode_ == linalg::OperatorApplyMode::Replace )
160 {
161 assign( dst, 0 );
162 }
163
164 src_ = src.grid_data();
165 dst_ = dst.grid_data();
166
167 if ( src_.extent( 0 ) != dst_.extent( 0 ) || src_.extent( 1 ) != dst_.extent( 1 ) ||
168 src_.extent( 2 ) != dst_.extent( 2 ) || src_.extent( 3 ) != dst_.extent( 3 ) )
169 {
170 throw std::runtime_error( "LaplaceSimple: src/dst mismatch" );
171 }
172
173 if ( src_.extent( 1 ) != grid_.extent( 1 ) || src_.extent( 2 ) != grid_.extent( 2 ) )
174 {
175 throw std::runtime_error( "LaplaceSimple: src/dst mismatch" );
176 }
177
178 Kokkos::parallel_for( "matvec", grid::shell::local_domain_md_range_policy_cells( domain_ ), *this );
179
180 if ( operator_communication_mode_ == linalg::OperatorCommunicationMode::CommunicateAdditively )
181 {
183 domain_, dst_, send_buffers_, recv_buffers_ );
185 }
186 }
187
188 KOKKOS_INLINE_FUNCTION void
189 operator()( const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell ) const
190 {
192 if ( !applyStoredLMatrices_ )
193 {
194 // Gather surface points for each wedge.
196 wedge_surface_physical_coords( wedge_phy_surf, grid_, local_subdomain_id, x_cell, y_cell );
197
198 // Gather wedge radii.
199 const ScalarT r_1 = radii_( local_subdomain_id, r_cell );
200 const ScalarT r_2 = radii_( local_subdomain_id, r_cell + 1 );
201
202 // Quadrature points.
203 int num_quad_points = single_quadpoint_ ? quadrature::quad_felippa_1x1_num_quad_points :
205
206 // Compute the local element matrix.
207
208 for ( int q = 0; q < num_quad_points; q++ )
209 {
210 const auto w = single_quadpoint_ ? quad_weights_1x1_[q] : quad_weights_3x2_[q];
211 const auto qp = single_quadpoint_ ? quad_points_1x1_[q] : quad_points_3x2_[q];
212
213 for ( int wedge = 0; wedge < num_wedges_per_hex_cell; wedge++ )
214 {
215 const auto J = jac( wedge_phy_surf[wedge], r_1, r_2, qp );
216 const auto det = Kokkos::abs( J.det() );
217 const auto J_inv_transposed = J.inv().transposed();
218
219 for ( int i = 0; i < num_nodes_per_wedge; i++ )
220 {
221 const auto grad_i = grad_shape( i, qp );
222
223 for ( int j = 0; j < num_nodes_per_wedge; j++ )
224 {
225 const auto grad_j = grad_shape( j, qp );
226
227 A[wedge]( i, j ) +=
228 w * ( ( J_inv_transposed * grad_i ).dot( J_inv_transposed * grad_j ) * det );
229 }
230 }
231 }
232 }
233
234 if ( treat_boundary_ )
235 {
236 for ( int wedge = 0; wedge < num_wedges_per_hex_cell; wedge++ )
237 {
238 dense::Mat< ScalarT, 6, 6 > boundary_mask;
239 boundary_mask.fill( 1.0 );
240 if ( r_cell == 0 )
241 {
242 // Inner boundary (CMB).
243 for ( int i = 0; i < 6; i++ )
244 {
245 for ( int j = 0; j < 6; j++ )
246 {
247 if ( i != j && ( i < 3 || j < 3 ) )
248 {
249 boundary_mask( i, j ) = 0.0;
250 }
251 }
252 }
253 }
254
255 if ( r_cell + 1 == radii_.extent( 1 ) - 1 )
256 {
257 // Outer boundary (surface).
258 for ( int i = 0; i < 6; i++ )
259 {
260 for ( int j = 0; j < 6; j++ )
261 {
262 if ( i != j && ( i >= 3 || j >= 3 ) )
263 {
264 boundary_mask( i, j ) = 0.0;
265 }
266 }
267 }
268 }
269
270 A[wedge].hadamard_product( boundary_mask );
271 }
272 }
273 }
274 else
275 {
276 // load LMatrix for both local wedges
277 A[0] = lmatrices_( local_subdomain_id, x_cell, y_cell, r_cell, 0 );
278 A[1] = lmatrices_( local_subdomain_id, x_cell, y_cell, r_cell, 1 );
279 }
280
281 if ( diagonal_ )
282 {
283 A[0] = A[0].diagonal();
284 A[1] = A[1].diagonal();
285 }
286
287 if ( storeLMatrices_ )
288 {
289 // write local matrices to mem
290 lmatrices_( local_subdomain_id, x_cell, y_cell, r_cell, 0 ) = A[0];
291 lmatrices_( local_subdomain_id, x_cell, y_cell, r_cell, 1 ) = A[1];
292 }
293 else
294 {
295 // apply local matrices to local DoFs
297 extract_local_wedge_scalar_coefficients( src, local_subdomain_id, x_cell, y_cell, r_cell, src_ );
298
300
301 dst[0] = A[0] * src[0];
302 dst[1] = A[1] * src[1];
303
304 atomically_add_local_wedge_scalar_coefficients( dst_, local_subdomain_id, x_cell, y_cell, r_cell, dst );
305 }
306 }
307
308 // Kernel body:
309};
310
314
315} // namespace terra::fe::wedge::operators::shell
terra::grid::Grid4DDataMatrices< ScalarType, 6, 6, 2 > Grid4DDataLocalMatrices
Definition laplace_simple.hpp:24
ScalarT ScalarType
Definition laplace_simple.hpp:23
void store_lmatrices()
allocates memory for the local matrices calls kernel with storeLMatrices_ = true to assemble and stor...
Definition laplace_simple.hpp:135
void set_local_matrix(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const int wedge, const dense::Mat< ScalarT, LocalMatrixDim, LocalMatrixDim > &mat) const
Definition laplace_simple.hpp:117
grid::Grid2DDataScalar< ScalarT > get_radii() const
Getter for radii member.
Definition laplace_simple.hpp:91
const grid::shell::DistributedDomain & get_domain() const
Getter for domain member.
Definition laplace_simple.hpp:88
grid::Grid3DDataVec< ScalarT, 3 > get_grid() const
Getter for grid member.
Definition laplace_simple.hpp:94
void apply_impl(const SrcVectorType &src, DstVectorType &dst)
Definition laplace_simple.hpp:154
void setApplyStoredLMatrices(bool v)
Setter/Getter for app applyStoredLMatrices_: usage of stored local matrices during apply.
Definition laplace_simple.hpp:129
dense::Mat< ScalarT, 6, 6 > get_local_matrix(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const int wedge) const
Retrives the local matrix stored in the operator.
Definition laplace_simple.hpp:104
void set_diagonal(bool v)
S/Getter for diagonal member.
Definition laplace_simple.hpp:97
void operator()(const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell) const
Definition laplace_simple.hpp:189
LaplaceSimple(const grid::shell::DistributedDomain &domain, const grid::Grid3DDataVec< ScalarT, 3 > &grid, const grid::Grid2DDataScalar< ScalarT > &radii, bool treat_boundary, bool diagonal, bool single_quadpoint=false, linalg::OperatorApplyMode operator_apply_mode=linalg::OperatorApplyMode::Replace, linalg::OperatorCommunicationMode operator_communication_mode=linalg::OperatorCommunicationMode::CommunicateAdditively)
Definition laplace_simple.hpp:59
void set_single_quadpoint(bool v)
S/Getter for quadpoint member.
Definition laplace_simple.hpp:100
static constexpr int LocalMatrixDim
Definition laplace_simple.hpp:26
Parallel data structure organizing the thick spherical shell metadata for distributed (MPI parallel) ...
Definition spherical_shell.hpp:2498
const std::map< SubdomainInfo, std::tuple< LocalSubdomainIdx, SubdomainNeighborhood > > & subdomains() const
Definition spherical_shell.hpp:2580
const DomainInfo & domain_info() const
Returns a const reference.
Definition spherical_shell.hpp:2577
int subdomain_num_nodes_radially() const
Equivalent to calling subdomain_num_nodes_radially( subdomain_refinement_level() )
Definition spherical_shell.hpp:861
int subdomain_num_nodes_per_side_laterally() const
Equivalent to calling subdomain_num_nodes_per_side_laterally( subdomain_refinement_level() )
Definition spherical_shell.hpp:852
Q1 scalar finite element vector on a distributed shell grid.
Definition vector_q1.hpp:21
const grid::Grid4DDataScalar< ScalarType > & grid_data() const
Get const reference to grid data.
Definition vector_q1.hpp:137
Concept for types that can be used as Galerkin coarse-grid operators in a multigrid hierarchy....
Definition operator.hpp:81
Concept for types that behave like linear operators.
Definition operator.hpp:57
void unpack_and_reduce_local_subdomain_boundaries(const grid::shell::DistributedDomain &domain, const GridDataType &data, SubdomainNeighborhoodSendRecvBuffer< typename GridDataType::value_type, grid::grid_data_vec_dim< GridDataType >() > &boundary_recv_buffers, CommunicationReduction reduction=CommunicationReduction::SUM)
Unpacks and reduces local subdomain boundaries.
Definition communication.hpp:672
void pack_send_and_recv_local_subdomain_boundaries(const grid::shell::DistributedDomain &domain, const GridDataType &data, SubdomainNeighborhoodSendRecvBuffer< typename GridDataType::value_type, grid::grid_data_vec_dim< GridDataType >() > &boundary_send_buffers, SubdomainNeighborhoodSendRecvBuffer< typename GridDataType::value_type, grid::grid_data_vec_dim< GridDataType >() > &boundary_recv_buffers)
Packs, sends and recvs local subdomain boundaries using two sets of buffers.
Definition communication.hpp:242
Definition boundary_mass.hpp:14
constexpr void quad_felippa_3x2_quad_weights(T(&quad_weights)[quad_felippa_3x2_num_quad_points])
Definition wedge/quadrature/quadrature.hpp:93
constexpr int quad_felippa_3x2_num_quad_points
Definition wedge/quadrature/quadrature.hpp:66
constexpr void quad_felippa_3x2_quad_points(dense::Vec< T, 3 >(&quad_points)[quad_felippa_3x2_num_quad_points])
Definition wedge/quadrature/quadrature.hpp:70
constexpr void quad_felippa_1x1_quad_points(dense::Vec< T, 3 >(&quad_points)[quad_felippa_1x1_num_quad_points])
Definition wedge/quadrature/quadrature.hpp:36
constexpr void quad_felippa_1x1_quad_weights(T(&quad_weights)[quad_felippa_1x1_num_quad_points])
Definition wedge/quadrature/quadrature.hpp:43
constexpr int quad_felippa_1x1_num_quad_points
Definition wedge/quadrature/quadrature.hpp:32
constexpr int num_nodes_per_wedge_surface
Definition kernel_helpers.hpp:6
void atomically_add_local_wedge_scalar_coefficients(const grid::Grid4DDataScalar< T > &global_coefficients, const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const dense::Vec< T, 6 >(&local_coefficients)[2])
Performs an atomic add of the two local wedge coefficient vectors of a hex cell into the global coeff...
Definition kernel_helpers.hpp:407
void wedge_surface_physical_coords(dense::Vec< T, 3 >(&wedge_surf_phy_coords)[num_wedges_per_hex_cell][num_nodes_per_wedge_surface], const grid::Grid3DDataVec< T, 3 > &lateral_grid, const int local_subdomain_id, const int x_cell, const int y_cell)
Extracts the (unit sphere) surface vertex coords of the two wedges of a hex cell.
Definition kernel_helpers.hpp:26
constexpr int num_wedges_per_hex_cell
Definition kernel_helpers.hpp:5
void extract_local_wedge_scalar_coefficients(dense::Vec< T, 6 >(&local_coefficients)[2], const int local_subdomain_id, const int x_cell, const int y_cell, const int r_cell, const grid::Grid4DDataScalar< T > &global_coefficients)
Extracts the local vector coefficients for the two wedges of a hex cell from the global coefficient v...
Definition kernel_helpers.hpp:306
constexpr int num_nodes_per_wedge
Definition kernel_helpers.hpp:7
constexpr dense::Vec< T, 3 > grad_shape(const int node_idx, const T xi, const T eta, const T zeta)
Gradient of the full shape function:
Definition integrands.hpp:228
constexpr dense::Mat< T, 3, 3 > jac(const dense::Vec< T, 3 > &p1_phy, const dense::Vec< T, 3 > &p2_phy, const dense::Vec< T, 3 > &p3_phy, const T r_1, const T r_2, const T xi, const T eta, const T zeta)
Definition integrands.hpp:643
Kokkos::MDRangePolicy< Kokkos::Rank< 4 > > local_domain_md_range_policy_cells(const DistributedDomain &distributed_domain)
Definition spherical_shell.hpp:2668
Kokkos::View< dense::Mat< ScalarType, Rows, Cols > ****[NumMatrices], Layout > Grid4DDataMatrices
Definition grid_types.hpp:46
Kokkos::View< ScalarType ***[VecDim], Layout > Grid3DDataVec
Definition grid_types.hpp:40
Kokkos::View< ScalarType ****, Layout > Grid4DDataScalar
Definition grid_types.hpp:25
Kokkos::View< ScalarType **, Layout > Grid2DDataScalar
Definition grid_types.hpp:19
OperatorApplyMode
Modes for applying an operator to a vector.
Definition operator.hpp:30
@ Replace
Overwrite the destination vector.
OperatorCommunicationMode
Modes for communication during operator application.
Definition operator.hpp:40
@ CommunicateAdditively
Communicate and add results.
Definition mat.hpp:10
void fill(const T value)
Definition mat.hpp:201
constexpr Mat diagonal() const
Definition mat.hpp:377
Mat & hadamard_product(const Mat &mat)
Definition mat.hpp:213