libMesh
sensitivity_data.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 =
176  cast_int<unsigned int>(sys.qoi.size());
177 
178  if (_grad_data.size() < Nq)
179  _grad_data.resize(Nq);
180 
181  for (unsigned int i=0; i != Nq; ++i)
182  if (qoi_indices.has_index(i))
183  {
184  _grad_data[i].clear();
185  _grad_data[i].resize(Np);
186  }
187 }
188 
189 
190 
191 inline
193  const System & sys,
194  const ParameterVector & parameter_vector)
195 {
196  const std::size_t Np = parameter_vector.size();
197  const unsigned int Nq =
198  cast_int<unsigned int>(sys.qoi.size());
199 
200  if (_hess_data.size() < Nq)
201  _hess_data.resize(Nq);
202 
203  for (unsigned int i=0; i != Nq; ++i)
204  if (qoi_indices.has_index(i))
205  {
206  _hess_data[i].clear();
207  _hess_data[i].resize(Np);
208  for (std::size_t j=0; j != Np; ++j)
209  _hess_data[i][j].resize(Np);
210  }
211 }
212 
213 
214 
215 inline
216 const Number & SensitivityData::derivative(unsigned int qoi_index,
217  unsigned int parameter_index) const
218 {
219  libmesh_assert_less (qoi_index, _grad_data.size());
220  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
221 
222  return _grad_data[qoi_index][parameter_index];
223 }
224 
225 
226 
227 inline
228 Number & SensitivityData::derivative(unsigned int qoi_index,
229  unsigned int parameter_index)
230 {
231  libmesh_assert_less (qoi_index, _grad_data.size());
232  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
233 
234  return _grad_data[qoi_index][parameter_index];
235 }
236 
237 
238 
239 inline
240 const Number & SensitivityData::second_derivative(unsigned int qoi_index,
241  unsigned int parameter_index1,
242  unsigned int parameter_index2) const
243 {
244  libmesh_assert_less (qoi_index, _hess_data.size());
245  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
246  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
247 
248  return _hess_data[qoi_index][parameter_index1][parameter_index2];
249 }
250 
251 
252 
253 inline
255  unsigned int parameter_index1,
256  unsigned int parameter_index2)
257 {
258  libmesh_assert_less (qoi_index, _hess_data.size());
259  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
260  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
261 
262  return _hess_data[qoi_index][parameter_index1][parameter_index2];
263 }
264 
265 } // namespace libMesh
266 
267 #endif // LIBMESH_SENSITIVITY_DATA_H
SensitivityData()
Default constructor: empty data set.
std::vector< std::vector< std::vector< Number > > > _hess_data
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
std::vector< std::vector< Number > > _grad_data
Data storage; currently pretty trivial.
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...
ImplicitSystem & sys
const Number & second_derivative(unsigned int qoi_index, unsigned int parameter_index1, unsigned int parameter_index2) const
The libMesh namespace provides an interface to certain functionality in the library.
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...
Data structure for holding completed parameter sensitivity calculations.
std::vector< Number > qoi
Values of the quantities of interest.
Definition: system.h:1553
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
void clear()
Clears and deallocates all data.
std::size_t size() const
const Number & derivative(unsigned int qoi_index, unsigned int parameter_index) const
ConstRow(const SensitivityData &sd, unsigned int qoi)
Row(SensitivityData &sd, unsigned int qoi)
Number & operator[](unsigned int parameter)
bool has_index(unsigned int) const
Return whether or not this index is in the set to be calculated.
Definition: qoi_set.h:221