Compadre 1.6.4
Loading...
Searching...
No Matches
Compadre_Misc.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Compadre: COMpatible PArticle Discretization and REmap Toolkit
4//
5// Copyright 2018 NTESS and the Compadre contributors.
6// SPDX-License-Identifier: BSD-2-Clause
7// *****************************************************************************
8// @HEADER
9#ifndef _COMPADRE_MISC_HPP_
10#define _COMPADRE_MISC_HPP_
11
13
14namespace Compadre {
15
16struct XYZ {
17
18 double x;
19 double y;
20 double z;
21
23 XYZ(double _x = 0.0, double _y = 0.0, double _z = 0.0) : x(_x), y(_y), z(_z) {}
24
26 XYZ(const scalar_type* arry) : x(arry[0]), y(arry[1]), z(arry[2]) {};
27
28 XYZ(const std::vector<scalar_type>& vec) : x(vec[0]), y(vec[1]), z(vec[2]) {};
29
32 { x += other.x;
33 y += other.y;
34 z += other.z;
35 return *this; }
38 { x += other.x;
39 y += other.y;
40 z += other.z;
41 return *this; }
44 { x -= other.x;
45 y -= other.y;
46 z -= other.z;
47 return *this; }
50 { x -= other.x;
51 y -= other.y;
52 z -= other.z;
53 return *this; }
55 XYZ operator *= (const double& scaling)
56 { x *= scaling;
57 y *= scaling;
58 z *= scaling;
59 return *this; }
62 switch (i) {
63 case 0:
64 return x;
65 case 1:
66 return y;
67 default:
68 return z;
69 }
70 }
72 scalar_type operator [](const int i) const {
73 switch (i) {
74 case 0:
75 return x;
76 case 1:
77 return y;
78 default:
79 return z;
80 }
81 }
84 XYZ result;
85 result.x = scalar*x;
86 result.y = scalar*y;
87 result.z = scalar*z;
88 return result;
89 }
90
92 size_t extent(int comp = 0) {
93 return 3;
94 }
95
97 int extent_int(int comp = 0) {
98 return 3;
99 }
100
101}; // XYZ
102
103
104KOKKOS_INLINE_FUNCTION
105XYZ operator + ( const XYZ& vecA, const XYZ& vecB ) {
106 return XYZ( vecA.x + vecB.x, vecA.y + vecB.y, vecA.z + vecB.z); }
107
108 KOKKOS_INLINE_FUNCTION
109XYZ operator - ( const XYZ& vecA, const XYZ& vecB ) {
110 return XYZ( vecA.x - vecB.x, vecA.y - vecB.y, vecA.z - vecB.z); }
111
112KOKKOS_INLINE_FUNCTION
113XYZ operator * ( const XYZ& vecA, const XYZ& vecB ) {
114 return XYZ( vecA.x * vecB.x, vecA.y * vecB.y, vecA.z * vecB.z); }
115
116KOKKOS_INLINE_FUNCTION
117XYZ operator + ( const XYZ& vecA, const scalar_type& constant ) {
118 return XYZ( vecA.x + constant, vecA.y + constant, vecA.z + constant); }
119
120KOKKOS_INLINE_FUNCTION
121XYZ operator + ( const scalar_type& constant, const XYZ& vecA ) {
122 return XYZ( vecA.x + constant, vecA.y + constant, vecA.z + constant); }
123
124KOKKOS_INLINE_FUNCTION
125XYZ operator - ( const XYZ& vecA, const scalar_type& constant ) {
126 return XYZ( vecA.x - constant, vecA.y - constant, vecA.z - constant); }
127
128KOKKOS_INLINE_FUNCTION
129XYZ operator - ( const scalar_type& constant, const XYZ& vecA ) {
130 return XYZ( constant - vecA.x, constant - vecA.y, constant - vecA.z); }
131
132KOKKOS_INLINE_FUNCTION
133XYZ operator * ( const XYZ& vecA, const scalar_type& constant ) {
134 return XYZ( vecA.x * constant, vecA.y * constant, vecA.z * constant); }
135
136KOKKOS_INLINE_FUNCTION
137XYZ operator * (const scalar_type& constant, const XYZ& vecA) {
138 return XYZ( vecA.x * constant, vecA.y * constant, vecA.z * constant); }
139
140KOKKOS_INLINE_FUNCTION
141XYZ operator / ( const XYZ& vecA, const scalar_type& constant ) {
142 return XYZ( vecA.x / constant, vecA.y / constant, vecA.z / constant); }
143
144inline std::ostream& operator << ( std::ostream& os, const XYZ& vec ) {
145 os << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")" ; return os; }
146
147//! n^p (n^0 returns 1, regardless of n)
148KOKKOS_INLINE_FUNCTION
149int pown(int n, unsigned p) {
150 // O(p) implementation
151 int y = 1;
152 for (unsigned i=0; i<p; i++) {
153 y *= n;
154 }
155 return y;
156 // O(log(p)) implementation
157 //int result = 1;
158 //while (p) {
159 // if (p & 0x1) {
160 // result *= n;
161 // }
162 // n *= n;
163 // p >>= 1;
164 //}
165 //return result;
166}
167
168KOKKOS_INLINE_FUNCTION
170 // Return the additional constraint size
171 if (constraint_type == NEUMANN_GRAD_SCALAR) {
172 return 1;
173 } else {
174 return 0;
175 }
176}
177
178KOKKOS_INLINE_FUNCTION
179int getAdditionalCoeffSizeFromConstraintAndSpace(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension) {
180 // Return the additional constraint size
181 int added_alpha_size = getAdditionalAlphaSizeFromConstraint(dense_solver_type, constraint_type);
182 if (reconstruction_space == VectorTaylorPolynomial) {
183 return dimension*added_alpha_size;
184 } else {
185 return added_alpha_size;
186 }
187}
188
189KOKKOS_INLINE_FUNCTION
190void getRHSDims(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension, const int M, const int N, int &RHS_row, int &RHS_col) {
191 // Return the appropriate size for _RHS. Since in LU, the system solves P^T*P against P^T*W.
192 // We store P^T*P in the RHS space, which means RHS can be much smaller compared to the
193 // case for QR/SVD where the system solves PsqrtW against sqrtW*Identity
194
195 int added_coeff_size = getAdditionalCoeffSizeFromConstraintAndSpace(dense_solver_type, constraint_type, reconstruction_space, dimension);
196
197 if (constraint_type == NEUMANN_GRAD_SCALAR) {
198 RHS_row = RHS_col = N + added_coeff_size;
199 } else {
200 if (dense_solver_type != LU) {
201 RHS_row = N;
202 RHS_col = M;
203 } else {
204 RHS_row = RHS_col = N + added_coeff_size;
205 }
206 }
207}
208
209KOKKOS_INLINE_FUNCTION
210void getPDims(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension, const int M, const int N, int &out_row, int &out_col) {
211 // Return the appropriate size for _P.
212 // In the case of solving with LU and additional constraint is used, _P needs
213 // to be resized to include additional row(s) based on the type of constraint.
214
215 int added_coeff_size = getAdditionalCoeffSizeFromConstraintAndSpace(dense_solver_type, constraint_type, reconstruction_space, dimension);
216 int added_alpha_size = getAdditionalAlphaSizeFromConstraint(dense_solver_type, constraint_type);
217
218 if (constraint_type == NEUMANN_GRAD_SCALAR) {
219 out_row = M + added_alpha_size;
220 out_col = N + added_coeff_size;
221 } else {
222 if (dense_solver_type == LU) {
223 out_row = M + added_alpha_size;
224 out_col = N + added_coeff_size;
225 } else {
226 out_row = M;
227 out_col = N;
228 }
229 }
230}
231
232//! Helper function for finding alpha coefficients
233KOKKOS_INLINE_FUNCTION
234int getTargetOutputIndex(const int operation_num, const int output_component_axis_1, const int output_component_axis_2, const int dimensions) {
235 const int axis_1_size = (getTargetOutputTensorRank(operation_num) > 1) ? dimensions : 1;
236 return axis_1_size*output_component_axis_1 + output_component_axis_2; // 0 for scalar, 0 for vector;
237}
238
239//! Helper function for finding alpha coefficients
240KOKKOS_INLINE_FUNCTION
241int getSamplingOutputIndex(const SamplingFunctional sf, const int output_component_axis_1, const int output_component_axis_2) {
242 const int axis_1_size = (sf.output_rank > 1) ? sf.output_rank : 1;
243 return axis_1_size*output_component_axis_1 + output_component_axis_2; // 0 for scalar, 0 for vector;
244}
245
246//! Input rank for sampling operation
247KOKKOS_INLINE_FUNCTION
251
252//! Dimensions ^ output rank for sampling operation
253//! (always in local chart if on a manifold, never ambient space)
254KOKKOS_INLINE_FUNCTION
255int getOutputDimensionOfSampling(SamplingFunctional sro, const int local_dimensions) {
256 return pown(local_dimensions, sro.output_rank);
257}
258
259//! Dimensions ^ output rank for sampling operation
260//! (always in ambient space, never local chart on a manifold)
261KOKKOS_INLINE_FUNCTION
262int getInputDimensionOfSampling(SamplingFunctional sro, const int global_dimensions) {
263 return pown(global_dimensions, sro.input_rank);
264}
265
266//! Calculate basis_multiplier
267KOKKOS_INLINE_FUNCTION
268int calculateBasisMultiplier(const ReconstructionSpace rs, const int local_dimensions) {
269 // calculate the dimension of the basis
270 // (a vector space on a manifold requires two components, for example)
271 return pown(local_dimensions, getActualReconstructionSpaceRank((int)rs));
272}
273
274//! Calculate sampling_multiplier
275KOKKOS_INLINE_FUNCTION
276int calculateSamplingMultiplier(const ReconstructionSpace rs, const SamplingFunctional sro, const int local_dimensions) {
277 // this would normally be SamplingOutputTensorRank[_data_sampling_functional], but we also want to handle the
278 // case of reconstructions where a scalar basis is reused as a vector, and this handles everything
279 // this handles scalars, vectors, and scalars that are reused as vectors
280 int bm = calculateBasisMultiplier(rs, local_dimensions);
281 int sm = getOutputDimensionOfSampling(sro, local_dimensions);
283 // storage and computational efficiency by reusing solution to scalar problem for
284 // a vector problem (in 3d, 27x cheaper computation, 9x cheaper storage)
285 sm =(bm < sm) ? bm : sm;
286 }
287 return sm;
288}
289
290//! Dimensions ^ output rank for target operation
291KOKKOS_INLINE_FUNCTION
292int getOutputDimensionOfOperation(TargetOperation lro, const int local_dimensions) {
293 return pown(local_dimensions, getTargetOutputTensorRank(lro));
294}
295
296//! Dimensions ^ input rank for target operation (always in local chart if on a manifold, never ambient space)
297KOKKOS_INLINE_FUNCTION
298int getInputDimensionOfOperation(TargetOperation lro, SamplingFunctional sro, const int local_dimensions) {
299 // this is the same return values as the OutputDimensionOfSampling for the GMLS class's SamplingFunctional
300 return getOutputDimensionOfSampling(sro, local_dimensions);
301}
302
303} // Compadre
304
305#endif
KOKKOS_INLINE_FUNCTION int pown(int n, unsigned p)
n^p (n^0 returns 1, regardless of n)
KOKKOS_INLINE_FUNCTION int getTargetOutputTensorRank(const int &index)
Rank of target functional output for each TargetOperation Rank of target functional input for each Ta...
KOKKOS_INLINE_FUNCTION int getAdditionalCoeffSizeFromConstraintAndSpace(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension)
KOKKOS_INLINE_FUNCTION void getRHSDims(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension, const int M, const int N, int &RHS_row, int &RHS_col)
KOKKOS_INLINE_FUNCTION XYZ operator-(const XYZ &vecA, const XYZ &vecB)
KOKKOS_INLINE_FUNCTION int getActualReconstructionSpaceRank(const int &index)
Number of actual components in the ReconstructionSpace.
ConstraintType
Constraint type.
@ NEUMANN_GRAD_SCALAR
Neumann Gradient Scalar Type.
KOKKOS_INLINE_FUNCTION XYZ operator*(const XYZ &vecA, const XYZ &vecB)
std::ostream & operator<<(std::ostream &os, const XYZ &vec)
ReconstructionSpace
Space in which to reconstruct polynomial.
@ VectorTaylorPolynomial
Vector polynomial basis having # of components _dimensions, or (_dimensions-1) in the case of manifol...
@ VectorOfScalarClonesTaylorPolynomial
Scalar basis reused as many times as there are components in the vector resulting in a much cheaper p...
KOKKOS_INLINE_FUNCTION int calculateBasisMultiplier(const ReconstructionSpace rs, const int local_dimensions)
Calculate basis_multiplier.
KOKKOS_INLINE_FUNCTION int calculateSamplingMultiplier(const ReconstructionSpace rs, const SamplingFunctional sro, const int local_dimensions)
Calculate sampling_multiplier.
KOKKOS_INLINE_FUNCTION XYZ operator+(const XYZ &vecA, const XYZ &vecB)
KOKKOS_INLINE_FUNCTION int getAdditionalAlphaSizeFromConstraint(DenseSolverType dense_solver_type, ConstraintType constraint_type)
KOKKOS_INLINE_FUNCTION void getPDims(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension, const int M, const int N, int &out_row, int &out_col)
KOKKOS_INLINE_FUNCTION int getOutputDimensionOfOperation(TargetOperation lro, const int local_dimensions)
Dimensions ^ output rank for target operation.
DenseSolverType
Dense solver type.
@ LU
LU factorization performed on P^T*W*P matrix.
KOKKOS_INLINE_FUNCTION int getInputDimensionOfOperation(TargetOperation lro, SamplingFunctional sro, const int local_dimensions)
Dimensions ^ input rank for target operation (always in local chart if on a manifold,...
KOKKOS_INLINE_FUNCTION XYZ operator/(const XYZ &vecA, const scalar_type &constant)
KOKKOS_INLINE_FUNCTION int getInputDimensionOfSampling(SamplingFunctional sro, const int global_dimensions)
Dimensions ^ output rank for sampling operation (always in ambient space, never local chart on a mani...
KOKKOS_INLINE_FUNCTION int getTargetOutputIndex(const int operation_num, const int output_component_axis_1, const int output_component_axis_2, const int dimensions)
Helper function for finding alpha coefficients.
TargetOperation
Available target functionals.
KOKKOS_INLINE_FUNCTION int getOutputDimensionOfSampling(SamplingFunctional sro, const int local_dimensions)
Dimensions ^ output rank for sampling operation (always in local chart if on a manifold,...
KOKKOS_INLINE_FUNCTION int getInputRankOfSampling(SamplingFunctional sro)
Input rank for sampling operation.
KOKKOS_INLINE_FUNCTION int getSamplingOutputIndex(const SamplingFunctional sf, const int output_component_axis_1, const int output_component_axis_2)
Helper function for finding alpha coefficients.
int output_rank
Rank of sampling functional output for each SamplingFunctional.
int input_rank
Rank of sampling functional input for each SamplingFunctional.
KOKKOS_INLINE_FUNCTION scalar_type & operator[](const int i)
KOKKOS_INLINE_FUNCTION XYZ(double _x=0.0, double _y=0.0, double _z=0.0)
KOKKOS_INLINE_FUNCTION XYZ operator+=(const XYZ &other)
KOKKOS_INLINE_FUNCTION XYZ operator*=(const double &scaling)
KOKKOS_INLINE_FUNCTION XYZ(const scalar_type *arry)
XYZ(const std::vector< scalar_type > &vec)
KOKKOS_INLINE_FUNCTION XYZ operator-=(const XYZ &other)
KOKKOS_INLINE_FUNCTION XYZ operator*(double scalar)
KOKKOS_INLINE_FUNCTION int extent_int(int comp=0)
KOKKOS_INLINE_FUNCTION size_t extent(int comp=0)