libMesh
petsc_dm_wrapper.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_PETSC_DM_WRAPPER_H
19 #define LIBMESH_PETSC_DM_WRAPPER_H
20 
21 #include "libmesh/libmesh_common.h"
22 #include "libmesh/petsc_macro.h"
23 
24 #ifdef LIBMESH_HAVE_PETSC
25 #if !PETSC_VERSION_LESS_THAN(3,7,3)
26 #if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
27 
28 #include <vector>
29 #include <memory>
30 #include <unordered_map>
31 #include <map>
32 
33 // PETSc includes
34 #include "libmesh/ignore_warnings.h"
35 #ifdef I
36 # define LIBMESH_SAW_I
37 #endif
38 #include <petsc.h>
39 #ifndef LIBMESH_SAW_I
40 # undef I // Avoid complex.h contamination
41 #endif
42 #include "libmesh/restore_warnings.h"
43 #include "libmesh/petsc_matrix.h"
44 #include "libmesh/petsc_vector.h"
45 
46 namespace libMesh
47 {
48  // Forward declarations
49  class System;
50  class DofObject;
51 
54  {
55  int n_dofs;
56  int mesh_dim;
57  DM * coarser_dm;
58  DM * finer_dm;
59  DM * global_dm;
64 
66  std::vector<std::vector<numeric_index_type>> dof_vec;
67 
69  std::vector<PetscInt> subfields;
70 
72  n_dofs(-12345),
73  mesh_dim(-12345),
74  coarser_dm(nullptr),
75  finer_dm(nullptr),
76  global_dm(nullptr),
77  K_interp_ptr(nullptr),
78  K_sub_interp_ptr(nullptr),
79  K_restrict_ptr(nullptr),
80  current_vec(nullptr)
81  {}
82 
83  };
84 
96  {
97  public:
98 
99  PetscDMWrapper() = default;
100 
101  ~PetscDMWrapper();
102 
104  void clear();
105 
106  void init_and_attach_petscdm(System & system, SNES & snes);
107 
108  private:
109 
111  std::vector<std::unique_ptr<DM>> _dms;
112 
114  std::vector<std::unique_ptr<PetscSection>> _sections;
115 
117  std::vector<std::unique_ptr<PetscSF>> _star_forests;
118 
120  std::vector<std::unique_ptr<PetscMatrix<Number>>> _pmtx_vec;
121 
123  std::vector<std::unique_ptr<PetscMatrix<Number>>> _subpmtx_vec;
124 
126  std::vector<std::unique_ptr<PetscDMContext>> _ctx_vec;
127 
129  std::vector<std::unique_ptr<PetscVector<Number>>> _vec_vec;
130 
132  std::vector<unsigned int> _mesh_dof_sizes;
133 
135  std::vector<unsigned int> _mesh_dof_loc_sizes;
136 
138  void init_dm_data(unsigned int n_levels, const Parallel::Communicator & comm);
139 
141 
144  DM & get_dm(unsigned int level)
145  { libmesh_assert_less(level, _dms.size());
146  return *(_dms[level].get()); }
147 
149 
152  PetscSection & get_section(unsigned int level)
153  { libmesh_assert_less(level, _sections.size());
154  return *(_sections[level].get()); }
155 
157 
160  PetscSF & get_star_forest(unsigned int level)
161  { libmesh_assert_less(level, _star_forests.size());
162  return *(_star_forests[level].get()); }
163 
165 
173  void build_section(const System & system, PetscSection & section);
174 
176 
187  void build_sf( const System & system, PetscSF & star_forest );
188 
190 
197  void set_point_range_in_section( const System & system,
198  PetscSection & section,
199  std::unordered_map<dof_id_type,dof_id_type> & node_map,
200  std::unordered_map<dof_id_type,dof_id_type> & elem_map,
201  std::map<dof_id_type,unsigned int> & scalar_map);
202 
204 
207  void add_dofs_to_section (const System & system,
208  PetscSection & section,
209  const std::unordered_map<dof_id_type,dof_id_type> & node_map,
210  const std::unordered_map<dof_id_type,dof_id_type> & elem_map,
211  const std::map<dof_id_type,unsigned int> & scalar_map);
212 
214 
218  dof_id_type check_section_n_dofs( PetscSection & section );
219 
221  void add_dofs_helper (const System & system,
222  const DofObject & dof_object,
223  dof_id_type local_id,
224  PetscSection & section);
225 
226  };
227 
228 }
229 
230 #endif // #if LIBMESH_ENABLE_AMR && LIBMESH_HAVE_METAPHYSICL
231 #endif // #if PETSC_VERSION
232 #endif // #ifdef LIBMESH_HAVE_PETSC
233 
234 #endif // LIBMESH_PETSC_DM_WRAPPER_H
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::PetscDMContext::subfields
std::vector< PetscInt > subfields
Stores subfield ids for use in subprojection matrixes on coarser DMs.
Definition: petsc_dm_wrapper.h:69
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::PetscDMWrapper::set_point_range_in_section
void set_point_range_in_section(const System &system, PetscSection &section, std::unordered_map< dof_id_type, dof_id_type > &node_map, std::unordered_map< dof_id_type, dof_id_type > &elem_map, std::map< dof_id_type, unsigned int > &scalar_map)
Helper function for build_section.
Definition: petsc_dm_wrapper.C:857
libMesh::PetscDMWrapper::get_section
PetscSection & get_section(unsigned int level)
Get reference to PetscSection for the given mesh level.
Definition: petsc_dm_wrapper.h:152
libMesh::PetscDMWrapper::_ctx_vec
std::vector< std::unique_ptr< PetscDMContext > > _ctx_vec
Vector of internal PetscDM context structs for all grid levels.
Definition: petsc_dm_wrapper.h:126
libMesh::PetscDMWrapper::get_dm
DM & get_dm(unsigned int level)
Get reference to DM for the given mesh level.
Definition: petsc_dm_wrapper.h:144
libMesh::PetscDMWrapper::add_dofs_helper
void add_dofs_helper(const System &system, const DofObject &dof_object, dof_id_type local_id, PetscSection &section)
Helper function to reduce code duplication when setting dofs in section.
Definition: petsc_dm_wrapper.C:1039
libMesh::PetscDMContext::n_dofs
int n_dofs
Definition: petsc_dm_wrapper.h:55
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscDMWrapper::build_section
void build_section(const System &system, PetscSection &section)
Takes System, empty PetscSection and populates the PetscSection.
Definition: petsc_dm_wrapper.C:729
libMesh::PetscDMWrapper::_dms
std::vector< std::unique_ptr< DM > > _dms
Vector of DMs for all grid levels.
Definition: petsc_dm_wrapper.h:111
libMesh::PetscDMContext::current_vec
PetscVector< libMesh::Number > * current_vec
Definition: petsc_dm_wrapper.h:63
libMesh::PetscDMWrapper::add_dofs_to_section
void add_dofs_to_section(const System &system, PetscSection &section, const std::unordered_map< dof_id_type, dof_id_type > &node_map, const std::unordered_map< dof_id_type, dof_id_type > &elem_map, const std::map< dof_id_type, unsigned int > &scalar_map)
Helper function for build_section.
Definition: petsc_dm_wrapper.C:972
libMesh::PetscDMContext
Struct to house data regarding where in the mesh hierarchy we are located.
Definition: petsc_dm_wrapper.h:53
libMesh::PetscDMContext::dof_vec
std::vector< std::vector< numeric_index_type > > dof_vec
Stores local dofs for each var for use in subprojection matrixes.
Definition: petsc_dm_wrapper.h:66
libMesh::PetscDMWrapper::_mesh_dof_loc_sizes
std::vector< unsigned int > _mesh_dof_loc_sizes
Stores n_local_dofs for each grid level, to be used for projection vector sizing.
Definition: petsc_dm_wrapper.h:135
libMesh::MeshTools::n_levels
unsigned int n_levels(const MeshBase &mesh)
Definition: mesh_tools.C:656
libMesh::DofObject
The DofObject defines an abstract base class for objects that have degrees of freedom associated with...
Definition: dof_object.h:53
libMesh::PetscDMContext::global_dm
DM * global_dm
Definition: petsc_dm_wrapper.h:59
libMesh::PetscDMWrapper::_subpmtx_vec
std::vector< std::unique_ptr< PetscMatrix< Number > > > _subpmtx_vec
Vector of sub projection matrixes for all grid levels for fieldsplit.
Definition: petsc_dm_wrapper.h:123
libMesh::PetscDMWrapper::_sections
std::vector< std::unique_ptr< PetscSection > > _sections
Vector of PETScSections for all grid levels.
Definition: petsc_dm_wrapper.h:114
libMesh::PetscDMContext::K_sub_interp_ptr
PetscMatrix< libMesh::Number > * K_sub_interp_ptr
Definition: petsc_dm_wrapper.h:61
libMesh::PetscDMContext::K_restrict_ptr
PetscMatrix< libMesh::Number > * K_restrict_ptr
Definition: petsc_dm_wrapper.h:62
libMesh::PetscDMWrapper::_star_forests
std::vector< std::unique_ptr< PetscSF > > _star_forests
Vector of star forests for all grid levels.
Definition: petsc_dm_wrapper.h:117
libMesh::PetscDMWrapper
This class defines a wrapper around the PETSc DM infrastructure.
Definition: petsc_dm_wrapper.h:95
libMesh::PetscDMWrapper::_mesh_dof_sizes
std::vector< unsigned int > _mesh_dof_sizes
Stores n_dofs for each grid level, to be used for projection matrix sizing.
Definition: petsc_dm_wrapper.h:132
libMesh::PetscDMWrapper::~PetscDMWrapper
~PetscDMWrapper()
Definition: petsc_dm_wrapper.C:425
libMesh::PetscDMWrapper::build_sf
void build_sf(const System &system, PetscSF &star_forest)
Takes System, empty PetscSF and populates the PetscSF.
Definition: petsc_dm_wrapper.C:795
libMesh::PetscDMContext::mesh_dim
int mesh_dim
Definition: petsc_dm_wrapper.h:56
libMesh::PetscDMContext::K_interp_ptr
PetscMatrix< libMesh::Number > * K_interp_ptr
Definition: petsc_dm_wrapper.h:60
libMesh::PetscVector< libMesh::Number >
libMesh::PetscDMWrapper::init_dm_data
void init_dm_data(unsigned int n_levels, const Parallel::Communicator &comm)
Init all the n_mesh_level dependent data structures.
Definition: petsc_dm_wrapper.C:1096
libMesh::PetscDMWrapper::init_and_attach_petscdm
void init_and_attach_petscdm(System &system, SNES &snes)
Definition: petsc_dm_wrapper.C:445
libMesh::PetscDMWrapper::clear
void clear()
Destroys and clears all build DM-related data.
Definition: petsc_dm_wrapper.C:430
libMesh::PetscDMWrapper::_pmtx_vec
std::vector< std::unique_ptr< PetscMatrix< Number > > > _pmtx_vec
Vector of projection matrixes for all grid levels.
Definition: petsc_dm_wrapper.h:120
libMesh::PetscMatrix< libMesh::Number >
libMesh::PetscDMContext::PetscDMContext
PetscDMContext()
Definition: petsc_dm_wrapper.h:71
libMesh::PetscDMWrapper::check_section_n_dofs
dof_id_type check_section_n_dofs(PetscSection &section)
Helper function to sanity check PetscSection construction.
Definition: petsc_dm_wrapper.C:1072
libMesh::PetscDMWrapper::get_star_forest
PetscSF & get_star_forest(unsigned int level)
Get reference to PetscSF for the given mesh level.
Definition: petsc_dm_wrapper.h:160
libMesh::PetscDMContext::finer_dm
DM * finer_dm
Definition: petsc_dm_wrapper.h:58
libMesh::PetscDMWrapper::_vec_vec
std::vector< std::unique_ptr< PetscVector< Number > > > _vec_vec
Vector of solution vectors for all grid levels.
Definition: petsc_dm_wrapper.h:129
libMesh::PetscDMContext::coarser_dm
DM * coarser_dm
Definition: petsc_dm_wrapper.h:57
libMesh::PetscDMWrapper::PetscDMWrapper
PetscDMWrapper()=default