libMesh
sensitivity_data.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 
19 
20 #ifndef LIBMESH_SENSITIVITY_DATA_H
21 #define LIBMESH_SENSITIVITY_DATA_H
22 
23 
24 // Local Includes
25 #include "libmesh/libmesh_common.h"
26 #include "libmesh/parameter_vector.h"
27 #include "libmesh/system.h"
28 
29 // C++ Includes
30 #include <vector>
31 
32 namespace libMesh
33 {
34 
35 // Forward declarations
36 class QoISet;
37 
47 {
48 public:
49  class Row
50  {
51  public:
52  Row(SensitivityData & sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
53 
54  Number & operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
55  private:
57  unsigned int _qoi;
58  };
59 
60  class ConstRow
61  {
62  public:
63  ConstRow(const SensitivityData & sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
64 
65  const Number & operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
66  private:
68  unsigned int _qoi;
69  };
70 
75 
80  SensitivityData(const QoISet & qoi_indices,
81  const System & sys,
82  const ParameterVector & parameter_vector);
83 
87  void clear() { _grad_data.clear(); }
88 
93  void allocate_data(const QoISet & qoi_indices,
94  const System & sys,
95  const ParameterVector & parameter_vector);
96 
101  void allocate_hessian_data(const QoISet & qoi_indices,
102  const System & sys,
103  const ParameterVector & parameter_vector);
104 
109  const Number & derivative (unsigned int qoi_index,
110  unsigned int parameter_index) const;
111 
116  const Number & second_derivative (unsigned int qoi_index,
117  unsigned int parameter_index1,
118  unsigned int parameter_index2) const;
119 
124  Number & derivative (unsigned int qoi_index,
125  unsigned int parameter_index);
126 
132  Number & second_derivative (unsigned int qoi_index,
133  unsigned int parameter_index1,
134  unsigned int parameter_index2);
135 
140  ConstRow operator[] (unsigned int qoi) const { return ConstRow(*this, qoi); }
141 
142  Row operator[] (unsigned int qoi) { return Row(*this, qoi); }
143 
144 private:
148  std::vector<std::vector<Number>> _grad_data;
149  std::vector<std::vector<std::vector<Number>>> _hess_data;
150 };
151 
152 
153 
154 // ------------------------------------------------------------
155 // SensitivityData inline methods
156 
157 
158 
159 inline
161  const System & sys,
162  const ParameterVector & parameter_vector)
163 {
164  this->allocate_data(qoi_indices, sys, parameter_vector);
165 }
166 
167 
168 
169 inline
170 void SensitivityData::allocate_data(const QoISet & qoi_indices,
171  const System & sys,
172  const ParameterVector & parameter_vector)
173 {
174  const std::size_t Np = parameter_vector.size();
175  const unsigned int Nq = sys.n_qois();
176 
177  if (_grad_data.size() < Nq)
178  _grad_data.resize(Nq);
179 
180  for (unsigned int i=0; i != Nq; ++i)
181  if (qoi_indices.has_index(i))
182  {
183  _grad_data[i].clear();
184  _grad_data[i].resize(Np);
185  }
186 }
187 
188 
189 
190 inline
192  const System & sys,
193  const ParameterVector & parameter_vector)
194 {
195  const std::size_t Np = parameter_vector.size();
196  const unsigned int Nq = sys.n_qois();
197 
198  if (_hess_data.size() < Nq)
199  _hess_data.resize(Nq);
200 
201  for (unsigned int i=0; i != Nq; ++i)
202  if (qoi_indices.has_index(i))
203  {
204  _hess_data[i].clear();
205  _hess_data[i].resize(Np);
206  for (std::size_t j=0; j != Np; ++j)
207  _hess_data[i][j].resize(Np);
208  }
209 }
210 
211 
212 
213 inline
214 const Number & SensitivityData::derivative(unsigned int qoi_index,
215  unsigned int parameter_index) const
216 {
217  libmesh_assert_less (qoi_index, _grad_data.size());
218  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
219 
220  return _grad_data[qoi_index][parameter_index];
221 }
222 
223 
224 
225 inline
226 Number & SensitivityData::derivative(unsigned int qoi_index,
227  unsigned int parameter_index)
228 {
229  libmesh_assert_less (qoi_index, _grad_data.size());
230  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
231 
232  return _grad_data[qoi_index][parameter_index];
233 }
234 
235 
236 
237 inline
238 const Number & SensitivityData::second_derivative(unsigned int qoi_index,
239  unsigned int parameter_index1,
240  unsigned int parameter_index2) const
241 {
242  libmesh_assert_less (qoi_index, _hess_data.size());
243  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
244  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
245 
246  return _hess_data[qoi_index][parameter_index1][parameter_index2];
247 }
248 
249 
250 
251 inline
253  unsigned int parameter_index1,
254  unsigned int parameter_index2)
255 {
256  libmesh_assert_less (qoi_index, _hess_data.size());
257  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
258  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
259 
260  return _hess_data[qoi_index][parameter_index1][parameter_index2];
261 }
262 
263 } // namespace libMesh
264 
265 #endif // LIBMESH_SENSITIVITY_DATA_H
libMesh::ParameterVector::size
std::size_t size() const
Definition: parameter_vector.h:90
libMesh::SensitivityData::Row
Definition: sensitivity_data.h:49
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::SparsityPattern::Row
std::vector< dof_id_type, Threads::scalable_allocator< dof_id_type > > Row
Definition: sparsity_pattern.h:51
libMesh::QoISet::has_index
bool has_index(std::size_t) const
Return whether or not this index is in the set to be calculated.
Definition: qoi_set.h:221
libMesh::SensitivityData::Row::Row
Row(SensitivityData &sd, unsigned int qoi)
Definition: sensitivity_data.h:52
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::SensitivityData::clear
void clear()
Clears and deallocates all data.
Definition: sensitivity_data.h:87
libMesh::SensitivityData::allocate_data
void allocate_data(const QoISet &qoi_indices, const System &sys, const ParameterVector &parameter_vector)
Given QoISet and ParameterVector, allocates space for all required first derivative data.
Definition: sensitivity_data.h:170
libMesh::SensitivityData::ConstRow::_qoi
unsigned int _qoi
Definition: sensitivity_data.h:68
libMesh::SensitivityData
Data structure for holding completed parameter sensitivity calculations.
Definition: sensitivity_data.h:46
libMesh::SensitivityData::_grad_data
std::vector< std::vector< Number > > _grad_data
Data storage; currently pretty trivial.
Definition: sensitivity_data.h:148
libMesh::System::n_qois
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition: system.h:2328
libMesh::QoISet
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
libMesh::ParameterVector
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Definition: parameter_vector.h:44
libMesh::SensitivityData::operator[]
ConstRow operator[](unsigned int qoi) const
Vector address type operator: sd[q][p] is an alias for sd.derivative(q,p)
Definition: sensitivity_data.h:140
libMesh::SensitivityData::_hess_data
std::vector< std::vector< std::vector< Number > > > _hess_data
Definition: sensitivity_data.h:149
libMesh::SensitivityData::ConstRow
Definition: sensitivity_data.h:60
libMesh::SensitivityData::Row::operator[]
Number & operator[](unsigned int parameter)
Definition: sensitivity_data.h:54
libMesh::SensitivityData::allocate_hessian_data
void allocate_hessian_data(const QoISet &qoi_indices, const System &sys, const ParameterVector &parameter_vector)
Given QoISet and ParameterVector, allocates space for all required second derivative data.
Definition: sensitivity_data.h:191
libMesh::SensitivityData::derivative
const Number & derivative(unsigned int qoi_index, unsigned int parameter_index) const
Definition: sensitivity_data.h:214
libMesh::SensitivityData::Row::_qoi
unsigned int _qoi
Definition: sensitivity_data.h:57
libMesh::SensitivityData::second_derivative
const Number & second_derivative(unsigned int qoi_index, unsigned int parameter_index1, unsigned int parameter_index2) const
Definition: sensitivity_data.h:238
libMesh::SensitivityData::Row::_sd
SensitivityData & _sd
Definition: sensitivity_data.h:56
libMesh::SensitivityData::ConstRow::_sd
const SensitivityData & _sd
Definition: sensitivity_data.h:67
libMesh::SensitivityData::ConstRow::ConstRow
ConstRow(const SensitivityData &sd, unsigned int qoi)
Definition: sensitivity_data.h:63
libMesh::SensitivityData::SensitivityData
SensitivityData()
Default constructor: empty data set.
Definition: sensitivity_data.h:74
libMesh::SensitivityData::ConstRow::operator[]
const Number & operator[](unsigned int parameter)
Definition: sensitivity_data.h:65