Compadre  1.5.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
12 #include "Compadre_Operators.hpp"
13 
14 namespace Compadre {
15 
16 struct XYZ {
17 
18  double x;
19  double y;
20  double z;
21 
22  KOKKOS_INLINE_FUNCTION
23  XYZ(double _x = 0.0, double _y = 0.0, double _z = 0.0) : x(_x), y(_y), z(_z) {}
24 
25  KOKKOS_INLINE_FUNCTION
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 
30  KOKKOS_INLINE_FUNCTION
31  XYZ operator += (const XYZ& other)
32  { x += other.x;
33  y += other.y;
34  z += other.z;
35  return *this; }
36  KOKKOS_INLINE_FUNCTION
38  { x += other.x;
39  y += other.y;
40  z += other.z;
41  return *this; }
42  KOKKOS_INLINE_FUNCTION
43  XYZ operator -= (const XYZ& other)
44  { x -= other.x;
45  y -= other.y;
46  z -= other.z;
47  return *this; }
48  KOKKOS_INLINE_FUNCTION
50  { x -= other.x;
51  y -= other.y;
52  z -= other.z;
53  return *this; }
54  KOKKOS_INLINE_FUNCTION
55  XYZ operator *= (const double& scaling)
56  { x *= scaling;
57  y *= scaling;
58  z *= scaling;
59  return *this; }
60  KOKKOS_INLINE_FUNCTION
61  scalar_type& operator [](const int i) {
62  switch (i) {
63  case 0:
64  return x;
65  case 1:
66  return y;
67  default:
68  return z;
69  }
70  }
71  KOKKOS_INLINE_FUNCTION
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  }
82  KOKKOS_INLINE_FUNCTION
83  XYZ operator *(double scalar) {
84  XYZ result;
85  result.x = scalar*x;
86  result.y = scalar*y;
87  result.z = scalar*z;
88  return result;
89  }
90 
91  KOKKOS_INLINE_FUNCTION
92  size_t extent(int comp = 0) {
93  return 3;
94  }
95 
96  KOKKOS_INLINE_FUNCTION
97  int extent_int(int comp = 0) {
98  return 3;
99  }
100 
101 }; // XYZ
102 
103 
104 KOKKOS_INLINE_FUNCTION
105 XYZ 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
109 XYZ operator - ( const XYZ& vecA, const XYZ& vecB ) {
110  return XYZ( vecA.x - vecB.x, vecA.y - vecB.y, vecA.z - vecB.z); }
111 
112 KOKKOS_INLINE_FUNCTION
113 XYZ operator * ( const XYZ& vecA, const XYZ& vecB ) {
114  return XYZ( vecA.x * vecB.x, vecA.y * vecB.y, vecA.z * vecB.z); }
115 
116 KOKKOS_INLINE_FUNCTION
117 XYZ operator + ( const XYZ& vecA, const scalar_type& constant ) {
118  return XYZ( vecA.x + constant, vecA.y + constant, vecA.z + constant); }
119 
120 KOKKOS_INLINE_FUNCTION
121 XYZ operator + ( const scalar_type& constant, const XYZ& vecA ) {
122  return XYZ( vecA.x + constant, vecA.y + constant, vecA.z + constant); }
123 
124 KOKKOS_INLINE_FUNCTION
125 XYZ operator - ( const XYZ& vecA, const scalar_type& constant ) {
126  return XYZ( vecA.x - constant, vecA.y - constant, vecA.z - constant); }
127 
128 KOKKOS_INLINE_FUNCTION
129 XYZ operator - ( const scalar_type& constant, const XYZ& vecA ) {
130  return XYZ( constant - vecA.x, constant - vecA.y, constant - vecA.z); }
131 
132 KOKKOS_INLINE_FUNCTION
133 XYZ operator * ( const XYZ& vecA, const scalar_type& constant ) {
134  return XYZ( vecA.x * constant, vecA.y * constant, vecA.z * constant); }
135 
136 KOKKOS_INLINE_FUNCTION
137 XYZ operator * (const scalar_type& constant, const XYZ& vecA) {
138  return XYZ( vecA.x * constant, vecA.y * constant, vecA.z * constant); }
139 
140 KOKKOS_INLINE_FUNCTION
141 XYZ operator / ( const XYZ& vecA, const scalar_type& constant ) {
142  return XYZ( vecA.x / constant, vecA.y / constant, vecA.z / constant); }
143 
144 inline 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)
148 KOKKOS_INLINE_FUNCTION
149 int 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 
168 KOKKOS_INLINE_FUNCTION
169 int getAdditionalAlphaSizeFromConstraint(DenseSolverType dense_solver_type, ConstraintType constraint_type) {
170  // Return the additional constraint size
171  if (constraint_type == NEUMANN_GRAD_SCALAR) {
172  return 1;
173  } else {
174  return 0;
175  }
176 }
177 
178 KOKKOS_INLINE_FUNCTION
179 int 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 
189 KOKKOS_INLINE_FUNCTION
190 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) {
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 
209 KOKKOS_INLINE_FUNCTION
210 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) {
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
233 KOKKOS_INLINE_FUNCTION
234 int 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
240 KOKKOS_INLINE_FUNCTION
241 int 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
247 KOKKOS_INLINE_FUNCTION
249  return sro.input_rank;
250 }
251 
252 //! Dimensions ^ output rank for sampling operation
253 //! (always in local chart if on a manifold, never ambient space)
254 KOKKOS_INLINE_FUNCTION
255 int 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)
261 KOKKOS_INLINE_FUNCTION
262 int getInputDimensionOfSampling(SamplingFunctional sro, const int global_dimensions) {
263  return pown(global_dimensions, sro.input_rank);
264 }
265 
266 //! Calculate basis_multiplier
267 KOKKOS_INLINE_FUNCTION
268 int 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
275 KOKKOS_INLINE_FUNCTION
276 int 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
291 KOKKOS_INLINE_FUNCTION
292 int 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)
297 KOKKOS_INLINE_FUNCTION
298 int 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 getOutputDimensionOfSampling(SamplingFunctional sro, const int local_dimensions)
Dimensions ^ output rank for sampling operation (always in local chart if on a manifold, never ambient space)
KOKKOS_INLINE_FUNCTION int getAdditionalAlphaSizeFromConstraint(DenseSolverType dense_solver_type, ConstraintType constraint_type)
ConstraintType
Constraint type.
double scalar_type
int output_rank
Rank of sampling functional output for each SamplingFunctional.
Neumann Gradient Scalar Type.
ReconstructionSpace
Space in which to reconstruct polynomial.
KOKKOS_INLINE_FUNCTION scalar_type & operator[](const int i)
Scalar basis reused as many times as there are components in the vector resulting in a much cheaper p...
KOKKOS_INLINE_FUNCTION int extent_int(int comp=0)
KOKKOS_INLINE_FUNCTION int getAdditionalCoeffSizeFromConstraintAndSpace(DenseSolverType dense_solver_type, ConstraintType constraint_type, ReconstructionSpace reconstruction_space, const int dimension)
KOKKOS_INLINE_FUNCTION XYZ operator-(const XYZ &vecA, const XYZ &vecB)
KOKKOS_INLINE_FUNCTION XYZ operator+(const XYZ &vecA, const XYZ &vecB)
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 &other)
KOKKOS_INLINE_FUNCTION XYZ(double _x=0.0, double _y=0.0, double _z=0.0)
KOKKOS_INLINE_FUNCTION XYZ operator*(double scalar)
KOKKOS_INLINE_FUNCTION int getActualReconstructionSpaceRank(const int &index)
Number of actual components in the ReconstructionSpace.
DenseSolverType
Dense solver type.
Vector polynomial basis having # of components _dimensions, or (_dimensions-1) in the case of manifol...
KOKKOS_INLINE_FUNCTION XYZ operator*(const XYZ &vecA, const XYZ &vecB)
XYZ(const std::vector< scalar_type > &vec)
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.
KOKKOS_INLINE_FUNCTION int getInputRankOfSampling(SamplingFunctional sro)
Input rank for sampling operation.
TargetOperation
Available target functionals.
int input_rank
Rank of sampling functional input for each SamplingFunctional.
KOKKOS_INLINE_FUNCTION int pown(int n, unsigned p)
n^p (n^0 returns 1, regardless of n)
LU factorization performed on P^T*W*P matrix.
KOKKOS_INLINE_FUNCTION XYZ(const scalar_type *arry)
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.
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)
import subprocess import os import re import math import sys import argparse p
KOKKOS_INLINE_FUNCTION XYZ operator-=(const XYZ &other)
std::ostream & operator<<(std::ostream &os, const XYZ &vec)
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 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 int getInputDimensionOfOperation(TargetOperation lro, SamplingFunctional sro, const int local_dimensions)
Dimensions ^ input rank for target operation (always in local chart if on a manifold, never ambient space)
KOKKOS_INLINE_FUNCTION XYZ operator*=(const double &scaling)
KOKKOS_INLINE_FUNCTION XYZ operator/(const XYZ &vecA, const scalar_type &constant)
KOKKOS_INLINE_FUNCTION int getOutputDimensionOfOperation(TargetOperation lro, const int local_dimensions)
Dimensions ^ output rank for target operation.
KOKKOS_INLINE_FUNCTION size_t extent(int comp=0)
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 calculateBasisMultiplier(const ReconstructionSpace rs, const int local_dimensions)
Calculate basis_multiplier.