Loading...
Searching...
No Matches
vector_q1.hpp
Go to the documentation of this file.
1#pragma once
2#include <mpi.h>
3
9#include "vector.hpp"
10
11namespace terra::linalg {
12
13/// @brief Q1 scalar finite element vector on a distributed shell grid.
14///
15/// Same layout as required for tensor-product wedge elements.
16///
17/// Satisfies the VectorLike concept (see vector.hpp).
18/// Provides masked grid data and operations for scalar fields.
19template < typename ScalarT >
21{
22 public:
23 /// @brief Scalar type of the vector.
24 using ScalarType = ScalarT;
25
26 /// @brief Default constructor.
27 VectorQ1Scalar() = default;
28
29 /// @brief Construct a Q1 scalar vector with label, domain, and mask data.
30 /// @param label Name for the vector.
31 /// @param distributed_domain Distributed shell domain.
32 /// @param mask_data Mask data grid.
34 const std::string& label,
35 const grid::shell::DistributedDomain& distributed_domain,
37 : mask_data_( mask_data )
38 , comm_( distributed_domain.comm() )
39 {
41 label,
42 distributed_domain.subdomains().size(),
45 distributed_domain.domain_info().subdomain_num_nodes_radially() );
46
47 grid_data_ = grid_data;
48
49 if ( mask_data_.extent( 0 ) != grid_data_.extent( 0 ) || mask_data_.extent( 1 ) != grid_data_.extent( 1 ) ||
50 mask_data_.extent( 2 ) != grid_data_.extent( 2 ) || mask_data_.extent( 3 ) != grid_data_.extent( 3 ) )
51 {
52 throw std::runtime_error(
53 "VectorQ1Scalar::VectorQ1Scalar: mask_data and grid_data must have the same size" );
54 }
55 }
56
57 /// @brief Linear combination implementation for VectorLike concept.
58 /// Computes: \f$ y = c_0 + \sum_i c_i x_i \f$
59 /// @param c Coefficients.
60 /// @param x Input vectors.
61 /// @param c0 Scalar to add.
62 void lincomb_impl( const std::vector< ScalarType >& c, const std::vector< VectorQ1Scalar >& x, const ScalarType c0 )
63 {
64 if ( c.size() != x.size() )
65 {
66 throw std::runtime_error( "VectorQ1Scalar::lincomb: c and x must have the same size" );
67 }
68
69 if ( x.size() == 0 )
70 {
71 kernels::common::set_constant( grid_data_, c0 );
72 }
73 else if ( x.size() == 1 )
74 {
75 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data() );
76 }
77 else if ( x.size() == 2 )
78 {
79 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data() );
80 }
81 else if ( x.size() == 3 )
82 {
84 grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data(), c[2], x[2].grid_data() );
85 }
86 else
87 {
88 throw std::runtime_error( "VectorQ1Scalar::lincomb: not implemented" );
89 }
90 }
91
92 /// @brief Dot product implementation for VectorLike concept.
93 /// Computes: \f$ \sum_{i} y_i \cdot x_i \f$ over owned nodes.
94 /// @param x Other vector.
95 /// @return Dot product value.
97 {
99 grid_data_, x.grid_data(), mask_data(), grid::NodeOwnershipFlag::OWNED, comm_ );
100 }
101
102 /// @brief Invert entries implementation for VectorLike concept.
103 /// Computes: \f$ y_i = 1 / y_i \f$
105
106 /// @brief Elementwise scaling implementation for VectorLike concept.
107 /// Computes: \f$ y_i = y_i \cdot x_i \f$
108 /// @param x Scaling vector.
113
114 /// @brief Randomize entries implementation for VectorLike concept.
115 /// Sets each entry of grid_data to a random value.
116 void randomize_impl() { return kernels::common::rand( grid_data_ ); }
117
118 /// @brief Max absolute entry implementation for VectorLike concept.
119 /// Computes: \f$ \max_i |y_i| \f$
120 /// @return Maximum absolute value.
121 ScalarType max_abs_entry_impl() const { return kernels::common::max_abs_entry( grid_data_, comm_ ); }
122
123 /// @brief NaN/inf check implementation for VectorLike concept.
124 /// Returns true if any entry of grid_data is NaN/inf.
125 /// @return True if NaN/inf is present.
126 bool has_nan_or_inf_impl() const { return kernels::common::has_nan_or_inf( grid_data_, comm_ ); }
127
128 /// @brief Swap implementation for VectorLike concept.
129 /// Exchanges grid_data and mask_data with another vector.
130 /// @param other Other vector.
132 {
133 std::swap( grid_data_, other.grid_data_ );
134 std::swap( mask_data_, other.mask_data_ );
135 std::swap( comm_, other.comm_ );
136 }
137
138 /// @brief Get const reference to grid data.
139 const grid::Grid4DDataScalar< ScalarType >& grid_data() const { return grid_data_; }
140 /// @brief Get mutable reference to grid data.
142
143 /// @brief Get const reference to mask data.
145 /// @brief Get mutable reference to mask data.
147
148 /// @brief MPI communicator this vector's reductions run on.
149 MPI_Comm comm() const { return comm_; }
150
151 private:
154 MPI_Comm comm_ = MPI_COMM_WORLD;
155};
156
157/// @brief Static assertion: VectorQ1Scalar satisfies VectorLike concept.
158static_assert( VectorLike< VectorQ1Scalar< double > > );
159
160/// @brief Q1 vector finite element vector on a distributed shell grid.
161///
162/// Same layout as required for tensor-product wedge elements.
163///
164/// Satisfies the VectorLike concept (see vector.hpp).
165/// Provides masked grid data and operations for vector fields.
166template < typename ScalarT, int VecDim = 3 >
168{
169 public:
170 /// @brief Default constructor.
171 VectorQ1Vec() = default;
172
173 /// @brief Construct a Q1 vector with label, domain, and mask data.
174 /// @param label Name for the vector.
175 /// @param distributed_domain Distributed shell domain.
176 /// @param mask_data Mask data grid.
178 const std::string& label,
179 const grid::shell::DistributedDomain& distributed_domain,
181 : mask_data_( mask_data )
182 , comm_( distributed_domain.comm() )
183 {
185 label,
186 distributed_domain.subdomains().size(),
189 distributed_domain.domain_info().subdomain_num_nodes_radially() );
190
191 grid_data_ = grid_data;
192
193 if ( mask_data_.extent( 0 ) != grid_data_.extent( 0 ) || mask_data_.extent( 1 ) != grid_data_.extent( 1 ) ||
194 mask_data_.extent( 2 ) != grid_data_.extent( 2 ) || mask_data_.extent( 3 ) != grid_data_.extent( 3 ) )
195 {
196 throw std::runtime_error(
197 "VectorQ1Scalar::VectorQ1Scalar: mask_data and grid_data must have the same size" );
198 }
199 }
200
201 /// @brief Scalar type of the vector.
202 using ScalarType = ScalarT;
203 /// @brief Dimension of the vector field.
204 const static int Dim = VecDim;
205
206 /// @brief Linear combination implementation for VectorLike concept.
207 /// Computes: \f$ y = c_0 + \sum_i c_i x_i \f$
208 /// @param c Coefficients.
209 /// @param x Input vectors.
210 /// @param c0 Scalar to add.
211 void lincomb_impl( const std::vector< ScalarType >& c, const std::vector< VectorQ1Vec >& x, const ScalarType c0 )
212 {
213 if ( c.size() != x.size() )
214 {
215 throw std::runtime_error( "VectorQ1Scalar::lincomb: c and x must have the same size" );
216 }
217
218 if ( x.size() == 0 )
219 {
220 kernels::common::set_constant( grid_data_, c0 );
221 }
222 else if ( x.size() == 1 )
223 {
224 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data() );
225 }
226 else if ( x.size() == 2 )
227 {
228 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data() );
229 }
230 else if ( x.size() == 3 )
231 {
233 grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data(), c[2], x[2].grid_data() );
234 }
235 else
236 {
237 throw std::runtime_error( "VectorQ1Scalar::lincomb: not implemented" );
238 }
239 }
240
241 /// @brief Dot product implementation for VectorLike concept.
242 /// Computes: \f$ \sum_{i} y_i \cdot x_i \f$ over owned nodes.
243 /// @param x Other vector.
244 /// @return Dot product value.
246 {
248 grid_data_, x.grid_data(), mask_data_, grid::NodeOwnershipFlag::OWNED, comm_ );
249 }
250
251 /// @brief Invert entries implementation for VectorLike concept.
252 /// Computes: \f$ y_i = 1 / y_i \f$
254
255 /// @brief Elementwise scaling implementation for VectorLike concept.
256 /// Computes: \f$ y_i = y_i \cdot x_i \f$
257 /// @param x Scaling vector.
259 {
261 }
262
263 /// @brief Randomize entries implementation for VectorLike concept.
264 /// Sets each entry of grid_data to a random value.
265 void randomize_impl() { return kernels::common::rand( grid_data_ ); }
266
267 /// @brief Max absolute entry implementation for VectorLike concept.
268 /// Computes: \f$ \max_i |y_i| \f$
269 /// @return Maximum absolute value.
270 ScalarType max_abs_entry_impl() const { return kernels::common::max_abs_entry( grid_data_, comm_ ); }
271
272 /// @brief NaN/inf check implementation for VectorLike concept.
273 /// Returns true if any entry of grid_data is NaN/inf.
274 /// @return True if NaN/inf is present.
275 bool has_nan_or_inf_impl() const { return kernels::common::has_nan_or_inf( grid_data_, comm_ ); }
276
277 /// @brief Swap implementation for VectorLike concept.
278 /// Exchanges grid_data and mask_data with another vector.
279 /// @param other Other vector.
280 void swap_impl( VectorQ1Vec& other )
281 {
282 std::swap( grid_data_, other.grid_data_ );
283 std::swap( mask_data_, other.mask_data_ );
284 std::swap( comm_, other.comm_ );
285 }
286
287 /// @brief Get const reference to grid data.
288 const grid::Grid4DDataVec< ScalarType, VecDim >& grid_data() const { return grid_data_; }
289 /// @brief Get mutable reference to grid data.
291
292 /// @brief Get const reference to mask data.
294 /// @brief Get mutable reference to mask data.
296
297 /// @brief MPI communicator this vector's reductions run on.
298 MPI_Comm comm() const { return comm_; }
299
300 private:
303 MPI_Comm comm_ = MPI_COMM_WORLD;
304};
305
306/// @brief Static assertion: VectorQ1Vec satisfies VectorLike concept.
307static_assert( VectorLike< VectorQ1Vec< double, 3 > > );
308
309} // namespace terra::linalg
Parallel data structure organizing the thick spherical shell metadata for distributed (MPI parallel) ...
Definition spherical_shell.hpp:2518
const std::map< SubdomainInfo, std::tuple< LocalSubdomainIdx, SubdomainNeighborhood > > & subdomains() const
Definition spherical_shell.hpp:2650
const DomainInfo & domain_info() const
Returns a const reference.
Definition spherical_shell.hpp:2647
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
ScalarType max_abs_entry_impl() const
Max absolute entry implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:121
grid::Grid4DDataScalar< grid::NodeOwnershipFlag > & mask_data()
Get mutable reference to mask data.
Definition vector_q1.hpp:146
VectorQ1Scalar(const std::string &label, const grid::shell::DistributedDomain &distributed_domain, const grid::Grid4DDataScalar< grid::NodeOwnershipFlag > &mask_data)
Construct a Q1 scalar vector with label, domain, and mask data.
Definition vector_q1.hpp:33
ScalarType dot_impl(const VectorQ1Scalar &x) const
Dot product implementation for VectorLike concept. Computes: over owned nodes.
Definition vector_q1.hpp:96
VectorQ1Scalar()=default
Default constructor.
void scale_with_vector_impl(const VectorQ1Scalar &x)
Elementwise scaling implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:109
void swap_impl(VectorQ1Scalar &other)
Swap implementation for VectorLike concept. Exchanges grid_data and mask_data with another vector.
Definition vector_q1.hpp:131
MPI_Comm comm() const
MPI communicator this vector's reductions run on.
Definition vector_q1.hpp:149
void randomize_impl()
Randomize entries implementation for VectorLike concept. Sets each entry of grid_data to a random val...
Definition vector_q1.hpp:116
bool has_nan_or_inf_impl() const
NaN/inf check implementation for VectorLike concept. Returns true if any entry of grid_data is NaN/in...
Definition vector_q1.hpp:126
const grid::Grid4DDataScalar< ScalarType > & grid_data() const
Get const reference to grid data.
Definition vector_q1.hpp:139
ScalarT ScalarType
Scalar type of the vector.
Definition vector_q1.hpp:24
void invert_entries_impl()
Invert entries implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:104
grid::Grid4DDataScalar< ScalarType > & grid_data()
Get mutable reference to grid data.
Definition vector_q1.hpp:141
void lincomb_impl(const std::vector< ScalarType > &c, const std::vector< VectorQ1Scalar > &x, const ScalarType c0)
Linear combination implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:62
const grid::Grid4DDataScalar< grid::NodeOwnershipFlag > & mask_data() const
Get const reference to mask data.
Definition vector_q1.hpp:144
Static assertion: VectorQ1Scalar satisfies VectorLike concept.
Definition vector_q1.hpp:168
ScalarT ScalarType
Scalar type of the vector.
Definition vector_q1.hpp:202
void swap_impl(VectorQ1Vec &other)
Swap implementation for VectorLike concept. Exchanges grid_data and mask_data with another vector.
Definition vector_q1.hpp:280
static const int Dim
Dimension of the vector field.
Definition vector_q1.hpp:204
VectorQ1Vec(const std::string &label, const grid::shell::DistributedDomain &distributed_domain, const grid::Grid4DDataScalar< grid::NodeOwnershipFlag > &mask_data)
Construct a Q1 vector with label, domain, and mask data.
Definition vector_q1.hpp:177
void randomize_impl()
Randomize entries implementation for VectorLike concept. Sets each entry of grid_data to a random val...
Definition vector_q1.hpp:265
ScalarType max_abs_entry_impl() const
Max absolute entry implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:270
void lincomb_impl(const std::vector< ScalarType > &c, const std::vector< VectorQ1Vec > &x, const ScalarType c0)
Linear combination implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:211
bool has_nan_or_inf_impl() const
NaN/inf check implementation for VectorLike concept. Returns true if any entry of grid_data is NaN/in...
Definition vector_q1.hpp:275
VectorQ1Vec()=default
Default constructor.
grid::Grid4DDataScalar< grid::NodeOwnershipFlag > & mask_data()
Get mutable reference to mask data.
Definition vector_q1.hpp:295
MPI_Comm comm() const
MPI communicator this vector's reductions run on.
Definition vector_q1.hpp:298
void invert_entries_impl()
Invert entries implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:253
const grid::Grid4DDataScalar< grid::NodeOwnershipFlag > & mask_data() const
Get const reference to mask data.
Definition vector_q1.hpp:293
void scale_with_vector_impl(const VectorQ1Vec &x)
Elementwise scaling implementation for VectorLike concept. Computes: .
Definition vector_q1.hpp:258
grid::Grid4DDataVec< ScalarType, VecDim > & grid_data()
Get mutable reference to grid data.
Definition vector_q1.hpp:290
ScalarType dot_impl(const VectorQ1Vec &x) const
Dot product implementation for VectorLike concept. Computes: over owned nodes.
Definition vector_q1.hpp:245
const grid::Grid4DDataVec< ScalarType, VecDim > & grid_data() const
Get const reference to grid data.
Definition vector_q1.hpp:288
Kokkos::View< ScalarType ****, Layout > Grid4DDataScalar
Definition grid_types.hpp:27
void invert_inplace(const grid::Grid4DDataScalar< ScalarType > &y)
Definition grid_operations.hpp:232
ScalarType max_abs_entry(const grid::Grid4DDataScalar< ScalarType > &x, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:316
bool has_nan_or_inf(const grid::Grid4DDataScalar< ScalarType > &x, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:704
ScalarType masked_dot_product(const grid::Grid4DDataScalar< ScalarType > &x, const grid::Grid4DDataScalar< ScalarType > &y, const grid::Grid4DDataScalar< FlagType > &mask, const FlagType &mask_value, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:631
void set_constant(const grid::Grid2DDataScalar< ScalarType > &x, ScalarType value)
Definition grid_operations.hpp:12
void mult_elementwise_inplace(const grid::Grid4DDataScalar< ScalarType > &y, const grid::Grid4DDataScalar< ScalarType > &x)
Definition grid_operations.hpp:252
void rand(const grid::Grid4DDataScalar< ScalarTypeDst > &dst)
Definition grid_operations.hpp:749
void lincomb(const grid::Grid4DDataScalar< ScalarType > &y, ScalarType c_0, ScalarType c_1, const grid::Grid4DDataScalar< ScalarType > &x_1)
Definition grid_operations.hpp:133
Contains linear algebra utilities and functions for the Terra project.
Definition inv_rho_drho_dt.hpp:12
SoA (Structure-of-Arrays) 4D vector grid data.
Definition grid_types.hpp:51