DOFAdmin.cc 9.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#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"

namespace AMDiS {

  const int DOFAdmin::sizeIncrement = 10;

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

26
  DOFAdmin::DOFAdmin(Mesh* m, std::string aName) 
27 28 29 30 31 32 33 34
    : name(aName), 
      mesh(m), 
      nrDOF(mesh->getDim(), NO_INIT),
      nr0DOF(mesh->getDim(), NO_INIT)
  { 
    init(); 
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
35 36 37
  DOFAdmin::~DOFAdmin() 
  {}

38 39
  void DOFAdmin::init()
  {
40 41 42 43 44
    firstHole = 0;
    size = 0;
    usedCount = 0;
    holeCount = 0;
    sizeUsed = 0;
45 46 47
    dofFree.clear();
  }

48 49
  DOFAdmin& DOFAdmin::operator=(const DOFAdmin& src) 
  {
50 51 52 53 54 55 56 57 58
    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;
59
      for (int i = 0; i <= mesh->getDim(); i++) {
60
	nrDOF[i] = src.nrDOF[i];
61
	nr0DOF[i] = src.nr0DOF[i];
62
      }
63 64
      dofIndexedList = src.dofIndexedList;
      dofContainerList = src.dofContainerList;
65
    }
66

67 68 69 70 71 72 73 74 75
    return *this;
  }
  /****************************************************************************/
  /*      use a bit vector to indicate used/unused dofs			    */
  /*      storage needed: one bit per dof                     		    */
  /****************************************************************************/

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

81 82 83 84 85
    return true;
  }


  DOFAdmin::DOFAdmin(const DOFAdmin&)
86
  {
87 88 89
    FUNCNAME("DOFAdmin::DOFAdmin()");

    ERROR_EXIT("TODO\n");
90
  }
91

92 93
  void DOFAdmin::freeDOFIndex(int dof) 
  {    
94
    FUNCNAME("DOFAdmin::freeDOFIndex()");
95

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

99 100
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
101

102
    for (di = dofIndexedList.begin(); di != end; ++di)
103 104
      (*di)->freeDOFContent(dof);

105 106
    std::list<DOFContainer*>::iterator dc;
    std::list<DOFContainer*>::iterator dcend = dofContainerList.end();
107

108
    for (dc = dofContainerList.begin(); dc != dcend; ++dc)
109 110 111
      (*dc)->freeDOFIndex(dof);

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

    usedCount--;
    holeCount++;
  }

  int DOFAdmin::getDOFIndex()
  {
122
    FUNCNAME("DOFAdmin::getDOFIndex()");
123
    int dof = 0;
124 125 126

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

138
      firstHole = i;
139
    } else {                    // if there is no hole
140 141
      // enlarge dof-list
      enlargeDOFLists(0);
142
      TEST_EXIT_DBG(firstHole < static_cast<int>(dofFree.size()))
143
	("no free entry after enlargeDOFLists\n");
144
      TEST_EXIT_DBG(dofFree[firstHole])("no free bit at firstHole\n");
145 146 147 148 149 150
      dofFree[firstHole] = false;
      dof = firstHole;
      firstHole++;
    }

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

    return(dof);
  }

  void DOFAdmin::enlargeDOFLists(int minsize)
  {
160
    FUNCNAME("DOFAdmin::enlargeDOFLists()");
161
  
162
    int old = size;
163 164 165 166
    if (minsize > 0) {
      if (old > minsize) return;
    }
  
167
    int newval = max(minsize, static_cast<int>((dofFree.size() + sizeIncrement)));
168 169 170 171 172 173 174 175 176 177 178

    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
  
179 180
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
181

182 183
    for (di = dofIndexedList.begin(); di != end; ++di)
      if ((*di)->getSize() < newval)
184 185 186
	(*di)->resize(newval);
  }

187 188
  void DOFAdmin::addDOFIndexed(DOFIndexedBase* dofIndexed) 
  {
189
    FUNCNAME("DOFAdmin::addDOFIndexed()");
190

191
    TEST_EXIT(dofIndexed)("no dofIndexed\n");
192

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

  void DOFAdmin::removeDOFIndexed(DOFIndexedBase* dofIndexed)
  {
207 208
    FUNCNAME("DOFAdmin::removeDOFIndexed()");

209 210 211 212 213 214 215 216 217 218 219 220 221
    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;
	}
222 223
      }
    }
224

225
    TEST_EXIT(removed)("DOFIndexed not in list\n");
226 227 228 229
  }

  void DOFAdmin::addDOFContainer(DOFContainer* cont)
  {
230
    FUNCNAME("DOFAdmin::addDOFContainer()");
231
    TEST_EXIT_DBG(cont)("no container\n");
232 233 234 235 236
    dofContainerList.push_back(cont);  
  }

  void DOFAdmin::removeDOFContainer(DOFContainer* cont)
  {
237 238
    FUNCNAME("DOFAdmin::removeDOFContainer()");

239 240
    std::list<DOFContainer*>::iterator it;
    std::list<DOFContainer*>::iterator end = dofContainerList.end();
241 242
    for (it = dofContainerList.begin(); it != end; ++it) {
      if (*it == cont) {
243 244 245 246 247 248 249
	dofContainerList.erase(it);
	return;
      }
    }
    ERROR("container not in list\n");
  }

