File: DegreeOfFreedomSetBuilder.cpp

package info (click to toggle)
freefem3d 1.0pre10-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 24,816 kB
  • ctags: 8,361
  • sloc: cpp: 57,201; sh: 8,788; yacc: 2,975; makefile: 1,148; ansic: 508; perl: 110
file content (428 lines) | stat: -rw-r--r-- 12,889 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
365
366
367
368
369
370
371
372
373
374
375
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//  This file is part of ff3d - http://www.freefem.org/ff3d
//  Copyright (C) 2001, 2002, 2003 Stphane Del Pino

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

//  $Id: DegreeOfFreedomSetBuilder.cpp,v 1.12 2007/05/20 23:34:30 delpinux Exp $

#include <DegreeOfFreedomSetBuilder.hpp>
#include <Mesh.hpp>

#include <DegreeOfFreedomSetManager.hpp>

#include <SpectralMesh.hpp>
#include <Structured3DMesh.hpp>
#include <MeshOfTetrahedra.hpp>
#include <MeshOfHexahedra.hpp>

#include <FiniteElementTraits.hpp>

#include <ErrorHandler.hpp>

template <size_t ItemDOFNumber>
struct DegreeOfFreedomSetBuilder::ItemTraits
{
  typedef TinyVector<ItemDOFNumber,size_t> DOFList;
};

template <>
struct DegreeOfFreedomSetBuilder::ItemTraits<0>
{
  // This Traits is built to avoid the TinyVector<0,size_t> which is undefined
  // This typedef only acts for correct syntaxe
  typedef TinyVector<1,size_t> DOFList;
};

template <typename MeshType,
	  typename FiniteElementType>
size_t DegreeOfFreedomSetBuilder::
__buildFEMCorrespondance(const MeshType& mesh,
			 DegreeOfFreedomSet::Correspondance& correspondance)
{
  size_t numberOfUsedDOF = 0;
  size_t dofPositionNumber = 0;

  const VerticesCorrespondance& verticesCorrespondance = *mesh.verticesCorrespondance();

  // vertices dof management
  if (FiniteElementType::numberOfVertexDegreesOfFreedom > 0) {
    for (size_t i=0; i<mesh.numberOfVertices(); ++i) {
      correspondance[i] = verticesCorrespondance[i];
      numberOfUsedDOF = std::max(verticesCorrespondance[i],numberOfUsedDOF);
      dofPositionNumber++;
    }
  }

  // edge dof management
  if (FiniteElementType::numberOfEdgeDegreesOfFreedom > 0) {
    ASSERT(mesh.hasEdges());

    typedef TinyVector<2,size_t> VerticesPair;

    typedef
      typename ItemTraits<FiniteElementType::numberOfEdgeDegreesOfFreedom>::DOFList
      EdgeDOFNumbers;

    typedef std::map<VerticesPair, EdgeDOFNumbers> SortedEdgeSet;
    SortedEdgeSet sortedEdgeSet;

    for (size_t i=0; i<mesh.numberOfEdges(); ++i) {
      const Edge& edge = mesh.edge(i);
      const size_t v0 = verticesCorrespondance[mesh.vertexNumber(edge(0))];
      const size_t v1 = verticesCorrespondance[mesh.vertexNumber(edge(1))];

      TinyVector<2,size_t> verticesPair;
      verticesPair[0] = std::min(v0,v1);
      verticesPair[1] = std::max(v0,v1);

      typename SortedEdgeSet::iterator iedge = sortedEdgeSet.find(verticesPair);
      if (iedge == sortedEdgeSet.end()) {
	EdgeDOFNumbers dofNumbers;
	for (size_t j=0; j<FiniteElementType::numberOfEdgeDegreesOfFreedom; ++j) {
	  numberOfUsedDOF++;
	  dofNumbers[j]=numberOfUsedDOF;
	}
	
	iedge = sortedEdgeSet.insert(iedge,std::make_pair(verticesPair, dofNumbers));
      }
      for (size_t j=0; j<FiniteElementType::numberOfEdgeDegreesOfFreedom; ++j) {
	correspondance[dofPositionNumber] = iedge->second[j];
	dofPositionNumber++;
      }
    }
  }

  // faces dof management
  if (FiniteElementType::numberOfFaceDegreesOfFreedom > 0) {
    ASSERT(mesh.hasFaces());

    typedef TinyVector<MeshType::FaceType::NumberOfVertices,size_t> VerticesList;

    typedef
      typename ItemTraits<FiniteElementType::numberOfFaceDegreesOfFreedom>::DOFList
      FaceDOFNumbers;

    typedef std::map<VerticesList, FaceDOFNumbers> SortedFaceSet;
    SortedFaceSet sortedFaceSet;

    for (size_t i=0; i<mesh.numberOfFaces(); ++i) {
      const typename MeshType::FaceType& face = mesh.face(i);
      std::set<size_t> verticesSet;

      for (size_t j=0; j<MeshType::FaceType::NumberOfVertices; ++j) {
	verticesSet.insert(verticesCorrespondance[mesh.vertexNumber(face(j))]);
      }
      VerticesList verticesList;
      {
	size_t j=0;
	for (std::set<size_t>::const_iterator k = verticesSet.begin();
	     k != verticesSet.end(); ++k, ++j) {
	  verticesList[j] = *k;
	}
      }

      typename SortedFaceSet::iterator iface = sortedFaceSet.find(verticesList);
      if (iface == sortedFaceSet.end()) {
	FaceDOFNumbers dofNumbers;
	for (size_t j=0; j<FiniteElementType::numberOfFaceDegreesOfFreedom; ++j) {
	  numberOfUsedDOF++;
	  dofNumbers[j]=numberOfUsedDOF;
	}
	
	iface = sortedFaceSet.insert(iface,std::make_pair(verticesList, dofNumbers));
      }
      for (size_t j=0; j<FiniteElementType::numberOfFaceDegreesOfFreedom; ++j) {
	correspondance[dofPositionNumber] = iface->second[j];
	dofPositionNumber++;
      }
    }
  }

  // cell dof management
  if (FiniteElementType::numberOfVolumeDegreesOfFreedom > 0) {
    for (size_t i=0;i<mesh.numberOfCells(); ++i) {
      for (size_t j=0;j<FiniteElementType::numberOfVolumeDegreesOfFreedom; ++j) {
	correspondance[dofPositionNumber] = numberOfUsedDOF;
	dofPositionNumber++;
	numberOfUsedDOF++;
      }
    }
  }
  return numberOfUsedDOF+1;
}

