libMesh
dense_subvector.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_DENSE_SUBVECTOR_H
21 #define LIBMESH_DENSE_SUBVECTOR_H
22 
23 // Local Includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/dense_vector.h"
26 #include "libmesh/int_range.h"
27 
28 // C++ includes
29 
30 namespace libMesh
31 {
32 
42 template<typename T>
44 {
45 public:
52  DenseSubVector(DenseVector<T> & new_parent,
53  const unsigned int ioff=0,
54  const unsigned int n=0);
55 
60  DenseSubVector (DenseSubVector &&) = default;
61  DenseSubVector (const DenseSubVector &) = default;
62  DenseSubVector & operator= (const DenseSubVector &) = default;
64  virtual ~DenseSubVector() = default;
65 
70 
71  virtual void zero() override final;
72 
77  const T & operator() (const unsigned int i) const;
78 
82  T & operator() (const unsigned int i);
83 
84  virtual T el(const unsigned int i) const override final
85  { return (*this)(i); }
86 
87  virtual T & el(const unsigned int i) override final
88  { return (*this)(i); }
89 
90  virtual unsigned int size() const override final
91  { return _n; }
92 
93  virtual bool empty() const override final
94  { return (_n == 0); }
95 
99  unsigned int i_off() const { return _i_off; }
100 
104  void reposition(const unsigned int ioff,
105  const unsigned int n);
106 
111  Real min () const;
112 
117  Real max () const;
118 
123  Real l1_norm () const;
124 
129  Real l2_norm () const;
130 
135  Real linfty_norm () const;
136 
137 private:
138 
139 
144 
148  unsigned int _n;
149 
153  unsigned int _i_off;
154 };
155 
156 
157 
158 // ------------------------------------------------------------
159 // Dense Vector member functions
160 template<typename T>
161 inline
163  const unsigned int ioff,
164  const unsigned int n) :
165  _parent_vector(new_parent)
166 {
167  reposition (ioff, n);
168 }
169 
170 
171 
172 template<typename T>
173 inline
174 void DenseSubVector<T>::reposition(const unsigned int ioff,
175  const unsigned int n)
176 {
177  _i_off = ioff;
178  _n = n;
179 
180  // Make sure we still fit in the parent vector.
181  libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size());
182 }
183 
184 
185 
186 template<typename T>
187 inline
189 {
190  for (auto i : index_range(*this))
191  _parent_vector (i + this->i_off()) = 0.;
192 }
193 
194 
195 
196 template<typename T>
197 inline
198 const T & DenseSubVector<T>::operator () (const unsigned int i) const
199 {
200  libmesh_assert_less (i, this->size());
201  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
202 
203  return _parent_vector (i + this->i_off());
204 }
205 
206 
207 template<typename T>
208 inline
209 T & DenseSubVector<T>::operator () (const unsigned int i)
210 {
211  libmesh_assert_less (i, this->size());
212  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
213 
214  return _parent_vector (i + this->i_off());
215 }
216 
217 template<typename T>
218 inline
220 {
221  libmesh_assert (this->size());
222  Real my_min = libmesh_real(_parent_vector (this->i_off()));
223 
224  for (auto i : make_range(1u, this->size()))
225  {
226  Real current = libmesh_real(_parent_vector (i + this->i_off()));
227  my_min = (my_min < current? my_min : current);
228  }
229  return my_min;
230 }
231 
232 
233 
234 template<typename T>
235 inline
237 {
238  libmesh_assert (this->size());
239  Real my_max = libmesh_real(_parent_vector (this->i_off()));
240 
241  for (auto i : make_range(1u, this->size()))
242  {
243  Real current = libmesh_real(_parent_vector (i + this->i_off()));
244  my_max = (my_max > current? my_max : current);
245  }
246  return my_max;
247 }
248 
249 
250 
251 template<typename T>
252 inline
254 {
255  Real my_norm = 0.;
256  for (auto i : index_range(*this))
257  {
258  my_norm += std::abs(_parent_vector (i + this->i_off()));
259  }
260  return my_norm;
261 }
262 
263 
264 
265 template<typename T>
266 inline
268 {
269  Real my_norm = 0.;
270  for (auto i : index_range(*this))
271  {
272  my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off()));
273  }
274  return sqrt(my_norm);
275 }
276 
277 
278 
279 template<typename T>
280 inline
282 {
283  if (!this->size())
284  return 0.;
285  Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off()));
286 
287  for (auto i : make_range(1u, this->size()))
288  {
289  Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off()));
290  my_norm = (my_norm > current? my_norm : current);
291  }
292  return sqrt(my_norm);
293 }
294 
295 } // namespace libMesh
296 
297 
298 #endif // LIBMESH_DENSE_SUBVECTOR_H
T libmesh_real(T a)
DenseSubVector(DenseVector< T > &new_parent, const unsigned int ioff=0, const unsigned int n=0)
Constructor.
unsigned int i_off() const
virtual unsigned int size() const override final
The libMesh namespace provides an interface to certain functionality in the library.
DenseVector< T > & parent()
virtual bool empty() const override final
unsigned int _n
The length of this subvector.
Defines a dense subvector for use in finite element computations.
void reposition(const unsigned int ioff, const unsigned int n)
Changes the location of the subvector in the parent vector.
virtual T el(const unsigned int i) const override final
const T & operator()(const unsigned int i) const
libmesh_assert(ctx)
virtual void zero() override final
Set every element in the vector to 0.
virtual ~DenseSubVector()=default
DenseVector< T > & _parent_vector
The parent vector that contains this subvector.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
auto norm_sq(const T &a) -> decltype(std::norm(a))
Definition: tensor_tools.h:104
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition: int_range.h:140
Defines an abstract dense vector base class for use in Finite Element-type computations.
Definition: dof_map.h:73
Defines a dense vector for use in Finite Element-type computations.
Definition: dof_map.h:74
virtual T & el(const unsigned int i) override final
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:117
unsigned int _i_off
The offset into the parent vector.
DenseSubVector & operator=(const DenseSubVector &)=default