$extrastylesheet
sensitivity_data.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_SENSITIVITY_DATA_H
00021 #define LIBMESH_SENSITIVITY_DATA_H
00022 
00023 
00024 // Local Includes -----------------------------------
00025 #include "libmesh/libmesh_common.h"
00026 #include "libmesh/parameter_vector.h"
00027 #include "libmesh/system.h"
00028 
00029 // C++ Includes   -----------------------------------
00030 #include <vector>
00031 
00032 namespace libMesh
00033 {
00034 
00035 // Forward declaractions
00036 class QoISet;
00037 
00042 class SensitivityData
00043 {
00044 public:
00045   class Row
00046   {
00047   public:
00048     Row(SensitivityData &sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
00049 
00050     Number& operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
00051   private:
00052     SensitivityData &_sd;
00053     unsigned int _qoi;
00054   };
00055 
00056   class ConstRow
00057   {
00058   public:
00059     ConstRow(const SensitivityData &sd, unsigned int qoi) : _sd(sd), _qoi(qoi) {}
00060 
00061     const Number& operator[] (unsigned int parameter) { return _sd.derivative(_qoi, parameter); }
00062   private:
00063     const SensitivityData &_sd;
00064     unsigned int _qoi;
00065   };
00066 
00070   SensitivityData() {}
00071 
00076   SensitivityData(const QoISet& qoi_indices,
00077                   const System& sys,
00078                   const ParameterVector& parameter_vector);
00079 
00083   void clear() { _grad_data.clear(); }
00084 
00089   void allocate_data(const QoISet &qoi_indices,
00090                      const System& sys,
00091                      const ParameterVector& parameter_vector);
00092 
00097   void allocate_hessian_data(const QoISet &qoi_indices,
00098                              const System& sys,
00099                              const ParameterVector& parameter_vector);
00100 
00105   const Number& derivative (unsigned int qoi_index,
00106                             unsigned int parameter_index) const;
00107 
00112   const Number& second_derivative (unsigned int qoi_index,
00113                                    unsigned int parameter_index1,
00114                                    unsigned int parameter_index2) const;
00115 
00120   Number& derivative (unsigned int qoi_index,
00121                       unsigned int parameter_index);
00122 
00128   Number& second_derivative (unsigned int qoi_index,
00129                              unsigned int parameter_index1,
00130                              unsigned int parameter_index2);
00131 
00136   ConstRow operator[] (unsigned int qoi) const { return ConstRow(*this, qoi); }
00137 
00138   Row operator[] (unsigned int qoi) { return Row(*this, qoi); }
00139 
00140 private:
00144   std::vector<std::vector<Number> > _grad_data;
00145   std::vector<std::vector<std::vector<Number> > > _hess_data;
00146 };
00147 
00148 
00149 
00150 // ------------------------------------------------------------
00151 // SensitivityData inline methods
00152 
00153 
00154 
00155 inline
00156 SensitivityData::SensitivityData(const QoISet &qoi_indices,
00157                                  const System& sys,
00158                                  const ParameterVector& parameter_vector)
00159 {
00160   this->allocate_data(qoi_indices, sys, parameter_vector);
00161 }
00162 
00163 
00164 
00165 inline
00166 void SensitivityData::allocate_data(const QoISet &qoi_indices,
00167                                     const System& sys,
00168                                     const ParameterVector& parameter_vector)
00169 {
00170   const std::size_t Np = parameter_vector.size();
00171   const unsigned int Nq =
00172     cast_int<unsigned int>(sys.qoi.size());
00173 
00174   if (_grad_data.size() < Nq)
00175     _grad_data.resize(Nq);
00176 
00177   for (unsigned int i=0; i != Nq; ++i)
00178     if (qoi_indices.has_index(i))
00179       {
00180         _grad_data[i].clear();
00181         _grad_data[i].resize(Np);
00182       }
00183 }
00184 
00185 
00186 
00187 inline
00188 void SensitivityData::allocate_hessian_data(const QoISet &qoi_indices,
00189                                             const System& sys,
00190                                             const ParameterVector& parameter_vector)
00191 {
00192   const std::size_t Np = parameter_vector.size();
00193   const unsigned int Nq =
00194     cast_int<unsigned int>(sys.qoi.size());
00195 
00196   if (_hess_data.size() < Nq)
00197     _hess_data.resize(Nq);
00198 
00199   for (unsigned int i=0; i != Nq; ++i)
00200     if (qoi_indices.has_index(i))
00201       {
00202         _hess_data[i].clear();
00203         _hess_data[i].resize(Np);
00204         for (std::size_t j=0; j != Np; ++j)
00205           _hess_data[i][j].resize(Np);
00206       }
00207 }
00208 
00209 
00210 
00211 inline
00212 const Number& SensitivityData::derivative(unsigned int qoi_index,
00213                                           unsigned int parameter_index) const
00214 {
00215   libmesh_assert_less (qoi_index, _grad_data.size());
00216   libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
00217 
00218   return _grad_data[qoi_index][parameter_index];
00219 }
00220 
00221 
00222 
00223 inline
00224 Number& SensitivityData::derivative(unsigned int qoi_index,
00225                                     unsigned int parameter_index)
00226 {
00227   libmesh_assert_less (qoi_index, _grad_data.size());
00228   libmesh_assert_less (parameter_index, _grad_data[qoi_index].size());
00229 
00230   return _grad_data[qoi_index][parameter_index];
00231 }
00232 
00233 
00234 
00235 inline
00236 const Number& SensitivityData::second_derivative(unsigned int qoi_index,
00237                                                  unsigned int parameter_index1,
00238                                                  unsigned int parameter_index2) const
00239 {
00240   libmesh_assert_less (qoi_index, _hess_data.size());
00241   libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
00242   libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
00243 
00244   return _hess_data[qoi_index][parameter_index1][parameter_index2];
00245 }
00246 
00247 
00248 
00249 inline
00250 Number& SensitivityData::second_derivative(unsigned int qoi_index,
00251                                            unsigned int parameter_index1,
00252                                            unsigned int parameter_index2)
00253 {
00254   libmesh_assert_less (qoi_index, _hess_data.size());
00255   libmesh_assert_less (parameter_index1, _hess_data[qoi_index].size());
00256   libmesh_assert_less (parameter_index2, _hess_data[qoi_index][parameter_index1].size());
00257 
00258   return _hess_data[qoi_index][parameter_index1][parameter_index2];
00259 }
00260 
00261 } // namespace libMesh
00262 
00263 #endif // LIBMESH_SENSITIVITY_DATA_H