Intrepid2
Intrepid2_OrientationToolsDefCoeffMatrix_HVOL.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_HVOL_HPP__
57 #define __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HVOL_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 cellBasisType>
71 inline
72 void
73 check_getCoeffMatrix_HVOL(const cellBasisType& cellBasis,
74  const ordinal_type cellOrt) {
75 
76  // populate points on a subcell and map to subcell
77  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
78  const ordinal_type cellDim = cellTopo.getDimension();
79 
80  INTREPID2_TEST_FOR_EXCEPTION( cellDim > 2,
81  std::logic_error,
82  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HVOL): " \
83  "HVOL orientation supported only for (side) cells with dimension less than 3.");
84 
85  const auto cellBaseKey = cellTopo.getBaseKey();
86 
87  INTREPID2_TEST_FOR_EXCEPTION( cellBaseKey != shards::Line<>::key &&
88  cellBaseKey != shards::Quadrilateral<>::key &&
89  cellBaseKey != shards::Triangle<>::key,
90  std::logic_error,
91  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HVOL): " \
92  "cellBasis must have line, quad, or triangle topology.");
93 
94 
95  //
96  // Function space
97  //
98 
99  {
100  INTREPID2_TEST_FOR_EXCEPTION( cellBasis.getFunctionSpace() != FUNCTION_SPACE_HVOL,
101  std::logic_error,
102  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HVOL): " \
103  "cellBasis function space is not HVOL.");
104  }
105  }
106 #endif
107 } // Debug namespace
108 
109 template<typename OutputViewType,
110 typename cellBasisHostType>
111 inline
112 void
114 getCoeffMatrix_HVOL(OutputViewType &output,
115  const cellBasisHostType& cellBasis,
116  const ordinal_type cellOrt,
117  const bool inverse) {
118 
119 #ifdef HAVE_INTREPID2_DEBUG
120  Debug::check_getCoeffMatrix_HVOL(cellBasis,cellOrt);
121 #endif
122 
123  using host_device_type = typename Kokkos::HostSpace::device_type;
124  using value_type = typename OutputViewType::non_const_value_type;
125 
126  //
127  // Topology
128  //
129 
130  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
131  const ordinal_type cellDim = cellTopo.getDimension();
132  const auto cellBaseKey = cellTopo.getBaseKey();
133  const ordinal_type cardinality = cellBasis.getCardinality();
134 
135  //
136  // Reference points
137  //
138 
139  // Reference points \xi_j on the subcell
140  Kokkos::DynRankView<value_type,host_device_type> refPtsCell("refPtsCell", cardinality, cellDim),refPtsCellNotOriented("refPtsCellNotOriented", cardinality, cellDim);
141 
142  ordinal_type latticeOffset(1);
143 
144  // this work for line and quadrilateral topologies
145  ordinal_type latticeOrder = (cellTopo.getBaseKey() == shards::Triangle<>::key) ?
146  cellBasis.getDegree() + 3 * latticeOffset : // triangle
147  cellBasis.getDegree() + 2 * latticeOffset; // line and quad
148 
149  PointTools::getLattice(refPtsCellNotOriented, cellTopo, latticeOrder, 1, POINTTYPE_WARPBLEND);
150 
151  // map the points into the parent, cell accounting for orientation
152  mapToModifiedReference(refPtsCell,refPtsCellNotOriented,cellBaseKey,cellOrt);
153 
154  //
155  // Bases evaluation on the reference points
156  //
157 
158  // cellBasisValues = \psi_k(\eta_o (\xi_j))
159  Kokkos::DynRankView<value_type,host_device_type> cellBasisValues("cellBasisValues", cardinality, cardinality);
160 
161  // basisValues = \phi_i (\xi_j)
162  Kokkos::DynRankView<value_type,host_device_type> nonOrientedBasisValues("subcellBasisValues", cardinality, cardinality);
163 
164  cellBasis.getValues(cellBasisValues, refPtsCell, OPERATOR_VALUE);
165  cellBasis.getValues(nonOrientedBasisValues, refPtsCellNotOriented, OPERATOR_VALUE);
166 
167  //
168  // Compute Psi_jk = \psi_k(\eta_o (\xi_j)) det(J_\eta) and Phi_ji = \phi_i (\xi_j),
169  // and solve
170  // Psi A^T = Phi
171  //
172 
173  // construct Psi and Phi matrices. LAPACK wants left layout
174  Kokkos::DynRankView<value_type,Kokkos::LayoutLeft,host_device_type>
175  PsiMat("PsiMat", cardinality, cardinality),
176  PhiMat("PhiMat", cardinality, cardinality),
177  RefMat,
178  OrtMat;
179 
180  auto cellTagToOrdinal = cellBasis.getAllDofOrdinal();
181 
182  Kokkos::DynRankView<value_type,host_device_type> jac("jacobian",cellDim,cellDim);
184  value_type jacDet(0);
185  if(cellDim == 2) {
186  jacDet = jac(0,0)*jac(1,1)-jac(0,1)*jac(1,0);
187  } else { //celldim == 1
188  jacDet = jac(0,0);
189  }
190 
191  for (ordinal_type i=0;i<cardinality;++i) {
192  const ordinal_type ic = cellTagToOrdinal(cellDim, 0, i);
193  for (ordinal_type j=0;j<cardinality;++j) {
194  PsiMat(j, i) = cellBasisValues(ic,j)*jacDet;
195  PhiMat(j, i) = nonOrientedBasisValues(ic,j);
196  }
197  }
198 
199  RefMat = inverse ? PhiMat : PsiMat;
200  OrtMat = inverse ? PsiMat : PhiMat;
201 
202  // Solve the system
203  {
204  Teuchos::LAPACK<ordinal_type,value_type> lapack;
205  ordinal_type info = 0;
206 
207  Kokkos::DynRankView<ordinal_type,Kokkos::LayoutLeft,host_device_type> pivVec("pivVec", cardinality);
208  lapack.GESV(cardinality, cardinality,
209  RefMat.data(),
210  RefMat.stride_1(),
211  pivVec.data(),
212  OrtMat.data(),
213  OrtMat.stride_1(),
214  &info);
215 
216  if (info) {
217  std::stringstream ss;
218  ss << ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HVOL): "
219  << "LAPACK return with error code: "
220  << info;
221  INTREPID2_TEST_FOR_EXCEPTION( true, std::runtime_error, ss.str().c_str() );
222  }
223 
224  //After solving the system w/ LAPACK, Phi contains A^T
225 
226  // transpose B and clean up numerical noise (for permutation matrices)
227  const double eps = tolerence();
228  for (ordinal_type i=0;i<cardinality;++i) {
229  auto intmatii = std::round(OrtMat(i,i));
230  OrtMat(i,i) = (std::abs(OrtMat(i,i) - intmatii) < eps) ? intmatii : OrtMat(i,i);
231  for (ordinal_type j=i+1;j<cardinality;++j) {
232  auto matij = OrtMat(i,j);
233 
234  auto intmatji = std::round(OrtMat(j,i));
235  OrtMat(i,j) = (std::abs(OrtMat(j,i) - intmatji) < eps) ? intmatji : OrtMat(j,i);
236 
237  auto intmatij = std::round(matij);
238  OrtMat(j,i) = (std::abs(matij - intmatij) < eps) ? intmatij : matij;
239  }
240  }
241 
242  }
243 
244  // Print A Matrix
245  /*
246  {
247  std::cout << "Ort: " << cellOrt << ": |";
248  for (ordinal_type i=0;i<cardinality;++i) {
249  for (ordinal_type j=0;j<cardinality;++j) {
250  std::cout << OrtMat(i,j) << " ";
251  }
252  std::cout << "| ";
253  }
254  std::cout <<std::endl;
255  }
256  */
257 
258  {
259  // move the data to original device memory
260  const Kokkos::pair<ordinal_type,ordinal_type> range(0, cardinality);
261  auto suboutput = Kokkos::subview(output, range, range);
262  auto tmp = Kokkos::create_mirror_view_and_copy(typename OutputViewType::device_type::memory_space(), OrtMat);
263  Kokkos::deep_copy(suboutput, tmp);
264  }
265 }
266 }
267 
268 }
269 #endif
static void getJacobianOfOrientationMap(JacobianViewType jacobian, const shards::CellTopology cellTopo, const ordinal_type cellOrt)
Computes jacobian of the parameterization maps of 1- and 2-subcells with orientation.
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 getCoeffMatrix_HVOL(OutputViewType &output, const cellBasisHostType &cellBasis, const ordinal_type cellOrt, const bool inverse=false)
Compute orientation matrix for HVOL basis for a given (2D or 1D) cell and its reference basis...
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.