Intrepid2
Intrepid2_OrientationToolsDefCoeffMatrix_HCURL.hpp
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 
62 #ifndef __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HCURL_HPP__
63 #define __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HCURL_HPP__
64 
68 
69 // disable clang warnings
70 #if defined (__clang__) && !defined (__INTEL_COMPILER)
71 #pragma clang system_header
72 #endif
73 
74 namespace Intrepid2 {
75 
76 namespace Impl {
77 
78 namespace Debug {
79 
80 #ifdef HAVE_INTREPID2_DEBUG
81 template<typename subcellBasisType,
82 typename cellBasisType>
83 inline
84 void
85 check_getCoeffMatrix_HCURL(const subcellBasisType& subcellBasis,
86  const cellBasisType& cellBasis,
87  const ordinal_type subcellId,
88  const ordinal_type subcellOrt) {
89  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
90  const shards::CellTopology subcellTopo = subcellBasis.getBaseCellTopology();
91 
92  const ordinal_type cellDim = cellTopo.getDimension();
93  const ordinal_type subcellDim = subcellTopo.getDimension();
94 
95 
96 
97  INTREPID2_TEST_FOR_EXCEPTION( subcellDim > cellDim,
98  std::logic_error,
99  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
100  "cellDim cannot be smaller than subcellDim.");
101 
102  const auto subcellBaseKey = subcellTopo.getBaseKey();
103  const auto cellBaseKey = cellTopo.getBaseKey();
104 
105  INTREPID2_TEST_FOR_EXCEPTION( subcellBaseKey != shards::Line<>::key &&
106  subcellBaseKey != shards::Quadrilateral<>::key &&
107  subcellBaseKey != shards::Triangle<>::key,
108  std::logic_error,
109  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
110  "subcellBasis must have line, quad, or triangle topology.");
111 
112  INTREPID2_TEST_FOR_EXCEPTION( cellBaseKey != shards::Quadrilateral<>::key &&
113  cellBaseKey != shards::Triangle<>::key &&
114  cellBaseKey != shards::Hexahedron<>::key &&
115  cellBaseKey != shards::Wedge<>::key &&
116  cellBaseKey != shards::Tetrahedron<>::key,
117  std::logic_error,
118  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
119  "cellBasis must have quad, triangle, hexhedron or tetrahedron topology.");
120 
121  //
122  // Function space
123  //
124  {
125  const bool cellBasisIsHCURL = cellBasis.getFunctionSpace() == FUNCTION_SPACE_HCURL;
126 
127 
128  if (cellBasisIsHCURL) {
129  const bool subcellBasisIsHGRAD = subcellBasis.getFunctionSpace() == FUNCTION_SPACE_HGRAD;
130  const bool subcellBasisIsHVOL = subcellBasis.getFunctionSpace() == FUNCTION_SPACE_HVOL;
131  const bool subcellBasisIsHCURL = subcellBasis.getFunctionSpace() == FUNCTION_SPACE_HCURL;
132  const bool cellIsTri = cellBaseKey == shards::Triangle<>::key;
133  const bool cellIsTet = cellBaseKey == shards::Tetrahedron<>::key;
134  const bool cellIsHex = cellBaseKey == shards::Hexahedron<>::key;
135  const bool cellIsQuad = cellBaseKey == shards::Quadrilateral<>::key;
136 
137 
138  // edge hcurl is hgrad with gauss legendre points
139  switch (subcellDim) {
140  case 1: {
141  //TODO: Hex, QUAD, TET and TRI element should have the same 1d basis
142  if (cellIsHex || cellIsQuad) {
143  INTREPID2_TEST_FOR_EXCEPTION( !(subcellBasisIsHGRAD||subcellBasisIsHVOL),
144  std::logic_error,
145  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
146  "subcellBasis function space (1d) is not consistent to cellBasis.");
147  } else if (cellIsTet || cellIsTri) {
148  INTREPID2_TEST_FOR_EXCEPTION( !subcellBasisIsHVOL,
149  std::logic_error,
150  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
151  "subcellBasis function space (1d) is not consistent to cellBasis.");
152  }
153  break;
154  }
155  case 2: {
156  INTREPID2_TEST_FOR_EXCEPTION( !subcellBasisIsHCURL,
157  std::logic_error,
158  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
159  "subcellBasis function space (2d) is not consistent to cellBasis.");
160  break;
161  }
162  }
163  }
164  }
165 }
166 #endif
167 }
168 
169 template<typename OutputViewType,
170 typename subcellBasisHostType,
171 typename cellBasisHostType>
172 inline
173 void
175 getCoeffMatrix_HCURL(OutputViewType &output,
176  const subcellBasisHostType& subcellBasis,
177  const cellBasisHostType& cellBasis,
178  const ordinal_type subcellId,
179  const ordinal_type subcellOrt,
180  const bool inverse) {
181 
182 #ifdef HAVE_INTREPID2_DEBUG
183  Debug::check_getCoeffMatrix_HCURL(subcellBasis,cellBasis,subcellId,subcellOrt);
184 #endif
185 
186  using value_type = typename OutputViewType::non_const_value_type;
187  using host_device_type = typename Kokkos::HostSpace::device_type;
188 
189  //
190  // Topology
191  //
192  // populate points on a subcell and map to subcell
193  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
194  const shards::CellTopology subcellTopo = subcellBasis.getBaseCellTopology();
195  const ordinal_type cellDim = cellTopo.getDimension();
196  const ordinal_type subcellDim = subcellTopo.getDimension();
197  const auto subcellBaseKey = subcellTopo.getBaseKey();
198  const ordinal_type numCellBasis = cellBasis.getCardinality();
199  const ordinal_type numSubcellBasis = subcellBasis.getCardinality();
200  const ordinal_type ndofSubcell = cellBasis.getDofCount(subcellDim,subcellId);
201 
202  // Compute reference points \xi_j and tangents t_j on the subcell
203  // To do so we use the DoF coordinates and DoF coefficients of a Lagrangian HCURL basis
204  // on the subcell spanning the same space as the bases \phi_j
205 
206  // Tangents t_j
207  Kokkos::DynRankView<value_type,host_device_type> subcellTangents("subcellTangents", numSubcellBasis, subcellDim);
208  auto degree = subcellBasis.getDegree();
209  BasisPtr<host_device_type, value_type, value_type> basisPtr;
210  if(subcellBaseKey == shards::Line<>::key) {
212  basisPtr->getDofCoeffs(Kokkos::subview(subcellTangents, Kokkos::ALL(),0));
213  } else if (subcellBaseKey == shards::Triangle<>::key) {
215  basisPtr->getDofCoeffs(subcellTangents);
216  } else if (subcellBaseKey == shards::Quadrilateral<>::key) {
218  basisPtr->getDofCoeffs(subcellTangents);
219  }
220 
221  // coordinates \xi_j
222  Kokkos::DynRankView<value_type,host_device_type> subcellDofCoords("subcellDofCoords", basisPtr->getCardinality(), subcellDim);
223  basisPtr->getDofCoords(subcellDofCoords);
224  INTREPID2_TEST_FOR_EXCEPTION( basisPtr->getDofCount(subcellDim,0) != ndofSubcell,
225  std::logic_error,
226  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
227  "the number of basisPtr internal DoFs should equate those of the subcell");
228 
229  // restrict \xi_j (and corresponding t_j) to points internal to the HCURL basis
230  Kokkos::DynRankView<value_type,host_device_type> refPtsSubcell("refPtsSubcell", ndofSubcell, subcellDim);
231  Kokkos::DynRankView<value_type,host_device_type> refSubcellTangents("subcellTangents", ndofSubcell, subcellDim);
232  auto tagToOrdinal = basisPtr->getAllDofOrdinal();
233  for (ordinal_type i=0;i<ndofSubcell;++i) {
234  ordinal_type isc = tagToOrdinal(subcellDim, 0, i);
235  for(ordinal_type d=0; d <subcellDim; ++d){
236  refPtsSubcell(i,d) = subcellDofCoords(isc,d);
237  for(ordinal_type k=0; k <subcellDim; ++k)
238  refSubcellTangents(i,d) = subcellTangents(isc,d);
239  }
240  }
241 
242  //
243  // Bases evaluation on the reference points
244  //
245 
246  // subcellBasisValues = \phi_i (\xi_j)
247  Kokkos::DynRankView<value_type,host_device_type> subCellValues("subCellValues", numSubcellBasis, ndofSubcell, subcellDim);
248  if(subcellDim==1) {
249  auto lineValues = Kokkos::subview(subCellValues, Kokkos::ALL(), Kokkos::ALL(), 0);
250  subcellBasis.getValues(lineValues, refPtsSubcell, OPERATOR_VALUE);
251  } else {
252  subcellBasis.getValues(subCellValues, refPtsSubcell, OPERATOR_VALUE);
253  }
254 
255  //
256  // Basis evaluation on the reference points
257  //
258 
259  Kokkos::DynRankView<value_type,host_device_type> refPtsCell("refPtsCell", ndofSubcell, cellDim);
260  Kokkos::DynRankView<value_type,host_device_type> trJacobianF("trJacobianF", subcellDim, cellDim );
261 
262  if(cellDim == subcellDim) {
263  // refPtsCell = \eta_o (refPtsSubcell)
264  mapToModifiedReference(refPtsCell,refPtsSubcell,subcellBaseKey,subcellOrt);
265 
266  //mapping tangents t_j into parent cell, i.e. computing J_F J_\eta t_j
267  Kokkos::DynRankView<value_type,host_device_type> jac("data", subcellDim, subcellDim);
268  getJacobianOfOrientationMap(jac,subcellBaseKey,subcellOrt);
269  for(ordinal_type d=0; d<subcellDim; ++d)
270  for(ordinal_type j=0; j<cellDim; ++j) {
271  trJacobianF(j,d) = jac(d,j);
272  }
273  }
274  else {
275  auto subcellParam = Intrepid2::RefSubcellParametrization<host_device_type>::get(subcellDim, cellTopo.getKey());
276  // refPtsCell = F_s (\eta_o (refPtsSubcell))
277  mapSubcellCoordsToRefCell(refPtsCell,refPtsSubcell, subcellParam, subcellBaseKey, subcellId, subcellOrt);
278 
279  //mapping tangents t_j into parent cell, i.e. computing J_F J_\eta t_j
280  OrientationTools::getRefSubcellTangents(trJacobianF, subcellParam, subcellBaseKey, subcellId, subcellOrt);
281  }
282 
283  // cellBasisValues = \psi_k(F_s (\eta_o (\xi_j)))
284  Kokkos::DynRankView<value_type,host_device_type> cellBasisValues("cellBasisValues", numCellBasis, ndofSubcell, cellDim);
285  cellBasis.getValues(cellBasisValues, refPtsCell, OPERATOR_VALUE);
286 
287  //
288  // Compute Psi_jk = \psi_k(F_s (\eta_o (\xi_j))) \cdot (J_F J_\eta t_j)
289  // and Phi_ji = \phi_i (\xi_j) \ctot t_j, and solve
290  // Psi A^T = Phi
291  //
292 
293  // construct Psi and Phi matrices. LAPACK wants left layout
294  Kokkos::DynRankView<value_type,Kokkos::LayoutLeft,host_device_type> // left layout for lapack
295  PsiMat("PsiMat", ndofSubcell, ndofSubcell),
296  PhiMat("PhiMat", ndofSubcell, ndofSubcell),
297  RefMat,
298  OrtMat;
299 
300  auto cellTagToOrdinal = cellBasis.getAllDofOrdinal();
301  auto subcellTagToOrdinal = subcellBasis.getAllDofOrdinal();
302 
303  for (ordinal_type i=0;i<ndofSubcell;++i) {
304  const ordinal_type ic = cellTagToOrdinal(subcellDim, subcellId, i);
305  for (ordinal_type j=0;j<ndofSubcell;++j) {
306  const ordinal_type isc = subcellTagToOrdinal(subcellDim, 0, i);
307  value_type refEntry = 0, ortEntry =0;
308  for (ordinal_type k=0;k<subcellDim;++k) {
309  ortEntry += subCellValues(isc,j,k)*refSubcellTangents(j,k);
310  for (ordinal_type d=0; d<cellDim; ++d)
311  refEntry += cellBasisValues(ic,j,d)*trJacobianF(k,d)*refSubcellTangents(j,k);
312  }
313  PsiMat(j,i) = refEntry;
314  PhiMat(j,i) = ortEntry;
315  }
316  }
317 
318  RefMat = inverse ? PhiMat : PsiMat;
319  OrtMat = inverse ? PsiMat : PhiMat;
320 
321  // Solve the system using Lapack
322  {
323  Teuchos::LAPACK<ordinal_type,value_type> lapack;
324  ordinal_type info = 0;
325 
326 
327  /*
328  Kokkos::View<value_type**,Kokkos::LayoutLeft,host_space_type> work("pivVec", 2*ndofSubcell, 1);
329  lapack.GELS('N', ndofSubcell*card, ndofSubcell, ndofSubcell,
330  RefMat.data(),
331  RefMat.stride_1(),
332  OrtMat.data(),
333  OrtMat.stride_1(),
334  work.data(), work.extent(0),
335  &info);
336 
337  */
338  Kokkos::DynRankView<ordinal_type,host_device_type> pivVec("pivVec", ndofSubcell);
339  lapack.GESV(ndofSubcell, ndofSubcell,
340  RefMat.data(),
341  RefMat.stride_1(),
342  pivVec.data(),
343  OrtMat.data(),
344  OrtMat.stride_1(),
345  &info);
346  //*/
347  if (info) {
348  std::stringstream ss;
349  ss << ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
350  << "LAPACK return with error code: "
351  << info;
352  INTREPID2_TEST_FOR_EXCEPTION( true, std::runtime_error, ss.str().c_str() );
353  }
354 
355  {
356  //After solving the system w/ LAPACK, Phi contains A^T (or A^-T)
357  // transpose and clean up numerical noise (for permutation matrices)
358  const double eps = tolerence();
359  for (ordinal_type i=0;i<ndofSubcell;++i) {
360  auto intmatii = std::round(OrtMat(i,i));
361  OrtMat(i,i) = (std::abs(OrtMat(i,i) - intmatii) < eps) ? intmatii : OrtMat(i,i);
362  for (ordinal_type j=i+1;j<ndofSubcell;++j) {
363  auto matij = OrtMat(i,j);
364 
365  auto intmatji = std::round(OrtMat(j,i));
366  OrtMat(i,j) = (std::abs(OrtMat(j,i) - intmatji) < eps) ? intmatji : OrtMat(j,i);
367 
368  auto intmatij = std::round(matij);
369  OrtMat(j,i) = (std::abs(matij - intmatij) < eps) ? intmatij : matij;
370  }
371  }
372  }
373 
374 
375 
376  // Print A matrix
377  /*
378  {
379  std::cout << "|";
380  for (ordinal_type i=0;i<ndofSubcell;++i) {
381  for (ordinal_type j=0;j<ndofSubcell;++j) {
382  std::cout << OrtMat(i,j) << " ";
383  }
384  std::cout << "| ";
385  }
386  std::cout <<std::endl;
387  }
388  */
389 
390 
391  }
392 
393  {
394  // move the data to original device memory
395  const Kokkos::pair<ordinal_type,ordinal_type> range(0, ndofSubcell);
396  auto suboutput = Kokkos::subview(output, range, range);
397  auto tmp = Kokkos::create_mirror_view_and_copy(typename OutputViewType::device_type::memory_space(), OrtMat);
398  Kokkos::deep_copy(suboutput, tmp);
399  }
400 }
401 }
402 
403 }
404 #endif
Implementation of the default H(curl)-compatible FEM basis on Quadrilateral cell. ...
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 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 getCoeffMatrix_HCURL(OutputViewType &output, const subcellBasisHostType &subcellBasis, const cellBasisHostType &cellBasis, const ordinal_type subcellId, const ordinal_type subcellOrt, const bool inverse=false)
Compute orientation matrix for HCURL basis for a given subcell and its reference basis.
static KOKKOS_INLINE_FUNCTION void getRefSubcellTangents(TanViewType tangents, const ParamViewType subCellParametrization, const unsigned subcellTopoKey, const ordinal_type subCellOrd, const ordinal_type ort)
Computes the (oriented) subCell tangents.
Header file for the Intrepid2::Basis_HVOL_LINE_Cn_FEM class.
Header file for the Intrepid2::Basis_HCURL_QUAD_In_FEM class.
Implementation of the locally HVOL-compatible FEM basis of variable order on the [-1,1] reference line cell, using Lagrange polynomials.
Implementation of the default H(curl)-compatible Nedelec (first kind) basis of arbitrary degree on Tr...
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.
Header file for the Intrepid2::Basis_HCURL_TRI_In_FEM class.