sensitivity_data.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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"
27 #include "libmesh/system.h"
28 
29 // C++ Includes -----------------------------------
30 #include <vector>
31 
32 namespace libMesh
33 {
34 
35  // Forward declaractions
36  class QoISet;
37 
43 {
44 public:
45  class Row
46  {
47  public:
48  Row(SensitivityData &sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
49 
50  Number& operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
51  private:
53  unsigned int _qoi;
54  };
55 
56  class ConstRow
57  {
58  public:
59  ConstRow(const SensitivityData &sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
60 
61  const Number& operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
62  private:
64  unsigned int _qoi;
65  };
66 
71 
76  SensitivityData(const QoISet& qoi_indices,
77  const System& sys,
78  const ParameterVector& parameter_vector);
79 
83  void clear() { _grad_data.clear(); }
84 
89  void allocate_data(const QoISet &qoi_indices,
90  const System& sys,
91  const ParameterVector& parameter_vector);
92 
97  void allocate_hessian_data(const QoISet &qoi_indices,
98  const System& sys,
99  const ParameterVector& parameter_vector);
100 
105  const Number& derivative (unsigned int qoi_index,
106  unsigned int parameter_index) const;
107 
112  const Number& second_derivative (unsigned int qoi_index,
113  unsigned int parameter_index1,
114  unsigned int parameter_index2) const;
115 
120  Number& derivative (unsigned int qoi_index,
121  unsigned int parameter_index);
122 
128  Number& second_derivative (unsigned int qoi_index,
129  unsigned int parameter_index1,
130  unsigned int parameter_index2);
131 
136  ConstRow operator[] (unsigned int qoi) const { return ConstRow(*this, qoi); }
137 
138  Row operator[] (unsigned int qoi) { return Row(*this, qoi); }
139 
140 private:
144  std::vector<std::vector<Number> > _grad_data;
145  std::vector<std::vector<std::vector<Number> > > _hess_data;
146 };
147 
148 
149 
150 // ------------------------------------------------------------
151 // SensitivityData inline methods
152 
153 
154 
155 inline
157  const System& sys,
158  const ParameterVector& parameter_vector)
159 {
160  this->allocate_data(qoi_indices, sys, parameter_vector);
161 }
162 
163 
164 
165 inline
166 void SensitivityData::allocate_data(const QoISet &qoi_indices,
167  const System& sys,
168  const ParameterVector& parameter_vector)
169 {
170  const std::size_t Np = parameter_vector.size();
171  const unsigned int Nq =
172  libmesh_cast_int<unsigned int>(sys.qoi.size());
173 
174  if (_grad_data.size() < Nq)
175  _grad_data.resize(Nq);
176 
177  for (unsigned int i=0; i != Nq; ++i)
178  if (qoi_indices.has_index(i))
179  {
180  _grad_data[i].clear();
181  _grad_data[i].resize(Np);
182  }
183 }
184 
185 
186 
187 inline
189  const System& sys,
190  const ParameterVector& parameter_vector)
191 {
192  const std::size_t Np = parameter_vector.size();
193  const unsigned int Nq =
194  libmesh_cast_int<unsigned int>(sys.qoi.size());
195 
196  if (_hess_data.size() < Nq)
197  _hess_data.resize(Nq);
198 
199  for (unsigned int i=0; i != Nq; ++i)
200  if (qoi_indices.has_index(i))
201  {
202  _hess_data[i].clear();
203  _hess_data[i].resize(Np);
204  for (std::size_t j=0; j != Np; ++j)
205  _hess_data[i][j].resize(Np);
206  }
207 }
208 
209 
210 
211 inline
212 const Number& SensitivityData::derivative(unsigned int qoi_index,
213  unsigned int parameter_index) const
214 {
215  libmesh_assert_less (qoi_index, _grad_data.size());
216  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
217 
218  return _grad_data[qoi_index][parameter_index];
219 }
220 
221 
222 
223 inline
224 Number& SensitivityData::derivative(unsigned int qoi_index,
225  unsigned int parameter_index)
226 {
227  libmesh_assert_less (qoi_index, _grad_data.size());
228  libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
229 
230  return _grad_data[qoi_index][parameter_index];
231 }
232 
233 
234 
235 inline
236 const Number& SensitivityData::second_derivative(unsigned int qoi_index,
237  unsigned int parameter_index1,
238  unsigned int parameter_index2) const
239 {
240  libmesh_assert_less (qoi_index, _hess_data.size());
241  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
242  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
243 
244  return _hess_data[qoi_index][parameter_index1][parameter_index2];
245 }
246 
247 
248 
249 inline
251  unsigned int parameter_index1,
252  unsigned int parameter_index2)
253 {
254  libmesh_assert_less (qoi_index, _hess_data.size());
255  libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
256  libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
257 
258  return _hess_data[qoi_index][parameter_index1][parameter_index2];
259 }
260 
261 } // namespace libMesh
262 
263 #endif // LIBMESH_SENSITIVITY_DATA_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo