ParallelDofMapping.h 14.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file FeSpaceMapping.h */

23
#include <vector>
24
#include <map>
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
25
26
#include <set>
#include "parallel/DofComm.h"
27
#include "parallel/MeshLevelData.h"
28
29
#include "parallel/MpiHelper.h"
#include "parallel/ParallelTypes.h"
30
#include "parallel/StdMpi.h"
31
32
33
34
35

#ifndef AMDIS_FE_SPACE_MAPPING_H
#define AMDIS_FE_SPACE_MAPPING_H

namespace AMDiS {
Thomas Witkowski's avatar
Thomas Witkowski committed
36

37
38
  using namespace std;

39
  /** \brief
40
41
42
   * Is used to store matrix indices to all DOFs in rank's subdomain. Thus, the
   * class defines a mapping from component number and DOF index to a global
   * matrix index. This class does not calculate the indices by itself!
43
44
45
46
47
48
49
50
51
52
53
54
   */
  class DofToMatIndex
  {
  public:
    DofToMatIndex() {}

    /// Reset the data structure.
    inline void clear()
    {
      data.clear();
    }

55
56
57
58
59
60
61
    /** Add a new mapping for a given DOF.
     * 
     * \param[in]   component       Component number for which the mapping 
     *                              is defined.
     * \param[in]   dof             DOF index
     * \param[in]   globalMatIndex  Global matrix index.
     */
62
63
64
65
66
    inline void add(int component, DegreeOfFreedom dof, int globalMatIndex)
    {
      data[component][dof] = globalMatIndex;
    }

67
    /// Maps a global DOF index to the global matrix index for a specific 
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    /// system component number.
    inline int get(int component, DegreeOfFreedom dof)
    {
      FUNCNAME("DofToMatIndex::get()");

      TEST_EXIT_DBG(data.count(component))
	("No mapping data for component %d available!\n", component);

      TEST_EXIT_DBG(data[component].count(dof))
	("Mapping for DOF %d in component %d does not exists!\n",
	 dof, component);

      return data[component][dof];
    }

83
84
85
86
87
    /// Returns for a given matrix index the component and (local or global) DOF
    /// index. As the data structure is not made for this kind of reverse
    /// search, this is very slow and should be only used for debugging.
    void getReverse(int rowIndex, int &component, int &dofIndex);

88
89
90
91
92
93
94
  private:
    /// The mapping data. For each system component there is a specific map that
    /// maps global DOF indices to global matrix indices.
    map<int, map<DegreeOfFreedom, int> > data;
  };


95
96
97
98
99
100
  /**
   * This class defines the parallel mapping of DOFs for one FE space. It is used
   * by the class \ref ParallelDofMapping to specifiy the mapping for a set of 
   * FE spaces.
   */
  class FeSpaceDofMap
101
102
  {
  public:
103
104
    /// This constructor exists only to create std::map of this class and make
    /// use of the operator [] for read access. Should never be called.
105
    FeSpaceDofMap() 
106
107
108
    {
      ERROR_EXIT("Should not be called!\n");
    }
109
110
     
    /// This is the only valid constructur to be used. 
111
112
    FeSpaceDofMap(MeshLevelData* ld)
      : levelData(ld),
113
	dofComm(NULL),
114
	feSpace(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
115
	needGlobalMapping(false),
116
	hasNonLocalDofs(false)
117
118
119
    {
      clear();
    }
120
121

    /// Clears all data of the mapping.
122
    void clear();
123

124
125
    /// Maps a DOF index to both, the local and global index of the mapping. The
    /// global index must not be set.
Thomas Witkowski's avatar
Thomas Witkowski committed
126
    MultiIndex& operator[](DegreeOfFreedom d)
127
    {
128
      TEST_EXIT_DBG(dofMap.count(d))("DOF %d is not in map!\n", d);
129

130
      return dofMap[d];
131
132
    }
    
133
134
    /// Inserts a new DOF to rank's mapping. The DOF is assumed to be owend by
    /// the rank.
135
    void insertRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
136
    {
137
      FUNCNAME("FeSpaceDofMap::insertRankDof()");
138
      
139
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
140
      
141
142
      dofMap[dof0].local = dof1;
      nLocalDofs++;
Thomas Witkowski's avatar
Thomas Witkowski committed
143
      if (dof1 != -1)
144
	nRankDofs++;
145
146
    }
    
147
148
    /// Inserts a new DOF to rank's mapping. The DOF exists in rank's subdomain
    /// but is owned by a different rank, thus it is part of an interior boundary.
149
    void insertNonRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
150
    {
151
      FUNCNAME("FeSpaceDofMap::insertNonRankDof()");
152
      
153
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
154
      
155
156
157
      dofMap[dof0].local = dof1;
      nLocalDofs++;
      nonRankDofs.insert(dof0);
158
159
    }
    
160
    /// Checks if a given DOF is in the DOF mapping.
161
    bool isSet(DegreeOfFreedom dof)
162
    {
163
      return static_cast<bool>(dofMap.count(dof));
164
    }
165

166
167
168
    /// Checks if a given DOF is a rank owned DOF of the DOF mapping. The DOF must
    /// a DOF of the mapping (this is not checked here), otherwise the result is
    /// meaningsless.
169
    bool isRankDof(DegreeOfFreedom dof)
170
    {
171
      return !(static_cast<bool>(nonRankDofs.count(dof)));
172
    }
173
    
174
    /// Returns number of DOFs in the mapping.
175
    unsigned int size()
176
    {
177
      return dofMap.size();
178
179
    }
    
180
    /// Returns the raw data of the mapping.
181
    DofMap& getMap()
182
    {
183
      return dofMap;
184
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
185

186
187
    /// Recomputes the mapping.
    void update();
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
188

189
    /// Sets the FE space this mapping corresponds to.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
190
191
192
193
194
    void setFeSpace(const FiniteElemSpace *fe)
    {
      feSpace = fe;
    }

195
196
    /// Informs the mapping whether the mapping will include DOFs that are not
    /// owned by the rank.
197
    void setNonLocalDofs(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
198
    {
199
      hasNonLocalDofs = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
200
201
    }

202
    /// Informs the mapping whether a global index must be computed.
203
    void setNeedGlobalMapping(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
204
    {
205
      needGlobalMapping = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
206
207
    }

208
    /// Sets the DOF communicators.
209
    void setDofComm(DofComm &dc)
Thomas Witkowski's avatar
Thomas Witkowski committed
210
    {
211
      dofComm = &dc;
Thomas Witkowski's avatar
Thomas Witkowski committed
212
213
    }

214
215
216
217
218
219
    void setMpiComm(MPI::Intracomm &m, int l)
    {
      mpiComm = m;
      meshLevel = l;
    }

220
  private:
221
    /// Computes a global mapping from the local one.
222
    void computeGlobalMapping();
223
224
225

    /// Computes the global indices of all DOFs in the mapping that are not owned
    /// by the rank.
226
    void computeNonLocalIndices();
227
228

  private:
229
    MeshLevelData *levelData;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
230

231
232
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
233

234
235
236
237
    MPI::Intracomm mpiComm;

    int meshLevel;

238
    /// The FE space this mapping belongs to. This is used only the get the
239
    /// correct DOF communicator in \ref dofComm.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
240
241
    const FiniteElemSpace *feSpace;

242
    /// Mapping data from DOF indices to local and global indices.
243
    DofMap dofMap;
244

245
    /// Set of all DOFs that are in mapping but are not owned by the rank.
246
    std::set<DegreeOfFreedom> nonRankDofs;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
247

248
    /// If true, a global index mapping will be computed for all DOFs.
Thomas Witkowski's avatar
Thomas Witkowski committed
249
    bool needGlobalMapping;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
250

251
252
253
254
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
    bool hasNonLocalDofs;
255

256
257
  public:
    /// 
258
    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
259
260
  };
  
261
262
263
264
265
266
  
  /**
   * Implements the mapping from sets of distributed DOF indices to local and
   * global indices. The mapping works for a given set of FE spaces. Furthermore,
   * this class may compute the matrix indices of the set of DOF indices.
   */
Thomas Witkowski's avatar
Thomas Witkowski committed
267
  class ParallelDofMapping
268
269
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
270
    ParallelDofMapping() 
271
      : levelData(NULL),
272
	hasNonLocalDofs(false),
273
274
	needMatIndex(false),
	needMatIndexFromGlobal(false),
275
	feSpaces(0),
276
277
278
279
280
	nRankDofs(1),
	nLocalDofs(1),
	nOverallDofs(1),
	rStartDofs(1)
    {
281
282
283
284
      nRankDofs = -1;
      nLocalDofs = -1;
      nOverallDofs = -1;
      rStartDofs = -1;
285
    } 
286

287
288
289
290
    /** \brief Initialize the parallel DOF mapping.
     *
     * \param[in]  m                  MPI communicator.
     * \param[in]  fe                 The FE spaces of all components of the 
291
292
293
294
     *                                PDE to be solved.
     * \param[in]  uniqueFe           Unique list of FE spaces. Thus, two
     *                                arbitrary elements of this list are always
     *                                different.
295
296
297
298
299
     * \param[in]  needGlobalMapping  If true, the mapping computes also a global
     *                                index for the DOFs.
     * \param[in]  bNonLocalDofs      If true, at least one rank's mapping con-
     *                                taines DOFs that are not owend by the rank.
     */
300
    void init(MeshLevelData& mld,
301
	      vector<const FiniteElemSpace*> &fe,
302
	      vector<const FiniteElemSpace*> &uniqueFe,
303
304
	      bool needGlobalMapping,
	      bool bNonLocalDofs);
305
306

    void setMpiComm(MPI::Intracomm &m, int l);
307
308
309
    
    /// Clear all data.
    void clear();
310
311
312

    /// Set the DOF communicator objects that are required to exchange information
    /// about DOFs that are on interior boundaries.
313
    void setDofComm(DofComm &dofComm);
314

315
316
317
318
319
320
    void setComputeMatIndex(bool b, bool global = false)
    {
      needMatIndex = b;
      needMatIndexFromGlobal = global;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
321
322
323
324
325
    inline bool isMatIndexFromGlobal()
    {
      return needMatIndexFromGlobal;
    }

326
327
    /// Access the DOF mapping for a given FE space.
    inline FeSpaceDofMap& operator[](const FiniteElemSpace* feSpace)
328
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
329
      FUNCNAME("ParallelDofMapping::operator[]()");
330

331
332
      TEST_EXIT_DBG(data.count(feSpace))
	("No data for FE space at address %p!\n", feSpace);
333
334
335
336

      return data.find(feSpace)->second;
    }

337
    /// Returns \ref nRankDofs, thus the number of DOFs owned by the rank.
338
    inline int getRankDofs()
339
    {
340
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");
341

342
      return nRankDofs;
343
344
    }

345
    /// Returns \ref nLocalDofs, thus the number of DOFs in ranks subdomain.
346
    inline int getLocalDofs()
Thomas Witkowski's avatar
Thomas Witkowski committed
347
    {
348
      TEST_EXIT_DBG(nLocalDofs >= 0)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
349

350
      return nLocalDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
351
352
    }

353
    /// Returns \ref nOverallDofs, thus the number of all DOFs in this mapping.
354
    inline int getOverallDofs()
355
    {
356
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");
357

358
      return nOverallDofs;
359
360
    }

361
362
    /// Returns \ref rStartDofs, thus the smallest global index of a DOF that is
    /// owned by the rank.
363
    inline int getStartDofs()
364
    {
365
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");
366

367
      return rStartDofs;
368
369
    }

370
    /// Update the mapping.
Thomas Witkowski's avatar
Thomas Witkowski committed
371
    void update();
372

373
374
375
    /// Update the mapping.
    void update(vector<const FiniteElemSpace*>& feSpaces);

376
377
    /// Returns the global matrix index of a given DOF for a given 
    /// component number.
378
    inline int getMatIndex(int ithComponent, DegreeOfFreedom d)
Thomas Witkowski's avatar
Thomas Witkowski committed
379
    {
380
      return dofToMatIndex.get(ithComponent, d);
Thomas Witkowski's avatar
Thomas Witkowski committed
381
382
    }

383
384
385
386
387
388
389
    /// Returns the component number and local/global DOF index for a given
    /// matrix row index. Should be used for debugging only!
    inline void getReverseMatIndex(int index, int &component, int &dofIndex)
    {
      dofToMatIndex.getReverse(index, component, dofIndex);
    }

390
391
    /// Returns the local matrix index of a given DOF for a given 
    /// component number.
392
    inline int getLocalMatIndex(int ithComponent, DegreeOfFreedom d)
393
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
394
      FUNCNAME("ParallelDofMapping::getLocalMatIndex()");
395

396
      TEST_EXIT_DBG(data[feSpaces[ithComponent]].isRankDof(d))
397
398
	("Should not happen!\n");

399
      return dofToMatIndex.get(ithComponent, d) - rStartDofs;
400
401
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
402
403
404
405
406
407
408
409
410
411
412
413
    // Writes all data of this object to an output stream.
    void serialize(ostream &out)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

    // Reads the object data from an input stream.
    void deserialize(istream &in)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

414
    /// Compute local and global matrix indices.
415
    void computeMatIndex(bool globalIndex);
416

417
418
419
420
421
  protected:
    /// Insert a new FE space DOF mapping for a given FE space.
    void addFeSpace(const FiniteElemSpace* feSpace);

    /// Compute \ref nRankDofs.
422
    int computeRankDofs();
423
424

    /// Compute \ref nLocalDofs.
425
    int computeLocalDofs();
426
427

    /// Compute \ref nOverallDofs.
428
    int computeOverallDofs();
429
430

    /// Compute \ref rStartDofs.
431
    int computeStartDofs();
432

433
  private:
434
435
436
437
    MPI::Intracomm mpiComm;

    int meshLevel;

438
    MeshLevelData *levelData;
439

440
441
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
442

443
444
445
446
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
    bool hasNonLocalDofs;
447

448
449
450
451
452
453
454
455
    /// If true, matrix indeces for the stored DOFs are computed, see
    /// \ref computeMatIndex.
    bool needMatIndex;

    /// If matrix indices should be computed, this variable defines if the
    /// mapping from DOF indices to matrix row indices is defined on local
    /// or global DOF indices. If true, the mapping is to specify and to use
    /// on global ones, otherwise on local DOF indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
456
457
458
459
460
    /// In most scenarios the mapping stored on local DOF indices is what we
    /// want to have. Only when periodic boundary conditions are used together
    /// with some global matrix approache, the matrix indices must be stored
    /// also for DOFs that are not part of the local subdomain. Thus, the
    /// mapping will be stored on global DOF indices.
461
462
    bool needMatIndexFromGlobal;

463
464
465
466
    /// Maps from FE space pointers to DOF mappings.
    map<const FiniteElemSpace*, FeSpaceDofMap> data;

    /// The FE spaces for all components.
467
468
    vector<const FiniteElemSpace*> feSpaces;

469
470
    /// The set of all FE spaces. It uniquly contains all different FE spaces
    /// from \ref feSpaces.
471
    vector<const FiniteElemSpace*> feSpacesUnique;
Thomas Witkowski's avatar
Thomas Witkowski committed
472

473
    /// Number of DOFs owned by rank.
474
    int nRankDofs;
475
476

    /// Number of DOFs in rank's subdomain.
477
    int nLocalDofs;
478
479

    /// Number of global DOFs (this value is thus the same on all ranks).
480
    int nOverallDofs;
481
482

    /// Smallest global index of a DOF owned by the rank.
483
    int rStartDofs;
484
485
486

    /// Mapping from global DOF indices to global matrix indices under 
    /// consideration of possibly multiple components.
487
    DofToMatIndex dofToMatIndex;
488
489
490
491
492
  };

}

#endif