ParallelDofMapping.h 13.6 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
83
84
85
86
87
88
89
    /// 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];
    }

  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;
  };


90
91
92
93
94
95
  /**
   * 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
96
97
  {
  public:
98
99
    /// This constructor exists only to create std::map of this class and make
    /// use of the operator [] for read access. Should never be called.
100
    FeSpaceDofMap() 
101
102
103
    {
      ERROR_EXIT("Should not be called!\n");
    }
104
105
     
    /// This is the only valid constructur to be used. 
106
107
    FeSpaceDofMap(MeshLevelData* ld)
      : levelData(ld),
108
	dofComm(NULL),
109
	feSpace(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
110
	needGlobalMapping(false),
111
	hasNonLocalDofs(false)
112
113
114
    {
      clear();
    }
115
116

    /// Clears all data of the mapping.
117
    void clear();
118

119
120
    /// 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
121
    MultiIndex& operator[](DegreeOfFreedom d)
122
    {
123
      TEST_EXIT_DBG(dofMap.count(d))("DOF %d is not in map!\n", d);
124

125
      return dofMap[d];
126
127
    }
    
128
129
    /// Inserts a new DOF to rank's mapping. The DOF is assumed to be owend by
    /// the rank.
130
    void insertRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
131
    {
132
      FUNCNAME("FeSpaceDofMap::insertRankDof()");
133
      
134
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
135
      
136
137
      dofMap[dof0].local = dof1;
      nLocalDofs++;
Thomas Witkowski's avatar
Thomas Witkowski committed
138
      if (dof1 != -1)
139
	nRankDofs++;
140
141
    }
    
142
143
    /// 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.
144
    void insertNonRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
145
    {
146
      FUNCNAME("FeSpaceDofMap::insertNonRankDof()");
147
      
148
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
149
      
150
151
152
      dofMap[dof0].local = dof1;
      nLocalDofs++;
      nonRankDofs.insert(dof0);
153
154
    }
    
155
    /// Checks if a given DOF is in the DOF mapping.
156
    bool isSet(DegreeOfFreedom dof)
157
    {
158
      return static_cast<bool>(dofMap.count(dof));
159
    }
160

161
162
163
    /// 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.
164
    bool isRankDof(DegreeOfFreedom dof)
165
    {
166
      return !(static_cast<bool>(nonRankDofs.count(dof)));
167
    }
168
    
169
    /// Returns number of DOFs in the mapping.
170
    unsigned int size()
171
    {
172
      return dofMap.size();
173
174
    }
    
175
    /// Returns the raw data of the mapping.
176
    DofMap& getMap()
177
    {
178
      return dofMap;
179
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
180

181
182
    /// Recomputes the mapping.
    void update();
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
183

184
    /// Sets the FE space this mapping corresponds to.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
185
186
187
188
189
    void setFeSpace(const FiniteElemSpace *fe)
    {
      feSpace = fe;
    }

190
191
    /// Informs the mapping whether the mapping will include DOFs that are not
    /// owned by the rank.
192
    void setNonLocalDofs(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
193
    {
194
      hasNonLocalDofs = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
195
196
    }

197
    /// Informs the mapping whether a global index must be computed.
198
    void setNeedGlobalMapping(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
199
    {
200
      needGlobalMapping = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
201
202
    }

203
    /// Sets the DOF communicators.
204
    void setDofComm(DofComm &dc)
Thomas Witkowski's avatar
Thomas Witkowski committed
205
    {
206
      dofComm = &dc;
Thomas Witkowski's avatar
Thomas Witkowski committed
207
208
    }

209
210
211
212
213
214
    void setMpiComm(MPI::Intracomm &m, int l)
    {
      mpiComm = m;
      meshLevel = l;
    }

215
  private:
216
    /// Computes a global mapping from the local one.
217
    void computeGlobalMapping();
218
219
220

    /// Computes the global indices of all DOFs in the mapping that are not owned
    /// by the rank.
221
    void computeNonLocalIndices();
222
223

  private:
224
    MeshLevelData *levelData;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
225

226
227
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
228

229
230
231
232
    MPI::Intracomm mpiComm;

    int meshLevel;

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

237
    /// Mapping data from DOF indices to local and global indices.
238
    DofMap dofMap;
239

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

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

246
247
248
249
    /// 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;
250

251
252
  public:
    /// 
253
    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
254
255
  };
  
256
257
258
259
260
261
  
  /**
   * 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
262
  class ParallelDofMapping
263
264
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
265
    ParallelDofMapping() 
266
      : levelData(NULL),
267
	hasNonLocalDofs(false),
268
269
	needMatIndex(false),
	needMatIndexFromGlobal(false),
270
	feSpaces(0),
271
272
273
274
275
	nRankDofs(1),
	nLocalDofs(1),
	nOverallDofs(1),
	rStartDofs(1)
    {
276
277
278
279
      nRankDofs = -1;
      nLocalDofs = -1;
      nOverallDofs = -1;
      rStartDofs = -1;
280
    } 
281

282
283
284
285
    /** \brief Initialize the parallel DOF mapping.
     *
     * \param[in]  m                  MPI communicator.
     * \param[in]  fe                 The FE spaces of all components of the 
286
287
288
289
     *                                PDE to be solved.
     * \param[in]  uniqueFe           Unique list of FE spaces. Thus, two
     *                                arbitrary elements of this list are always
     *                                different.
290
291
292
293
294
     * \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.
     */
