FeSpaceMapping.h 7.74 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
28
29
30
31
32
33
34
35
#include "parallel/MpiHelper.h"
#include "parallel/ParallelTypes.h"

#ifndef AMDIS_FE_SPACE_MAPPING_H
#define AMDIS_FE_SPACE_MAPPING_H

namespace AMDiS {
  using namespace std;

Thomas Witkowski's avatar
Thomas Witkowski committed
36
37
38
39
40
  struct MultiIndex
  {
    int local, global;
  };

41
42
43
  class GlobalDofMap
  {
  public:
44
45
46
47
48
49
50
    /// This constructor exists only to create std::map of this class and make
    /// use of the operator [] for read access. Should never be called.
    GlobalDofMap() 
    {
      ERROR_EXIT("Should not be called!\n");
    }
      
51
52
    GlobalDofMap(MPI::Intracomm* m)
      : mpiComm(m),
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
53
	feSpace(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
54
55
	sendDofs(NULL),
	recvDofs(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
56
	needGlobalMapping(false),
57
	nRankDofs(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	nLocalDofs(0),
59
	nOverallDofs(0),
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
60
61
	rStartDofs(0),
	overlap(false)
62
63
    {}
    
64
    void clear();
65
    
Thomas Witkowski's avatar
Thomas Witkowski committed
66
    MultiIndex& operator[](DegreeOfFreedom d)
67
68
69
70
71
72
    {
      TEST_EXIT_DBG(dofMap.count(d))("Should not happen!\n");

      return dofMap[d];
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
73
    void insertRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
74
75
76
77
78
    {
      FUNCNAME("GlobalDofMap::insertRankDof()");
      
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
      
Thomas Witkowski's avatar
Thomas Witkowski committed
79
      dofMap[dof0].local = dof1;
Thomas Witkowski's avatar
Thomas Witkowski committed
80
      nLocalDofs++;
Thomas Witkowski's avatar
Thomas Witkowski committed
81
82
      if (dof1 != -1)
	nRankDofs++;
83
84
    }
    
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
85
    void insert(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
86
87
88
89
90
    {
      FUNCNAME("GlobalDofMap::insert()");
      
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
      
Thomas Witkowski's avatar
Thomas Witkowski committed
91
      dofMap[dof0].local = dof1;
Thomas Witkowski's avatar
Thomas Witkowski committed
92
      nLocalDofs++;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
93
      nonRankDofs.insert(dof0);
94
95
96
97
98
99
100
101
102
103
104
105
    }
    
    bool isSet(DegreeOfFreedom dof)
    {
      return static_cast<bool>(dofMap.count(dof));
    }
    
    unsigned int size()
    {
      return dofMap.size();
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
106
    map<DegreeOfFreedom, MultiIndex>& getMap()
107
108
109
110
    {
      return dofMap;
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
111
    void update();
112

113
    void addOffset(int offset);
114

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
115
116
    void computeNonLocalIndices();

Thomas Witkowski's avatar
Thomas Witkowski committed
117
118
    void computeMatIndex(int offset);

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    void print();

    void setFeSpace(const FiniteElemSpace *fe)
    {
      feSpace = fe;
    }

    void setOverlap(bool b)
    {
      overlap = b;
    }

    void setDofComm(DofComm &pSend, DofComm &pRecv)
    {
      sendDofs = &pSend;
      recvDofs = &pRecv;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
137
138
139
140
141
    void setNeedGlobalMapping(bool b)
    {
      needGlobalMapping = b;
    }

142
143
  private:
    MPI::Intracomm* mpiComm;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
144
145
146

    const FiniteElemSpace *feSpace;

147
    /// 
Thomas Witkowski's avatar
Thomas Witkowski committed
148
    map<DegreeOfFreedom, MultiIndex> dofMap;
149

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
150
151
152
153
154
    std::set<DegreeOfFreedom> nonRankDofs;

    DofComm *sendDofs;
    
    DofComm *recvDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
155
156

    bool needGlobalMapping;
157
158
  public:
    /// 
Thomas Witkowski's avatar
Thomas Witkowski committed
159
    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
160
161

    bool overlap;
162
163
164
165
166
167
168
  };
  

  template<typename T>
  class FeSpaceData
  {
  public:
169
170
171
172
173
174
175
    FeSpaceData() 
      : mpiComm(NULL),
	feSpaces(0),
	nRankDofs(-1),
	nOverallDofs(-1),
	rStartDofs(-1)
    {} 
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

    T& operator[](const FiniteElemSpace* feSpace)
    {
      FUNCNAME("FeSpaceData::operator[]()");

      TEST_EXIT_DBG(data.count(feSpace))("Should not happen!\n");

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

    void addFeSpace(const FiniteElemSpace* feSpace)
    {
      FUNCNAME("FeSpaceData::addFeSpace()");
      
      if (data.count(feSpace))
	data.find(feSpace)->second.clear();
      else
	data.insert(make_pair(feSpace, T(mpiComm)));
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
194
195

      data.find(feSpace)->second.setFeSpace(feSpace);
196
197
    }    

198
    int getRankDofs(vector<const FiniteElemSpace*> &fe)
199
    {
200
201
      FUNCNAME("FeSpaceData::getRankDofs()");

202
      int result = 0;
203
204
205
      for (unsigned int i = 0; i < fe.size(); i++) {
	TEST_EXIT_DBG(data.count(fe[i]))("Cannot find FE space: %p\n", fe[i]);
	result += data[fe[i]].nRankDofs;
206
207
208
209
210
      }

      return result;
    }

211
212
213
214
215
216
217
    inline int getRankDofs()
    {
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");

      return nRankDofs;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    int getLocalDofs(vector<const FiniteElemSpace*> &fe)
    {
      FUNCNAME("FeSpaceData::getLocalDofs()");

      int result = 0;
      for (unsigned int i = 0; i < fe.size(); i++) {
	TEST_EXIT_DBG(data.count(fe[i]))("Cannot find FE space: %p\n", fe[i]);
	result += data[fe[i]].nLocalDofs;
      }

      return result;
    }

    inline int getLocalDofs()
    {
      TEST_EXIT_DBG(nLocalDofs >= 0)("Should not happen!\n");

      return nLocalDofs;
    }

238
239
240
241
242
243
244
245
246
247
248
    int getOverallDofs(vector<const FiniteElemSpace*> &feSpaces)
    {
      int result = 0;
      for (unsigned int i = 0; i < feSpaces.size(); i++) {
	TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
	result += data.find(feSpaces[i])->second.nOverallDofs;
      }

      return result;
    }

249
250
251
252
253
254
255
    inline int getOverallDofs()
    {
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");

      return nOverallDofs;
    }

256
257
258
259
260
261
262
263
264
265
266
    int getStartDofs(vector<const FiniteElemSpace*> &feSpaces)
    {
      int result = 0;
      for (unsigned int i = 0; i < feSpaces.size(); i++) {
	TEST_EXIT_DBG(data.count(feSpaces[i]))("Should not happen!\n");
	result += data.find(feSpaces[i])->second.rStartDofs;
      }

      return result;
    }

267
268
269
270
271
272
273
    int getStartDofs()
    {
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");

      return rStartDofs;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
274
275
276
    void init(MPI::Intracomm *m,
	      vector<const FiniteElemSpace*> &fe,
	      bool needGlobalMapping)
277
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
278
      mpiComm = m;
279
      feSpaces = fe;
Thomas Witkowski's avatar
Thomas Witkowski committed
280
      for (unsigned int i = 0; i < feSpaces.size(); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
281
282
	feSpacesUnique.insert(feSpaces[i]);
	
283
	addFeSpace(feSpaces[i]);
Thomas Witkowski's avatar
Thomas Witkowski committed
284
285
	data[feSpaces[i]].setNeedGlobalMapping(needGlobalMapping);
      }
286
287
288
289
    }

    void update()
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
290
291
292
      for (std::set<const FiniteElemSpace*>::iterator it = feSpacesUnique.begin();
	   it != feSpacesUnique.end(); ++it)
	data[*it].update();
Thomas Witkowski's avatar
Thomas Witkowski committed
293

294
      nRankDofs = getRankDofs(feSpaces);
Thomas Witkowski's avatar
Thomas Witkowski committed
295
      nLocalDofs = getLocalDofs(feSpaces);
296
297
298
299
300
301
302
303
304
      nOverallDofs = getOverallDofs(feSpaces);
      rStartDofs = getStartDofs(feSpaces);
    }

    inline int mapLocal(int index, int ithFeSpace)
    {
      int result = 0;
      for (int i = 0; i < ithFeSpace; i++)
	result += data[feSpaces[i]].nRankDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
305
      result += data[feSpaces[ithFeSpace]][index].local;
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

      return result;
    }

    inline int mapLocal(int index, const FiniteElemSpace *feSpace)
    {
      for (unsigned int i = 0; i < feSpaces.size(); i++)
	if (feSpaces[i] == feSpace)
	  return mapLocal(index, feSpace, i);

      return -1;
    }

    inline int mapGlobal(int index, int ithFeSpace)
    {
      int result = rStartDofs;
      for (int i = 0; i < ithFeSpace; i++)
	result += data[feSpaces[i]].nRankDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      result += data[feSpaces[ithFeSpace]][index].local;
325
326
327
328
329
330
331
332
333
334
335
336
337

      return result;
    }

    inline int mapGlobal(int index, const FiniteElemSpace *feSpace)
    {
      for (unsigned int i = 0; i < feSpaces.size(); i++)
	if (feSpaces[i] == feSpace)
	  return mapGlobal(index, feSpace, i);

      return -1;
    }

338
339
340
341
  private:
    MPI::Intracomm* mpiComm;
    
    map<const FiniteElemSpace*, T> data;
342
343
344

    vector<const FiniteElemSpace*> feSpaces;

Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
347
    std::set<const FiniteElemSpace*> feSpacesUnique;

    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
348
349
350
351
352
  };

}

#endif