DOFAdmin.cc 8.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>

#include "QPsiPhi.h"
#include "BasisFunction.h"
#include "Boundary.h"
#include "DOFAdmin.h"
#include "ElInfo.h"
#include "Error.h"
#include "FiniteElemSpace.h"
#include "Mesh.h"
#include "DOFVector.h"
#include "DOFIterator.h"
13
#include "Serializer.h"
14
15
16
17
18
19
20

namespace AMDiS {

  const int DOFAdmin::sizeIncrement = 10;

  DOFAdmin::DOFAdmin(Mesh* m) 
    : mesh(m), 
21
22
      nDof(mesh->getDim(), NO_INIT),
      nPreDof(mesh->getDim(), NO_INIT)
Thomas Witkowski's avatar
Thomas Witkowski committed
23
24
25
  { 
    init(); 
  }
26

27

28
  DOFAdmin::DOFAdmin(Mesh* m, std::string aName) 
29
30
    : name(aName), 
      mesh(m), 
31
32
      nDof(mesh->getDim(), NO_INIT),
      nPreDof(mesh->getDim(), NO_INIT)
33
34
35
36
  { 
    init(); 
  }

37

Thomas Witkowski's avatar
Thomas Witkowski committed
38
39
40
  DOFAdmin::~DOFAdmin() 
  {}

41

42
43
  void DOFAdmin::init()
  {
44
45
46
47
48
    firstHole = 0;
    size = 0;
    usedCount = 0;
    holeCount = 0;
    sizeUsed = 0;
49
50
51
    dofFree.clear();
  }

52

53
54
  DOFAdmin& DOFAdmin::operator=(const DOFAdmin& src) 
  {
55
56
57
58
59
60
61
62
63
    if (this != &src) { 
      mesh = src.mesh;
      name = src.name;
      dofFree = src.dofFree;
      firstHole = src.firstHole;
      size = src.size;
      usedCount = src.usedCount;
      holeCount = src.holeCount;
      sizeUsed = src.sizeUsed;
64
      for (int i = 0; i <= mesh->getDim(); i++) {
65
66
	nDof[i] = src.nDof[i];
	nPreDof[i] = src.nPreDof[i];
67
      }
68
69
      dofIndexedList = src.dofIndexedList;
      dofContainerList = src.dofContainerList;
70
    }
71

72
73
    return *this;
  }
74

75
76
77

  bool DOFAdmin::operator==(const DOFAdmin& ad) const
  {
78
79
80
81
82
    if (name != ad.name) 
      return false;
    if (mesh != ad.mesh) 
      return false;

83
84
85
86
87
    return true;
  }


  DOFAdmin::DOFAdmin(const DOFAdmin&)
88
  {
89
90
91
    FUNCNAME("DOFAdmin::DOFAdmin()");

    ERROR_EXIT("TODO\n");
92
  }
93

94
95

