libMesh
qoi_set.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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(std::size_t i, const std::vector<bool> & v) : _i(i), _vecbool(v)
52  {
53  while (_i < _vecbool.size() && !_vecbool[_i])
54  _i++;
55  }
56 
57  std::size_t operator*() const { return _i; }
58 
60  {
61  do {
62  _i++;
63  } while (_i < _vecbool.size() && !_vecbool[_i]);
64  return *this;
65  }
66 
68  {
69  iterator it = *this;
70  ++(*this);
71  return it;
72  }
73 
74  bool operator==(const iterator & other) const
75  {
76  libmesh_assert_equal_to (&_vecbool, &other._vecbool);
77  return _i == other._i;
78  }
79 
80  bool operator!=(const iterator & other) const
81  {
82  libmesh_assert_equal_to (&_vecbool, &other._vecbool);
83  return _i != other._i;
84  }
85 
86  private:
87 
88  std::size_t _i;
89 
90  const std::vector<bool> & _vecbool;
91  };
92 
101 
106  explicit
107  QoISet(const System & sys);
108 
113  explicit
114  QoISet(std::vector<bool> indices) :
115  _indices(std::move(indices)), _weights() {}
116 
121  explicit
122  QoISet(const std::vector<unsigned int> & indices);
123 
127  void clear() { _indices.clear(); _weights.clear(); }
128 
133  std::size_t size(const System & sys) const;
134 
138  void add_indices(const std::vector<unsigned int> & indices);
139 
143  void add_index(std::size_t);
144 
148  void remove_indices(const std::vector<unsigned int> & indices);
149 
153  void remove_index(std::size_t);
154 
158  void set_weight(std::size_t, Real);
159 
163  Real weight(std::size_t) const;
164 
168  bool has_index(std::size_t) const;
169 
173  iterator begin() const { return iterator(0, _indices); }
174 
175 private:
179  std::vector<bool> _indices;
180 
184  std::vector<Real> _weights;
185 };
186 
187 
188 
189 // ------------------------------------------------------------
190 // QoISet inline methods
191 
192 
193 
194 inline
195 QoISet::QoISet(const std::vector<unsigned int> & indices) :
196  _indices(), _weights()
197 {
198  this->add_indices(indices);
199 }
200 
201 
202 
203 inline
204 void QoISet::add_index(std::size_t i)
205 {
206  if (i >= _indices.size())
207  _indices.resize(i+1, true);
208  _indices[i] = true;
209 }
210 
211 
212 
213 inline
214 void QoISet::remove_index(std::size_t i)
215 {
216  if (i >= _indices.size())
217  _indices.resize(i+1, true);
218  _indices[i] = false;
219 }
220 
221 
222 
223 inline
224 bool QoISet::has_index(std::size_t i) const
225 {
226  return (_indices.size() <= i || _indices[i]);
227 }
228 
229 
230 
231 inline
232 void QoISet::set_weight(std::size_t i, Real w)
233 {
234  if (_weights.size() <= i)
235  _weights.resize(i+1, 1.0);
236 
237  _weights[i] = w;
238 }
239 
240 
241 
242 inline
243 Real QoISet::weight(std::size_t i) const
244 {
245  if (_weights.size() <= i)
246  return 1.0;
247  return _weights[i];
248 }
249 
250 } // namespace libMesh
251 
252 #endif // LIBMESH_QOI_SET_H
void add_index(std::size_t)
Add this index to the set to be calculated.
Definition: qoi_set.h:204
void set_weight(std::size_t, Real)
Set the weight for this index.
Definition: qoi_set.h:232
const std::vector< bool > & _vecbool
Definition: qoi_set.h:90
iterator begin() const
Return an iterator pointing to the first index in the set.
Definition: qoi_set.h:173
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
The libMesh namespace provides an interface to certain functionality in the library.
bool operator==(const iterator &other) const
Definition: qoi_set.h:74
std::vector< bool > _indices
Interpret _indices.empty() to mean "calculate all indices".
Definition: qoi_set.h:179
iterator & operator++()
Definition: qoi_set.h:59
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
std::size_t operator*() const
Definition: qoi_set.h:57
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
QoISet()
Empty constructor: "calculate all QoIs in the System".
Definition: qoi_set.h:100
std::size_t size(const System &sys) const
Definition: qoi_set.C:35
Real weight(std::size_t) const
Get the weight for this index (default 1.0)
Definition: qoi_set.h:243
std::vector< Real > _weights
Interpret _weights.size() <= i to mean "weight i = 1.0".
Definition: qoi_set.h:184
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void remove_index(std::size_t)
Remove this index from the set to be calculated.
Definition: qoi_set.h:214
void add_indices(const std::vector< unsigned int > &indices)
Add this indices to the set to be calculated.
Definition: qoi_set.C:46
QoISet(std::vector< bool > indices)
Constructor-from-vector-of-bool: "calculate the QoIs for which \p indices[q] is true".
Definition: qoi_set.h:114
iterator operator++(int)
Definition: qoi_set.h:67
iterator(std::size_t i, const std::vector< bool > &v)
Definition: qoi_set.h:51
bool operator!=(const iterator &other) const
Definition: qoi_set.h:80
void remove_indices(const std::vector< unsigned int > &indices)
Remove these indices from the set to be calculated.
Definition: qoi_set.C:61
void clear()
Resets to "calculate all QoIs, give every QoI weight 1.0".
Definition: qoi_set.h:127