GEOSX
DofManager.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_LINEARALGEBRA_DOFMANAGER_HPP_
20 #define GEOS_LINEARALGEBRA_DOFMANAGER_HPP_
21 
22 #include "common/DataTypes.hpp"
25 
26 #include <numeric>
27 
28 namespace geos
29 {
30 
31 class DomainPartition;
32 class MeshLevel;
33 class ObjectManagerBase;
34 class FluxApproximationBase;
35 
43 {
44 public:
45 
47  static int constexpr MAX_COMP = 32;
48 
51 
57  struct SubComponent
58  {
59  string fieldName;
61  };
62 
66  struct FieldSupport
67  {
68 public:
70  string meshBodyName;
72  string meshLevelName;
74  std::set< string > regionNames;
75 
84  bool add( FieldSupport const & input )
85  {
86  bool added = false;
87  if( meshBodyName == input.meshBodyName && meshLevelName == input.meshLevelName )
88  {
89  regionNames.insert( input.regionNames.begin(), input.regionNames.end() );
90  added = true;
91  }
92 
93  return added;
94  }
95  };
96 
102  enum class Connector
103  {
104  Elem,
105  Face,
106  Edge,
107  Node,
108  None,
109  Stencil
110  };
111 
116  {
117  None,
119  };
120 
126  explicit DofManager( string name );
127 
131  DofManager( DofManager const & ) = delete;
132 
136  DofManager( DofManager && ) = default;
137 
142  DofManager & operator=( DofManager const & ) = delete;
143 
148  DofManager & operator=( DofManager && ) = default;
149 
153  void clear();
154 
163  void setDomain( DomainPartition & domain );
164 
173  void addField( string const & fieldName,
175  integer components,
176  std::vector< FieldSupport > const & regions = {} );
177 
183  void addField( string const & fieldName,
185  integer components,
186  map< std::pair< string, string >, array1d< string > > const & regions );
187 
193  void setLocalReorderingType( string const & fieldName,
194  LocalReorderingType const reorderingType );
195 
201  void disableGlobalCouplingForEquation( string const & fieldName,
202  integer const c );
203 
209  void disableGlobalCouplingForEquations( string const & fieldName,
210  arrayView1d< integer const > const components );
211 
235  void addCoupling( string const & rowFieldName,
236  string const & colFieldName,
237  Connector connectivity,
238  std::vector< FieldSupport > const & regions = {},
239  bool symmetric = true );
243  void addCoupling( string const & rowFieldName,
244  string const & colFieldName,
245  Connector connectivity,
246  map< std::pair< string, string >, array1d< string > > const & regions,
247  bool symmetric = true );
248 
257  void addCoupling( string const & fieldName,
258  FluxApproximationBase const & stencils );
259 
276 
282  bool fieldExists( string const & name ) const;
283 
289  string const & getKey( string const & fieldName ) const;
290 
295  globalIndex numGlobalDofs( string const & fieldName ) const;
296 
301 
306  localIndex numLocalDofs( string const & fieldName ) const;
307 
312 
317  globalIndex rankOffset( string const & fieldName ) const;
318 
323 
328  integer numComponents( string const & fieldName = "" ) const;
329 
334 
340  FieldLocation location( string const & fieldName ) const;
341 
346  globalIndex globalOffset( string const & fieldName ) const;
347 
353 
362  template< typename CONTAINER >
363  void getLocalDofComponentLabels( CONTAINER & labels ) const
364  {
365  labels.resize( numLocalDofs() );
366  typename CONTAINER::value_type labelStart = 0;
367  auto it = labels.begin();
368  for( FieldDescription const & field : m_fields )
369  {
370  localIndex const numComp = field.numComponents;
371  localIndex const numSupp = field.numLocalDof / numComp;
372  for( localIndex i = 0; i < numSupp; ++i, it += numComp )
373  {
374  std::iota( it, it + numComp, labelStart );
375  }
376  labelStart += numComp;
377  }
378  }
379 
385 
396  string const & srcFieldName,
397  string const & dstFieldName,
398  real64 scalingFactor,
399  CompMask mask = CompMask( MAX_COMP, true ) ) const;
400 
410  template< typename SCALING_FACTOR_TYPE >
412  string const & srcFieldName,
413  string const & dstFieldName,
414  SCALING_FACTOR_TYPE const & scalingFactor,
415  CompMask mask = CompMask( MAX_COMP, true ) ) const;
416 
426  void copyFieldToVector( arrayView1d< real64 > const & localVector,
427  string const & srcFieldName,
428  string const & dstFieldName,
429  real64 scalingFactor,
430  CompMask mask = CompMask( MAX_COMP, true ) ) const;
431 
441  void addFieldToVector( arrayView1d< real64 > const & localVector,
442  string const & srcFieldName,
443  string const & dstFieldName,
444  real64 scalingFactor,
445  CompMask mask = CompMask( MAX_COMP, true ) ) const;
446 
456  std::vector< SubComponent >
457  filterDofs( std::vector< SubComponent > const & excluded ) const;
458 
465  void setupFrom( DofManager const & source,
466  std::vector< SubComponent > const & selection );
467 
480  template< typename MATRIX >
481  void makeRestrictor( std::vector< SubComponent > const & selection,
482  MPI_Comm const & comm,
483  bool transpose,
484  MATRIX & restrictor ) const;
485 
491  void printFieldInfo( std::ostream & os = std::cout ) const;
492 
493 private:
494 
498  struct FieldDescription
499  {
500  string name;
501  string key;
502  string docstring;
503  std::vector< FieldSupport > support;
505  integer numComponents = 1;
506  CompMask globallyCoupledComponents;
508  localIndex numLocalDof = 0;
509  globalIndex numGlobalDof = 0;
510  globalIndex blockOffset = 0;
511  globalIndex rankOffset = 0;
514  };
515 
519  struct CouplingDescription
520  {
521  Connector connector = Connector::None;
522  std::vector< FieldSupport > support;
523  FluxApproximationBase const * stencils = nullptr;
524  };
525 
529  localIndex getFieldIndex( string const & name ) const;
530 
535  void computeFieldDimensions( localIndex fieldIndex );
536 
542  void createIndexArray( FieldDescription const & field,
543  arrayView1d< localIndex const > const permutation );
544 
549  void removeIndexArray( FieldDescription const & field );
550 
556  array1d< localIndex > computePermutation( FieldDescription & field );
557 
564  void computePermutation( FieldDescription const & field,
565  arrayView1d< localIndex > const permutation );
566 
567 
574  void countRowLengthsOneBlock( arrayView1d< localIndex > const & rowLengths,
575  localIndex rowFieldIndex,
576  localIndex colFieldIndex ) const;
577 
578  void countRowLengthsFromStencil( arrayView1d< localIndex > const & rowLengths,
579  localIndex fieldIndex ) const;
580 
589  void setSparsityPatternOneBlock( SparsityPatternView< globalIndex > const & pattern,
590  localIndex rowFieldIndex,
591  localIndex colFieldIndex ) const;
592 
593  void setSparsityPatternFromStencil( SparsityPatternView< globalIndex > const & pattern,
594  localIndex fieldIndex ) const;
595 
596  template< int DIMS_PER_DOF >
597  void setFiniteElementSparsityPattern( SparsityPattern< globalIndex > & pattern,
598  localIndex fieldIndex ) const;
599 
610  template< typename FIELD_OP, typename POLICY, typename SCALING_FACTOR_TYPE >
611  void vectorToField( arrayView1d< real64 const > const & localVector,
612  string const & srcFieldName,
613  string const & dstFieldName,
614  SCALING_FACTOR_TYPE const & scalingFactor,
615  CompMask mask ) const;
616 
627  template< typename FIELD_OP, typename POLICY >
628  void fieldToVector( arrayView1d< real64 > const & localVector,
629  string const & srcFieldName,
630  string const & dstFieldName,
631  real64 scalingFactor,
632  CompMask mask ) const;
633 
635  string m_name;
636 
638  DomainPartition * m_domain = nullptr;
639 
641  std::vector< FieldDescription > m_fields;
642 
644  std::map< std::pair< localIndex, localIndex >, CouplingDescription > m_coupling;
645 
647  bool m_reordered = false;
648 };
649 
650 } /* namespace geos */
651 
652 #endif /*GEOS_LINEARALGEBRA_DOFMANAGER_HPP_*/
Utility class that represents a mask for included/excluded component of a mask.
The DoFManager is responsible for allocating global dofs, constructing sparsity patterns,...
Definition: DofManager.hpp:43
void setSparsityPattern(SparsityPattern< globalIndex > &pattern) const
Populate sparsity pattern of the entire system matrix.
void addVectorToField(arrayView1d< real64 const > const &localVector, string const &srcFieldName, string const &dstFieldName, SCALING_FACTOR_TYPE const &scalingFactor, CompMask mask=CompMask(MAX_COMP, true)) const
Add values from LA vectors to simulation data arrays.
void addField(string const &fieldName, FieldLocation location, integer components, map< std::pair< string, string >, array1d< string > > const &regions)
Add a new field and enumerate its degrees-of-freedom.
void addFieldToVector(arrayView1d< real64 > const &localVector, string const &srcFieldName, string const &dstFieldName, real64 scalingFactor, CompMask mask=CompMask(MAX_COMP, true)) const
Add values from a simulation data array to a DOF vector.
array1d< integer > numComponentsPerField() const
Return an array of number of components per field, sorted by field registration order.
void addCoupling(string const &rowFieldName, string const &colFieldName, Connector connectivity, std::vector< FieldSupport > const &regions={}, bool symmetric=true)
Add coupling between two fields.
DofManager(DofManager const &)=delete
Deleted copy constructor.
globalIndex rankOffset(string const &fieldName) const
void copyFieldToVector(arrayView1d< real64 > const &localVector, string const &srcFieldName, string const &dstFieldName, real64 scalingFactor, CompMask mask=CompMask(MAX_COMP, true)) const
Copy values from simulation data arrays to vectors.
void disableGlobalCouplingForEquations(string const &fieldName, arrayView1d< integer const > const components)
Disable the global coupling for a set of equations.
ComponentMask< MAX_COMP > CompMask
Type of component mask used by DofManager.
Definition: DofManager.hpp:50
integer numComponents() const
localIndex numLocalDofs() const
void setLocalReorderingType(string const &fieldName, LocalReorderingType const reorderingType)
Set the local reodering of the dof numbers.
globalIndex numGlobalDofs(string const &fieldName) const
void addCoupling(string const &fieldName, FluxApproximationBase const &stencils)
Special interface for self-connectivity through a stencil.
static constexpr int MAX_COMP
Maximum number of components in a field.
Definition: DofManager.hpp:47
void addCoupling(string const &rowFieldName, string const &colFieldName, Connector connectivity, map< std::pair< string, string >, array1d< string > > const &regions, bool symmetric=true)
Add coupling between two fields.
DofManager(string name)
Constructor.
void getLocalDofComponentLabels(CONTAINER &labels) const
Fill a container with unique dof labels for each local dof.
Definition: DofManager.hpp:363
void addField(string const &fieldName, FieldLocation location, integer components, std::vector< FieldSupport > const &regions={})
Add a new field and enumerate its degrees-of-freedom.
void clear()
Remove all fields and couplings and re-enable addition of new fields.
void reorderByRank()
Finish populating fields and apply appropriate dof renumbering.
bool fieldExists(string const &name) const
Check if string key is already being used.
std::vector< SubComponent > filterDofs(std::vector< SubComponent > const &excluded) const
Create a dof selection by filtering out excluded components.
globalIndex rankOffset() const
globalIndex globalOffset(string const &fieldName) const
integer numComponents(string const &fieldName="") const
FieldLocation location(string const &fieldName) const
Get the support location type of the field.
DofManager & operator=(DofManager &&)=default
Defaulted move assignment.
LocalReorderingType
Indicates the type of (local to a rank) reordering applied to a given field.
Definition: DofManager.hpp:116
@ ReverseCutHillMcKee
Use reverve CutHill-McKee reordering algorithm.
@ None
Do not reorder the variables.
void makeRestrictor(std::vector< SubComponent > const &selection, MPI_Comm const &comm, bool transpose, MATRIX &restrictor) const
Create a matrix that restricts vectors and matrices to a subset of DOFs.
globalIndex numGlobalDofs() const
localIndex numLocalDofs(string const &fieldName) const
void disableGlobalCouplingForEquation(string const &fieldName, integer const c)
Disable the global coupling for a given equation.
Connector
Enumeration of geometric objects for connectivity type. Note that this enum is nearly identical to Fi...
Definition: DofManager.hpp:103
@ None
there is no connectivity (self connected field, like a lumped mass matrix)
@ Node
connectivity is node (like in finite volumes MPFA)
@ Face
connectivity is face (like in finite volumes TPFA)
@ Elem
connectivity is element (like in finite elements)
@ Stencil
connectivity is through a (set of) user-provided stencil(s)
@ Edge
connectivity is edge (like fracture element connectors)
DofManager & operator=(DofManager const &)=delete
Deleted copy assignment.
void setDomain(DomainPartition &domain)
Assign a domain.
void printFieldInfo(std::ostream &os=std::cout) const
Print the summary of declared fields and coupling.
void copyVectorToField(arrayView1d< real64 const > const &localVector, string const &srcFieldName, string const &dstFieldName, real64 scalingFactor, CompMask mask=CompMask(MAX_COMP, true)) const
Copy values from LA vectors to simulation data arrays.
void setupFrom(DofManager const &source, std::vector< SubComponent > const &selection)
Populate this manager from another using a sub-selection of fields/components.
DofManager(DofManager &&)=default
Move constructor.
string const & getKey(string const &fieldName) const
Return the key used to record the field in the DofManager.
Partition of the decomposed physical domain. It also manages the connexion information to its neighbo...
Base template for ordered and unordered maps.
Definition: DataTypes.hpp:369
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
LvArray::SparsityPattern< COL_INDEX, INDEX_TYPE, LvArray::ChaiBuffer > SparsityPattern
Alias for Sparsity pattern class.
Definition: DataTypes.hpp:338
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
FieldLocation
Enum defining the possible location of a field on the mesh.
GEOSX_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216
Describes field support on a single mesh body/level.
Definition: DofManager.hpp:67
std::set< string > regionNames
list of the region names
Definition: DofManager.hpp:74
bool add(FieldSupport const &input)
add the regionNames contained in input if the meshBodyName and the meshLevelName of input match the o...
Definition: DofManager.hpp:84
string meshLevelName
name of the mesh level
Definition: DofManager.hpp:72
string meshBodyName
name of the mesh body
Definition: DofManager.hpp:70
Describes a selection of components from a DoF field.
Definition: DofManager.hpp:58
CompMask mask
Mask that defines component selection.
Definition: DofManager.hpp:60
string fieldName
Name of the DOF field in DofManager.
Definition: DofManager.hpp:59