libMesh
dense_subvector.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_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 
27 // C++ includes
28 
29 namespace libMesh
30 {
31 
41 template<typename T>
43 {
44 public:
51  DenseSubVector(DenseVector<T> & new_parent,
52  const unsigned int ioff=0,
53  const unsigned int n=0);
54 
58  virtual ~DenseSubVector() {}
59 
64 
65  virtual void zero() libmesh_override;
66 
71  const T & operator() (const unsigned int i) const;
72 
76  T & operator() (const unsigned int i);
77 
78  virtual T el(const unsigned int i) const libmesh_override
79  { return (*this)(i); }
80 
81  virtual T & el(const unsigned int i) libmesh_override
82  { return (*this)(i); }
83 
84  virtual unsigned int size() const libmesh_override
85  { return _n; }
86 
87  virtual bool empty() const libmesh_override
88  { return (_n == 0); }
89 
93  unsigned int i_off() const { return _i_off; }
94 
98  void reposition(const unsigned int ioff,
99  const unsigned int n);
100 
105  Real min () const;
106 
111  Real max () const;
112 
117  Real l1_norm () const;
118 
123  Real l2_norm () const;
124 
129  Real linfty_norm () const;
130 
131 private:
132 
133 
138 
142  unsigned int _n;
143 
147  unsigned int _i_off;
148 };
149 
150 
151 
152 // ------------------------------------------------------------
153 // Dense Vector member functions
154 template<typename T>
155 inline
157  const unsigned int ioff,
158  const unsigned int n) :
159  _parent_vector(new_parent)
160 {
161  reposition (ioff, n);
162 }
163 
164 
165 
166 template<typename T>
167 inline
168 void DenseSubVector<T>::reposition(const unsigned int ioff,
169  const unsigned int n)
170 {
171  _i_off = ioff;
172  _n = n;
173 
174  // Make sure we still fit in the parent vector.
175  libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size());
176 }
177 
178 
179 
180 template<typename T>
181 inline
183 {
184  for (unsigned int i=0; i<this->size(); i++)
185  _parent_vector (i + this->i_off()) = 0.;
186 }
187 
188 
189 
190 template<typename T>
191 inline
192 const T & DenseSubVector<T>::operator () (const unsigned int i) const
193 {
194  libmesh_assert_less (i, this->size());
195  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
196 
197  return _parent_vector (i + this->i_off());
198 }
199 
200 
201 template<typename T>
202 inline
203 T & DenseSubVector<T>::operator () (const unsigned int i)
204 {
205  libmesh_assert_less (i, this->size());
206  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
207 
208  return _parent_vector (i + this->i_off());
209 }
210 
211 template<typename T>
212 inline
214 {
215  libmesh_assert (this->size());
216  Real my_min = libmesh_real(_parent_vector (this->i_off()));
217 
218  for (unsigned int i=1; i!=this->size(); i++)
219  {
220  Real current = libmesh_real(_parent_vector (i + this->i_off()));
221  my_min = (my_min < current? my_min : current);
222  }
223  return my_min;
224 }
225 
226 
227 
228 template<typename T>
229 inline
231 {
232  libmesh_assert (this->size());
233  Real my_max = libmesh_real(_parent_vector (this->i_off()));
234 
235  for (unsigned int i=1; i!=this->size(); i++)
236  {
237  Real current = libmesh_real(_parent_vector (i + this->i_off()));
238  my_max = (my_max > current? my_max : current);
239  }
240  return my_max;
241 }
242 
243 
244 
245 template<typename T>
246 inline
248 {
249  Real my_norm = 0.;
250  for (unsigned int i=0; i!=this->size(); i++)
251  {
252  my_norm += std::abs(_parent_vector (i + this->i_off()));
253  }
254  return my_norm;
255 }
256 
257 
258 
259 template<typename T>
260 inline
262 {
263  Real my_norm = 0.;
264  for (unsigned int i=0; i!=this->size(); i++)
265  {
266  my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off()));
267  }
268  return sqrt(my_norm);
269 }
270 
271 
272 
273 template<typename T>
274 inline
276 {
277  if (!this->size())
278  return 0.;
279  Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off()));
280 
281  for (unsigned int i=1; i!=this->size(); i++)
282  {
283  Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off()));
284  my_norm = (my_norm > current? my_norm : current);
285  }
286  return sqrt(my_norm);
287 }
288 
289 } // namespace libMesh
290 
291 
292 #endif // LIBMESH_DENSE_SUBVECTOR_H
T libmesh_real(T a)
double abs(double a)
DenseSubVector(DenseVector< T > &new_parent, const unsigned int ioff=0, const unsigned int n=0)
Constructor.
const T & operator()(const unsigned int i) const
The libMesh namespace provides an interface to certain functionality in the library.
DenseVector< T > & parent()
libmesh_assert(j)
unsigned int _n
The length of this subvector.
Defines a dense subvector for use in finite element computations.
virtual ~DenseSubVector()
Destructor.
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) libmesh_override
unsigned int i_off() const
virtual void zero() libmesh_override
Set every element in the vector to 0.
virtual bool empty() const libmesh_override
DenseVector< T > & _parent_vector
The parent vector that contains this subvector.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual T el(const unsigned int i) const libmesh_override
Defines an abstract dense vector base class for use in Finite Element-type computations.
Definition: dof_map.h:62
Defines a dense vector for use in Finite Element-type computations.
virtual unsigned int size() const libmesh_override
unsigned int _i_off
The offset into the parent vector.