Intrepid2
Intrepid2_HVOL_TET_Cn_FEMDef.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Intrepid2 Package
4 //
5 // Copyright 2007 NTESS and the Intrepid2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #ifndef __INTREPID2_HVOL_TET_CN_FEM_DEF_HPP__
16 #define __INTREPID2_HVOL_TET_CN_FEM_DEF_HPP__
17 
19 
20 namespace Intrepid2 {
21 
22  // -------------------------------------------------------------------------------------
23 
24  namespace Impl {
25 
26  template<EOperator OpType>
27  template<typename OutputViewType,
28  typename InputViewType,
29  typename WorkViewType,
30  typename VinvViewType>
31  KOKKOS_INLINE_FUNCTION
32  void
33  Basis_HVOL_TET_Cn_FEM::Serial<OpType>::
34  getValues( OutputViewType output,
35  const InputViewType input,
36  WorkViewType work,
37  const VinvViewType vinv ) {
38 
39  constexpr ordinal_type spaceDim = 3;
40  const ordinal_type
41  card = vinv.extent(0),
42  npts = input.extent(0);
43 
44  // compute order
45  ordinal_type order = 0;
46  for (ordinal_type p=0;p<=Parameters::MaxOrder;++p) {
47  if (card == Intrepid2::getPnCardinality<spaceDim>(p)) {
48  order = p;
49  break;
50  }
51  }
52 
53  typedef typename Kokkos::DynRankView<typename InputViewType::value_type, typename WorkViewType::memory_space> ViewType;
54  auto vcprop = Kokkos::common_view_alloc_prop(input);
55  auto ptr = work.data();
56 
57  switch (OpType) {
58  case OPERATOR_VALUE: {
59  const ViewType phis(Kokkos::view_wrap(ptr, vcprop), card, npts);
60  ViewType dummyView;
61 
62  Impl::Basis_HGRAD_TET_Cn_FEM_ORTH::
63  Serial<OpType>::getValues(phis, input, dummyView, order);
64 
65  for (ordinal_type i=0;i<card;++i)
66  for (ordinal_type j=0;j<npts;++j) {
67  output.access(i,j) = 0.0;
68  for (ordinal_type k=0;k<card;++k)
69  output.access(i,j) += vinv(k,i)*phis.access(k,j);
70  }
71  break;
72  }
73  case OPERATOR_GRAD:
74  case OPERATOR_D1: {
75  const ViewType phis(Kokkos::view_wrap(ptr, vcprop), card, npts, spaceDim);
76  ptr += card*npts*spaceDim*get_dimension_scalar(input);
77  const ViewType workView(Kokkos::view_wrap(ptr, vcprop), card, npts, spaceDim+1);
78  Impl::Basis_HGRAD_TET_Cn_FEM_ORTH::
79  Serial<OpType>::getValues(phis, input, workView, order);
80 
81  for (ordinal_type i=0;i<card;++i)
82  for (ordinal_type j=0;j<npts;++j)
83  for (ordinal_type k=0;k<spaceDim;++k) {
84  output.access(i,j,k) = 0.0;
85  for (ordinal_type l=0;l<card;++l)
86  output.access(i,j,k) += vinv(l,i)*phis.access(l,j,k);
87  }
88  break;
89  }
90  case OPERATOR_D2:
91  case OPERATOR_D3:
92  case OPERATOR_D4:
93  case OPERATOR_D5:
94  case OPERATOR_D6:
95  case OPERATOR_D7:
96  case OPERATOR_D8:
97  case OPERATOR_D9:
98  case OPERATOR_D10: {
99  const ordinal_type dkcard = getDkCardinality<OpType,spaceDim>(); //(orDn + 1);
100  const
101  ViewType phis(Kokkos::view_wrap(ptr, vcprop), card, npts, dkcard);
102  ViewType dummyView;
103 
104  Impl::Basis_HGRAD_TET_Cn_FEM_ORTH::
105  Serial<OpType>::getValues(phis, input, dummyView, order);
106 
107  for (ordinal_type i=0;i<card;++i)
108  for (ordinal_type j=0;j<npts;++j)
109  for (ordinal_type k=0;k<dkcard;++k) {
110  output.access(i,j,k) = 0.0;
111  for (ordinal_type l=0;l<card;++l)
112  output.access(i,j,k) += vinv(l,i)*phis.access(l,j,k);
113  }
114  break;
115  }
116  default: {
117  INTREPID2_TEST_FOR_ABORT( true,
118  ">>> ERROR (Basis_HVOL_TET_Cn_FEM): Operator type not implemented");
119  }
120  }
121  }
122 
123  template<typename DT, ordinal_type numPtsPerEval,
124  typename outputValueValueType, class ...outputValueProperties,
125  typename inputPointValueType, class ...inputPointProperties,
126  typename vinvValueType, class ...vinvProperties>
127  void
128  Basis_HVOL_TET_Cn_FEM::
129  getValues( Kokkos::DynRankView<outputValueValueType,outputValueProperties...> outputValues,
130  const Kokkos::DynRankView<inputPointValueType, inputPointProperties...> inputPoints,
131  const Kokkos::DynRankView<vinvValueType, vinvProperties...> vinv,
132  const EOperator operatorType) {
133  typedef Kokkos::DynRankView<outputValueValueType,outputValueProperties...> outputValueViewType;
134  typedef Kokkos::DynRankView<inputPointValueType, inputPointProperties...> inputPointViewType;
135  typedef Kokkos::DynRankView<vinvValueType, vinvProperties...> vinvViewType;
136  typedef typename ExecSpace<typename inputPointViewType::execution_space,typename DT::execution_space>::ExecSpaceType ExecSpaceType;
137 
138  // loopSize corresponds to cardinality
139  const auto loopSizeTmp1 = (inputPoints.extent(0)/numPtsPerEval);
140  const auto loopSizeTmp2 = (inputPoints.extent(0)%numPtsPerEval != 0);
141  const auto loopSize = loopSizeTmp1 + loopSizeTmp2;
142  Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(0, loopSize);
143 
144  typedef typename inputPointViewType::value_type inputPointType;
145 
146  const ordinal_type cardinality = outputValues.extent(0);
147  const ordinal_type spaceDim = 3;
148 
149  ordinal_type order = 0;
150  while((Intrepid2::getPnCardinality<spaceDim>(++order) != cardinality) && (order != Parameters::MaxOrder));
151 
152  auto vcprop = Kokkos::common_view_alloc_prop(inputPoints);
153  typedef typename Kokkos::DynRankView< inputPointType, typename inputPointViewType::memory_space> workViewType;
154 
155  switch (operatorType) {
156  case OPERATOR_VALUE: {
157  auto bufferSize = Basis_HVOL_TET_Cn_FEM::Serial<OPERATOR_VALUE>::getWorkSizePerPoint(order);
158  workViewType work(Kokkos::view_alloc("Basis_HVOL_TET_Cn_FEM::getValues::work", vcprop), bufferSize, inputPoints.extent(0));
159  typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
160  OPERATOR_VALUE,numPtsPerEval> FunctorType;
161  Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work) );
162  break;
163  }
164  case OPERATOR_GRAD:
165  case OPERATOR_D1: {
166  auto bufferSize = Basis_HVOL_TET_Cn_FEM::Serial<OPERATOR_D1>::getWorkSizePerPoint(order);
167  workViewType work(Kokkos::view_alloc("Basis_HVOL_TET_Cn_FEM::getValues::work", vcprop), bufferSize, inputPoints.extent(0));
168  typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
169  OPERATOR_D1,numPtsPerEval> FunctorType;
170  Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work) );
171  break;
172  }
173  case OPERATOR_D2: {
174  auto bufferSize = Basis_HVOL_TET_Cn_FEM::Serial<OPERATOR_D2>::getWorkSizePerPoint(order);
175  typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
176  OPERATOR_D2,numPtsPerEval> FunctorType;
177  workViewType work(Kokkos::view_alloc("Basis_HVOL_TET_Cn_FEM::getValues::work", vcprop), bufferSize, inputPoints.extent(0));
178  Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work) );
179  break;
180  }
181  /* case OPERATOR_D3: {
182  typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType
183  OPERATOR_D3,numPtsPerEval> FunctorType;
184  workViewType work(Kokkos::view_alloc("Basis_HVOL_TET_Cn_FEM::getValues::work", vcprop), cardinality, inputPoints.extent(0), outputValues.extent(2));
185  Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work) );
186  break;
187  }*/
188  default: {
189  INTREPID2_TEST_FOR_EXCEPTION( true , std::invalid_argument,
190  ">>> ERROR (Basis_HVOL_TET_Cn_FEM): Operator type not implemented" );
191  }
192  }
193  }
194  }
195 
196  // -------------------------------------------------------------------------------------
197  template<typename DT, typename OT, typename PT>
199  Basis_HVOL_TET_Cn_FEM( const ordinal_type order,
200  const EPointType pointType ) {
201  constexpr ordinal_type spaceDim = 3;
202 
203  this->pointType_ = pointType;
204  this->basisCardinality_ = Intrepid2::getPnCardinality<spaceDim>(order); // bigN
205  this->basisDegree_ = order; // small n
206  this->basisCellTopologyKey_ = shards::Tetrahedron<4>::key;
207  this->basisType_ = BASIS_FEM_LAGRANGIAN;
208  this->basisCoordinates_ = COORDINATES_CARTESIAN;
209  this->functionSpace_ = FUNCTION_SPACE_HVOL;
210 
211  const ordinal_type card = this->basisCardinality_;
212 
213  // points are computed in the host and will be copied
214  Kokkos::DynRankView<scalarType,typename DT::execution_space::array_layout,Kokkos::HostSpace>
215  dofCoords("HVOL::Tet::Cn::dofCoords", card, spaceDim);
216 
217  // construct lattice (only internal nodes for HVOL element)
218  const ordinal_type offset = 1;
219  const shards::CellTopology cellTopo(shards::getCellTopologyData<shards::Tetrahedron<4> >());
220  PointTools::getLattice( dofCoords,
221  cellTopo,
222  order+spaceDim+offset, offset,
223  pointType );
224 
225  this->dofCoords_ = Kokkos::create_mirror_view(typename DT::memory_space(), dofCoords);
226  Kokkos::deep_copy(this->dofCoords_, dofCoords);
227 
228  // form Vandermonde matrix. Actually, this is the transpose of the VDM,
229  // so we transpose on copy below.
230  const ordinal_type lwork = card*card;
231  Kokkos::DynRankView<scalarType,Kokkos::LayoutLeft,Kokkos::HostSpace>
232  vmat("HVOL::Tet::Cn::vmat", card, card),
233  work("HVOL::Tet::Cn::work", lwork),
234  ipiv("HVOL::Tet::Cn::ipiv", card);
235 
236  Impl::Basis_HGRAD_TET_Cn_FEM_ORTH::getValues<Kokkos::HostSpace::execution_space,Parameters::MaxNumPtsPerBasisEval>(typename Kokkos::HostSpace::execution_space{},
237  vmat,
238  dofCoords,
239  order,
240  OPERATOR_VALUE);
241 
242  ordinal_type info = 0;
243  Teuchos::LAPACK<ordinal_type,scalarType> lapack;
244 
245  lapack.GETRF(card, card,
246  vmat.data(), vmat.stride_1(),
247  (ordinal_type*)ipiv.data(),
248  &info);
249 
250  INTREPID2_TEST_FOR_EXCEPTION( info != 0,
251  std::runtime_error ,
252  ">>> ERROR: (Intrepid2::Basis_HVOL_TET_Cn_FEM) lapack.GETRF returns nonzero info." );
253 
254  lapack.GETRI(card,
255  vmat.data(), vmat.stride_1(),
256  (ordinal_type*)ipiv.data(),
257  work.data(), lwork,
258  &info);
259 
260  INTREPID2_TEST_FOR_EXCEPTION( info != 0,
261  std::runtime_error ,
262  ">>> ERROR: (Intrepid2::Basis_HVOL_TET_Cn_FEM) lapack.GETRI returns nonzero info." );
263 
264  // create host mirror
265  Kokkos::DynRankView<scalarType,typename DT::execution_space::array_layout,Kokkos::HostSpace>
266  vinv("HVOL::Line::Cn::vinv", card, card);
267 
268  for (ordinal_type i=0;i<card;++i)
269  for (ordinal_type j=0;j<card;++j)
270  vinv(i,j) = vmat(j,i);
271 
272  this->vinv_ = Kokkos::create_mirror_view(typename DT::memory_space(), vinv);
273  Kokkos::deep_copy(this->vinv_ , vinv);
274 
275  // initialize tags
276  {
277  // Basis-dependent initializations
278  constexpr ordinal_type tagSize = 4; // size of DoF tag, i.e., number of fields in the tag
279  const ordinal_type posScDim = 0; // position in the tag, counting from 0, of the subcell dim
280  const ordinal_type posScOrd = 1; // position in the tag, counting from 0, of the subcell ordinal
281  const ordinal_type posDfOrd = 2; // position in the tag, counting from 0, of DoF ordinal relative to the subcell
282 
283  constexpr ordinal_type maxCard = Intrepid2::getPnCardinality<spaceDim, Parameters::MaxOrder>();
284  ordinal_type tags[maxCard][tagSize];
285 
286  const ordinal_type
287  numElemDof = this->basisCardinality_; //all the degrees of freedom are internal.
288 
289 
290  ordinal_type elemId = 0;
291  for (ordinal_type i=0;i<this->basisCardinality_;++i) {
292  // elem
293  tags[i][0] = spaceDim; // intr dof
294  tags[i][1] = 0; // intr id
295  tags[i][2] = elemId++; // local dof id
296  tags[i][3] = numElemDof; // total vert dof
297  }
298 
299  OrdinalTypeArray1DHost tagView(&tags[0][0], card*tagSize);
300 
301  // Basis-independent function sets tag and enum data in tagToOrdinal_ and ordinalToTag_ arrays:
302  // tags are constructed on host
303  this->setOrdinalTagData(this->tagToOrdinal_,
304  this->ordinalToTag_,
305  tagView,
306  this->basisCardinality_,
307  tagSize,
308  posScDim,
309  posScOrd,
310  posDfOrd);
311  }
312  }
313 
314  template<typename DT, typename OT, typename PT>
315  void
317  ordinal_type& perTeamSpaceSize,
318  ordinal_type& perThreadSpaceSize,
319  const PointViewType inputPoints,
320  const EOperator operatorType) const {
321  perTeamSpaceSize = 0;
322  perThreadSpaceSize = this->vinv_.extent(0)*get_dimension_scalar(inputPoints)*sizeof(typename BasisBase::scalarType);
323  }
324 
325  template<typename DT, typename OT, typename PT>
326  KOKKOS_INLINE_FUNCTION
327  void
329  OutputViewType outputValues,
330  const PointViewType inputPoints,
331  const EOperator operatorType,
332  const typename Kokkos::TeamPolicy<typename DT::execution_space>::member_type& team_member,
333  const typename DT::execution_space::scratch_memory_space & scratchStorage,
334  const ordinal_type subcellDim,
335  const ordinal_type subcellOrdinal) const {
336 
337  INTREPID2_TEST_FOR_ABORT( !((subcellDim == -1) && (subcellOrdinal == -1)),
338  ">>> ERROR: (Intrepid2::Basis_HVOL_TET_Cn_FEM::getValues), The capability of selecting subsets of basis functions has not been implemented yet.");
339 
340  const int numPoints = inputPoints.extent(0);
341  using ScalarType = typename ScalarTraits<typename PointViewType::value_type>::scalar_type;
342  using WorkViewType = Kokkos::DynRankView< ScalarType,typename DT::execution_space::scratch_memory_space,Kokkos::MemoryTraits<Kokkos::Unmanaged> >;
343  auto sizePerPoint = this->vinv_.extent(0)*get_dimension_scalar(inputPoints);
344  WorkViewType workView(scratchStorage, sizePerPoint*team_member.team_size());
345  using range_type = Kokkos::pair<ordinal_type,ordinal_type>;
346  switch(operatorType) {
347  case OPERATOR_VALUE:
348  Kokkos::parallel_for (Kokkos::TeamThreadRange (team_member, numPoints), [=] (ordinal_type& pt) {
349  auto output = Kokkos::subview( outputValues, Kokkos::ALL(), range_type (pt,pt+1), Kokkos::ALL() );
350  const auto input = Kokkos::subview( inputPoints, range_type(pt, pt+1), Kokkos::ALL() );
351  WorkViewType work(workView.data() + sizePerPoint*team_member.team_rank(), sizePerPoint);
352  Impl::Basis_HVOL_TET_Cn_FEM::Serial<OPERATOR_VALUE>::getValues( output, input, work, this->vinv_);
353  });
354  break;
355  default: {
356  INTREPID2_TEST_FOR_ABORT( true,
357  ">>> ERROR (Basis_HVOL_TET_Cn_FEM): getValues not implemented for this operator");
358  }
359  }
360  }
361 
362 } // namespace Intrepid2
363 #endif
ScalarTraits< pointValueType >::scalar_type scalarType
Scalar type for point values.
virtual void getValues(OutputViewType outputValues, const PointViewType inputPoints, const EOperator operatorType=OPERATOR_VALUE) const override
virtual void getScratchSpaceSize(ordinal_type &perTeamSpaceSize, ordinal_type &perThreadSpaceSize, const PointViewType inputPoints, const EOperator operatorType=OPERATOR_VALUE) const override
Return the size of the scratch space, in bytes, needed for using the team-level implementation of get...
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...
Basis_HVOL_TET_Cn_FEM(const ordinal_type order, const EPointType pointType=POINTTYPE_EQUISPACED)
Constructor.
Kokkos::View< ordinal_type *, typename ExecutionSpace::array_layout, Kokkos::HostSpace > OrdinalTypeArray1DHost
View type for 1d host array.
Kokkos::DynRankView< PointValueType, Kokkos::LayoutStride, DeviceType > PointViewType
View type for input points.
Header file for the Intrepid2::Basis_HGRAD_TET_Cn_FEM_ORTH class.
static constexpr ordinal_type MaxOrder
The maximum reconstruction order.