  void DOFAdmin::freeDofIndex(int dof) 
96
  {    
97
    FUNCNAME("DOFAdmin::freeDofIndex()");
98

99
    TEST_EXIT_DBG(usedCount > 0)("no dofs in use\n");
100
    TEST_EXIT_DBG(dof >= 0 && dof < size)("invalid dof index %d\n",dof);
101

102
103
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
104

105
    for (di = dofIndexedList.begin(); di != end; ++di)
106
107
      (*di)->freeDOFContent(dof);

108
109
    std::list<DOFContainer*>::iterator dc;
    std::list<DOFContainer*>::iterator dcend = dofContainerList.end();
110

111
    for (dc = dofContainerList.begin(); dc != dcend; ++dc)
112
      (*dc)->freeDofIndex(dof);
113
114

    dofFree[dof] = true;
115
    
116
117
    if (static_cast<int>(firstHole) > dof) 
      firstHole = dof;
118
119
120
121
122

    usedCount--;
    holeCount++;
  }

123

124
125
  int DOFAdmin::getDOFIndex()
  {
126
    FUNCNAME("DOFAdmin::getDOFIndex()");
127
    int dof = 0;
128
129

    // if there is a hole
130
    if (firstHole < static_cast<int>(dofFree.size())) {      
131
      TEST_EXIT_DBG(dofFree[firstHole])("no hole at firstHole!\n");
132
133
134
135
136
      // its no longer a hole
      dofFree[firstHole] = false;
      dof = firstHole;
      // search new hole
      int dfsize = static_cast<int>(dofFree.size());
137
      int i = firstHole + 1;
138
139
      for (; i < dfsize; i++)
	if (dofFree[i])
140
	  break;
141

142
      firstHole = i;
143
    } else {                    // if there is no hole
144
      // enlarge dof-list
145
      enlargeDofLists();
146

147
      TEST_EXIT_DBG(firstHole < static_cast<int>(dofFree.size()))
148
	("no free entry after enlargeDofLists\n");
149
      TEST_EXIT_DBG(dofFree[firstHole])("no free bit at firstHole\n");
150
151
152
153
154
155
      dofFree[firstHole] = false;
      dof = firstHole;
      firstHole++;
    }

    usedCount++;
156
157
158
    if (holeCount > 0) 
      holeCount--;
    sizeUsed = max(sizeUsed, dof + 1);
159

160
    return dof;
161
162
  }

163

164
  void DOFAdmin::enlargeDofLists(int minsize)
165
  {
166
    FUNCNAME("DOFAdmin::enlargeDofLists()");
167
  
168
    int old = size;
169
170
171
    if (minsize > 0)
      if (old > minsize) 
	return;
172
  
173
    int newval = max(minsize, static_cast<int>((dofFree.size() + sizeIncrement)));
174
175
176
177
178
179
180
181
182
183
184

    size = newval;
  
    // stl resizes dofFree to at least newval and sets all new values true
    dofFree.resize(newval, true);

    firstHole = old;
  
    // enlarge all vectors and matrices
    // but DOFVectors<int> don't have to be changed
  
185
    std::list<DOFIndexedBase*>::iterator di;
186
    for (di = dofIndexedList.begin(); di != dofIndexedList.end(); ++di)
187
      if ((*di)->getSize() < newval)
188
 	(*di)->resize(newval);
189
190
  }

191

192
193
  void DOFAdmin::addDOFIndexed(DOFIndexedBase* dofIndexed) 
  {
194
    FUNCNAME("DOFAdmin::addDOFIndexed()");
195

196
    TEST_EXIT(dofIndexed)("no dofIndexed\n");
197

198
199
200
201
#ifdef _OPENMP
#pragma omp critical (dofIndexAccess)
#endif
    {
202
      if (dofIndexed->getSize() < size)
203
204
205
206
	dofIndexed->resize(size);
      
      dofIndexedList.push_back(dofIndexed);
    }
207
208
  }

209

210
211
  void DOFAdmin::removeDOFIndexed(DOFIndexedBase* dofIndexed)
  {
212
213
    FUNCNAME("DOFAdmin::removeDOFIndexed()");

214
215
216
217
218
219
220
221
222
223
224
225
226
    bool removed = false;
#ifdef _OPENMP
#pragma omp critical (dofIndexAccess)
#endif
    {
      std::list<DOFIndexedBase*>::iterator it;
      std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
      for (it = dofIndexedList.begin(); it != end; ++it) {
	if (*it == dofIndexed) {
	  dofIndexedList.erase(it);
	  removed = true;
	  break;
	}
227
228
      }
    }
229

230
    TEST_EXIT(removed)("DOFIndexed not in list!\n");
231
232
  }

233

234
235
  void DOFAdmin::addDOFContainer(DOFContainer* cont)
  {
236
    FUNCNAME("DOFAdmin::addDOFContainer()");
237

238
    TEST_EXIT_DBG(cont)("no container\n");
239

240
241
242
    dofContainerList.push_back(cont);  
  }

243

244
245
  void DOFAdmin::removeDOFContainer(DOFContainer* cont)
  {
246
247
    FUNCNAME("DOFAdmin::removeDOFContainer()");

248
249
    std::list<DOFContainer*>::iterator it;
    std::list<DOFContainer*>::iterator end = dofContainerList.end();
250
251
    for (it = dofContainerList.begin(); it != end; ++it) {
      if (*it == cont) {
252
253
254
255
	dofContainerList.erase(it);
	return;
      }
    }
256
257

    ERROR("Container not in list!\n");
258
259
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
260

261
  void DOFAdmin::compress(std::vector<DegreeOfFreedom> &newDofIndex)
262
  {
263
    FUNCNAME("DOFAdmin::compress()");
264
265

    // nothing to do ?
Thomas Witkowski's avatar
Thomas Witkowski committed
266
267
    if (size < 1 || usedCount < 1 || holeCount < 1) 
      return;
268
269

    // vector to mark used dofs
270
    for (int i = 0; i < size; i++)
271
      newDofIndex[i] = -1;
272
273
274

    // mark used dofs
    DOFIteratorBase it(this, USED_DOFS);
275
    for (it.reset(); !it.end(); ++it)
276
      newDofIndex[it.getDOFIndex()] = 1;   
277
    
278
279
    int n = 0, last = 0;
    for (int i = 0; i < size; i++) {             /* create a MONOTONE compress */
280
281
      if (newDofIndex[i] == 1) {
	newDofIndex[i] = n++;
282
283
284
285
	last = i;
      }
    }
  
286
    TEST_EXIT_DBG(n == usedCount)("count %d != usedCount %d\n", n, usedCount);
287
288
  
    // mark used dofs in compressed dofFree
289
    for (int i = 0; i < n; i++)
290
      dofFree[i] = false;
291

292
    // mark unused dofs in compressed dofFree
293
    for (int i = n; i < size; i++)
294
295
296
297
298
299
300
      dofFree[i] = true;

    firstHole = n;  
    holeCount = 0;
    sizeUsed  = n;
  
    // get index of first changed dof
301
    int first = last;
302
    for (int i = 0; i < size; i++) {
303
      if (newDofIndex[i] < i && newDofIndex[i] >= 0) {
304
305
306
307
	first = i;
	break;
      }
    }
308
309
310
311
    
    for (std::list<DOFIndexedBase*>::iterator di = dofIndexedList.begin(); 
	 di != dofIndexedList.end(); ++di)
      (*di)->compressDOFIndexed(first, last, newDofIndex);
312

313
314
315
    for (std::list<DOFContainer*>::iterator dc = dofContainerList.begin(); 
	 dc != dofContainerList.end(); ++dc)
      (*dc)->compressDofContainer(n, newDofIndex);    
316
317
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
318

319
  void DOFAdmin::setNumberOfDofs(int i, int v) 
320
  { 
321
322
323
    FUNCNAME("DOFAdmin::setNumberOfDOFs()");

    TEST_EXIT_DBG(0 <= i && 4 > i)("Should not happen!\n");
324

325
    nDof[i] = v; 
326
327
  }

328

329
  void DOFAdmin::setNumberOfPreDofs(int i, int v) 
330
  { 
331
332
333
    FUNCNAME("DOFAdmin::setNumberOfPreDOFs()");

    TEST_EXIT_DBG(0 <= i && 4 > i)("Should not happen!\n"); 
334

335
    nPreDof[i] = v; 
336
337
  }

338

339
340
341
342
343
  int DOFAdmin::calcMemoryUsage()
  {
    return sizeof(DOFAdmin);
  }

344

345
  void DOFAdmin::serialize(std::ostream &out) 
346
347
  {
    // write name
Thomas Witkowski's avatar
Thomas Witkowski committed
348
    out << name << "\n";
349
350
351

    // write dofFree
    int s = static_cast<int>(dofFree.size());
352
    SerUtil::serialize(out, s);
353
    for (int i = 0; i < s; i++) {
354
355
      bool free = dofFree[i];     
      SerUtil::serialize(out, free);
356
357
    }

358
359
360
361
362
    SerUtil::serialize(out, firstHole);
    SerUtil::serialize(out, size);
    SerUtil::serialize(out, usedCount);
    SerUtil::serialize(out, holeCount);
    SerUtil::serialize(out, sizeUsed);
363

364
365
    nDof.serialize(out);
    nPreDof.serialize(out);
366
367
  }

368

369
  void DOFAdmin::deserialize(std::istream &in)
370
371
372
373
374
375
376
  {
    // read name
    in >> name;
    in.get();

    // read dofFree
    int s;
377
    SerUtil::deserialize(in, s);
378
    dofFree.resize(s);
379
    for (int i = 0; i < s; i++) {
380
      bool free;
381
      SerUtil::deserialize(in, free);
382
383
384
      dofFree[i] = free;
    }

385
386
387
388
389
    SerUtil::deserialize(in, firstHole);
    SerUtil::deserialize(in, size);
    SerUtil::deserialize(in, usedCount);
    SerUtil::deserialize(in, holeCount);
    SerUtil::deserialize(in, sizeUsed);
390

391
392
    nDof.deserialize(in);
    nPreDof.deserialize(in);
393

394
395
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
396

397
    for (di = dofIndexedList.begin(); di != end; ++di)
398
399
400
401
      (*di)->resize(size);
  }

}