Intrepid2
Intrepid2_HGRAD_TRI_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
16#ifndef __INTREPID2_HGRAD_TRI_CN_FEM_DEF_HPP__
17#define __INTREPID2_HGRAD_TRI_CN_FEM_DEF_HPP__
18
20
21namespace Intrepid2 {
22
23// -------------------------------------------------------------------------------------
24namespace Impl {
25
26template<EOperator OpType>
27template<typename OutputViewType,
28typename InputViewType,
29typename WorkViewType,
30typename VinvViewType>
31KOKKOS_INLINE_FUNCTION
32void
34getValues( OutputViewType output,
35 const InputViewType input,
36 WorkViewType work,
37 const VinvViewType vinv,
38 const ordinal_type order ) {
39
40 constexpr ordinal_type spaceDim = 2;
41 const ordinal_type
42 card = vinv.extent(0),
43 npts = input.extent(0);
44
45 typedef typename Kokkos::DynRankView<typename InputViewType::value_type, typename WorkViewType::memory_space> ViewType;
46 auto ptr = work.data();
47
48 switch (OpType) {
49 case OPERATOR_VALUE: {
50 const ViewType phis = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts);
51 ViewType dummyView;
52
53 Impl::Basis_HGRAD_TRI_Cn_FEM_ORTH::
54 Serial<OpType>::getValues(phis, input, dummyView, order);
55
56 for (ordinal_type i=0;i<card;++i)
57 for (ordinal_type j=0;j<npts;++j) {
58 output.access(i,j) = 0.0;
59 for (ordinal_type k=0;k<card;++k)
60 output.access(i,j) += vinv(k,i)*phis.access(k,j);
61 }
62 break;
63 }
64 case OPERATOR_GRAD:
65 case OPERATOR_D1: {
66 const ViewType phis = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts, spaceDim);
67 ptr += card*npts*spaceDim*get_dimension_scalar(input);
68 const ViewType workView = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts, spaceDim+1);
69 Impl::Basis_HGRAD_TRI_Cn_FEM_ORTH::
70 Serial<OpType>::getValues(phis, input, workView, order);
71
72 for (ordinal_type i=0;i<card;++i)
73 for (ordinal_type j=0;j<npts;++j)
74 for (ordinal_type k=0;k<spaceDim;++k) {
75 output.access(i,j,k) = 0.0;
76 for (ordinal_type l=0;l<card;++l)
77 output.access(i,j,k) += vinv(l,i)*phis.access(l,j,k);
78 }
79 break;
80 }
81 case OPERATOR_D2:
82 case OPERATOR_D3:
83 case OPERATOR_D4:
84 case OPERATOR_D5:
85 case OPERATOR_D6:
86 case OPERATOR_D7:
87 case OPERATOR_D8:
88 case OPERATOR_D9:
89 case OPERATOR_D10: {
90 const ordinal_type dkcard = getDkCardinality<OpType,spaceDim>(); //(orDn + 1);
91 const ViewType phis = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts, dkcard);
92 ViewType dummyView;
93
94 Impl::Basis_HGRAD_TRI_Cn_FEM_ORTH::
95 Serial<OpType>::getValues(phis, input, dummyView, order);
96
97 for (ordinal_type i=0;i<card;++i)
98 for (ordinal_type j=0;j<npts;++j)
99 for (ordinal_type k=0;k<dkcard;++k) {
100 output.access(i,j,k) = 0.0;
101 for (ordinal_type l=0;l<card;++l)
102 output.access(i,j,k) += vinv(l,i)*phis.access(l,j,k);
103 }
104 break;
105 }
106 case OPERATOR_CURL: { // only works in 2d. first component is -d/dy, second is d/dx
107 const ViewType phis = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts, spaceDim);
108 ptr += card*npts*spaceDim*get_dimension_scalar(input);
109 const ViewType workView = createMatchingUnmanagedView<ViewType>(input, ptr, card, npts, spaceDim+1);
110
111
112 Impl::Basis_HGRAD_TRI_Cn_FEM_ORTH::
113 Serial<OPERATOR_D1>::getValues(phis, input, workView, order);
114
115 for (ordinal_type i=0;i<card;++i)
116 for (ordinal_type j=0;j<npts;++j) {
117 output.access(i,j,0) = 0.0;
118 for (ordinal_type l=0;l<card;++l)
119 output.access(i,j,0) += vinv(l,i)*phis.access(l,j,1);
120 output.access(i,j,1) = 0.0;
121 for (ordinal_type l=0;l<card;++l)
122 output.access(i,j,1) -= vinv(l,i)*phis.access(l,j,0);
123 }
124 break;
125 }
126 default: {
127 INTREPID2_TEST_FOR_ABORT( true,
128 ">>> ERROR (Basis_HGRAD_TRI_Cn_FEM): Operator type not implemented");
129 }
130 }
131}
132
133template<typename DT, ordinal_type numPtsPerEval,
134typename outputValueValueType, class ...outputValueProperties,
135typename inputPointValueType, class ...inputPointProperties,
136typename vinvValueType, class ...vinvProperties>
137void
138Basis_HGRAD_TRI_Cn_FEM::
139getValues(
140 const typename DT::execution_space& space,
141 Kokkos::DynRankView<outputValueValueType,outputValueProperties...> outputValues,
142 const Kokkos::DynRankView<inputPointValueType, inputPointProperties...> inputPoints,
143 const Kokkos::DynRankView<vinvValueType, vinvProperties...> vinv,
144 const ordinal_type order,
145 const EOperator operatorType) {
146 typedef Kokkos::DynRankView<outputValueValueType,outputValueProperties...> outputValueViewType;
147 typedef Kokkos::DynRankView<inputPointValueType, inputPointProperties...> inputPointViewType;
148 typedef Kokkos::DynRankView<vinvValueType, vinvProperties...> vinvViewType;
149 typedef typename ExecSpace<typename inputPointViewType::execution_space,typename DT::execution_space>::ExecSpaceType ExecSpaceType;
150
151 // loopSize corresponds to cardinality
152 const auto loopSizeTmp1 = (inputPoints.extent(0)/numPtsPerEval);
153 const auto loopSizeTmp2 = (inputPoints.extent(0)%numPtsPerEval != 0);
154 const auto loopSize = loopSizeTmp1 + loopSizeTmp2;
155 Kokkos::RangePolicy<ExecSpaceType,Kokkos::Schedule<Kokkos::Static> > policy(space, 0, loopSize);
156
157 const ordinal_type cardinality = outputValues.extent(0);
158 const ordinal_type spaceDim = 2;
159
160 typedef typename DeduceDynRankView<inputPointViewType>::type workViewType;
161
162 switch (operatorType) {
163 case OPERATOR_VALUE: {
164 workViewType work = createMatchingView<workViewType>(inputPoints, "Basis_HGRAD_TRI_Cn_FEM::getValues::work", cardinality, inputPoints.extent(0));
165 typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
166 OPERATOR_VALUE,numPtsPerEval> FunctorType;
167 Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work, order) );
168 break;
169 }
170 case OPERATOR_GRAD:
171 case OPERATOR_D1: {
172 workViewType work = createMatchingView<workViewType>(inputPoints, "Basis_HGRAD_TRI_Cn_FEM::getValues::work", cardinality*(2*spaceDim+1), inputPoints.extent(0));
173 typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
174 OPERATOR_D1,numPtsPerEval> FunctorType;
175 Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work, order) );
176 break;
177 }
178 case OPERATOR_CURL: {
179 workViewType work = createMatchingView<workViewType>(inputPoints, "Basis_HGRAD_TRI_Cn_FEM::getValues::work", cardinality*(2*spaceDim+1), inputPoints.extent(0));
180 typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
181 OPERATOR_CURL,numPtsPerEval> FunctorType;
182 Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work, order) );
183 break;
184 }
185 case OPERATOR_D2: {
186 typedef Functor<outputValueViewType,inputPointViewType,vinvViewType, workViewType,
187 OPERATOR_D2,numPtsPerEval> FunctorType;
188 workViewType work = createMatchingView<workViewType>(inputPoints, "Basis_HGRAD_TRI_Cn_FEM::getValues::work", cardinality*outputValues.extent(2), inputPoints.extent(0));
189 Kokkos::parallel_for( policy, FunctorType(outputValues, inputPoints, vinv, work, order) );
190 break;
191 }
192 default: {
193 INTREPID2_TEST_FOR_EXCEPTION( true , std::invalid_argument,
194 ">>> ERROR (Basis_HGRAD_TRI_Cn_FEM): Operator type not implemented" );
195 }
196 }
197}
198}
199
200// -------------------------------------------------------------------------------------
201template<typename DT, typename OT, typename PT>
203Basis_HGRAD_TRI_Cn_FEM( const ordinal_type order,
204 const EPointType pointType ) {
205 constexpr ordinal_type spaceDim = 2;
206
208 this->basisDegree_ = order; // small n
209 this->basisCellTopologyKey_ = shards::Triangle<3>::key;
210 this->basisType_ = BASIS_FEM_LAGRANGIAN;
211 this->basisCoordinates_ = COORDINATES_CARTESIAN;
212 this->functionSpace_ = FUNCTION_SPACE_HGRAD;
213
214 pointType_ = (pointType == POINTTYPE_DEFAULT) ? POINTTYPE_EQUISPACED : pointType;
215 const ordinal_type card = this->basisCardinality_;
216
217 // points are computed in the host and will be copied
218 Kokkos::DynRankView<scalarType,typename DT::execution_space::array_layout,Kokkos::HostSpace>
219 dofCoords("Hgrad::Tri::Cn::dofCoords", card, spaceDim);
220
221 // construct lattice
222 const shards::CellTopology cellTopo(shards::getCellTopologyData<shards::Triangle<3>>());
223 const ordinal_type offset = 0;
224 PointTools::getLattice( dofCoords,
225 cellTopo,
226 order, offset,
227 this->pointType_ );
228
229 this->dofCoords_ = Kokkos::create_mirror_view(typename DT::memory_space(), dofCoords);
230 Kokkos::deep_copy(this->dofCoords_, dofCoords);
231
232 // form Vandermonde matrix. Actually, this is the transpose of the VDM,
233 // so we transpose on copy below.
234 const ordinal_type lwork = card*card;
235 Kokkos::DynRankView<scalarType,Kokkos::LayoutLeft,Kokkos::HostSpace>
236 vmat("Hgrad::Tri::Cn::vmat", card, card),
237 work("Hgrad::Tri::Cn::work", lwork),
238 ipiv("Hgrad::Tri::Cn::ipiv", card);
239
240 Impl::Basis_HGRAD_TRI_Cn_FEM_ORTH::getValues<Kokkos::HostSpace::execution_space,Parameters::MaxNumPtsPerBasisEval>(typename Kokkos::HostSpace::execution_space{},
241 vmat,
242 dofCoords,
243 order,
244 OPERATOR_VALUE);
245
246 ordinal_type info = 0;
247 Teuchos::LAPACK<ordinal_type,scalarType> lapack;
248
249 lapack.GETRF(card, card,
250 vmat.data(), vmat.stride(1),
251 (ordinal_type*)ipiv.data(),
252 &info);
253
254 INTREPID2_TEST_FOR_EXCEPTION( info != 0,
255 std::runtime_error ,
256 ">>> ERROR: (Intrepid2::Basis_HGRAD_TRI_Cn_FEM) lapack.GETRF returns nonzero info." );
257
258 lapack.GETRI(card,
259 vmat.data(), vmat.stride(1),
260 (ordinal_type*)ipiv.data(),
261 work.data(), lwork,
262 &info);
263
264 INTREPID2_TEST_FOR_EXCEPTION( info != 0,
265 std::runtime_error ,
266 ">>> ERROR: (Intrepid2::Basis_HGRAD_TRI_Cn_FEM) lapack.GETRI returns nonzero info." );
267
268 // create host mirror
269 Kokkos::DynRankView<scalarType,typename DT::execution_space::array_layout,Kokkos::HostSpace>
270 vinv("Hgrad::Line::Cn::vinv", card, card);
271
272 for (ordinal_type i=0;i<card;++i)
273 for (ordinal_type j=0;j<card;++j)
274 vinv(i,j) = vmat(j,i);
275
276 this->vinv_ = Kokkos::create_mirror_view(typename DT::memory_space(), vinv);
277 Kokkos::deep_copy(this->vinv_ , vinv);
278
279 // initialize tags
280 {
281 // Basis-dependent initializations
282 constexpr ordinal_type tagSize = 4; // size of DoF tag, i.e., number of fields in the tag
283 const ordinal_type posScDim = 0; // position in the tag, counting from 0, of the subcell dim
284 const ordinal_type posScOrd = 1; // position in the tag, counting from 0, of the subcell ordinal
285 const ordinal_type posDfOrd = 2; // position in the tag, counting from 0, of DoF ordinal relative to the subcell
286
287 // Note: the only reason why equispaced can't support higher order than Parameters::MaxOrder appears to be the fact that the tags below get stored into a fixed-length array.
288 // TODO: relax the maximum order requirement by setting up tags in a different container, perhaps directly into an OrdinalTypeArray1DHost (tagView, below). (As of this writing (1/25/22), looks like other nodal bases do this in a similar way -- those should be fixed at the same time; maybe search for Parameters::MaxOrder.)
289 INTREPID2_TEST_FOR_EXCEPTION( order > Parameters::MaxOrder, std::invalid_argument, "polynomial order exceeds the max supported by this class");
290 constexpr ordinal_type maxCard = Intrepid2::getPnCardinality<spaceDim, Parameters::MaxOrder>();
291 ordinal_type tags[maxCard][tagSize];
292
293 const ordinal_type
294 numEdgeDof = Intrepid2::getPnCardinality<1>(order-2),
295 numElemDof = (order > 2 ? Intrepid2::getPnCardinality<2>(order-3) : 0);
296
297 scalarType xi0, xi1, xi2;
298 const scalarType eps = threshold();
299
300 ordinal_type edgeId[3] = {}, elemId = 0;
301 for (ordinal_type i=0;i<card;++i) {
302
303 // compute barycentric coordinates
304 const auto x = dofCoords(i,0);
305 const auto y = dofCoords(i,1);
306 xi0 = 1.0 - x - y;
307 xi1= x;
308 xi2= y;
309
310 // vertex
311 if ((1.0 - xi0) < eps) { // vert 0
312 tags[i][0] = 0; // vertex dof
313 tags[i][1] = 0; // vertex id
314 tags[i][2] = 0; // local dof id
315 tags[i][3] = 1; // total vert dof
316 }
317 else if ((1.0 - xi1) < eps) { // vert 1
318 tags[i][0] = 0; // vertex dof
319 tags[i][1] = 1; // vertex id
320 tags[i][2] = 0; // local dof id
321 tags[i][3] = 1; // total vert dof
322 }
323 else if ((1.0 - xi2) < eps) { // vert 2
324 tags[i][0] = 0; // vertex dof
325 tags[i][1] = 2; // vertex id
326 tags[i][2] = 0; // local dof id
327 tags[i][3] = 1; // total vert dof
328 }
329 else if (xi2 < eps) { // edge 0
330 tags[i][0] = 1; // edge dof
331 tags[i][1] = 0; // edge id
332 tags[i][2] = edgeId[0]++; // local dof id
333 tags[i][3] = numEdgeDof; // total vert dof
334 }
335 else if (xi0 < eps) { // edge 1
336 tags[i][0] = 1; // edge dof
337 tags[i][1] = 1; // edge id
338 tags[i][2] = edgeId[1]++; // local dof id
339 tags[i][3] = numEdgeDof; // total vert dof
340 }
341 else if (xi1 < eps) { // edge 2
342 tags[i][0] = 1; // edge dof
343 tags[i][1] = 2; // edge id
344 tags[i][2] = edgeId[2]++; // local dof id
345 tags[i][3] = numEdgeDof; // total vert dof
346 }
347 else { // elem
348 tags[i][0] = 2; // intr dof
349 tags[i][1] = 0; // intr id
350 tags[i][2] = elemId++; // local dof id
351 tags[i][3] = numElemDof; // total vert dof
352 }
353 }
354
355 OrdinalTypeArray1DHost tagView(&tags[0][0], card*tagSize);
356
357 // Basis-independent function sets tag and enum data in tagToOrdinal_ and ordinalToTag_ arrays:
358 // tags are constructed on host
360 this->ordinalToTag_,
361 tagView,
362 this->basisCardinality_,
363 tagSize,
364 posScDim,
365 posScOrd,
366 posDfOrd);
367 }
368}
369
370 template<typename DT, typename OT, typename PT>
371 void
372 Basis_HGRAD_TRI_Cn_FEM<DT,OT,PT>::getScratchSpaceSize(
373 ordinal_type& perTeamSpaceSize,
374 ordinal_type& perThreadSpaceSize,
375 const PointViewType inputPoints,
376 const EOperator operatorType) const {
377 perTeamSpaceSize = 0;
378 perThreadSpaceSize = getWorkSizePerPoint(operatorType)*get_dimension_scalar(inputPoints)*sizeof(typename BasisBase::scalarType);
379 }
380
381 template<typename DT, typename OT, typename PT>
382 KOKKOS_INLINE_FUNCTION
383 void
384 Basis_HGRAD_TRI_Cn_FEM<DT,OT,PT>::getValues(
385 OutputViewType outputValues,
386 const PointViewType inputPoints,
387 const EOperator operatorType,
388 const typename Kokkos::TeamPolicy<typename DT::execution_space>::member_type& team_member,
389 const typename DT::execution_space::scratch_memory_space & scratchStorage,
390 const ordinal_type subcellDim,
391 const ordinal_type subcellOrdinal) const {
392
393 INTREPID2_TEST_FOR_ABORT( !((subcellDim == -1) && (subcellOrdinal == -1)),
394 ">>> ERROR: (Intrepid2::Basis_HGRAD_TRI_Cn_FEM::getValues), The capability of selecting subsets of basis functions has not been implemented yet.");
395
396 const int numPoints = inputPoints.extent(0);
397 using ScalarType = typename ScalarTraits<typename PointViewType::value_type>::scalar_type;
398 using WorkViewType = Kokkos::DynRankView< ScalarType,typename DT::execution_space::scratch_memory_space,Kokkos::MemoryTraits<Kokkos::Unmanaged> >;
399 constexpr ordinal_type spaceDim = 2;
400 auto sizePerPoint = (operatorType==OPERATOR_VALUE) ?
401 this->vinv_.extent(0)*get_dimension_scalar(inputPoints) :
402 (2*spaceDim+1)*this->vinv_.extent(0)*get_dimension_scalar(inputPoints);
403 WorkViewType workView(scratchStorage, sizePerPoint*team_member.team_size());
404 using range_type = Kokkos::pair<ordinal_type,ordinal_type>;
405 switch(operatorType) {
406 case OPERATOR_VALUE:
407 Kokkos::parallel_for (Kokkos::TeamThreadRange (team_member, numPoints), [=, &vinv_ = this->vinv_, basisDegree_ = this->basisDegree_] (ordinal_type& pt) {
408 auto output = Kokkos::subview( outputValues, Kokkos::ALL(), range_type (pt,pt+1), Kokkos::ALL() );
409 const auto input = Kokkos::subview( inputPoints, range_type(pt, pt+1), Kokkos::ALL() );
410 WorkViewType work(workView.data() + sizePerPoint*team_member.team_rank(), sizePerPoint);
411 Impl::Basis_HGRAD_TRI_Cn_FEM::Serial<OPERATOR_VALUE>::getValues( output, input, work, vinv_, basisDegree_);
412 });
413 break;
414 case OPERATOR_GRAD:
415 Kokkos::parallel_for (Kokkos::TeamThreadRange (team_member, numPoints), [=, &vinv_ = this->vinv_, basisDegree_ = this->basisDegree_] (ordinal_type& pt) {
416 auto output = Kokkos::subview( outputValues, Kokkos::ALL(), range_type(pt,pt+1), Kokkos::ALL() );
417 const auto input = Kokkos::subview( inputPoints, range_type(pt,pt+1), Kokkos::ALL() );
418 WorkViewType work(workView.data() + sizePerPoint*team_member.team_rank(), sizePerPoint);
419 Impl::Basis_HGRAD_TRI_Cn_FEM::Serial<OPERATOR_GRAD>::getValues( output, input, work, vinv_, basisDegree_);
420 });
421 break;
422 case OPERATOR_CURL:
423 Kokkos::parallel_for (Kokkos::TeamThreadRange (team_member, numPoints), [=, &vinv_ = this->vinv_, basisDegree_ = this->basisDegree_] (ordinal_type& pt) {
424 auto output = Kokkos::subview( outputValues, Kokkos::ALL(), range_type(pt,pt+1), Kokkos::ALL() );
425 const auto input = Kokkos::subview( inputPoints, range_type(pt,pt+1), Kokkos::ALL() );
426 WorkViewType work(workView.data() + sizePerPoint*team_member.team_rank(), sizePerPoint);
427 Impl::Basis_HGRAD_TRI_Cn_FEM::Serial<OPERATOR_CURL>::getValues( output, input, work, vinv_, basisDegree_);
428 });
429 break;
430 default: {
431 INTREPID2_TEST_FOR_ABORT( true,
432 ">>> ERROR (Basis_HGRAD_TRI_Cn_FEM): getValues not implemented for this operator");
433 }
434 }
435 }
436
437} // namespace Intrepid2
438
439#endif
KOKKOS_INLINE_FUNCTION ordinal_type getPnCardinality(ordinal_type n)
Returns cardinality of Polynomials of order n (P^n).
Header file for the Intrepid2::Basis_HGRAD_TRI_Cn_FEM_ORTH class.
KOKKOS_INLINE_FUNCTION std::enable_if< std::is_pointer_v< CtorProp > &&!std::is_convertible_v< CtorProp, constchar * >, OutViewType >::type createMatchingUnmanagedView(const InViewType &view, const CtorProp &data, const Dims... dims)
Creates an unmanaged view that matches the value_type of the provided view The type of the output vie...
Basis_HGRAD_TRI_Cn_FEM(const ordinal_type order, const EPointType pointType=POINTTYPE_EQUISPACED)
Constructor.
EPointType pointType_
type of lattice used for creating the DoF coordinates
Kokkos::DynRankView< scalarType, DeviceType > vinv_
inverse of Generalized Vandermonde matrix, whose columns store the expansion coefficients of the noda...
void setOrdinalTagData(OrdinalTypeView3D &tagToOrdinal, OrdinalTypeView2D &ordinalToTag, const OrdinalTypeView1D tags, const ordinal_type basisCard, const ordinal_type tagSize, const ordinal_type posScDim, const ordinal_type posScOrd, const ordinal_type posDfOrd)
Kokkos::DynRankView< scalarType, DeviceType > dofCoords_
ScalarTraits< double >::scalar_type scalarType
Kokkos::View< ordinal_type *, typename ExecutionSpace::array_layout, Kokkos::HostSpace > OrdinalTypeArray1DHost
static constexpr ordinal_type MaxOrder
The maximum reconstruction order.
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...
See Intrepid2::Basis_HGRAD_TRI_Cn_FEM work is a rank 1 view having the same value_type of inputPoints...