MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
material_point_storage.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_material_point_storage_h__
21 #define __mast_material_point_storage_h__
22 
23 // MAST includes
25 #include <mast/base/exceptions.hpp>
26 
27 // MPI includes
28 #include <mpi.h>
29 
30 
31 namespace MAST {
32 namespace Base {
33 namespace MaterialPoint {
34 
35 
36 template <typename ScalarType, uint_t NComponents>
37 class Storage {
38 
39 public:
40 
41  using scalar_t = ScalarType;
42  using view_t = Eigen::Map<typename Eigen::Matrix<scalar_t, NComponents, 1>>;
43 
44 
45  Storage(MPI_Comm comm):
46  _initialized (false),
47  _n_points (0),
48  _data (nullptr),
49  _comm (comm)
50  { }
51 
52  virtual ~Storage() {
53 
54  if (_data) delete _data;
55  }
56 
60  inline void init(uint_t n_points) {
61 
62  Assert0(!_initialized, "Object already initialized");
63 
64  _n_points = n_points;
65 
66  _data = new scalar_t[n_points*NComponents];
67 
68  _initialized = true;
69 
70  this->zero();
71  }
72 
73 
77  inline uint_t size() const {
78 
79  return _n_points * NComponents;
80  }
81 
82 
83  inline scalar_t* data() {
84 
85  Assert0(_initialized, "Object must be initialized");
86  return _data;
87  }
88 
89 
90  inline const scalar_t* data() const {
91 
92  Assert0(_initialized, "Object must be initialized");
93  return _data;
94  }
95 
96 
97  inline void zero() {
98 
99  Assert0(_initialized, "Object must be initialized");
100  std::fill(_data, _data+_n_points*NComponents, 0.);
101  }
102 
106  inline view_t data(uint_t pt) {
107 
108  Assert0(_initialized, "Object must be initialized");
109  return view_t(_data + NComponents*pt, NComponents, 1);
110  }
111 
112 
116  inline const view_t data(uint_t pt) const {
117 
118  Assert0(_initialized, "Object must be initialized");
119  return view_t(_data + NComponents*pt, NComponents, 1);
120  }
121 
122 
123 private:
124 
128  MPI_Comm _comm;
129 };
130 
131 } // namespace MaterialPoint
132 } // namespace Base
133 } // namespace MAST
134 
135 #endif // __mast_material_point_storage_h__
Eigen::Map< typename Eigen::Matrix< scalar_t, NComponents, 1 > > view_t
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
void init(uint_t n_points)
initializes the storage for n_points with NComponents data on each point
unsigned int uint_t