// ============================================================================ // == == // == AMDiS - Adaptive multidimensional simulations == // == == // ============================================================================ // == == // == TU Dresden == // == == // == Institut für Wissenschaftliches Rechnen == // == Zellescher Weg 12-14 == // == 01069 Dresden == // == germany == // == == // ============================================================================ // == == // == https://gforge.zih.tu-dresden.de/projects/amdis/ == // == == // ============================================================================ /** \file ParallelDomainBase.h */ #ifndef AMDIS_PARALLELDOMAINBASE_H #define AMDIS_PARALLELDOMAINBASE_H #include #include #include #include "ProblemTimeInterface.h" #include "ProblemIterationInterface.h" #include "FiniteElemSpace.h" #include "AdaptInfo.h" #include "InteriorBoundary.h" #include "AMDiS_fwd.h" #include "petsc.h" #include "petscsys.h" #include "petscao.h" #include "mpi.h" #include "Global.h" namespace AMDiS { class ParMetisPartitioner; class ParallelDomainBase : public ProblemIterationInterface, public ProblemTimeInterface { private: /// Defines type for a vector of DOFs. typedef std::vector DofContainer; /// Defines a mapping type from DOFs to rank numbers. typedef std::map DofToRank; /// Defines a mapping type from DOFs to a set of rank numbers. typedef std::map > DofToPartitions; /// Defines a mapping type from rank numbers to sets of DOFs. typedef std::map RankToDofContainer; /// Defines a mapping type from DOF indices to DOF indices. typedef std::map DofMapping; /// Defines a mapping type from DOFs to boolean values. typedef std::map DofToBool; /// Defines a mapping type from DOF indices to boolean values. typedef std::map DofIndexToBool; /// Defines a mapping type from rank numbers to sets of coordinates. typedef std::map > > RankToCoords; /// Forward type (it maps rank numbers to the interior boundary objects). typedef InteriorBoundary::RankToBoundMap RankToBoundMap; typedef std::map ElementIdxToDofs; typedef std::map DofIndexMap; public: ParallelDomainBase(std::string name, ProblemIterationInterface *iterationIF, ProblemTimeInterface *timeIF, FiniteElemSpace *feSpace, RefinementManager *refineManager); virtual ~ParallelDomainBase() {} virtual void initParallelization(AdaptInfo *adaptInfo); void exitParallelization(AdaptInfo *adaptInfo); void updateDofAdmins(); /** \brief * Test, if the mesh consists of macro elements only. The mesh partitioning of * the parallelization works for macro meshes only and would fail, if the mesh * is already refined in some way. Therefore, this function will exit the program * if it finds a non macro element in the mesh. */ void testForMacroMesh(); /// Set for each element on the partitioning level the number of leaf elements. double setElemWeights(AdaptInfo *adaptInfo); void partitionMesh(AdaptInfo *adaptInfo); virtual void setTime(AdaptInfo *adaptInfo) { if (timeIF) timeIF->setTime(adaptInfo); } virtual void initTimestep(AdaptInfo *adaptInfo) { if (timeIF) timeIF->initTimestep(adaptInfo); } virtual void closeTimestep(AdaptInfo *adaptInfo) { if (timeIF) timeIF->closeTimestep(adaptInfo); } virtual void solveInitialProblem(AdaptInfo *adaptInfo) { if (timeIF) timeIF->solveInitialProblem(adaptInfo); } virtual void transferInitialSolution(AdaptInfo *adaptInfo) { if (timeIF) timeIF->transferInitialSolution(adaptInfo); } virtual void beginIteration(AdaptInfo *adaptInfo) { iterationIF->beginIteration(adaptInfo); } virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION); virtual void endIteration(AdaptInfo *adaptInfo) { iterationIF->endIteration(adaptInfo); } virtual void solve() {} virtual int getNumProblems() { return 0; } inline virtual std::string getName() { return name; } /// Returns \ref nRankDOFs, the number of DOFs in the rank mesh. int getNumberRankDofs() { return nRankDofs; } void fillPetscMatrix(DOFMatrix *mat, DOFVector *vec); void fillPetscMatrix(Matrix *mat, SystemVector *vec); void solvePetscMatrix(DOFVector *vec); void solvePetscMatrix(SystemVector &vec); virtual ProblemStatBase *getProblem(int number = 0) { return NULL; } // Writes all data of this object to an output stream. virtual void serialize(std::ostream &out); // Reads the object data from an input stream. virtual void deserialize(std::istream &in); protected: /** \brief * Determine the interior boundaries, i.e. boundaries between ranks, and store * all information about them in \ref interiorBoundary. */ void createInteriorBoundaryInfo(DofContainer& rankDOFs); /// Removes all macro elements from the mesh that are not part of ranks partition. void removeMacroElements(); /** \brief * Creates from a macro mesh a correct local and global DOF index numbering. * * \param[out] rankDOFs Returns all DOFs from the macro mesh, which are owned * by the rank after partitioning the macro mesh. * \param[out] nRankDOFs Number of DOFs owned by rank. * \param[out] nOverallDOFs Number of all DOFs in macro mesh. */ void createLocalGlobalNumbering(DofContainer& rankDOFs, int& nRankDOFs, int& nOverallDOFs); void updateLocalGlobalNumbering(int& nRankDOFs, int& nOverallDOFs); /** \brief * This function create new mappings from local to global indices, * \ref mapLocalGlobalDOFs, and from local to dof indices, \ref mapLocalToDofIndex. * Furthermore, using the first argument the dof indices in ranks partition are * changed. * * \param[in] rankDofsNewLocalIndex Map from dof pointers of all dofs in rank * to new dof indices. * \param[in] rankOwnedDofsNewLocalIndex Map from dof pointers of dofs owned by * the rank to the new local index. * \param[in] rankDofsNewGlobalIndex Map from dof pointers of all dofs in rank * to the new global index. */ void createLocalMappings(DofIndexMap &rankDofsNewLocalIndex, DofIndexMap &rankOwnedDofsNewLocalIndex, DofIndexMap &rankDofsNewGlobalIndex); void addAllVertexDOFs(Element *el, int ithEdge, DofContainer& dofs); void addAllEdgeDOFs(Element *el, int ithEdge, DofContainer& dofs); /** \brief * This function traverses the whole mesh, i.e. before it is really partitioned, * and collects information about which DOF corresponds to which rank. Can only * be used, if \ref partitionVec is set correctly. This is only the case, when * the macro mesh is partitioned. * * \param[out] partionDOFs Stores to each DOF pointer the set of ranks the DOF is * part of. * \param[out] rankDOFs Stores all rank DOFs. * \param[out] boundaryDOFs Stores all DOFs in ranks partition that are on an * interior boundary but correspond to another rank. */ void createDOFMemberInfo(DofToPartitions& partitionDofs, DofContainer& rankOwnedDofs, DofContainer& rankAllDofs, DofToRank& boundaryDofs, DofToBool& vertexDof); void setDofMatrix(DOFMatrix* mat, int dispMult = 1, int dispAddRow = 0, int dispAddCol = 0); void setDofVector(DOFVector* vec, int disMult = 1, int dispAdd = 0); void DbgCreateElementMap(ElementIdxToDofs &elMap); void DbgTestElementMap(ElementIdxToDofs &elMap); void DbgTestInteriorBoundary(); /** \brief * This function is used for debugging only. It traverses all interior boundaries * and compares the dof indices on them with the dof indices of the boundarys * neighbours. The function fails, when dof indices on an interior boundary does * not fit together. * * \param printCoords If true, the coords of all common dofs are printed to * the screen. */ void DbgTestCommonDofs(bool printCoords = false); /** \brief * This functions create a Paraview file with the macro mesh where the elements * are colored by the partition they are part of. This function can be used for * debugging. */ void writePartitioningMesh(std::string filename); /** \brief * This function must be used if the values of a SystemVector must be synchronised * over all ranks. That means, that each rank sends the values of the DOFs, which * are owned by the rank and lie on an interior bounday, to all other ranks also * having this DOF. * * This function must be used, for example, after the lineary system is solved, or * after the SystemVector is set by some user defined functions, e.g., initial * solution functions. */ void synchVectors(SystemVector &vec); /// Writes a vector of dof pointers to an output stream. void serialize(std::ostream &out, DofContainer &data); /// Reads a vector of dof pointers from an input stream. void deserialize(std::istream &in, DofContainer &data, std::map &dofMap); /// Writes a \ref RankToDofContainer to an output stream. void serialize(std::ostream &out, RankToDofContainer &data); /// Reads a \ref RankToDofContainer from an input stream. void deserialize(std::istream &in, RankToDofContainer &data, std::map &dofMap); inline void orderDOFs(const DegreeOfFreedom* dof1, const DegreeOfFreedom* dof2, const DegreeOfFreedom* dof3, DofContainer &vec) { vec.resize(3); if (*dof1 < *dof2 && *dof1 < *dof3) vec[0] = dof1; else if (*dof2 < *dof1 && *dof2 < *dof3) vec[0] = dof2; else vec[0] = dof3; if (*dof1 > *dof2 && *dof1 > *dof3) vec[2] = dof1; else if (*dof2 > *dof1 && *dof2 > *dof3) vec[2] = dof2; else vec[2] = dof3; if (dof1 != vec[0] && dof1 != vec[2]) vec[1] = dof1; else if (dof2 != vec[0] && dof2 != vec[2]) vec[1] = dof2; else vec[1] = dof3; } protected: /// ProblemIterationInterface *iterationIF; /// ProblemTimeInterface *timeIF; /// The rank of the current process. int mpiRank; /// Overall number of processes. int mpiSize; /** \brief * MPI communicator collected all processes, which should * be used for calculation. The Debug procces is not included * in this communicator. */ MPI::Intracomm mpiComm; /// Name of the problem (as used in the init files) std::string name; /// Finite element space of the problem. FiniteElemSpace *feSpace; /// Mesh of the problem. Mesh *mesh; /// Info level. int info; /// Refinement manager for the mesh. RefinementManager *refinementManager; /// Pointer to the paritioner which is used to devide a mesh into partitions. ParMetisPartitioner *partitioner; /// Weights for the elements, i.e., the number of leaf elements within this element. std::map elemWeights; /// Is true, if the mesh was not partitioned before, otherwise it's false. bool initialPartitionMesh; /** \brief * Stores to every coarse element index the number of the partition it * corresponds to. */ std::map partitionVec; /** \brief * Stores an old partitioning of elements. To every element index the number * of the parition it corresponds to is stored. */ std::map oldPartitionVec; /// Petsc's matrix structure. Mat petscMatrix; /** \brief * Petsc's vector structures for the rhs vector, the solution vector and a * temporary vector for calculating the final residuum. */ Vec petscRhsVec, petscSolVec, petscTmpVec; /// Number of DOFs in the rank mesh. int nRankDofs; /** \brief * Defines the interior boundaries of the domain that result from partitioning * the whole mesh. Contains only the boundaries, which are owned by the rank, i.e., * the object gives for every neighbour rank i the boundaries this rank owns and * shares with rank i. */ InteriorBoundary myIntBoundary; /** \brief * Defines the interior boundaries of the domain that result from partitioning * the whole mesh. Contains only the boundaries, which are not owned by the rank, * i.e., the object gives for every neighbour rank i the boundaries that are * owned by rank i and are shared with this rank. */ InteriorBoundary otherIntBoundary; /** \brief * This map contains for each rank the list of dofs the current rank must send * to exchange solution dofs at the interior boundaries. */ RankToDofContainer sendDofs; /** \brief * This map contains for each rank the list of dofs from which the current rank * must receive solution values of dofs at the interior boundaries. */ RankToDofContainer recvDofs; /// Maps local to global dof indices. DofMapping mapLocalGlobalDOFs; /// Maps local dof indices to real dof indices. DofMapping mapLocalToDofIndex; /** \brief * Maps all DOFs in ranks partition to a bool value. If it is true, the DOF is * owned by the rank. Otherwise, its an interior boundary DOF that is owned by * another rank. */ DofIndexToBool isRankDof; DofToBool vertexDof; /// Is the index of the first row of the linear system, which is owned by the rank. int rstart; /** \brief * Number of components of the equation. Is used to calculate the exact number * of rows in the the overall linear system. */ int nComponents; /// Number of rows of the whole linear system that are stored on this rank. int nRankRows; /// Overall number of the rows in the lineary system. int nOverallRows; }; } #endif // AMDIS_PARALLELDOMAINBASE_H