Intrepid2
Intrepid2_OrientationToolsDefCoeffMatrix_HCURL.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 
59 #ifndef __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HCURL_HPP__
60 #define __INTREPID2_ORIENTATIONTOOLS_DEF_COEFF_MATRIX_HCURL_HPP__
61 
62 // disable clang warnings
63 #if defined (__clang__) && !defined (__INTEL_COMPILER)
64 #pragma clang system_header
65 #endif
66 
67 namespace Intrepid2 {
68 
69  namespace Impl {
70 
71  template<typename outputViewType,
72  typename subcellBasisType,
73  typename cellBasisType>
74  inline
75  void
77  getCoeffMatrix_HCURL(outputViewType &output,
78  const subcellBasisType subcellBasis,
79  const cellBasisType cellBasis,
80  const ordinal_type subcellId,
81  const ordinal_type subcellOrt) {
82  typedef typename outputViewType::execution_space space_type;
83  typedef typename outputViewType::value_type value_type;
84 
85  // with shards, everything should be computed on host space
86  typedef typename
87  Kokkos::Impl::is_space<space_type>::host_mirror_space::execution_space host_space_type;
88 
89  typedef Kokkos::DynRankView<value_type,host_space_type> DynRankViewHostType;
90 
91  //
92  // Topology
93  //
94  // populate points on a subcell and map to subcell
95  const shards::CellTopology cellTopo = cellBasis.getBaseCellTopology();
96  const shards::CellTopology subcellTopo = subcellBasis.getBaseCellTopology();
97 
98  const ordinal_type cellDim = cellTopo.getDimension();
99  const ordinal_type subcellDim = subcellTopo.getDimension();
100 
101  INTREPID2_TEST_FOR_EXCEPTION( subcellDim >= cellDim,
102  std::logic_error,
103  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
104  "cellDim must be greater than subcellDim.");
105 
106  const auto subcellBaseKey = subcellTopo.getBaseKey();
107  const auto cellBaseKey = cellTopo.getBaseKey();
108 
109  INTREPID2_TEST_FOR_EXCEPTION( subcellBaseKey != shards::Line<>::key &&
110  subcellBaseKey != shards::Quadrilateral<>::key &&
111  subcellBaseKey != shards::Triangle<>::key,
112  std::logic_error,
113  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
114  "subcellBasis must have line, quad, or triangle topology.");
115 
116  INTREPID2_TEST_FOR_EXCEPTION( cellBaseKey != shards::Quadrilateral<>::key &&
117  cellBaseKey != shards::Triangle<>::key &&
118  cellBaseKey != shards::Hexahedron<>::key &&
119  cellBaseKey != shards::Tetrahedron<>::key,
120  std::logic_error,
121  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
122  "cellBasis must have quad, triangle, hexhedron or tetrahedron topology.");
123 
124  //
125  // Function space
126  //
127  {
128  const std::string cellBasisName(cellBasis.getName());
129  if (cellBasisName.find("HCURL") != std::string::npos) {
130  const std::string subcellBasisName(subcellBasis.getName());
131  // edge hcurl is hgrad with gauss legendre points
132  switch (subcellDim) {
133  case 1: {
134  //TODO: Hex, QUAD, TET and TRI element should have the same 1d basis
135  if ((cellBasisName.find("HEX") != std::string::npos) || (cellBasisName.find("QUAD") != std::string::npos)) {
136  INTREPID2_TEST_FOR_EXCEPTION( subcellBasisName.find("HGRAD") == std::string::npos,
137  std::logic_error,
138  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
139  "subcellBasis function space (1d) is not consistent to cellBasis.");
140  } else if ((cellBasisName.find("TET") != std::string::npos) || (cellBasisName.find("TRI") != std::string::npos)) {
141  INTREPID2_TEST_FOR_EXCEPTION( subcellBasisName.find("HVOL") == std::string::npos,
142  std::logic_error,
143  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
144  "subcellBasis function space (1d) is not consistent to cellBasis.");
145  }
146  break;
147  }
148  case 2: {
149  INTREPID2_TEST_FOR_EXCEPTION( subcellBasisName.find("HCURL") == std::string::npos,
150  std::logic_error,
151  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
152  "subcellBasis function space (2d) is not consistent to cellBasis.");
153  break;
154  }
155  }
156  }
157  }
158 
159  const ordinal_type numCellBasis = cellBasis.getCardinality();
160  const ordinal_type numSubcellBasis = subcellBasis.getCardinality();
161 
162  const ordinal_type ordSubcell = cellBasis.getDofOrdinal(subcellDim, subcellId, 0);
163  INTREPID2_TEST_FOR_EXCEPTION( ordSubcell == -1,
164  std::logic_error,
165  ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): " \
166  "Invalid subcellId returns -1 ordSubcell.");
167 
168  const ordinal_type ndofSubcell = cellBasis.getDofTag(ordSubcell)(3);
169 
170  // reference points on a subcell
171  DynRankViewHostType refPtsSubcell("refPtsSubcell", ndofSubcell, subcellDim);
172  DynRankViewHostType subcellDofCoords("subcellDofCoords", numSubcellBasis, subcellDim);
173  subcellBasis.getDofCoords(subcellDofCoords);
174  for(ordinal_type i=0; i<ndofSubcell; ++i)
175  for(ordinal_type d=0; d <subcellDim; ++d)
176  refPtsSubcell(i,d) = subcellDofCoords(subcellBasis.getDofOrdinal(subcellDim, 0, i),d);
177 
178  // modified points with orientation
179  DynRankViewHostType ortPtsSubcell("ortPtsSubcell", ndofSubcell, subcellDim);
181  refPtsSubcell,
182  subcellTopo,
183  subcellOrt);
184 
185  // map to reference coordinates
186  DynRankViewHostType refPtsCell("refPtsCell", ndofSubcell, cellDim);
188  ortPtsSubcell,
189  subcellDim,
190  subcellId,
191  cellTopo);
192 
193  //
194  // Basis evaluation on the reference points
195  //
196 
197  // evaluate values on the reference cell
198  DynRankViewHostType refValues("refValues", numCellBasis, ndofSubcell, cellDim);
199  cellBasis.getValues(refValues, refPtsCell, OPERATOR_VALUE);
200 
201  // evaluate values on the modified cell
202  DynRankViewHostType subcellTangents("subcellTangents", numSubcellBasis, subcellDim);
203  DynRankViewHostType ortJacobian("ortJacobian", subcellDim, subcellDim);
204  Impl::OrientationTools::getJacobianOfOrientationMap(ortJacobian, subcellTopo, subcellOrt);
205 
206  //
207  // Compute jacobianF
208  //
209 
210  DynRankViewHostType jacobianF("jacobianF", cellDim, subcellDim );
211  switch (subcellBaseKey) {
212  case shards::Line<>::key: {
213  auto lineDofCoeffs = Kokkos::subview(subcellTangents, Kokkos::ALL(),0);
214  subcellBasis.getDofCoeffs(lineDofCoeffs);
215  auto edgeTan = Kokkos::subview(jacobianF, Kokkos::ALL(),0);
216  CellTools<host_space_type>::getReferenceEdgeTangent(edgeTan, subcellId, cellTopo);
217  if((cellBaseKey == shards::Triangle<>::key) || (cellBaseKey == shards::Tetrahedron<>::key))
218  for(ordinal_type i=0; i<cellDim; ++i)
219  edgeTan(i) *= 2.0; //scale by reference tangent
220  break;
221  }
222  case shards::Quadrilateral<>::key:
223  case shards::Triangle<>::key: {
224  subcellBasis.getDofCoeffs(subcellTangents);
225 
226  auto faceTanU = Kokkos::subview(jacobianF, Kokkos::ALL(), 0);
227  auto faceTanV = Kokkos::subview(jacobianF, Kokkos::ALL(), 1);
228  CellTools<host_space_type>::getReferenceFaceTangents(faceTanU, faceTanV,subcellId, cellTopo);
229  break;
230  }
231  default: {
232  INTREPID2_TEST_FOR_EXCEPTION( true, std::runtime_error, "Should not come here" );
233  }
234  }
235 
236  Kokkos::View<value_type**,Kokkos::LayoutLeft,host_space_type> // left layout for lapack
237  refMat("refMat", ndofSubcell, ndofSubcell),
238  ortMat("ortMat", ndofSubcell, ndofSubcell);
239  for (ordinal_type i=0;i<ndofSubcell;++i) {
240  const ordinal_type iout = cellBasis.getDofOrdinal(subcellDim, subcellId, i);
241  for (ordinal_type j=0;j<ndofSubcell;++j) {
242  value_type tmp = 0;
243  const ordinal_type jsc = subcellBasis.getDofOrdinal(subcellDim, 0, j);
244  for (ordinal_type k=0;k<subcellDim;++k)
245  for (ordinal_type l=0;l<subcellDim;++l)
246  for (ordinal_type d=0; d<cellDim; ++d)
247  tmp += refValues(iout,j,d)*jacobianF(d,l)*ortJacobian(l,k)*subcellTangents(jsc,k);
248  refMat(i,j) = tmp;
249  ortMat(j,i) = (i==j); //identity because of the basis Kronecher property
250  }
251  }
252 
253  //
254  // Construct collocation matrix and solve problems
255  //
256 
257  // solve the system using Lapack
258  {
259  Teuchos::LAPACK<ordinal_type,value_type> lapack;
260  ordinal_type info = 0;
261  Kokkos::View<value_type**,Kokkos::LayoutLeft,host_space_type> pivVec("pivVec", ndofSubcell, 1);
262 
263  lapack.GESV(ndofSubcell, ndofSubcell,
264  refMat.data(),
265  refMat.stride_1(),
266  (ordinal_type*)pivVec.data(),
267  ortMat.data(),
268  ortMat.stride_1(),
269  &info);
270 
271  if (info) {
272  std::stringstream ss;
273  ss << ">>> ERROR (Intrepid::OrientationTools::getCoeffMatrix_HCURL): "
274  << "LAPACK return with error code: "
275  << info;
276  INTREPID2_TEST_FOR_EXCEPTION( true, std::runtime_error, ss.str().c_str() );
277  }
278 
279 
280  // Clean up numerical noise
281  {
282  const double eps = threshold();
283  for (ordinal_type i=0;i<ndofSubcell;++i)
284  for (ordinal_type j=i;j<ndofSubcell;++j) {
285  auto intOrtMat = std::round(ortMat(i,j));
286  ortMat(i,j) = (std::abs(ortMat(i,j) - std::round(ortMat(i,j))) < eps) ? intOrtMat : ortMat(i,j);
287  }
288  }
289  }
290 
291  {
292  // move the data to original device memory
293  const Kokkos::pair<ordinal_type,ordinal_type> range(0, ndofSubcell);
294  Kokkos::deep_copy(Kokkos::subview(output, range, range),
295  Kokkos::subview(ortMat, range, range));
296  }
297  }
298  }
299 
300 }
301 #endif
static void getReferenceEdgeTangent(Kokkos::DynRankView< refEdgeTangentValueType, refEdgeTangentProperties...> refEdgeTangent, const ordinal_type edgeOrd, const shards::CellTopology parentCell)
Computes constant tangent vectors to edges of 2D or 3D reference cells.
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 subcellBasisType subcellBasis, const cellBasisType cellBasis, const ordinal_type subcellId, const ordinal_type subcellOrt)
Compute coefficient matrix for HCURL by collocating point values.
static void mapToReferenceSubcell(Kokkos::DynRankView< refSubcellPointValueType, refSubcellPointProperties...> refSubcellPoints, const Kokkos::DynRankView< paramPointValueType, paramPointProperties...> paramPoints, const ordinal_type subcellDim, const ordinal_type subcellOrd, const shards::CellTopology parentCell)
Computes parameterization maps of 1- and 2-subcells of reference cells.
static void getReferenceFaceTangents(Kokkos::DynRankView< refFaceTanUValueType, refFaceTanUProperties...> refFaceTanU, Kokkos::DynRankView< refFaceTanVValueType, refFaceTanVProperties...> refFaceTanV, const ordinal_type faceOrd, const shards::CellTopology parentCell)
Computes pairs of constant tangent vectors to faces of a 3D reference cells.
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.