template <typename MeshType>
size_t DegreeOfFreedomSetBuilder::
__buildCorrespondance(const MeshType& mesh,
		      const DiscretizationType& discretization,
		      DegreeOfFreedomSet::Correspondance& correspondance)
{
  if (not mesh.isPeriodic()) {
    for (size_t i=0; i<correspondance.size(); ++i) {
      correspondance[i]=i;
    }
    return correspondance.size();
  }

  typedef typename MeshType::CellType CellType;

  switch (discretization.type()) {
  case DiscretizationType::lagrangianFEM1: {
    typedef
      typename FiniteElementTraits<CellType,
      DiscretizationType::lagrangianFEM1>::Type
      FiniteElementType;

    return this->__buildFEMCorrespondance<MeshType,FiniteElementType>(mesh,correspondance);
  }
  case DiscretizationType::lagrangianFEM2: {
    typedef
      typename FiniteElementTraits<CellType,
      DiscretizationType::lagrangianFEM2>::Type
      FiniteElementType;

    return this->__buildFEMCorrespondance<MeshType,FiniteElementType>(mesh,correspondance);
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented",
		       ErrorHandler::unexpected);
  }
  }
  return 0;
}

template <typename MeshType,
	  typename FiniteElementType>
void DegreeOfFreedomSetBuilder::
__buildFEMFictitious(const size_t& numberOfVariables,
		     const MeshType& mesh,
		     const Domain& domain)
{
  typedef DegreeOfFreedomSet::Correspondance Correspondance;

  ReferenceCounting<Correspondance> pCorrespondance
    = new Correspondance(__dofPositionSet.number());

  size_t numberOfUsedDegreeOfFreedom=0;

  Correspondance& correspondance = *pCorrespondance;
  numberOfUsedDegreeOfFreedom
    = this->__buildFEMCorrespondance<MeshType,FiniteElementType>(mesh,
								 correspondance);

  Vector<bool> nonFictitiousDOF(__dofPositionSet.number());
  nonFictitiousDOF = false;

  for (size_t i=0; i<__dofPositionSet.number(); ++i) {
    nonFictitiousDOF[i] = domain.inside(__dofPositionSet.vertex(i));
  }

  typedef typename MeshType::CellType CellType;

  for (size_t i=0; i<mesh.numberOfCells();++i) {
    size_t numberIn = 0;
    const CellType& k = mesh.cell(i);
    const size_t cellNumer = mesh.cellNumber(k);
    // Computation of the caracteristic function
    for (size_t j=0; j<FiniteElementType::numberOfDegreesOfFreedom; ++j) {
      numberIn += (nonFictitiousDOF(__dofPositionSet(cellNumer,j)))?1:0;
    }

    // reduction of degrees of freedom
    if (numberIn != 0) {
      for (size_t j=0; j<FiniteElementType::numberOfDegreesOfFreedom; ++j) {
	const size_t n = __dofPositionSet(cellNumer,j);
	nonFictitiousDOF[n] = true;
      }
    }
  }

  // numbering correspondant vertices remapping
  std::map<size_t, size_t> newVerticesCorrespondance;
  for (size_t i=0; i<__dofPositionSet.number(); ++i) {
    if (nonFictitiousDOF[i]) {
      newVerticesCorrespondance[correspondance[i]] = 0;
    }
  }

  size_t numberOfDLVertices = 0;
  for (std::map<size_t, size_t>::iterator i=newVerticesCorrespondance.begin();
       i != newVerticesCorrespondance.end(); ++i) {
    i->second = numberOfDLVertices++;
  }

  ReferenceCounting<Correspondance> pFDMCorrespondance
    = new Correspondance(__dofPositionSet.number());

  DegreeOfFreedomSet::Correspondance& fdmCorrespondance = *pFDMCorrespondance;
  fdmCorrespondance = -1;

  for (size_t i = 0; i<__dofPositionSet.number(); ++i) {
    if (nonFictitiousDOF[i]) {
      fdmCorrespondance[i] = newVerticesCorrespondance[correspondance[i]];
    }
  }
  
  __degreeOfFreedomSet
    = new DegreeOfFreedomSet(numberOfVariables,
			     numberOfDLVertices,
			     __dofPositionSet,
			     pFDMCorrespondance);
}

