FeSpaceMapping.h 6.58 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
36
37
38
#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;

  class GlobalDofMap
  {
  public:
39
40
41
42
43
44
45
    /// 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");
    }
      
46
47
    GlobalDofMap(MPI::Intracomm* m)
      : mpiComm(m),
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
48
	feSpace(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
49
50
	sendDofs(NULL),
	recvDofs(NULL),
51
52
	nRankDofs(0),
	nOverallDofs(0),
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
53
54
	rStartDofs(0),
	overlap(false)
55
56
    {}
    
57
    void clear();
58
59
60
61
62
63
64
65
    
    DegreeOfFreedom operator[](DegreeOfFreedom d)
    {
      TEST_EXIT_DBG(dofMap.count(d))("Should not happen!\n");

      return dofMap[d];
    }
    
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
66
    void insertRankDof(DegreeOfFreedom dof0)
67
68
69
70
71
    {
      FUNCNAME("GlobalDofMap::insertRankDof()");
      
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
      
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
72
      dofMap[dof0] = -1;
73
74
    }
    
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
75
    void insert(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
76
77
78
79
80
81
    {
      FUNCNAME("GlobalDofMap::insert()");
      
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
      
      dofMap[dof0] = dof1;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
82
83

      nonRankDofs.insert(dof0);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    }
    
    bool isSet(DegreeOfFreedom dof)
    {
      return static_cast<bool>(dofMap.count(dof));
    }
    
    unsigned int size()
    {
      return dofMap.size();
    }
    
    DofMapping& getMap()
    {
      return dofMap;
    }
    
101
    void update(bool add = true);
102

103
    void addOffset(int offset);
104

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
105
106
    void computeNonLocalIndices();

Thomas Witkowski's avatar
Thomas Witkowski committed
107
108
    void computeMatIndex(int offset);

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    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;
    }

127
128
  private:
    MPI::Intracomm* mpiComm;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
129
130
131

    const FiniteElemSpace *feSpace;

132
    /// 
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
133
    DofMapping dofMap;
134

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
135
136
137
138
139
    std::set<DegreeOfFreedom> nonRankDofs;

    DofComm *sendDofs;
    
    DofComm *recvDofs;
140
141
142
  public:
    /// 
    int nRankDofs, nOverallDofs, rStartDofs;   
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
143
144

    bool overlap;
145
146
147
148
149
150
151
  };
  

  template<typename T>
  class FeSpaceData
  {
  public:
152
153
154
155
156
157
158
    FeSpaceData() 
      : mpiComm(NULL),
	feSpaces(0),
	nRankDofs(-1),
	nOverallDofs(-1),
	rStartDofs(-1)
    {} 
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

    void setMpiComm(MPI::Intracomm *m)
    {
      mpiComm = m;
    }

    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
182
183

      data.find(feSpace)->second.setFeSpace(feSpace);
184
185
    }    

186
    int getRankDofs(vector<const FiniteElemSpace*> &fe)
187
    {
188
189
      FUNCNAME("FeSpaceData::getRankDofs()");

190
      int result = 0;
191
192
193
      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;
194
195
196
197
198
      }

      return result;
    }

199
200
201
202
203
204
205
    inline int getRankDofs()
    {
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");

      return nRankDofs;
    }

206
207
208
209
210
211
212
213
214
215
216
    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;
    }

217
218
219
220
221
222
223
    inline int getOverallDofs()
    {
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");

      return nOverallDofs;
    }

224
225
226
227
228
229
230
231
232
233
234
    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;
    }

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    int getStartDofs()
    {
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");

      return rStartDofs;
    }

    void setFeSpaces(vector<const FiniteElemSpace*> &fe)
    {
      feSpaces = fe;
      for (unsigned int i = 0; i < feSpaces.size(); i++)
	addFeSpace(feSpaces[i]);
    }

    void update()
    {
      nRankDofs = getRankDofs(feSpaces);
      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;
      result += data[feSpaces[ithFeSpace]][index];

      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;
      result += data[feSpaces[ithFeSpace]][index];

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

294
295
296
297
  private:
    MPI::Intracomm* mpiComm;
    
    map<const FiniteElemSpace*, T> data;
298
299
300
301

    vector<const FiniteElemSpace*> feSpaces;

    int nRankDofs, nOverallDofs, rStartDofs;
302
303
304
305
306
  };

}

#endif