libMesh
Loading...
Searching...
No Matches
laspack_vector.C
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// 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
33namespace libMesh
34{
35
36template <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
53template <typename T>
55{
56 libmesh_assert (this->closed());
57
58 return static_cast<Real>(l1Norm_V(const_cast<QVector*>(&_vec)));
59}
60
61
62
63template <typename T>
65{
66 libmesh_assert (this->closed());
67
68 return static_cast<Real>(l2Norm_V(const_cast<QVector*>(&_vec)));
69}
70
71
72
73template <typename T>
75{
76 libmesh_assert (this->closed());
77
78 return static_cast<Real>(MaxNorm_V(const_cast<QVector*>(&_vec)));
79}
80
81
82
83template <typename T>
85{
86 libmesh_assert (this->closed());
87
88 this->add(1., v);
89
90 return *this;
91}
92
93
94
95
96template <typename T>
98{
99 libmesh_assert (this->closed());
100
101 this->add(-1., v);
102
103 return *this;
104}
105
106
107
108template <typename T>
110{
111 libmesh_assert_equal_to(size(), v.size());
112
113#ifndef NDEBUG
114 const bool was_closed = this->_is_closed;
115#endif
116
117 const numeric_index_type n = this->size();
118
119 for (numeric_index_type i=0; i<n; i++)
120 this->set(i, (*this)(i) * v(i));
121
122 // This is an embarrassingly parallel method, but set() isn't in
123 // general so set() overzealously marked us as non-closed
124#ifndef NDEBUG
125 this->_is_closed = was_closed;
126#endif
127
128 return *this;
129}
130
131
132
133template <typename T>
135{
136 libmesh_assert_equal_to(size(), v.size());
137
138#ifndef NDEBUG
139 const bool was_closed = this->_is_closed;
140#endif
141
142 const numeric_index_type n = this->size();
143
144 for (numeric_index_type i=0; i<n; i++)
145 this->set(i, (*this)(i) / v(i));
146
147 // This is an embarrassingly parallel method, but set() isn't in
148 // general so set() overzealously marked us as non-closed
149#ifndef NDEBUG
150 this->_is_closed = was_closed;
151#endif
152
153 return *this;
154}
155
156
157
158template <typename T>
160{
161 const numeric_index_type n = this->size();
162
163#ifndef NDEBUG
164 const bool was_closed = this->_is_closed;
165#endif
166
167 for (numeric_index_type i=0; i<n; i++)
168 {
169 T v = (*this)(i);
170
171 // Don't divide by zero!
172 libmesh_assert_not_equal_to (v, T(0));
173
174 this->set(i, 1. / v);
175 }
176
177 // This is an embarrassingly parallel method, but set() isn't in
178 // general so set() overzealously marked us as non-closed
179#ifndef NDEBUG
180 this->_is_closed = was_closed;
181#endif
182}
183
184
185
186template <typename T>
188{
189 const numeric_index_type n = this->size();
190
191#ifndef NDEBUG
192 const bool was_closed = this->_is_closed;
193#endif
194
195 for (numeric_index_type i=0; i<n; i++)
196 {
197 T v = (*this)(i);
198
199 this->set(i, libmesh_conj(v) );
200 }
201
202 // This is an embarrassingly parallel method, but set() isn't in
203 // general so set() overzealously marked us as non-closed
204#ifndef NDEBUG
205 this->_is_closed = was_closed;
206#endif
207}
208
209
210template <typename T>
211void LaspackVector<T>::add (const T v)
212{
213#ifndef NDEBUG
214 const bool was_closed = this->_is_closed;
215#endif
216
217 const numeric_index_type n = this->size();
218
219 for (numeric_index_type i=0; i<n; i++)
220 this->add (i, v);
221
222 // This is an embarrassingly parallel method, but set() isn't in
223 // general so set() overzealously marked us as non-closed
224#ifndef NDEBUG
225 this->_is_closed = was_closed;
226#endif
227}
228
229
230
231
232template <typename T>
234{
235 this->add (1., v);
236}
237
238
239
240template <typename T>
241void LaspackVector<T>::add (const T a, const NumericVector<T> & v_in)
242{
243 // Make sure the vector passed in is really a LaspackVector
244 const LaspackVector * v = cast_ptr<const LaspackVector *>(&v_in);
245
246#ifndef NDEBUG
247 const bool was_closed = this->_is_closed;
248#endif
249
251 libmesh_assert_equal_to (this->size(), v->size());
252
253 for (auto i : make_range(v->size()))
254 this->add (i, a*(*v)(i));
255
256#ifndef NDEBUG
257 this->_is_closed = was_closed;
258#endif
259}
260
261
262
263template <typename T>
265 const SparseMatrix<T> & mat_in)
266{
267 // Make sure the data passed in are really in Laspack types
268 const LaspackVector<T> * vec = cast_ptr<const LaspackVector<T> *>(&vec_in);
269 const LaspackMatrix<T> * mat = cast_ptr<const LaspackMatrix<T> *>(&mat_in);
270
271 libmesh_assert(vec);
272 libmesh_assert(mat);
273
274 // += mat*vec
275 AddAsgn_VV (&_vec, Mul_QV(const_cast<QMatrix*>(&mat->_QMat),
276 const_cast<QVector*>(&vec->_vec)));
277}
278
279
280template <typename T>
282 const SparseMatrix<T> &)
283{
284 libmesh_not_implemented();
285}
286
287
288
289template <typename T>
290void LaspackVector<T>::scale (const T factor)
291{
292 libmesh_assert (this->initialized());
293
294 Asgn_VV(&_vec, Mul_SV (factor, &_vec));
295}
296
297template <typename T>
299{
300 libmesh_assert (this->initialized());
301
302 const numeric_index_type n = this->size();
303
304 for (numeric_index_type i=0; i!=n; ++i)
305 this->set(i,std::abs((*this)(i)));
306}
307
308template <typename T>
310{
311 libmesh_assert (this->initialized());
312
313 // Make sure the NumericVector passed in is really a LaspackVector
314 const LaspackVector<T> * v = cast_ptr<const LaspackVector<T> *>(&v_in);
316
317 return Mul_VV (const_cast<QVector*>(&(this->_vec)),
318 const_cast<QVector*>(&(v->_vec)));
319}
320
321
322
323template <typename T>
326{
327 libmesh_assert (this->initialized());
328 libmesh_assert (this->closed());
329
330 V_SetAllCmp (&_vec, s);
331
332 return *this;
333}
334
335
336
337template <typename T>
340{
341 // Make sure the NumericVector passed in is really a LaspackVector
342 const LaspackVector<T> * v =
343 cast_ptr<const LaspackVector<T> *>(&v_in);
344
346
347 *this = *v;
348
349 return *this;
350}
351
352
353
354template <typename T>
357{
358 libmesh_assert (this->initialized());
360 libmesh_assert_equal_to (this->size(), v.size());
361
362 if (v.size() != 0)
363 Asgn_VV (const_cast<QVector*>(&_vec),
364 const_cast<QVector*>(&v._vec)
365 );
366
367#ifndef NDEBUG
368 this->_is_closed = true;
369#endif
370
371 return *this;
372}
373
374
375
376template <typename T>
378LaspackVector<T>::operator = (const std::vector<T> & v)
379{
384 if (this->size() == v.size())
385 for (auto i : index_range(v))
386 this->set (i, v[i]);
387
388 else
389 libmesh_error_msg("this->size() = " << this->size() << " must be equal to v.size() = " << v.size());
390
391 return *this;
392}
393
394
395template <typename T>
397{
398 // Make sure the NumericVector passed in is really a LaspackVector
399 LaspackVector<T> * v_local =
400 cast_ptr<LaspackVector<T> *>(&v_local_in);
401
402 libmesh_assert(v_local);
403
404 *v_local = *this;
405}
406
407
408
409template <typename T>
411 const std::vector<numeric_index_type> & libmesh_dbg_var(send_list)) const
412{
413 // Make sure the NumericVector passed in is really a LaspackVector
414 LaspackVector<T> * v_local =
415 cast_ptr<LaspackVector<T> *>(&v_local_in);
416
417 libmesh_assert(v_local);
418 libmesh_assert_less_equal (send_list.size(), v_local->size());
419
420 *v_local = *this;
421}
422
423
424
425template <typename T>
426void LaspackVector<T>::localize (std::vector<T> & v_local,
427 const std::vector<numeric_index_type> & indices) const
428{
429 // LaspackVectors are serial, so we can just copy values
430 v_local.resize(indices.size());
431
432 for (auto i : index_range(v_local))
433 v_local[i] = (*this)(indices[i]);
434}
435
436
437
438template <typename T>
439void LaspackVector<T>::localize (const numeric_index_type libmesh_dbg_var(first_local_idx),
440 const numeric_index_type libmesh_dbg_var(last_local_idx),
441 const std::vector<numeric_index_type> & libmesh_dbg_var(send_list))
442{
443 libmesh_assert_equal_to (first_local_idx, 0);
444 libmesh_assert_equal_to (last_local_idx+1, this->size());
445
446 libmesh_assert_less_equal (send_list.size(), this->size());
447
448#ifndef NDEBUG
449 this->_is_closed = true;
450#endif
451}
452
453
454
455template <typename T>
456void LaspackVector<T>::localize (std::vector<T> & v_local) const
457
458{
459 v_local.resize(this->size());
460
461 for (auto i : index_range(v_local))
462 v_local[i] = (*this)(i);
463}
464
465
466
467template <typename T>
468void LaspackVector<T>::localize_to_one (std::vector<T> & v_local,
469 const processor_id_type libmesh_dbg_var(pid)) const
470{
471 libmesh_assert_equal_to (pid, 0);
472
473 this->localize (v_local);
474}
475
476
477
478template <typename T>
480 const NumericVector<T> & /*vec2*/)
481{
482 libmesh_not_implemented();
483}
484
485template <typename T>
487 const NumericVector<T> & /*vec2*/)
488{
489 libmesh_not_implemented();
490}
491
492template <typename T>
494{
495 libmesh_assert (this->initialized());
496 if (!this->size())
497 return -std::numeric_limits<Real>::max();
498
499 Real the_max = libmesh_real((*this)(0));
500
501 const numeric_index_type n = this->size();
502
503 for (numeric_index_type i=1; i<n; i++)
504 the_max = std::max (the_max, libmesh_real((*this)(i)));
505
506 return the_max;
507}
508
509
510
511template <typename T>
513{
514 libmesh_assert (this->initialized());
515 if (!this->size())
516 return std::numeric_limits<Real>::max();
517
518 Real the_min = libmesh_real((*this)(0));
519
520 const numeric_index_type n = this->size();
521
522 for (numeric_index_type i=1; i<n; i++)
523 the_min = std::min (the_min, libmesh_real((*this)(i)));
524
525 return the_min;
526}
527
528
529//------------------------------------------------------------------
530// Explicit instantiations
531template class LIBMESH_EXPORT LaspackVector<Number>;
532
533} // namespace libMesh
534
535
536#endif // #ifdef LIBMESH_HAVE_LASPACK
The LaspackMatrix class wraps a QMatrix object from the Laspack library.
This class provides a nice interface to the Laspack C-based data structures for serial vectors.
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
Subtracts v from *this, .
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
QVector _vec
Actual Laspack vector datatype to hold vector entries.
virtual void localize(std::vector< T > &v_local) const override
Creates a copy of the global vector in the local vector v_local.
virtual numeric_index_type size() const override
virtual void add_vector(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
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.
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
Computes the component-wise division of this vector's entries by another's, .
virtual T sum() const override
virtual Real linfty_norm() const override
virtual Real l2_norm() const override
virtual Real l1_norm() const override
virtual void conjugate() override
Negates the imaginary component of each entry in the vector.
virtual Real min() const override
virtual void pointwise_divide(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
virtual void reciprocal() override
Computes the component-wise reciprocal, .
virtual void abs() override
Sets for each entry in the vector.
virtual void add(const numeric_index_type i, const T value) override
Adds value to the vector entry specified by i.
LaspackVector< T > & operator=(const LaspackVector< T > &v)
Copy assignment operator.
virtual T dot(const NumericVector< T > &v) const override
virtual void scale(const T factor) override
Scale each element of the vector by the given factor.
virtual NumericVector< T > & operator*=(const NumericVector< T > &v) override
Computes the component-wise multiplication of this vector's entries by another's, .
virtual Real max() const override
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
Adds v to *this, .
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual numeric_index_type size() const =0
virtual bool closed() const
Generic sparse matrix.
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
bool closed()
Checks that the library has been closed.
Definition libmesh.C:331
T libmesh_real(T a)
libmesh_assert(ctx)
dof_id_type numeric_index_type
Definition id_types.h:99
bool initialized()
Checks that library initialization has been done.
Definition libmesh.C:324
T libmesh_conj(T a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
Definition id_types.h:104
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