template <typename MeshType>
void DegreeOfFreedomSetBuilder::
__buildFictitious(const size_t& numberOfVariables,
		  const DiscretizationType& discretization,
		  const MeshType& mesh,
		  const Domain& domain)
{
  typedef typename MeshType::CellType CellType;

  switch (discretization.type()) {
  case DiscretizationType::lagrangianFEM1: {
    typedef
      typename FiniteElementTraits<CellType,
      DiscretizationType::lagrangianFEM1>::Type
      FiniteElementType;

    this->__buildFEMFictitious<MeshType,FiniteElementType>(numberOfVariables,
							   mesh,
							   domain);
    break;
  }
  case DiscretizationType::lagrangianFEM2: {
    typedef
      typename FiniteElementTraits<CellType,
      DiscretizationType::lagrangianFEM2>::Type
      FiniteElementType;

    this->__buildFEMFictitious<MeshType,FiniteElementType>(numberOfVariables,
							   mesh,
							   domain);
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented",
		       ErrorHandler::unexpected);
  }
  }
}

DegreeOfFreedomSetBuilder::
DegreeOfFreedomSetBuilder(const size_t& numberOfVariables,
			  const DiscretizationType& discretization,
			  const Mesh& mesh)
  : __dofPositionSet(DegreeOfFreedomSetManager::instance().getDOFPositionsSet(mesh,
									      discretization))
{
  typedef DegreeOfFreedomSet::Correspondance Correspondance;

  ReferenceCounting<Correspondance> pCorrespondance
    = new Correspondance(__dofPositionSet.number());
  Correspondance& correspondance = *pCorrespondance;

  size_t numberOfUsedDegreeOfFreedom=0;

  switch(mesh.type()) {
  case Mesh::cartesianHexahedraMesh: {
    numberOfUsedDegreeOfFreedom
      = this->__buildCorrespondance(static_cast<const Structured3DMesh&>(mesh),
				    discretization,
				    correspondance);
    break;
  }
  case Mesh::hexahedraMesh: {
    numberOfUsedDegreeOfFreedom
      = this->__buildCorrespondance(static_cast<const MeshOfHexahedra&>(mesh),
				    discretization,
				    correspondance);
    break;
  }
  case Mesh::tetrahedraMesh: {
    numberOfUsedDegreeOfFreedom
      = this->__buildCorrespondance(static_cast<const MeshOfTetrahedra&>(mesh),
				    discretization,
				    correspondance);
    break;
  }
  case Mesh::spectralMesh: {
    numberOfUsedDegreeOfFreedom
      = this->__buildCorrespondance(static_cast<const SpectralMesh&>(mesh),
				    discretization,
				    correspondance);
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented yet",
		       ErrorHandler::unexpected);
  }
  }

  __degreeOfFreedomSet
    = new DegreeOfFreedomSet(numberOfVariables,
			     numberOfUsedDegreeOfFreedom,
			     __dofPositionSet,
			     pCorrespondance);    

}

DegreeOfFreedomSetBuilder::
DegreeOfFreedomSetBuilder(const size_t& numberOfVariables,
			  const DiscretizationType& discretizationType,
			  const Mesh& mesh,
			  const Domain& domain)
  : __dofPositionSet(DegreeOfFreedomSetManager::instance().getDOFPositionsSet(mesh,
									      discretizationType))
{
  switch(mesh.type()) {
  case Mesh::cartesianHexahedraMesh: {
    this->__buildFictitious(numberOfVariables,
			    discretizationType,
			    static_cast<const Structured3DMesh&>(mesh),
			    domain);
    break;
  }
  case Mesh::tetrahedraMesh: {
    this->__buildFictitious(numberOfVariables,
			    discretizationType,
			    static_cast<const Structured3DMesh&>(mesh),
			    domain);
    break;
  }
  case Mesh::hexahedraMesh: {
    this->__buildFictitious(numberOfVariables,
			    discretizationType,
			    static_cast<const Structured3DMesh&>(mesh),
			    domain);
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented yet",
		       ErrorHandler::unexpected);
  }
  }
}


DegreeOfFreedomSetBuilder::
~DegreeOfFreedomSetBuilder()
{
  DegreeOfFreedomSetManager::instance().unsubscribe(__dofPositionSet);
}