Thyra_ParameterDrivenMultiVectorInput.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP
00030 #define THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP
00031 
00032 #include "Thyra_MultiVectorFileIOBase.hpp"
00033 #include "Teuchos_ParameterListAcceptor.hpp"
00034 #include "Teuchos_VerboseObject.hpp"
00035 #include "Teuchos_StandardCompositionMacros.hpp"
00036 #include "Teuchos_implicit_cast.hpp"
00037 
00038 namespace Thyra {
00039 
00073 template<class Scalar>
00074 class ParameterDrivenMultiVectorInput
00075   : public Teuchos::ParameterListAcceptor
00076   , public Teuchos::VerboseObject<ParameterDrivenMultiVectorInput<Scalar> >
00077 {
00078 public:
00079 
00082 
00084   ParameterDrivenMultiVectorInput();
00085 
00089   STANDARD_CONST_COMPOSITION_MEMBERS( VectorSpaceBase<Scalar>, vecSpc );
00090 
00094   STANDARD_COMPOSITION_MEMBERS( MultiVectorFileIOBase<Scalar>, fileIO );
00095 
00097 
00100 
00102   void setParameterList(Teuchos::RCP<Teuchos::ParameterList> const& paramList);
00104   Teuchos::RCP<Teuchos::ParameterList> getParameterList();
00106   Teuchos::RCP<Teuchos::ParameterList> unsetParameterList();
00108   Teuchos::RCP<const Teuchos::ParameterList> getParameterList() const;
00110   Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const;
00111 
00113 
00116 
00120   const std::string& readinFileNameBase() const;
00121 
00125   const Teuchos::Array<Scalar>& readinExplicitArray() const;
00126 
00130   Scalar readinScaleBy() const;
00131 
00133 
00136   
00158   bool readMultiVector(
00159     const std::string                 &mvName
00160     ,Thyra::MultiVectorBase<Scalar>   *mv
00161     ) const;
00162   
00203   bool readVector(
00204     const std::string &vName
00205     ,Teuchos::RCP<Thyra::VectorBase<Scalar> > *v
00206     ) const;
00207   
00217   Teuchos::RCP<Thyra::VectorBase<Scalar> >
00218   readVector( const std::string &vName ) const;
00219   
00221 
00222 private:
00223 
00224   mutable Teuchos::RCP<const Teuchos::ParameterList> validParamList_;
00225   Teuchos::RCP<Teuchos::ParameterList> paramList_;
00226 
00227   std::string fileNameBase_;
00228   Teuchos::Array<Scalar> explicitArray_;
00229   Scalar scaleBy_;
00230   Scalar addScalar_;
00231 
00232   static const std::string FileNameBase_name_;
00233   static const std::string FileNameBase_default_;
00234 
00235   static const std::string ExplicitArray_name_;
00236   static const std::string ExplicitArray_default_;
00237 
00238   static const std::string ScaleBy_name_;
00239   static const double ScaleBy_default_;
00240 
00241   static const std::string AddScalar_name_;
00242   static const double AddScalar_default_;
00243 
00244 };
00245 
00246 
00251 template<class Scalar>
00252 RCP<const VectorBase<Scalar> >
00253 readVectorOverride(
00254   const ParameterDrivenMultiVectorInput<Scalar> &pdmvi,
00255   const std::string &vName,
00256   const RCP<const VectorBase<Scalar> > &defaultVector
00257   )
00258 {
00259   RCP<const VectorBase<Scalar> >
00260     vector = pdmvi.readVector(vName);
00261   if (!is_null(vector))
00262     return vector;
00263   return defaultVector;
00264 }
00265 
00266 
00267 // //////////////////////////////////////////
00268 // Inline functions
00269 
00270 template<class Scalar>
00271 inline
00272 const std::string&
00273 ParameterDrivenMultiVectorInput<Scalar>::readinFileNameBase() const
00274 {
00275   return fileNameBase_;
00276 }
00277 
00278 template<class Scalar>
00279 inline
00280 const Teuchos::Array<Scalar>&
00281 ParameterDrivenMultiVectorInput<Scalar>::readinExplicitArray() const
00282 {
00283   return explicitArray_;
00284 }
00285 
00286 template<class Scalar>
00287 inline
00288 Scalar
00289 ParameterDrivenMultiVectorInput<Scalar>::readinScaleBy() const
00290 {
00291   return scaleBy_;
00292 }
00293 
00294 // //////////////////////////////////////////
00295 // Implementations
00296 
00297 namespace PDMVIUtilityPack {
00298 
00299 template<class Scalar>
00300 void copy(
00301   const Teuchos::Array<Scalar>  &array
00302   ,VectorBase<Scalar>           *vec
00303   )
00304 {
00305   using Teuchos::implicit_cast;
00306   TEST_FOR_EXCEPT(vec==0);
00307   DetachedVectorView<Scalar> dVec(*vec);
00308   TEST_FOR_EXCEPT(implicit_cast<int>(dVec.subDim())!=implicit_cast<int>(array.size())); // ToDo: Give a very good error message!
00309   for( Index i = 0; i < dVec.subDim(); ++i ) {
00310     dVec[i] = array[i];
00311   }
00312 }
00313 
00314 } // namespace PDMVIUtilityPack
00315 
00316 // Static data members
00317 
00318 template<class Scalar>
00319 const std::string
00320 ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_name_ = "File Name Base";
00321 template<class Scalar>
00322 const std::string
00323 ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_default_ = "";
00324 
00325 template<class Scalar>
00326 const std::string
00327 ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_name_ = "Explicit Array";
00328 template<class Scalar>
00329 const std::string
00330 ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_default_ = "{}";
00331 
00332 template<class Scalar>
00333 const std::string
00334 ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_name_ = "Scale By";
00335 template<class Scalar>
00336 const double
00337 ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_default_ = 1.0;
00338 
00339 template<class Scalar>
00340 const std::string
00341 ParameterDrivenMultiVectorInput<Scalar>::AddScalar_name_ = "Add Scalar";
00342 template<class Scalar>
00343 const double
00344 ParameterDrivenMultiVectorInput<Scalar>::AddScalar_default_ = 0.0;
00345 
00346 // Constructors/Initializers
00347 
00348 template<class Scalar>
00349 ParameterDrivenMultiVectorInput<Scalar>::ParameterDrivenMultiVectorInput()
00350   :fileNameBase_(FileNameBase_default_),
00351    scaleBy_(ScaleBy_default_),
00352    addScalar_(AddScalar_default_)
00353 {}
00354 
00355 // Overridden from ParameterListAcceptor
00356 
00357 template<class Scalar>
00358 void ParameterDrivenMultiVectorInput<Scalar>::setParameterList(
00359   Teuchos::RCP<Teuchos::ParameterList> const& paramList
00360   )
00361 {
00362   TEST_FOR_EXCEPT(0==paramList.get());
00363   paramList->validateParameters(*getValidParameters());
00364   paramList_ = paramList;
00365   fileNameBase_ = paramList_->get(
00366     FileNameBase_name_,FileNameBase_default_ );
00367   explicitArray_ = Teuchos::getArrayFromStringParameter<Scalar>(
00368     *paramList_,ExplicitArray_name_
00369     ,-1     // An array of any size will do here
00370     ,false  // The parameter does not need to exist
00371     );
00372   scaleBy_ = paramList_->get(ScaleBy_name_,ScaleBy_default_);
00373   addScalar_ = paramList_->get(AddScalar_name_,AddScalar_default_);
00374 #ifdef TEUCHOS_DEBUG
00375   paramList_->validateParameters(*getValidParameters(),0);
00376 #endif // TEUCHOS_DEBUG
00377 }
00378 
00379 template<class Scalar>
00380 Teuchos::RCP<Teuchos::ParameterList>
00381 ParameterDrivenMultiVectorInput<Scalar>::getParameterList()
00382 {
00383   return paramList_;
00384 }
00385 
00386 template<class Scalar>
00387 Teuchos::RCP<Teuchos::ParameterList>
00388 ParameterDrivenMultiVectorInput<Scalar>::unsetParameterList()
00389 {
00390   Teuchos::RCP<Teuchos::ParameterList>
00391     _paramList = paramList_;
00392   paramList_ = Teuchos::null;
00393   return _paramList;
00394 }
00395 
00396 template<class Scalar>
00397 Teuchos::RCP<const Teuchos::ParameterList>
00398 ParameterDrivenMultiVectorInput<Scalar>::getParameterList() const
00399 {
00400   return paramList_;
00401 }
00402 
00403 template<class Scalar>
00404 Teuchos::RCP<const Teuchos::ParameterList>
00405 ParameterDrivenMultiVectorInput<Scalar>::getValidParameters() const
00406 {
00407   if(!validParamList_.get()) {
00408     Teuchos::RCP<Teuchos::ParameterList>
00409       pl = Teuchos::rcp(new Teuchos::ParameterList);
00410     pl->set(
00411       FileNameBase_name_,FileNameBase_default_
00412       ,"Base-name of file(s) that will be used to read in the vector.\n"
00413       "If this parameter is empty \"\", no file(s) will be read.\n"
00414       "Note that a MultiVectorFileIOBase object and a VectorSpaceBase object\n"
00415       "must be set internally for this to work."
00416       );
00417     pl->set(
00418       ExplicitArray_name_,ExplicitArray_default_
00419       ,"The vector specified explicitly as a string interpreted as a Teuchos::Array\n"
00420       "object.  If this array is set, it will override the vector specified\n"
00421       "by the above \"" + FileNameBase_name_ + "\" parameter.\n"
00422       "Note that a VectorSpaceBase object\n"
00423       "must be set internally for this to work."
00424       );
00425     pl->set(
00426       ScaleBy_name_,ScaleBy_default_,
00427       "A factor by which the read in vector will be scaled by."
00428       );
00429     pl->set(
00430       AddScalar_name_, AddScalar_default_,
00431       "A scalar that will added to the read in vector after it\n"
00432       "optionally scaled."
00433       );
00434     validParamList_ = pl;
00435   }
00436   return validParamList_;
00437 }
00438 
00439 // (Multi)Vector Readers
00440 
00441 template<class Scalar>
00442 bool ParameterDrivenMultiVectorInput<Scalar>::readMultiVector(
00443   const std::string                 &mvName
00444   ,Thyra::MultiVectorBase<Scalar>   *mv
00445   ) const
00446 {
00447   using Teuchos::implicit_cast;
00448   TEST_FOR_EXCEPT(0==mv);
00449   typedef Teuchos::ScalarTraits<Scalar> ST;
00450   const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
00451   Teuchos::RCP<Teuchos::FancyOStream>
00452     out = this->getOStream();
00453   const bool trace = ( verbLevel >= implicit_cast<int>(Teuchos::VERB_LOW) );
00454   Teuchos::OSTab tab(out);
00455   bool vectorWasRead = false;
00456   if(fileNameBase_.length()) {
00457     if( out.get() && trace )
00458       *out << "\nReading \"" << mvName << "\" from the file(s) with base name \""
00459            << fileNameBase_ << "\" ...\n";
00460     fileIO().readMultiVectorFromFile(fileNameBase_,mv);
00461     vectorWasRead = true;
00462   }
00463   if(explicitArray_.size()) {
00464     if( implicit_cast<Index>(explicitArray_.size()) != vecSpc().dim() ) {
00465       // Call back to throw an exception with a better erro message!
00466       Teuchos::getArrayFromStringParameter<Scalar>(
00467         *paramList_,ExplicitArray_name_,vecSpc().dim(),false);
00468       TEST_FOR_EXCEPT(!"Should never get here!");
00469     }
00470     if( out.get() && trace )
00471       *out << "\nSetting \"" << mvName << "\" directly from the parameter array "
00472            << explicitArray_ << " ...\n";
00473     TEST_FOR_EXCEPTION(
00474       mv->domain()->dim()!=implicit_cast<Index>(1), std::logic_error
00475       ,"Error! We can not handle reading in multi-vectors directly from"
00476       " the parameter list yet!"
00477       );
00478     PDMVIUtilityPack::copy(explicitArray_,&*mv->col(0));
00479     // ToDo: Find a way to read a matrix from a file (perhaps a nested
00480     // Array<Array<Scalar> > or something!)
00481     vectorWasRead = true;
00482   }
00483   if( scaleBy_ != ST::one() && vectorWasRead ) {
00484     if( out.get() && trace )
00485       *out << "\nScaling \"" << mvName << "\" by " << scaleBy_ << " ...\n";
00486     Vt_S(&*mv,scaleBy_);
00487   }
00488   if( addScalar_ != ST::zero() && vectorWasRead ) {
00489     if( out.get() && trace )
00490       *out << "\nAdding scalar " << addScalar_ << " to \"" << mvName << "\" ...\n";
00491     Vp_S(&*mv,addScalar_);
00492   }
00493   return vectorWasRead;
00494 }
00495 
00496 template<class Scalar>
00497 bool ParameterDrivenMultiVectorInput<Scalar>::readVector(
00498   const std::string                                   &vName
00499   ,Teuchos::RCP<Thyra::VectorBase<Scalar> >   *v
00500   ) const
00501 {
00502   TEST_FOR_EXCEPT(0==v);
00503   bool vectorWasRead = false;
00504   if( fileNameBase_.length() || explicitArray_.size() ) {
00505     if(!(*v).get())
00506       (*v) = createMember(this->vecSpc());
00507     vectorWasRead = this->readMultiVector(vName,&*(*v));
00508   }
00509   return vectorWasRead;
00510 }
00511 
00512 template<class Scalar>
00513 Teuchos::RCP<Thyra::VectorBase<Scalar> >
00514 ParameterDrivenMultiVectorInput<Scalar>::readVector(
00515   const std::string &vName
00516   ) const
00517 {
00518   Teuchos::RCP<Thyra::VectorBase<Scalar> > v;
00519   const bool vectorWasRead = readVector(vName,&v);
00520   if(!vectorWasRead)
00521     v = Teuchos::null;
00522   return v;
00523 }
00524 
00525 } // namespace Thyra
00526 
00527 #endif // THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP

Generated on Tue Oct 20 12:47:27 2009 for Thyra Package Browser (Single Doxygen Collection) by doxygen 1.4.7