Intrepid2
Intrepid2_OrientationToolsDefCoeffMatrix_HGRAD.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid2 Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Kyungjoo Kim (kyukim@sandia.gov), or
38 // Mauro Perego (mperego@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 
43 
56 #ifndef __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HGRAD_HPP__
57 #define __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HGRAD_HPP__
58 
59 // disable clang warnings
60 #if defined (__clang__) && !defined (__INTEL_COMPILER)
61 #pragma clang system_header
62 #endif
63 
64 namespace Intrepid2 {
65 
66 namespace Impl {
67 namespace Debug {
68 
69 #ifdef HAVE_INTREPID2_DEBUG
70 template<typename subcellBasisType,
71 typename cellBasisType>
72 inline
73 void
74 check_getCoeffMatrix_HGRAD(const subcellBasisType& subcellBasis,
75  const cellBasisType& cellBasis,
76  const ordinal_type subcellId,
77  const ordinal_type subcellOrt) {
78 
79  // populate points on a subcell and map to subcell
80  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
81  const shards::CellTopology subcellTopo = subcellBasis.getBaseCellTopology();
82 
83  const ordinal_type cellDim = cellTopo.getDimension();
84  const ordinal_type subcellDim = subcellTopo.getDimension();
85 
86  INTREPID2_TEST_FOR_EXCEPTION( subcellDim > cellDim,
87  std::logic_error,
88  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
89  "cellDim cannot be smaller than subcellDim.");
90 
91  INTREPID2_TEST_FOR_EXCEPTION( subcellDim > 2,
92  std::logic_error,
93  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
94  "subCellDim cannot be larger than 2.");
95 
96  const auto subcellBaseKey = subcellTopo.getBaseKey();
97 
98  INTREPID2_TEST_FOR_EXCEPTION( subcellBaseKey != shards::Line<>::key &&
99  subcellBaseKey != shards::Quadrilateral<>::key &&
100  subcellBaseKey != shards::Triangle<>::key,
101  std::logic_error,
102  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
103  "subcellBasis must have line, quad, or triangle topology.");
104 
105 
106  //
107  // Function space
108  //
109 
110  {
111  const bool cellBasisIsHGRAD = cellBasis.getFunctionSpace() == FUNCTION_SPACE_HGRAD;
112  const bool subcellBasisIsHGRAD = subcellBasis.getFunctionSpace() == FUNCTION_SPACE_HGRAD;
113  if (cellBasisIsHGRAD) {
114  INTREPID2_TEST_FOR_EXCEPTION( !subcellBasisIsHGRAD,
115  std::logic_error,
116  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
117  "subcellBasis function space is not consistent to cellBasis.");
118  }
119 
120  INTREPID2_TEST_FOR_EXCEPTION( subcellBasis.getDegree() != cellBasis.getDegree(),
121  std::logic_error,
122  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
123  "subcellBasis has a different polynomial degree from cellBasis' degree.");
124  }
125 }
126 #endif
127 } // Debug namespace
128 
129 template<typename OutputViewType,
130 typename subcellBasisHostType,
131 typename cellBasisHostType>
132 inline
133 void
135 getCoeffMatrix_HGRAD(OutputViewType &output,
136  const subcellBasisHostType& subcellBasis,
137  const cellBasisHostType& cellBasis,
138  const ordinal_type subcellId,
139  const ordinal_type subcellOrt,
140  const bool inverse) {
141 
142 #ifdef HAVE_INTREPID2_DEBUG
143  Debug::check_getCoeffMatrix_HGRAD(subcellBasis,cellBasis,subcellId,subcellOrt);
144 #endif
145 
146  using host_device_type = typename Kokkos::HostSpace::device_type;
147  using value_type = typename OutputViewType::non_const_value_type;
148 
149  //
150  // Topology
151  //
152 
153  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
154  const shards::CellTopology subcellTopo = subcellBasis.getBaseCellTopology();
155  const ordinal_type cellDim = cellTopo.getDimension();
156  const ordinal_type subcellDim = subcellTopo.getDimension();
157  const auto subcellBaseKey = subcellTopo.getBaseKey();
158  const ordinal_type numCellBasis = cellBasis.getCardinality();
159  const ordinal_type numSubcellBasis = subcellBasis.getCardinality();
160  const ordinal_type ndofSubcell = cellBasis.getDofCount(subcellDim,subcellId);
161 
162  //
163  // Reference points
164  //
165 
166  // Reference points \xi_j on the subcell
167  Kokkos::DynRankView<value_type,host_device_type> refPtsSubcell("refPtsSubcell", ndofSubcell, subcellDim);
168  auto latticeSize=PointTools::getLatticeSize(subcellTopo, subcellBasis.getDegree(), 1);
169 
170  INTREPID2_TEST_FOR_EXCEPTION( latticeSize != ndofSubcell,
171  std::logic_error,
172  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): " \
173  "Lattice size should be equal to the number of subcell internal DoFs");
174  PointTools::getLattice(refPtsSubcell, subcellTopo, subcellBasis.getDegree(), 1, POINTTYPE_WARPBLEND);
175 
176  // map the points into the parent, cell accounting for orientation
177  Kokkos::DynRankView<value_type,host_device_type> refPtsCell("refPtsCell", ndofSubcell, cellDim);
178  // refPtsCell = F_s (\eta_o (refPtsSubcell))
179  if(cellDim == subcellDim) //the cell is a side of dimension 1 or 2.
180  mapToModifiedReference(refPtsCell,refPtsSubcell,subcellBaseKey,subcellOrt);
181  else {
182  auto subcellParam = Intrepid2::RefSubcellParametrization<host_device_type>::get(subcellDim, cellTopo.getKey());
183  mapSubcellCoordsToRefCell(refPtsCell,refPtsSubcell, subcellParam, subcellBaseKey, subcellId, subcellOrt);
184  }
185 
186  //
187  // Bases evaluation on the reference points
188  //
189 
190  // cellBasisValues = \psi_k(F_s (\eta_o (\xi_j)))
191  Kokkos::DynRankView<value_type,host_device_type> cellBasisValues("cellBasisValues", numCellBasis, ndofSubcell);
192 
193  // subcellBasisValues = \phi_i (\xi_j)
194  Kokkos::DynRankView<value_type,host_device_type> subcellBasisValues("subcellBasisValues", numSubcellBasis, ndofSubcell);
195 
196  cellBasis.getValues(cellBasisValues, refPtsCell, OPERATOR_VALUE);
197  subcellBasis.getValues(subcellBasisValues, refPtsSubcell, OPERATOR_VALUE);
198 
199  //
200  // Compute Psi_jk = \psi_k(F_s (\eta_o (\xi_j))) and Phi_ji = \phi_i (\xi_j),
201  // and solve
202  // Psi A^T = Phi
203  //
204 
205  // construct Psi and Phi matrices. LAPACK wants left layout
206  Kokkos::DynRankView<value_type,Kokkos::LayoutLeft,host_device_type>
207  PsiMat("PsiMat", ndofSubcell, ndofSubcell),
208  PhiMat("PhiMat", ndofSubcell, ndofSubcell),
209  RefMat,
210  OrtMat;
211 
212  auto cellTagToOrdinal = cellBasis.getAllDofOrdinal();
213  auto subcellTagToOrdinal = subcellBasis.getAllDofOrdinal();
214 
215  for (ordinal_type i=0;i<ndofSubcell;++i) {
216  const ordinal_type ic = cellTagToOrdinal(subcellDim, subcellId, i);
217  const ordinal_type isc = subcellTagToOrdinal(subcellDim, 0, i);
218  for (ordinal_type j=0;j<ndofSubcell;++j) {
219  PsiMat(j, i) = cellBasisValues(ic,j);
220  PhiMat(j, i) = subcellBasisValues(isc,j);
221  }
222  }
223 
224  RefMat = inverse ? PhiMat : PsiMat;
225  OrtMat = inverse ? PsiMat : PhiMat;
226 
227  // Solve the system
228  {
229  Teuchos::LAPACK<ordinal_type,value_type> lapack;
230  ordinal_type info = 0;
231 
232  Kokkos::DynRankView<ordinal_type,Kokkos::LayoutLeft,host_device_type> pivVec("pivVec", ndofSubcell);
233  lapack.GESV(ndofSubcell, ndofSubcell,
234  RefMat.data(),
235  RefMat.stride_1(),
236  pivVec.data(),
237  OrtMat.data(),
238  OrtMat.stride_1(),
239  &info);
240 
241  if (info) {
242  std::stringstream ss;
243  ss << ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HGRAD): "
244  << "LAPACK return with error code: "
245  << info;
246  INTREPID2_TEST_FOR_EXCEPTION( true, std::runtime_error, ss.str().c_str() );
247  }
248 
249  //After solving the system w/ LAPACK, Phi contains A^T
250 
251  // transpose B and clean up numerical noise (for permutation matrices)
252  const double eps = tolerence();
253  for (ordinal_type i=0;i<ndofSubcell;++i) {
254  auto intmatii = std::round(OrtMat(i,i));
255  OrtMat(i,i) = (std::abs(OrtMat(i,i) - intmatii) < eps) ? intmatii : OrtMat(i,i);
256  for (ordinal_type j=i+1;j<ndofSubcell;++j) {
257  auto matij = OrtMat(i,j);
258 
259  auto intmatji = std::round(OrtMat(j,i));
260  OrtMat(i,j) = (std::abs(OrtMat(j,i) - intmatji) < eps) ? intmatji : OrtMat(j,i);
261 
262  auto intmatij = std::round(matij);
263  OrtMat(j,i) = (std::abs(matij - intmatij) < eps) ? intmatij : matij;
264  }
265  }
266 
267  }
268 
269  // Print A Matrix
270  /*
271  {
272  std::cout << "|";
273  for (ordinal_type i=0;i<ndofSubcell;++i) {
274  for (ordinal_type j=0;j<ndofSubcell;++j) {
275  std::cout << OrtMat(i,j) << " ";
276  }
277  std::cout << "| ";
278  }
279  std::cout <<std::endl;
280  }
281  */
282 
283  {
284  // move the data to original device memory
285  const Kokkos::pair<ordinal_type,ordinal_type> range(0, ndofSubcell);
286  auto suboutput = Kokkos::subview(output, range, range);
287  auto tmp = Kokkos::create_mirror_view_and_copy(typename OutputViewType::device_type::memory_space(), OrtMat);
288  Kokkos::deep_copy(suboutput, tmp);
289  }
290 }
291 }
292 
293 }
294 #endif
static void getCoeffMatrix_HGRAD(OutputViewType &output, const subcellBasisHostType &subcellBasis, const cellBasisHostType &cellBasis, const ordinal_type subcellId, const ordinal_type subcellOrt, const bool inverse=false)
Compute orientation matrix for HGRAD basis for a given subcell and its reference basis.
static KOKKOS_INLINE_FUNCTION void mapSubcellCoordsToRefCell(coordsViewType cellCoords, const subcellCoordsViewType subCellCoords, const ParamViewType subcellParametrization, const unsigned subcellTopoKey, const ordinal_type subCellOrd, const ordinal_type ort)
Maps points defined on the subCell manifold into the parent Cell accounting for orientation.
static ConstViewType get(const ordinal_type subcellDim, const unsigned parentCellKey)
Returns a Kokkos view with the coefficients of the parametrization maps for the edges or faces of a r...
static void getLattice(Kokkos::DynRankView< pointValueType, pointProperties...> points, const shards::CellTopology cellType, const ordinal_type order, const ordinal_type offset=0, const EPointType pointType=POINTTYPE_EQUISPACED)
Computes a lattice of points of a given order on a reference simplex, quadrilateral or hexahedron (cu...
static void mapToModifiedReference(outPointViewType outPoints, const refPointViewType refPoints, const shards::CellTopology cellTopo, const ordinal_type cellOrt=0)
Computes modified parameterization maps of 1- and 2-subcells with orientation.
static ordinal_type getLatticeSize(const shards::CellTopology cellType, const ordinal_type order, const ordinal_type offset=0)
Computes the number of points in a lattice of a given order on a simplex (currently disabled for othe...