Thyra Version of the Day
Loading...
Searching...
No Matches
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
21namespace Thyra {
22
56template<class Scalar>
59 , public Teuchos::VerboseObject<ParameterDrivenMultiVectorInput<Scalar> >
60{
61public:
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
205private:
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
234template<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
253template<class Scalar>
254inline
255const std::string&
257{
258 return fileNameBase_;
259}
260
261template<class Scalar>
262inline
265{
266 return explicitArray_;
267}
268
269template<class Scalar>
270inline
271Scalar
273{
274 return scaleBy_;
275}
276
277// //////////////////////////////////////////
278// Implementations
279
280namespace PDMVIUtilityPack {
281
282template<class Scalar>
283void copy(
284 const Teuchos::Array<Scalar> &array
286 )
287{
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
301template<class Scalar>
302const std::string
303ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_name_ = "File Name Base";
304template<class Scalar>
305const std::string
306ParameterDrivenMultiVectorInput<Scalar>::FileNameBase_default_ = "";
307
308template<class Scalar>
309const std::string
310ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_name_ = "Explicit Array";
311template<class Scalar>
312const std::string
313ParameterDrivenMultiVectorInput<Scalar>::ExplicitArray_default_ = "{}";
314
315template<class Scalar>
316const std::string
317ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_name_ = "Scale By";
318template<class Scalar>
319const double
320ParameterDrivenMultiVectorInput<Scalar>::ScaleBy_default_ = 1.0;
321
322template<class Scalar>
323const std::string
324ParameterDrivenMultiVectorInput<Scalar>::AddScalar_name_ = "Add Scalar";
325template<class Scalar>
326const double
327ParameterDrivenMultiVectorInput<Scalar>::AddScalar_default_ = 0.0;
328
329// Constructors/Initializers
330
331template<class Scalar>
333 :fileNameBase_(FileNameBase_default_),
334 scaleBy_(ScaleBy_default_),
335 addScalar_(AddScalar_default_)
336{}
337
338// Overridden from ParameterListAcceptor
339
340template<class Scalar>
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_ );
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
362template<class Scalar>
368
369template<class Scalar>
372{
374 _paramList = paramList_;
375 paramList_ = Teuchos::null;
376 return _paramList;
377}
378
379template<class Scalar>
382{
383 return paramList_;
384}
385
386template<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
424template<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!
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
479template<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
495template<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
size_type size() const
T * get() const
Create an explicit mutable (non-const) view of a VectorBase object.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
Interface for a collection of column vectors called a multi-vector.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
Abstract strategy interface for reading and writing (multi)vector objects to and from files.
Concrete utility class that an ANA can use for reading in a (multi)vector as directed by a parameter ...
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
const Teuchos::Array< Scalar > & readinExplicitArray() const
Return the value of the parameter "Explicit Array" that was read in from the setParameterList() funct...
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
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.
STANDARD_CONST_COMPOSITION_MEMBERS(VectorSpaceBase< Scalar >, vecSpc)
Set the vector space used to create the (multi)vectors that are read in.
STANDARD_COMPOSITION_MEMBERS(MultiVectorFileIOBase< Scalar >, fileIO)
Set the MultiVectorFileIOBase object that will be used to read the vector from file(s).
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.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
const std::string & readinFileNameBase() const
Return the value of the parameter "File Name Base" that was read in from the setParameterList() funct...
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const &paramList)
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Scalar readinScaleBy() const
Return the value of the parameter "Explicit Array" that was read in from the setParameterList() funct...
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...
Abstract interface for finite-dimensional dense vectors.
Abstract interface for objects that represent a space for vectors.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
TypeTo implicit_cast(const TypeFrom &t)
T_To & dyn_cast(T_From &from)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)