FemProcess
The GeMA Fem Process Plugin
gmpFemAssembler.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_PLUGIN_FEM_ASSEMBLER_H_
25 #define _GEMA_PLUGIN_FEM_ASSEMBLER_H_
26 
27 #include <QList>
28 #include <QVector>
29 #include <QHash>
30 #include <gmElementMesh.h>
31 #include <gmElementDof.h>
32 #include <gmElement.h>
33 #include <gmThreadLocalBuffer.h>
34 
35 #include "gmpFemProcessConfig.h"
36 
37 class GmpFemPhysics;
38 class GmpFemMatrixSet;
39 class GmpFemVectorSet;
40 class GmpFemLocker;
41 
42 class GmStateVar;
43 class GmLogCategory;
44 class GmSolverMatrix;
45 
48 
51 
53 
54 // Enable the define bellow to force validation of internal structures after assembler initialization
55 // #define ENABLE_ASSEMBLER_TESTS
56 
57 
62 class GMP_FEM_PROCESS_API_EXPORT GmpFemAssembler
63 {
64 public:
65 
67  {
70  };
71 
73  {
77  };
78 
80  {
81  NODES_ADDED = 0x001,
82  DOFS_ADDED = 0x002,
83  DOFS_REMOVED = 0x004,
84  ELEMENTS_ADDED = 0x008,
85  ELEMENTS_REMOVED = 0x010,
86 
87  FIXED_BCS_UPDATE = 0x020,
88  FIXED_BCS_CHANGED = 0x040,
89  FIXED_BCS_ALL = 0x080,
90  FIXED_BCS_KEEPOLD = 0x100,
91 
92  FULL_UPDATE = 0x4000,
93  SAVE_MAPPING_INFO = 0x8000,
94  };
95 
97  ~GmpFemAssembler();
98 
99  bool init(GmpFemMatrixSet* matSet, GmpFemVectorSet* vecSet, FixedDofMode fixedMode,
100  GhostDofMode ghostMode, bool enableFastUpdate, bool createReverseMapping,
101  int numThreads, QString& err);
102  bool update(int mode, QString& err);
103 
104  void setPartitions(int npart);
105 
107  int numDof() const { return _nMatrixDof; }
108 
113  int numTotalDof() const { return _nTotalDof; }
114 
115  // Returns the number of fixed degrees of freeedom handled by the assembler. Equal to numTotalDof() - numDof()
116  int numFixedDof() const { return _nTotalDof - _nMatrixDof; }
117 
118  int numElementDof(const GmElement* e, int physicsIndex) const;
119 
121  int maxElementDof() const { return _elemIndexMap.size(); }
122 
123  int dofIndex (int node, int dof) const;
124  int fillIndexMap(const GmElement* e, int physicsIndex, int* indexMap, const GmElementDof*& dofMap);
125 
127  double dofFixedValue(int index) { assert(index < 0); return _fixedValues[abs(index+1)]; }
128 
129  GmStateVar* stateVar (int globalIndex);
130  int svGroupId(int globalIndex);
131 
133  const QVector<QPair<GmStateVar*, GmValueAccessor*> >& stateVars() const { return _stateVars; }
134 
136  Unit timeUnit() const { return _timeUnit; }
137 
138  bool beginAssembly();
139  bool endAssembly(bool discardData = false);
140  const int* addElementData(const GmElement* e, int physicsIndex,
141  const GmpFemAssemblerMatrixCombiner* matCombiner,
142  const GmpFemAssemblerVectorCombiner* vecCombiner);
143  bool addLocalMatrixData (GmMatrixDof& localDofMap,
144  const GmpFemAssemblerMatrixCombiner* matCombiner,
145  const GmpFemAssemblerVectorCombiner* vecCombiner);
146 
147  void saveResultData(const GmVector& data, double lambda = 1.0);
148  void saveResultData(const GmVector& data, int baseDof, GmValueAccessor* ac, double lambda = 1.0);
149  void saveResultDataToFixedDofs(const GmVector& data, int baseDof, GmValueAccessor* ac, bool clearFree);
150 
151  void loadResultData(GmVector& data);
152  void loadFixedData(GmVector& data);
153 
154  const QVector<int>& remapInfo() const;
155  void remapData(GmVector& data, double newValue = 0.0) const;
156  void remapData(const GmVector& oldData, GmVector& newData, double newValue = 0.0) const;
157 
159  bool fastUpdate() const { return _dofUnitList; }
160 
161 #ifdef TRACK_ASSEMBLER_TIME
162  void resetTimeData() { _elapsedTime = 0; }
163  void printTimeData(const GmLogCategory& logger) { gmTimeMsg(logger, QObject::tr(" *** Assembler merge total time: %1 s").arg(_elapsedTime/1.0e9)); }
164 #endif
165 
166 private:
167  Q_DISABLE_COPY(GmpFemAssembler)
168 
169 
171  {
175  int _dofMap[GM_MAX_DOF];
176 
179 
186 
198  };
199 
200  bool addMappedMatrixData(int ndof, const int* indexMap, const GmDofMap* dofMap,
201  const GmpFemAssemblerMatrixCombiner* matCombiner,
202  const GmpFemAssemblerVectorCombiner* vecCombiner);
203 
204  bool initSparseLayout(GmSparseMatrixLayoutBuilder* layoutBuilder);
205 
206  void cleanup();
207  bool initTranslationStructures(bool enableFastUpdate, bool createReverseMapping, QString& err);
208  bool initTemplateAssociations(const GmElementDofMap* nodeDofMap, const QHash<int, GmElementDofMap>& fixedNodeDofMap, QString& err);
209  void initDofToNodeIndex(int firstNode);
210  void initUpdateDofPhysicsList();
211 
212  GmElementDofMap* createNodeDofMap (GmElementDofMap* physicsDofMapList, int* maxElemDof, size_t* dofsSum);
213  bool fillFixedDofMap (QHash<int, GmElementDofMap>& fixedDofMap, const GmElementDofMap* fullNodeDofMap,
214  const QHash<int, GmElementDofMap>* partialNodeDofMap, QString& err);
215  bool fillBcNodeDofMap (const QList<GmpFemPhysics*>& physicsList, QMap<QPair<int, int>, double>& fixedData,
216  bool saveNonConstantPhysics, QString& err);
217  bool fillUnitMap (const QList<GmpFemPhysics*>& physicsList, GmElementDofMap* physicsDofMapList, QString& err);
218  int fecthNodeTemplate(const GmElementDofMap& nodeDofMap, const GmElementDofMap& fixedNodeDofMap, int node, QString& err);
219  int fecthDofStatevar (int dof);
220  void fillFixedData(double* data, int size);
221 
222  bool updateFixedBcValues(QString& err);
223  bool updateTranslationStructures(const QList<GmpFemPhysics*>& physList, const QList<QList<int> >& elementLists,
224  bool nodesAdded, int updateFixedMode, bool updateUnits, QString& err);
225 
226  QHash<int, GmElementDofMap> createUpdateNodeDofMap(const QList<GmpFemPhysics*>& physList, const QList<QList<int> >& elementLists,
227  GmElementDofMap* physicsDofMapList, int* maxElemDof, size_t* dofsSum);
228 
229  int updateNodeTemplate(int node, const GmElementDofMap& addedDofMap, const GmElementDofMap& newFixedDofMap,
230  bool useOldFixedDofMap, int* nFreeAdded, int* nFixedAdded, QString& err);
231 
232  int extendedDofIndex(int node, int dof) const;
233 
234  bool saveNodeDofInfo(QVector<QPair<int, int> >& map, QString& err) const;
235  bool buildRemapInfo(const QVector<QPair<int, int> >& oldMap, QString& err);
236 
241  int tgi(int node) const { return _mesh->toLinearGhostIndex(node); }
242 
244  int nodeIndex(int node) const { return _nodeIndex[tgi(node)]; }
245 
247  void setNodeIndex(int node, int baseIndex) { _nodeIndex[tgi(node)] = baseIndex; }
248 
250  void incNodeIndex(int node, int offset) { _nodeIndex[tgi(node)] += offset; }
251 
253  int nodeTemplateIndex(int node) const { return _nodeTemplateIndex[tgi(node)]; }
254 
256  void setNodeTemplateIndex(int node, int templateIndex) { _nodeTemplateIndex[tgi(node)] = templateIndex; }
257 
259  const TemplateData& nodeTemplate(int node) const { return _templateList.at(nodeTemplateIndex(node)); }
260 
266 
268 
271 
274 
275  int _nthreads;
276  int _nt;
281  size_t _elemDofsSum;
282  int _nNodes;
283  int* _nodeIndex;
285  unsigned char* _nodeTemplateIndex;
290 
295 
300 
303 
306 
312 
317 
322 
325 
330 
337 
338 #ifdef TRACK_ASSEMBLER_TIME
339  qint64 _elapsedTime;
340 #endif
341 
342 #if defined ENABLE_TESTS || defined ENABLE_ASSEMBLER_TESTS
343  void validateInternalStructure() const;
344 #endif
345 };
346 
347 #endif
QMap< int, int > _dofToSvCache
A map keyed by dof number storing the corresponding entry in _stateVars. This map holds values outsid...
Definition: gmpFemAssembler.h:316
QVector< int > _sv
List of indices in _stateVars for every non -1 value in _dofMap, organized so that the _ndof "free" d...
Definition: gmpFemAssembler.h:185
void incNodeIndex(int node, int offset)
Updates the base node index of the given node by adding the given value. Node can be a ghost node ind...
Definition: gmpFemAssembler.h:250
QVector< QPair< GmStateVar *, GmValueAccessor * > > _stateVars
A list storing pairs of state vars and accessors to them for every different state var that provides ...
Definition: gmpFemAssembler.h:294
int numTotalDof() const
Returns the total number of the system degrees of freedom, including fixed ones. This value will be e...
Definition: gmpFemAssembler.h:113
size_t _elemDofsSum
A sum of the number of local matrix entries for the whole set of elements. When update() is called,...
Definition: gmpFemAssembler.h:281
GmpFemMatrixSet * _matSet
The matrix set used together with the assembler.
Definition: gmpFemAssembler.h:264
int _nTotalDof
The total number of degrees of freedom of the system, including fixed ones.
Definition: gmpFemAssembler.h:279
int tgi(int node) const
Translate Ghost Index - An auxiliary function that given a mesh node index, whose value might be a gh...
Definition: gmpFemAssembler.h:241
The assembler will support dofs in ghost nodes.
Definition: gmpFemAssembler.h:74
Unit timeUnit() const
Returns the unit in which time is handled by the physics.
Definition: gmpFemAssembler.h:136
QVector< double > _oldFixedValues
A vector storing the values in _fixedValues before the last call to update()
Definition: gmpFemAssembler.h:287
const TemplateData & nodeTemplate(int node) const
Returns the node template. Node can be a ghost node index if ghost support is enabled.
Definition: gmpFemAssembler.h:259
A convenience class that builds a GmVectorSet with the types given by GmpFemVectorTypes and also asso...
Definition: gmpFemVectorSet.h:47
QString tr(const char *sourceText, const char *disambiguation, int n)
A convenience class that builds a GmMatrixSet with the types given by GmpFemMatrixTypes and also asso...
Definition: gmpFemMatrixSet.h:56
QVector< TemplateData > _templateList
A list with node metadata information for every node template type.
Definition: gmpFemAssembler.h:289
QList< GmpFemPhysics * > _nonConstantDofPhysics
A list with the set of physics that should be queried when updating dofs if _updateDofMode == FAST_UP...
Definition: gmpFemAssembler.h:305
GmpFemAssemblerMatrixAdder * _debugMatAdder
The configured adder object used to assemble matrices in "single debug" mode.
Definition: gmpFemAssembler.h:272
No fixed dof handling. The full matrix will be generated.
Definition: gmpFemAssembler.h:68
bool fastUpdate() const
Returns whether the assembler was initialized with the fast update option turned on or off.
Definition: gmpFemAssembler.h:159
int nodeTemplateIndex(int node) const
Returns the node template index. Node can be a ghost node index if ghost support is enabled.
Definition: gmpFemAssembler.h:253
GhostDofMode
Definition: gmpFemAssembler.h:72
int _nNodes
The number of nodes in _nodeIndex & _nodeTemplateIndex. Will be equal to _mesh->numNodes() or _mesh->...
Definition: gmpFemAssembler.h:282
GmpFemVectorSet * _vecSet
The vector set used together with the assembler.
Definition: gmpFemAssembler.h:265
Basic interface for defining the rules used to combine information from multiple matrices into a sing...
Definition: gmpFemAssemblerCombiner.h:33
bool _hasRemapInfo
Is remap info available (The last call to update included the SAVE_MAPPING_INFO flag)?...
Definition: gmpFemAssembler.h:329
QVector< int > _dofNode
A reverse vector mapping, storing for each dof in the assembled matrix the node it belongs to,...
Definition: gmpFemAssembler.h:284
Fixed dofs are removed from the matrix.
Definition: gmpFemAssembler.h:69
int _nMatrixDof
The number of degrees of freedom of the assembled matrix.
Definition: gmpFemAssembler.h:278
QVector< int > _svOffset
Vector with size equal to _sv, storing the offset of this degree of freedom inside the state variable...
Definition: gmpFemAssembler.h:197
void setNodeTemplateIndex(int node, int templateIndex)
Updates the template index of the given node. Node can be a ghost node index if ghost support is enab...
Definition: gmpFemAssembler.h:256
Struct storing node metadata information.
Definition: gmpFemAssembler.h:170
void setNodeIndex(int node, int baseIndex)
Updates the base node index of the given node. Node can be a ghost node index if ghost support is ena...
Definition: gmpFemAssembler.h:247
quint64 GmElementDofMap
GmTLBuffer< int, true > _elemIndexMap
An auxiliar vector used by addElementMatrix, with size equal to the biggest local matrix number of do...
Definition: gmpFemAssembler.h:299
const QVector< QPair< GmStateVar *, GmValueAccessor * > > & stateVars() const
Returns the vector storing the set of state variables/accesors to them in use by the assembler.
Definition: gmpFemAssembler.h:133
unsigned char * _nodeTemplateIndex
A vector storing for each mesh node its dof template index.
Definition: gmpFemAssembler.h:285
QMap< QPair< GmElementDofMap, GmElementDofMap >, int > _templateCache
A map keyed by both the node dof mapping and the fixed dof mapping, storing the equivalent template i...
Definition: gmpFemAssembler.h:311
QVector< double > _fixedValues
A vector storing the values of the fixed dofs with size equal to _nTotalDof - _nMatrixDof.
Definition: gmpFemAssembler.h:286
FixedDofMode _fixedDofMode
How does the assembler treats fixed degrees of freedom.
Definition: gmpFemAssembler.h:263
int _nt
The number of threads that should be used by the assembler in OMP operations.
Definition: gmpFemAssembler.h:276
int _maxNodeDofs
The maximum number of dofs for a node. Might over estimate if number of node dofs are reduced over ti...
Definition: gmpFemAssembler.h:280
Interface for an object responsible for assemblying local matrices into global matrices....
Definition: gmpFemAssemblerAdder.h:42
int _nFreeDof
The number of "free" degrees of freedom in _dofMap (values >= 0 && < MAX_DOF)
Definition: gmpFemAssembler.h:178
A lock manager to be used by GmpFemAssemblerMatrixAdder and GmpFemAssemblerVectorAdder objects when w...
Definition: gmpFemLocker.h:38
int nodeIndex(int node) const
Returns the base node index of the given node. Node can be a ghost node index if ghost support is ena...
Definition: gmpFemAssembler.h:244
double dofFixedValue(int index)
Returns the fixed dof value associated to a negative index in an entry in the vector returned by fill...
Definition: gmpFemAssembler.h:127
UpdateMode
Definition: gmpFemAssembler.h:79
int numDof() const
Returns the number of degrees of freedom of the global assembled matrix.
Definition: gmpFemAssembler.h:107
GmpFemAssemblerVectorAdder * _debugVecAdder
The configured adder object used to assemble vectors in "single debug" mode.
Definition: gmpFemAssembler.h:273
Basic interface for defining the rules used to combine information from multiple vectors into a singl...
Definition: gmpFemAssemblerCombiner.h:102
A class responsible for assembling the global stiffness matrix, handling multiple physics with possib...
Definition: gmpFemAssembler.h:62
GmpFemAssemblerMatrixAdder * _matAdder
The configured adder object used to assemble matrices.
Definition: gmpFemAssembler.h:269
int maxElementDof() const
Returns the maximum number of degrees of freedom used by a physics/element type.
Definition: gmpFemAssembler.h:121
bool _ghostEnabled
Should the assembler handle ghost nodes?
Definition: gmpFemAssembler.h:277
QVector< int > _remapInfo
The remap vector storing information on how to translate values from a vector prior to an update() ca...
Definition: gmpFemAssembler.h:336
int * _nodeIndex
A vector storing for each mesh node its base index in the matrix.
Definition: gmpFemAssembler.h:283
const QList< GmpFemPhysics * > & _physicsList
List of physics that will cooperate to generate the global matrix.
Definition: gmpFemAssembler.h:262
FixedDofMode
Definition: gmpFemAssembler.h:66
QHash< int, int > _fixedNodeIndex
Map determining the base index of a node in the _fixedValues vector.
Definition: gmpFemAssembler.h:288
#define GM_MAX_DOF
GmpFemAssemblerVectorAdder * _vecAdder
The configured adder object used to assemble vectors.
Definition: gmpFemAssembler.h:270
Base interface class for FEM Physics type plugins.
Definition: gmpFemPhysics.h:44
arma::vec GmVector
QList< GmpFemPhysics * > _nonConstantFixedValuesPhysics
A list with the set of physics whose fixed boundary condition values are not constant.
Definition: gmpFemAssembler.h:302
Equal to GHOST_SUPPORT if there is ANY state var in the mesh with ghost support. Equal to NO_GHOST_SU...
Definition: gmpFemAssembler.h:76
Declaration of usefull configuration definitions for the plugin library.
int _nthreads
The number of threads configured for the assembler. Can be any value >= -1.
Definition: gmpFemAssembler.h:275
The assembler will not support dofs in ghost nodes.
Definition: gmpFemAssembler.h:75
Interface for an object responsible for assemblying local vectors into global vectors....
Definition: gmpFemAssemblerAdder.h:64
GmElementMesh * _mesh
The mesh.
Definition: gmpFemAssembler.h:261
Unit _timeUnit
The time unit used by all the physics.
Definition: gmpFemAssembler.h:324
GmpFemLocker * _locker
The locker object used when adding values to the global matrices/vectors in multiple threads.
Definition: gmpFemAssembler.h:267
Unit * _dofUnitList
A vector keyed by dof number, with size equal to GM_MAX_DOF, storing the corresponding dof unit....
Definition: gmpFemAssembler.h:321