laspack_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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
26 #include "libmesh/dense_vector.h"
27 #include "libmesh/laspack_vector.h"
28 #include "libmesh/laspack_matrix.h"
29 
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  const numeric_index_type n = this->size();
127 
128  for (numeric_index_type i=0; i<n; i++)
129  {
130  T v = (*this)(i);
131 
132  // Don't divide by zero!
133  libmesh_assert_not_equal_to (v, T(0));
134 
135  this->set(i, 1. / v);
136  }
137 }
138 
139 
140 
141 template <typename T>
143 {
144  const numeric_index_type n = this->size();
145 
146  for (numeric_index_type i=0; i<n; i++)
147  {
148  T v = (*this)(i);
149 
150  this->set(i, libmesh_conj(v) );
151  }
152 }
153 
154 
155 template <typename T>
156 void LaspackVector<T>::add (const T v)
157 {
158  const numeric_index_type n = this->size();
159 
160  for (numeric_index_type i=0; i<n; i++)
161  this->add (i, v);
162 
163 #ifndef NDEBUG
164  this->_is_closed = false;
165 #endif
166 }
167 
168 
169 
170 
171 template <typename T>
173 {
174  this->add (1., v);
175 }
176 
177 
178 
179 template <typename T>
180 void LaspackVector<T>::add (const T a, const NumericVector<T>& v_in)
181 {
182  // Make sure the vector passed in is really a LaspackVector
183  const LaspackVector* v = libmesh_cast_ptr<const LaspackVector*>(&v_in);
184 
185 #ifndef NDEBUG
186  const bool was_closed = this->_is_closed;
187 #endif
188 
189  libmesh_assert(v);
190  libmesh_assert_equal_to (this->size(), v->size());
191 
192  for (numeric_index_type i=0; i<v->size(); i++)
193  this->add (i, a*(*v)(i));
194 
195 #ifndef NDEBUG
196  this->_is_closed = was_closed;
197 #endif
198 }
199 
200 
201 
202 template <typename T>
203 void LaspackVector<T>::add_vector (const std::vector<T>& v,
204  const std::vector<numeric_index_type>& dof_indices)
205 {
206  libmesh_assert (!v.empty());
207  libmesh_assert_equal_to (v.size(), dof_indices.size());
208 
209  for (numeric_index_type i=0; i<v.size(); i++)
210  this->add (dof_indices[i], v[i]);
211 }
212 
213 
214 
215 template <typename T>
217  const std::vector<numeric_index_type>& dof_indices)
218 {
219  libmesh_assert_equal_to (V.size(), dof_indices.size());
220 
221  for (numeric_index_type i=0; i<V.size(); i++)
222  this->add (dof_indices[i], V(i));
223 }
224 
225 
226 
227 template <typename T>
229  const std::vector<numeric_index_type>& dof_indices)
230 {
231  libmesh_assert_equal_to (V.size(), dof_indices.size());
232 
233  for (unsigned int i=0; i<V.size(); i++)
234  this->add (dof_indices[i], V(i));
235 }
236 
237 
238 
239 template <typename T>
240 void LaspackVector<T>::insert (const std::vector<T>& v,
241  const std::vector<numeric_index_type>& dof_indices)
242 {
243  libmesh_assert (!v.empty());
244  libmesh_assert_equal_to (v.size(), dof_indices.size());
245 
246  for (numeric_index_type i=0; i<v.size(); i++)
247  this->set (dof_indices[i], v[i]);
248 }
249 
250 
251 
252 template <typename T>
254  const std::vector<numeric_index_type>& dof_indices)
255 {
256  libmesh_assert_equal_to (V.size(), dof_indices.size());
257 
258  for (numeric_index_type i=0; i<V.size(); i++)
259  this->set (dof_indices[i], V(i));
260 }
261 
262 
263 
264 template <typename T>
266  const std::vector<numeric_index_type>& dof_indices)
267 {
268  libmesh_assert_equal_to (V.size(), dof_indices.size());
269 
270  for (unsigned int i=0; i<V.size(); i++)
271  this->set (dof_indices[i], V(i));
272 }
273 
274 
275 
276 template <typename T>
278  const std::vector<numeric_index_type>& dof_indices)
279 {
280  libmesh_assert_equal_to (V.size(), dof_indices.size());
281 
282  for (unsigned int i=0; i<V.size(); i++)
283  this->set (dof_indices[i], V(i));
284 }
285 
286 
287 
288 template <typename T>
290  const SparseMatrix<T> &mat_in)
291 {
292  // Make sure the data passed in are really in Laspack types
293  const LaspackVector<T>* vec = libmesh_cast_ptr<const LaspackVector<T>*>(&vec_in);
294  const LaspackMatrix<T>* mat = libmesh_cast_ptr<const LaspackMatrix<T>*>(&mat_in);
295 
296  libmesh_assert(vec);
297  libmesh_assert(mat);
298 
299  // += mat*vec
300  AddAsgn_VV (&_vec, Mul_QV(const_cast<QMatrix*>(&mat->_QMat),
301  const_cast<QVector*>(&vec->_vec)));
302 }
303 
304 
305 template <typename T>
307  const SparseMatrix<T> &)
308 {
309  libmesh_not_implemented();
310 }
311 
312 
313 
314 template <typename T>
315 void LaspackVector<T>::scale (const T factor)
316 {
317  libmesh_assert (this->initialized());
318 
319  Asgn_VV(&_vec, Mul_SV (factor, &_vec));
320 }
321 
322 template <typename T>
324 {
325  libmesh_assert (this->initialized());
326 
327  const numeric_index_type n = this->size();
328 
329  for (numeric_index_type i=0; i!=n; ++i)
330  this->set(i,std::abs((*this)(i)));
331 }
332 
333 template <typename T>
335 {
336  libmesh_assert (this->initialized());
337 
338  // Make sure the NumericVector passed in is really a LaspackVector
339  const LaspackVector<T>* v = libmesh_cast_ptr<const LaspackVector<T>*>(&V);
340  libmesh_assert(v);
341 
342  return Mul_VV (const_cast<QVector*>(&(this->_vec)),
343  const_cast<QVector*>(&(v->_vec)));
344 }
345 
346 
347 
348 template <typename T>
351 {
352  libmesh_assert (this->initialized());
353  libmesh_assert (this->closed());
354 
355  V_SetAllCmp (&_vec, s);
356 
357  return *this;
358 }
359 
360 
361 
362 template <typename T>
365 {
366  // Make sure the NumericVector passed in is really a LaspackVector
367  const LaspackVector<T>* v =
368  libmesh_cast_ptr<const LaspackVector<T>*>(&v_in);
369 
370  libmesh_assert(v);
371 
372  *this = *v;
373 
374  return *this;
375 }
376 
377 
378 
379 template <typename T>
382 {
383  libmesh_assert (this->initialized());
384  libmesh_assert (v.closed());
385  libmesh_assert_equal_to (this->size(), v.size());
386 
387  if (v.size() != 0)
388  Asgn_VV (const_cast<QVector*>(&_vec),
389  const_cast<QVector*>(&v._vec)
390  );
391 
392 #ifndef NDEBUG
393  this->_is_closed = true;
394 #endif
395 
396  return *this;
397 }
398 
399 
400 
401 template <typename T>
403 LaspackVector<T>::operator = (const std::vector<T>& v)
404 {
409  if (this->size() == v.size())
410  for (numeric_index_type i=0; i<v.size(); i++)
411  this->set (i, v[i]);
412 
413  else
414  libmesh_error();
415 
416  return *this;
417 }
418 
419 
420 template <typename T>
422 {
423  // Make sure the NumericVector passed in is really a LaspackVector
424  LaspackVector<T>* v_local =
425  libmesh_cast_ptr<LaspackVector<T>*>(&v_local_in);
426 
427  libmesh_assert(v_local);
428 
429  *v_local = *this;
430 }
431 
432 
433 
434 template <typename T>
436  const std::vector<numeric_index_type>& libmesh_dbg_var(send_list)) const
437 {
438  // Make sure the NumericVector passed in is really a LaspackVector
439  LaspackVector<T>* v_local =
440  libmesh_cast_ptr<LaspackVector<T>*>(&v_local_in);
441 
442  libmesh_assert(v_local);
443  libmesh_assert_less_equal (send_list.size(), v_local->size());
444 
445  *v_local = *this;
446 }
447 
448 
449 
450 template <typename T>
451 void LaspackVector<T>::localize (const numeric_index_type libmesh_dbg_var(first_local_idx),
452  const numeric_index_type libmesh_dbg_var(last_local_idx),
453  const std::vector<numeric_index_type>& libmesh_dbg_var(send_list))
454 {
455  libmesh_assert_equal_to (first_local_idx, 0);
456  libmesh_assert_equal_to (last_local_idx+1, this->size());
457 
458  libmesh_assert_less_equal (send_list.size(), this->size());
459 
460 #ifndef NDEBUG
461  this->_is_closed = true;
462 #endif
463 }
464 
465 
466 
467 template <typename T>
468 void LaspackVector<T>::localize (std::vector<T>& v_local) const
469 
470 {
471  v_local.resize(this->size());
472 
473  for (numeric_index_type i=0; i<v_local.size(); i++)
474  v_local[i] = (*this)(i);
475 }
476 
477 
478 
479 template <typename T>
480 void LaspackVector<T>::localize_to_one (std::vector<T>& v_local,
481  const processor_id_type libmesh_dbg_var(pid)) const
482 {
483  libmesh_assert_equal_to (pid, 0);
484 
485  this->localize (v_local);
486 }
487 
488 
489 
490 template <typename T>
492  const NumericVector<T>& /*vec2*/)
493 {
494  libmesh_not_implemented();
495 }
496 
497 
498 
499 template <typename T>
501 {
502  libmesh_assert (this->initialized());
503  if (!this->size())
505 
506  Real the_max = libmesh_real((*this)(0));
507 
508  const numeric_index_type n = this->size();
509 
510  for (numeric_index_type i=1; i<n; i++)
511  the_max = std::max (the_max, libmesh_real((*this)(i)));
512 
513  return the_max;
514 }
515 
516 
517 
518 template <typename T>
520 {
521  libmesh_assert (this->initialized());
522  if (!this->size())
524 
525  Real the_min = libmesh_real((*this)(0));
526 
527  const numeric_index_type n = this->size();
528 
529  for (numeric_index_type i=1; i<n; i++)
530  the_min = std::min (the_min, libmesh_real((*this)(i)));
531 
532  return the_min;
533 }
534 
535 
536 //------------------------------------------------------------------
537 // Explicit instantiations
538 template class LaspackVector<Number>;
539 
540 } // namespace libMesh
541 
542 
543 #endif // #ifdef LIBMESH_HAVE_LASPACK

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:05 UTC

Hosted By:
SourceForge.net Logo