libMesh
laspack_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 // C++ includes
21 #include <algorithm> // for std::min
22 #include <limits>
23 
24 // Local Includes
25 #include "libmesh/dense_subvector.h"
26 #include "libmesh/dense_vector.h"
27 #include "libmesh/laspack_vector.h"
28 #include "libmesh/laspack_matrix.h"
29 #include "libmesh/int_range.h"
30 
31 #ifdef LIBMESH_HAVE_LASPACK
32 
33 namespace libMesh
34 {
35 
36 template <typename T>
38 {
39  libmesh_assert (this->closed());
40 
41  T _sum = 0;
42 
43  const numeric_index_type n = this->size();
44 
45  for (numeric_index_type i=0; i!=n; ++i)
46  _sum += (*this)(i);
47 
48  return _sum;
49 }
50 
51 
52 
53 template <typename T>
55 {
56  libmesh_assert (this->closed());
57 
58  return static_cast<Real>(l1Norm_V(const_cast<QVector*>(&_vec)));
59 }
60 
61 
62 
63 template <typename T>
65 {
66  libmesh_assert (this->closed());
67 
68  return static_cast<Real>(l2Norm_V(const_cast<QVector*>(&_vec)));
69 }
70 
71 
72 
73 template <typename T>
75 {
76  libmesh_assert (this->closed());
77 
78  return static_cast<Real>(MaxNorm_V(const_cast<QVector*>(&_vec)));
79 }
80 
81 
82 
83 template <typename T>
85 {
86  libmesh_assert (this->closed());
87 
88  this->add(1., v);
89 
90  return *this;
91 }
92 
93 
94 
95 
96 template <typename T>
98 {
99  libmesh_assert (this->closed());
100 
101  this->add(-1., v);
102 
103  return *this;
104 }
105 
106 
107 
108 template <typename T>
110 {
111  libmesh_assert_equal_to(size(), v.size());
112 
113  const numeric_index_type n = this->size();
114 
115  for (numeric_index_type i=0; i<n; i++)
116  this->set(i, (*this)(i) * v(i));
117 
118  return *this;
119 }
120 
121 
122 
123 template <typename T>
125 {
126  libmesh_assert_equal_to(size(), v.size());
127 
128  const numeric_index_type n = this->size();
129 
130  for (numeric_index_type i=0; i<n; i++)
131  this->set(i, (*this)(i) / v(i));
132 
133  return *this;
134 }
135 
136 
137 
138 template <typename T>
140 {
141  const numeric_index_type n = this->size();
142 
143  for (numeric_index_type i=0; i<n; i++)
144  {
145  T v = (*this)(i);
146 
147  // Don't divide by zero!
148  libmesh_assert_not_equal_to (v, T(0));
149 
150  this->set(i, 1. / v);
151  }
152 }
153 
154 
155 
156 template <typename T>
158 {
159  const numeric_index_type n = this->size();
160 
161  for (numeric_index_type i=0; i<n; i++)
162  {
163  T v = (*this)(i);
164 
165  this->set(i, libmesh_conj(v) );
166  }
167 }
168 
169 
170 template <typename T>
171 void LaspackVector<T>::add (const T v)
172 {
173  const numeric_index_type n = this->size();
174 
175  for (numeric_index_type i=0; i<n; i++)
176  this->add (i, v);
177 
178 #ifndef NDEBUG
179  this->_is_closed = false;
180 #endif
181 }
182 
183 
184 
185 
186 template <typename T>
188 {
189  this->add (1., v);
190 }
191 
192 
193 
194 template <typename T>
195 void LaspackVector<T>::add (const T a, const NumericVector<T> & v_in)
196 {
197  // Make sure the vector passed in is really a LaspackVector
198  const LaspackVector * v = cast_ptr<const LaspackVector *>(&v_in);
199 
200 #ifndef NDEBUG
201  const bool was_closed = this->_is_closed;
202 #endif
203 
204  libmesh_assert(v);
205  libmesh_assert_equal_to (this->size(), v->size());
206 
207  for (auto i : IntRange<numeric_index_type>(0, v->size()))
208  this->add (i, a*(*v)(i));
209 
210 #ifndef NDEBUG
211  this->_is_closed = was_closed;
212 #endif
213 }
214 
215 
216 
217 template <typename T>
219  const SparseMatrix<T> & mat_in)
220 {
221  // Make sure the data passed in are really in Laspack types
222  const LaspackVector<T> * vec = cast_ptr<const LaspackVector<T> *>(&vec_in);
223  const LaspackMatrix<T> * mat = cast_ptr<const LaspackMatrix<T> *>(&mat_in);
224 
225  libmesh_assert(vec);
226  libmesh_assert(mat);
227 
228  // += mat*vec
229  AddAsgn_VV (&_vec, Mul_QV(const_cast<QMatrix*>(&mat->_QMat),
230  const_cast<QVector*>(&vec->_vec)));
231 }
232 
233 
234 template <typename T>
236  const SparseMatrix<T> &)
237 {
238  libmesh_not_implemented();
239 }
240 
241 
242 
243 template <typename T>
244 void LaspackVector<T>::scale (const T factor)
245 {
246  libmesh_assert (this->initialized());
247 
248  Asgn_VV(&_vec, Mul_SV (factor, &_vec));
249 }
250 
251 template <typename T>
253 {
254  libmesh_assert (this->initialized());
255 
256  const numeric_index_type n = this->size();
257 
258  for (numeric_index_type i=0; i!=n; ++i)
259  this->set(i,std::abs((*this)(i)));
260 }
261 
262 template <typename T>
264 {
265  libmesh_assert (this->initialized());
266 
267  // Make sure the NumericVector passed in is really a LaspackVector
268  const LaspackVector<T> * v = cast_ptr<const LaspackVector<T> *>(&v_in);
269  libmesh_assert(v);
270 
271  return Mul_VV (const_cast<QVector*>(&(this->_vec)),
272  const_cast<QVector*>(&(v->_vec)));
273 }
274 
275 
276 
277 template <typename T>
280 {
281  libmesh_assert (this->initialized());
282  libmesh_assert (this->closed());
283 
284  V_SetAllCmp (&_vec, s);
285 
286  return *this;
287 }
288 
289 
290 
291 template <typename T>
294 {
295  // Make sure the NumericVector passed in is really a LaspackVector
296  const LaspackVector<T> * v =
297  cast_ptr<const LaspackVector<T> *>(&v_in);
298 
299  libmesh_assert(v);
300 
301  *this = *v;
302 
303  return *this;
304 }
305 
306 
307 
308 template <typename T>
311 {
312  libmesh_assert (this->initialized());
313  libmesh_assert (v.closed());
314  libmesh_assert_equal_to (this->size(), v.size());
315 
316  if (v.size() != 0)
317  Asgn_VV (const_cast<QVector*>(&_vec),
318  const_cast<QVector*>(&v._vec)
319  );
320 
321 #ifndef NDEBUG
322  this->_is_closed = true;
323 #endif
324 
325  return *this;
326 }
327 
328 
329 
330 template <typename T>
332 LaspackVector<T>::operator = (const std::vector<T> & v)
333 {
338  if (this->size() == v.size())
339  for (auto i : index_range(v))
340  this->set (i, v[i]);
341 
342  else
343  libmesh_error_msg("this->size() = " << this->size() << " must be equal to v.size() = " << v.size());
344 
345  return *this;
346 }
347 
348 
349 template <typename T>
351 {
352  // Make sure the NumericVector passed in is really a LaspackVector
353  LaspackVector<T> * v_local =
354  cast_ptr<LaspackVector<T> *>(&v_local_in);
355 
356  libmesh_assert(v_local);
357 
358  *v_local = *this;
359 }
360 
361 
362 
363 template <typename T>
365  const std::vector<numeric_index_type> & libmesh_dbg_var(send_list)) const
366 {
367  // Make sure the NumericVector passed in is really a LaspackVector
368  LaspackVector<T> * v_local =
369  cast_ptr<LaspackVector<T> *>(&v_local_in);
370 
371  libmesh_assert(v_local);
372  libmesh_assert_less_equal (send_list.size(), v_local->size());
373 
374  *v_local = *this;
375 }
376 
377 
378 
379 template <typename T>
380 void LaspackVector<T>::localize (std::vector<T> & v_local,
381  const std::vector<numeric_index_type> & indices) const
382 {
383  // LaspackVectors are serial, so we can just copy values
384  v_local.resize(indices.size());
385 
386  for (auto i : index_range(v_local))
387  v_local[i] = (*this)(indices[i]);
388 }
389 
390 
391 
392 template <typename T>
393 void LaspackVector<T>::localize (const numeric_index_type libmesh_dbg_var(first_local_idx),
394  const numeric_index_type libmesh_dbg_var(last_local_idx),
395  const std::vector<numeric_index_type> & libmesh_dbg_var(send_list))
396 {
397  libmesh_assert_equal_to (first_local_idx, 0);
398  libmesh_assert_equal_to (last_local_idx+1, this->size());
399 
400  libmesh_assert_less_equal (send_list.size(), this->size());
401 
402 #ifndef NDEBUG
403  this->_is_closed = true;
404 #endif
405 }
406 
407 
408 
409 template <typename T>
410 void LaspackVector<T>::localize (std::vector<T> & v_local) const
411 
412 {
413  v_local.resize(this->size());
414 
415  for (auto i : index_range(v_local))
416  v_local[i] = (*this)(i);
417 }
418 
419 
420 
421 template <typename T>
422 void LaspackVector<T>::localize_to_one (std::vector<T> & v_local,
423  const processor_id_type libmesh_dbg_var(pid)) const
424 {
425  libmesh_assert_equal_to (pid, 0);
426 
427  this->localize (v_local);
428 }
429 
430 
431 
432 template <typename T>
434  const NumericVector<T> & /*vec2*/)
435 {
436  libmesh_not_implemented();
437 }
438 
439 
440 
441 template <typename T>
443 {
444  libmesh_assert (this->initialized());
445  if (!this->size())
446  return -std::numeric_limits<Real>::max();
447 
448  Real the_max = libmesh_real((*this)(0));
449 
450  const numeric_index_type n = this->size();
451 
452  for (numeric_index_type i=1; i<n; i++)
453  the_max = std::max (the_max, libmesh_real((*this)(i)));
454 
455  return the_max;
456 }
457 
458 
459 
460 template <typename T>
462 {
463  libmesh_assert (this->initialized());
464  if (!this->size())
465  return std::numeric_limits<Real>::max();
466 
467  Real the_min = libmesh_real((*this)(0));
468 
469  const numeric_index_type n = this->size();
470 
471  for (numeric_index_type i=1; i<n; i++)
472  the_min = std::min (the_min, libmesh_real((*this)(i)));
473 
474  return the_min;
475 }
476 
477 
478 //------------------------------------------------------------------
479 // Explicit instantiations
480 template class LaspackVector<Number>;
481 
482 } // namespace libMesh
483 
484 
485 #endif // #ifdef LIBMESH_HAVE_LASPACK
libMesh::LaspackVector::localize
virtual void localize(std::vector< T > &v_local) const override
Creates a copy of the global vector in the local vector v_local.
Definition: laspack_vector.C:410
libMesh::LaspackVector::l2_norm
virtual Real l2_norm() const override
Definition: laspack_vector.C:64
libMesh::LaspackVector::l1_norm
virtual Real l1_norm() const override
Definition: laspack_vector.C:54
libMesh::LaspackVector::dot
virtual T dot(const NumericVector< T > &v) const override
Definition: laspack_vector.C:263
libMesh::libmesh_real
T libmesh_real(T a)
Definition: libmesh_common.h:166
libMesh::index_range
IntRange< std::size_t > index_range(const std::vector< T > &vec)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:106
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::libmesh_conj
T libmesh_conj(T a)
Definition: libmesh_common.h:167
libMesh::LaspackVector::max
virtual Real max() const override
Definition: laspack_vector.C:442
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::NumericVector::closed
virtual bool closed() const
Definition: numeric_vector.h:171
libMesh::NumericVector::size
virtual numeric_index_type size() const =0
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::IntRange
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition: int_range.h:53
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::LaspackVector::add
virtual void add(const numeric_index_type i, const T value) override
Adds value to each entry of the vector.
Definition: laspack_vector.h:512
libMesh::LaspackVector::operator/=
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
Computes the component-wise division of this vector's entries by another's, .
Definition: laspack_vector.C:124
libMesh::LaspackVector::localize_to_one
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
Creates a local copy of the global vector in v_local only on processor proc_id.
Definition: laspack_vector.C:422
libMesh::LaspackVector
This class provides a nice interface to the Laspack C-based data structures for serial vectors.
Definition: laspack_matrix.h:42
libMesh::LaspackVector::abs
virtual void abs() override
Sets for each entry in the vector.
Definition: laspack_vector.C:252
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::LaspackVector::reciprocal
virtual void reciprocal() override
Computes the component-wise reciprocal, .
Definition: laspack_vector.C:139
libMesh::LaspackVector::scale
virtual void scale(const T factor) override
Scale each element of the vector by the given factor.
Definition: laspack_vector.C:244
libMesh::LaspackMatrix
The LaspackMatrix class wraps a QMatrix object from the Laspack library.
Definition: laspack_matrix.h:56
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::LaspackVector::add_vector_transpose
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
Definition: laspack_vector.C:235
libMesh::LaspackVector::operator*=
virtual NumericVector< T > & operator*=(const NumericVector< T > &v) override
Computes the component-wise multiplication of this vector's entries by another's, .
Definition: laspack_vector.C:109
libMesh::initialized
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:265
libMesh::LaspackVector::_vec
QVector _vec
Actual Laspack vector datatype to hold vector entries.
Definition: laspack_vector.h:236
libMesh::LaspackVector::size
virtual numeric_index_type size() const override
Definition: laspack_vector.h:452
libMesh::LaspackVector::min
virtual Real min() const override
Definition: laspack_vector.C:461
libMesh::LaspackVector::linfty_norm
virtual Real linfty_norm() const override
Definition: laspack_vector.C:74
libMesh::LaspackVector::operator-=
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
Subtracts v from *this, .
Definition: laspack_vector.C:97
libMesh::LaspackVector::operator=
LaspackVector< T > & operator=(const LaspackVector< T > &v)
Copy assignment operator.
Definition: laspack_vector.C:310
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::LaspackVector::pointwise_mult
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
Definition: laspack_vector.C:433
libMesh::LaspackVector::conjugate
virtual void conjugate() override
Negates the imaginary component of each entry in the vector.
Definition: laspack_vector.C:157
libMesh::LaspackVector::operator+=
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
Adds v to *this, .
Definition: laspack_vector.C:84
libMesh::closed
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:272
libMesh::LaspackVector::sum
virtual T sum() const override
Definition: laspack_vector.C:37
libMesh::LaspackVector::add_vector
virtual void add_vector(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
Definition: laspack_vector.C:218