ParallelDomainProblem.h 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

20
/** \file ParallelDomain.h */
21

22
23
#ifndef AMDIS_PARALLELDOMAIN_H
#define AMDIS_PARALLELDOMAIN_H
24
25

#include <map>
26
#include <set>
27
28
29
30
#include <vector>

#include "ProblemTimeInterface.h"
#include "ProblemIterationInterface.h"
31
#include "FiniteElemSpace.h"
32
#include "AdaptInfo.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
33
#include "InteriorBoundary.h"
34
35
#include "AMDiS_fwd.h"

36
37
#include "petsc.h"
#include "petscsys.h"
38
#include "petscao.h"
39
40
41
42
43
44
#include "mpi.h"

namespace AMDiS {

  class ParMetisPartitioner;

45
  class ParallelDomainBase : public ProblemIterationInterface,
46
47
48
                                    public ProblemTimeInterface
  {
  public:
49
    ParallelDomainBase(const std::string& name,
50
51
			      ProblemIterationInterface *iterationIF,
			      ProblemTimeInterface *timeIF,
52
53
			      FiniteElemSpace *feSpace,
			      RefinementManager *refineManager);
54

55
    virtual ~ParallelDomainBase() {}
56

57
    virtual void initParallelization(AdaptInfo *adaptInfo);
58

59
60
61
62
63
64
65
66
67
    void exitParallelization(AdaptInfo *adaptInfo);

    /** \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();
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    /// 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) {}

    virtual void initTimestep(AdaptInfo *adaptInfo) {}

    virtual void closeTimestep(AdaptInfo *adaptInfo) {}

    virtual void solveInitialProblem(AdaptInfo *adaptInfo) {}
  
    virtual void transferInitialSolution(AdaptInfo *adaptInfo) {}

84
85
    virtual void beginIteration(AdaptInfo *adaptInfo) 
    {
86
87
88
      iterationIF->beginIteration(adaptInfo);
    }

89
90
    virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION) 
    {
91
92
93
      ERROR_EXIT("Not implemented!\n");
      return 0;
    }
94

95
96
    virtual void endIteration(AdaptInfo *adaptInfo) 
    {
97
98
      iterationIF->endIteration(adaptInfo);
    }
99

100
101
102
103
    virtual int getNumProblems() 
    {
      return 0;
    }
104

105
106
    inline virtual const std::string& getName() 
    { 
107
108
109
      return name; 
    }

110
    /// Returns \ref nRankDOFs, the number of DOFs in the rank mesh.
111
112
    int getNumberRankDOFs() 
    {
113
114
115
      return nRankDOFs;
    }

116
117
    void fillPetscMatrix(DOFMatrix *mat, DOFVector<double> *vec);

118
119
    void solvePetscMatrix(DOFVector<double> *vec);

120
121
122
123
    virtual ProblemStatBase *getProblem(int number = 0) 
    {
      return NULL;
    }
124
125
126
127

    virtual void serialize(std::ostream&) {}

    virtual void deserialize(std::istream&) {}
128
129
130


  protected:
131
132
133
134
    /** \brief
     * Determine the interior boundaries, i.e. boundaries between ranks, and store
     * all information about them in \ref interiorBoundary.
     */
135
136
    void createInteriorBoundaryInfo(std::vector<const DegreeOfFreedom*>& rankDOFs,
				    std::map<const DegreeOfFreedom*, int>& boundaryDOFs);
137
138
139
140

    /// Removes all macro elements from the mesh that are not part of ranks partition.
    void removeMacroElements();

141
142
143
144
145
146
147
148
149
150
151
152

    /** \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] boundaryDOFs  Returns all DOFs from the macro mesh, which lies at
     *                           an interior boundary of the rank. This object maps
     *                           each such DOF to the rank that owns this DOF.
     * @param[out] nRankDOFs     Number of DOFs owned by rank.
     * @param[out] nOverallDOFs  Number of all DOFs in macro mesh.
     */
153
154
155
156
157
158
    void createLocalGlobalNumbering(std::vector<const DegreeOfFreedom*>& rankDOFs,
				    std::map<const DegreeOfFreedom*, int>& boundaryDOFs,
				    int& nRankDOFs, 
				    int& nOverallDOFs);

    void updateLocalGlobalNumbering(int& nRankDOFs, int& nOverallDOFs);
159

Thomas Witkowski's avatar
Thomas Witkowski committed
160
    void addAllDOFs(Element *el, int ithEdge, 
161
		    std::vector<const DegreeOfFreedom*>& dofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
162

163
164
    /** \brief
     * This function traverses the whole mesh, i.e. before it is really partitioned,
Thomas Witkowski's avatar
Thomas Witkowski committed
165
166
167
     * 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.
168
169
170
171
172
173
174
     *
     * @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.
     */
175
    void createDOFMemberInfo(std::map<const DegreeOfFreedom*, std::set<int> >& partitionDOFs,
176
177
			     std::vector<const DegreeOfFreedom*>& rankDOFs,
			     std::map<const DegreeOfFreedom*, int>& boundaryDOFs);
Thomas Witkowski's avatar
Thomas Witkowski committed
178

179
     
Thomas Witkowski's avatar
Thomas Witkowski committed
180
181
182
183
184
185
186
    /** \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.
     */
    void testInteriorBoundary();
187

188
  protected:
189
190
191
192
193
194
    ///
    ProblemIterationInterface *iterationIF;

    ///
    ProblemTimeInterface *timeIF;

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    /// 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;

211
212
213
    /// Finite element space of the problem.
    FiniteElemSpace *feSpace;

214
215
216
    /// Mesh of the problem.
    Mesh *mesh;

217
218
219
    /// Info level.
    int info;

220
221
222
    /// Refinement manager for the mesh.
    RefinementManager *refinementManager;

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    /// 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<int, double> 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<int, int> partitionVec;

    /** \brief
     * Stores an old partitioning of elements. To every element index the number
     * of the parition it corresponds to is stored.
     */
    std::map<int, int> oldPartitionVec;    
243

244
245
246
247
    Mat petscMatrix;

    Vec petscRhsVec;
    
248
    Vec petscSolVec;
249

250
251
    /// Number of DOFs in the rank mesh.
    int nRankDOFs;
Thomas Witkowski's avatar
Thomas Witkowski committed
252

253
254
255
256
257
258
    /** \brief
     * Set of all interior boundary DOFs in ranks partition. The object maps to
     * each such DOF to the number of the rank that owns this DOF.
     */
    std::map<const DegreeOfFreedom*, int> boundaryDOFs;

Thomas Witkowski's avatar
Thomas Witkowski committed
259
    /** \brief 
Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
262
263
264
265
266
267
268
269
270
271
     * 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.
Thomas Witkowski's avatar
Thomas Witkowski committed
272
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
273
    InteriorBoundary otherIntBoundary;
Thomas Witkowski's avatar
Thomas Witkowski committed
274

275
276
277
278
    /** \brief
     * This map contains for each rank the list of dofs the current rank must send
     * to exchange solution dofs at the interior boundaries.
     */
279
    std::map<int, std::vector<const DegreeOfFreedom*> > sendDofs;
280
281
282
283
284

    /** \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.
     */
285
    std::map<int, std::vector<const DegreeOfFreedom*> > recvDofs;
286
287
288
289
290
291
292
293
294
295
296
297
298

    /// Maps local to global dof indices.
    std::map<DegreeOfFreedom, DegreeOfFreedom> mapLocalGlobalDOFs;

    /// Maps global to local dof indices.
    std::map<DegreeOfFreedom, DegreeOfFreedom> mapGlobalLocalDOFs;

    /** \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.
     */
    std::map<DegreeOfFreedom, bool> isRankDOF;
299
300
  };

301
  class ParallelDomainScal : public ParallelDomainBase
302
303
  {
  public:
304
    ParallelDomainScal(const std::string& name,
305
306
307
			      ProblemScal *problem,
			      ProblemInstatScal *problemInstat);

308
309
    void initParallelization(AdaptInfo *adaptInfo);

310
311
312
    virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION);

  protected:
313
314
    /// Starts the solution of the linear system using Petsc.
    void solve();
315

316
317
318
  protected:
    /// Pointer to the stationary problem.
    ProblemScal *probScal;
319
320
321
  };
}

322
#endif // AMDIS_PARALLELDOMAIN_H