295
    void init(MeshLevelData& mld,
296
	      vector<const FiniteElemSpace*> &fe,
297
	      vector<const FiniteElemSpace*> &uniqueFe,
298
299
	      bool needGlobalMapping,
	      bool bNonLocalDofs);
300
301

    void setMpiComm(MPI::Intracomm &m, int l);
302
303
304
    
    /// Clear all data.
    void clear();
305
306
307

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

310
311
312
313
314
315
    void setComputeMatIndex(bool b, bool global = false)
    {
      needMatIndex = b;
      needMatIndexFromGlobal = global;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
316
317
318
319
320
    inline bool isMatIndexFromGlobal()
    {
      return needMatIndexFromGlobal;
    }

321
322
    /// Access the DOF mapping for a given FE space.
    inline FeSpaceDofMap& operator[](const FiniteElemSpace* feSpace)
323
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      FUNCNAME("ParallelDofMapping::operator[]()");
325

326
327
      TEST_EXIT_DBG(data.count(feSpace))
	("No data for FE space at address %p!\n", feSpace);
328
329
330
331

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

332
    /// Returns \ref nRankDofs, thus the number of DOFs owned by the rank.
333
    inline int getRankDofs()
334
    {
335
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");
336

337
      return nRankDofs;
338
339
    }

340
    /// Returns \ref nLocalDofs, thus the number of DOFs in ranks subdomain.
341
    inline int getLocalDofs()
Thomas Witkowski's avatar
Thomas Witkowski committed
342
    {
343
      TEST_EXIT_DBG(nLocalDofs >= 0)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
344

345
      return nLocalDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
346
347
    }

348
    /// Returns \ref nOverallDofs, thus the number of all DOFs in this mapping.
349
    inline int getOverallDofs()
350
    {
351
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");
352

353
      return nOverallDofs;
354
355
    }

356
357
    /// Returns \ref rStartDofs, thus the smallest global index of a DOF that is
    /// owned by the rank.
358
    inline int getStartDofs()
359
    {
360
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");
361

362
      return rStartDofs;
363
364
    }

365
    /// Update the mapping.
Thomas Witkowski's avatar
Thomas Witkowski committed
366
    void update();
367

368
369
370
    /// Update the mapping.
    void update(vector<const FiniteElemSpace*>& feSpaces);

371
372
    /// Returns the global matrix index of a given DOF for a given 
    /// component number.
373
    inline int getMatIndex(int ithComponent, DegreeOfFreedom d)
Thomas Witkowski's avatar
Thomas Witkowski committed
374
    {
375
      return dofToMatIndex.get(ithComponent, d);
Thomas Witkowski's avatar
Thomas Witkowski committed
376
377
    }

378
379
    /// Returns the local matrix index of a given DOF for a given 
    /// component number.
380
    inline int getLocalMatIndex(int ithComponent, DegreeOfFreedom d)
381
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
382
      FUNCNAME("ParallelDofMapping::getLocalMatIndex()");
383

384
      TEST_EXIT_DBG(data[feSpaces[ithComponent]].isRankDof(d))
385
386
	("Should not happen!\n");

387
      return dofToMatIndex.get(ithComponent, d) - rStartDofs;
388
389
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
390
391
392
393
394
395
396
397
398
399
400
401
    // 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");
    }

402
    /// Compute local and global matrix indices.
403
    void computeMatIndex(bool globalIndex);
404

405
406
407
408
409
  protected:
    /// Insert a new FE space DOF mapping for a given FE space.
    void addFeSpace(const FiniteElemSpace* feSpace);

    /// Compute \ref nRankDofs.
410
    int computeRankDofs();
411
412

    /// Compute \ref nLocalDofs.
413
    int computeLocalDofs();
414
415

    /// Compute \ref nOverallDofs.
416
    int computeOverallDofs();
417
418

    /// Compute \ref rStartDofs.
419
    int computeStartDofs();
420

421
  private:
422
423
424
425
    MPI::Intracomm mpiComm;

    int meshLevel;

426
    MeshLevelData *levelData;
427

428
429
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
430

431
432
433
434
    /// 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;
435

436
437
438
439
440
441
442
443
    /// 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
444
445
446
447
448
    /// 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.
449
450
    bool needMatIndexFromGlobal;

451
452
453
454
    /// Maps from FE space pointers to DOF mappings.
    map<const FiniteElemSpace*, FeSpaceDofMap> data;

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

457
458
    /// The set of all FE spaces. It uniquly contains all different FE spaces
    /// from \ref feSpaces.
459
    vector<const FiniteElemSpace*> feSpacesUnique;
Thomas Witkowski's avatar
Thomas Witkowski committed
460

461
    /// Number of DOFs owned by rank.
462
    int nRankDofs;
463
464

    /// Number of DOFs in rank's subdomain.
465
    int nLocalDofs;
466
467

    /// Number of global DOFs (this value is thus the same on all ranks).
468
    int nOverallDofs;
469
470

    /// Smallest global index of a DOF owned by the rank.
471
    int rStartDofs;
472
473
474

    /// Mapping from global DOF indices to global matrix indices under 
    /// consideration of possibly multiple components.
475
    DofToMatIndex dofToMatIndex;
476
477
478
479
480
  };

}

#endif