laspack_vector.C
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 // C++ includes 00021 #include <algorithm> // for std::min 00022 #include <limits> 00023 00024 // Local Includes 00025 #include "libmesh/dense_subvector.h" 00026 #include "libmesh/dense_vector.h" 00027 #include "libmesh/laspack_vector.h" 00028 #include "libmesh/laspack_matrix.h" 00029 00030 00031 #ifdef LIBMESH_HAVE_LASPACK 00032 00033 namespace libMesh 00034 { 00035 00036 template <typename T> 00037 T LaspackVector<T>::sum () const 00038 { 00039 libmesh_assert (this->closed()); 00040 00041 T _sum = 0; 00042 00043 const numeric_index_type n = this->size(); 00044 00045 for (numeric_index_type i=0; i!=n; ++i) 00046 _sum += (*this)(i); 00047 00048 return _sum; 00049 } 00050 00051 00052 00053 template <typename T> 00054 Real LaspackVector<T>::l1_norm () const 00055 { 00056 libmesh_assert (this->closed()); 00057 00058 return static_cast<Real>(l1Norm_V(const_cast<QVector*>(&_vec))); 00059 } 00060 00061 00062 00063 template <typename T> 00064 Real LaspackVector<T>::l2_norm () const 00065 { 00066 libmesh_assert (this->closed()); 00067 00068 return static_cast<Real>(l2Norm_V(const_cast<QVector*>(&_vec))); 00069 } 00070 00071 00072 00073 template <typename T> 00074 Real LaspackVector<T>::linfty_norm () const 00075 { 00076 libmesh_assert (this->closed()); 00077 00078 return static_cast<Real>(MaxNorm_V(const_cast<QVector*>(&_vec))); 00079 } 00080 00081 00082 00083 template <typename T> 00084 NumericVector<T>& LaspackVector<T>::operator += (const NumericVector<T>& v) 00085 { 00086 libmesh_assert (this->closed()); 00087 00088 this->add(1., v); 00089 00090 return *this; 00091 } 00092 00093 00094 00095 00096 template <typename T> 00097 NumericVector<T>& LaspackVector<T>::operator -= (const NumericVector<T>& v) 00098 { 00099 libmesh_assert (this->closed()); 00100 00101 this->add(-1., v); 00102 00103 return *this; 00104 } 00105 00106 00107 00108 template <typename T> 00109 void LaspackVector<T>::reciprocal() 00110 { 00111 const numeric_index_type n = this->size(); 00112 00113 for (numeric_index_type i=0; i<n; i++) 00114 { 00115 T v = (*this)(i); 00116 00117 // Don't divide by zero! 00118 libmesh_assert_not_equal_to (v, T(0)); 00119 00120 this->set(i, 1. / v); 00121 } 00122 } 00123 00124 00125 00126 00127 template <typename T> 00128 void LaspackVector<T>::add (const T v) 00129 { 00130 const numeric_index_type n = this->size(); 00131 00132 for (numeric_index_type i=0; i<n; i++) 00133 this->add (i, v); 00134 00135 #ifndef NDEBUG 00136 this->_is_closed = false; 00137 #endif 00138 } 00139 00140 00141 00142 00143 template <typename T> 00144 void LaspackVector<T>::add (const NumericVector<T>& v) 00145 { 00146 this->add (1., v); 00147 } 00148 00149 00150 00151 template <typename T> 00152 void LaspackVector<T>::add (const T a, const NumericVector<T>& v_in) 00153 { 00154 // Make sure the vector passed in is really a LaspackVector 00155 const LaspackVector* v = libmesh_cast_ptr<const LaspackVector*>(&v_in); 00156 00157 #ifndef NDEBUG 00158 const bool was_closed = this->_is_closed; 00159 #endif 00160 00161 libmesh_assert(v); 00162 libmesh_assert_equal_to (this->size(), v->size()); 00163 00164 for (numeric_index_type i=0; i<v->size(); i++) 00165 this->add (i, a*(*v)(i)); 00166 00167 #ifndef NDEBUG 00168 this->_is_closed = was_closed; 00169 #endif 00170 } 00171 00172 00173 00174 template <typename T> 00175 void LaspackVector<T>::add_vector (const std::vector<T>& v, 00176 const std::vector<numeric_index_type>& dof_indices) 00177 { 00178 libmesh_assert (!v.empty()); 00179 libmesh_assert_equal_to (v.size(), dof_indices.size()); 00180 00181 for (numeric_index_type i=0; i<v.size(); i++) 00182 this->add (dof_indices[i], v[i]); 00183 } 00184 00185 00186 00187 template <typename T> 00188 void LaspackVector<T>::add_vector (const NumericVector<T>& V, 00189 const std::vector<numeric_index_type>& dof_indices) 00190 { 00191 libmesh_assert_equal_to (V.size(), dof_indices.size()); 00192 00193 for (numeric_index_type i=0; i<V.size(); i++) 00194 this->add (dof_indices[i], V(i)); 00195 } 00196 00197 00198 00199 template <typename T> 00200 void LaspackVector<T>::add_vector (const DenseVector<T>& V, 00201 const std::vector<numeric_index_type>& dof_indices) 00202 { 00203 libmesh_assert_equal_to (V.size(), dof_indices.size()); 00204 00205 for (unsigned int i=0; i<V.size(); i++) 00206 this->add (dof_indices[i], V(i)); 00207 } 00208 00209 00210 00211 template <typename T> 00212 void LaspackVector<T>::insert (const std::vector<T>& v, 00213 const std::vector<numeric_index_type>& dof_indices) 00214 { 00215 libmesh_assert (!v.empty()); 00216 libmesh_assert_equal_to (v.size(), dof_indices.size()); 00217 00218 for (numeric_index_type i=0; i<v.size(); i++) 00219 this->set (dof_indices[i], v[i]); 00220 } 00221 00222 00223 00224 template <typename T> 00225 void LaspackVector<T>::insert (const NumericVector<T>& V, 00226 const std::vector<numeric_index_type>& dof_indices) 00227 { 00228 libmesh_assert_equal_to (V.size(), dof_indices.size()); 00229 00230 for (numeric_index_type i=0; i<V.size(); i++) 00231 this->set (dof_indices[i], V(i)); 00232 } 00233 00234 00235 00236 template <typename T> 00237 void LaspackVector<T>::insert (const DenseVector<T>& V, 00238 const std::vector<numeric_index_type>& dof_indices) 00239 { 00240 libmesh_assert_equal_to (V.size(), dof_indices.size()); 00241 00242 for (unsigned int i=0; i<V.size(); i++) 00243 this->set (dof_indices[i], V(i)); 00244 } 00245 00246 00247 00248 template <typename T> 00249 void LaspackVector<T>::insert (const DenseSubVector<T>& V, 00250 const std::vector<numeric_index_type>& dof_indices) 00251 { 00252 libmesh_assert_equal_to (V.size(), dof_indices.size()); 00253 00254 for (unsigned int i=0; i<V.size(); i++) 00255 this->set (dof_indices[i], V(i)); 00256 } 00257 00258 00259 00260 template <typename T> 00261 void LaspackVector<T>::add_vector (const NumericVector<T> &vec_in, 00262 const SparseMatrix<T> &mat_in) 00263 { 00264 // Make sure the data passed in are really in Laspack types 00265 const LaspackVector<T>* vec = libmesh_cast_ptr<const LaspackVector<T>*>(&vec_in); 00266 const LaspackMatrix<T>* mat = libmesh_cast_ptr<const LaspackMatrix<T>*>(&mat_in); 00267 00268 libmesh_assert(vec); 00269 libmesh_assert(mat); 00270 00271 // += mat*vec 00272 AddAsgn_VV (&_vec, Mul_QV(const_cast<QMatrix*>(&mat->_QMat), 00273 const_cast<QVector*>(&vec->_vec))); 00274 } 00275 00276 00277 template <typename T> 00278 void LaspackVector<T>::add_vector_transpose (const NumericVector<T> &, 00279 const SparseMatrix<T> &) 00280 { 00281 libmesh_not_implemented(); 00282 } 00283 00284 00285 00286 template <typename T> 00287 void LaspackVector<T>::scale (const T factor) 00288 { 00289 libmesh_assert (this->initialized()); 00290 00291 Asgn_VV(&_vec, Mul_SV (factor, &_vec)); 00292 } 00293 00294 template <typename T> 00295 void LaspackVector<T>::abs() 00296 { 00297 libmesh_assert (this->initialized()); 00298 00299 const numeric_index_type n = this->size(); 00300 00301 for (numeric_index_type i=0; i!=n; ++i) 00302 this->set(i,std::abs((*this)(i))); 00303 } 00304 00305 template <typename T> 00306 T LaspackVector<T>::dot (const NumericVector<T>& V) const 00307 { 00308 libmesh_assert (this->initialized()); 00309 00310 // Make sure the NumericVector passed in is really a LaspackVector 00311 const LaspackVector<T>* v = libmesh_cast_ptr<const LaspackVector<T>*>(&V); 00312 libmesh_assert(v); 00313 00314 return Mul_VV (const_cast<QVector*>(&(this->_vec)), 00315 const_cast<QVector*>(&(v->_vec))); 00316 } 00317 00318 00319 00320 template <typename T> 00321 NumericVector<T>& 00322 LaspackVector<T>::operator = (const T s) 00323 { 00324 libmesh_assert (this->initialized()); 00325 libmesh_assert (this->closed()); 00326 00327 V_SetAllCmp (&_vec, s); 00328 00329 return *this; 00330 } 00331 00332 00333 00334 template <typename T> 00335 NumericVector<T>& 00336 LaspackVector<T>::operator = (const NumericVector<T>& v_in) 00337 { 00338 // Make sure the NumericVector passed in is really a LaspackVector 00339 const LaspackVector<T>* v = 00340 libmesh_cast_ptr<const LaspackVector<T>*>(&v_in); 00341 00342 libmesh_assert(v); 00343 00344 *this = *v; 00345 00346 return *this; 00347 } 00348 00349 00350 00351 template <typename T> 00352 LaspackVector<T>& 00353 LaspackVector<T>::operator = (const LaspackVector<T>& v) 00354 { 00355 libmesh_assert (this->initialized()); 00356 libmesh_assert (v.closed()); 00357 libmesh_assert_equal_to (this->size(), v.size()); 00358 00359 if (v.size() != 0) 00360 Asgn_VV (const_cast<QVector*>(&_vec), 00361 const_cast<QVector*>(&v._vec) 00362 ); 00363 00364 #ifndef NDEBUG 00365 this->_is_closed = true; 00366 #endif 00367 00368 return *this; 00369 } 00370 00371 00372 00373 template <typename T> 00374 NumericVector<T>& 00375 LaspackVector<T>::operator = (const std::vector<T>& v) 00376 { 00381 if (this->size() == v.size()) 00382 for (numeric_index_type i=0; i<v.size(); i++) 00383 this->set (i, v[i]); 00384 00385 else 00386 libmesh_error(); 00387 00388 return *this; 00389 } 00390 00391 00392 template <typename T> 00393 void LaspackVector<T>::localize (NumericVector<T>& v_local_in) const 00394 { 00395 // Make sure the NumericVector passed in is really a LaspackVector 00396 LaspackVector<T>* v_local = 00397 libmesh_cast_ptr<LaspackVector<T>*>(&v_local_in); 00398 00399 libmesh_assert(v_local); 00400 00401 *v_local = *this; 00402 } 00403 00404 00405 00406 template <typename T> 00407 void LaspackVector<T>::localize (NumericVector<T>& v_local_in, 00408 const std::vector<numeric_index_type>& libmesh_dbg_var(send_list)) const 00409 { 00410 // Make sure the NumericVector passed in is really a LaspackVector 00411 LaspackVector<T>* v_local = 00412 libmesh_cast_ptr<LaspackVector<T>*>(&v_local_in); 00413 00414 libmesh_assert(v_local); 00415 libmesh_assert_less_equal (send_list.size(), v_local->size()); 00416 00417 *v_local = *this; 00418 } 00419 00420 00421 00422 template <typename T> 00423 void LaspackVector<T>::localize (const numeric_index_type libmesh_dbg_var(first_local_idx), 00424 const numeric_index_type libmesh_dbg_var(last_local_idx), 00425 const std::vector<numeric_index_type>& libmesh_dbg_var(send_list)) 00426 { 00427 libmesh_assert_equal_to (first_local_idx, 0); 00428 libmesh_assert_equal_to (last_local_idx+1, this->size()); 00429 00430 libmesh_assert_less_equal (send_list.size(), this->size()); 00431 00432 #ifndef NDEBUG 00433 this->_is_closed = true; 00434 #endif 00435 } 00436 00437 00438 00439 template <typename T> 00440 void LaspackVector<T>::localize (std::vector<T>& v_local) const 00441 00442 { 00443 v_local.resize(this->size()); 00444 00445 for (numeric_index_type i=0; i<v_local.size(); i++) 00446 v_local[i] = (*this)(i); 00447 } 00448 00449 00450 00451 template <typename T> 00452 void LaspackVector<T>::localize_to_one (std::vector<T>& v_local, 00453 const processor_id_type libmesh_dbg_var(pid)) const 00454 { 00455 libmesh_assert_equal_to (pid, 0); 00456 00457 this->localize (v_local); 00458 } 00459 00460 00461 00462 template <typename T> 00463 void LaspackVector<T>::pointwise_mult (const NumericVector<T>& /*vec1*/, 00464 const NumericVector<T>& /*vec2*/) 00465 { 00466 libmesh_not_implemented(); 00467 } 00468 00469 00470 00471 template <typename T> 00472 Real LaspackVector<T>::max() const 00473 { 00474 libmesh_assert (this->initialized()); 00475 if (!this->size()) 00476 return -std::numeric_limits<Real>::max(); 00477 00478 Real the_max = libmesh_real((*this)(0)); 00479 00480 const numeric_index_type n = this->size(); 00481 00482 for (numeric_index_type i=1; i<n; i++) 00483 the_max = std::max (the_max, libmesh_real((*this)(i))); 00484 00485 return the_max; 00486 } 00487 00488 00489 00490 template <typename T> 00491 Real LaspackVector<T>::min () const 00492 { 00493 libmesh_assert (this->initialized()); 00494 if (!this->size()) 00495 return std::numeric_limits<Real>::max(); 00496 00497 Real the_min = libmesh_real((*this)(0)); 00498 00499 const numeric_index_type n = this->size(); 00500 00501 for (numeric_index_type i=1; i<n; i++) 00502 the_min = std::min (the_min, libmesh_real((*this)(i))); 00503 00504 return the_min; 00505 } 00506 00507 00508 //------------------------------------------------------------------ 00509 // Explicit instantiations 00510 template class LaspackVector<Number>; 00511 00512 } // namespace libMesh 00513 00514 00515 #endif // #ifdef LIBMESH_HAVE_LASPACK
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: