Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_BlockedTriangularLinearOpWithSolveFactory.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Thyra_BlockedTriangularLinearOpWithSolveFactory_hpp
10 #define Thyra_BlockedTriangularLinearOpWithSolveFactory_hpp
11 
12 #include "Thyra_LinearOpWithSolveBase.hpp"
13 #include "Thyra_DefaultBlockedLinearOp.hpp"
14 #include "Thyra_DefaultBlockedTriangularLinearOpWithSolve.hpp"
15 #include "Thyra_LinearOpSourceBase.hpp"
16 #include "Teuchos_Array.hpp"
17 
18 namespace Thyra {
19 
60 template <class Scalar>
62  : virtual public LinearOpWithSolveFactoryBase<Scalar> {
63  public:
66 
78  const Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > > &lowsf);
79 
91  const Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > > &lowsf);
92 
93  Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > > getUnderlyingLOWSF();
94 
95  Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > > getUnderlyingLOWSF()
96  const;
97 
99 
102 
103  std::string description() const;
104 
106 
110 
111  void setParameterList(RCP<ParameterList> const &paramList);
112  RCP<ParameterList> getNonconstParameterList();
113  RCP<ParameterList> unsetParameterList();
114  RCP<const ParameterList> getParameterList() const;
115  RCP<const ParameterList> getValidParameters() const;
116 
118 
121 
123  virtual bool acceptsPreconditionerFactory() const;
124 
126  virtual void setPreconditionerFactory(
127  const RCP<PreconditionerFactoryBase<Scalar> > &precFactory,
128  const std::string &precFactoryName);
129 
131  virtual RCP<PreconditionerFactoryBase<Scalar> > getPreconditionerFactory()
132  const;
133 
135  virtual void unsetPreconditionerFactory(
136  RCP<PreconditionerFactoryBase<Scalar> > *precFactory,
137  std::string *precFactoryName);
138 
139  virtual bool isCompatible(const LinearOpSourceBase<Scalar> &fwdOpSrc) const;
140 
141  virtual RCP<LinearOpWithSolveBase<Scalar> > createOp() const;
142 
143  virtual void initializeOp(
144  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
146  const ESupportSolveUse supportSolveUse) const;
147 
148  virtual void initializeAndReuseOp(
149  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
151 
152  virtual void uninitializeOp(
154  RCP<const LinearOpSourceBase<Scalar> > *fwdOpSrc,
155  RCP<const PreconditionerBase<Scalar> > *prec,
156  RCP<const LinearOpSourceBase<Scalar> > *approxFwdOpSrc,
157  ESupportSolveUse *supportSolveUse) const;
158 
159  virtual bool supportsPreconditionerInputType(
160  const EPreconditionerInputType precOpType) const;
161 
162  virtual void initializePreconditionedOp(
163  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
164  const RCP<const PreconditionerBase<Scalar> > &prec,
166  const ESupportSolveUse supportSolveUse) const;
167 
169  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
170  const RCP<const LinearOpSourceBase<Scalar> > &approxFwdOpSrc,
172  const ESupportSolveUse supportSolveUse) const;
173 
175 
176  protected:
179 
180  void informUpdatedVerbosityState() const;
181 
183 
184  private:
188 
189  Array<LOWSF_t> lowsf_;
190 
191  // Not defined and not to be called
193 };
194 
199 template <class Scalar>
200 RCP<BlockedTriangularLinearOpWithSolveFactory<Scalar> >
202  const Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > > &lowsf)
203 {
204  return Teuchos::rcp(
206 }
207 
212 template <class Scalar>
213 RCP<BlockedTriangularLinearOpWithSolveFactory<Scalar> >
215  const Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > > &lowsf)
216 {
217  return Teuchos::rcp(
219 }
220 
221 // Overridden from Constructors/Initializers/Accessors
222 
223 template <class Scalar>
226  const Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > > &lowsf)
227  : lowsf_(lowsf.size())
228 {
229  for (Ordinal i = 0; i < lowsf.size(); ++i) {
230 #ifdef TEUCHOS_DEBUG
231  TEUCHOS_TEST_FOR_EXCEPT(is_null(lowsf[i]));
232 #endif
233  lowsf_[i].initialize(lowsf[i]);
234  }
235 }
236 
237 template <class Scalar>
240  const Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > > &lowsf)
241  : lowsf_(lowsf.size())
242 {
243  for (Ordinal i = 0; i < lowsf.size(); ++i) {
244 #ifdef TEUCHOS_DEBUG
245  TEUCHOS_TEST_FOR_EXCEPT(is_null(lowsf[i]));
246 #endif
247  lowsf_[i].initialize(lowsf[i]);
248  }
249 }
250 
251 template <class Scalar>
252 Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > >
254 {
255  Array<RCP<LinearOpWithSolveFactoryBase<Scalar> > > lowsf(lowsf_.size());
256  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
257  lowsf[i] = lowsf_[i].getNonconstObj();
258  }
259  return lowsf;
260 }
261 
262 template <class Scalar>
263 Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > >
265 {
266  Array<RCP<const LinearOpWithSolveFactoryBase<Scalar> > > lowsf(lowsf_.size());
267  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
268  lowsf[i] = lowsf_[i].getConstObj();
269  }
270  return lowsf;
271 }
272 
273 // Overridden from Teuchos::Describable
274 
275 template <class Scalar>
277  const
278 {
279  std::ostringstream oss;
280  oss << this->Teuchos::Describable::description() << "{";
281  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
282  oss << "lowsf=";
283  if (!is_null(lowsf_[i].getConstObj()))
284  oss << lowsf_[i].getConstObj()->description();
285  else
286  oss << "NULL";
287  }
288  oss << "}";
289  return oss.str();
290 }
291 
292 // Overridden from ParameterListAcceptor
293 
294 // Note, we should probably do something smarter with the parameter lists
295 
296 template <class Scalar>
298  RCP<ParameterList> const &paramList)
299 {
300  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
301  lowsf_[i].getNonconstObj()->setParameterList(paramList);
302  }
303 }
304 
305 template <class Scalar>
306 RCP<ParameterList>
308 {
309  return lowsf_[0].getNonconstObj()->getNonconstParameterList();
310 }
311 
312 template <class Scalar>
313 RCP<ParameterList>
315 {
316  RCP<ParameterList> pl;
317  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
318  pl = lowsf_[i].getNonconstObj()->unsetParameterList();
319  }
320  return pl;
321 }
322 
323 template <class Scalar>
324 RCP<const ParameterList>
326 {
327  return lowsf_[0].getConstObj()->getParameterList();
328 }
329 
330 template <class Scalar>
331 RCP<const ParameterList>
333 {
334  return lowsf_[0].getConstObj()->getValidParameters();
335 }
336 
337 // Overridden from LinearOpWithSolveFactoyBase
338 
339 template <class Scalar>
341  Scalar>::acceptsPreconditionerFactory() const
342 {
343  return false;
344 }
345 
346 template <class Scalar>
349  const RCP<PreconditionerFactoryBase<Scalar> > & /* precFactory */,
350  const std::string & /* precFactoryName */
351  )
352 {
354  true, std::logic_error,
355  "Error, we don't support a preconditioner factory!");
356 }
357 
358 template <class Scalar>
359 RCP<PreconditionerFactoryBase<Scalar> >
361  const
362 {
363  return Teuchos::null;
364 }
365 
366 template <class Scalar>
368  Scalar>::unsetPreconditionerFactory(RCP<PreconditionerFactoryBase<Scalar> >
369  * /* precFactory */,
370  std::string * /* precFactoryName */
371 )
372 {
374  true, std::logic_error,
375  "Error, we don't support a preconditioner factory!");
376 }
377 
378 template <class Scalar>
380  const LinearOpSourceBase<Scalar> & /* fwdOpSrc */
381 ) const
382 {
385 }
386 
387 template <class Scalar>
388 RCP<LinearOpWithSolveBase<Scalar> >
390 {
391  return defaultBlockedTriangularLinearOpWithSolve<Scalar>();
392 }
393 
394 template <class Scalar>
396  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
398  const ESupportSolveUse /* supportSolveUse */
399 ) const
400 {
401  using Teuchos::dyn_cast;
402  using Teuchos::rcp_dynamic_cast;
403 
404 #ifdef TEUCHOS_DEBUG
405  TEUCHOS_TEST_FOR_EXCEPT(0 == Op);
406 #endif
407 
408  // Set the verbosity settings for the wrapped LOWSF object!
409  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
410  lowsf_[i].getConstObj()->setOStream(this->getOStream());
411  lowsf_[i].getConstObj()->setVerbLevel(this->getVerbLevel());
412  }
413 
414  // Get the block interface to get at the blocks
416  const RCP<const PBLOB> blo =
417  rcp_dynamic_cast<const PBLOB>(fwdOpSrc->getOp().assert_not_null());
418 
419  // Dynamic cast to get the DefaultBlockedTriangularLinearOpWithSolveBase
420  // interface that we will fill.
421 
423  DBTLOWS &btlows = dyn_cast<DBTLOWS>(*Op);
424 
425  // Determine if this is the first time through or if we have already
426  // initialized before. This will be needed to allow efficient reuse of the
427  // LOWSB objects for the diagonal blocks.
428  const bool firstTime = is_null(btlows.range());
429 
430  // If this is the first time through, we need to fill and create the block
431  // structure
432  if (firstTime)
433  btlows.beginBlockFill(blo->productRange(), blo->productDomain());
434 
435  const int N = blo->productRange()->numBlocks();
436  for (int k = 0; k < N; ++k) {
437  const RCP<const LinearOpBase<Scalar> > fwdOp_k =
438  blo->getBlock(k, k).assert_not_null();
439  if (firstTime) {
440  // This is the first time through so reate and initialize a new LOWSB
441  // object for each block
442  btlows.setNonconstLOWSBlock(
443  k, k, linearOpWithSolve<Scalar>(*lowsf_[k].getConstObj(), fwdOp_k));
444  }
445  else {
446  // This is not the first time through so we need to just reinitiallize
447  // the object that is already created. This allows us to efficiently
448  // reuse precreated structure and storage.
449  RCP<LinearOpWithSolveBase<Scalar> > invOp_k =
450  btlows.getNonconstLOWSBlock(k, k).assert_not_null();
451  Thyra::initializeOp<Scalar>(*lowsf_[k].getConstObj(), fwdOp_k,
452  invOp_k.ptr());
453  }
454  }
455 
456  // If this is the first time through, then we need to finalize the block
457  // structure.
458  if (firstTime) btlows.endBlockFill();
459 
460  // After the block structure has been setup, set the off-diagonal blocks.
461  // Note that this also sets the diagonal blocks but these are ignored since
462  // the LOWSB blocks created above override these.
463  btlows.setBlocks(blo);
464 
465  // Set the verbosity settings
466  btlows.setOStream(this->getOStream());
467  btlows.setVerbLevel(this->getVerbLevel());
468 }
469 
470 template <class Scalar>
472  const RCP<const LinearOpSourceBase<Scalar> > & /* fwdOpSrc */,
474 ) const
475 {
477 }
478 
479 template <class Scalar>
482  RCP<const LinearOpSourceBase<Scalar> > *fwdOpSrc,
483  RCP<const PreconditionerBase<Scalar> > *prec,
484  RCP<const LinearOpSourceBase<Scalar> > *approxFwdOpSrc,
485  ESupportSolveUse * /* supportSolveUse */
486 ) const
487 {
488  using Teuchos::dyn_cast;
489  using Teuchos::rcp_dynamic_cast;
490  using Teuchos::rcp_implicit_cast;
492  TEUCHOS_TEST_FOR_EXCEPT(0 == Op);
493  DBTLOWS &btlowsOp = dyn_cast<DBTLOWS>(*Op);
494  if (fwdOpSrc) {
495  const RCP<const LinearOpBase<Scalar> > fwdOp = btlowsOp.getBlocks();
496  if (!is_null(fwdOp))
497  *fwdOpSrc = defaultLinearOpSource<Scalar>(fwdOp);
498  else
499  *fwdOpSrc = Teuchos::null;
500  }
501  if (prec) *prec = Teuchos::null;
502  if (approxFwdOpSrc) *approxFwdOpSrc = Teuchos::null;
503 }
504 
505 template <class Scalar>
508  const EPreconditionerInputType /* precOpType */
509  ) const
510 {
511  // We don't support any external preconditioners!
512  return false;
513  // 20071006: rabartl: Note: We could support external preconditioners but it
514  // will take some work. We would have to extract out the individual
515  // preconditioners from each block. This would be pretty easy to do but I
516  // am not going to do this until we have to.
517 }
518 
519 template <class Scalar>
522  const RCP<const LinearOpSourceBase<Scalar> > & /* fwdOpSrc */,
523  const RCP<const PreconditionerBase<Scalar> > & /* prec */,
525  const ESupportSolveUse /* supportSolveUse */
526  ) const
527 {
529  true, std::logic_error,
530  "Error, we don't support an external preconditioner!");
531 }
532 
533 template <class Scalar>
536  const RCP<const LinearOpSourceBase<Scalar> > & /* fwdOpSrc */,
537  const RCP<const LinearOpSourceBase<Scalar> > & /* approxFwdOpSrc */,
539  const ESupportSolveUse /* supportSolveUse */
540  ) const
541 {
543  true, std::logic_error,
544  "Error, we don't support an external preconditioner!");
545 }
546 
547 // protected
548 
549 template <class Scalar>
551  Scalar>::informUpdatedVerbosityState() const
552 {
553  for (Ordinal i = 0; i < lowsf_.size(); ++i) {
554  lowsf_[i].getConstObj()->setVerbLevel(this->getVerbLevel());
555  lowsf_[i].getConstObj()->setOStream(this->getOStream());
556  }
557 }
558 
559 } // namespace Thyra
560 
561 #endif // Thyra_BlockedTriangularLinearOpWithSolveFactory_hpp
bool is_null(const boost::shared_ptr< T > &p)
virtual RCP< PreconditionerFactoryBase< Scalar > > getPreconditionerFactory() const
Returns null .
void informUpdatedVerbosityState() const
Overridden from Teuchos::VerboseObjectBase.
Teuchos::ConstNonconstObjectContainer< LinearOpWithSolveFactoryBase< Scalar > > LOWSF_t
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T_To & dyn_cast(T_From &from)
RCP< BlockedTriangularLinearOpWithSolveFactory< Scalar > > blockedTriangularLinearOpWithSolveFactory(const Array< RCP< const LinearOpWithSolveFactoryBase< Scalar > > > &lowsf)
Nonmember constructor.
virtual bool isCompatible(const LinearOpSourceBase< Scalar > &fwdOpSrc) const
virtual void uninitializeOp(LinearOpWithSolveBase< Scalar > *Op, RCP< const LinearOpSourceBase< Scalar > > *fwdOpSrc, RCP< const PreconditionerBase< Scalar > > *prec, RCP< const LinearOpSourceBase< Scalar > > *approxFwdOpSrc, ESupportSolveUse *supportSolveUse) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void initializeOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
Teuchos::Ordinal Ordinal
virtual RCP< LinearOpWithSolveBase< Scalar > > createOp() const
virtual void initializePreconditionedOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, const RCP< const PreconditionerBase< Scalar > > &prec, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
virtual std::string description() const
virtual void unsetPreconditionerFactory(RCP< PreconditionerFactoryBase< Scalar > > *precFactory, std::string *precFactoryName)
Throws exception.
virtual void setPreconditionerFactory(const RCP< PreconditionerFactoryBase< Scalar > > &precFactory, const std::string &precFactoryName)
Throws exception.
ESupportSolveUse
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
EPreconditionerInputType
virtual void initializeAndReuseOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, LinearOpWithSolveBase< Scalar > *Op) const
Array< RCP< LinearOpWithSolveFactoryBase< Scalar > > > getUnderlyingLOWSF()
virtual bool supportsPreconditionerInputType(const EPreconditionerInputType precOpType) const
virtual void initializeApproxPreconditionedOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, const RCP< const LinearOpSourceBase< Scalar > > &approxFwdOpSrc, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
RCP< BlockedTriangularLinearOpWithSolveFactory< Scalar > > blockedTriangularLinearOpWithSolveFactory(const Array< RCP< LinearOpWithSolveFactoryBase< Scalar > > > &lowsf)
Nonmember constructor.
Implicit subclass that takes a blocked triangular LOWB object and turns it into a LOWSB object...