libMesh
qoi_set.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_QOI_SET_H
21 #define LIBMESH_QOI_SET_H
22 
23 
24 // Local Includes
25 #include "libmesh/libmesh_common.h"
26 
27 // C++ Includes
28 #include <vector>
29 
30 namespace libMesh
31 {
32 
33 // Forward Declarations
34 class System;
35 
45 class QoISet
46 {
47 public:
48  class iterator
49  {
50  public:
51  iterator(unsigned int i, const std::vector<bool> & v) : _i(i), _vecbool(v)
52  {
53  while (_i < _vecbool.size() && !_vecbool[_i])
54  _i++;
55  }
56 
57  unsigned int operator*() const { return _i; }
58 
60  {
61  do {
62  _i++;
63  } while (_i < _vecbool.size() && !_vecbool[_i]);
64  return *this;
65  }
66 
68  iterator it = *this;
69  ++(*this);
70  return it;
71  }
72 
73  bool operator==(const iterator & other) const {
74  libmesh_assert_equal_to (&_vecbool, &other._vecbool);
75  return _i == other._i;
76  }
77 
78  bool operator!=(const iterator & other) const {
79  libmesh_assert_equal_to (&_vecbool, &other._vecbool);
80  return _i != other._i;
81  }
82 
83  private:
84 
85  unsigned int _i;
86 
87  const std::vector<bool> & _vecbool;
88  };
89 
97  QoISet() : _indices(), _weights() {}
98 
103  explicit
104  QoISet(const System & sys);
105 
110  explicit
111  QoISet(const std::vector<bool> & indices) :
112  _indices(indices), _weights() {}
113 
118  explicit
119  QoISet(const std::vector<unsigned int> & indices);
120 
124  void clear() { _indices.clear(); _weights.clear(); }
125 
130  unsigned int size(const System & sys) const;
131 
135  void add_indices(const std::vector<unsigned int> & indices);
136 
140  void add_index(unsigned int);
141 
145  void remove_indices(const std::vector<unsigned int> & indices);
146 
150  void remove_index(unsigned int);
151 
155  void set_weight(unsigned int, Real);
156 
160  Real weight(unsigned int) const;
161 
165  bool has_index(unsigned int) const;
166 
170  iterator begin() const { return iterator(0, _indices); }
171 
172 private:
176  std::vector<bool> _indices;
177 
181  std::vector<Real> _weights;
182 };
183 
184 
185 
186 // ------------------------------------------------------------
187 // QoISet inline methods
188 
189 
190 
191 inline
192 QoISet::QoISet(const std::vector<unsigned int> & indices) :
193  _indices(), _weights()
194 {
195  this->add_indices(indices);
196 }
197 
198 
199 
200 inline
201 void QoISet::add_index(unsigned int i)
202 {
203  if (i >= _indices.size())
204  _indices.resize(i+1, true);
205  _indices[i] = true;
206 }
207 
208 
209 
210 inline
211 void QoISet::remove_index(unsigned int i)
212 {
213  if (i >= _indices.size())
214  _indices.resize(i+1, true);
215  _indices[i] = false;
216 }
217 
218 
219 
220 inline
221 bool QoISet::has_index(unsigned int i) const
222 {
223  return (_indices.size() <= i || _indices[i]);
224 }
225 
226 
227 
228 inline
229 void QoISet::set_weight(unsigned int i, Real w)
230 {
231  if (_weights.size() <= i)
232  _weights.resize(i+1, 1.0);
233 
234  _weights[i] = w;
235 }
236 
237 
238 
239 inline
240 Real QoISet::weight(unsigned int i) const
241 {
242  if (_weights.size() <= i)
243  return 1.0;
244  return _weights[i];
245 }
246 
247 } // namespace libMesh
248 
249 #endif // LIBMESH_QOI_SET_H
bool operator!=(const iterator &other) const
Definition: qoi_set.h:78
unsigned int operator*() const
Definition: qoi_set.h:57
const std::vector< bool > & _vecbool
Definition: qoi_set.h:87
void set_weight(unsigned int, Real)
Set the weight for this index.
Definition: qoi_set.h:229
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
ImplicitSystem & sys
iterator(unsigned int i, const std::vector< bool > &v)
Definition: qoi_set.h:51
unsigned int size(const System &sys) const
Definition: qoi_set.C:35
The libMesh namespace provides an interface to certain functionality in the library.
std::vector< bool > _indices
Interpret _indices.empty() to mean "calculate all indices".
Definition: qoi_set.h:176
iterator & operator++()
Definition: qoi_set.h:59
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
Real weight(unsigned int) const
Get the weight for this index (default 1.0)
Definition: qoi_set.h:240
QoISet()
Empty constructor: "calculate all QoIs in the System".
Definition: qoi_set.h:97
void remove_index(unsigned int)
Remove this index from the set to be calculated.
Definition: qoi_set.h:211
std::vector< Real > _weights
Interpret _weights.size() <= i to mean "weight i = 1.0".
Definition: qoi_set.h:181
QoISet(const std::vector< bool > &indices)
Constructor-from-vector-of-bool: "calculate the QoIs for which \p indices[q] is true".
Definition: qoi_set.h:111
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void add_index(unsigned int)
Add this index to the set to be calculated.
Definition: qoi_set.h:201
void add_indices(const std::vector< unsigned int > &indices)
Add this indices to the set to be calculated.
Definition: qoi_set.C:46
iterator operator++(int)
Definition: qoi_set.h:67
iterator begin() const
Return an iterator pointing to the first index in the set.
Definition: qoi_set.h:170
void remove_indices(const std::vector< unsigned int > &indices)
Remove these indices from the set to be calculated.
Definition: qoi_set.C:63
bool operator==(const iterator &other) const
Definition: qoi_set.h:73
bool has_index(unsigned int) const
Return whether or not this index is in the set to be calculated.
Definition: qoi_set.h:221
void clear()
Resets to "calculate all QoIs, give every QoI weight 1.0".
Definition: qoi_set.h:124