MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
accessor.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_accessor_h__
21 #define __mast_libmesh_accessor_h__
22 
23 // MAST includes
25 #include <mast/base/exceptions.hpp>
27 
28 // libMesh includes
29 #include <libmesh/system.h>
30 #include <libmesh/dof_map.h>
31 
32 
33 namespace MAST {
34 namespace Base {
35 namespace Assembly {
36 namespace libMeshWrapper {
37 
38 template <typename ScalarType, typename VecType>
39 class Accessor {
40 
41 public:
42 
43  Accessor(const libMesh::System& sys, const VecType& vec):
44  _sys (&sys),
45  _vec (&vec)
46  { }
47 
49  _sys (nullptr),
50  _vec (nullptr)
51  { }
52 
53  inline void set_system(libMesh::System& sys) { _sys = &sys;}
54  inline void set_vec(const VecType& vec) { _vec = &vec;}
55  inline const std::vector<libMesh::dof_id_type>& dof_indices() const {return _dof_ids;}
56  inline std::vector<libMesh::dof_id_type>& dof_indices() {return _dof_ids;}
57  inline uint_t n_dofs() const { return _dof_ids.size();}
58  inline uint_t size() const { return _dof_ids.size();}
59 
60  inline ScalarType operator() (uint_t i) const {
61 
62  Assert2(i < _dof_ids.size(),
63  i, _dof_ids.size(),
64  "Invalid element degree-of-freedom index");
65 
66  return (*_vec)(_dof_ids[i]);
67  }
68 
69  inline void init(const libMesh::Elem& e) {
70 
71  _dof_ids.clear();
72  _sys->get_dof_map().dof_indices (&e, _dof_ids);
73  }
74 
75  inline void init_dof_id_set(std::set<uint_t>& dofs) {
76 
77  dofs.clear();
78 
79  for (uint_t i=0; i<_dof_ids.size(); i++)
80  dofs.insert(_dof_ids[i]);
81  }
82 
83  template <typename Vec2Type>
84  inline ScalarType dot(const Vec2Type& v) const {
85 
86  Assert2(this->size() == v.size(),
87  this->size(), v.size(),
88  "Inconsistent dimensions");
89  ScalarType res = 0.;
90 
91  for (uint_t i = 0; i<this->size(); i++)
92  res += (*this)(i) * MAST::Numerics::Utility::get(v, i);
93 
94  return res;
95  }
96 
97 private:
98 
99  const libMesh::System *_sys;
100  const VecType *_vec;
101  std::vector<libMesh::dof_id_type> _dof_ids;
102 };
103 
104 } // namespace libMeshWrapper
105 } // namespace Assembly
106 } // namespace Base
107 } // namespace MAST
108 
109 #endif // __mast_libmesh_accessor_h__
110 
ScalarType get(const std::vector< ScalarType > &v, uint_t i)
Definition: utility.hpp:232
std::vector< libMesh::dof_id_type > & dof_indices()
Definition: accessor.hpp:56
void init_dof_id_set(std::set< uint_t > &dofs)
Definition: accessor.hpp:75
ScalarType operator()(uint_t i) const
Definition: accessor.hpp:60
void init(const libMesh::Elem &e)
Definition: accessor.hpp:69
std::vector< libMesh::dof_id_type > _dof_ids
Definition: accessor.hpp:101
void set_system(libMesh::System &sys)
Definition: accessor.hpp:53
const std::vector< libMesh::dof_id_type > & dof_indices() const
Definition: accessor.hpp:55
Accessor(const libMesh::System &sys, const VecType &vec)
Definition: accessor.hpp:43
unsigned int uint_t
#define Assert2(cond, v1, v2, msg)
Definition: exceptions.hpp:152
ScalarType dot(const Vec2Type &v) const
Definition: accessor.hpp:84