// @HEADER // ***************************************************************************** // Intrepid2 Package // // Copyright 2007 NTESS and the Intrepid2 contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** \file \brief Test the DeRahm commutativity property of projection-based interpolation for Tri elements The test considers two triangles in the physical space sharing a common face. In order to test significant configurations, we consider 3 mappings of the reference triangle to the first (physical) triangular, so that the common face is mapped from all the 3 sides of the reference triangular. Then, for each of the mappings, the global ids of the vertices of the common side are permuted. The test checks that, for elements of different degree, 1. projecting a target function f into the HGRAD space, and taking the gradient of the projection is equivalent to taking the gradient of f and then project it to the HCURL space. projecting a target function f into the HGRAD space, and taking the curl of the projection is equivalent to taking the curl of f and then project it to the HDIV space. 2. projecting a target vector function f into the HCURL space, and taking the curl of the projection is equivalent to taking the curl of f and then project it to the HVOL space. 3. projecting a target vector function f into the HDIV space, and taking the divergence of the projection is equivalent to taking the divergence of f and then project it to the HVOL space. \author Created by Mauro Perego */ #include "Intrepid2_config.h" #ifdef HAVE_INTREPID2_DEBUG #define INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE #endif #include "Intrepid2_Orientation.hpp" #include "Intrepid2_OrientationTools.hpp" #include "Intrepid2_ProjectionTools.hpp" #include "Intrepid2_HGRAD_TRI_C1_FEM.hpp" #include "Intrepid2_HGRAD_TRI_Cn_FEM.hpp" #include "Intrepid2_HCURL_TRI_In_FEM.hpp" #include "Intrepid2_HVOL_TRI_Cn_FEM.hpp" #include "Intrepid2_PointTools.hpp" #include "Intrepid2_CellTools.hpp" #include "Intrepid2_FunctionSpaceTools.hpp" #include "Teuchos_oblackholestream.hpp" #include "Teuchos_RCP.hpp" #include #include #include #include namespace Intrepid2 { namespace Test { #define INTREPID2_TEST_ERROR_EXPECTED( S ) \ try { \ ++nthrow; \ S ; \ } \ catch (std::exception &err) { \ ++ncatch; \ *outStream << "Expected Error ----------------------------------------------------------------\n"; \ *outStream << err.what() << '\n'; \ *outStream << "-------------------------------------------------------------------------------" << "\n\n"; \ } template int DeRhamCommutativityTri(const bool verbose) { using ExecSpaceType = typename DeviceType::execution_space; using MemSpaceType = typename DeviceType::memory_space; using DynRankView = Kokkos::DynRankView; using HostSpaceType = Kokkos::DefaultHostExecutionSpace; using DynRankViewIntHost = Kokkos::DynRankView; #define ConstructWithLabel(obj, ...) obj(#obj, __VA_ARGS__) Teuchos::RCP outStream; Teuchos::oblackholestream bhs; // outputs nothing if (verbose) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); Teuchos::oblackholestream oldFormatState; oldFormatState.copyfmt(std::cout); *outStream << "DeviceSpace:: "; ExecSpaceType().print_configuration(*outStream, false); *outStream << "HostSpace:: "; HostSpaceType().print_configuration(*outStream, false); *outStream << "\n"; int errorFlag = 0; const ValueType tol = tolerence(); //target functions and their derivatives struct Fun { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y) { return (x*x*x*x+1)*(y-2); } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 5;} }; struct GradFun { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y, const int comp) { switch (comp) { case 0: return 4*x*x*x*(y - 2); case 1: return x*x*x*x + 1; default: return 0; } } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 4;} }; struct CurlFun { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y, const int comp) { GradFun df; return std::pow(-1,comp)*df(x,y,1-comp); } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return GradFun::degree();} }; struct FunDiv { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y, const int comp=0) { ValueType a = 2*x*y+x*x*x; ValueType f0 = 5+y+x*x; ValueType f1 = -7-2+x+y*y; //fun = f + a x switch (comp) { case 0: return f0 + a*x; case 1: return f1 + a*y; default: return 0; } } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 4;} }; struct DivFunDiv { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y) { ValueType a = 2*x*y+x*x*x; ValueType ga[2] = {2*y+3*x*x,2*x}; ValueType divf = 2*x+2*y; //fun = div f + x \cdot \nabla a + 2 a return divf + (x*ga[0]+y*ga[1]) + 2*a; } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 3;} }; struct FunCurl { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y, const int comp=0) { ValueType a = (y-7)*x*x*x*x; ValueType f0 = 2+x+x*y; ValueType f1 = 3; //fun = f + a \times x switch (comp) { case 0: return f0 -a*y;//2*x+y-z + (x+2*(y+z); case 1: return f1 + a*x;//y+2*(z+x); default: return 0; } } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 5;} }; struct CurlFunCurl { ValueType KOKKOS_INLINE_FUNCTION operator()(const ValueType& x, const ValueType& y) { ValueType a = (y-7)*x*x*x*x; ValueType ga[2] = {4*x*x*x*(y-7),x*x*x*x}; ValueType gf0[2] = {1.0+y,x}; ValueType gf1[2] = {0, 0}; //fun = curl f + 2 a - (x \cdot \nabla) a return gf1[0] - gf0[1] + 2*a + (x*ga[0]+y*ga[1]); } static ordinal_type KOKKOS_INLINE_FUNCTION degree() {return 5;} }; using ct = CellTools; using ots = OrientationTools; using pts = ProjectionTools; using ProjStruct = ProjectionStruct; using rst = RealSpaceTools; using fst = FunctionSpaceTools; constexpr ordinal_type dim = 2; constexpr ordinal_type numCells = 2; constexpr ordinal_type numElemVertexes = 3; constexpr ordinal_type numTotalVertexes = 4; ValueType vertices_orig[numTotalVertexes][dim] = {{0,0},{0,1},{1,0},{1,1}}; ordinal_type tris_orig[numCells][numElemVertexes] = {{0,1,2},{1,2,3}}; ordinal_type tris_rotated[numCells][numElemVertexes]; *outStream << "===============================================================================\n" << "| |\n" << "| Test 1 (DeRham Commutativity - HGRAD-HCURL, HGRAD-HDIV) |\n" << "| |\n" << "===============================================================================\n"; try { //reordering of nodes to explore different orientations const ordinal_type order = 3; ordinal_type reorder[numTotalVertexes] = {0,1,2,3}; do { ordinal_type orderback[numTotalVertexes]; for(ordinal_type i=0;i >()); shards::CellTopology line(shards::getCellTopologyData >()); const ordinal_type numNodesPerElem = tri.getNodeCount(); //computing vertices coords DynRankView ConstructWithLabel(physVertexes, numCells, numNodesPerElem, dim); auto hPhysVertexes = Kokkos::create_mirror_view(physVertexes); for(ordinal_type i=0; i warpBasis(order,POINTTYPE_WARPBLEND); //used only for computing reference points ordinal_type numRefCoords = warpBasis.getCardinality(); DynRankView ConstructWithLabel(refPoints, numRefCoords, dim); warpBasis.getDofCoords(refPoints); // compute orientations for cells (one time computation) DynRankViewIntHost elemNodesHost(&tris[0][0], 2, numElemVertexes); auto elemNodes = Kokkos::create_mirror_view_and_copy(MemSpaceType(),elemNodesHost); Kokkos::DynRankView elemOrts("elemOrts", numCells); ots::getOrientation(elemOrts, elemNodes, tri); Basis_HGRAD_TRI_Cn_FEM basis(order); Basis_HCURL_TRI_In_FEM basisHCurl(order); Basis_HDIV_TRI_In_FEM basisHDiv(order); ordinal_type basisCardinality = basis.getCardinality(); ordinal_type basisHCurlCardinality = basisHCurl.getCardinality(); ordinal_type basisHDivCardinality = basisHDiv.getCardinality(); // compute projection-based interpolation of fun into HGRAD DynRankView ConstructWithLabel(basisCoeffsHGrad, numCells, basisCardinality); { ordinal_type targetCubDegree(Fun::degree()),targetDerivCubDegree(GradFun::degree()); ProjStruct projStruct; projStruct.createHGradProjectionStruct(&basis, targetCubDegree, targetDerivCubDegree); auto evaluationPoints = projStruct.getAllEvalPoints(); auto evaluationGradPoints = projStruct.getAllDerivEvalPoints(); ordinal_type numPoints = evaluationPoints.extent(0), numGradPoints = evaluationGradPoints.extent(0); DynRankView ConstructWithLabel(targetAtEvalPoints, numCells, numPoints); DynRankView ConstructWithLabel(targetGradAtEvalPoints, numCells, numGradPoints, dim); DynRankView ConstructWithLabel(physEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(physEvalGradPoints, numCells, numGradPoints, dim); { DynRankView ConstructWithLabel(linearBasisValuesAtEvalPoints, numCells, numNodesPerElem); DynRankView ConstructWithLabel(linearBasisValuesAtEvalGradPoints, numCells, numNodesPerElem); Kokkos::parallel_for(Kokkos::RangePolicy(0,numCells), KOKKOS_LAMBDA (const int &i) { Fun fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d::getValues(basisValuesAtEvalCurlPoints, evalCurlPoint); for(ordinal_type d=0; d0) { DynRankView ConstructWithLabel(jacobian, numCells, numGradPoints, dim, dim); ct::setJacobian(jacobian, evaluationGradPoints, physVertexes, tri); fst::mapHCurlDataFromPhysToRef(refTargetGradAtEvalPoints,jacobian,targetGradAtEvalPoints); } pts::getHGradBasisCoeffs(basisCoeffsHGrad, refTargetAtEvalPoints, refTargetGradAtEvalPoints, elemOrts, &basis, &projStruct); } // compute projection-based interpolation of gradient of fun into HCURL DynRankView ConstructWithLabel(basisCoeffsHCurl, numCells, basisHCurl.getCardinality()); { ordinal_type targetCubDegree(GradFun::degree()),targetDerivCubDegree(0); ProjStruct projStruct; projStruct.createHCurlProjectionStruct(&basisHCurl, targetCubDegree, targetDerivCubDegree); auto evaluationPoints = projStruct.getAllEvalPoints(); auto evaluationDivPoints = projStruct.getAllDerivEvalPoints(); ordinal_type numPoints = evaluationPoints.extent(0), numDivPoints = evaluationDivPoints.extent(0); DynRankView ConstructWithLabel(targetAtEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(physEvalPoints, numCells, numPoints, dim); { DynRankView ConstructWithLabel(linearBasisValuesAtEvalPoints, numCells, numNodesPerElem); Kokkos::parallel_for(Kokkos::RangePolicy(0,numCells), KOKKOS_LAMBDA (const int &i) { GradFun fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j10000*tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE! " << error << "\n"; *outStream << "Gradient of projection is different than projection of gradient at reference points " << i << "\n"; *outStream << "Gradient of projection at reference points are:\n"; for(ordinal_type j=0; j(0,numCells), KOKKOS_LAMBDA (const int &i) { CurlFun fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j10000*tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE! " << error << "\n"; *outStream << "Curl of projection is different than projection of curl at reference points " << i << "\n"; *outStream << "curl of projection at reference points are:\n"; for(ordinal_type j=0; j >()); shards::CellTopology line(shards::getCellTopologyData >()); const ordinal_type numNodesPerElem = tri.getNodeCount(); //computing vertices coords DynRankView ConstructWithLabel(physVertexes, numCells, numNodesPerElem, dim); auto hostPhysVertexes = Kokkos::create_mirror_view(physVertexes); for(ordinal_type i=0; i warpBasis(order,POINTTYPE_WARPBLEND); //used only for computing reference points ordinal_type numRefCoords = warpBasis.getCardinality(); DynRankView ConstructWithLabel(refPoints, numRefCoords, dim); warpBasis.getDofCoords(refPoints); // compute orientations for cells (one time computation) DynRankViewIntHost elemNodesHost(&tris[0][0], 2, numElemVertexes); auto elemNodes = Kokkos::create_mirror_view_and_copy(MemSpaceType(),elemNodesHost); Kokkos::DynRankView elemOrts("elemOrts", numCells); ots::getOrientation(elemOrts, elemNodes, tri); Basis_HCURL_TRI_In_FEM basis(order); Basis_HVOL_TRI_Cn_FEM basisHVol(order-1); ordinal_type basisCardinality = basis.getCardinality(); ordinal_type basisHVolCardinality = basisHVol.getCardinality(); // compute projection-based interpolation of fun into HVOL DynRankView ConstructWithLabel(basisCoeffsHCurl, numCells, basisCardinality); { ordinal_type targetCubDegree(FunCurl::degree()),targetDerivCubDegree(CurlFunCurl::degree()); ProjStruct projStruct; projStruct.createHCurlProjectionStruct(&basis, targetCubDegree, targetDerivCubDegree); auto evaluationPoints = projStruct.getAllEvalPoints(); auto evaluationCurlPoints = projStruct.getAllDerivEvalPoints(); ordinal_type numPoints = evaluationPoints.extent(0), numCurlPoints = evaluationCurlPoints.extent(0); DynRankView ConstructWithLabel(targetAtEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(targetCurlAtEvalPoints, numCells, numCurlPoints); DynRankView ConstructWithLabel(physEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(physEvalCurlPoints, numCells, numCurlPoints, dim); { DynRankView ConstructWithLabel(linearBasisValuesAtEvalPoints, numCells, numNodesPerElem); DynRankView ConstructWithLabel(linearBasisValuesAtEvalCurlPoints, numCells, numNodesPerElem); Kokkos::parallel_for(Kokkos::RangePolicy(0,numCells), KOKKOS_LAMBDA (const int &i) { FunCurl fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d::getValues(basisValuesAtEvalCurlPoints, evalCurlPoint); for(ordinal_type d=0; d0) { DynRankView ConstructWithLabel(jacobianCurl, numCells, numCurlPoints, dim, dim); DynRankView ConstructWithLabel(jacobianCurl_det, numCells, numCurlPoints); ct::setJacobian(jacobianCurl, evaluationCurlPoints, physVertexes, tri); ct::setJacobianDet (jacobianCurl_det, jacobianCurl); fst::mapHVolDataFromPhysToRef(refTargetCurlAtEvalPoints,jacobianCurl_det,targetCurlAtEvalPoints); } pts::getHCurlBasisCoeffs(basisCoeffsHCurl, refTargetAtEvalPoints, refTargetCurlAtEvalPoints, elemOrts, &basis, &projStruct); } // compute projection-based interpolation of curl of fun into HDIV DynRankView ConstructWithLabel(basisCoeffsHDiv, numCells, basisHVol.getCardinality()); { ordinal_type targetCubDegree(CurlFunCurl::degree()); ProjStruct projStruct; projStruct.createHVolProjectionStruct(&basisHVol, targetCubDegree); auto evaluationPoints = projStruct.getAllEvalPoints(); ordinal_type numPoints = evaluationPoints.extent(0); DynRankView ConstructWithLabel(targetAtEvalPoints, numCells, numPoints); DynRankView ConstructWithLabel(physEvalPoints, numCells, numPoints, dim); { DynRankView ConstructWithLabel(linearBasisValuesAtEvalPoints, numCells, numNodesPerElem); Kokkos::parallel_for(Kokkos::RangePolicy(0,numCells), KOKKOS_LAMBDA (const int &i) { CurlFunCurl fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j10000*tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE! " << error << "\n"; *outStream << "Curl of projection is different than projection of curl at reference points " << i << "\n"; *outStream << "Curl of rojection at reference points are:\n"; for(ordinal_type j=0; j >()); shards::CellTopology line(shards::getCellTopologyData >()); const ordinal_type numNodesPerElem = tri.getNodeCount(); //computing vertices coords DynRankView ConstructWithLabel(physVertexes, numCells, numNodesPerElem, dim); auto hostPhysVertexes = Kokkos::create_mirror_view(physVertexes); for(ordinal_type i=0; i elemOrts("elemOrts", numCells); ots::getOrientation(elemOrts, elemNodes, tri); //compute reference points Basis_HGRAD_TRI_Cn_FEM warpBasis(order,POINTTYPE_WARPBLEND); //used only for computing reference points ordinal_type numRefCoords = warpBasis.getCardinality(); DynRankView ConstructWithLabel(refPoints, numRefCoords, dim); warpBasis.getDofCoords(refPoints); Basis_HDIV_TRI_In_FEM basis(order); Basis_HVOL_TRI_Cn_FEM basisHVol(order-1); ordinal_type basisCardinality = basis.getCardinality(); ordinal_type basisHVolCardinality = basisHVol.getCardinality(); // compute projection-based interpolation of fun into HDIV DynRankView ConstructWithLabel(basisCoeffsHDiv, numCells, basisCardinality); { ordinal_type targetCubDegree(FunDiv::degree()),targetDerivCubDegree(DivFunDiv::degree()); ProjStruct projStruct; projStruct.createHDivProjectionStruct(&basis, targetCubDegree, targetDerivCubDegree); auto evaluationPoints = projStruct.getAllEvalPoints(); auto evaluationDivPoints = projStruct.getAllDerivEvalPoints(); ordinal_type numPoints = evaluationPoints.extent(0), numDivPoints = evaluationDivPoints.extent(0); DynRankView ConstructWithLabel(targetAtEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(targetDivAtEvalPoints, numCells, numDivPoints); DynRankView ConstructWithLabel(physEvalPoints, numCells, numPoints, dim); DynRankView ConstructWithLabel(physEvalDivPoints, numCells, numDivPoints, dim); { DynRankView ConstructWithLabel(linearBasisValuesAtEvalPoints, numCells, numNodesPerElem); DynRankView ConstructWithLabel(linearBasisValuesAtEvalDivPoints, numCells, numNodesPerElem); Kokkos::parallel_for(Kokkos::RangePolicy(0,numCells), KOKKOS_LAMBDA (const int &i) { FunDiv fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d::getValues(basisValuesAtEvalDivPoints, evalDivPoint); for(ordinal_type d=0; d(0,numCells), KOKKOS_LAMBDA (const int &i) { DivFunDiv fun; auto basisValuesAtEvalPoints = Kokkos::subview(linearBasisValuesAtEvalPoints,i,Kokkos::ALL()); for(ordinal_type j=0; j::getValues(basisValuesAtEvalPoints, evalPoint); for(ordinal_type d=0; d(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j(0,numCells), KOKKOS_LAMBDA (const int &i) { for(ordinal_type j=0; j10000*tol) { errorFlag++; *outStream << std::setw(70) << "^^^^----FAILURE! " << error << "\n"; *outStream << "Divergence of projection is different than projection of divergence at reference points " << i << "\n"; *outStream << "Divergence of projection at reference points are:\n"; for(ordinal_type j=0; j