// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file data.hpp \brief Generates and manages data for the Poisson example, including all mesh and discretization data, matrices, etc. */ #ifndef ROL_PDEOPT_PDE_FEM_H #define ROL_PDEOPT_PDE_FEM_H #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_TimeMonitor.hpp" #include "Tpetra_MultiVector.hpp" #include "Tpetra_Vector.hpp" #include "Tpetra_CrsGraph.hpp" #include "Tpetra_CrsMatrix.hpp" #include "Tpetra_Version.hpp" #include "Tpetra_RowMatrixTransposer.hpp" #include "MatrixMarket_Tpetra.hpp" #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp" #include "Intrepid_DefaultCubatureFactory.hpp" #include "Intrepid_FunctionSpaceTools.hpp" #include "Intrepid_CellTools.hpp" #include "Amesos2.hpp" #include "./dofmanager.hpp" // Global Timers. #ifdef ROL_TIMERS ROL::Ptr FactorTime_example_PDEOPT_TOOLS_PDEFEM_GLOB = Teuchos::TimeMonitor::getNewCounter("ROL: Factorization Time in PDEFEM"); ROL::Ptr LUSubstitutionTime_example_PDEOPT_TOOLS_PDEFEM_GLOB = Teuchos::TimeMonitor::getNewCounter("ROL: LU Substitution Time in PDEFEM"); ROL::Ptr SolverUpdateTime_example_PDEOPT_TOOLS_PDEFEM_GLOB = Teuchos::TimeMonitor::getNewCounter("ROL: Solver Update Time in PDEFEM"); ROL::Ptr LocalAssemblyTime_example_PDEOPT_TOOLS_PDEFEM_GLOB = Teuchos::TimeMonitor::getNewCounter("ROL: Local Assembly Time in PDEFEM"); ROL::Ptr ConstraintDerivativeTime_example_PDEOPT_TOOLS_PDEFEM_GLOB = Teuchos::TimeMonitor::getNewCounter("ROL: Constraint Derivative Application Time in PDEFEM"); #endif template class PDE_FEM { private: using GO = typename Tpetra::Map<>::global_ordinal_type; protected: ROL::Ptr > meshMgr_; ROL::Ptr > dofMgr_; std::vector > > > basisPtrs_; ROL::Ptr > > coord_iface_; Intrepid::FieldContainer cellDofs_; Teuchos::RCP parlist_; ROL::Ptr > commPtr_; ROL::Ptr outStream_; int numLocalDofs_; Intrepid::FieldContainer ctn_; Intrepid::FieldContainer cte_; int myRank_; int numProcs_; Real alpha_; int basisOrder_; ROL::Ptr > myCellMap_; ROL::Ptr > myOverlapMap_; ROL::Ptr > myUniqueMap_; ROL::Ptr > matGraph_; ROL::Ptr > matA_; ROL::Ptr > matA_dirichlet_; ROL::Ptr > matA_dirichlet_trans_; ROL::Ptr > matM_; ROL::Ptr > matM_dirichlet_; ROL::Ptr > matM_dirichlet_trans_; ROL::Ptr > vecUd_; ROL::Ptr > vecF_; ROL::Ptr > vecF_overlap_; ROL::Ptr > vecF_dirichlet_; Teuchos::Array myCellMeasure_; Teuchos::Array myCellIds_; // Elements on Boundary std::vector > myBoundaryCellIds_; // DBC Teuchos::Array myDirichletDofs_; // BC Sides std::vector my_dbc_; std::vector my_nbc_; //Point load on Bundary! std::vector myPointLoadDofs_; std::vector myPointLoadVals_; ROL::Ptr, Tpetra::MultiVector<> > > solverA_; ROL::Ptr, Tpetra::MultiVector<> > > solverA_trans_; shards::CellTopology cellType_; int sideDim_; int spaceDim_; int numNodesPerCell_; int numCubPoints_; int lfs_; GO totalNumCells_; GO totalNumDofs_; GO numCells_; ROL::Ptr > cubPoints_; ROL::Ptr > cubWeights_; ROL::Ptr > cellNodes_; ROL::Ptr > cellJac_; ROL::Ptr > cellJacInv_; ROL::Ptr > cellJacDet_; ROL::Ptr > cellWeightedMeasure_; ROL::Ptr > valReference_; ROL::Ptr > gradReference_; ROL::Ptr > valPhysical_; ROL::Ptr > gradPhysical_; ROL::Ptr > valPhysicalWeighted_; ROL::Ptr > gradPhysicalWeighted_; ROL::Ptr > gradgradMats_; ROL::Ptr > valvalMats_; ROL::Ptr > cubPointsPhysical_; ROL::Ptr > dataF_; ROL::Ptr > datavalVecF_; ROL::Ptr > dofPoints_; ROL::Ptr > dofPointsPhysical_; ROL::Ptr > dataUd_; bool verbose_; protected: public: // constructor //PDE_FEM() {} PDE_FEM() {} // destructor virtual ~PDE_FEM() {} virtual void Initialize(const ROL::Ptr > &comm, const Teuchos::RCP &parlist, const ROL::Ptr &outStream) { commPtr_ = comm; parlist_ = parlist; outStream_ = outStream; myRank_ = comm->getRank(); numProcs_ = comm->getSize(); verbose_ = parlist->sublist("PDE FEM").get("Verbose Output",false); if ( verbose_ ) { *outStream_ << "Total number of processors: " << numProcs_ << std::endl; } basisOrder_ = parlist->sublist("PDE FEM").get("Order of FE Discretization"); } void SetDiscretization(const ROL::Ptr > &meshMgr, const std::vector > > > &basisPtrs) { meshMgr_ = meshMgr; totalNumCells_ = meshMgr_->getNumCells(); basisPtrs_ = basisPtrs; // Retrieve some basic cell information. cellType_ = (basisPtrs_[0])->getBaseCellTopology(); // get the cell type from any basis spaceDim_ = cellType_.getDimension(); // retrieve spatial dimension numNodesPerCell_ = cellType_.getNodeCount(); // retrieve number of nodes per cell sideDim_ = spaceDim_ - 1; coord_iface_ = ROL::dynamicPtrCast > >(basisPtrs_[0]); dofMgr_ = ROL::makePtr>(meshMgr_, basisPtrs_); } virtual void SetParallelStructure(void) { int cellSplit = parlist_->sublist("Geometry").get("Partition type"); /****************************************************/ /*** Build parallel communication infrastructure. ***/ /****************************************************/ // Partition the cells in the mesh. We use a basic quasi-equinumerous partitioning, // where the remainder, if any, is assigned to the last processor. Teuchos::Array myGlobIds_; Teuchos::Array cellOffsets_(numProcs_, 0); GO cellsPerProc = totalNumCells_ / numProcs_; numCells_ = cellsPerProc; switch(cellSplit) { case 0: if (myRank_ == 0) { // remainder in the first numCells_ += totalNumCells_ % numProcs_; } for (int i=1; i(i==1))*(totalNumCells_ % numProcs_); } break; case 1: if (myRank_ == numProcs_-1) { // remainder in the last numCells_ += totalNumCells_ % numProcs_; } for (int i=1; i(i-1<(totalNumCells_%numProcs_))); } break; } cellDofs_ = *(dofMgr_->getCellDofs()); numLocalDofs_ = cellDofs_.dimension(1); if ( verbose_ ) { *outStream_ << "Cell offsets across processors: " << cellOffsets_ << std::endl; } for (int i=0; i>(Teuchos::OrdinalTraits::invalid(), myGlobIds_, 0, commPtr_); //std::cout << std::endl << myOverlapMap_->getLocalElementList()<(myGlobIds_, commPtr_); to build the overlap map. **/ myUniqueMap_ = Tpetra::createOneToOne(myOverlapMap_); //std::cout << std::endl << myUniqueMap_->getLocalElementList() << std::endl; myCellMap_ = ROL::makePtr>(Teuchos::OrdinalTraits::invalid(), this->myCellIds_, 0, this->commPtr_); } virtual void SetUpLocalIntrepidArrays(void) { /****************************************************/ /*** Set up local discretization data and arrays. ***/ /****************************************************/ // Cubature data. Intrepid::DefaultCubatureFactory cubFactory; // create cubature factory int cubDegree = 4; // set cubature degree, e.g., 2 ROL::Ptr > cellCub = cubFactory.create(cellType_, cubDegree); // create default cubature numCubPoints_ = cellCub->getNumPoints(); // retrieve number of cubature points lfs_ = dofMgr_->getLocalFieldSize(0); // Discretization data. cubPoints_ = ROL::makePtr>(numCubPoints_, spaceDim_); cubWeights_ = ROL::makePtr>(numCubPoints_); cubPointsPhysical_ = ROL::makePtr>(numCells_, numCubPoints_, spaceDim_); dofPoints_ = ROL::makePtr>(lfs_, spaceDim_); dofPointsPhysical_ = ROL::makePtr>(numCells_, lfs_, spaceDim_); cellNodes_ = ROL::makePtr>(numCells_, numNodesPerCell_, spaceDim_); cellJac_ = ROL::makePtr>(numCells_, numCubPoints_, spaceDim_, spaceDim_); cellJacInv_ = ROL::makePtr>(numCells_, numCubPoints_, spaceDim_, spaceDim_); cellJacDet_ = ROL::makePtr>(numCells_, numCubPoints_); cellWeightedMeasure_ = ROL::makePtr>(numCells_, numCubPoints_); valReference_ = ROL::makePtr>(lfs_, numCubPoints_); gradReference_ = ROL::makePtr>(lfs_, numCubPoints_, spaceDim_); valPhysical_ = ROL::makePtr>(numCells_, lfs_, numCubPoints_); gradPhysical_ = ROL::makePtr>(numCells_, lfs_, numCubPoints_, spaceDim_); valPhysicalWeighted_ = ROL::makePtr>(numCells_, lfs_, numCubPoints_); gradPhysicalWeighted_ = ROL::makePtr>(numCells_, lfs_, numCubPoints_, spaceDim_); // Geometric definition of the cells in the mesh, based on the cell-to-node map and the domain partition. Intrepid::FieldContainer &nodes = *meshMgr_->getNodes(); ctn_ = *meshMgr_->getCellToNodeMap(); for (int i=0; igetCubature(*cubPoints_, *cubWeights_); // retrieve cubature points and weights (*basisPtrs_[0]).getValues(*gradReference_, *cubPoints_, Intrepid::OPERATOR_GRAD); // evaluate grad operator at cubature points (*basisPtrs_[0]).getValues(*valReference_, *cubPoints_, Intrepid::OPERATOR_VALUE); // evaluate value operator at cubature points Intrepid::CellTools::setJacobian(*cellJac_, *cubPoints_, *cellNodes_, cellType_); // compute cell Jacobians Intrepid::CellTools::setJacobianInv(*cellJacInv_, *cellJac_); // compute inverses of cell Jacobians Intrepid::CellTools::setJacobianDet(*cellJacDet_, *cellJac_); // compute determinants of cell Jacobians Intrepid::FunctionSpaceTools::computeCellMeasure(*cellWeightedMeasure_, // compute weighted cell measure *cellJacDet_, *cubWeights_); Intrepid::FunctionSpaceTools::HGRADtransformGRAD(*gradPhysical_, // transform reference gradients into physical space *cellJacInv_, *gradReference_); Intrepid::FunctionSpaceTools::multiplyMeasure(*gradPhysicalWeighted_, // multiply with weighted measure *cellWeightedMeasure_, *gradPhysical_); Intrepid::FunctionSpaceTools::HGRADtransformVALUE(*valPhysical_, // transform reference values into physical space *valReference_); Intrepid::FunctionSpaceTools::multiplyMeasure(*valPhysicalWeighted_, // multiply with weighted measure *cellWeightedMeasure_, *valPhysical_); Intrepid::CellTools::mapToPhysicalFrame(*cubPointsPhysical_, // map reference cubature points to physical space *cubPoints_, *cellNodes_, cellType_); ComputeCellTotalMeasures(); } virtual void ComputeCellTotalMeasures(void) { for (int i=0; i cellDofsArrayRCP = cellDofs_.getData(); Teuchos::Array graphEntriesPerRow(myUniqueMap_->getLocalNumElements()); for (GO i=0; iisNodeGlobalElement(gid)) graphEntriesPerRow[myUniqueMap_->getLocalElement(gid)] += numLocalDofs_; } } matGraph_ = ROL::makePtr>(myUniqueMap_, graphEntriesPerRow()); for (GO i=0; iinsertGlobalIndices(cellDofs_(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i]*numLocalDofs_, numLocalDofs_)); } } matGraph_->fillComplete(); } virtual void AssembleSystemMats(void) { /****************************************/ /*** Assemble global data structures. ***/ /****************************************/ // Assemble matrices. // Stiffness matrix A. matA_ = ROL::makePtr>(matGraph_); matA_dirichlet_ = ROL::makePtr>(matGraph_); int numLocalMatEntries = numLocalDofs_ * numLocalDofs_; Teuchos::ArrayRCP cellDofsArrayRCP = cellDofs_.getData(); Teuchos::ArrayRCP gradgradArrayRCP = gradgradMats_->getData(); for (int i=0; isumIntoGlobalValues(cellDofs_(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i] * numLocalDofs_, numLocalDofs_), gradgradArrayRCP(i*numLocalMatEntries+j*numLocalDofs_, numLocalDofs_)); matA_dirichlet_->sumIntoGlobalValues(cellDofs_(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i] * numLocalDofs_, numLocalDofs_), gradgradArrayRCP(i*numLocalMatEntries+j*numLocalDofs_, numLocalDofs_)); } } matA_->fillComplete(); matA_dirichlet_->fillComplete(); // Mass matrix M. /* matM_ = ROL::makePtr>(matGraph_); matM_dirichlet_ = ROL::makePtr>(matGraph_); Teuchos::ArrayRCP valvalArrayRCP = valvalMats_->getData(); for (int i=0; isumIntoGlobalValues(cellDofs_(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i]*numLocalDofs_, numLocalDofs_), valvalArrayRCP(i*numLocalMatEntries+j*numLocalDofs_, numLocalDofs_)); matM_dirichlet_->sumIntoGlobalValues(cellDofs_(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i]*numLocalDofs_, numLocalDofs_), valvalArrayRCP(i*numLocalMatEntries+j*numLocalDofs_, numLocalDofs_)); } } matM_->fillComplete(); matM_dirichlet_->fillComplete(); */ } virtual void AssembleRHSVector(void) { // Assemble vectors. // vecF_ requires assembly using vecF_overlap_ and redistribution vecF_ = ROL::makePtr>(matA_->getRangeMap(), 1, true); vecF_dirichlet_ = ROL::makePtr>(matA_->getRangeMap(), 1, true); vecF_overlap_ = ROL::makePtr>(myOverlapMap_, 1, true); // assembly on the overlap map for (int i=0; isumIntoGlobalValue(cellDofs_(myCellIds_[i],j), 0, (*datavalVecF_)[i*numLocalDofs_+j]); } } //Assemble the point loads! for (unsigned i=0; isumIntoGlobalValue(myPointLoadDofs_[i], 0, myPointLoadVals_[i]); } // change map Tpetra::Export<> exporter(vecF_overlap_->getMap(), vecF_->getMap()); // redistribution vecF_->doExport(*vecF_overlap_, exporter, Tpetra::ADD); // from the overlap map to the unique map vecF_dirichlet_->doExport(*vecF_overlap_, exporter, Tpetra::ADD); // from the overlap map to the unique map } virtual void AssembleDataVector(void) { // vecUd_ does not require assembly vecUd_ = ROL::makePtr>(matA_->getDomainMap(), 1, true); for (int i=0; igetMap()->isNodeGlobalElement(cellDofs_(myCellIds_[i],j))) { vecUd_->replaceGlobalValue(cellDofs_(myCellIds_[i],j), 0, (*dataUd_)[i*numLocalDofs_+j]); } } } } // find the local index of a global cell virtual int find_local_index(GO globalCell) { return myCellMap_->getLocalElement(globalCell); /* for(int i=0; i dbc_side) { if(ifUseCoordsToSpecifyBC){ my_dbc_.resize(4); my_dbc_ = {0, 1, 2, 3}; }else{ my_dbc_ = dbc_side; } //Print to check my BC info if ( verbose_ ) { *outStream_ << "My dbc numbers: "; for(unsigned i=0; i > > > dirichletSideSets = meshMgr_->getSideSets(); std::vector > > &dss = *dirichletSideSets; Teuchos::Array mySortedCellIds_(myCellIds_); std::sort(mySortedCellIds_.begin(), mySortedCellIds_.end()); mySortedCellIds_.erase( std::unique(mySortedCellIds_.begin(), mySortedCellIds_.end()), mySortedCellIds_.end() ); myBoundaryCellIds_.resize(dss[0].size()); for (int i=0; i(dss[0].size()); ++i) { for (int j=0; jgetCellToEdgeMap()); Intrepid::FieldContainer &nodeDofs = *(dofMgr_->getNodeDofs()); //Intrepid::FieldContainer &edgeDofs = *(dofMgr_->getEdgeDofs()); std::vector > dofTags = (basisPtrs_[0])->getAllDofTags(); int numDofsPerNode = 0; int numDofsPerEdge = 0; for (int j=0; j<(basisPtrs_[0])->getCardinality(); ++j) { if (dofTags[j][0] == 0) { numDofsPerNode = dofTags[j][3]; } if (dofTags[j][0] == 1) { numDofsPerEdge = dofTags[j][3]; } } // vector field int nfields = basisPtrs_.size(); numDofsPerNode = numDofsPerNode * nfields; numDofsPerEdge = numDofsPerEdge * nfields; Intrepid::FieldContainer &nodes = *meshMgr_->getNodes(); int n_dbc = my_dbc_.size(); for (int i=0; i(myBoundaryCellIds_.size()); ++i) { bool isdbc = false; for(int i_dbc = 0; i_dbc < n_dbc; i_dbc++) { if(i == my_dbc_[i_dbc]) { isdbc = true; break; } } if(!isdbc) continue; for (int j=0; j x(spaceDim_); const CellTopologyData * ctd = cellType_.getCellTopologyData(); Teuchos::ArrayView locNodes(const_cast(ctd->subcell[spaceDim_-1][i].node), cellType_.getVertexCount(spaceDim_-1, i)); for (int l=0; l(cellType_.getVertexCount(spaceDim_-1, i)); ++l) { x[0]=nodes(ctn_(myBoundaryCellIds_[i][j], locNodes[l]), 0); x[1]=nodes(ctn_(myBoundaryCellIds_[i][j], locNodes[l]), 1); // use coordinates to check if a DOF is DBC DOF int ifDBCNode = check_DBC_Coords_Range( x ); if(ifDBCNode < 0){ continue; } else if(ifDBCNode == 0){ if ( verbose_ ) { *outStream_ << "DBC node: " << ctn_(myBoundaryCellIds_[i][j], locNodes[l]) << ", fixing X direction" << std::endl; } myDirichletDofs_.push_back(nodeDofs(ctn_(myBoundaryCellIds_[i][j], locNodes[l]), 0)); } else if(ifDBCNode == 1 && numDofsPerNode >= 2){ if ( verbose_ ) { *outStream_ << "DBC node: " << ctn_(myBoundaryCellIds_[i][j], locNodes[l]) << ", fixing Y direction" << std::endl; } myDirichletDofs_.push_back(nodeDofs(ctn_(myBoundaryCellIds_[i][j], locNodes[l]), 1)); } else { if ( verbose_ ) { *outStream_ << "DBC node: " << ctn_(myBoundaryCellIds_[i][j], locNodes[l]) << ", fixing ALL direction" << std::endl; } for (int k=0; k &x ) const { // return value : // -1: not a DBC node // 0: x direction fixed // 1: y direction fixed // 5: both x, y direction are fixed return 5; } // // // virtual void process_loading_information(const Teuchos::RCP &parlist) {} // //note that the point load is only allowed to applied on the boundary, not inside the body! 2D only virtual void check_and_Apply_PointLoad_By_Coords(int globalCellNum, int pointload_bc) { Intrepid::FieldContainer &nodes = *meshMgr_->getNodes(); const CellTopologyData * ctd = cellType_.getCellTopologyData(); Teuchos::ArrayView locNodes(const_cast(ctd->subcell[spaceDim_-1][pointload_bc].node), cellType_.getVertexCount(spaceDim_-1, pointload_bc)); std::vector x1(spaceDim_); std::vector x2(spaceDim_); std::vector localNodeNum(2); x1[0]=nodes(ctn_(globalCellNum, locNodes[0]), 0); x1[1]=nodes(ctn_(globalCellNum, locNodes[0]), 1); x2[0]=nodes(ctn_(globalCellNum, locNodes[1]), 0); x2[1]=nodes(ctn_(globalCellNum, locNodes[1]), 1); ApplyPointLoad(pointload_bc, globalCellNum, localNodeNum, x1, x2); } virtual void ApplyPointLoad(const int pointload_bc, const GO globalCellNum, const std::vector &localNodeNum, const std::vector &coord1, const std::vector &coord2) { //Intrepid::FieldContainer &nodeDofs = *(dofMgr_->getNodeDofs()); //bool isLoadPosContainedInCurrentSegment = false; //int whichNodeIsCloserToPos = -1; return; } // // // virtual void EnforceDBC(void) { // Apply Dirichlet conditions. // zero out row and column, make matrix symmetric //ROL::Ptr node = matA_->getNode(); //matA_dirichlet_ = matA_->clone(node); //matM_dirichlet_ = matM_->clone(node); //vecF_dirichlet_ = ROL::makePtr>(matA_->getRangeMap(), 1, true); //Tpetra::deep_copy(*vecF_dirichlet_, *vecF_); matA_dirichlet_->resumeFill(); //matM_dirichlet_->resumeFill(); GO gDof; for(int i=0; iisNodeGlobalElement(gDof) && check_myGlobalDof_on_boundary(gDof)) { size_t numRowEntries = matA_dirichlet_->getNumEntriesInGlobalRow(gDof); Teuchos::Array indices(numRowEntries, 0); Teuchos::Array values(numRowEntries, 0); Teuchos::Array canonicalValues(numRowEntries, 0); Teuchos::Array zeroValues(numRowEntries, 0); matA_dirichlet_->getGlobalRowCopy(gDof, indices, values, numRowEntries); //matM_dirichlet_->getGlobalRowCopy(gDof, indices, values, numRowEntries); for (int k=0; kreplaceGlobalValues(gDof, indices, canonicalValues); //matM_dirichlet_->replaceGlobalValues(gDof, indices, zeroValues); vecF_dirichlet_->replaceGlobalValue (gDof, 0, 0); } if (!check_myGlobalDof_on_boundary(gDof)) { size_t numDBCDofs = myDirichletDofs_.size(); Teuchos::Array indices(numDBCDofs, 0); Teuchos::Array zeroValues(numDBCDofs, 0); for (int k=0; kreplaceGlobalValues(gDof, indices, zeroValues); //matM_dirichlet_->replaceGlobalValues(gDof, indices, zeroValues); } } } matA_dirichlet_->fillComplete(); //matM_dirichlet_->fillComplete(); // GenerateTransposedMats(); // ConstructSolvers(); // ConstructAdjointSolvers(); } // virtual void MatrixRemoveDBC(void) { // // Apply Dirichlet conditions. // // zero out row and column, make matrix symmetric // //ROL::Ptr node = matA_->getNode(); // //matA_dirichlet_ = matA_->clone(node); // //matM_dirichlet_ = matM_->clone(node); // // matA_dirichlet_->resumeFill(); // matM_dirichlet_->resumeFill(); // // GO gDof; // for(int i=0; iisNodeGlobalElement(gDof) && check_myGlobalDof_on_boundary(gDof)) { // size_t numRowEntries = matA_dirichlet_->getNumEntriesInGlobalRow(gDof); // Teuchos::Array indices(numRowEntries, 0); // Teuchos::Array values(numRowEntries, 0); // Teuchos::Array canonicalValues(numRowEntries, 0); // Teuchos::Array zeroValues(numRowEntries, 0); // matA_dirichlet_->getGlobalRowCopy(gDof, indices, values, numRowEntries); // matM_dirichlet_->getGlobalRowCopy(gDof, indices, values, numRowEntries); // for (int k=0; kreplaceGlobalValues(gDof, indices, canonicalValues); // matM_dirichlet_->replaceGlobalValues(gDof, indices, zeroValues); // } // // if (!check_myGlobalDof_on_boundary(gDof)) { // size_t numDBCDofs = myDirichletDofs_.size(); // Teuchos::Array indices(numDBCDofs, 0); // Teuchos::Array zeroValues(numDBCDofs, 0); // for (int k=0; kreplaceGlobalValues(gDof, indices, zeroValues); // matM_dirichlet_->replaceGlobalValues(gDof, indices, zeroValues); // } // } // } // matA_dirichlet_->fillComplete(); // matM_dirichlet_->fillComplete(); // } virtual void MatrixRemoveDBC(void) { // Apply Dirichlet conditions. // zero out row and column, make matrix symmetric matA_dirichlet_->resumeFill(); //matM_dirichlet_->resumeFill(); for (GO i=0; iisNodeGlobalElement(myDirichletDofs_[i])) { size_t numRowEntries = matA_dirichlet_->getNumEntriesInGlobalRow(myDirichletDofs_[i]); Teuchos::Array indices(numRowEntries, 0); Teuchos::Array values(numRowEntries, 0); Teuchos::Array canonicalValues(numRowEntries, 0); Teuchos::Array zeroValues(numRowEntries, 0); matA_dirichlet_->getGlobalRowCopy(myDirichletDofs_[i], indices, values, numRowEntries); //matM_dirichlet_->getGlobalRowCopy(myDirichletDofs_[i], indices, values, numRowEntries); for (int j=0; j < static_cast(indices.size()); ++j) { if (myDirichletDofs_[i] == indices[j]) { canonicalValues[j] = 1.0; } } matA_dirichlet_->replaceGlobalValues(myDirichletDofs_[i], indices, canonicalValues); //matM_dirichlet_->replaceGlobalValues(myDirichletDofs_[i], indices, zeroValues); for (int j=0; j < static_cast(indices.size()); ++j) { Teuchos::Array ind(1, myDirichletDofs_[i]); Teuchos::Array valA(1, canonicalValues[j]); Teuchos::Array valM(1, zeroValues[j]); matA_dirichlet_->replaceGlobalValues(indices[j], ind, valA); //matM_dirichlet_->replaceGlobalValues(indices[j], ind, valM); } } } matA_dirichlet_->fillComplete(); //matM_dirichlet_->fillComplete(); // GenerateTransposedMats(); // ConstructSolvers(); // ConstructAdjointSolvers(); } virtual void VectorRemoveDBC(void) { // Apply Dirichlet conditions. //vecF_dirichlet_ = ROL::makePtr>(matA_->getRangeMap(), 1, true); //Tpetra::deep_copy(*vecF_dirichlet_, *vecF_); GO gDof(0); for(GO i=0; iisNodeGlobalElement(gDof) && check_myGlobalDof_on_boundary(gDof)) { vecF_dirichlet_->replaceGlobalValue (gDof, 0, 0); } } } } virtual void GenerateTransposedMats() { // Create matrix transposes. Tpetra::RowMatrixTransposer<> transposerA(matA_dirichlet_); //Tpetra::RowMatrixTransposer<> transposerM(matM_dirichlet_); matA_dirichlet_trans_ = transposerA.createTranspose(); //matM_dirichlet_trans_ = transposerM.createTranspose(); } virtual void ConstructSolvers(void) { // Construct solver using Amesos2 factory. #ifdef ROL_TIMERS Teuchos::TimeMonitor LocalTimer(*FactorTime_example_PDEOPT_TOOLS_PDEFEM_GLOB); #endif try { solverA_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", matA_dirichlet_); //solverA_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("PardisoMKL", matA_dirichlet_); //solverA_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("SuperLU", matA_dirichlet_); } catch (std::invalid_argument& e) { std::cout << e.what() << std::endl; } // Perform factorization. solverA_->numericFactorization(); } virtual void ConstructAdjointSolvers() { // Construct solver using Amesos2 factory. try{ solverA_trans_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", matA_dirichlet_trans_); //solverA_trans_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("PardisoMKL", matA_dirichlet_trans_); //solverA_trans_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("SuperLU", matA_dirichlet_trans_); } catch (std::invalid_argument& e) { std::cout << e.what() << std::endl; } solverA_trans_->numericFactorization(); } ROL::Ptr > getMatA(const bool &transpose = false) const { if (transpose) { //return matA_dirichlet_trans_; return matA_dirichlet_; } else { return matA_dirichlet_; } } ROL::Ptr > getMatB(const bool &transpose = false) const { if (transpose) { //return matM_dirichlet_trans_; return matM_dirichlet_; } else { return matM_dirichlet_; } } ROL::Ptr > getMatM() const { return matM_; } ROL::Ptr > getMatR() const { return matM_; } ROL::Ptr > getVecUd() const { return vecUd_; } ROL::Ptr > getVecF() const { return vecF_dirichlet_; } ROL::Ptr, Tpetra::MultiVector<> > > getSolver(const bool &transpose = false) const { if (transpose) { //return solverA_trans_; return solverA_; } else { return solverA_; } } virtual Real funcTarget(const Real &x1, const Real &x2) const { return 0.0; } void printMeshData(std::ostream &outStream) const { ROL::Ptr > nodesPtr = meshMgr_->getNodes(); ROL::Ptr > cellToNodeMapPtr = meshMgr_->getCellToNodeMap(); Intrepid::FieldContainer &nodes = *nodesPtr; Intrepid::FieldContainer &cellToNodeMap = *cellToNodeMapPtr; outStream << "Number of nodes = " << meshMgr_->getNumNodes() << std::endl; outStream << "Number of cells = " << meshMgr_->getNumCells() << std::endl; outStream << "Number of edges = " << meshMgr_->getNumEdges() << std::endl; // Print mesh to file. if ((myRank_ == 0)) { std::ofstream meshfile; meshfile.open("cell_to_node_quad.txt"); for (GO i=0; i > matWriter; matWriter.writeSparseFile("stiffness_mat", matA_); matWriter.writeSparseFile("dirichlet_mat", matA_dirichlet_); //matWriter.writeSparseFile("mass_mat", matM_); matWriter.writeDenseFile("Ud_vec", vecUd_); } void outputTpetraVector(const ROL::Ptr > &vec, const std::string &filename) const { Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > vecWriter; vecWriter.writeDenseFile(filename, vec); } // ACCESSOR FUNCTIONS ROL::Ptr >& GetMeshManager(void) { TEUCHOS_TEST_FOR_EXCEPTION(meshMgr_==ROL::nullPtr, std::logic_error, ">>> (PDE_FEM::GetMeshManager): MeshManager not initialized!"); return meshMgr_; } ROL::Ptr > >& GetBasisPtr(const int ind) { TEUCHOS_TEST_FOR_EXCEPTION(ind > spaceDim_-1 || ind < 0, std::logic_error, ">>> (PDE_FEM::GetBasisPtr): ind out of bounds!"); TEUCHOS_TEST_FOR_EXCEPTION(basisPtrs_.size()==0, std::logic_error, ">>> (PDE_FEM::GetBasisPtr): BasisPntrs not initialized!"); return basisPtrs_[ind]; } int GetBasisOrder(void) const { return basisOrder_; } int GetSpaceDim(void) const { return spaceDim_; } GO GetNumCells(void) const { return numCells_; } int GetNumLocalDofs(void) const { return numLocalDofs_; } int GetNumCubPoints(void) const { return numCubPoints_; } int GetLocalFieldSize(void) const { return lfs_; } }; // class PDE_FEM #endif