250
  void DOFAdmin::compress(std::vector<DegreeOfFreedom> &new_dof)
251
  {
252
    FUNCNAME("DOFAdmin::compress()");
253 254 255 256 257 258 259

    // nothing to do ?
    if (size < 1) return;
    if (usedCount < 1) return;
    if (holeCount < 1) return;

    // vector to mark used dofs
260
    for (int i = 0; i < size; i++)
261 262 263 264
      new_dof[i] = -1;

    // mark used dofs
    DOFIteratorBase it(this, USED_DOFS);
265
    for (it.reset(); !it.end(); ++it)
266
      new_dof[it.getDOFIndex()] = 1;
267
    
268 269
    int n = 0, last = 0;
    for (int i = 0; i < size; i++) {             /* create a MONOTONE compress */
270 271 272 273 274 275
      if (new_dof[i] == 1) {
	new_dof[i] = n++;
	last = i;
      }
    }
  
276
    TEST_EXIT_DBG(n == usedCount)("count %d != usedCount %d\n", n, usedCount);
277 278
  
    // mark used dofs in compressed dofFree
279
    for (int i = 0; i < n; i++)
280
      dofFree[i] = false;
281

282
    // mark unused dofs in compressed dofFree
283
    for (int i = n; i < size; i++)
284 285 286 287 288 289 290
      dofFree[i] = true;

    firstHole = n;  
    holeCount = 0;
    sizeUsed  = n;
  
    // get index of first changed dof
291 292
    int first = last;
    for (int i = 0; i<size; i++) {
293 294 295 296 297 298
      if ((new_dof[i] < i) && (new_dof[i] >= 0)) {
	first = i;
	break;
      }
    }

299 300
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
301
    for (di = dofIndexedList.begin(); di != end; ++di)
302 303 304
      (*di)->compressDOFIndexed(first, last, new_dof);

  
305 306
    std::list<DOFContainer*>::iterator dc;
    std::list<DOFContainer*>::iterator endc = dofContainerList.end();
307
    for (dc = dofContainerList.begin(); dc != endc; dc++)
308 309 310
      (*dc)->compressDOFContainer(n, new_dof);
  }

311 312
  void DOFAdmin::setNumberOfDOFs(int i,int v) 
  { 
313
    TEST_EXIT_DBG((0 <= i) && (4 > i))("");
314 315

    nrDOF[i] = v; 
316 317
  }

318 319
  void DOFAdmin::setNumberOfPreDOFs(int i, int v) 
  { 
320
    TEST_EXIT_DBG((0 <= i) && (4 > i))(""); 
321 322

    nr0DOF[i] = v; 
323 324
  }

325 326 327 328 329
  int DOFAdmin::calcMemoryUsage()
  {
    return sizeof(DOFAdmin);
  }

330
  void DOFAdmin::serialize(std::ostream &out) 
331 332
  {
    // write name
Thomas Witkowski's avatar
Thomas Witkowski committed
333
    out << name << "\n";
334 335 336 337

    // write dofFree
    int s = static_cast<int>(dofFree.size());
    out.write(reinterpret_cast<const char*>(&s), sizeof(int));
338
    for (int i = 0; i < s; i++) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
      bool free = dofFree[i];
      out.write(reinterpret_cast<const char*>(&free), sizeof(bool));
    }

    // write firstHole
    out.write(reinterpret_cast<const char*>(&firstHole), sizeof(unsigned int));

    // write size
    out.write(reinterpret_cast<const char*>(&size), sizeof(int));

    // write usedCount
    out.write(reinterpret_cast<const char*>(&usedCount), sizeof(int));

    // write holeCount
    out.write(reinterpret_cast<const char*>(&holeCount), sizeof(int));

    // write sizeUsed
    out.write(reinterpret_cast<const char*>(&sizeUsed), sizeof(int));

    // write nrDOF
    nrDOF.serialize(out);

    // write nr0DOF
    nr0DOF.serialize(out);
}

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

    // read dofFree
    int s;
    in.read(reinterpret_cast<char*>(&s), sizeof(int));
    dofFree.resize(s);
375
    for (int i = 0; i < s; i++) {
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      bool free;
      in.read(reinterpret_cast<char*>(&free), sizeof(bool));
      dofFree[i] = free;
    }

    // read firstHole
    in.read(reinterpret_cast<char*>(&firstHole), sizeof(unsigned int));

    // read size
    in.read(reinterpret_cast<char*>(&size), sizeof(int));

    // read usedCount
    in.read(reinterpret_cast<char*>(&usedCount), sizeof(int));

    // read holeCount
    in.read(reinterpret_cast<char*>(&holeCount), sizeof(int));

    // read sizeUsed
    in.read(reinterpret_cast<char*>(&sizeUsed), sizeof(int));

    // read nrDOF
    nrDOF.deserialize(in);

    // read nr0DOF
    nr0DOF.deserialize(in);

402 403
    std::list<DOFIndexedBase*>::iterator di;
    std::list<DOFIndexedBase*>::iterator end = dofIndexedList.end();
404

405
    for (di = dofIndexedList.begin(); di != end; ++di)
406 407 408 409
      (*di)->resize(size);
  }

}