FeSpaceMapping.h 6.51 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),
49
50
	nRankDofs(0),
	nOverallDofs(0),
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
51
52
	rStartDofs(0),
	overlap(false)
53
54
    {}
    
55
    void clear();
56
57
58
59
60
61
62
63
    
    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
64
    void insertRankDof(DegreeOfFreedom dof0)
65
66
67
68
69
    {
      FUNCNAME("GlobalDofMap::insertRankDof()");
      
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
      
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
70
      dofMap[dof0] = -1;
71
72
    }
    
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
73
    void insert(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
74
75
76
77
78
79
    {
      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
80
81

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

101
    void addOffset(int offset);
102

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    void computeNonLocalIndices();

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

123
124
  private:
    MPI::Intracomm* mpiComm;
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
125
126
127

    const FiniteElemSpace *feSpace;

128
    /// 
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
129
    DofMapping dofMap;
130

Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
131
132
133
134
135
    std::set<DegreeOfFreedom> nonRankDofs;

    DofComm *sendDofs;
    
    DofComm *recvDofs;
136
137
138
  public:
    /// 
    int nRankDofs, nOverallDofs, rStartDofs;   
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
139
140

    bool overlap;
141
142
143
144
145
146
147
  };
  

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

    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
178
179

      data.find(feSpace)->second.setFeSpace(feSpace);
180
181
    }    

182
    int getRankDofs(vector<const FiniteElemSpace*> &fe)
183
    {
184
185
      FUNCNAME("FeSpaceData::getRankDofs()");

186
      int result = 0;
187
188
189
      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;
190
191
192
193
194
      }

      return result;
    }

195
196
197
198
199
200
201
    inline int getRankDofs()
    {
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");

      return nRankDofs;
    }

202
203
204
205
206
207
208
209
210
211
212
    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;
    }

213
214
215
216
217
218
219
    inline int getOverallDofs()
    {
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");

      return nOverallDofs;
    }

220
221
222
223
224
225
226
227
228
229
230
    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;
    }

231
232
233
234
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
    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;
    }

290
291
292
293
  private:
    MPI::Intracomm* mpiComm;
    
    map<const FiniteElemSpace*, T> data;
294
295
296
297

    vector<const FiniteElemSpace*> feSpaces;

    int nRankDofs, nOverallDofs, rStartDofs;
298
299
300
301
302
  };

}

#endif