libMesh
laspack_vector.h
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 
21 #ifndef LIBMESH_LASPACK_VECTOR_H
22 #define LIBMESH_LASPACK_VECTOR_H
23 
24 
25 
26 #include "libmesh/libmesh_common.h"
27 
28 #ifdef LIBMESH_HAVE_LASPACK
29 
30 // Local includes
31 #include "libmesh/numeric_vector.h"
32 
33 // C++ includes
34 #include <cstdio> // for std::sprintf
35 
36 // Laspack includes
37 #include <operats.h>
38 #include <qvector.h>
39 
40 namespace libMesh
41 {
42 
43 // Forward declarations
44 template <typename T> class LaspackLinearSolver;
45 template <typename T> class SparseMatrix;
46 
55 template <typename T>
56 class LaspackVector final : public NumericVector<T>
57 {
58 public:
59 
63  explicit
64  LaspackVector (const Parallel::Communicator & comm,
65  const ParallelType = AUTOMATIC);
66 
70  explicit
71  LaspackVector (const Parallel::Communicator & comm,
72  const numeric_index_type n,
73  const ParallelType = AUTOMATIC);
74 
79  LaspackVector (const Parallel::Communicator & comm,
80  const numeric_index_type n,
81  const numeric_index_type n_local,
82  const ParallelType = AUTOMATIC);
83 
89  LaspackVector (const Parallel::Communicator & comm,
90  const numeric_index_type N,
91  const numeric_index_type n_local,
92  const std::vector<numeric_index_type> & ghost,
93  const ParallelType = AUTOMATIC);
94 
100  LaspackVector<T> & operator= (const LaspackVector<T> & v);
101 
107  LaspackVector (LaspackVector &&) = delete;
108  LaspackVector (const LaspackVector &) = delete;
109  LaspackVector & operator= (LaspackVector &&) = delete;
110  virtual ~LaspackVector ();
111 
112  virtual void close () override;
113 
114  virtual void clear () override;
115 
116  virtual void zero () override;
117 
118  virtual std::unique_ptr<NumericVector<T>> zero_clone () const override;
119 
120  virtual std::unique_ptr<NumericVector<T>> clone () const override;
121 
122  virtual void init (const numeric_index_type N,
123  const numeric_index_type n_local,
124  const bool fast=false,
125  const ParallelType ptype=AUTOMATIC) override;
126 
127  virtual void init (const numeric_index_type N,
128  const bool fast=false,
129  const ParallelType ptype=AUTOMATIC) override;
130 
131  virtual void init (const numeric_index_type N,
132  const numeric_index_type n_local,
133  const std::vector<numeric_index_type> & ghost,
134  const bool fast = false,
135  const ParallelType = AUTOMATIC) override;
136 
137  virtual void init (const NumericVector<T> & other,
138  const bool fast = false) override;
139 
140  virtual NumericVector<T> & operator= (const T s) override;
141 
142  virtual NumericVector<T> & operator= (const NumericVector<T> & v) override;
143 
144  virtual NumericVector<T> & operator= (const std::vector<T> & v) override;
145 
146  virtual Real min () const override;
147 
148  virtual Real max () const override;
149 
150  virtual T sum () const override;
151 
152  virtual Real l1_norm () const override;
153 
154  virtual Real l2_norm () const override;
155 
156  virtual Real linfty_norm () const override;
157 
158  virtual numeric_index_type size () const override;
159 
160  virtual numeric_index_type local_size() const override;
161 
162  virtual numeric_index_type first_local_index() const override;
163 
164  virtual numeric_index_type last_local_index() const override;
165 
166  virtual T operator() (const numeric_index_type i) const override;
167 
168  virtual NumericVector<T> & operator += (const NumericVector<T> & v) override;
169 
170  virtual NumericVector<T> & operator -= (const NumericVector<T> & v) override;
171 
172  virtual NumericVector<T> & operator *= (const NumericVector<T> & v) override;
173 
174  virtual NumericVector<T> & operator /= (const NumericVector<T> & v) override;
175 
176  virtual void reciprocal() override;
177 
178  virtual void conjugate() override;
179 
180  virtual void set (const numeric_index_type i, const T value) override;
181 
182  virtual void add (const numeric_index_type i, const T value) override;
183 
184  virtual void add (const T s) override;
185 
186  virtual void add (const NumericVector<T> & v) override;
187 
188  virtual void add (const T a, const NumericVector<T> & v) override;
189 
195 
196  virtual void add_vector (const NumericVector<T> & v,
197  const SparseMatrix<T> & A) override;
198 
199  virtual void add_vector_transpose (const NumericVector<T> & v,
200  const SparseMatrix<T> & A) override;
201 
202  virtual void scale (const T factor) override;
203 
204  virtual void abs() override;
205 
206  virtual T dot(const NumericVector<T> & v) const override;
207 
208  virtual void localize (std::vector<T> & v_local) const override;
209 
210  virtual void localize (NumericVector<T> & v_local) const override;
211 
212  virtual void localize (NumericVector<T> & v_local,
213  const std::vector<numeric_index_type> & send_list) const override;
214 
215  virtual void localize (std::vector<T> & v_local,
216  const std::vector<numeric_index_type> & indices) const override;
217 
218  virtual void localize (const numeric_index_type first_local_idx,
219  const numeric_index_type last_local_idx,
220  const std::vector<numeric_index_type> & send_list) override;
221 
222  virtual void localize_to_one (std::vector<T> & v_local,
223  const processor_id_type proc_id=0) const override;
224 
225  virtual void pointwise_mult (const NumericVector<T> & vec1,
226  const NumericVector<T> & vec2) override;
227 
228  virtual void swap (NumericVector<T> & v) override;
229 
230 private:
231 
236  QVector _vec;
237 
241  friend class LaspackLinearSolver<T>;
242 };
243 
244 
245 
246 //----------------------------------------------------------
247 // LaspackVector inline methods
248 template <typename T>
249 inline
250 LaspackVector<T>::LaspackVector (const Parallel::Communicator & comm,
251  const ParallelType ptype)
252  : NumericVector<T>(comm, ptype)
253 {
254  this->_type = ptype;
255 }
256 
257 
258 
259 template <typename T>
260 inline
261 LaspackVector<T>::LaspackVector (const Parallel::Communicator & comm,
262  const numeric_index_type n,
263  const ParallelType ptype)
264  : NumericVector<T>(comm, ptype)
265 {
266  this->init(n, n, false, ptype);
267 }
268 
269 
270 
271 template <typename T>
272 inline
273 LaspackVector<T>::LaspackVector (const Parallel::Communicator & comm,
274  const numeric_index_type n,
275  const numeric_index_type n_local,
276  const ParallelType ptype)
277  : NumericVector<T>(comm, ptype)
278 {
279  this->init(n, n_local, false, ptype);
280 }
281 
282 
283 
284 template <typename T>
285 inline
286 LaspackVector<T>::LaspackVector (const Parallel::Communicator & comm,
287  const numeric_index_type N,
288  const numeric_index_type n_local,
289  const std::vector<numeric_index_type> & ghost,
290  const ParallelType ptype)
291  : NumericVector<T>(comm, ptype)
292 {
293  this->init(N, n_local, ghost, false, ptype);
294 }
295 
296 
297 
298 template <typename T>
299 inline
301 {
302  this->clear ();
303 }
304 
305 
306 
307 template <typename T>
308 inline
310  const numeric_index_type libmesh_dbg_var(n_local),
311  const bool fast,
312  const ParallelType)
313 {
314  // Laspack vectors only for serial cases,
315  // but can provide a "parallel" vector on one processor.
316  libmesh_assert_equal_to (n, n_local);
317 
318  this->_type = SERIAL;
319 
320  // Clear initialized vectors
321  if (this->initialized())
322  this->clear();
323 
324  // create a sequential vector
325 
326  static int cnt = 0;
327  char foo[80];
328  std::sprintf(foo, "Vec-%d", cnt++);
329 
330  V_Constr(&_vec, const_cast<char *>(foo), n, Normal, _LPTrue);
331 
332  this->_is_initialized = true;
333 #ifndef NDEBUG
334  this->_is_closed = true;
335 #endif
336 
337  // Optionally zero out all components
338  if (fast == false)
339  this->zero ();
340 
341  return;
342 }
343 
344 
345 
346 template <typename T>
347 inline
349  const bool fast,
350  const ParallelType ptype)
351 {
352  this->init(n,n,fast,ptype);
353 }
354 
355 
356 template <typename T>
357 inline
359  const numeric_index_type n_local,
360  const std::vector<numeric_index_type> & libmesh_dbg_var(ghost),
361  const bool fast,
362  const ParallelType ptype)
363 {
364  libmesh_assert(ghost.empty());
365  this->init(n,n_local,fast,ptype);
366 }
367 
368 
369 
370 /* Default implementation for solver packages for which ghosted
371  vectors are not yet implemented. */
372 template <class T>
374  const bool fast)
375 {
376  this->init(other.size(),other.local_size(),fast,other.type());
377 }
378 
379 
380 
381 template <typename T>
382 inline
384 {
385  libmesh_assert (this->initialized());
386 
387 #ifndef NDEBUG
388  this->_is_closed = true;
389 #endif
390 }
391 
392 
393 
394 template <typename T>
395 inline
397 {
398  if (this->initialized())
399  {
400  V_Destr (&_vec);
401  }
402 
403  this->_is_initialized = false;
404 #ifndef NDEBUG
405  this->_is_closed = false;
406 #endif
407 }
408 
409 
410 
411 template <typename T> inline
413 {
414  libmesh_assert (this->initialized());
415  libmesh_assert (this->closed());
416 
417  V_SetAllCmp (&_vec, 0.);
418 }
419 
420 
421 
422 template <typename T>
423 inline
424 std::unique_ptr<NumericVector<T>> LaspackVector<T>::zero_clone () const
425 {
426  NumericVector<T> * cloned_vector = new LaspackVector<T>(this->comm());
427 
428  cloned_vector->init(*this);
429 
430  return std::unique_ptr<NumericVector<T>>(cloned_vector);
431 }
432 
433 
434 
435 template <typename T>
436 inline
437 std::unique_ptr<NumericVector<T>> LaspackVector<T>::clone () const
438 {
439  NumericVector<T> * cloned_vector = new LaspackVector<T>(this->comm());
440 
441  cloned_vector->init(*this, true);
442 
443  *cloned_vector = *this;
444 
445  return std::unique_ptr<NumericVector<T>>(cloned_vector);
446 }
447 
448 
449 
450 template <typename T>
451 inline
453 {
454  libmesh_assert (this->initialized());
455 
456  return static_cast<numeric_index_type>(V_GetDim(const_cast<QVector*>(&_vec)));
457 }
458 
459 
460 
461 template <typename T>
462 inline
464 {
465  libmesh_assert (this->initialized());
466 
467  return this->size();
468 }
469 
470 
471 
472 template <typename T>
473 inline
475 {
476  libmesh_assert (this->initialized());
477 
478  return 0;
479 }
480 
481 
482 
483 template <typename T>
484 inline
486 {
487  libmesh_assert (this->initialized());
488 
489  return this->size();
490 }
491 
492 
493 
494 template <typename T>
495 inline
497 {
498  libmesh_assert (this->initialized());
499  libmesh_assert_less (i, this->size());
500 
501  V_SetCmp (&_vec, i+1, value);
502 
503 #ifndef NDEBUG
504  this->_is_closed = false;
505 #endif
506 }
507 
508 
509 
510 template <typename T>
511 inline
513 {
514  libmesh_assert (this->initialized());
515  libmesh_assert_less (i, this->size());
516 
517  V_AddCmp (&_vec, i+1, value);
518 
519 #ifndef NDEBUG
520  this->_is_closed = false;
521 #endif
522 }
523 
524 
525 
526 template <typename T>
527 inline
529 {
530  libmesh_assert (this->initialized());
531  libmesh_assert ( ((i >= this->first_local_index()) &&
532  (i < this->last_local_index())) );
533 
534 
535  return static_cast<T>(V_GetCmp(const_cast<QVector*>(&_vec), i+1));
536 }
537 
538 
539 
540 template <typename T>
541 inline
543 {
544  LaspackVector<T> & v = cast_ref<LaspackVector<T> &>(other);
545 
546  // This is all grossly dependent on Laspack version...
547 
548  std::swap(_vec.Name, v._vec.Name);
549  std::swap(_vec.Dim, v._vec.Dim);
550  std::swap(_vec.Instance, v._vec.Instance);
551  std::swap(_vec.LockLevel, v._vec.LockLevel);
552  std::swap(_vec.Multipl, v._vec.Multipl);
553  std::swap(_vec.OwnData, v._vec.OwnData);
554 
555  // This should still be O(1), since _vec.Cmp is just a pointer to
556  // data on the heap
557 
558  std::swap(_vec.Cmp, v._vec.Cmp);
559 }
560 
561 
562 } // namespace libMesh
563 
564 
565 #endif // #ifdef LIBMESH_HAVE_LASPACK
566 #endif // LIBMESH_LASPACK_VECTOR_H
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::last_local_index
virtual numeric_index_type last_local_index() const override
Definition: laspack_vector.h:485
libMesh::LaspackVector::dot
virtual T dot(const NumericVector< T > &v) const override
Definition: laspack_vector.C:263
libMesh::SERIAL
Definition: enum_parallel_type.h:35
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::NumericVector::init
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::LaspackVector::max
virtual Real max() const override
Definition: laspack_vector.C:442
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::zero
const Number zero
.
Definition: libmesh.h:243
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::NumericVector::_type
ParallelType _type
Type of vector.
Definition: numeric_vector.h:737
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::NumericVector::add_vector
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i].
Definition: numeric_vector.C:363
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::local_size
virtual numeric_index_type local_size() const override
Definition: laspack_vector.h:463
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::NumericVector::local_size
virtual numeric_index_type local_size() const =0
libMesh::LaspackVector::first_local_index
virtual numeric_index_type first_local_index() const override
Definition: laspack_vector.h:474
libMesh::LaspackVector::LaspackVector
LaspackVector(const Parallel::Communicator &comm, const ParallelType=AUTOMATIC)
Dummy-Constructor.
Definition: laspack_vector.h:250
A
static PetscErrorCode Mat * A
Definition: petscdmlibmeshimpl.C:1026
libMesh::LaspackVector::zero_clone
virtual std::unique_ptr< NumericVector< T > > zero_clone() const override
Definition: laspack_vector.h:424
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::clone
virtual std::unique_ptr< NumericVector< T > > clone() const override
Definition: laspack_vector.h:437
swap
void swap(Iterator &lhs, Iterator &rhs)
swap, used to implement op=
Definition: variant_filter_iterator.h:478
libMesh::LaspackVector::clear
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
Definition: laspack_vector.h:396
libMesh::LaspackVector::init
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC) override
Change the dimension of the vector to n.
libMesh::LaspackVector::close
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
Definition: laspack_vector.h:383
libMesh::libMeshPrivateData::_is_initialized
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:246
libMesh::LaspackVector::_vec
QVector _vec
Actual Laspack vector datatype to hold vector entries.
Definition: laspack_vector.h:236
value
static const bool value
Definition: xdr_io.C:56
libMesh::LaspackLinearSolver
This class provides an interface to Laspack iterative solvers that is compatible with the libMesh Lin...
Definition: laspack_matrix.h:43
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::zero
virtual void zero() override
Set all entries to zero.
Definition: laspack_vector.h:412
libMesh::LaspackVector::operator()
virtual T operator()(const numeric_index_type i) const override
Definition: laspack_vector.h:528
libMesh::AUTOMATIC
Definition: enum_parallel_type.h:34
libMesh::LaspackVector::linfty_norm
virtual Real linfty_norm() const override
Definition: laspack_vector.C:74
libMesh::LaspackVector::~LaspackVector
virtual ~LaspackVector()
Definition: laspack_vector.h:300
libMesh::ParallelType
ParallelType
Defines an enum for parallel data structure types.
Definition: enum_parallel_type.h:33
libMesh::LaspackVector::set
virtual void set(const numeric_index_type i, const T value) override
Sets v(i) = value.
Definition: laspack_vector.h:496
libMesh::LaspackVector::operator-=
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
Subtracts v from *this, .
Definition: laspack_vector.C:97
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::LaspackVector::operator=
LaspackVector< T > & operator=(const LaspackVector< T > &v)
Copy assignment operator.
Definition: laspack_vector.C:310
libMesh::LaspackVector::swap
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
Definition: laspack_vector.h:542
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::NumericVector::type
ParallelType type() const
Definition: numeric_vector.h:160
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