Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_ParameterDrivenMultiVectorInput.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP
11 #define THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP
12 
13 #include "Thyra_MultiVectorFileIOBase.hpp"
14 #include "Thyra_DetachedVectorView.hpp"
15 #include "Thyra_MultiVectorStdOps.hpp"
16 #include "Teuchos_ParameterListAcceptor.hpp"
17 #include "Teuchos_VerboseObject.hpp"
18 #include "Teuchos_StandardCompositionMacros.hpp"
19 #include "Teuchos_implicit_cast.hpp"
20 
21 namespace Thyra {
22 
56 template<class Scalar>
59  , public Teuchos::VerboseObject<ParameterDrivenMultiVectorInput<Scalar> >
60 {
61 public:
62 
65 
68 
73 
78 
80 
83 
94 
96 
99 
103  const std::string& readinFileNameBase() const;
104 
109 
113  Scalar readinScaleBy() const;
114 
116 
119 
141  bool readMultiVector(
142  const std::string &mvName
144  ) const;
145 
186  bool readVector(
187  const std::string &vName
189  ) const;
190 
201  readVector( const std::string &vName ) const;
202 
204 
205 private:
206 
207  mutable Teuchos::RCP<const Teuchos::ParameterList> validParamList_;
209 
210  std::string fileNameBase_;
211  Teuchos::Array<Scalar> explicitArray_;
212  Scalar scaleBy_;
213  Scalar addScalar_;
214 
215  static const std::string FileNameBase_name_;
216  static const std::string FileNameBase_default_;
217 
218  static const std::string ExplicitArray_name_;
219  static const std::string ExplicitArray_default_;
220 
221  static const std::string ScaleBy_name_;
222  static const double ScaleBy_default_;
223 
224  static const std::string AddScalar_name_;
225  static const double AddScalar_default_;
226 
227 };
228 
229 
234 template<class Scalar>
238  const std::string &vName,
239  const RCP<const VectorBase<Scalar> > &defaultVector
240  )
241 {
243  vector = pdmvi.readVector(vName);
244  if (!is_null(vector))
245  return vector;
246  return defaultVector;
247 }
248 
249 
250 // //////////////////////////////////////////
251 // Inline functions
252 
253 template<class Scalar>
254 inline
255 const std::string&
257 {
258  return fileNameBase_;
259 }
260 
261 template<class Scalar>
262 inline
265 {
266  return explicitArray_;
267 }
268 
269 template<class Scalar>
270 inline
271 Scalar
273 {
274  return scaleBy_;
275 }
276 
277 // //////////////////////////////////////////
278 // Implementations
279 
280 namespace PDMVIUtilityPack {
281 
282 template<class Scalar>
283 void copy(
284  const Teuchos::Array<Scalar> &array
285  ,VectorBase<Scalar> *vec
286  )
287 {
289  TEUCHOS_TEST_FOR_EXCEPT(vec==0);
290  DetachedVectorView<Scalar> dVec(*vec);
291  TEUCHOS_TEST_FOR_EXCEPT(implicit_cast<int>(dVec.subDim())!=implicit_cast<int>(array.size())); // ToDo: Give a very good error message!
292  for( Ordinal i = 0; i < dVec.subDim(); ++i ) {
293  dVec[i] = array[i];
294  }
295 }
296 
297 } // namespace PDMVIUtilityPack
298 
299 // Static data members
300 
301 template<class Scalar>
302 const std::string
303 ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_name_ = "File Name Base";
304 template<class Scalar>
305 const std::string
306 ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_default_ = "";
307 
308 template<class Scalar>
309 const std::string
310 ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_name_ = "Explicit Array";
311 template<class Scalar>
312 const std::string
313 ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_default_ = "{}";
314 
315 template<class Scalar>
316 const std::string
317 ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_name_ = "Scale By";
318 template<class Scalar>
319 const double
320 ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_default_ = 1.0;
321 
322 template<class Scalar>
323 const std::string
324 ParameterDrivenMultiVectorInput<Scalar>::AddScalar_name_ = "Add Scalar";
325 template<class Scalar>
326 const double
327 ParameterDrivenMultiVectorInput<Scalar>::AddScalar_default_ = 0.0;
328 
329 // Constructors/Initializers
330 
331 template<class Scalar>
333  :fileNameBase_(FileNameBase_default_),
334  scaleBy_(ScaleBy_default_),
335  addScalar_(AddScalar_default_)
336 {}
337 
338 // Overridden from ParameterListAcceptor
339 
340 template<class Scalar>
342  Teuchos::RCP<Teuchos::ParameterList> const& paramList
343  )
344 {
345  TEUCHOS_TEST_FOR_EXCEPT(0==paramList.get());
346  paramList->validateParameters(*getValidParameters());
347  paramList_ = paramList;
348  fileNameBase_ = paramList_->get(
349  FileNameBase_name_,FileNameBase_default_ );
350  explicitArray_ = Teuchos::getArrayFromStringParameter<Scalar>(
351  *paramList_,ExplicitArray_name_
352  ,-1 // An array of any size will do here
353  ,false // The parameter does not need to exist
354  );
355  scaleBy_ = paramList_->get(ScaleBy_name_,ScaleBy_default_);
356  addScalar_ = paramList_->get(AddScalar_name_,AddScalar_default_);
357 #ifdef TEUCHOS_DEBUG
358  paramList_->validateParameters(*getValidParameters(),0);
359 #endif // TEUCHOS_DEBUG
360 }
361 
362 template<class Scalar>
365 {
366  return paramList_;
367 }
368 
369 template<class Scalar>
372 {
374  _paramList = paramList_;
375  paramList_ = Teuchos::null;
376  return _paramList;
377 }
378 
379 template<class Scalar>
382 {
383  return paramList_;
384 }
385 
386 template<class Scalar>
389 {
390  if(!validParamList_.get()) {
393  pl->set(
394  FileNameBase_name_,FileNameBase_default_
395  ,"Base-name of file(s) that will be used to read in the vector.\n"
396  "If this parameter is empty \"\", no file(s) will be read.\n"
397  "Note that a MultiVectorFileIOBase object and a VectorSpaceBase object\n"
398  "must be set internally for this to work."
399  );
400  pl->set(
401  ExplicitArray_name_,ExplicitArray_default_
402  ,"The vector specified explicitly as a string interpreted as a Teuchos::Array\n"
403  "object. If this array is set, it will override the vector specified\n"
404  "by the above \"" + FileNameBase_name_ + "\" parameter.\n"
405  "Note that a VectorSpaceBase object\n"
406  "must be set internally for this to work."
407  );
408  pl->set(
409  ScaleBy_name_,ScaleBy_default_,
410  "A factor by which the read in vector will be scaled by."
411  );
412  pl->set(
413  AddScalar_name_, AddScalar_default_,
414  "A scalar that will added to the read in vector after it\n"
415  "optionally scaled."
416  );
417  validParamList_ = pl;
418  }
419  return validParamList_;
420 }
421 
422 // (Multi)Vector Readers
423 
424 template<class Scalar>
426  const std::string &mvName
428  ) const
429 {
433  const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
435  out = this->getOStream();
436  const bool trace = ( verbLevel >= implicit_cast<int>(Teuchos::VERB_LOW) );
437  Teuchos::OSTab tab(out);
438  bool vectorWasRead = false;
439  if(fileNameBase_.length()) {
440  if( out.get() && trace )
441  *out << "\nReading \"" << mvName << "\" from the file(s) with base name \""
442  << fileNameBase_ << "\" ...\n";
443  fileIO().readMultiVectorFromFile(fileNameBase_,mv);
444  vectorWasRead = true;
445  }
446  if(explicitArray_.size()) {
447  if( implicit_cast<Ordinal>(explicitArray_.size()) != vecSpc().dim() ) {
448  // Call back to throw an exception with a better erro message!
449  Teuchos::getArrayFromStringParameter<Scalar>(
450  *paramList_,ExplicitArray_name_,vecSpc().dim(),false);
451  TEUCHOS_TEST_FOR_EXCEPT(!"Should never get here!");
452  }
453  if( out.get() && trace )
454  *out << "\nSetting \"" << mvName << "\" directly from the parameter array "
455  << explicitArray_ << " ...\n";
457  mv->domain()->dim()!=implicit_cast<Ordinal>(1), std::logic_error
458  ,"Error! We can not handle reading in multi-vectors directly from"
459  " the parameter list yet!"
460  );
461  PDMVIUtilityPack::copy(explicitArray_,&*mv->col(0));
462  // ToDo: Find a way to read a matrix from a file (perhaps a nested
463  // Array<Array<Scalar> > or something!)
464  vectorWasRead = true;
465  }
466  if( scaleBy_ != ST::one() && vectorWasRead ) {
467  if( out.get() && trace )
468  *out << "\nScaling \"" << mvName << "\" by " << scaleBy_ << " ...\n";
469  Vt_S(Teuchos::ptr(mv), scaleBy_);
470  }
471  if( addScalar_ != ST::zero() && vectorWasRead ) {
472  if( out.get() && trace )
473  *out << "\nAdding scalar " << addScalar_ << " to \"" << mvName << "\" ...\n";
474  Vp_S(Teuchos::ptr(mv), addScalar_);
475  }
476  return vectorWasRead;
477 }
478 
479 template<class Scalar>
481  const std::string &vName
483  ) const
484 {
486  bool vectorWasRead = false;
487  if( fileNameBase_.length() || explicitArray_.size() ) {
488  if(!(*v).get())
489  (*v) = createMember(this->vecSpc());
490  vectorWasRead = this->readMultiVector(vName, &*(*v));
491  }
492  return vectorWasRead;
493 }
494 
495 template<class Scalar>
498  const std::string &vName
499  ) const
500 {
502  const bool vectorWasRead = readVector(vName,&v);
503  if(!vectorWasRead)
504  v = Teuchos::null;
505  return v;
506 }
507 
508 } // namespace Thyra
509 
510 #endif // THYRA_PARAMETER_DRIVEN_MULTI_VECTOR_INPUT_HPP
const Teuchos::Array< Scalar > & readinExplicitArray() const
Return the value of the parameter &quot;Explicit Array&quot; that was read in from the setParameterList() funct...
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
bool is_null(const boost::shared_ptr< T > &p)
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
RCP< const VectorBase< Scalar > > readVectorOverride(const ParameterDrivenMultiVectorInput< Scalar > &pdmvi, const std::string &vName, const RCP< const VectorBase< Scalar > > &defaultVector)
Read a vector and override if one is read.
T * get() const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Abstract interface for objects that represent a space for vectors.
bool readVector(const std::string &vName, Teuchos::RCP< Thyra::VectorBase< Scalar > > *v) const
Read a Vector as directed by the set parameter sublist, allocating the Vector object if it has not al...
Concrete utility class that an ANA can use for reading in a (multi)vector as directed by a parameter ...
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const &paramList)
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Interface for a collection of column vectors called a multi-vector.
Create an explicit mutable (non-const) view of a VectorBase object.
STANDARD_CONST_COMPOSITION_MEMBERS(VectorSpaceBase< Scalar >, vecSpc)
Set the vector space used to create the (multi)vectors that are read in.
const std::string & readinFileNameBase() const
Return the value of the parameter &quot;File Name Base&quot; that was read in from the setParameterList() funct...
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
Abstract strategy interface for reading and writing (multi)vector objects to and from files...
TypeTo implicit_cast(const TypeFrom &t)
Abstract interface for finite-dimensional dense vectors.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Scalar readinScaleBy() const
Return the value of the parameter &quot;Explicit Array&quot; that was read in from the setParameterList() funct...
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
size_type size() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
bool readMultiVector(const std::string &mvName, Thyra::MultiVectorBase< Scalar > *mv) const
Read a MultiVector that has already been allocated, as directed by the set parameter sublist...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
STANDARD_COMPOSITION_MEMBERS(MultiVectorFileIOBase< Scalar >, fileIO)
Set the MultiVectorFileIOBase object that will be used to read the vector from file(s).