GEOS
DofManager.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_LINEARALGEBRA_DOFMANAGER_HPP_
21 #define GEOS_LINEARALGEBRA_DOFMANAGER_HPP_
22 
23 #include "common/DataTypes.hpp"
26 
27 #include <numeric>
28 
29 namespace geos
30 {
31 
32 class DomainPartition;
33 class MeshLevel;
34 class ObjectManagerBase;
35 class FluxApproximationBase;
36 
44 {
45 public:
46 
48  static int constexpr MAX_COMP = 32;
49 
52 
58  struct SubComponent
59  {
60  string fieldName;
62  };
63 
67  struct FieldSupport
68  {
69 public:
71  string meshBodyName;
73  string meshLevelName;
75  std::set< string > regionNames;
76 
85  bool add( FieldSupport const & input )
86  {
87  bool added = false;
88  if( meshBodyName == input.meshBodyName && meshLevelName == input.meshLevelName )
89  {
90  regionNames.insert( input.regionNames.begin(), input.regionNames.end() );
91  added = true;
92  }
93 
94  return added;
95  }
96  };
97 
103  enum class Connector
104  {
105  Elem,
106  Face,
107  Edge,
108  Node,
109  None,
110  Stencil
111  };
112 
117  {
118  None,
120  };
121 
127  explicit DofManager( string name );
128 
132  DofManager( DofManager const & ) = delete;
133 
137  DofManager( DofManager && ) = default;
138 
143  DofManager & operator=( DofManager const & ) = delete;
144 
149  DofManager & operator=( DofManager && ) = default;
150 
154  void clear();
155 
164  void setDomain( DomainPartition & domain );
165 
174  void addField( string const & fieldName,
176  integer components,
177  std::vector< FieldSupport > const & regions = {} );
178 
184  void addField( string const & fieldName,
186  integer components,
187  map< std::pair< string, string >, array1d< string > > const & regions );
188 
194  void setLocalReorderingType( string const & fieldName,
195  LocalReorderingType const reorderingType );
196 
202  void disableGlobalCouplingForEquation( string const & fieldName,
203  integer const c );
204 
210  void disableGlobalCouplingForEquations( string const & fieldName,
211  arrayView1d< integer const > const components );
212 
236  void addCoupling( string const & rowFieldName,
237  string const & colFieldName,
238  Connector connectivity,
239  std::vector< FieldSupport > const & regions = {},
240  bool symmetric = true );
244  void addCoupling( string const & rowFieldName,
245  string const & colFieldName,
246  Connector connectivity,
247  map< std::pair< string, string >, array1d< string > > const & regions,
248  bool symmetric = true );
249 
258  void addCoupling( string const & fieldName,
259  FluxApproximationBase const & stencils );
260 
277 
283  bool fieldExists( string const & name ) const;
284 
290  string const & getKey( string const & fieldName ) const;
291 
296  globalIndex numGlobalDofs( string const & fieldName ) const;
297 
302 
307  localIndex numLocalDofs( string const & fieldName ) const;
308 
313 
318  globalIndex rankOffset( string const & fieldName ) const;
319 
324 
329  integer numComponents( string const & fieldName = "" ) const;
330 
335 
341  FieldLocation location( string const & fieldName ) const;
342 
347  globalIndex globalOffset( string const & fieldName ) const;
348 
354 
363  template< typename CONTAINER >
364  void getLocalDofComponentLabels( CONTAINER & labels ) const
365  {
366  labels.resize( numLocalDofs() );
367  typename CONTAINER::value_type labelStart = 0;
368  auto it = labels.begin();
369  for( FieldDescription const & field : m_fields )
370  {
371  localIndex const numComp = field.numComponents;
372  localIndex const numSupp = field.numLocalDof / numComp;
373  for( localIndex i = 0; i < numSupp; ++i, it += numComp )
374  {
375  std::iota( it, it + numComp, labelStart );
376  }
377  labelStart += numComp;
378  }
379  }
380 
386 
397  string const & srcFieldName,
398  string const & dstFieldName,
399  real64 scalingFactor,
400  CompMask mask = CompMask( MAX_COMP, true ) ) const;
401 
411  template< typename SCALING_FACTOR_TYPE >
413  string const & srcFieldName,
414  string const & dstFieldName,
415  SCALING_FACTOR_TYPE const & scalingFactor,
416  CompMask mask = CompMask( MAX_COMP, true ) ) const;
417 
427  void copyFieldToVector( arrayView1d< real64 > const & localVector,
428  string const & srcFieldName,
429  string const & dstFieldName,
430  real64 scalingFactor,
431  CompMask mask = CompMask( MAX_COMP, true ) ) const;
432 
442  void addFieldToVector( arrayView1d< real64 > const & localVector,
443  string const & srcFieldName,
444  string const & dstFieldName,
445  real64 scalingFactor,
446  CompMask mask = CompMask( MAX_COMP, true ) ) const;
447 
457  std::vector< SubComponent >
458  filterDofs( std::vector< SubComponent > const & excluded ) const;
459 
466  void setupFrom( DofManager const & source,
467  std::vector< SubComponent > const & selection );
468 
481  template< typename MATRIX >
482  void makeRestrictor( std::vector< SubComponent > const & selection,
483  MPI_Comm const & comm,
484  bool transpose,
485  MATRIX & restrictor ) const;
486 
492  void printFieldInfo( std::ostream & os = std::cout ) const;
493 
494 private:
495 
499  struct FieldDescription
500  {
501  string name;
502  string key;
503  string docstring;
504  std::vector< FieldSupport > support;
506  integer numComponents = 1;
507  CompMask globallyCoupledComponents;
509  localIndex numLocalDof = 0;
510  globalIndex numGlobalDof = 0;
511  globalIndex blockOffset = 0;
512  globalIndex rankOffset = 0;
515  };
516 
520  struct CouplingDescription
521  {
522  Connector connector = Connector::None;
523  std::vector< FieldSupport > support;
524  FluxApproximationBase const * stencils = nullptr;
525  };
526 
530  localIndex getFieldIndex( string const & name ) const;
531 
536  void computeFieldDimensions( localIndex fieldIndex );
537 
543  void createIndexArray( FieldDescription const & field,
544  arrayView1d< localIndex const > const permutation );
545 
550  void removeIndexArray( FieldDescription const & field );
551 
557  array1d< localIndex > computePermutation( FieldDescription & field );
558 
565  void computePermutation( FieldDescription const & field,
566  arrayView1d< localIndex > const permutation );
567 
568 
575  void countRowLengthsOneBlock( arrayView1d< localIndex > const & rowLengths,
576  localIndex rowFieldIndex,
577  localIndex colFieldIndex ) const;
578 
579  void countRowLengthsFromStencil( arrayView1d< localIndex > const & rowLengths,
580  localIndex fieldIndex ) const;
581 
590  void setSparsityPatternOneBlock( SparsityPatternView< globalIndex > const & pattern,
591  localIndex rowFieldIndex,
592  localIndex colFieldIndex ) const;
593 
594  void setSparsityPatternFromStencil( SparsityPatternView< globalIndex > const & pattern,
595  localIndex fieldIndex ) const;
596 
597  template< int DIMS_PER_DOF >
598  void setFiniteElementSparsityPattern( SparsityPattern< globalIndex > & pattern,
599  localIndex fieldIndex ) const;
600 
611  template< typename FIELD_OP, typename POLICY, typename SCALING_FACTOR_TYPE >
612  void vectorToField( arrayView1d< real64 const > const & localVector,
613  string const & srcFieldName,
614  string const & dstFieldName,
615  SCALING_FACTOR_TYPE const & scalingFactor,
616  CompMask mask ) const;
617 
628  template< typename FIELD_OP, typename POLICY >
629  void fieldToVector( arrayView1d< real64 > const & localVector,
630  string const & srcFieldName,
631  string const & dstFieldName,
632  real64 scalingFactor,
633  CompMask mask ) const;
634 
636  string m_name;
637 
639  DomainPartition * m_domain = nullptr;
640 
642  std::vector< FieldDescription > m_fields;
643 
645  std::map< std::pair< localIndex, localIndex >, CouplingDescription > m_coupling;
646 
648  bool m_reordered = false;
649 };
650 
651 } /* namespace geos */
652 
653 #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:44
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:51
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:48
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:364
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:117
@ 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:104
@ 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:329
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:180
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:88
LvArray::SparsityPattern< COL_INDEX, INDEX_TYPE, LvArray::ChaiBuffer > SparsityPattern
Alias for Sparsity pattern class.
Definition: DataTypes.hpp:298
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
FieldLocation
Enum defining the possible location of a field on the mesh.
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176
Describes field support on a single mesh body/level.
Definition: DofManager.hpp:68
std::set< string > regionNames
list of the region names
Definition: DofManager.hpp:75
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:85
string meshLevelName
name of the mesh level
Definition: DofManager.hpp:73
string meshBodyName
name of the mesh body
Definition: DofManager.hpp:71
Describes a selection of components from a DoF field.
Definition: DofManager.hpp:59
CompMask mask
Mask that defines component selection.
Definition: DofManager.hpp:61
string fieldName
Name of the DOF field in DofManager.
Definition: DofManager.hpp:60