// @HEADER // ***************************************************************************** // Trilinos: An Object-Oriented Solver Framework // // Copyright 2001-2024 NTESS and the Trilinos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** \file example_Poisson.cpp \brief Example solution of a Poisson equation on a quad mesh using nodal (Hgrad) elements. This example uses the following Trilinos packages: \li Pamgen to generate a Quad mesh. \li Sacado to form the source term from user-specified manufactured solution. \li Intrepid to build the discretization matrix and right-hand side. \li Tpetra to handle the global matrix and vector. \li Isorropia to partition the matrix. (Optional) \li ML to solve the linear system. \verbatim Poisson system: div A grad u = f in Omega u = g on Gamma where A is a symmetric, positive definite material tensor f is a given source term Corresponding discrete linear system for nodal coefficients(x): Kx = b K - HGrad stiffness matrix b - right hand side vector \endverbatim \author Created by P. Bochev, D. Ridzal, K. Peterson, D. Hensinger, C. Siefert. \remark Usage: \code ./TrilinosCouplings_examples_scaling_example_Poisson.exe \endcode \remark Example driver requires input file named Poisson2D.xml with Pamgen formatted mesh description and settings for Isorropia (a version is included in the Trilinos repository with this driver). \remark The exact solution (u) and material tensor (A) are set in the functions "exactSolution" and "materialTensor" and may be modified by the user. */ /*** Uncomment if you would like output data for plotting ***/ //#define DUMP_DATA /**************************************************************/ /* Includes */ /**************************************************************/ #include // TrilinosCouplings includes #include "TrilinosCouplings_config.h" #include "TrilinosCouplings_Pamgen_Utils.hpp" #include "TrilinosCouplings_IntrepidPoissonExampleHelpers.hpp" // Intrepid includes #include "Intrepid_FunctionSpaceTools.hpp" #include "Intrepid_CellTools.hpp" #include "Intrepid_ArrayTools.hpp" #include "Intrepid_HGRAD_QUAD_Cn_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp" #include "Intrepid_RealSpaceTools.hpp" #include "Intrepid_DefaultCubatureFactory.hpp" #include "Intrepid_Utils.hpp" // Intrepid2 Includes #include "Kokkos_DynRankView.hpp" // Tpetra includes #include #include #include // Teuchos includes #include "Teuchos_oblackholestream.hpp" #include "Teuchos_RCP.hpp" #include "Teuchos_BLAS.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" // Shards includes #include "Shards_CellTopology.hpp" // Pamgen includes #include "create_inline_mesh.h" #include "pamgen_im_exodusII_l.h" #include "pamgen_im_ne_nemesisI_l.h" #include "pamgen_extras.h" // MueLu Includes # include "MueLu.hpp" # include "MueLu_ParameterListInterpreter.hpp" # include "MueLu_TpetraOperator.hpp" # include "MueLu_Utilities.hpp" # include "MueLu_HierarchyManager.hpp" # include "MueLu_FactoryManagerBase.hpp" # include "MueLu_CreateTpetraPreconditioner.hpp" // MueLu/Avatar Includes #ifdef HAVE_TRILINOSCOUPLINGS_AVATAR # include "MueLu_AvatarInterface.hpp" #endif #ifdef HAVE_INTREPID_KOKKOS #include "Sacado.hpp" #else // Sacado includes #include "Sacado_No_Kokkos.hpp" #endif #include #include #include #include #include #include // => This header defines Belos::XpetraOp #include // => This header defines Belos::MueLuOp using namespace std; using namespace Intrepid; using Teuchos::RCP; using Teuchos::ArrayRCP; using Teuchos::Array; using Teuchos::ArrayView; using Teuchos::rcp; using Teuchos::rcpFromRef; using Teuchos::TimeMonitor; using Teuchos::ParameterList; // for debugging #define zwrap(x) (std::abs(x)<1e-10 ? 0.0 : x) // Statistics for machine learning const int NUM_STATISTICS = 8; std::vector local_stat_max(NUM_STATISTICS); std::vector local_stat_min(NUM_STATISTICS); std::vector local_stat_sum(NUM_STATISTICS); // Disable the full set of problem statistics const bool use_new_problem_stats = true; 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 double distance2(const FC & coord, int n1, int n2) { double dist = 0.0; for(int i=0; i ScalarTraits; using local_ordinal_type = Tpetra::Map<>::local_ordinal_type; using global_ordinal_type = Tpetra::Map<>::global_ordinal_type; typedef Tpetra::KokkosClassic::DefaultNode::DefaultNodeType NO; typedef Sacado::Fad::SFad Fad2; //# ind. vars fixed at 2 typedef Intrepid::FunctionSpaceTools IntrepidFSTools; typedef Intrepid::RealSpaceTools IntrepidRSTools; typedef Intrepid::CellTools IntrepidCTools; typedef Tpetra::Operator operator_type; typedef Tpetra::CrsGraph crs_graph_type; typedef Tpetra::CrsMatrix crs_matrix_type; typedef Tpetra::Vector vector_type; typedef Tpetra::MultiVector multivector_type; typedef Tpetra::Map driver_map_type; Tpetra::global_size_t INVALID_GO = Teuchos::OrdinalTraits::invalid(); Tpetra::global_size_t INVALID_LO = Teuchos::OrdinalTraits::invalid(); typedef Xpetra::Matrix xpetra_crs_matrix_type; typedef Xpetra::MultiVector xpetra_multivector_type; typedef Belos::LinearProblem linear_problem_type; typedef MueLu::TpetraOperator muelu_tpetra_operator; //typedef Tpetra::MatrixMarket::Writer writer_type; #define TC_sumAll(rcpComm, in, out) \ Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out)) #define TC_minAll(rcpComm, in, out) \ Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MIN, in, Teuchos::outArg(out)) #define TC_maxAll(rcpComm, in, out) \ Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MAX, in, Teuchos::outArg(out)) double myDistance2(const multivector_type &v, int i0, int i1) { const size_t numVectors = v.getNumVectors(); double distance = 0.0; for (size_t j=0; j & P1_elemToNode, const FieldContainer & P1_nodeCoord, const FieldContainer & P1_edgeCoord, const FieldContainer & P1_elemToEdge, const FieldContainer & P1_elemToEdgeOrient, const FieldContainer & P1_nodeOnBoundary, FieldContainer & Pn_elemToNode, FieldContainer & Pn_nodeCoord,FieldContainer & Pn_nodeOnBoundary, std::vector &Pn_edgeNodes, std::vector &Pn_cellNodes); void GenerateEdgeEnumeration(const FieldContainer & elemToNode, const FieldContainer & nodeCoord, FieldContainer & elemToEdge, FieldContainer & elemToEdgeOrient, FieldContainer & edgeToNode, FieldContainer & edgeCoord); void CreateP1MeshFromPnMesh(const int degree, const FieldContainer & P2_elemToNode, FieldContainer &aux_P1_elemToNode); void CreateLinearSystem(int numWorkSets, int desiredWorksetSize, FieldContainer const &elemToNode, FieldContainer const &nodeCoord, FieldContainer const &cubPoints, FieldContainer const &cubWeights, RCP > > &myBasis_rcp, FieldContainer const &HGBGrads, FieldContainer const &HGBValues, std::vector const &globalNodeIds, crs_matrix_type &StiffMatrix, RCP &rhsVector, std::string &msg ); void GenerateIdentityCoarsening_pn_to_p1(const FieldContainer & P2_elemToNode, //const FieldContainer & P1_elemToNode, RCP const & P1_map_aux, RCP const &P2_map, RCP & Interpolation, RCP & Restriction); //GenerateIdentityCoarsening_pn_to_p1(elemToNode, StiffMatrix_aux.getDomainMap(), StiffMatrix.getRangeMap(), P_identity, R_identity); void Apply_Dirichlet_BCs(std::vector &BCNodes, crs_matrix_type & A, multivector_type & x, multivector_type & b, const multivector_type & soln); /**********************************************************************************/ /***************** FUNCTION DECLARATION FOR ML PRECONDITIONER *********************/ /**********************************************************************************/ /** \brief ML Preconditioner \param ProblemType [in] problem type \param MLList [in] ML parameter list \param A [in] discrete operator matrix \param nCoord [in] Nodal Coordinates \param xexact [in] exact solution \param b [in] right-hand-side vector \param maxits [in] max iterations \param tol [in] solver tolerance \param uh [in/out] solution vector \param TotalErrorResidual [out] error residual \param TotalErrorExactSol [out] error in uh */ int TestMultiLevelPreconditionerLaplace(char ProblemType[], ParameterList & AMGList, RCP const & A, RCP & nCoord, RCP const & xexact, RCP & b, int maxits, double tol, RCP & uh, double & TotalErrorResidual, double & TotalErrorExactSol, std::string &amgType, std::string &solveType); /**********************************************************************************/ /******** FUNCTION DECLARATIONS FOR EXACT SOLUTION AND SOURCE TERMS ***************/ /**********************************************************************************/ /** \brief User-defined exact solution. \param x [in] x-coordinate of the evaluation point \param y [in] y-coordinate of the evaluation point \return Value of the exact solution at (x,y) */ template const Scalar exactSolution(const Scalar& x, const Scalar& y); /** \brief User-defined material tensor. \param material [out] 3 x 3 material tensor evaluated at (x,y) \param x [in] x-coordinate of the evaluation point \param y [in] y-coordinate of the evaluation point \warning Symmetric and positive definite tensor is required for every (x,y). */ template void materialTensor(Scalar material[][2], const Scalar& x, const Scalar& y); /** \brief Computes gradient of the exact solution. Requires user-defined exact solution. \param gradExact [out] gradient of the exact solution evaluated at (x,y) \param x [in] x-coordinate of the evaluation point \param y [in] y-coordinate of the evaluation point */ template void exactSolutionGrad(Scalar gradExact[2], const Scalar& x, const Scalar& y); /** \brief Computes source term: f = -div(A.grad u). Requires user-defined exact solution and material tensor. \param x [in] x-coordinate of the evaluation point \param y [in] y-coordinate of the evaluation point \return Source term corresponding to the user-defined exact solution evaluated at (x,y) */ template const Scalar sourceTerm(Scalar& x, Scalar& y); /** \brief Computation of the material tensor at array of points in physical space. \param worksetMaterialValues [out] Rank-2, 3 or 4 array with dimensions (C,P), (C,P,D) or (C,P,D,D) with the values of the material tensor \param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame */ template void evaluateMaterialTensor(ArrayOut & worksetMaterialValues, const ArrayIn & evaluationPoints); /** \brief Computation of the source term at array of points in physical space. \param sourceTermValues [out] Rank-2 (C,P) array with the values of the source term \param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame */ template void evaluateSourceTerm(ArrayOut & sourceTermValues, const ArrayIn & evaluationPoints); /** \brief Computation of the exact solution at array of points in physical space. \param exactSolutionValues [out] Rank-2 (C,P) array with the values of the exact solution \param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame */ template void evaluateExactSolution(ArrayOut & exactSolutionValues, const ArrayIn & evaluationPoints); /** \brief Computation of the gradient of the exact solution at array of points in physical space. \param exactSolutionGradValues [out] Rank-3 (C,P,D) array with the values of the gradient of the exact solution \param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame */ template void evaluateExactSolutionGrad(ArrayOut & exactSolutionGradValues, const ArrayIn & evaluationPoints); // Copy field containers template void CopyFieldContainer2D(const FC1 & c1, FC2 & c2) { Kokkos::resize(c2,c1.dimension(0),c1.dimension(1)); auto c2_h = Kokkos::create_mirror_view(c2); for(size_t i=0; i<(size_t)c1.dimension(0); i++) for(size_t j=0; j<(size_t)c1.dimension(1); j++) c2_h(i,j) = c1(i,j); Kokkos::deep_copy(c2, c2_h); } /**********************************************************************************/ /******************************** MAIN ********************************************/ /**********************************************************************************/ int main(int argc, char *argv[]) { int error = 0; int numProcs=1; int rank=0; Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); RCP > Comm = Teuchos::DefaultComm::getComm(); int MyPID = Comm->getRank(); Teuchos::CommandLineProcessor clp(false); Teuchos::ParameterList problemStatistics; RCP tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Pamgen Setup"))); std::string optMeshFile = "Poisson2D.xml"; clp.setOption("mesh", &optMeshFile, "xml file with description of Pamgen mesh"); std::string optSolverFile = ""; clp.setOption("solver", &optSolverFile, "xml file containing linear solver options"); bool optPrintTimings = false; clp.setOption("timings", "notimings", &optPrintTimings, "print timer summary"); // If matrixFilename is nonempty, dump the matrix to that file // in MatrixMarket format. std::string matrixFilename; clp.setOption ("matrixFilename", &matrixFilename, "If nonempty, dump the " "generated matrix to that file in MatrixMarket format."); // If coordsFilename is nonempty, dump the rhs to that file // in MatrixMarket format. std::string rhsFilename; clp.setOption ("rhsFilename", &rhsFilename, "If nonempty, dump the " "generated rhs to that file in MatrixMarket format."); // If coordsFilename is nonempty, dump the coords to that file // in MatrixMarket format. std::string coordsFilename; clp.setOption ("coordsFilename", &coordsFilename, "If nonempty, dump the " "generated coordinates to that file in MatrixMarket format."); // Random number seed int randomSeed=24601; clp.setOption ("seed", &randomSeed, "Random Seed."); // Material diffusion strength and rotation (in 3D) // We'll get the 2D version of this later std::vector diff_rotation_angle {0.0, 0.0, 0.0}; std::vector diff_strength {1.0, 1.0, 1.0}; for(int i=0; i<3; i++) { char letter[4] = "xyz"; char str1[80], str2[80]; // Rotation sprintf(str1,"rot_%c_angle",letter[i]); sprintf(str2,"Rotation around %c axis, in degrees",letter[i]); clp.setOption(str1,&diff_rotation_angle[i],str2); // Strength sprintf(str1,"strength_%c",letter[i]); sprintf(str2,"Strength of pre-rotation %c-diffusion",letter[i]); clp.setOption(str1,&diff_strength[i],str2); } switch (clp.parse(argc, argv)) { case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; case Teuchos::CommandLineProcessor::PARSE_ERROR: case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; } // Initialize RNG srand(randomSeed); if (MyPID == 0){ std::cout \ << "===============================================================================\n" \ << "| |\n" \ << "| Example: Solve Poisson Equation on Hexahedral Mesh |\n" \ << "| |\n" \ << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov), |\n" \ << "| Denis Ridzal (dridzal@sandia.gov), |\n" \ << "| Kara Peterson (kjpeter@sandia.gov). |\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" \ << "| Isorropia's website: http://trilinos.sandia.gov/packages/isorropia |\n" \ << "| Trilinos website: http://trilinos.sandia.gov |\n" \ << "| |\n" \ << "===============================================================================\n"; } // Diffusion Tensor ::TrilinosCouplings::IntrepidPoissonExample::setDiffusionRotationAndStrength(diff_rotation_angle, diff_strength); if(MyPID == 0) { const std::vector & A = ::TrilinosCouplings::IntrepidPoissonExample::getDiffusionMatrix2D(); std::cout<<"[ "<(inputMeshList,"meshInput"); int degree = inputMeshList.get("degree",2); // Get Isorropia and Zoltan parameters. ParameterList iso_paramlist = inputMeshList.sublist ("Isorropia Input") ; if (MyPID == 0) { std::cout << "Isorropia/Zoltan parameters" << std::endl; iso_paramlist.print(std::cout,2,true,true); } /**********************************************************************************/ /***************************** GET CELL TOPOLOGY **********************************/ /**********************************************************************************/ // Get cell topology for base hexahedron shards::CellTopology P1_cellType(shards::getCellTopologyData >() ); shards::CellTopology Pn_cellType(shards::getCellTopologyData >() ); assert(P1_cellType.getDimension() == Pn_cellType.getDimension()); // Get dimensions int P1_numNodesPerElem = P1_cellType.getNodeCount(); int P1_numEdgesPerElem = P1_cellType.getEdgeCount(); int P1_numNodesPerEdge = 2; // for any rational universe int spaceDim = P1_cellType.getDimension(); int dim = 2; // Build reference element edge to node map FieldContainer refEdgeToNode(P1_numEdgesPerElem, P1_numNodesPerEdge); for (int i=0; i P1_elemToNode(numElems,P1_numNodesPerElem); for(long long b = 0; b < numElemBlk; b++){ for(long long el = 0; el < elements[b]; el++){ for (int j=0; j P1_nodeCoord(numNodes,dim); double * nodeCoordx = new double [numNodes]; double * nodeCoordy = new double [numNodes]; im_ex_get_coord_l(id,nodeCoordx,nodeCoordy,0); 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]; } delete [] elem_cmap_ids; delete [] elem_cmap_elem_cnts; } tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Global Node Nums"))); //Calculate global node ids long long * P1_globalNodeIds = new long long[numNodes]; bool * P1_nodeIsOwned = new bool[numNodes]; calc_global_node_ids(P1_globalNodeIds, P1_nodeIsOwned, numNodes, num_node_comm_maps, node_cmap_node_cnts, node_comm_proc_ids, comm_node_ids, rank); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Boundary Conds"))); // Container indicating whether a node is on the boundary (1-yes 0-no) FieldContainer P1_nodeOnBoundary(numNodes); // Get boundary (side set) information long long * sideSetIds = new long long [numSideSets]; long long numSidesInSet; long long numDFinSet; 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 P1_elemToEdge(numElems,4);// Because quads FieldContainer P1_elemToEdgeOrient(numElems,4); FieldContainer P1_edgeCoord(1,dim);//will be resized FieldContainer P1_edgeToNode(1,2);//will be resized GenerateEdgeEnumeration(P1_elemToNode, P1_nodeCoord, P1_elemToEdge,P1_elemToEdgeOrient,P1_edgeToNode,P1_edgeCoord); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Mesh Statistics"))); /**********************************************************************************/ /****************************** STATISTICS (Part I) *******************************/ /**********************************************************************************/ // Statistics: Compute max / min of sigma parameter, mesh information // Put the current statistics into their own functions so this looks less awful double edge_length_max = 0; double edge_length_min = 0; double diag_length_max = 0; double diag_length_min = 0; double maxmin_ratio = 0; double diag_ratio = 0; double stretch = 0; double taper = 0; double skew = 0; double dist1 = 0.0; double dist2 = 0.0; double dist3 = 0.0; double dist4 = 0.0; int edge = P1_elemToEdge(0, 0); int node1 = P1_edgeToNode(edge, 0); int node2 = P1_edgeToNode(edge, 1); double dist = 0; static const int NUM_NODE_PAIRS = 2; int diag_nodes1[] = {0, 1}; int diag_nodes2[] = {2, 3}; double x0 = 0.0; double x1 = 0.0; double x2 = 0.0; double x3 = 0.0; double y0 = 0.0; double y1 = 0.0; double y2 = 0.0; double y3 = 0.0; double e3 = 0.0; double f3 = 0.0; // Intialize for(int i=0; i::max(); local_stat_sum[i] = 0.0; } for(int i=0; i elemToNode(numElems,Pn_numNodesperElem); FieldContainer nodeCoord(Pn_numNodes,dim); FieldContainer nodeOnBoundary(Pn_numNodes); std::vector Pn_edgeNodes; std::vector Pn_cellNodes; PromoteMesh_Pn_Kirby(degree,POINTTYPE_EQUISPACED,P1_elemToNode,P1_nodeCoord,P1_edgeCoord,P1_elemToEdge,P1_elemToEdgeOrient, P1_nodeOnBoundary, elemToNode, nodeCoord, nodeOnBoundary, Pn_edgeNodes, Pn_cellNodes); long long numElems_aux = numElems*degree*degree; //degree^2 P1 elements per Pn element in auxiliary mesh FieldContainer aux_P1_elemToNode(numElems_aux,P1_numNodesPerElem); //4 P1 elements per Pn element CreateP1MeshFromPnMesh(degree, elemToNode, aux_P1_elemToNode); // Only works in serial std::vectorPn_nodeIsOwned(Pn_numNodes,true); std::vectorPn_globalNodeIds(Pn_numNodes); for(int i=0; i(i); std::vector Pn_nodeCoordx(Pn_numNodes); std::vector Pn_nodeCoordy(Pn_numNodes); for (int i=0; i cubFactory; int cubDegree = 2*degree; RCP > myCub = cubFactory.create(Pn_cellType, cubDegree); int cubDim = myCub->getDimension(); int numCubPoints = myCub->getNumPoints(); FieldContainer cubPoints(numCubPoints, cubDim); FieldContainer cubWeights(numCubPoints); myCub->getCubature(cubPoints, cubWeights); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Getting cubature"))); /**********************************************************************************/ /*********************************** GET BASIS ************************************/ /**********************************************************************************/ // Define basis Basis_HGRAD_QUAD_Cn_FEM > myHGradBasis(degree,POINTTYPE_EQUISPACED); RCP > > myHGradBasis_rcp(&myHGradBasis, false); RCP > > myHGradBasisWithDofCoords_rcp(&myHGradBasis, false); // Auxillary p=1 basis Basis_HGRAD_QUAD_C1_FEM > myHGradBasis_aux; RCP > > myHGradBasis_aux_rcp(&myHGradBasis_aux, false); int numFieldsG = myHGradBasis.getCardinality(); FieldContainer HGBValues(numFieldsG, numCubPoints); FieldContainer HGBGrads(numFieldsG, numCubPoints, spaceDim); // Evaluate basis values and gradients at cubature points myHGradBasis.getValues(HGBValues, cubPoints, OPERATOR_VALUE); myHGradBasis.getValues(HGBGrads, cubPoints, OPERATOR_GRAD); //#define OUTPUT_REFERENCE_FUNCTIONS #ifdef OUTPUT_REFERENCE_FUNCTIONS // Evaluate basis values and gradients at DOF points FieldContainer DofCoords(numFieldsG,spaceDim); myHGradBasis.getDofCoords(DofCoords); FieldContainer HGBValues_at_Dofs(numFieldsG,numFieldsG); myHGradBasis.getValues(HGBValues_at_Dofs, DofCoords, OPERATOR_VALUE); printf("*** Dof Coords ***\n"); for(int j=0; j Pn_ownedGIDs(Pn_ownedNodes); int oidx=0; for(int i=0;i P1_ownedGIDs(P1_ownedNodes); oidx=0; for(int i=0;i nodeSeeds(Pn_numNodes,INVALID_LO); //unknowns at mesh nodes oidx=0; for(int i=0;i edgeSeeds(Pn_numNodes,INVALID_LO); for (size_t i=0; i cellSeeds(Pn_numNodes,INVALID_LO); for (size_t i=0; i globalMapG = rcp(new driver_map_type(INVALID_GO,&Pn_ownedGIDs[0],Pn_ownedNodes,0,Comm)); // Generate p1 map RCP P1_globalMap = rcp(new driver_map_type(INVALID_GO,&P1_ownedGIDs[0],P1_ownedNodes,0,Comm)); // Genetrate Pn-to-P1 coarsening. Kokkos::DynRankView elemToNodeI2; CopyFieldContainer2D(elemToNode,elemToNodeI2); if (inputSolverList.isParameter("aux P1") && inputSolverList.isParameter("linear P1")) throw std::runtime_error("Can only specify \"aux P1\" or \"linear P1\", not both."); if (inputSolverList.isParameter("linear P1")) { printf("Activating Linear scheduled p-coarsening...\n"); Teuchos::ParameterList & mymuelu = inputSolverList.sublist("MueLu"); Teuchos::ParameterList & level0 = mymuelu.sublist("level 0"); level0.set("pcoarsen: element to node map",rcp(&elemToNodeI2,false)); inputSolverList.remove("linear P1"); //even though LevelWrap happily accepts this parameter } // Global arrays in Tpetra format crs_matrix_type StiffMatrix(globalMapG, 20*numFieldsG); RCP rhsVector = rcp(new multivector_type(globalMapG,1)); RCP femCoefficients = rcp(new multivector_type(globalMapG,1)); tm = Teuchos::null; /**********************************************************************************/ /**** COOORDINATES FOR DISTANCE LAPLACIAN ***/ /**********************************************************************************/ // Put coordinates in multivector for output RCP nCoord = rcp(new multivector_type(globalMapG,dim)); int indOwned = 0; for (int inode=0; inodegetDataNonConst(0)[indOwned]=nodeCoord(inode,0); nCoord->getDataNonConst(1)[indOwned]=nodeCoord(inode,1); indOwned++; } } /**********************************************************************************/ /************************** DIRICHLET BC SETUP ************************************/ /**********************************************************************************/ tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Get Dirichlet boundary values"))); int numBCNodes = 0; for (int inode = 0; inode < numNodes; inode++){ if (nodeOnBoundary(inode) && Pn_nodeIsOwned[inode]){ numBCNodes++; } } // Vector for use in applying BCs multivector_type v(globalMapG,true); ArrayRCP vdata = v.getDataNonConst(0); // Set v to boundary values on Dirichlet nodes std::vector BCNodes(numBCNodes); int indbc=0; int iOwned=0; for (int inode=0; inodegetGlobalNumElements(), (int)StiffMatrix.getRowMap()->getGlobalNumElements(), (int)StiffMatrix.getColMap()->getGlobalNumElements(), (int)StiffMatrix.getRangeMap()->getGlobalNumElements()); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Getting cubature for auxiliary P1 mesh"))); ///////////////////////////////////////////////////////////////////// // Create P1 matrix and RHS to be used in preconditioner ///////////////////////////////////////////////////////////////////// //Cubature // Get numerical integration points and weights int cubDegree_aux = 2; //TODO This was 3 for P=2. I think this should be 2 now .... Ask Chris. RCP > myCub_aux = cubFactory.create(P1_cellType, cubDegree_aux); int cubDim_aux = myCub_aux->getDimension(); int numCubPoints_aux = myCub_aux->getNumPoints(); FieldContainer cubPoints_aux(numCubPoints_aux, cubDim_aux); FieldContainer cubWeights_aux(numCubPoints_aux); myCub_aux->getCubature(cubPoints_aux, cubWeights_aux); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Getting basis for auxiliary P1 mesh"))); //Basis // Define basis int numFieldsG_aux = myHGradBasis_aux.getCardinality(); FieldContainer HGBValues_aux(numFieldsG_aux, numCubPoints_aux); FieldContainer HGBGrads_aux(numFieldsG_aux, numCubPoints_aux, spaceDim); // Evaluate basis values and gradients at cubature points myHGradBasis_aux.getValues(HGBValues_aux, cubPoints_aux, OPERATOR_VALUE); myHGradBasis_aux.getValues(HGBGrads_aux, cubPoints_aux, OPERATOR_GRAD); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Global assembly (auxiliary system)"))); crs_matrix_type StiffMatrix_aux(globalMapG, 20*numFieldsG_aux); RCP rhsVector_aux = rcp(new multivector_type(globalMapG,1)); desiredWorksetSize = numElems_aux; numWorksets = numElems_aux/desiredWorksetSize; if(numWorksets*desiredWorksetSize < numElems_aux) numWorksets += 1; CreateLinearSystem(numWorksets, desiredWorksetSize, aux_P1_elemToNode, nodeCoord, cubPoints_aux, cubWeights_aux, myHGradBasis_aux_rcp, HGBGrads_aux, HGBValues_aux, Pn_globalNodeIds, StiffMatrix_aux, rhsVector_aux, msg ); /**********************************************************************************/ /***************************** STATISTICS (Part III) ******************************/ /**********************************************************************************/ std::vector global_stat_max(NUM_STATISTICS),global_stat_min(NUM_STATISTICS), global_stat_sum(NUM_STATISTICS); Teuchos::reduceAll(*Comm,Teuchos::REDUCE_MIN,NUM_STATISTICS,local_stat_min.data(),global_stat_min.data()); Teuchos::reduceAll(*Comm,Teuchos::REDUCE_MAX,NUM_STATISTICS,local_stat_max.data(),global_stat_max.data()); Teuchos::reduceAll(*Comm,Teuchos::REDUCE_SUM,NUM_STATISTICS,local_stat_sum.data(),global_stat_sum.data()); // NOTE: All output properties should be unitless if we want to compare across problems. // NOTE: Should the mean be weighted by cell volume? That is not currently done. // 0 - Material property problemStatistics.set("sigma: min/mean",global_stat_min[0]/global_stat_sum[0]*numElemsGlobal); problemStatistics.set("sigma: max/mean",global_stat_max[0]/global_stat_sum[0]*numElemsGlobal); // 1 - Max/min edge ratio problemStatistics.set("element edge ratio: min",global_stat_min[1]); problemStatistics.set("element edge ratio: max",global_stat_max[1]); problemStatistics.set("element edge ratio: mean",global_stat_sum[1] / numElemsGlobal); // 2 - det of cell Jacobian (later) problemStatistics.set("element det jacobian: min/mean",global_stat_min[2]/global_stat_sum[2]*numElemsGlobal); problemStatistics.set("element det jacobian: max/mean",global_stat_max[2]/global_stat_sum[2]*numElemsGlobal); if(use_new_problem_stats) { // 3 - Stretch problemStatistics.set("Stretch max", global_stat_max[3]); problemStatistics.set("Stretch min", global_stat_min[3]); problemStatistics.set("Stretch mean", global_stat_sum[3] / numElemsGlobal); // 4 - Diagonal Ratio problemStatistics.set("Diagonal Ratio max", global_stat_max[4]); problemStatistics.set("Diagonal Ratio min", global_stat_min[4]); problemStatistics.set("Diagonal Ratio mean", global_stat_sum[4]/numElemsGlobal); // 5 - Inverse Taper problemStatistics.set("Inverse Taper max", global_stat_max[5]); problemStatistics.set("Inverse Taper min", global_stat_min[5]); problemStatistics.set("Inverse Taper mean", global_stat_sum[5] / numElemsGlobal); // 6 - Skew problemStatistics.set("Skew max", global_stat_max[6]); problemStatistics.set("Skew min", global_stat_min[6]); problemStatistics.set("Skew mean", global_stat_sum[6] / numElemsGlobal); // 7 - Lapl Diag problemStatistics.set("Lapl Diag max", global_stat_max[7]); problemStatistics.set("Lapl Diag min", global_stat_min[7]); problemStatistics.set("Lapl Diag mean", global_stat_sum[7] / StiffMatrix.getGlobalNumEntries()); } // Print Problem Statistics std::cout<<"*** Problem Statistics ***\n"< P_identity, R_identity; if (inputSolverList.isParameter("aux P1")) { printf("Generating Identity Pn-to-P1 coarsening...\n"); GenerateIdentityCoarsening_pn_to_p1(elemToNode, StiffMatrix_aux.getDomainMap(), StiffMatrix.getRangeMap(), P_identity, R_identity); inputSolverList.remove("aux P1"); //even though LevelWrap happily accepts this parameter } /**********************************************************************************/ /******************************* ADJUST MATRIX DUE TO BC **************************/ /**********************************************************************************/ // Zero out rows and columns of stiffness matrix corresponding to Dirichlet edges // and add one to diagonal. Apply_Dirichlet_BCs(BCNodes,StiffMatrix,*femCoefficients,*rhsVector,v); /////////////////////////////////////////// // Apply BCs to auxiliary P1 matrix /////////////////////////////////////////// // Zero out rows and columns of stiffness matrix corresponding to Dirichlet edges // and add one to diagonal. std::cout << "numBCNodes = " << numBCNodes << std::endl; std::cout << "globalMapG #elts = " << globalMapG->getLocalNumElements() << std::endl; Apply_Dirichlet_BCs(BCNodes,StiffMatrix_aux,*rhsVector_aux,*rhsVector_aux,v); tm = Teuchos::null; // Optionally dump the matrix and/or its coords to files. { typedef Tpetra::MatrixMarket::Writer writer_type; if (matrixFilename != "") { writer_type::writeSparseFile (matrixFilename, StiffMatrix); } if (rhsFilename != "") { writer_type::writeDenseFile (rhsFilename, rhsVector); } if (coordsFilename != "") { writer_type::writeDenseFile (coordsFilename, nCoord); } } /**********************************************************************************/ /*********************************** SOLVE ****************************************/ /**********************************************************************************/ // Which Solver? std::string solveType("cg"); if(inputSolverList.isParameter("solver")) { solveType = inputSolverList.get("solver"); } // Run the solver std::string amgType("MueLu"); ParameterList amgList; std::string seedType = inputSolverList.get("seed type","node"); inputSolverList.remove("seed type"); if (inputSolverList.isSublist("MueLu")) amgList = inputSolverList.sublist("MueLu"); else amgList = inputSolverList; #ifdef HAVE_TRILINOSCOUPLINGS_AVATAR // If we have Avatar, then let's use it if (inputSolverList.isSublist("Avatar-MueLu")) { // NOTE: User will need to make sure these are named consistently with the tree files specified ParameterList problemFeatures = problemStatistics; ParameterList avatarParams = inputSolverList.sublist("Avatar-MueLu"); std::cout<<"*** Avatar Parameters ***\n"< > mycomm = StiffMatrix.getRowMap()->getComm(); MueLu::AvatarInterface avatar(mycomm,avatarParams); std::cout<<"*** Avatar Setup ***"<("smoother: type"); if ( (smooType == "SPARSE BLOCK RELAXATION" || smooType == "BLOCK RELAXATION") && sl.isParameter("smoother: params")) { ParameterList &ssl = sl.sublist("smoother: params"); std::cout << "found \"smoother: params\" for block relaxation" << std::endl; int numLocalParts = -1; std::cout << "setting \"partitioner: map\"" << std::endl; if (seedType == "node") { ssl.set("partitioner: map", nodeSeeds); numLocalParts = numNodeSeeds; } else if (seedType == "edge") { ssl.set("partitioner: map", edgeSeeds); numLocalParts = numEdgeSeeds; } else if (seedType == "cell") { ssl.set("partitioner: map", cellSeeds); numLocalParts = numCellSeeds; } if(numLocalParts!=-1){ std::cout << "setting \"partitioner: local parts\" = " << numLocalParts << std::endl; ssl.set("partitioner: local parts", numLocalParts); } } if(sl.isParameter("coarse: type")) { std::string coarseType = sl.get("coarse: type"); if ((coarseType == "SPARSE BLOCK RELAXATION"|| coarseType == "BLOCK RELAXATION") && sl.isParameter("coarse: params")) { ParameterList &ssl = sl.sublist("coarse: params"); std::cout << "found \"smoother: params\" for block relaxation" << std::endl; int numLocalParts=-1; std::cout << "setting \"partitioner: map\"" << std::endl; if (seedType == "node") { ssl.set("partitioner: map", nodeSeeds); numLocalParts = numNodeSeeds; } else if (seedType == "edge") { ssl.set("partitioner: map", edgeSeeds); numLocalParts = numEdgeSeeds; } else if (seedType == "cell") { ssl.set("partitioner: map", cellSeeds); numLocalParts = numCellSeeds; } if(numLocalParts!=-1){ std::cout << "setting \"partitioner: local parts\" = " << numLocalParts << std::endl; ssl.set("partitioner: local parts", numLocalParts); } } } } // /////////////////////////////////////////////////////////////////////// // // Shove coordinates in multivector for saving to file. multivector_type coordinates(StiffMatrix.getRowMap(),2); ArrayRCP xdat = coordinates.getDataNonConst(0); ArrayRCP ydat = coordinates.getDataNonConst(1); for (size_t i=0; i exactNodalVals = rcp(new multivector_type(globalMapG,1)); double TotalErrorResidual = 0.0; double TotalErrorExactSol = 0.0; // Get exact solution at nodes for (int i = 0; isumIntoGlobalValue(rowindex, 0, exactu); } } char probType[10] = "laplace"; RCP interpolationMatrix, restrictionMatrix; if (P_identity != Teuchos::null) { Teuchos::ParameterList & level1 = amgList.sublist("level 1"); RCP xA1 = MueLu::TpetraCrs_To_XpetraMatrix(rcpFromRef(StiffMatrix_aux)); RCP xP = MueLu::TpetraCrs_To_XpetraMatrix(P_identity); level1.set("A",xA1); level1.set("P",xP); amgList.set("transpose: use implicit",true); // create nullspace for Level 1 (first coarse AMG level) RCP nullspace = rcp(new multivector_type(exactNodalVals->getMap(),1)); ArrayRCP data = nullspace->getDataNonConst(0); for (int i=0; i xnullspace = MueLu::TpetraMultiVector_To_XpetraMultiVector(nullspace); level1.set("Nullspace",xnullspace); } int maxits = inputSolverList.get("Maximum Iterations",(int)100); double tol = inputSolverList.get("Convergence Tolerance",(double)1e-10); if (amgList.isParameter("solve Ae=0")) { rhsVector->scale(0.0); femCoefficients->randomize(); } TestMultiLevelPreconditionerLaplace(probType, amgList, rcpFromRef(StiffMatrix), nCoord, exactNodalVals, rhsVector, maxits, tol, femCoefficients, TotalErrorResidual, TotalErrorExactSol, amgType, solveType); /**********************************************************************************/ /**************************** CALCULATE ERROR *************************************/ /**********************************************************************************/ tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Calculate error"))); double L2err = 0.0; double L2errTot = 0.0; double H1err = 0.0; double H1errTot = 0.0; double Linferr = 0.0; double LinferrTot = 0.0; // Import solution onto current processor //int numNodesGlobal = globalMapG.NumGlobalElements(); RCP solnMap = rcp(new driver_map_type(static_cast(numNodesGlobal), static_cast(numNodesGlobal), 0, Comm)); Tpetra::Import solnImporter(globalMapG,solnMap); multivector_type uCoeff(solnMap,1); uCoeff.doImport(*femCoefficients, solnImporter, Tpetra::INSERT); // Define desired workset size desiredWorksetSize = numElems; int numWorksetsErr = numElems/desiredWorksetSize; // When numElems is not divisible by desiredWorksetSize, increase workset count by 1 if(numWorksetsErr*desiredWorksetSize < numElems) numWorksetsErr += 1; // Get cubature points and weights for error calc (may be different from previous) Intrepid::DefaultCubatureFactory cubFactoryErr; int cubDegErr = 3*degree; RCP > cellCubatureErr = cubFactoryErr.create(Pn_cellType, cubDegErr); int cubDimErr = cellCubatureErr->getDimension(); int numCubPointsErr = cellCubatureErr->getNumPoints(); Intrepid::FieldContainer cubPointsErr(numCubPointsErr, cubDimErr); Intrepid::FieldContainer cubWeightsErr(numCubPointsErr); cellCubatureErr->getCubature(cubPointsErr, cubWeightsErr); // Evaluate basis values and gradients at cubature points Intrepid::FieldContainer uhGVals(numFieldsG, numCubPointsErr); Intrepid::FieldContainer uhGrads(numFieldsG, numCubPointsErr, spaceDim); myHGradBasis.getValues(uhGVals, cubPointsErr, Intrepid::OPERATOR_VALUE); myHGradBasis.getValues(uhGrads, cubPointsErr, Intrepid::OPERATOR_GRAD); // Loop over worksets for(int workset = 0; workset < numWorksetsErr; workset++){ // compute cell numbers where the workset starts and ends int worksetSize = 0; int worksetBegin = (workset + 0)*desiredWorksetSize; int worksetEnd = (workset + 1)*desiredWorksetSize; // when numElems is not divisible by desiredWorksetSize, the last workset ends at numElems worksetEnd = (worksetEnd <= numElems) ? worksetEnd : numElems; // now we know the actual workset size and can allocate the array for the cell nodes worksetSize = worksetEnd - worksetBegin; Intrepid::FieldContainer cellWorksetEr(worksetSize, numFieldsG, spaceDim); Intrepid::FieldContainer worksetApproxSolnCoef(worksetSize, numFieldsG); // loop over cells to fill arrays with coordinates and discrete solution coefficient int cellCounter = 0; ArrayRCP uCoeffData = uCoeff.getData(0); ArrayRCP femCoeffData = femCoefficients->getData(0); for(int cell = worksetBegin; cell < worksetEnd; cell++){ for (int node = 0; node < numFieldsG; node++) { cellWorksetEr(cellCounter, node, 0) = nodeCoord( elemToNode(cell, node), 0); cellWorksetEr(cellCounter, node, 1) = nodeCoord( elemToNode(cell, node), 1); int rowIndex = Pn_globalNodeIds[elemToNode(cell, node)]; #ifdef HAVE_MPI worksetApproxSolnCoef(cellCounter, node) = uCoeffData[rowIndex]; #else worksetApproxSolnCoef(cellCounter, node) = femCoeffData[rowIndex]; #endif } cellCounter++; } // end cell loop // Containers for Jacobian Intrepid::FieldContainer worksetJacobianE(worksetSize, numCubPointsErr, spaceDim, spaceDim); Intrepid::FieldContainer worksetJacobInvE(worksetSize, numCubPointsErr, spaceDim, spaceDim); Intrepid::FieldContainer worksetJacobDetE(worksetSize, numCubPointsErr); Intrepid::FieldContainer worksetCubWeightsE(worksetSize, numCubPointsErr); // Containers for basis values and gradients in physical space Intrepid::FieldContainer uhGValsTrans(worksetSize,numFieldsG, numCubPointsErr); Intrepid::FieldContainer uhGradsTrans(worksetSize, numFieldsG, numCubPointsErr, spaceDim); // compute cell Jacobians, their inverses and their determinants IntrepidCTools::setJacobian(worksetJacobianE, cubPointsErr, cellWorksetEr, myHGradBasis_rcp); IntrepidCTools::setJacobianInv(worksetJacobInvE, worksetJacobianE ); IntrepidCTools::setJacobianDet(worksetJacobDetE, worksetJacobianE ); // map cubature points to physical frame Intrepid::FieldContainer worksetCubPoints(worksetSize, numCubPointsErr, cubDimErr); IntrepidCTools::mapToPhysicalFrame(worksetCubPoints, cubPointsErr, cellWorksetEr, myHGradBasis_rcp); // evaluate exact solution and gradient at cubature points Intrepid::FieldContainer worksetExactSoln(worksetSize, numCubPointsErr); Intrepid::FieldContainer worksetExactSolnGrad(worksetSize, numCubPointsErr, spaceDim); evaluateExactSolution(worksetExactSoln, worksetCubPoints); evaluateExactSolutionGrad(worksetExactSolnGrad, worksetCubPoints); // transform basis values to physical coordinates IntrepidFSTools::HGRADtransformVALUE(uhGValsTrans, uhGVals); IntrepidFSTools::HGRADtransformGRAD(uhGradsTrans, worksetJacobInvE, uhGrads); // compute weighted measure IntrepidFSTools::computeCellMeasure(worksetCubWeightsE, worksetJacobDetE, cubWeightsErr); // evaluate the approximate solution and gradient at cubature points Intrepid::FieldContainer worksetApproxSoln(worksetSize, numCubPointsErr); Intrepid::FieldContainer worksetApproxSolnGrad(worksetSize, numCubPointsErr, spaceDim); IntrepidFSTools::evaluate(worksetApproxSoln, worksetApproxSolnCoef, uhGValsTrans); IntrepidFSTools::evaluate(worksetApproxSolnGrad, worksetApproxSolnCoef, uhGradsTrans); // get difference between approximate and exact solutions Intrepid::FieldContainer worksetDeltaSoln(worksetSize, numCubPointsErr); Intrepid::FieldContainer worksetDeltaSolnGrad(worksetSize, numCubPointsErr, spaceDim); IntrepidRSTools::subtract(worksetDeltaSoln, worksetApproxSoln, worksetExactSoln); IntrepidRSTools::subtract(worksetDeltaSolnGrad, worksetApproxSolnGrad, worksetExactSolnGrad); // take absolute values IntrepidRSTools::absval(worksetDeltaSoln); IntrepidRSTools::absval(worksetDeltaSolnGrad); // apply cubature weights to differences in values and grads for use in integration Intrepid::FieldContainer worksetDeltaSolnWeighted(worksetSize, numCubPointsErr); Intrepid::FieldContainer worksetDeltaSolnGradWeighted(worksetSize, numCubPointsErr, spaceDim); IntrepidFSTools::scalarMultiplyDataData(worksetDeltaSolnWeighted, worksetCubWeightsE, worksetDeltaSoln); IntrepidFSTools::scalarMultiplyDataData(worksetDeltaSolnGradWeighted, worksetCubWeightsE, worksetDeltaSolnGrad); // integrate to get errors on each element Intrepid::FieldContainer worksetL2err(worksetSize); Intrepid::FieldContainer worksetH1err(worksetSize); IntrepidFSTools::integrate(worksetL2err, worksetDeltaSoln, worksetDeltaSolnWeighted, Intrepid::COMP_BLAS); IntrepidFSTools::integrate(worksetH1err, worksetDeltaSolnGrad, worksetDeltaSolnGradWeighted, Intrepid::COMP_BLAS); // loop over cells to get errors for total workset cellCounter = 0; for(int cell = worksetBegin; cell < worksetEnd; cell++){ // loop over cubature points for(int nPt = 0; nPt < numCubPointsErr; nPt++){ Linferr = std::max(Linferr, worksetDeltaSoln(cellCounter,nPt)); } L2err += worksetL2err(cellCounter); H1err += worksetH1err(cellCounter); cellCounter++; } // end cell loop } // end loop over worksets #ifdef HAVE_MPI // sum over all processors TC_sumAll(Comm,L2err,L2errTot); TC_sumAll(Comm,H1err,H1errTot); TC_maxAll(Comm,Linferr,LinferrTot); #else L2errTot = L2err; H1errTot = H1err; LinferrTot = Linferr; #endif if (MyPID == 0) { std::cout << "\n" << "L2 Error: " << sqrt(L2errTot) <<"\n"; std::cout << "H1 Error: " << sqrt(H1errTot) <<"\n"; std::cout << "LInf Error: " << LinferrTot <<"\n\n"; } const bool alwaysWriteLocal = false; const bool writeGlobalStats = true; const bool writeZeroTimers = false; const bool ignoreZeroTimers = true; const std::string filter = ""; if (optPrintTimings) { tm.reset(); TimeMonitor::summarize(Comm.ptr(), std::cout, alwaysWriteLocal, writeGlobalStats, writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers); } // Cleanup 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 [] P1_globalNodeIds; delete [] P1_nodeIsOwned; 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 const Scalar exactSolution(const Scalar& x, const Scalar& y) { // Patch test: bi-linear function is in the FE space and should be recovered return 1. + x + y + x*y; // Analytic solution with homogeneous Dirichlet boundary data // return sin(M_PI*x)*sin(M_PI*y)**exp(x+y); // Analytic solution with inhomogeneous Dirichlet boundary data // return exp(x + y )/(1. + x*y); } // Stored diffusion tensor std::vector diffusion_tensor; template void materialTensor(Scalar material[][2], const Scalar& x, const Scalar& y) { if(diffusion_tensor.size() == 0) diffusion_tensor = ::TrilinosCouplings::IntrepidPoissonExample::getDiffusionMatrix2D(); material[0][0] = (Scalar)diffusion_tensor[0]; material[0][1] = (Scalar)diffusion_tensor[1]; // material[1][0] = (Scalar)diffusion_tensor[2]; material[1][1] = (Scalar)diffusion_tensor[3]; } /**********************************************************************************/ /************** AUXILIARY FUNCTIONS FROM EXACT SOLUTION ***************************/ /**********************************************************************************/ /************ Grad of Exact Solution ****************/ template void exactSolutionGrad(Scalar gradExact[2], const Scalar& x, const Scalar& y) { // To enable derivatives of the gradient (i.e., 2nd derivatives of the exact solution) need 2 levels of fad types Sacado::Fad::SFad fad_x = x; Sacado::Fad::SFad fad_y = y; Sacado::Fad::SFad u; // Indicate the independent variables fad_x.diff(0,2); fad_y.diff(1,2); u = exactSolution(fad_x, fad_y); gradExact[0] = u.dx(0); gradExact[1] = u.dx(1); } /************ Source Term (RHS) ****************/ template const Scalar sourceTerm(Scalar& x, Scalar& y){ Scalar u; Scalar grad_u[2]; Scalar flux[2] = {0.0, 0.0}; Scalar material[2][2]; Scalar f = 0.; // Indicate the independent variables x.diff(0,2); y.diff(1,2); // Get exact solution and its gradient u = exactSolution(x, y); exactSolutionGrad(grad_u, x, y); // Get material tensor materialTensor(material, x, y); // Compute total flux = (A.grad u) for(int i = 0; i < 2; i++){ // Add diffusive flux for(int j = 0; j < 2; j++){ flux[i] += material[i][j]*grad_u[j]; } } // Compute source term (right hand side): f = -div(A.grad u) f = -(flux[0].dx(0) + flux[1].dx(1)); return f; } /**********************************************************************************/ /*************************** EVALUATION METHODS ***********************************/ /**********************************************************************************/ /************ Material Tensor ****************/ 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[2][2]; for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ double x = evaluationPoints(cell, pt, 0); double y = evaluationPoints(cell, pt, 1); materialTensor(material, x, y); for(int row = 0; row < spaceDim; row++){ for(int col = 0; col < spaceDim; col++){ matTensorValues(cell, pt, row, col) = material[row][col]; } } } } } /************ Source Term (RHS) ****************/ template void evaluateSourceTerm(ArrayOut & sourceTermValues, const ArrayIn & evaluationPoints){ int numWorksetCells = evaluationPoints.dimension(0); int numPoints = evaluationPoints.dimension(1); for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ Sacado::Fad::SFad x = evaluationPoints(cell, pt, 0); Sacado::Fad::SFad y = evaluationPoints(cell, pt, 1); sourceTermValues(cell, pt) = sourceTerm >(x, y).val(); } } } /************ Exact Solution ****************/ template void evaluateExactSolution(ArrayOut & exactSolutionValues, const ArrayIn & evaluationPoints){ int numWorksetCells = evaluationPoints.dimension(0); int numPoints = evaluationPoints.dimension(1); for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ double x = evaluationPoints(cell, pt, 0); double y = evaluationPoints(cell, pt, 1); exactSolutionValues(cell, pt) = exactSolution(x, y); } } } /************ Grad of Exact Solution ****************/ template void evaluateExactSolutionGrad(ArrayOut & exactSolutionGradValues, const ArrayIn & evaluationPoints){ int numWorksetCells = evaluationPoints.dimension(0); int numPoints = evaluationPoints.dimension(1); int spaceDim = evaluationPoints.dimension(2); double gradient[2]; for(int cell = 0; cell < numWorksetCells; cell++){ for(int pt = 0; pt < numPoints; pt++){ double x = evaluationPoints(cell, pt, 0); double y = evaluationPoints(cell, pt, 1); exactSolutionGrad(gradient, x, y); for(int row = 0; row < spaceDim; row++){ exactSolutionGradValues(cell, pt, row) = gradient[row]; } } } } /**********************************************************************************/ /******************************* TEST ML ******************************************/ /**********************************************************************************/ // Test ML int TestMultiLevelPreconditionerLaplace(char ProblemType[], ParameterList & amgList, RCP const &A0, RCP & nCoord, RCP const & xexact, RCP & b, int maxIts, double tol, RCP & uh, double & TotalErrorResidual, double & TotalErrorExactSol, std::string &amgType, std::string &solveType) { // int mypid = A0->getComm()->getRank(); RCPx = uh; linear_problem_type Problem(linear_problem_type(A0, x, b)); RCP lhs = Problem.getLHS(); RCP rhs = Problem.getRHS(); RCP tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Total solve time"))); // =================== // // call ML and Belos // // =================== // if (amgType != "ML" && amgType != "MueLu") { std::ostringstream errStr; errStr << "Error: \"amgType\" has value \"" << amgType << "\". Valid values are \"ML\" or \"MueLu\"."; throw std::runtime_error(errStr.str()); } int numIterations=0; if (amgType == "ML") { throw std::runtime_error("Error: ML does not support Tpetra objects"); } else if (amgType == "MueLu") { // Multigrid Hierarchy, the easy way RCP A0op = A0; amgList.sublist("user data").set("Coordinates",nCoord); Teuchos::RCP M = MueLu::CreateTpetraPreconditioner(A0op, amgList); Problem.setRightPrec(M); bool set = Problem.setProblem(); if (set == false) { std::cout << "\nERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; return EXIT_FAILURE; } // Belos parameter list ParameterList belosList; belosList.set("Maximum Iterations", maxIts); // Maximum number of iterations allowed belosList.set("Convergence Tolerance", tol); // Relative convergence tolerance requested // belosList.set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); belosList.set("Output Frequency", 1); belosList.set("Output Style", 1); belosList.set ("Verbosity", 33); bool scaleResidualHist = true; if (!scaleResidualHist) belosList.set("Implicit Residual Scaling", "None"); // Create an iterative solver manager RCP< Belos::SolverManager > solver; if(solveType == "cg") solver = rcp(new Belos::PseudoBlockCGSolMgr(rcpFromRef(Problem), rcp(&belosList, false))); else if (solveType == "gmres") { solver = rcp(new Belos::PseudoBlockGmresSolMgr(rcpFromRef(Problem), rcp(&belosList, false))); } else if (solveType == "fixed point" || solveType == "fixed-point") { solver = rcp(new Belos::FixedPointSolMgr(rcpFromRef(Problem), rcp(&belosList, false))); } else { std::cout << "\nERROR: Invalid solver '"<solve(); //writer_type::writeDenseFile("sol.m", x); numIterations = solver->getNumIters(); } ArrayRCP lhsdata = lhs->getData(0); ArrayRCP xexactdata = xexact->getData(0); // ==================================================== // // compute difference between exact solution and ML one // // ==================================================== // double d = 0.0, d_tot = 0.0 , s =0.0, s_tot=0.0; for( size_t i=0 ; igetMap()->getLocalNumElements() ; ++i ) { d += (lhsdata[i] - xexactdata[i]) * (lhsdata[i] - xexactdata[i]); s += xexactdata[i]* xexactdata[i]; } lhsdata = Teuchos::null; xexactdata = Teuchos::null; TC_sumAll(A0->getComm(),d,d_tot); TC_sumAll(A0->getComm(),s,s_tot); // ================== // // compute ||Ax - b|| // // ================== // Array Norm(1); vector_type Ax(rhs->getMap()); A0->apply(*lhs, Ax); Ax.update(1.0, *rhs, -1.0); Ax.norm2(Norm); string msg = ProblemType; int numProc = A0->getComm()->getSize(); if (A0->getComm()->getRank() == 0) { cout << msg << endl << "......Using " << numProc << " processes" << endl; cout << msg << "......||A x - b||_2 = " << Norm[0] << endl; cout << msg << "......||x_exact - x||_2/||x_exact||_2 = " << sqrt(d_tot/s_tot) << endl; } TotalErrorExactSol += sqrt(d_tot); TotalErrorResidual += Norm[0]; return(numIterations); } /*********************************************************************************************************/ /*********************************************************************************************************/ /*********************************************************************************************************/ void GenerateEdgeEnumeration(const FieldContainer & elemToNode, const FieldContainer & nodeCoord, FieldContainer & elemToEdge, FieldContainer & elemToEdgeOrient, FieldContainer & edgeToNode, FieldContainer & edgeCoord) { // Not especially efficient, but effective... at least in serial! int numElems = elemToNode.dimension(0); int numNodesperElem = elemToNode.dimension(1); int dim = nodeCoord.dimension(1); // Sanity checks if(numNodesperElem !=4) throw std::runtime_error("Error: GenerateEdgeEnumeration only works on Quads!"); if(elemToEdge.dimension(0)!=numElems || elemToEdge.dimension(1)!=4 || elemToEdge.dimension(0)!=elemToEdgeOrient.dimension(0) || elemToEdge.dimension(1)!=elemToEdgeOrient.dimension(1)) throw std::runtime_error("Error: GenerateEdgeEnumeration array size mismatch"); int edge_node0_id[4]={0,1,2,3}; int edge_node1_id[4]={1,2,3,0}; // Run over all the elements and start enumerating edges typedef std::map,int> en_map_type; en_map_type e2n; int num_edges=0; en_map_type edge_map; for(int i=0; i ep(lo,hi); int edge_id; en_map_type::iterator iter = edge_map.find(ep); if(iter==edge_map.end()) { edge_map[ep] = num_edges; edge_id = num_edges; num_edges++; } else edge_id = (*iter).second; elemToEdge(i,j) = edge_id; elemToEdgeOrient(i,j) = (lo==elemToNode(i,edge_node0_id[j]))?1:-1; } } // Fill out the edge centers (clobbering data if needed) edgeCoord.resize(num_edges,dim); for(int i=0; i & P1_elemToNode, const FieldContainer & P1_nodeCoord, const FieldContainer & P1_edgeCoord, const FieldContainer & P1_elemToEdge, const FieldContainer & P1_elemToEdgeOrient, const FieldContainer & P1_nodeOnBoundary, FieldContainer & Pn_elemToNode, FieldContainer & Pn_nodeCoord, FieldContainer & Pn_nodeOnBoundary, std::vector & Pn_edgeNodes, std::vector & Pn_cellNodes) { //#define DEBUG_PROMOTE_MESH int numElems = P1_elemToNode.dimension(0); int P1_numNodesperElem = P1_elemToNode.dimension(1); int P1_numEdgesperElem = P1_elemToEdge.dimension(1); int Pn_numNodesperElem = Pn_elemToNode.dimension(1); int P1_numNodes = P1_nodeCoord.dimension(0); int P1_numEdges = P1_edgeCoord.dimension(0); int dim = P1_nodeCoord.dimension(1); #ifdef DEBUG_PROMOTE_MESH int Pn_numNodes = Pn_nodeCoord.dimension(0); #endif int Pn_ExpectedNodesperElem = P1_numNodesperElem + (degree-1)*P1_numEdgesperElem + (degree-1)*(degree-1); int Pn_ExpectedNumNodes = P1_numNodes + (degree-1)*P1_numEdges + (degree-1)*(degree-1)*numElems; // Sanity checks if(P1_numNodesperElem !=4 || Pn_numNodesperElem !=Pn_ExpectedNodesperElem ) throw std::runtime_error("Error: PromoteMesh_Pn_Kirby only works on Quads!"); if(P1_elemToEdge.dimension(0)!=numElems || P1_elemToEdge.dimension(1)!=4 || P1_elemToEdge.dimension(0)!=P1_elemToEdgeOrient.dimension(0) || P1_elemToEdge.dimension(1)!=P1_elemToEdgeOrient.dimension(1) || Pn_elemToNode.dimension(0)!=numElems || Pn_nodeCoord.dimension(0) != Pn_ExpectedNumNodes) throw std::runtime_error("Error: PromoteMesh_Pn_Kirby array size mismatch"); const CellTopologyData &cellTopoData = *shards::getCellTopologyData >(); shards::CellTopology cellTopo(&cellTopoData); // Kirby elements are ordered strictly in lex ordering from the bottom left to the top right /* Kirby Quad-9 Layout (p=2) inode6 -- inode7 -- inode8 | | inode3 inode4 inode5 | | inode0 -- inode1 -- inode2 Kirby Quad-16 Layout (p=3) inode12-- inode13-- inode14-- inode15 | | inode8 inode9 inode10 inode11 | | inode4 inode5 inode6 inode7 | | inode0 -- inode1 -- inode2 -- inode3 */ // We still number the global nodes in the exodus-style (node,edge,face) ordering, *by global orientation* but // the elem2node map has to account for Kirby-style elements int p1_node_in_pn[4] = {0,degree, (degree+1)*(degree+1)-1, degree*(degree+1)}; int edge_node0_id[4]={0,1,2,3}; int edge_node1_id[4]={1,2,3,0}; // As you advance along each edge, we'll skip by the following amount to get to the next Kirby dof // edge_skip is in *local* orientation, so we'll multiply it by the elemToEdgeOrient value int edge_skip[4] = {1,degree+1,-1,-(degree+1)}; int center_root = degree+2; // Make the new el2node array for(int i=0; i RefNodeCoords(Pn_numNodesperElem,dim); FieldContainer RefNodeCoords2(1,Pn_numNodesperElem,dim); FieldContainer PhysNodeCoords(1,Pn_numNodesperElem,dim); Basis_HGRAD_QUAD_Cn_FEM > BasisPn(degree,pointType); BasisPn.getDofCoords(RefNodeCoords); #ifdef DEBUG_PROMOTE_MESH printf("**** P=%d reference nodal coordinates ***\n",degree); for(int i=0; i my_p1_nodes(1,P1_numNodesperElem,dim); for(int j=0; j::mapToPhysicalFrame(PhysNodeCoords,RefNodeCoords2,my_p1_nodes,cellTopo); #ifdef DEBUG_PROMOTE_MESH printf("[%2d] PhysNodes : ",i); for(int j=0; j const & Pn_elemToNode, FieldContainer & P1_elemToNode) { /* Main idea: For each P=n element Create n^2 P1 elements For each of those P1 elements Create element to node map */ int Pn_numElems = Pn_elemToNode.dimension(0); int p1ElemCtr=0; for (int i=0; i const &elemToNode, FieldContainer const &nodeCoord, FieldContainer const &cubPoints, FieldContainer const &cubWeights, RCP > >&myBasis_rcp, FieldContainer const &HGBGrads, FieldContainer const &HGBValues, std::vector const &globalNodeIds, crs_matrix_type &StiffMatrix, RCP &rhsVector, std::string &msg ) { int numCubPoints = cubPoints.dimension(0); int cubDim = cubPoints.dimension(1); int spaceDim = nodeCoord.dimension(1); int numFieldsG = HGBGrads.dimension(0); long long numElems = elemToNode.dimension(0); int numNodesPerElem = elemToNode.dimension(1); RCP tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Allocate arrays"))); std::cout << "CreateLinearSystem:" << std::endl; std::cout << " numCubPoints = " << numCubPoints << std::endl; std::cout << " cubDim = " << cubDim << std::endl; std::cout << " spaceDim = " << spaceDim << std::endl; std::cout << " numFieldsG = " << numFieldsG << std::endl; std::cout << " numElems = " << numElems << std::endl; std::cout << " numNodesPerElem = " << numNodesPerElem << std::endl; std::cout << " length(globalNodeIds) = " << globalNodeIds.size() << std::endl; std::cout << " length(nodeCoord) = " << nodeCoord.dimension(0) << std::endl; if(nodeCoord.dimension(0) != Teuchos::as(globalNodeIds.size())) { std::ostringstream errStr; errStr << "Error: CreateLinearSystem: length of coordinates != #nodes (" << nodeCoord.dimension(0) << "!=" << globalNodeIds.size() << ")"; throw std::runtime_error(errStr.str()); } for(int workset = 0; workset < numWorksets; workset++){ // Compute cell numbers where the workset starts and ends int worksetSize = 0; int worksetBegin = (workset + 0)*desiredWorksetSize; int worksetEnd = (workset + 1)*desiredWorksetSize; // When numElems is not divisible by desiredWorksetSize, the last workset ends at numElems worksetEnd = (worksetEnd <= numElems) ? worksetEnd : numElems; // Now we know the actual workset size and can allocate the array for the cell nodes worksetSize = worksetEnd - worksetBegin; //std::cout << " cellWorkset: begin=" << worksetBegin << ", end=" << worksetEnd // << ", size=" << worksetSize << std::endl; FieldContainer cellWorkset(worksetSize, numNodesPerElem, spaceDim); // Copy coordinates into cell workset int cellCounter = 0; for(int cell = worksetBegin; cell < worksetEnd; cell++){ for (int node = 0; node < numNodesPerElem; node++) { cellWorkset(cellCounter, node, 0) = nodeCoord( elemToNode(cell, node), 0); cellWorkset(cellCounter, node, 1) = nodeCoord( elemToNode(cell, node), 1); } cellCounter++; } /**********************************************************************************/ /* Allocate arrays */ /**********************************************************************************/ // Containers for Jacobians, integration measure & cubature points in workset cells FieldContainer worksetJacobian (worksetSize, numCubPoints, spaceDim, spaceDim); FieldContainer worksetJacobInv (worksetSize, numCubPoints, spaceDim, spaceDim); FieldContainer worksetJacobDet (worksetSize, numCubPoints); FieldContainer worksetCubWeights(worksetSize, numCubPoints); FieldContainer worksetCubPoints (worksetSize, numCubPoints, cubDim); // Containers for basis values transformed to workset cells and them multiplied by cubature weights FieldContainer worksetHGBValues (worksetSize, numFieldsG, numCubPoints); FieldContainer worksetHGBValuesWeighted(worksetSize, numFieldsG, numCubPoints); FieldContainer worksetHGBGrads (worksetSize, numFieldsG, numCubPoints, spaceDim); FieldContainer worksetHGBGradsWeighted (worksetSize, numFieldsG, numCubPoints, spaceDim); // Containers for diffusive & advective fluxes & non-conservative adv. term and reactive terms FieldContainer worksetDiffusiveFlux(worksetSize, numFieldsG, numCubPoints, spaceDim); // Containers for material values and source term. Require user-defined functions FieldContainer worksetMaterialVals (worksetSize, numCubPoints, spaceDim, spaceDim); FieldContainer worksetSourceTerm (worksetSize, numCubPoints); // Containers for workset contributions to the discretization matrix and the right hand side FieldContainer worksetStiffMatrix (worksetSize, numFieldsG, numFieldsG); FieldContainer worksetRHS (worksetSize, numFieldsG); tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Calculate Jacobians"))); /**********************************************************************************/ /* Calculate Jacobians */ /**********************************************************************************/ // Due to the way setJacobian works, we need to use the basis form here // IntrepidCTools::setJacobian(worksetJacobian, cubPoints, cellWorkset, cellType); IntrepidCTools::setJacobian(worksetJacobian, cubPoints, cellWorkset, myBasis_rcp); IntrepidCTools::setJacobianInv(worksetJacobInv, worksetJacobian ); IntrepidCTools::setJacobianDet(worksetJacobDet, worksetJacobian ); //#define DEBUG_OUTPUT #ifdef DEBUG_OUTPUT printf("*** cubPoints ***\n"); for(int j=0; j(worksetHGBGrads, // DF^{-T}(grad u) worksetJacobInv, HGBGrads); // Compute integration measure for workset cells: IntrepidFSTools::computeCellMeasure(worksetCubWeights, // Det(DF)*w = J*w worksetJacobDet, cubWeights); // Multiply transformed (workset) gradients with weighted measure IntrepidFSTools::multiplyMeasure(worksetHGBGradsWeighted, // DF^{-T}(grad u)*J*w worksetCubWeights, worksetHGBGrads); // Compute the diffusive flux: IntrepidFSTools::tensorMultiplyDataField(worksetDiffusiveFlux, // A*(DF^{-T}(grad u) worksetMaterialVals, worksetHGBGrads); // Integrate to compute workset diffusion contribution to global matrix: IntrepidFSTools::integrate(worksetStiffMatrix, //(DF^{-T}(grad u)*J*w)*(A*DF^{-T}(grad u)) worksetHGBGradsWeighted, worksetDiffusiveFlux, COMP_BLAS); /**********************************************************************************/ /* Compute RHS */ /**********************************************************************************/ tm.reset(); tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Compute right-hand side"))); // Transform basis values to physical frame: IntrepidFSTools::HGRADtransformVALUE(worksetHGBValues, // clones basis values (u) HGBValues); // Multiply transformed (workset) values with weighted measure IntrepidFSTools::multiplyMeasure(worksetHGBValuesWeighted, // (u)*J*w worksetCubWeights, worksetHGBValues); // Integrate worksetSourceTerm against weighted basis function set IntrepidFSTools::integrate(worksetRHS, // f.(u)*J*w worksetSourceTerm, worksetHGBValuesWeighted, COMP_BLAS); /**********************************************************************************/ /***************************** STATISTICS (Part II) ******************************/ /**********************************************************************************/ for(int i=0; i vals1(1); Array cols1(1); for(int cell = worksetBegin; cell < worksetEnd; cell++){ // Compute cell ordinal relative to the current workset int worksetCellOrdinal = cell - worksetBegin; // "CELL EQUATION" loop for the workset cell: cellRow is relative to the cell DoF numbering for (int cellRow = 0; cellRow < numFieldsG; cellRow++){ int localRow = elemToNode(cell, cellRow); int globalRow = globalNodeIds[localRow]; double sourceTermContribution = worksetRHS(worksetCellOrdinal, cellRow); rhsVector->sumIntoGlobalValue(globalRow, 0, sourceTermContribution); // "CELL VARIABLE" loop for the workset cell: cellCol is relative to the cell DoF numbering for (int cellCol = 0; cellCol < numFieldsG; cellCol++){ int localCol = elemToNode(cell, cellCol); int globalCol = globalNodeIds[localCol]; double operatorMatrixContribution = worksetStiffMatrix(worksetCellOrdinal, cellRow, cellCol); cols1[0] = globalCol; vals1[0] = operatorMatrixContribution; StiffMatrix.insertGlobalValues(globalRow, cols1(), vals1()); // printf("Inserting A(%d,%d) = %6.4e\n",(int)globalCol,(int)cols1[0],vals1[0]); }// *** cell col loop *** }// *** cell row loop *** }// *** workset cell loop ** }// *** workset loop *** StiffMatrix.fillComplete(); /**********************************************************************************/ /***************************** STATISTICS (Part IIb) ******************************/ /**********************************************************************************/ Teuchos::RCP gl_StiffGraph = StiffMatrix.getCrsGraph(); Teuchos::RCP rowMap = gl_StiffGraph->getRowMap(); Teuchos::RCP coordsOwnedPlusShared; RCP nCoord= rcp(new multivector_type(rowMap,2)); { auto nCoordD = nCoord->get2dViewNonConst(); for (int inode=0; inodegetImporter().is_null())) { coordsOwnedPlusShared = rcp(new multivector_type(gl_StiffGraph->getColMap(), 3, true)); coordsOwnedPlusShared->doImport(*nCoord, *gl_StiffGraph->getImporter(), Tpetra::CombineMode::ADD, false); } else { coordsOwnedPlusShared = nCoord; } vector_type laplDiagOwned(rowMap, true); typename crs_matrix_type::local_inds_host_view_type indices; typename crs_matrix_type::values_host_view_type values; size_t numOwnedRows = rowMap->getLocalNumElements(); for (size_t row=0; row laplDiagOwnedPlusShared; if (!gl_StiffGraph->getImporter().is_null()) { laplDiagOwnedPlusShared = rcp(new vector_type(gl_StiffGraph->getColMap(), true)); laplDiagOwnedPlusShared->doImport(laplDiagOwned, *gl_StiffGraph->getImporter(), Tpetra::CombineMode::ADD, false); } else { laplDiagOwnedPlusShared = rcp(&laplDiagOwned, false); } for (size_t row=0; rowgetData()[row]*laplDiagOwnedPlusShared->getData()[col]); double aij = laplVal * laplVal; double ratio = sqrt(aij / aiiajj); local_stat_max[7] = std::max(local_stat_max[7], ratio); local_stat_min[7] = std::min(local_stat_min[7], ratio); local_stat_sum[7] += ratio; } } } //CreateLinearSystem /*********************************************************************************************************/ void GenerateIdentityCoarsening_pn_to_p1(const FieldContainer & Pn_elemToNode, RCP const & P1_map_aux, RCP const &Pn_map, RCP & P, RCP & R) { // Generate prolongator matrix that will be used to transfer from P1 on auxiliary mesh to Pn on base mesh. // It's just the identity matrix. // By construction, the node numbering on the P1 auxiliary mesh is the same as on the Pn base mesh // (see CreateP1MeshFromPnMesh). The P2 map is the range map, the P1 auxiliary map is the domain map. double one = 1.0; P = rcp(new crs_matrix_type(Pn_map,1)); //We must keep track of the nodes already encountered. Inserting more than once will cause //the values to be summed. Using a hashtable would work -- we abuse std::map for this purpose. std::map hashTable; int Nelem=Pn_elemToNode.dimension(0); Array vals1(1); vals1[0] = one; Array cols1(1); for(int i=0; iinsertGlobalValues(row,cols1(),vals1()); hashTable[row] = 1; } } } P->fillComplete(P1_map_aux,Pn_map); /* //JJH FIXME no need to generate R ... it's the identity, stupid hashTable.clear(); R = rcp(new crs_matrix_type(P1_map_aux,1)); Nelem = P1_elemToNode.dimension(0); int nodesPerElem = P1_elemToNode.dimension(1); for(int i=0; iinsertGlobalValues(row,cols1(),vals1()); hashTable[row] = 1; } } } R->fillComplete(Pn_map,P1_map_aux); */ } /*********************************************************************************************************/ void Apply_Dirichlet_BCs(std::vector &BCNodes, crs_matrix_type & A, multivector_type & x, multivector_type & b, const multivector_type & soln) { int N=(int)BCNodes.size(); ArrayRCP xdata = x.getDataNonConst(0); ArrayRCP bdata = b.getDataNonConst(0); ArrayRCP solndata = soln.getData(0); A.resumeFill(); for(int i=0; i