dense_subvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 
59  DenseSubVector (DenseSubVector &&) = default;
60  DenseSubVector (const DenseSubVector &) = default;
61  DenseSubVector & operator= (const DenseSubVector &) = default;
63  virtual ~DenseSubVector() = default;
64 
69 
70  virtual void zero() override;
71 
76  const T & operator() (const unsigned int i) const;
77 
81  T & operator() (const unsigned int i);
82 
83  virtual T el(const unsigned int i) const override
84  { return (*this)(i); }
85 
86  virtual T & el(const unsigned int i) override
87  { return (*this)(i); }
88 
89  virtual unsigned int size() const override
90  { return _n; }
91 
92  virtual bool empty() const override
93  { return (_n == 0); }
94 
98  unsigned int i_off() const { return _i_off; }
99 
103  void reposition(const unsigned int ioff,
104  const unsigned int n);
105 
110  Real min () const;
111 
116  Real max () const;
117 
122  Real l1_norm () const;
123 
128  Real l2_norm () const;
129 
134  Real linfty_norm () const;
135 
136 private:
137 
138 
143 
147  unsigned int _n;
148 
152  unsigned int _i_off;
153 };
154 
155 
156 
157 // ------------------------------------------------------------
158 // Dense Vector member functions
159 template<typename T>
160 inline
162  const unsigned int ioff,
163  const unsigned int n) :
164  _parent_vector(new_parent)
165 {
166  reposition (ioff, n);
167 }
168 
169 
170 
171 template<typename T>
172 inline
173 void DenseSubVector<T>::reposition(const unsigned int ioff,
174  const unsigned int n)
175 {
176  _i_off = ioff;
177  _n = n;
178 
179  // Make sure we still fit in the parent vector.
180  libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size());
181 }
182 
183 
184 
185 template<typename T>
186 inline
188 {
189  for (unsigned int i=0; i<this->size(); i++)
190  _parent_vector (i + this->i_off()) = 0.;
191 }
192 
193 
194 
195 template<typename T>
196 inline
197 const T & DenseSubVector<T>::operator () (const unsigned int i) const
198 {
199  libmesh_assert_less (i, this->size());
200  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
201 
202  return _parent_vector (i + this->i_off());
203 }
204 
205 
206 template<typename T>
207 inline
208 T & DenseSubVector<T>::operator () (const unsigned int i)
209 {
210  libmesh_assert_less (i, this->size());
211  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
212 
213  return _parent_vector (i + this->i_off());
214 }
215 
216 template<typename T>
217 inline
219 {
220  libmesh_assert (this->size());
221  Real my_min = libmesh_real(_parent_vector (this->i_off()));
222 
223  for (unsigned int i=1; i!=this->size(); i++)
224  {
225  Real current = libmesh_real(_parent_vector (i + this->i_off()));
226  my_min = (my_min < current? my_min : current);
227  }
228  return my_min;
229 }
230 
231 
232 
233 template<typename T>
234 inline
236 {
237  libmesh_assert (this->size());
238  Real my_max = libmesh_real(_parent_vector (this->i_off()));
239 
240  for (unsigned int i=1; i!=this->size(); i++)
241  {
242  Real current = libmesh_real(_parent_vector (i + this->i_off()));
243  my_max = (my_max > current? my_max : current);
244  }
245  return my_max;
246 }
247 
248 
249 
250 template<typename T>
251 inline
253 {
254  Real my_norm = 0.;
255  for (unsigned int i=0; i!=this->size(); i++)
256  {
257  my_norm += std::abs(_parent_vector (i + this->i_off()));
258  }
259  return my_norm;
260 }
261 
262 
263 
264 template<typename T>
265 inline
267 {
268  Real my_norm = 0.;
269  for (unsigned int i=0; i!=this->size(); i++)
270  {
271  my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off()));
272  }
273  return sqrt(my_norm);
274 }
275 
276 
277 
278 template<typename T>
279 inline
281 {
282  if (!this->size())
283  return 0.;
284  Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off()));
285 
286  for (unsigned int i=1; i!=this->size(); i++)
287  {
288  Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off()));
289  my_norm = (my_norm > current? my_norm : current);
290  }
291  return sqrt(my_norm);
292 }
293 
294 } // namespace libMesh
295 
296 
297 #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)
unsigned int i_off() const
virtual void zero() override
virtual T el(const unsigned int i) const override
DenseVector< T > & parent()
void reposition(const unsigned int ioff, const unsigned int n)
const T & operator()(const unsigned int i) const
virtual T & el(const unsigned int i) override
virtual unsigned int size() const override
virtual ~DenseSubVector()=default
DenseVector< T > & _parent_vector
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool empty() const override
DenseSubVector & operator=(const DenseSubVector &)=default