numeric_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 #ifndef LIBMESH_NUMERIC_VECTOR_H 00021 #define LIBMESH_NUMERIC_VECTOR_H 00022 00023 // Local includes 00024 #include "libmesh/libmesh_common.h" 00025 #include "libmesh/auto_ptr.h" 00026 #include "libmesh/enum_parallel_type.h" 00027 #include "libmesh/enum_solver_package.h" 00028 #include "libmesh/id_types.h" 00029 #include "libmesh/reference_counted_object.h" 00030 #include "libmesh/libmesh.h" 00031 00032 // C++ includes 00033 #include <cstddef> 00034 #include <set> 00035 #include <vector> 00036 00037 namespace libMesh 00038 { 00039 00040 00041 // forward declarations 00042 template <typename T> class NumericVector; 00043 template <typename T> class DenseVector; 00044 template <typename T> class DenseSubVector; 00045 template <typename T> class SparseMatrix; 00046 template <typename T> class ShellMatrix; 00047 00048 00056 template <typename T> 00057 class NumericVector : public ReferenceCountedObject<NumericVector<T> > 00058 { 00059 public: 00060 00064 explicit 00065 NumericVector (const ParallelType ptype = AUTOMATIC); 00066 00070 explicit 00071 NumericVector (const numeric_index_type n, 00072 const ParallelType ptype = AUTOMATIC); 00073 00078 NumericVector (const numeric_index_type n, 00079 const numeric_index_type n_local, 00080 const ParallelType ptype = AUTOMATIC); 00081 00087 NumericVector (const numeric_index_type N, 00088 const numeric_index_type n_local, 00089 const std::vector<numeric_index_type>& ghost, 00090 const ParallelType ptype = AUTOMATIC); 00091 00092 public: 00093 00098 virtual ~NumericVector (); 00099 00104 static AutoPtr<NumericVector<T> > 00105 build(const SolverPackage solver_package = libMesh::default_solver_package()); 00106 00111 virtual bool initialized() const { return _is_initialized; } 00112 00116 ParallelType type() const { return _type; } 00117 00121 ParallelType & type() { return _type; } 00122 00127 virtual bool closed() const { return _is_closed; } 00128 00132 virtual void close () = 0; 00133 00137 virtual void clear (); 00138 00143 virtual void zero () = 0; 00144 00151 virtual AutoPtr<NumericVector<T> > zero_clone () const = 0; 00152 00157 virtual AutoPtr<NumericVector<T> > clone () const = 0; 00158 00172 virtual void init (const numeric_index_type, 00173 const numeric_index_type, 00174 const bool = false, 00175 const ParallelType = AUTOMATIC) = 0; 00176 00180 virtual void init (const numeric_index_type, 00181 const bool = false, 00182 const ParallelType = AUTOMATIC) = 0; 00183 00188 virtual void init (const numeric_index_type /*N*/, 00189 const numeric_index_type /*n_local*/, 00190 const std::vector<numeric_index_type>& /*ghost*/, 00191 const bool /*fast*/ = false, 00192 const ParallelType = AUTOMATIC) = 0; 00193 00198 virtual void init (const NumericVector<T>& other, 00199 const bool fast = false) = 0; 00200 00204 virtual NumericVector<T> & operator= (const T s) = 0; 00205 00209 virtual NumericVector<T> & operator= (const NumericVector<T> &V) = 0; 00210 00214 virtual NumericVector<T> & operator= (const std::vector<T> &v) = 0; 00215 00221 virtual Real min () const = 0; 00222 00228 virtual Real max () const = 0; 00229 00233 virtual T sum() const = 0; 00234 00239 virtual Real l1_norm () const = 0; 00240 00246 virtual Real l2_norm () const = 0; 00247 00253 virtual Real linfty_norm () const = 0; 00254 00263 virtual Real subset_l1_norm (const std::set<numeric_index_type> & indices) const; 00264 00274 virtual Real subset_l2_norm (const std::set<numeric_index_type> & indices) const; 00275 00284 virtual Real subset_linfty_norm (const std::set<numeric_index_type> & indices) const; 00285 00293 virtual numeric_index_type size () const = 0; 00294 00300 virtual numeric_index_type local_size() const = 0; 00301 00307 virtual numeric_index_type first_local_index() const = 0; 00308 00314 virtual numeric_index_type last_local_index() const = 0; 00315 00319 virtual T operator() (const numeric_index_type i) const = 0; 00320 00324 virtual T el(const numeric_index_type i) const { return (*this)(i); } 00325 00332 virtual void get(const std::vector<numeric_index_type>& index, std::vector<T>& values) const; 00333 00338 virtual NumericVector<T> & operator += (const NumericVector<T> &V) = 0; 00339 00344 virtual NumericVector<T> & operator -= (const NumericVector<T> &V) = 0; 00345 00350 NumericVector<T> & operator *= (const T a) { this->scale(a); return *this; } 00351 00356 NumericVector<T> & operator /= (const T a) { this->scale(1./a); return *this; } 00357 00361 virtual void reciprocal() = 0; 00362 00366 virtual void set (const numeric_index_type i, const T value) = 0; 00367 00371 virtual void add (const numeric_index_type i, const T value) = 0; 00372 00378 virtual void add (const T s) = 0; 00379 00385 virtual void add (const NumericVector<T>& V) = 0; 00386 00392 virtual void add (const T a, const NumericVector<T>& v) = 0; 00393 00399 virtual void add_vector (const std::vector<T>& v, 00400 const std::vector<numeric_index_type>& dof_indices) = 0; 00401 00408 virtual void add_vector (const NumericVector<T>& V, 00409 const std::vector<numeric_index_type>& dof_indices) = 0; 00410 00415 virtual void add_vector (const NumericVector<T>&, 00416 const SparseMatrix<T>&) = 0; 00417 00422 void add_vector (const NumericVector<T>& v, 00423 const ShellMatrix<T>& a); 00424 00431 virtual void add_vector (const DenseVector<T>& V, 00432 const std::vector<numeric_index_type>& dof_indices) = 0; 00433 00438 virtual void add_vector_transpose (const NumericVector<T>&, 00439 const SparseMatrix<T>&) = 0; 00440 00445 virtual void insert (const std::vector<T>& v, 00446 const std::vector<numeric_index_type>& dof_indices) = 0; 00447 00454 virtual void insert (const NumericVector<T>& V, 00455 const std::vector<numeric_index_type>& dof_indices) = 0; 00456 00463 virtual void insert (const DenseVector<T>& V, 00464 const std::vector<numeric_index_type>& dof_indices) = 0; 00465 00471 virtual void insert (const DenseSubVector<T>& V, 00472 const std::vector<numeric_index_type>& dof_indices) = 0; 00473 00478 virtual void scale (const T factor) = 0; 00479 00484 virtual void abs() = 0; 00485 00489 virtual T dot(const NumericVector<T>&) const = 0; 00490 00495 virtual void localize (std::vector<T>& v_local) const = 0; 00496 00501 virtual void localize (NumericVector<T>& v_local) const = 0; 00502 00508 virtual void localize (NumericVector<T>& v_local, 00509 const std::vector<numeric_index_type>& send_list) const = 0; 00510 00515 virtual void localize (const numeric_index_type first_local_idx, 00516 const numeric_index_type last_local_idx, 00517 const std::vector<numeric_index_type>& send_list) = 0; 00518 00525 virtual void localize_to_one (std::vector<T>& v_local, 00526 const processor_id_type proc_id=0) const = 0; 00527 00536 virtual int compare (const NumericVector<T> &other_vector, 00537 const Real threshold = TOLERANCE) const; 00538 00547 virtual int local_relative_compare (const NumericVector<T> &other_vector, 00548 const Real threshold = TOLERANCE) const; 00549 00558 virtual int global_relative_compare (const NumericVector<T> &other_vector, 00559 const Real threshold = TOLERANCE) const; 00560 00565 virtual void pointwise_mult (const NumericVector<T>& vec1, 00566 const NumericVector<T>& vec2) = 0; 00567 00572 virtual void print(std::ostream& os=libMesh::out) const; 00573 00578 virtual void print_global(std::ostream& os=libMesh::out) const; 00579 00583 friend std::ostream& operator << (std::ostream& os, const NumericVector<T>& v) 00584 { 00585 v.print_global(os); 00586 return os; 00587 } 00588 00595 virtual void print_matlab(const std::string name="NULL") const 00596 { 00597 libMesh::err << "ERROR: Not Implemented in base class yet!" << std::endl; 00598 libMesh::err << "ERROR writing MATLAB file " << name << std::endl; 00599 libmesh_error(); 00600 } 00601 00608 virtual void create_subvector(NumericVector<T>& , 00609 const std::vector<numeric_index_type>& ) const 00610 { 00611 libMesh::err << "ERROR: Not Implemented in base class yet!" << std::endl; 00612 libmesh_error(); 00613 } 00614 00620 virtual void swap (NumericVector<T> &v); 00621 00622 protected: 00623 00628 bool _is_closed; 00629 00634 bool _is_initialized; 00635 00639 ParallelType _type; 00640 }; 00641 00642 00643 /*----------------------- Inline functions ----------------------------------*/ 00644 00645 00646 00647 template <typename T> 00648 inline 00649 NumericVector<T>::NumericVector (const ParallelType ptype) : 00650 _is_closed(false), 00651 _is_initialized(false), 00652 _type(ptype) 00653 { 00654 } 00655 00656 00657 00658 template <typename T> 00659 inline 00660 NumericVector<T>::NumericVector (const numeric_index_type /*n*/, 00661 const ParallelType ptype) : 00662 _is_closed(false), 00663 _is_initialized(false), 00664 _type(ptype) 00665 { 00666 libmesh_error(); // Abstract base class! 00667 // init(n, n, false, ptype); 00668 } 00669 00670 00671 00672 template <typename T> 00673 inline 00674 NumericVector<T>::NumericVector (const numeric_index_type /*n*/, 00675 const numeric_index_type /*n_local*/, 00676 const ParallelType ptype) : 00677 _is_closed(false), 00678 _is_initialized(false), 00679 _type(ptype) 00680 { 00681 libmesh_error(); // Abstract base class! 00682 // init(n, n_local, false, ptype); 00683 } 00684 00685 00686 00687 template <typename T> 00688 inline 00689 NumericVector<T>::NumericVector (const numeric_index_type /*n*/, 00690 const numeric_index_type /*n_local*/, 00691 const std::vector<numeric_index_type>& /*ghost*/, 00692 const ParallelType ptype) : 00693 _is_closed(false), 00694 _is_initialized(false), 00695 _type(ptype) 00696 { 00697 libmesh_error(); // Abstract base class! 00698 // init(n, n_local, ghost, false, ptype); 00699 } 00700 00701 00702 00703 template <typename T> 00704 inline 00705 NumericVector<T>::~NumericVector () 00706 { 00707 clear (); 00708 } 00709 00710 00711 00712 // These should be pure virtual, not bugs waiting to happen - RHS 00713 /* 00714 template <typename T> 00715 inline 00716 NumericVector<T> & NumericVector<T>::operator= (const T) 00717 { 00718 // libmesh_error(); 00719 00720 return *this; 00721 } 00722 00723 00724 00725 template <typename T> 00726 inline 00727 NumericVector<T> & NumericVector<T>::operator= (const NumericVector<T>&) 00728 { 00729 // libmesh_error(); 00730 00731 return *this; 00732 } 00733 00734 00735 00736 template <typename T> 00737 inline 00738 NumericVector<T> & NumericVector<T>::operator= (const std::vector<T>&) 00739 { 00740 // libmesh_error(); 00741 00742 return *this; 00743 } 00744 */ 00745 00746 00747 00748 template <typename T> 00749 inline 00750 void NumericVector<T>::clear () 00751 { 00752 _is_closed = false; 00753 _is_initialized = false; 00754 } 00755 00756 00757 00758 template <typename T> 00759 inline 00760 void NumericVector<T>::get(const std::vector<numeric_index_type>& index, std::vector<T>& values) const 00761 { 00762 const std::size_t num = index.size(); 00763 values.resize(num); 00764 for(std::size_t i=0; i<num; i++) 00765 { 00766 values[i] = (*this)(index[i]); 00767 } 00768 } 00769 00770 00771 00772 // Full specialization of the print() member for complex 00773 // variables. This must precede the non-specialized 00774 // version, at least according to icc v7.1 00775 template <> 00776 inline 00777 void NumericVector<Complex>::print(std::ostream& os) const 00778 { 00779 libmesh_assert (this->initialized()); 00780 os << "Size\tglobal = " << this->size() 00781 << "\t\tlocal = " << this->local_size() << std::endl; 00782 00783 // std::complex<>::operator<<() is defined, but use this form 00784 os << "#\tReal part\t\tImaginary part" << std::endl; 00785 for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++) 00786 os << i << "\t" 00787 << (*this)(i).real() << "\t\t" 00788 << (*this)(i).imag() << std::endl; 00789 } 00790 00791 00792 00793 template <typename T> 00794 inline 00795 void NumericVector<T>::print(std::ostream& os) const 00796 { 00797 libmesh_assert (this->initialized()); 00798 os << "Size\tglobal = " << this->size() 00799 << "\t\tlocal = " << this->local_size() << std::endl; 00800 00801 os << "#\tValue" << std::endl; 00802 for (numeric_index_type i=this->first_local_index(); i<this->last_local_index(); i++) 00803 os << i << "\t" << (*this)(i) << std::endl; 00804 } 00805 00806 00807 00808 template <> 00809 inline 00810 void NumericVector<Complex>::print_global(std::ostream& os) const 00811 { 00812 libmesh_assert (this->initialized()); 00813 00814 std::vector<Complex> v(this->size()); 00815 this->localize(v); 00816 00817 // Right now we only want one copy of the output 00818 if (libMesh::processor_id()) 00819 return; 00820 00821 os << "Size\tglobal = " << this->size() << std::endl; 00822 os << "#\tReal part\t\tImaginary part" << std::endl; 00823 for (numeric_index_type i=0; i!=v.size(); i++) 00824 os << i << "\t" 00825 << v[i].real() << "\t\t" 00826 << v[i].imag() << std::endl; 00827 } 00828 00829 00830 template <typename T> 00831 inline 00832 void NumericVector<T>::print_global(std::ostream& os) const 00833 { 00834 libmesh_assert (this->initialized()); 00835 00836 std::vector<T> v(this->size()); 00837 this->localize(v); 00838 00839 // Right now we only want one copy of the output 00840 if (libMesh::processor_id()) 00841 return; 00842 00843 os << "Size\tglobal = " << this->size() << std::endl; 00844 os << "#\tValue" << std::endl; 00845 for (numeric_index_type i=0; i!=v.size(); i++) 00846 os << i << "\t" << v[i] << std::endl; 00847 } 00848 00849 00850 00851 template <typename T> 00852 inline 00853 void NumericVector<T>::swap (NumericVector<T> &v) 00854 { 00855 std::swap(_is_closed, v._is_closed); 00856 std::swap(_is_initialized, v._is_initialized); 00857 std::swap(_type, v._type); 00858 } 00859 00860 00861 } // namespace libMesh 00862 00863 00864 #endif // LIBMESH_NUMERIC_VECTOR_H
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:48 UTC
Hosted By: