MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
mat_null_space.hpp
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef __mast_libmesh_wrapper_elasticity_null_space_h__
21 #define __mast_libmesh_wrapper_elasticity_null_space_h__
22 
23 // C++ includes
24 #include <limits>
25 
26 // MAST includes
28 #include <mast/base/exceptions.hpp>
29 
30 // libMesh includes
31 #include <libmesh/system.h>
32 #include <libmesh/dof_map.h>
33 #include <libmesh/mesh_base.h>
34 #include <libmesh/petsc_vector.h>
35 #include <libmesh/node.h>
36 
37 // PETSc includes
38 #include <petscmat.h>
39 
40 namespace MAST {
41 namespace Physics {
42 namespace Conduction {
43 namespace libMeshWrapper {
44 
45 class NullSpace {
46 
47 public:
48 
49  NullSpace(libMesh::System& sys, uint_t dim):
50  _sys (sys),
51  _dim (dim) {
52 
53  Assert1(_dim > 0, _dim, "Invalid dimension");
54  Assert2(_sys.n_vars() == 1,
55  _sys.n_vars(), 1,
56  "Number of unknowns should be equal to one");
57 
58  // make sure all variable types are LAGRANGE
59  Assert0(_sys.variable_type(0).family == libMesh::LAGRANGE,
60  "Variables are expected to be LAGRANGE");
61 
62  // now initialize
63  _init();
64  }
65 
66  virtual ~NullSpace() {
67 
68  PetscErrorCode ierr = 0;
69 
70  ierr = MatNullSpaceDestroy(&_mns);
71  CHKERRABORT(_sys.comm().get(), ierr);
72  }
73 
74 
75  inline MatNullSpace get() {
76 
77  Assert0(_mns, "Object not initialized");
78 
79  return _mns;
80  }
81 
82  inline void attach_to_matrix(Mat m) {
83 
84  PetscErrorCode ierr = 0;
85 
86  ierr = MatSetNearNullSpace(m, _mns);
87  CHKERRABORT(_sys.comm().get(), ierr);
88  }
89 
90 
91 private:
92 
93  inline void _init() {
94 
95  PetscErrorCode
96  ierr = 0;
97 
98  std::unique_ptr<libMesh::NumericVector<real_t>>
99  vec(_sys.solution->zero_clone().release());
100 
101  libMesh::MeshBase::const_node_iterator
102  it = _sys.get_mesh().local_nodes_begin(),
103  end = _sys.get_mesh().local_nodes_end();
104 
105  for ( ; it != end; it++)
106  // constant temperature
107  vec->set((*it)->dof_number(0, 0, 0), 1.);
108 
109  // close the vector and scale it to unit norm
110  vec->close();
111 
112  real_t
113  l2 = vec->l2_norm();
114 
115  vec->scale(1./l2);
116 
117  l2 = vec->l2_norm();
118 
119  // vector of vectors to be sent to PETSc
120  std::vector<Vec> v(1);
121  v[0] = dynamic_cast<libMesh::PetscVector<real_t>*>(vec.get())->vec();
122 
123  ierr = MatNullSpaceCreate(_sys.comm().get(),
124  PETSC_FALSE,
125  1,
126  &v[0],
127  &_mns);
128  CHKERRABORT(_sys.comm().get(), ierr);
129  }
130 
131 
132 
133 
134  libMesh::System &_sys;
135  const uint_t _dim;
136  MatNullSpace _mns;
137 
138 };
139 } // namespace MAST
140 } // namespace Conduction
141 } // namespace Physics
142 } // namespace libMeshWrapper
143 
144 
145 #endif // __mast_libmesh_wrapper_elasticity_null_space_h__
#define Assert1(cond, v1, msg)
Definition: exceptions.hpp:143
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
unsigned int uint_t
#define Assert2(cond, v1, v2, msg)
Definition: exceptions.hpp:152
double real_t