// @HEADER // ***************************************************************************** // Trilinos: An Object-Oriented Solver Framework // // Copyright 2001-2024 NTESS and the Trilinos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** \file example_DivLSFEM.cpp \brief Example solution of a div-curl system on a hexahedral mesh using div-conforming (face) elements. This example uses Pamgen to generate a hexahedral mesh, Intrepid to build mass and stiffness matrices, and ML to solve. \verbatim System div v + \phi = f in Omega v+ A grad \phi = 0 in Omega Dirichlet BC: \phi given on Gamma Omega is the box [0,1]^3 where f is derived from a prescribed solution \phi(x,y,z)=-sin^2(\pi x)*sin^2(\pi y)*sin^2(\pi z) or other, discontinuous solution, aka "patch test" \endverbatim \remark Usage \verbatim ./TrilinosCouplings_examples_scaling_Example_DivLSFEM.exe inputfile.xml inputfile.xml (optional) - xml input file containing Pamgen mesh description and material parameters for each Pamgen block, if not present code attempts to read DivLSFEMin.xml. \endverbatim **/ // TrilinosCouplings includes #include "TrilinosCouplings_config.h" #include "TrilinosCouplings_Pamgen_Utils.hpp" // Intrepid includes #include "Intrepid_FunctionSpaceTools.hpp" #include "Intrepid_FieldContainer.hpp" #include "Intrepid_CellTools.hpp" #include "Intrepid_ArrayTools.hpp" #include "Intrepid_HCURL_HEX_I1_FEM.hpp" #include "Intrepid_HGRAD_HEX_C1_FEM.hpp" #include "Intrepid_HDIV_HEX_I1_FEM.hpp" #include "Intrepid_RealSpaceTools.hpp" #include "Intrepid_DefaultCubatureFactory.hpp" #include "Intrepid_Utils.hpp" // Epetra includes #include "Epetra_Time.h" #include "Epetra_Map.h" #ifdef HAVE_MPI #include "Epetra_MpiComm.h" #else #include "Epetra_SerialComm.h" #endif #include "Epetra_FECrsMatrix.h" #include "Epetra_FEVector.h" #include "Epetra_Vector.h" #include "Epetra_LinearProblem.h" #include "Epetra_Import.h" #include "Epetra_Export.h" // Teuchos includes #include "Teuchos_oblackholestream.hpp" #include "Teuchos_RCP.hpp" #include "Teuchos_BLAS.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_ParameterList.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" // Shards includes #include "Shards_CellTopology.hpp" // EpetraExt includes #include "EpetraExt_RowMatrixOut.h" #include "EpetraExt_MultiVectorOut.h" #include "EpetraExt_VectorOut.h" #include "EpetraExt_MatrixMatrix.h" // Pamgen includes #include "create_inline_mesh.h" #include "pamgen_im_exodusII_l.h" #include "pamgen_im_ne_nemesisI_l.h" #include "pamgen_extras.h" // AztecOO includes #include "AztecOO.h" // ML Includes #include "ml_config.h" // minor abuse #include "ml_epetra_utils.h" #include "ml_GradDiv.h" #include "ml_MultiLevelPreconditioner.h" // Teko includes #include "Teko_CloneFactory.hpp" #include "Teko_BlockedEpetraOperator.hpp" #include "Teko_JacobiPreconditionerFactory.hpp" #include "Teko_EpetraBlockPreconditioner.hpp" // Thyra includes #include "Thyra_EpetraLinearOp.hpp" #define ABS(x) ((x)>0?(x):-(x)) //#define DUMP_DATA using namespace std; using namespace Intrepid; using namespace Teuchos; using namespace Thyra; using namespace Teko; using namespace ML_Epetra; struct fecomp{ bool operator () ( topo_entity* x, topo_entity* y )const { if(x->sorted_local_node_ids < y->sorted_local_node_ids)return true; return false; } }; template void evaluateMaterialTensor(ArrayOut & matTensorValues, const ArrayIn & evaluationPoints); template void evaluateMaterialTensorInv(ArrayOut & matTensorValues, const ArrayIn & evaluationPoints); template void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z); template void materialTensorInv(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z); template Scalar evalKappa(const Scalar& x, const Scalar& y, const Scalar& z); template Scalar evalF(const Scalar& x, const Scalar& y, const Scalar& z); template Scalar evalPhi(const Scalar& x, const Scalar& y, const Scalar& z); template int evalu(Scalar & uExact0, Scalar & uExact1, Scalar & uExact2, Scalar & x, Scalar & y, Scalar & z); template Scalar evalDivu(Scalar & x, Scalar & y, Scalar & z); template int evalGradPhi(Scalar & phiGExact0, Scalar & phiGExact1, Scalar & phiGExact2, Scalar & x, Scalar & y, Scalar & z); int main(int argc, char *argv[]) { int error = 0; #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); Epetra_MpiComm Comm(MPI_COMM_WORLD); int MyPID = Comm.MyPID(); #else int rank=0; int numProcs=1; int MyPID = 0; Epetra_SerialComm Comm; #endif Epetra_Time Time(Comm); //Check number of arguments if (argc > 2) { std::cout <<"\n>>> ERROR: Invalid number of arguments.\n\n"; std::cout <<"Usage:\n\n"; std::cout <<" ./TrilinosCouplings_examples_scaling_Example_DivLSFEM.exe [inputfile.xml] \n\n"; std::cout <<" inputfile.xml(optional) - xml file with description of Pamgen mesh \n"; std::cout <<" and material parameters for each block \nn"; exit(1); } if (MyPID == 0) { std::cout \ << "===============================================================================\n" \ << "| |\n" \ << "| Example: Darcy Flow on Hexahedral Mesh |\n" \ << "| |\n" \ << "| Questions? Contact |\n" \ << "| |\n" \ << "| |\n" \ << "| |\n" \ << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \ << "| Pamgen's website: http://trilinos.sandia.gov/packages/pamgen |\n" \ << "| ML's website: http://trilinos.sandia.gov/packages/ml |\n" \ << "| Trilinos website: http://trilinos.sandia.gov |\n" \ << "| |\n" \ << "===============================================================================\n"; } long long * node_comm_proc_ids = NULL; long long * node_cmap_node_cnts = NULL; long long * node_cmap_ids = NULL; long long ** comm_node_ids = NULL; long long ** comm_node_proc_ids = NULL; std::set < topo_entity * , fecomp > edge_set; std::set < topo_entity * , fecomp > face_set; std::vector < topo_entity * > edge_vector; std::vector < topo_entity * > face_vector; std::vector < int > edge_comm_procs; int dim = 3; // ************************************ GET INPUTS ************************************** //what problem do we solve? // "patch test" or... //#define PATCH // ...smooth solution #define SMOOTH // Command line for xml file, otherwise use default std::string xmlInFileName; if(argc>=2) xmlInFileName=string(argv[1]); else xmlInFileName="Darcy.xml"; // Read xml file into parameter list Teuchos::ParameterList inputList; if(xmlInFileName.length()) { if (MyPID == 0) { std::cout << "\nReading parameter list from the XML file \""<(inputList,"meshInput"); // *********************************** CELL TOPOLOGY ********************************** // Get cell topology for base hexahedron typedef shards::CellTopology CellTopology; CellTopology hex_8(shards::getCellTopologyData >() ); // Get dimensions int numNodesPerElem = hex_8.getNodeCount(); int numEdgesPerElem = hex_8.getEdgeCount(); int numFacesPerElem = hex_8.getSideCount(); int numNodesPerEdge = 2; int numNodesPerFace = 4; int numEdgesPerFace = 4; int spaceDim = hex_8.getDimension(); // Build reference element edge to node map FieldContainer refEdgeToNode(numEdgesPerElem,numNodesPerEdge); for (int i=0; i refFaceToNode(numFacesPerElem,numNodesPerFace); for (int i=0; i refFaceToEdge(numFacesPerElem,numEdgesPerFace); refFaceToEdge(0,0)=0; refFaceToEdge(0,1)=9; refFaceToEdge(0,2)=4; refFaceToEdge(0,3)=8; refFaceToEdge(1,0)=1; refFaceToEdge(1,1)=10; refFaceToEdge(1,2)=5; refFaceToEdge(1,3)=9; refFaceToEdge(2,0)=2; refFaceToEdge(2,1)=11; refFaceToEdge(2,2)=6; refFaceToEdge(2,3)=10; refFaceToEdge(3,0)=8; refFaceToEdge(3,1)=7; refFaceToEdge(3,2)=11; refFaceToEdge(3,3)=3; refFaceToEdge(4,0)=3; refFaceToEdge(4,1)=2; refFaceToEdge(4,2)=1; refFaceToEdge(4,3)=0; refFaceToEdge(5,0)=4; refFaceToEdge(5,1)=5; refFaceToEdge(5,2)=6; refFaceToEdge(5,3)=7; // *********************************** GENERATE MESH ************************************ if (MyPID == 0) { std::cout << "Generating mesh ... \n\n"; } // Generate mesh with Pamgen long long maxInt = 9223372036854775807LL; long long cr_result = Create_Pamgen_Mesh(meshInput.c_str(), dim, rank, numProcs, maxInt); TrilinosCouplings::pamgen_error_check(std::cout,cr_result); // Get local mesh size info char title[100]; long long numDim; long long numNodes; long long numElems; long long numElemBlk; long long numNodeSets; long long numSideSets; int id = 0; im_ex_get_init_l(id, title, &numDim, &numNodes, &numElems, &numElemBlk, &numNodeSets, &numSideSets); // Get global mesh size info long long numNodesGlobal; long long numElemsGlobal; long long numElemBlkGlobal; long long numNodeSetsGlobal; long long numSideSetsGlobal; im_ne_get_init_global_l(id, &numNodesGlobal, &numElemsGlobal, &numElemBlkGlobal, &numNodeSetsGlobal, &numSideSetsGlobal); long long * block_ids = new long long [numElemBlk]; error += im_ex_get_elem_blk_ids_l(id, block_ids); long long *nodes_per_element = new long long [numElemBlk]; long long *element_attributes = new long long [numElemBlk]; long long *elements = new long long [numElemBlk]; char **element_types = new char * [numElemBlk]; long long **elmt_node_linkage = new long long * [numElemBlk]; for(long long i = 0; i < numElemBlk; i ++){ element_types[i] = new char [MAX_STR_LENGTH + 1]; error += im_ex_get_elem_block_l(id, block_ids[i], element_types[i], (long long*)&(elements[i]), (long long*)&(nodes_per_element[i]), (long long*)&(element_attributes[i])); } /*connectivity*/ for(long long b = 0; b < numElemBlk; b++){ elmt_node_linkage[b] = new long long [nodes_per_element[b]*elements[b]]; error += im_ex_get_elem_conn_l(id,block_ids[b],elmt_node_linkage[b]); } // Get mu value for each block of elements from parameter list /* double *mu = new double [numElemBlk]; for(int b = 0; b < numElemBlk; b++){ stringstream muBlock; muBlock.clear(); muBlock << "mu" << b; mu[b] = inputList.get(muBlock.str(),1.0); }*/ // Get node-element connectivity and set element mu value int telct = 0; FieldContainer elemToNode(numElems,numNodesPerElem); FieldContainer muVal(numElems); for(long long b = 0; b < numElemBlk; b++){ for(long long el = 0; el < elements[b]; el++){ for (int j=0; j nodeCoord(numNodes,dim); double * nodeCoordx = new double [numNodes]; double * nodeCoordy = new double [numNodes]; double * nodeCoordz = new double [numNodes]; im_ex_get_coord_l(id,nodeCoordx,nodeCoordy,nodeCoordz); for (int i=0; i 0){ node_comm_proc_ids = new long long [num_node_comm_maps]; node_cmap_node_cnts = new long long [num_node_comm_maps]; node_cmap_ids = new long long [num_node_comm_maps]; comm_node_ids = new long long* [num_node_comm_maps]; comm_node_proc_ids = new long long* [num_node_comm_maps]; long long * elem_cmap_ids = new long long [num_elem_comm_maps]; long long * elem_cmap_elem_cnts = new long long [num_elem_comm_maps]; if ( im_ne_get_cmap_params_l( id, node_cmap_ids, (long long*)node_cmap_node_cnts, elem_cmap_ids, (long long*)elem_cmap_elem_cnts, 0/*not used proc_id*/ ) < 0 )++error; for(long long j = 0; j < num_node_comm_maps; j++) { comm_node_ids[j] = new long long [node_cmap_node_cnts[j]]; comm_node_proc_ids[j] = new long long [node_cmap_node_cnts[j]]; if ( im_ne_get_node_cmap_l( id, node_cmap_ids[j], comm_node_ids[j], comm_node_proc_ids[j], 0/*not used proc_id*/ ) < 0 )++error; node_comm_proc_ids[j] = comm_node_proc_ids[j][0]; } } //Calculate global node ids long long * globalNodeIds = new long long[numNodes]; bool * nodeIsOwned = new bool[numNodes]; calc_global_node_ids(globalNodeIds, nodeIsOwned, numNodes, num_node_comm_maps, node_cmap_node_cnts, node_comm_proc_ids, comm_node_ids, rank); // Count owned nodes int numOwnedNodes=0; for(int i=0;i elemToEdge(numElems,numEdgesPerElem); FieldContainer elemToFace(numElems,numFacesPerElem); // calculate edge and face ids int elct = 0; for(long long b = 0; b < numElemBlk; b++){ if(nodes_per_element[b] == 4){ } else if (nodes_per_element[b] == 8){ //loop over all elements and push their edges onto a set if they are not there already for(long long el = 0; el < elements[b]; el++){ std::set< topo_entity *, fecomp > ::iterator fit; for (int i=0; i < numEdgesPerElem; i++){ topo_entity * teof = new topo_entity; for(int j = 0; j < numNodesPerEdge;j++){ teof->add_node(elmt_node_linkage[b][el*numNodesPerElem + refEdgeToNode(i,j)],globalNodeIds); } teof->sort(); fit = edge_set.find(teof); if(fit == edge_set.end()){ teof->local_id = edge_vector.size(); edge_set.insert(teof); elemToEdge(elct,i)= edge_vector.size(); edge_vector.push_back(teof); } else{ elemToEdge(elct,i) = (*fit)->local_id; delete teof; } } for (int i=0; i < numFacesPerElem; i++){ topo_entity * teof = new topo_entity; for(int j = 0; j < numNodesPerFace;j++){ teof->add_node(elmt_node_linkage[b][el*numNodesPerElem + refFaceToNode(i,j)],globalNodeIds); } teof->sort(); fit = face_set.find(teof); if(fit == face_set.end()){ teof->local_id = face_vector.size(); face_set.insert(teof); elemToFace(elct,i)= face_vector.size(); face_vector.push_back(teof); } else{ elemToFace(elct,i) = (*fit)->local_id; delete teof; } } elct ++; } } } // Edge to Node connectivity FieldContainer edgeToNode(edge_vector.size(), numNodesPerEdge); for(unsigned ect = 0; ect != edge_vector.size(); ect++){ std::list::iterator elit; int nct = 0; for(elit = edge_vector[ect]->local_node_ids.begin(); elit != edge_vector[ect]->local_node_ids.end(); elit ++){ edgeToNode(ect,nct) = *elit-1; nct++; } } // Face to Node connectivity FieldContainer faceToNode(face_vector.size(), numNodesPerFace); for(unsigned fct = 0; fct != face_vector.size(); fct++){ std::list::iterator flit; int nct = 0; for(flit = face_vector[fct]->local_node_ids.begin(); flit != face_vector[fct]->local_node_ids.end(); flit ++){ faceToNode(fct,nct) = *flit-1; nct++; } } // Face to Edge connectivity FieldContainer faceToEdge(face_vector.size(), numEdgesPerFace); FieldContainer faceDone(face_vector.size()); for (int ielem = 0; ielem < numElems; ielem++){ for (int iface = 0; iface < numFacesPerElem; iface++){ if (!faceDone(elemToFace(ielem,iface))){ for (int iedge = 0; iedge < numEdgesPerFace; iedge++){ faceToEdge(elemToFace(ielem,iface),iedge) = elemToEdge(ielem,refFaceToEdge(iface,iedge)); faceDone(elemToFace(ielem,iface))=1; } } } } int numEdges = edge_vector.size(); int numFaces = face_vector.size(); // Calculate global edge and face numbering std::string doing_type; doing_type = "EDGES"; calc_global_ids(edge_vector, comm_node_ids, node_comm_proc_ids, node_cmap_node_cnts, num_node_comm_maps, rank, doing_type); doing_type = "FACES"; calc_global_ids(face_vector, comm_node_ids, node_comm_proc_ids, node_cmap_node_cnts, num_node_comm_maps, rank, doing_type); // Build list of owned global edge ids long long * globalEdgeIds = new long long[numEdges]; bool * edgeIsOwned = new bool[numEdges]; int numOwnedEdges=0; for (int i=0; iowned; globalEdgeIds[i] = edge_vector[i]->global_id; if (edgeIsOwned[i]){ numOwnedEdges++; } } int * ownedEdgeIds = new int[numOwnedEdges]; int nedge=0; for (int i=0; iowned; globalFaceIds[i] = face_vector[i]->global_id; if (faceIsOwned[i]){ numOwnedFaces++; } } int * ownedFaceIds = new int[numOwnedFaces]; int nface=0; for (int i=0; i edgeOnBoundary(numEdges); FieldContainer faceOnBoundary(numFaces); // Get boundary (side set) information long long * sideSetIds = new long long [numSideSets]; long long numSidesInSet; long long numDFinSet; int numBndyFaces=0; im_ex_get_side_set_ids_l(id,sideSetIds); for (int i=0; i 0){ long long * sideSetElemList = new long long [numSidesInSet]; long long * sideSetSideList = new long long [numSidesInSet]; im_ex_get_side_set_l(id,sideSetIds[i],sideSetElemList,sideSetSideList); for (int j=0; j nodeOnBoundary(numNodes); for (int i=0; i cubFactory; int cubDegree = 2; Teuchos::RCP > hexCub = cubFactory.create(hex_8, cubDegree); int cubDim = hexCub->getDimension(); int numCubPoints = hexCub->getNumPoints(); FieldContainer cubPoints(numCubPoints, cubDim); FieldContainer cubWeights(numCubPoints); hexCub->getCubature(cubPoints, cubWeights); // Get numerical integration points and weights for hexahedron face // (needed for rhs boundary term) // Define topology of the face parametrization domain as [-1,1]x[-1,1] CellTopology paramQuadFace(shards::getCellTopologyData >() ); // Define cubature DefaultCubatureFactory cubFactoryFace; Teuchos::RCP > hexFaceCubature = cubFactoryFace.create(paramQuadFace, 3); int cubFaceDim = hexFaceCubature -> getDimension(); int numFacePoints = hexFaceCubature -> getNumPoints(); // Define storage for cubature points and weights on [-1,1]x[-1,1] FieldContainer paramGaussWeights(numFacePoints); FieldContainer paramGaussPoints(numFacePoints,cubFaceDim); // Define storage for cubature points on workset faces hexFaceCubature -> getCubature(paramGaussPoints, paramGaussWeights); // ************************************** BASIS *************************************** // Define basis if (MyPID == 0) { std::cout << "Getting basis ... \n\n"; } Basis_HCURL_HEX_I1_FEM > hexHCurlBasis; Basis_HDIV_HEX_I1_FEM > hexHDivBasis; Basis_HGRAD_HEX_C1_FEM > hexHGradBasis; int numFieldsC = hexHCurlBasis.getCardinality(); int numFieldsD = hexHDivBasis.getCardinality(); int numFieldsG = hexHGradBasis.getCardinality(); // Evaluate basis at cubature points FieldContainer hexDVals(numFieldsD, numCubPoints, spaceDim); FieldContainer hexDivs(numFieldsD, numCubPoints); FieldContainer hexGVals(numFieldsG, numCubPoints); FieldContainer hexGrads(numFieldsG, numCubPoints, spaceDim); hexHDivBasis.getValues(hexDVals, cubPoints, OPERATOR_VALUE); hexHDivBasis.getValues(hexDivs, cubPoints, OPERATOR_DIV); hexHGradBasis.getValues(hexGVals, cubPoints, OPERATOR_VALUE); hexHGradBasis.getValues(hexGrads, cubPoints, OPERATOR_GRAD); /**********************************************************************************/ /*************************BUILD INCIDENCE MATRIX***********************************/ /**********************************************************************************/ Epetra_FECrsMatrix DCurl(Epetra_DataAccess::Copy, globalMapD, 4); Epetra_FECrsMatrix DGrad(Epetra_DataAccess::Copy, globalMapC, 2); // Edge to node incidence matrix double vals[2]; vals[0]=-1.0; vals[1]=1.0; for (int j=0; j faceDone2(numFaces); for (int i=0; i= numEdgesPerFace) indm=0; if (edgeToNode(faceToEdge(iface,m),1) == edgeToNode(faceToEdge(iface,indm),0) || edgeToNode(faceToEdge(iface,m),1) == edgeToNode(faceToEdge(iface,indm),1)){ vals[m]=1.0;} else vals[m]=-1.0; // This is a convoluted way to account for edge orientations that // may be incorrect on the local processor because the edge is // not owned by the local processor. int edgeIndex = -1; if (!edgeIsOwned[faceToEdge(iface,m)]){ for (int l=0; l 3 && faceIsOwned[elemToFace(i,5)]){ vals[m]=-1.0*vals[m]; } } } // end loop over face edges DCurl.InsertGlobalValues(1, &rowNum, 4, colNum, vals); faceDone2(iface)=1; } // end if face is owned and face not done } // end loop over element faces } // end loop over elements DCurl.GlobalAssemble(false); DCurl.FillComplete(globalMapC,globalMapD); if(MyPID==0) {std::cout << "Building incidence matrices " << Time.ElapsedTime() << " sec \n" ; Time.ResetStartTime();} // Build Face-Node Incidence Matrix Epetra_CrsMatrix DGrad1(DGrad); Epetra_CrsMatrix DCurl1(DCurl); Epetra_CrsMatrix FaceNode(Epetra_DataAccess::Copy,globalMapD,0); DGrad1.PutScalar(1.0); DCurl1.PutScalar(1.0); EpetraExt::MatrixMatrix::Multiply(DCurl1,false,DGrad1,false,FaceNode); FaceNode.PutScalar(1.0); /**********************************************************************************/ /******************** INHOMOGENEOUS BOUNDARY CONDITIONS ***************************/ /**********************************************************************************/ typedef CellTools CellTools; typedef FunctionSpaceTools fst; FieldContainer bndyFaceVal(numBndyFaces); FieldContainer bndyFaceToFace(numFaces); FieldContainer refFacePoints(numFacePoints,spaceDim); FieldContainer bndyFacePoints(1,numFacePoints,spaceDim); FieldContainer bndyFaceJacobians(1,numFacePoints,spaceDim,spaceDim); FieldContainer faceNorm(1,numFacePoints,spaceDim); FieldContainer uDotNormal(1,numFacePoints); FieldContainer uFace(numFacePoints,spaceDim); FieldContainer nodes(1, numNodesPerElem, spaceDim); int ibface=0; // Evaluate normal at face quadrature points for (int ielem=0; ielem hexNodes(numCells, numNodesPerElem, spaceDim); FieldContainer hexEdgeSigns(numCells, numFieldsC); FieldContainer hexFaceSigns(numCells, numFieldsD); // Containers for Jacobian FieldContainer hexJacobian(numCells, numCubPoints, spaceDim, spaceDim); FieldContainer hexJacobInv(numCells, numCubPoints, spaceDim, spaceDim); FieldContainer hexJacobDet(numCells, numCubPoints); FieldContainer weightedMeasure(numCells, numCubPoints); // Containers for element HDIV mass matrix FieldContainer massMatrixD(numCells, numFieldsD, numFieldsD); FieldContainer hexDValsTransformed(numCells, numFieldsD, numCubPoints, spaceDim); FieldContainer hexDValsTransformedWeighted(numCells, numFieldsD, numCubPoints, spaceDim); FieldContainer hexDValsTransformedMatrAinv(numCells, numFieldsD, numCubPoints, spaceDim); // Containers for element HDIV stiffness matrix FieldContainer stiffMatrixD(numCells, numFieldsD, numFieldsD); FieldContainer hexDivsTransformed(numCells, numFieldsD, numCubPoints); FieldContainer hexDivsTransformedWeighted(numCells, numFieldsD, numCubPoints); // Containers for element HDIV stiffness matrix FieldContainer stiffMatrixG(numCells, numFieldsG, numFieldsG); // Containers for element HDIV stiffness matrix FieldContainer massMatrixG(numCells, numFieldsG, numFieldsG); FieldContainer hexGValsTransformed(numCells, numFieldsG, numCubPoints); FieldContainer hexGValsTransformedWeighted(numCells, numFieldsG, numCubPoints); FieldContainer hexGradsTransformed(numCells, numFieldsG, numCubPoints, spaceDim); FieldContainer hexGradsTransformedWeighted(numCells, numFieldsG, numCubPoints, spaceDim); FieldContainer hexGradsTransformedMatrA(numCells, numFieldsG, numCubPoints, spaceDim); FieldContainer hexGradsTransformedWeightedMatrA(numCells, numFieldsG, numCubPoints, spaceDim); // Containers for element GRAD(HGRAD) x HDIV matrix FieldContainer stiffMatrixDG(numCells, numFieldsD, numFieldsG); FieldContainer stiffMatrixDG2(numCells, numFieldsD, numFieldsG); // Containers for element HDIV x GRAD(HGRAD) matrix (not really necessary, it is a transpose of stiffMatrixDG) FieldContainer stiffMatrixGD(numCells, numFieldsG, numFieldsD); FieldContainer stiffMatrixGD2(numCells, numFieldsG, numFieldsD); // Containers for right hand side vectors FieldContainer rhsDatah(numCells, numCubPoints); FieldContainer hD(numCells, numFieldsD); FieldContainer kD(numCells, numFieldsG); FieldContainer gDBoundary(numCells, numFieldsD); FieldContainer refGaussPoints(numFacePoints,spaceDim); FieldContainer worksetGaussPoints(numCells,numFacePoints,spaceDim); FieldContainer worksetJacobians(numCells, numFacePoints, spaceDim, spaceDim); FieldContainer worksetJacobDet(numCells, numFacePoints); FieldContainer worksetFaceN(numCells, numFacePoints, spaceDim); FieldContainer worksetVFieldVals(numCells, numFacePoints, spaceDim); FieldContainer worksetDValsTransformed(numCells, numFieldsD, numFacePoints, spaceDim); FieldContainer curluFace(numCells, numFacePoints, spaceDim); FieldContainer worksetDataCrossField(numCells, numFieldsD, numFacePoints, spaceDim); // Container for cubature points in physical space FieldContainer physCubPoints(numCells,numCubPoints, cubDim); FieldContainer worksetMaterialVals (numCells, numCubPoints, spaceDim, spaceDim); FieldContainer worksetMaterialValsInv (numCells, numCubPoints, spaceDim, spaceDim); // Global matrices arrays in Epetra format //THIS MATRIX IS FOR PRECONDITIONING Epetra_FECrsMatrix StiffG(Epetra_DataAccess::Copy, globalMapG, numFieldsG); //last agr here is not that important, epetra will extend storage if needed Epetra_FECrsMatrix jointMatrix(Epetra_DataAccess::Copy, globalMapJoint, numFieldsD); Epetra_FEVector rhsD(globalMapD); Epetra_FEVector jointVector(globalMapJoint); //this vector is only for output Epetra_FEVector jointVectorGIDs(globalMapJoint); #ifdef DUMP_DATA std::stringstream fSignfname; fSignfname << "faceSigns"; fSignfname << MyPID << ".dat"; ofstream fSignsout(fSignfname.str().c_str()); #endif // *** Element loop *** for (int k=0; k= numNodesPerFace) indf=0; if (elemToNode(k,refFaceToNode(j,0))==faceToNode(elemToFace(k,j),i) && elemToNode(k,refFaceToNode(j,1))==faceToNode(elemToFace(k,j),indf)) hexFaceSigns(0,j) = 1.0; } if (!faceIsOwned[elemToFace(k,j)]){ hexFaceSigns(0,j)=-1.0*hexFaceSigns(0,j); } #ifdef DUMP_DATA fSignsout << hexFaceSigns(0,j) << " "; #endif } #ifdef DUMP_DATA fSignsout << "\n"; #endif // Edge signs for (int j=0; j(weightedMeasure, hexJacobDet, cubWeights); // transform to physical coordinates fst::HDIVtransformVALUE(hexDValsTransformed, hexJacobian, hexJacobDet, hexDVals); // multiply by weighted measure fst::multiplyMeasure(hexDValsTransformedWeighted, weightedMeasure, hexDValsTransformed); // Compute A*Dvals fst::tensorMultiplyDataField(hexDValsTransformedMatrAinv, worksetMaterialValsInv, hexDValsTransformed); // integrate to compute element mass matrix fst::integrate(massMatrixD, hexDValsTransformedMatrAinv, hexDValsTransformedWeighted, COMP_BLAS); // apply face signs fst::applyLeftFieldSigns(massMatrixD, hexFaceSigns); fst::applyRightFieldSigns(massMatrixD, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsD; row++){ for (int col = 0; col < numFieldsD; col++){ int rowIndex = globalFaceIds[elemToFace(k,row)]; int colIndex = globalFaceIds[elemToFace(k,col)]; double val = massMatrixD(0,row,col); jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // ************************ Compute element HDiv stiffness matrices ***************************** // transform to physical coordinates fst::HDIVtransformDIV(hexDivsTransformed, hexJacobDet, hexDivs); // multiply by weighted measure fst::multiplyMeasure(hexDivsTransformedWeighted, weightedMeasure, hexDivsTransformed); // integrate to compute element stiffness matrix fst::integrate(stiffMatrixD, hexDivsTransformed, hexDivsTransformedWeighted, COMP_BLAS); // apply face signs fst::applyLeftFieldSigns(stiffMatrixD, hexFaceSigns); fst::applyRightFieldSigns(stiffMatrixD, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsD; row++){ for (int col = 0; col < numFieldsD; col++){ int rowIndex = globalFaceIds[elemToFace(k,row)]; int colIndex = globalFaceIds[elemToFace(k,col)]; double val = stiffMatrixD(0,row,col); jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // **************** Compute element HGrad stiffness matrices ******************************* // transform to physical coordinates fst::HGRADtransformGRAD(hexGradsTransformed, hexJacobInv, hexGrads); // multiply values with weighted measure fst::multiplyMeasure(hexGradsTransformedWeighted, weightedMeasure, hexGradsTransformed); // Compute A*Hgrads fst::tensorMultiplyDataField(hexGradsTransformedWeightedMatrA, worksetMaterialVals, hexGradsTransformedWeighted); // integrate to compute element stiff matrix fst::integrate(stiffMatrixG, hexGradsTransformed, hexGradsTransformedWeightedMatrA, COMP_BLAS); // assemble into global matrix for (int row = 0; row < numFieldsG; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsG; col++){ rowIndex = globalNodeIds[elemToNode(k,row)]; colIndex = globalNodeIds[elemToNode(k,col)]; double val = stiffMatrixG(0,row,col); StiffG.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); rowIndex+=numFacesGlobal; colIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // **************** Compute element HGrad mass matrices ******************************* // transform to physical coordinates fst::HGRADtransformVALUE(hexGValsTransformed, hexGVals); // multiply values with weighted measure fst::multiplyMeasure(hexGValsTransformedWeighted, weightedMeasure, hexGValsTransformed); // integrate to compute element stiff matrix fst::integrate(massMatrixG, hexGValsTransformed, hexGValsTransformedWeighted, COMP_BLAS); // assemble into global matrix for (int row = 0; row < numFieldsG; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsG; col++){ rowIndex = globalNodeIds[elemToNode(k,row)]; colIndex = globalNodeIds[elemToNode(k,col)]; double val = massMatrixG(0,row,col); //do we need this for preconditioning too? //StiffG.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); rowIndex+=numFacesGlobal; colIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // *********************** Compute element Div x GRAD(HGrad) matrices ********************** // Compute A*Hgrads fst::tensorMultiplyDataField(hexGradsTransformedWeightedMatrA, worksetMaterialVals, hexGradsTransformedWeighted); fst::integrate(stiffMatrixDG, hexDValsTransformedMatrAinv,hexGradsTransformedWeightedMatrA, COMP_BLAS); // apply face signs fst::applyLeftFieldSigns(stiffMatrixDG, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsD; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsG; col++){ rowIndex = globalFaceIds[elemToFace(k,row)]; colIndex = globalNodeIds[elemToNode(k,col)]; double val = stiffMatrixDG(0,row,col); colIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } /////////////and now matrix divs by gvals fst::integrate(stiffMatrixDG2, hexDivsTransformed,hexGValsTransformedWeighted, COMP_BLAS); // apply face signs fst::applyLeftFieldSigns(stiffMatrixDG2, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsD; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsG; col++){ rowIndex = globalFaceIds[elemToFace(k,row)]; colIndex = globalNodeIds[elemToNode(k,col)]; double val = stiffMatrixDG2(0,row,col); colIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // ************************** Compute element GRAD(HGrad) x Div matrices ************* fst::integrate(stiffMatrixGD, hexGradsTransformed,hexDValsTransformedWeighted, COMP_BLAS); // apply face signs fst::applyRightFieldSigns(stiffMatrixGD, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsG; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsD; col++){ rowIndex = globalNodeIds[elemToNode(k,row)]; colIndex = globalFaceIds[elemToFace(k,col)]; double val = stiffMatrixGD(0,row,col); rowIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } fst::integrate(stiffMatrixGD2, hexGValsTransformed,hexDivsTransformedWeighted, COMP_BLAS); // apply face signs fst::applyRightFieldSigns(stiffMatrixGD2, hexFaceSigns); // assemble into global matrix for (int row = 0; row < numFieldsG; row++){ int rowIndex,colIndex; for (int col = 0; col < numFieldsD; col++){ rowIndex = globalNodeIds[elemToNode(k,row)]; colIndex = globalFaceIds[elemToFace(k,col)]; double val = stiffMatrixGD2(0,row,col); rowIndex+=numFacesGlobal; jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val); } } // ******************************* Build right hand side ************************************ // evaluate right hand side functions at physical points for (int nPt = 0; nPt < numCubPoints; nPt++){ double x = physCubPoints(0,nPt,0); double y = physCubPoints(0,nPt,1); double z = physCubPoints(0,nPt,2); rhsDatah(0,nPt) = evalF(x,y,z); } // integrate (f,div v_hat) term fst::integrate(hD, rhsDatah, hexDivsTransformedWeighted, COMP_BLAS); // apply signs fst::applyFieldSigns(hD, hexFaceSigns); // assemble into global vector for (int row = 0; row < numFieldsD; row++){ int rowIndex = globalFaceIds[elemToFace(k,row)]; double val = hD(0,row); rhsD.SumIntoGlobalValues(1, &rowIndex, &val); jointVector.SumIntoGlobalValues(1, &rowIndex, &val); } // integrate (f,\phi_hat) term fst::integrate(kD, rhsDatah, hexGValsTransformedWeighted, COMP_BLAS); // assemble into global vector for (int row = 0; row < numFieldsG; row++){ int rowIndex = globalNodeIds[elemToNode(k,row)] + numFacesGlobal; double val = kD(0,row); jointVector.SumIntoGlobalValues(1, &rowIndex, &val); } } // *** end element loop *** for(int i=0;i > tvec; tvec.resize(2); tvec[0].resize(globalMapD.NumMyElements()); tvec[1].resize(globalMapG.NumMyElements()); for(int i=0;i BlockOp=rcp(new Teko::Epetra::BlockedEpetraOperator(tvec,rcp(&jointMatrix,false))); // Grab diagonal blocks RCP A00=rcp_dynamic_cast(BlockOp->GetBlock(0,0)); RCP A11=rcp_dynamic_cast(BlockOp->GetBlock(1,1)); // Make H(Div) preconditioner A00 Teuchos::ParameterList ListHdiv, List_Coarse; List_Coarse.set("PDE equations",3); List_Coarse.set("x-coordinates",Nx.Values()); List_Coarse.set("y-coordinates",Ny.Values()); List_Coarse.set("z-coordinates",Nz.Values()); List_Coarse.set("ML output",10); List_Coarse.set("smoother: type","Chebyshev"); List_Coarse.set("smoother: sweeps",2); List_Coarse.set("coarse: max size",1000); Teuchos::ParameterList List11,List11c,List22,List22c; ML_Epetra::UpdateList(List_Coarse,List11,true); List11.set("smoother: type","do-nothing"); List11.set("smoother: sweeps",0); ML_Epetra::UpdateList(List_Coarse,List22,true); ML_Epetra::UpdateList(List_Coarse,List11c,true); #ifdef HAVE_ML_ZOLTAN List11c.set("aggregation: type","Uncoupled"); List11c.set("repartition: enable",1); List11c.set("repartition: Zoltan dimensions",3); List11c.set("repartition: max min ratio",1.4); List11c.set("repartition: min per proc",1000); #endif ML_Epetra::UpdateList(List_Coarse,List22c,true); #ifdef HAVE_ML_ZOLTAN List22c.set("aggregation: type","Uncoupled"); List22c.set("repartition: enable",1); List22c.set("repartition: Zoltan dimensions",3); List22c.set("repartition: max min ratio",1.4); List22c.set("repartition: min per proc",1000); #endif List11.set("face matrix free: coarse",List11c); List22.set("edge matrix free: coarse",List22c); ListHdiv.setName("graddiv list"); ListHdiv.set("graddiv: 11list",List11); ListHdiv.set("graddiv: 22list",List22); ListHdiv.set("smoother: sweeps",2); ListHdiv.set("ML output",10); ListHdiv.set("smoother: type","Chebyshev"); ML_Epetra::SetDefaultsGradDiv(ListHdiv,false); RCP Prec0=rcp(new GradDivPreconditioner(*A00,FaceNode,DCurl,DGrad,StiffG,ListHdiv)); // Make H(Grad) preconditioner A11 Teuchos::ParameterList ListHgrad; ML_Epetra::SetDefaults("SA",ListHgrad); ListHgrad.set("smoother: sweeps",2); ListHgrad.set("smoother: type","Chebyshev"); ListHgrad.set("coarse: type","Amesos-KLU"); ListHgrad.set("coarse: max size",1000); ListHgrad.set("ML output",10); ListHgrad.set("ML label","Poisson solver"); ListHgrad.set("aggregation: threshold",0.01); #ifdef HAVE_ML_ZOLTAN ListHgrad.set("aggregation: type","Uncoupled"); ListHgrad.set("repartition: enable",1); ListHgrad.set("repartition: Zoltan dimensions",3); ListHgrad.set("repartition: max min ratio",1.4); ListHgrad.set("repartition: min per proc",1000); ListHgrad.set("x-coordinates",Nx.Values()); ListHgrad.set("y-coordinates",Ny.Values()); ListHgrad.set("z-coordinates",Nz.Values()); #endif RCP Prec1=rcp(new MultiLevelPreconditioner(*A11,ListHgrad)); // Build the linear ops with the Apply/ApplyInverse switcheroo RCP invD0=epetraLinearOp(Prec0, NOTRANS,EPETRA_OP_APPLY_APPLY_INVERSE); RCP invD1=epetraLinearOp(Prec1, NOTRANS,EPETRA_OP_APPLY_APPLY_INVERSE); // Build the full preconditioner JacobiPreconditionerFactory precFact(rcp(new Teko::StaticInvDiagStrategy(invD0,invD1))); Teko::Epetra::EpetraBlockPreconditioner FullPrec(rcpFromRef(precFact)); FullPrec.buildPreconditioner(BlockOp); //set up linear system Epetra_FEVector xx(globalMapJoint); xx.PutScalar(0.0); Epetra_LinearProblem Problem(&jointMatrix,&xx,&jointVector); Epetra_Time Time2(jointMatrix.Comm()); AztecOO solver(Problem); // tell AztecOO to use this preconditioner, then solve solver.SetPrecOperator(&FullPrec); //solver.SetAztecOption(AZ_solver, AZ_gmres); solver.SetAztecOption(AZ_solver, AZ_cg); solver.SetAztecOption(AZ_output, 10); //solve linear system solver.Iterate(1000, 1e-8); Epetra_FEVector globalSoln(globalMapJoint); globalSoln = xx; //or //Epetra_MultiVector globalSoln = *lhs; // note: solution map will be same globalJointMap! // Get exact solution at nodes, keep the order of soln same as in jointMap double * exactSoln = new double [numOwnedNodes]; for (int nn = 0; nn(numFacesGlobal+numNodesGlobal), static_cast(numFacesGlobal+numNodesGlobal), 0, Comm); Epetra_Import solnImporter(solnMap, globalMapJoint); Epetra_FEVector uCoeff(solnMap); uCoeff.Import(globalSoln, solnImporter, Insert); double L2err_u = 0.0; double HDiverr_u = 0.0; double Linferr_u = 0.0; double L2errTot_u = 0.0; double HDiverrTot_u = 0.0; double LinferrTot_u = 0.0; double L2err_phi = 0.0; double H1err_phi = 0.0; double L2errTot_phi = 0.0; double H1errTot_phi = 0.0; // Get cubature points and weights for error calc (may be different from previous) DefaultCubatureFactory cubFactoryErr; int cubDegErr = 3; Teuchos::RCP > hexCubErr = cubFactoryErr.create(hex_8, cubDegErr); int cubDimErr = hexCubErr->getDimension(); int numCubPointsErr = hexCubErr->getNumPoints(); FieldContainer cubPointsErr(numCubPointsErr, cubDimErr); FieldContainer cubWeightsErr(numCubPointsErr); hexCubErr->getCubature(cubPointsErr, cubWeightsErr); FieldContainer physCubPointsE(numCells,numCubPointsErr, cubDimErr); // Containers for Jacobian FieldContainer hexJacobianE(numCells, numCubPointsErr, spaceDim, spaceDim); FieldContainer hexJacobInvE(numCells, numCubPointsErr, spaceDim, spaceDim); FieldContainer hexJacobDetE(numCells, numCubPointsErr); FieldContainer weightedMeasureE(numCells, numCubPointsErr); // Evaluate basis values and divs at cubature points FieldContainer uhDVals(numFieldsD, numCubPointsErr, spaceDim); FieldContainer uhDValsTrans(numCells,numFieldsD, numCubPointsErr, spaceDim); FieldContainer uhDivs(numFieldsD, numCubPointsErr); FieldContainer uhDivsTrans(numCells, numFieldsD, numCubPointsErr); hexHDivBasis.getValues(uhDVals, cubPointsErr, OPERATOR_VALUE); hexHDivBasis.getValues(uhDivs, cubPointsErr, OPERATOR_DIV); FieldContainer phihGVals(numFieldsG, numCubPointsErr); FieldContainer phihGValsTrans(numCells,numFieldsG, numCubPointsErr); FieldContainer phihGrads(numFieldsG, numCubPointsErr, spaceDim); FieldContainer phihGradsTrans(numCells, numFieldsG, numCubPointsErr, spaceDim); hexHGradBasis.getValues(phihGVals, cubPointsErr, OPERATOR_VALUE); hexHGradBasis.getValues(phihGrads, cubPointsErr, OPERATOR_GRAD); // Loop over elements for (int k=0; k numNodesPerFace) indf=0; if (elemToNode(k,refFaceToNode(j,0))==faceToNode(elemToFace(k,j),i) && elemToNode(k,refFaceToNode(j,1))==faceToNode(elemToFace(k,j),indf)) hexFaceSigns(0,j) = 1.0; } if (!faceIsOwned[elemToFace(k,j)]){ hexFaceSigns(0,j)=-1.0*hexFaceSigns(0,j); } } // compute cell Jacobians, their inverses and their determinants CellTools::setJacobian(hexJacobianE, cubPointsErr, hexNodes, hex_8); CellTools::setJacobianInv(hexJacobInvE, hexJacobianE ); CellTools::setJacobianDet(hexJacobDetE, hexJacobianE ); // transform integration points to physical points CellTools::mapToPhysicalFrame(physCubPointsE, cubPointsErr, hexNodes, hex_8); // transform basis values to physical coordinates fst::HDIVtransformVALUE(uhDValsTrans, hexJacobianE, hexJacobDetE, uhDVals); fst::HDIVtransformDIV(uhDivsTrans, hexJacobDetE, uhDivs); // transform basis values to physical coordinates fst::HGRADtransformVALUE(phihGValsTrans, phihGVals); fst::HGRADtransformGRAD(phihGradsTrans, hexJacobInvE, phihGrads); // compute weighted measure fst::computeCellMeasure(weightedMeasureE, hexJacobDetE, cubWeightsErr); // loop over cubature points for (int nPt = 0; nPt < numCubPointsErr; nPt++){ // get exact solution and divs double x = physCubPointsE(0,nPt,0); double y = physCubPointsE(0,nPt,1); double z = physCubPointsE(0,nPt,2); evalu(uExact1, uExact2, uExact3, x, y, z); divuExact = evalDivu(x, y, z); phiExact = evalPhi(x, y, z); evalGradPhi(gradphiExact1, gradphiExact2, gradphiExact3, x, y, z); // calculate approximate solution and divs double uApprox1 = 0.0; double uApprox2 = 0.0; double uApprox3 = 0.0; double divuApprox = 0.0; double phiApprox = 0.; double gradphiApprox1 = 0.; double gradphiApprox2 = 0.; double gradphiApprox3 = 0.; for (int i = 0; i < numFieldsD; i++){ int rowIndex = globalFaceIds[elemToFace(k,i)]; //was rowIndex double uh1 = uCoeff.Values()[rowIndex]; uApprox1 += uh1*uhDValsTrans(0,i,nPt,0)*hexFaceSigns(0,i); uApprox2 += uh1*uhDValsTrans(0,i,nPt,1)*hexFaceSigns(0,i); uApprox3 += uh1*uhDValsTrans(0,i,nPt,2)*hexFaceSigns(0,i); divuApprox += uh1*uhDivsTrans(0,i,nPt)*hexFaceSigns(0,i); } // evaluate the error at cubature points Linferr_u = max(Linferr_u, abs(uExact1 - uApprox1)); Linferr_u = max(Linferr_u, abs(uExact2 - uApprox2)); Linferr_u = max(Linferr_u, abs(uExact3 - uApprox3)); L2errElem_u+=(uExact1 - uApprox1)*(uExact1 - uApprox1)*weightedMeasureE(0,nPt); L2errElem_u+=(uExact2 - uApprox2)*(uExact2 - uApprox2)*weightedMeasureE(0,nPt); L2errElem_u+=(uExact3 - uApprox3)*(uExact3 - uApprox3)*weightedMeasureE(0,nPt); HDiverrElem_u+=((divuExact - divuApprox)*(divuExact - divuApprox)) *weightedMeasureE(0,nPt); L2err_u+=L2errElem_u; HDiverr_u+=HDiverrElem_u; for (int i = 0; i < numFieldsG; i++){ int rowIndex = globalNodeIds[elemToNode(k,i)]; double phih1 = uCoeff.Values()[rowIndex+numFacesGlobal]; phiApprox += phih1*phihGValsTrans(0,i,nPt); gradphiApprox1 += phih1*phihGradsTrans(0,i,nPt,0); gradphiApprox2 += phih1*phihGradsTrans(0,i,nPt,1); gradphiApprox3 += phih1*phihGradsTrans(0,i,nPt,2); } // evaluate the error at cubature points //GET IT! later //Linferr_phi = max(Linferr_phi, abs(phiExact - phiApprox)); L2errElem_phi+=(phiExact - phiApprox)*(phiExact - phiApprox)*weightedMeasureE(0,nPt); H1errElem_phi+=((gradphiExact1 - gradphiApprox1)*(gradphiExact1 - gradphiApprox1)) *weightedMeasureE(0,nPt); H1errElem_phi+=((gradphiExact2 - gradphiApprox2)*(gradphiExact2 - gradphiApprox2)) *weightedMeasureE(0,nPt); H1errElem_phi+=((gradphiExact3 - gradphiApprox3)*(gradphiExact3 - gradphiApprox3)) *weightedMeasureE(0,nPt); } L2err_phi+=L2errElem_phi; H1err_phi+=H1errElem_phi; } // sum over all processors Comm.SumAll(&L2err_u,&L2errTot_u,1); Comm.SumAll(&HDiverr_u,&HDiverrTot_u,1); Comm.MaxAll(&Linferr_u,&LinferrTot_u,1); Comm.SumAll(&L2err_phi,&L2errTot_phi,1); Comm.SumAll(&H1err_phi,&H1errTot_phi,1); if (MyPID == 0) { std::cout << "\n" << "L2 Error U: " << sqrt(L2errTot_u) <<"\n"; std::cout << "HDiv Error U: " << sqrt(HDiverrTot_u) <<"\n"; std::cout << "LInf Error U: " << LinferrTot_u <<"\n\n"; std::cout << "\n" << "L2 Error: Phi " << sqrt(L2errTot_phi) <<"\n"; std::cout << "H1 Error: Phi " << sqrt(H1errTot_phi) <<"\n"; } // delete mesh Delete_Pamgen_Mesh(); //clean up for(long long b = 0; b < numElemBlk; b++){ delete [] elmt_node_linkage[b]; delete [] element_types[b]; } delete [] block_ids; delete [] nodes_per_element; delete [] element_attributes; delete [] element_types; delete [] elmt_node_linkage; delete [] elements; delete [] nodeCoordx; delete [] nodeCoordy; delete [] nodeCoordz; delete [] globalNodeIds; delete [] nodeIsOwned; delete [] globalEdgeIds; delete [] edgeIsOwned; delete [] globalFaceIds; delete [] faceIsOwned; delete [] ownedGIDs; delete [] exactSoln; delete [] ownedFaceIds; delete [] ownedEdgeIds; delete [] ownedFaceNodeIds; if(num_node_comm_maps > 0){ delete [] node_comm_proc_ids; delete [] node_cmap_node_cnts; delete [] node_cmap_ids; for(long long i=0;i void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z) { Scalar kappa=evalKappa(x,y,z); material[0][0] = kappa; material[0][1] = 0.; material[0][2] = 0.; // material[1][0] = 0.; material[1][1] = kappa; material[1][2] = 0.; // material[2][0] = 0.; material[2][1] = 0.; material[2][2] = kappa; } template void materialTensorInv(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z) { Scalar kappa=evalKappa(x,y,z); kappa=1./kappa; material[0][0] = kappa; material[0][1] = 0.; material[0][2] = 0.; // material[1][0] = 0.; material[1][1] = kappa; material[1][2] = 0.; // material[2][0] = 0.; material[2][1] = 0.; material[2][2] = kappa; } template void evaluateMaterialTensor(ArrayOut & matTensorValues, const ArrayIn & evaluationPoints){ //int numWorksetCells = evaluationPoints.dimension(0); int numPoints = evaluationPoints.dimension(1); int spaceDim = evaluationPoints.dimension(2); double material[3][3]; //for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ double x = evaluationPoints(0, pt, 0); double y = evaluationPoints(0, pt, 1); double z = evaluationPoints(0, pt, 2); materialTensor(material, x, y, z); for(int row = 0; row < spaceDim; row++){ for(int col = 0; col < spaceDim; col++){ matTensorValues(0, pt, row, col) = material[row][col]; } } } //} } template void evaluateMaterialTensorInv(ArrayOut & matTensorValues, const ArrayIn & evaluationPoints){ //int numWorksetCells = evaluationPoints.dimension(0); int numPoints = evaluationPoints.dimension(1); int spaceDim = evaluationPoints.dimension(2); double material[3][3]; //for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ double x = evaluationPoints(0, pt, 0); double y = evaluationPoints(0, pt, 1); double z = evaluationPoints(0, pt, 2); materialTensorInv(material, x, y, z); for(int row = 0; row < spaceDim; row++){ for(int col = 0; col < spaceDim; col++){ matTensorValues(0, pt, row, col) = material[row][col]; } } } //} } template Scalar evalKappa(const Scalar& x, const Scalar& y, const Scalar& z){ Scalar kappa=0; #ifdef SMOOTH kappa=1.; #endif #ifdef PATCH if((y>=0)&&(y<=.2)) kappa=16; if((y>.2)&&(y<=.4)) kappa=6; if((y>.4)&&(y<=.6)) kappa=1; if((y>.6)&&(y<=.8)) kappa=10; if((y>.8)&&(y<=1.0)) kappa=2; #endif return kappa; } template Scalar evalF(const Scalar& x, const Scalar& y, const Scalar& z){ //patch test #ifdef PATCH return 1 - x; #endif //smooth solution #ifdef SMOOTH Scalar a; double pi = 3.1415926535897932384626433; a = 2*pi*pi*cos(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); a += 2*pi*pi*cos(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z); a += 2*pi*pi*cos(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x); a += -sin(pi*x)*sin(pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); return a; #endif } template Scalar evalPhi(const Scalar& x, const Scalar& y, const Scalar& z){ //patch test #ifdef PATCH return 1 - x; #endif //smooth solution #ifdef SMOOTH double pi = 3.1415926535897932384626433; return -sin(pi*x)*sin(pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); #endif } // Calculates divergence of exact solution u template Scalar evalDivu(Scalar & x, Scalar & y, Scalar & z){ //patch test #ifdef PATCH return 0.0; #endif //smooth solution #ifdef SMOOTH Scalar a; double pi = 3.1415926535897932384626433; a = 2*pi*pi*cos(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); a += 2*pi*pi*cos(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z); a += 2*pi*pi*cos(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x); return a; #endif } // Calculates value of exact solution u template int evalu(Scalar & uExact0, Scalar & uExact1, Scalar & uExact2, Scalar & x, Scalar & y, Scalar & z){ //patch test #ifdef PATCH uExact0 = evalKappa(x,y,z); uExact1 = 0.0; uExact2 = 0.0; #endif #ifdef SMOOTH //smooth solution double pi = 3.1415926535897932384626433; uExact0 = pi*sin(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); uExact1 = pi*sin(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z); uExact2 = pi*sin(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x); #endif return 0; } // Calculates value of exact solution grad phi template int evalGradPhi(Scalar & phiGExact0, Scalar & phiGExact1, Scalar & phiGExact2, Scalar & x, Scalar & y, Scalar & z){ //patch test #ifdef PATCH phiGExact0 = -1.; phiGExact1 = 0.0; phiGExact2 = 0.0; #endif #ifdef SMOOTH //smooth solution double pi = 3.1415926535897932384626433; phiGExact0 = -pi*sin(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z); phiGExact1 = -pi*sin(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z); phiGExact2 = -pi*sin(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x); #endif return 0; }