Loading...
Searching...
No Matches
vector_fv.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 Finite volume vector on distributed shell grid with one DoF per hex (merging 2 wedges) and ghost-layer in all
14/// directions.
15///
16/// Only non-ghost-layer cells can be expected to be up-to-date. Unlike nodal grids, communication has to be executed
17/// first in general, before running kernels (if ghost-layer data is required).
18///
19/// Satisfies the VectorLike concept (see vector.hpp).
20/// Provides operations for scalar fields.
21template < typename ScalarT >
23{
24 public:
25 /// @brief Scalar type of the vector.
26 using ScalarType = ScalarT;
27
28 /// @brief Default constructor.
29 VectorFVScalar() = default;
30
31 /// @brief Construct a scalar finite volume vector with label and domain.
32 /// @param label Name for the vector.
33 /// @param distributed_domain Distributed shell domain.
34 VectorFVScalar( const std::string& label, const grid::shell::DistributedDomain& distributed_domain )
35 : comm_( distributed_domain.comm() )
36 {
38 label,
39 distributed_domain.subdomains().size(),
40 distributed_domain.domain_info().subdomain_num_nodes_per_side_laterally() + 1,
41 distributed_domain.domain_info().subdomain_num_nodes_per_side_laterally() + 1,
42 distributed_domain.domain_info().subdomain_num_nodes_radially() + 1 );
43
44 grid_data_ = grid_data;
45 }
46
47 /// @brief Linear combination implementation for VectorLike concept.
48 /// Computes: \f$ y = c_0 + \sum_i c_i x_i \f$
49 /// @param c Coefficients.
50 /// @param x Input vectors.
51 /// @param c0 Scalar to add.
52 void lincomb_impl( const std::vector< ScalarType >& c, const std::vector< VectorFVScalar >& x, const ScalarType c0 )
53 {
54 if ( c.size() != x.size() )
55 {
56 throw std::runtime_error( "VectorQ1Scalar::lincomb: c and x must have the same size" );
57 }
58
59 if ( x.size() == 0 )
60 {
61 kernels::common::set_constant( grid_data_, c0 );
62 }
63 else if ( x.size() == 1 )
64 {
65 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data() );
66 }
67 else if ( x.size() == 2 )
68 {
69 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data() );
70 }
71 else if ( x.size() == 3 )
72 {
74 grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data(), c[2], x[2].grid_data() );
75 }
76 else
77 {
78 throw std::runtime_error( "VectorFVScalar::lincomb: not implemented" );
79 }
80 }
81
82 /// @brief Dot product implementation for VectorLike concept.
83 /// Computes: \f$ \sum_{i} y_i \cdot x_i \f$ over owned nodes.
84 /// @param x Other vector.
85 /// @return Dot product value.
87 {
89 grid_data_,
90 x.grid_data(),
91 dense::Vec< int, 4 >{ 0, 1, 1, 1 },
93 static_cast< int >( grid_data_.extent( 0 ) ),
94 static_cast< int >( grid_data_.extent( 1 ) - 1 ),
95 static_cast< int >( grid_data_.extent( 2 ) - 1 ),
96 static_cast< int >( grid_data_.extent( 3 ) - 1 ) },
97 comm_ );
98 }
99
100 /// @brief Invert entries implementation for VectorLike concept.
101 /// Computes: \f$ y_i = 1 / y_i \f$
103
104 /// @brief Elementwise scaling implementation for VectorLike concept.
105 /// Computes: \f$ y_i = y_i \cdot x_i \f$
106 /// @param x Scaling vector.
109
110 /// @brief Randomize entries implementation for VectorLike concept.
111 /// Sets each entry of grid_data to a random value.
112 void randomize_impl() { return kernels::common::rand( grid_data_ ); }
113
114 /// @brief Max absolute entry implementation for VectorLike concept.
115 /// Computes: \f$ \max_i |y_i| \f$
116 /// @return Maximum absolute value.
118 {
120 grid_data_,
121 dense::Vec< int, 4 >{ 0, 1, 1, 1 },
123 grid_data_.extent( 0 ),
124 grid_data_.extent( 1 ) - 1,
125 grid_data_.extent( 2 ) - 1,
126 grid_data_.extent( 3 ) - 1 },
127 comm_ );
128 }
129
130 /// @brief NaN/Inf check implementation for VectorLike concept.
131 /// Returns true if any entry of grid_data is NaN or inf.
132 /// @return True if NaN or inf is present.
133 bool has_nan_or_inf_impl() const { return kernels::common::has_nan_or_inf( grid_data_, comm_ ); }
134
135 /// @brief Swap implementation for VectorLike concept.
136 /// Exchanges grid_data and mask_data with another vector.
137 /// @param other Other vector.
139 {
140 std::swap( grid_data_, other.grid_data_ );
141 std::swap( comm_, other.comm_ );
142 }
143
144 /// @brief Get const reference to grid data.
145 const grid::Grid4DDataScalar< ScalarType >& grid_data() const { return grid_data_; }
146 /// @brief Get mutable reference to grid data.
148
149 /// @brief MPI communicator this vector's reductions run on.
150 MPI_Comm comm() const { return comm_; }
151
152 private:
154 MPI_Comm comm_ = MPI_COMM_WORLD;
155};
156
157/// @brief Static assertion: VectorQ1Scalar satisfies VectorLike concept.
158static_assert( VectorLike< VectorFVScalar< double > > );
159
160/// @brief Finite volume vector on distributed shell grid with one DoF per hex (merging 2 wedges) and ghost-layer in all
161/// directions.
162///
163/// Only non-ghost-layer cells can be expected to be up-to-date. Unlike nodal grids, communication has to be executed
164/// first in general, before running kernels (if ghost-layer data is required).
165///
166/// Satisfies the VectorLike concept (see vector.hpp).
167/// Provides operations for vector fields.
168template < typename ScalarT, int VecDim = 3 >
170{
171 public:
172 /// @brief Scalar type of the vector.
173 using ScalarType = ScalarT;
174
175 /// @brief Default constructor.
176 VectorFVVec() = default;
177
178 /// @brief Construct a scalar finite volume vector with label and domain.
179 /// @param label Name for the vector.
180 /// @param distributed_domain Distributed shell domain.
181 VectorFVVec( const std::string& label, const grid::shell::DistributedDomain& distributed_domain )
182 : comm_( distributed_domain.comm() )
183 {
185 label,
186 distributed_domain.subdomains().size(),
187 distributed_domain.domain_info().subdomain_num_nodes_per_side_laterally() + 1,
188 distributed_domain.domain_info().subdomain_num_nodes_per_side_laterally() + 1,
189 distributed_domain.domain_info().subdomain_num_nodes_radially() + 1 );
190
191 grid_data_ = grid_data;
192 }
193
194 /// @brief Linear combination implementation for VectorLike concept.
195 /// Computes: \f$ y = c_0 + \sum_i c_i x_i \f$
196 /// @param c Coefficients.
197 /// @param x Input vectors.
198 /// @param c0 Scalar to add.
199 void lincomb_impl( const std::vector< ScalarType >& c, const std::vector< VectorFVVec >& x, const ScalarType c0 )
200 {
201 if ( c.size() != x.size() )
202 {
203 throw std::runtime_error( "VectorQ1Scalar::lincomb: c and x must have the same size" );
204 }
205
206 if ( x.size() == 0 )
207 {
208 kernels::common::set_constant( grid_data_, c0 );
209 }
210 else if ( x.size() == 1 )
211 {
212 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data() );
213 }
214 else if ( x.size() == 2 )
215 {
216 kernels::common::lincomb( grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data() );
217 }
218 else if ( x.size() == 3 )
219 {
221 grid_data_, c0, c[0], x[0].grid_data(), c[1], x[1].grid_data(), c[2], x[2].grid_data() );
222 }
223 else
224 {
225 throw std::runtime_error( "VectorFVScalar::lincomb: not implemented" );
226 }
227 }
228
229 /// @brief Dot product implementation for VectorLike concept.
230 /// Computes: \f$ \sum_{i} y_i \cdot x_i \f$ over owned nodes.
231 /// @param x Other vector.
232 /// @return Dot product value.
234 {
236 grid_data_,
237 x.grid_data(),
238 dense::Vec< int, 5 >{ 0, 1, 1, 1, 0 },
240 static_cast< int >( grid_data_.extent( 0 ) ),
241 static_cast< int >( grid_data_.extent( 1 ) - 1 ),
242 static_cast< int >( grid_data_.extent( 2 ) - 1 ),
243 static_cast< int >( grid_data_.extent( 3 ) - 1 ),
244 VecDim } );
245 }
246
247 /// @brief Invert entries implementation for VectorLike concept.
248 /// Computes: \f$ y_i = 1 / y_i \f$
250
251 /// @brief Elementwise scaling implementation for VectorLike concept.
252 /// Computes: \f$ y_i = y_i \cdot x_i \f$
253 /// @param x Scaling vector.
256
257 /// @brief Randomize entries implementation for VectorLike concept.
258 /// Sets each entry of grid_data to a random value.
259 void randomize_impl() { return kernels::common::rand( grid_data_ ); }
260
261 /// @brief Max absolute entry implementation for VectorLike concept.
262 /// Computes: \f$ \max_i |y_i| \f$
263 /// @return Maximum absolute value.
265 {
267 grid_data_,
268 dense::Vec< int, 5 >{ 0, 1, 1, 1, 0 },
270 grid_data_.extent( 0 ),
271 grid_data_.extent( 1 ) - 1,
272 grid_data_.extent( 2 ) - 1,
273 grid_data_.extent( 3 ) - 1,
274 VecDim } );
275 }
276
277 /// @brief NaN/Inf check implementation for VectorLike concept.
278 /// Returns true if any entry of grid_data is NaN or inf.
279 /// @return True if NaN or inf is present.
280 bool has_nan_or_inf_impl() const { return kernels::common::has_nan_or_inf( grid_data_, comm_ ); }
281
282 /// @brief Swap implementation for VectorLike concept.
283 /// Exchanges grid_data and mask_data with another vector.
284 /// @param other Other vector.
285 void swap_impl( VectorFVVec& other )
286 {
287 std::swap( grid_data_, other.grid_data_ );
288 std::swap( comm_, other.comm_ );
289 }
290
291 /// @brief Get const reference to grid data.
292 const grid::Grid4DDataVec< ScalarType, VecDim >& grid_data() const { return grid_data_; }
293 /// @brief Get mutable reference to grid data.
295
296 /// @brief MPI communicator this vector's reductions run on.
297 MPI_Comm comm() const { return comm_; }
298
299 private:
301 MPI_Comm comm_ = MPI_COMM_WORLD;
302};
303
304/// @brief Static assertion: VectorQ1Scalar satisfies VectorLike concept.
305static_assert( VectorLike< VectorFVVec< double > > );
306
307} // 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
Finite volume vector on distributed shell grid with one DoF per hex (merging 2 wedges) and ghost-laye...
Definition vector_fv.hpp:23
ScalarT ScalarType
Scalar type of the vector.
Definition vector_fv.hpp:26
grid::Grid4DDataScalar< ScalarType > & grid_data()
Get mutable reference to grid data.
Definition vector_fv.hpp:147
const grid::Grid4DDataScalar< ScalarType > & grid_data() const
Get const reference to grid data.
Definition vector_fv.hpp:145
void swap_impl(VectorFVScalar &other)
Swap implementation for VectorLike concept. Exchanges grid_data and mask_data with another vector.
Definition vector_fv.hpp:138
void lincomb_impl(const std::vector< ScalarType > &c, const std::vector< VectorFVScalar > &x, const ScalarType c0)
Linear combination implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:52
bool has_nan_or_inf_impl() const
NaN/Inf check implementation for VectorLike concept. Returns true if any entry of grid_data is NaN or...
Definition vector_fv.hpp:133
void invert_entries_impl()
Invert entries implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:102
void scale_with_vector_impl(const VectorFVScalar &x)
Elementwise scaling implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:107
ScalarType dot_impl(const VectorFVScalar &x) const
Dot product implementation for VectorLike concept. Computes: over owned nodes.
Definition vector_fv.hpp:86
ScalarType max_abs_entry_impl() const
Max absolute entry implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:117
MPI_Comm comm() const
MPI communicator this vector's reductions run on.
Definition vector_fv.hpp:150
VectorFVScalar()=default
Default constructor.
void randomize_impl()
Randomize entries implementation for VectorLike concept. Sets each entry of grid_data to a random val...
Definition vector_fv.hpp:112
VectorFVScalar(const std::string &label, const grid::shell::DistributedDomain &distributed_domain)
Construct a scalar finite volume vector with label and domain.
Definition vector_fv.hpp:34
Static assertion: VectorQ1Scalar satisfies VectorLike concept.
Definition vector_fv.hpp:170
void randomize_impl()
Randomize entries implementation for VectorLike concept. Sets each entry of grid_data to a random val...
Definition vector_fv.hpp:259
ScalarType dot_impl(const VectorFVVec &x) const
Dot product implementation for VectorLike concept. Computes: over owned nodes.
Definition vector_fv.hpp:233
void lincomb_impl(const std::vector< ScalarType > &c, const std::vector< VectorFVVec > &x, const ScalarType c0)
Linear combination implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:199
VectorFVVec(const std::string &label, const grid::shell::DistributedDomain &distributed_domain)
Construct a scalar finite volume vector with label and domain.
Definition vector_fv.hpp:181
VectorFVVec()=default
Default constructor.
void swap_impl(VectorFVVec &other)
Swap implementation for VectorLike concept. Exchanges grid_data and mask_data with another vector.
Definition vector_fv.hpp:285
MPI_Comm comm() const
MPI communicator this vector's reductions run on.
Definition vector_fv.hpp:297
void scale_with_vector_impl(const VectorFVVec &x)
Elementwise scaling implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:254
ScalarT ScalarType
Scalar type of the vector.
Definition vector_fv.hpp:173
grid::Grid4DDataVec< ScalarType, VecDim > & grid_data()
Get mutable reference to grid data.
Definition vector_fv.hpp:294
bool has_nan_or_inf_impl() const
NaN/Inf check implementation for VectorLike concept. Returns true if any entry of grid_data is NaN or...
Definition vector_fv.hpp:280
void invert_entries_impl()
Invert entries implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:249
const grid::Grid4DDataVec< ScalarType, VecDim > & grid_data() const
Get const reference to grid data.
Definition vector_fv.hpp:292
ScalarType max_abs_entry_impl() const
Max absolute entry implementation for VectorLike concept. Computes: .
Definition vector_fv.hpp:264
Kokkos::View< ScalarType ****, Layout > Grid4DDataScalar
Definition grid_types.hpp:27
void invert_inplace(const grid::Grid4DDataScalar< ScalarType > &y)
Definition grid_operations.hpp:232
bool has_nan_or_inf(const grid::Grid4DDataScalar< ScalarType > &x, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:704
ScalarType max_abs_entry_subset(const grid::Grid4DDataScalar< ScalarType > &x, dense::Vec< int, 4 > start, dense::Vec< int, 4 > end_excl, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:377
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
ScalarType dot_product_subset(const grid::Grid4DDataScalar< ScalarType > &x, const grid::Grid4DDataScalar< ScalarType > &y, dense::Vec< int, 4 > start, dense::Vec< int, 4 > end_excl, MPI_Comm comm=MPI_COMM_WORLD)
Definition grid_operations.hpp:676
Contains linear algebra utilities and functions for the Terra project.
Definition inv_rho_drho_dt.hpp:12
Definition vec.hpp:9
SoA (Structure-of-Arrays) 4D vector grid data.
Definition grid_types.hpp:51