libMesh
Loading...
Searching...
No Matches
dense_subvector.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_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
30namespace libMesh
31{
32
42template<typename T>
44{
45public:
52 DenseSubVector(DenseVector<T> & new_parent,
53 const unsigned int ioff=0,
54 const unsigned int n=0);
55
61 DenseSubVector (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
137private:
138
139
144
148 unsigned int _n;
149
153 unsigned int _i_off;
154};
155
156
157
158// ------------------------------------------------------------
159// Dense Vector member functions
160template<typename T>
161inline
163 const unsigned int ioff,
164 const unsigned int n) :
165 _parent_vector(new_parent)
166{
167 reposition (ioff, n);
168}
169
170
171
172template<typename T>
173inline
174void 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
186template<typename T>
187inline
189{
190 for (auto i : index_range(*this))
191 _parent_vector (i + this->i_off()) = 0.;
192}
193
194
195
196template<typename T>
197inline
198const 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
207template<typename T>
208inline
209T & 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
217template<typename T>
218inline
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
234template<typename T>
235inline
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
251template<typename T>
252inline
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
265template<typename T>
266inline
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
279template<typename T>
280inline
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
Defines a dense subvector for use in finite element computations.
const T & operator()(const unsigned int i) const
virtual unsigned int size() const override final
DenseSubVector & operator=(const DenseSubVector &)=default
DenseSubVector(DenseVector< T > &new_parent, const unsigned int ioff=0, const unsigned int n=0)
Constructor.
unsigned int _i_off
The offset into the parent vector.
DenseVector< T > & parent()
virtual ~DenseSubVector()=default
virtual T el(const unsigned int i) const override final
virtual bool empty() const override final
unsigned int _n
The length of this subvector.
DenseSubVector(DenseSubVector &&)=default
The 5 special functions can be defaulted for this class, as it does not manage any memory itself.
unsigned int i_off() const
virtual void zero() override final
Set every element in the vector to 0.
virtual T & el(const unsigned int i) override final
DenseSubVector(const DenseSubVector &)=default
void reposition(const unsigned int ioff, const unsigned int n)
Changes the location of the subvector in the parent vector.
DenseVector< T > & _parent_vector
The parent vector that contains this subvector.
Defines an abstract dense vector base class for use in Finite Element-type computations.
Defines a dense vector for use in Finite Element-type computations.
auto norm_sq(const T &a)
The libMesh namespace provides an interface to certain functionality in the library.
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:153
T libmesh_real(T a)
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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:176