libMesh
Loading...
Searching...
No Matches
sensitivity_data.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
32namespace libMesh
33{
34
35// Forward declarations
36class QoISet;
37
47{
48public:
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
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
144private:
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
159inline
161 const System & sys,
162 const ParameterVector & parameter_vector)
163{
164 this->allocate_data(qoi_indices, sys, parameter_vector);
165}
166
167
168
169inline
170void 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
190inline
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
213inline
214const 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
225inline
226Number & 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
237inline
238const 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
251inline
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
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
std::size_t size() const
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition qoi_set.h:46
bool has_index(std::size_t) const
Return whether or not this index is in the set to be calculated.
Definition qoi_set.h:224
const Number & operator[](unsigned int parameter)
ConstRow(const SensitivityData &sd, unsigned int qoi)
Number & operator[](unsigned int parameter)
Row(SensitivityData &sd, unsigned int qoi)
Data structure for holding completed parameter sensitivity calculations.
void clear()
Clears and deallocates all 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.
std::vector< std::vector< Number > > _grad_data
Data storage; currently pretty trivial.
const Number & second_derivative(unsigned int qoi_index, unsigned int parameter_index1, unsigned int parameter_index2) const
const Number & derivative(unsigned int qoi_index, unsigned int parameter_index) const
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.
ConstRow operator[](unsigned int qoi) const
Vector address type operator: sd[q][p] is an alias for sd.derivative(q,p)
std::vector< std::vector< std::vector< Number > > > _hess_data
SensitivityData()
Default constructor: empty data set.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition system.h:2562
The libMesh namespace provides an interface to certain functionality in the library.