laspack_vector.h
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 00020 00021 #ifndef LIBMESH_LASPACK_VECTOR_H 00022 #define LIBMESH_LASPACK_VECTOR_H 00023 00024 00025 00026 #include "libmesh/libmesh_common.h" 00027 00028 #ifdef LIBMESH_HAVE_LASPACK 00029 00030 // Local includes 00031 #include "libmesh/numeric_vector.h" 00032 00033 // C++ includes 00034 #include <cstdio> // for std::sprintf 00035 00036 // Laspack includes 00037 #include <operats.h> 00038 #include <qvector.h> 00039 00040 namespace libMesh 00041 { 00042 00043 00044 // Forward declarations 00045 template <typename T> class LaspackLinearSolver; 00046 template <typename T> class SparseMatrix; 00047 00048 00056 template <typename T> 00057 class LaspackVector : public NumericVector<T> 00058 { 00059 public: 00060 00064 explicit 00065 LaspackVector (const ParallelType = AUTOMATIC); 00066 00070 explicit 00071 LaspackVector (const numeric_index_type n, 00072 const ParallelType = AUTOMATIC); 00073 00078 LaspackVector (const numeric_index_type n, 00079 const numeric_index_type n_local, 00080 const ParallelType = AUTOMATIC); 00081 00087 LaspackVector (const numeric_index_type N, 00088 const numeric_index_type n_local, 00089 const std::vector<numeric_index_type>& ghost, 00090 const ParallelType = AUTOMATIC); 00091 00096 ~LaspackVector (); 00097 00101 void close (); 00102 00106 void clear (); 00107 00112 void zero (); 00113 00119 virtual AutoPtr<NumericVector<T> > zero_clone () const; 00120 00124 AutoPtr<NumericVector<T> > clone () const; 00125 00139 void init (const numeric_index_type N, 00140 const numeric_index_type n_local, 00141 const bool fast=false, 00142 const ParallelType ptype=AUTOMATIC); 00143 00147 void init (const numeric_index_type N, 00148 const bool fast=false, 00149 const ParallelType ptype=AUTOMATIC); 00150 00155 void init (const numeric_index_type /*N*/, 00156 const numeric_index_type /*n_local*/, 00157 const std::vector<numeric_index_type>& /*ghost*/, 00158 const bool /*fast*/ = false, 00159 const ParallelType = AUTOMATIC); 00160 00165 virtual void init (const NumericVector<T>& other, 00166 const bool fast = false); 00167 00171 NumericVector<T> & operator= (const T s); 00172 00176 NumericVector<T> & operator= (const NumericVector<T> &V); 00177 00181 LaspackVector<T> & operator= (const LaspackVector<T> &V); 00182 00186 NumericVector<T> & operator= (const std::vector<T> &v); 00187 00193 Real min () const; 00194 00200 Real max () const; 00201 00205 T sum () const; 00206 00211 Real l1_norm () const; 00212 00218 Real l2_norm () const; 00219 00225 Real linfty_norm () const; 00226 00234 numeric_index_type size () const; 00235 00240 numeric_index_type local_size() const; 00241 00246 numeric_index_type first_local_index() const; 00247 00252 numeric_index_type last_local_index() const; 00253 00257 T operator() (const numeric_index_type i) const; 00258 00263 NumericVector<T> & operator += (const NumericVector<T> &V); 00264 00269 NumericVector<T> & operator -= (const NumericVector<T> &V); 00270 00274 virtual void reciprocal(); 00275 00279 void set (const numeric_index_type i, const T value); 00280 00284 void add (const numeric_index_type i, const T value); 00285 00291 void add (const T s); 00292 00298 void add (const NumericVector<T>& V); 00299 00305 void add (const T a, const NumericVector<T>& v); 00306 00312 void add_vector (const std::vector<T>& v, 00313 const std::vector<numeric_index_type>& dof_indices); 00314 00321 void add_vector (const NumericVector<T>& V, 00322 const std::vector<numeric_index_type>& dof_indices); 00323 00328 void add_vector (const NumericVector<T> &, 00329 const SparseMatrix<T> &); 00330 00337 void add_vector (const DenseVector<T>& V, 00338 const std::vector<numeric_index_type>& dof_indices); 00339 00344 void add_vector_transpose (const NumericVector<T> &, 00345 const SparseMatrix<T> &); 00346 00351 virtual void insert (const std::vector<T>& v, 00352 const std::vector<numeric_index_type>& dof_indices); 00353 00360 virtual void insert (const NumericVector<T>& V, 00361 const std::vector<numeric_index_type>& dof_indices); 00362 00368 virtual void insert (const DenseVector<T>& V, 00369 const std::vector<numeric_index_type>& dof_indices); 00370 00376 virtual void insert (const DenseSubVector<T>& V, 00377 const std::vector<numeric_index_type>& dof_indices); 00378 00383 void scale (const T factor); 00384 00389 virtual void abs(); 00390 00394 virtual T dot(const NumericVector<T>& V) const; 00395 00400 void localize (std::vector<T>& v_local) const; 00401 00406 void localize (NumericVector<T>& v_local) const; 00407 00413 void localize (NumericVector<T>& v_local, 00414 const std::vector<numeric_index_type>& send_list) const; 00415 00420 void localize (const numeric_index_type first_local_idx, 00421 const numeric_index_type last_local_idx, 00422 const std::vector<numeric_index_type>& send_list); 00423 00424 00431 void localize_to_one (std::vector<T>& v_local, 00432 const processor_id_type proc_id=0) const; 00433 00438 virtual void pointwise_mult (const NumericVector<T>& vec1, 00439 const NumericVector<T>& vec2); 00440 00444 virtual void swap (NumericVector<T> &v); 00445 00446 private: 00447 00452 QVector _vec; 00453 00457 friend class LaspackLinearSolver<T>; 00458 }; 00459 00460 00461 00462 //----------------------- ---------------------------------- 00463 // LaspackVector inline methods 00464 template <typename T> 00465 inline 00466 LaspackVector<T>::LaspackVector (const ParallelType ptype) 00467 { 00468 this->_type = ptype; 00469 } 00470 00471 00472 00473 template <typename T> 00474 inline 00475 LaspackVector<T>::LaspackVector (const numeric_index_type n, 00476 const ParallelType ptype) 00477 { 00478 this->init(n, n, false, ptype); 00479 } 00480 00481 00482 00483 template <typename T> 00484 inline 00485 LaspackVector<T>::LaspackVector (const numeric_index_type n, 00486 const numeric_index_type n_local, 00487 const ParallelType ptype) 00488 { 00489 this->init(n, n_local, false, ptype); 00490 } 00491 00492 00493 00494 template <typename T> 00495 inline 00496 LaspackVector<T>::LaspackVector (const numeric_index_type N, 00497 const numeric_index_type n_local, 00498 const std::vector<numeric_index_type>& ghost, 00499 const ParallelType ptype) 00500 { 00501 this->init(N, n_local, ghost, false, ptype); 00502 } 00503 00504 00505 00506 template <typename T> 00507 inline 00508 LaspackVector<T>::~LaspackVector () 00509 { 00510 this->clear (); 00511 } 00512 00513 00514 00515 template <typename T> 00516 inline 00517 void LaspackVector<T>::init (const numeric_index_type n, 00518 const numeric_index_type libmesh_dbg_var(n_local), 00519 const bool fast, 00520 const ParallelType) 00521 { 00522 // Laspack vectors only for serial cases, 00523 // but can provide a "parallel" vector on one processor. 00524 libmesh_assert_equal_to (n, n_local); 00525 00526 this->_type = SERIAL; 00527 00528 // Clear initialized vectors 00529 if (this->initialized()) 00530 this->clear(); 00531 00532 // create a sequential vector 00533 00534 static int cnt = 0; 00535 char foo[80]; 00536 std::sprintf(foo, "Vec-%d", cnt++); 00537 00538 V_Constr(&_vec, const_cast<char*>(foo), n, Normal, _LPTrue); 00539 00540 this->_is_initialized = true; 00541 #ifndef NDEBUG 00542 this->_is_closed = true; 00543 #endif 00544 00545 // Optionally zero out all components 00546 if (fast == false) 00547 this->zero (); 00548 00549 return; 00550 } 00551 00552 00553 00554 template <typename T> 00555 inline 00556 void LaspackVector<T>::init (const numeric_index_type n, 00557 const bool fast, 00558 const ParallelType ptype) 00559 { 00560 this->init(n,n,fast,ptype); 00561 } 00562 00563 00564 template <typename T> 00565 inline 00566 void LaspackVector<T>::init (const numeric_index_type n, 00567 const numeric_index_type n_local, 00568 const std::vector<numeric_index_type>& libmesh_dbg_var(ghost), 00569 const bool fast, 00570 const ParallelType ptype) 00571 { 00572 libmesh_assert(ghost.empty()); 00573 this->init(n,n_local,fast,ptype); 00574 } 00575 00576 00577 00578 /* Default implementation for solver packages for which ghosted 00579 vectors are not yet implemented. */ 00580 template <class T> 00581 void LaspackVector<T>::init (const NumericVector<T>& other, 00582 const bool fast) 00583 { 00584 this->init(other.size(),other.local_size(),fast,other.type()); 00585 } 00586 00587 00588 00589 template <typename T> 00590 inline 00591 void LaspackVector<T>::close () 00592 { 00593 libmesh_assert (this->initialized()); 00594 00595 #ifndef NDEBUG 00596 this->_is_closed = true; 00597 #endif 00598 } 00599 00600 00601 00602 template <typename T> 00603 inline 00604 void LaspackVector<T>::clear () 00605 { 00606 if (this->initialized()) 00607 { 00608 V_Destr (&_vec); 00609 } 00610 00611 this->_is_initialized = false; 00612 #ifndef NDEBUG 00613 this->_is_closed = false; 00614 #endif 00615 } 00616 00617 00618 00619 template <typename T> inline 00620 void LaspackVector<T>::zero () 00621 { 00622 libmesh_assert (this->initialized()); 00623 libmesh_assert (this->closed()); 00624 00625 V_SetAllCmp (&_vec, 0.); 00626 } 00627 00628 00629 00630 template <typename T> 00631 inline 00632 AutoPtr<NumericVector<T> > LaspackVector<T>::zero_clone () const 00633 { 00634 AutoPtr<NumericVector<T> > cloned_vector (new LaspackVector<T>); 00635 00636 cloned_vector->init(*this); 00637 00638 return cloned_vector; 00639 } 00640 00641 00642 00643 template <typename T> 00644 inline 00645 AutoPtr<NumericVector<T> > LaspackVector<T>::clone () const 00646 { 00647 AutoPtr<NumericVector<T> > cloned_vector (new LaspackVector<T>); 00648 00649 cloned_vector->init(*this, true); 00650 00651 *cloned_vector = *this; 00652 00653 return cloned_vector; 00654 } 00655 00656 00657 00658 template <typename T> 00659 inline 00660 numeric_index_type LaspackVector<T>::size () const 00661 { 00662 libmesh_assert (this->initialized()); 00663 00664 return static_cast<numeric_index_type>(V_GetDim(const_cast<QVector*>(&_vec))); 00665 } 00666 00667 00668 00669 template <typename T> 00670 inline 00671 numeric_index_type LaspackVector<T>::local_size () const 00672 { 00673 libmesh_assert (this->initialized()); 00674 00675 return this->size(); 00676 } 00677 00678 00679 00680 template <typename T> 00681 inline 00682 numeric_index_type LaspackVector<T>::first_local_index () const 00683 { 00684 libmesh_assert (this->initialized()); 00685 00686 return 0; 00687 } 00688 00689 00690 00691 template <typename T> 00692 inline 00693 numeric_index_type LaspackVector<T>::last_local_index () const 00694 { 00695 libmesh_assert (this->initialized()); 00696 00697 return this->size(); 00698 } 00699 00700 00701 00702 template <typename T> 00703 inline 00704 void LaspackVector<T>::set (const numeric_index_type i, const T value) 00705 { 00706 libmesh_assert (this->initialized()); 00707 libmesh_assert_less (i, this->size()); 00708 00709 V_SetCmp (&_vec, i+1, value); 00710 00711 #ifndef NDEBUG 00712 this->_is_closed = false; 00713 #endif 00714 } 00715 00716 00717 00718 template <typename T> 00719 inline 00720 void LaspackVector<T>::add (const numeric_index_type i, const T value) 00721 { 00722 libmesh_assert (this->initialized()); 00723 libmesh_assert_less (i, this->size()); 00724 00725 V_AddCmp (&_vec, i+1, value); 00726 00727 #ifndef NDEBUG 00728 this->_is_closed = false; 00729 #endif 00730 } 00731 00732 00733 00734 template <typename T> 00735 inline 00736 T LaspackVector<T>::operator() (const numeric_index_type i) const 00737 { 00738 libmesh_assert (this->initialized()); 00739 libmesh_assert ( ((i >= this->first_local_index()) && 00740 (i < this->last_local_index())) ); 00741 00742 00743 return static_cast<T>(V_GetCmp(const_cast<QVector*>(&_vec), i+1)); 00744 } 00745 00746 00747 00748 template <typename T> 00749 inline 00750 void LaspackVector<T>::swap (NumericVector<T> &other) 00751 { 00752 LaspackVector<T>& v = libmesh_cast_ref<LaspackVector<T>&>(other); 00753 00754 // This is all grossly dependent on Laspack version... 00755 00756 std::swap(_vec.Name, v._vec.Name); 00757 std::swap(_vec.Dim, v._vec.Dim); 00758 std::swap(_vec.Instance, v._vec.Instance); 00759 std::swap(_vec.LockLevel, v._vec.LockLevel); 00760 std::swap(_vec.Multipl, v._vec.Multipl); 00761 std::swap(_vec.OwnData, v._vec.OwnData); 00762 00763 // This should still be O(1), since _vec.Cmp is just a pointer to 00764 // data on the heap 00765 00766 std::swap(_vec.Cmp, v._vec.Cmp); 00767 } 00768 00769 00770 } // namespace libMesh 00771 00772 00773 #endif // #ifdef LIBMESH_HAVE_LASPACK 00774 #endif // LIBMESH_LASPACK_VECTOR_H
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: