Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Thyra_AdjointLinearOpWithSolveFactory.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_AdjointLinearOpWithSolveFactory_hpp
10 #define Thyra_AdjointLinearOpWithSolveFactory_hpp
11 
12 #include "Thyra_LinearOpWithSolveFactoryBase.hpp"
13 #include "Thyra_DefaultScaledAdjointLinearOp.hpp"
14 #include "Thyra_DefaultAdjointLinearOpWithSolve.hpp"
15 #include "Thyra_DefaultLinearOpSource.hpp"
18 
19 namespace Thyra {
20 
21 /** \brief Create a LinearOpWithSolveFactory for an adjoint linear op.
22  */
23 template<class Scalar>
25  : virtual public LinearOpWithSolveFactoryBase<Scalar>
26 {
27 public:
28 
29  /** @name Overridden from Constructors/Initializers/Accessors */
30  //@{
31 
32  /** \brief Construct to uninitialized. */
34 
35  /** \brief Initialize given a single non-const LOWSFB object.
36  *
37  * \param lowsf [in,persisting] The LOWSFB object that will be used to
38  * create the LOWSB object for the original system.
39  *
40  * <b>Preconditions:</b><ul>
41  * <li><tt>!is_null(lowsf)</tt>
42  * </ul>
43  *
44  */
45  void nonconstInitialize(
46  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &lowsf
47  );
48 
49 
50  /** \brief Initialize given a single const LOWSFB object.
51  *
52  * \param lowsf [in,persisting] The LOWSFB object that will be used to
53  * create the LOWSB object for the original system.
54  *
55  * <b>Preconditions:</b><ul>
56  * <li><tt>!is_null(lowsf)</tt>
57  * </ul>
58  *
59  */
60  void initialize(
61  const RCP<const LinearOpWithSolveFactoryBase<Scalar> > &lowsf
62  );
63 
64  RCP<LinearOpWithSolveFactoryBase<Scalar> > getUnderlyingLOWSF();
65 
66  RCP<const LinearOpWithSolveFactoryBase<Scalar> > getUnderlyingLOWSF() const;
67 
68  //@}
69 
70  /** \name Overridden from Teuchos::Describable. */
71  //@{
72 
73  std::string description() const;
74 
75  //@}
76 
77  /** @name Overridden from ParameterListAcceptor (simple forwarding functions) */
78  //@{
79 
80  void setParameterList(RCP<ParameterList> const& paramList);
81  RCP<ParameterList> getNonconstParameterList();
82  RCP<ParameterList> unsetParameterList();
83  RCP<const ParameterList> getParameterList() const;
84  RCP<const ParameterList> getValidParameters() const;
85 
86  //@}
87 
88  /** \name Overridden from LinearOpWithSolveFactoyBase */
89  //@{
90 
91  /** \brief returns false. */
92  virtual bool acceptsPreconditionerFactory() const;
93 
94  /** \brief Throws exception. */
95  virtual void setPreconditionerFactory(
96  const RCP<PreconditionerFactoryBase<Scalar> > &precFactory,
97  const std::string &precFactoryName
98  );
99 
100  /** \brief Returns null . */
101  virtual RCP<PreconditionerFactoryBase<Scalar> >
102  getPreconditionerFactory() const;
103 
104  /** \brief Throws exception. */
105  virtual void unsetPreconditionerFactory(
106  RCP<PreconditionerFactoryBase<Scalar> > *precFactory,
107  std::string *precFactoryName
108  );
109 
110  virtual bool isCompatible(
111  const LinearOpSourceBase<Scalar> &fwdOpSrc
112  ) const;
113 
114  virtual RCP<LinearOpWithSolveBase<Scalar> > createOp() const;
115 
116  virtual void initializeOp(
117  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
118  LinearOpWithSolveBase<Scalar> *Op,
119  const ESupportSolveUse supportSolveUse
120  ) const;
121 
122  virtual void initializeAndReuseOp(
123  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
124  LinearOpWithSolveBase<Scalar> *Op
125  ) const;
126 
127  virtual void uninitializeOp(
128  LinearOpWithSolveBase<Scalar> *Op,
129  RCP<const LinearOpSourceBase<Scalar> > *fwdOpSrc,
130  RCP<const PreconditionerBase<Scalar> > *prec,
131  RCP<const LinearOpSourceBase<Scalar> > *approxFwdOpSrc,
132  ESupportSolveUse *supportSolveUse
133  ) const;
134 
135  virtual bool supportsPreconditionerInputType(
136  const EPreconditionerInputType precOpType
137  ) const;
138 
139  virtual void initializePreconditionedOp(
140  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
141  const RCP<const PreconditionerBase<Scalar> > &prec,
142  LinearOpWithSolveBase<Scalar> *Op,
143  const ESupportSolveUse supportSolveUse
144  ) const;
145 
147  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
148  const RCP<const LinearOpSourceBase<Scalar> > &approxFwdOpSrc,
149  LinearOpWithSolveBase<Scalar> *Op,
150  const ESupportSolveUse supportSolveUse
151  ) const;
152 
153  //@}
154 
155 protected:
156 
157  /** \brief Overridden from Teuchos::VerboseObjectBase */
158  //@{
159 
160  void informUpdatedVerbosityState() const;
161 
162  //@}
163 
164 private:
165 
166  typedef Teuchos::ConstNonconstObjectContainer<LinearOpWithSolveFactoryBase<Scalar> > LOWSF_t;
167 
169 
170 };
171 
172 /** \brief Nonmember constructor.
173  *
174  * \relates AdjointLinearOpWithSolveFactory
175  */
176 template<class Scalar>
177 RCP<const AdjointLinearOpWithSolveFactory<Scalar> >
179  const RCP<const LinearOpWithSolveFactoryBase<Scalar> > &lowsf
180  )
181 {
182  RCP<AdjointLinearOpWithSolveFactory<Scalar> > alowsf =
183  Teuchos::rcp(new AdjointLinearOpWithSolveFactory<Scalar>);
184  alowsf->initialize(lowsf);
185  return alowsf;
186 }
187 
188 /** \brief Nonmember constructor.
189  *
190  * \relates AdjointLinearOpWithSolveFactory
191  */
192 template<class Scalar>
193 RCP<AdjointLinearOpWithSolveFactory<Scalar> >
195  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &lowsf
196  )
197 {
198  RCP<AdjointLinearOpWithSolveFactory<Scalar> > alowsf =
199  Teuchos::rcp(new AdjointLinearOpWithSolveFactory<Scalar>);
200  alowsf->nonconstInitialize(lowsf);
201  return alowsf;
202 }
203 
204 // Overridden from Constructors/Initializers/Accessors
205 
206 template<class Scalar>
207 void
210  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &lowsf
211  )
212 {
213 #ifdef TEUCHOS_DEBUG
214  TEUCHOS_TEST_FOR_EXCEPT(is_null(lowsf));
215 #endif
216  lowsf_.initialize(lowsf);
217 }
218 
219 template<class Scalar>
220 void
223  const RCP<const LinearOpWithSolveFactoryBase<Scalar> > &lowsf
224  )
225 {
226 #ifdef TEUCHOS_DEBUG
227  TEUCHOS_TEST_FOR_EXCEPT(is_null(lowsf));
228 #endif
229  lowsf_.initialize(lowsf);
230 }
231 
232 template<class Scalar>
233 RCP<LinearOpWithSolveFactoryBase<Scalar> >
236 {
237  return lowsf_.getNonconstObj();
238 }
239 
240 template<class Scalar>
241 RCP<const LinearOpWithSolveFactoryBase<Scalar> >
244 {
245  return lowsf_.getConstObj();
246 }
247 
248 // Overridden from Teuchos::Describable
249 
250 template<class Scalar>
251 std::string
253 description() const
254 {
255  std::ostringstream oss;
256  oss << this->Teuchos::Describable::description()
257  << "{"
258  << "lowsf=";
259  if (!is_null(lowsf_.getConstObj()))
260  oss << lowsf_.getConstObj()->description();
261  else
262  oss << "NULL";
263  oss << "}";
264  return oss.str();
265 }
266 
267 // Overridden from ParameterListAcceptor
268 
269 template<class Scalar>
270 void
273  RCP<ParameterList> const& paramList
274  )
275 {
276  lowsf_.getNonconstObj()->setParameterList(paramList);
277 }
278 
279 template<class Scalar>
280 RCP<ParameterList>
283 {
284  return lowsf_.getNonconstObj()->getNonconstParameterList();
285 }
286 
287 template<class Scalar>
288 RCP<ParameterList>
291 {
292  return lowsf_.getNonconstObj()->unsetParameterList();
293 }
294 
295 template<class Scalar>
296 RCP<const ParameterList>
299 {
300  return lowsf_.getConstObj()->getParameterList();
301 }
302 
303 template<class Scalar>
304 RCP<const ParameterList>
307 {
308  return lowsf_.getConstObj()->getValidParameters();
309 }
310 
311 // Overridden from LinearOpWithSolveFactoyBase
312 
313 template<class Scalar>
314 bool
317 {
318  return lowsf_.getConstObj()->acceptsPreconditionerFactory();
319 }
320 
321 template<class Scalar>
322 void
325  const RCP<PreconditionerFactoryBase<Scalar> > &precFactory,
326  const std::string &precFactoryName
327  )
328 {
330  RCP<APF> apf = Teuchos::rcp_dynamic_cast<APF>(precFactory);
331  lowsf_.getNonconstObj()->setPreconditionerFactory(
332  apf->getNonconstPreconditionerFactory(),
333  precFactoryName);
334 }
335 
336 template<class Scalar>
337 RCP<PreconditionerFactoryBase<Scalar> >
340 {
341  RCP<PreconditionerFactoryBase<Scalar> > prec_fac =
342  lowsf_.getConstObj()->getPreconditionerFactory();
343  if (prec_fac == Teuchos::null)
344  return Teuchos::null;
345  else
346  return nonconstAdjointPreconditionerFactory(prec_fac);
347 }
348 
349 template<class Scalar>
352  RCP<PreconditionerFactoryBase<Scalar> > *precFactory,
353  std::string *precFactoryName
354  )
355 {
356  RCP<PreconditionerFactoryBase<Scalar> > inner_precFactory;
357  lowsf_.getNonconstObj()->unsetPreconditionerFactory(
358  precFactory ? &inner_precFactory : NULL,
359  precFactoryName);
360  if (precFactory)
361  *precFactory = nonconstAdjointPreconditionerFactory(inner_precFactory);
362 }
363 
364 template<class Scalar>
365 bool
368  const LinearOpSourceBase<Scalar> &fwdOpSrc
369  ) const
370 {
371  typedef DefaultScaledAdjointLinearOp<Scalar> ALO;
372  RCP<const ALO> alo =
373  Teuchos::rcp_dynamic_cast<const ALO>(fwdOpSrc.getOp().assert_not_null());
374  RCP<const LinearOpSourceBase<Scalar> > inner_fwdOpSrc =
375  defaultLinearOpSource<Scalar>(alo->getOp());
376  return lowsf_.getConstObj()->isCompatible(*inner_fwdOpSrc);
377 }
378 
379 template<class Scalar>
380 RCP<LinearOpWithSolveBase<Scalar> >
382 createOp() const
383 {
384  return nonconstAdjointLows<Scalar>(lowsf_.getConstObj()->createOp());
385 }
386 
387 template<class Scalar>
388 void
391  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
392  LinearOpWithSolveBase<Scalar> *Op,
393  const ESupportSolveUse supportSolveUse
394  ) const
395 {
396  using Teuchos::dyn_cast;
397  using Teuchos::rcp_dynamic_cast;
398 
399 #ifdef TEUCHOS_DEBUG
400  TEUCHOS_TEST_FOR_EXCEPT(0==Op);
401 #endif
402 
403  // Set the verbosity settings for the wrapped LOWSF object!
404  lowsf_.getConstObj()->setOStream(this->getOStream());
405  lowsf_.getConstObj()->setVerbLevel(this->getVerbLevel());
406 
407  typedef DefaultScaledAdjointLinearOp<Scalar> ALO;
408  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALOWS;
409  const RCP<const ALO> alo =
410  rcp_dynamic_cast<const ALO>(fwdOpSrc->getOp().assert_not_null());
411  ALOWS &alows = dyn_cast<ALOWS>(*Op);
412 
413  lowsf_.getConstObj()->initializeOp(
414  defaultLinearOpSource<Scalar>(alo->getOrigOp()),
415  alows.getNonconstOp().get(),
416  supportSolveUse);
417 }
418 
419 template<class Scalar>
420 void
423  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
424  LinearOpWithSolveBase<Scalar> *Op
425  ) const
426 {
427  using Teuchos::dyn_cast;
428  using Teuchos::rcp_dynamic_cast;
429 
430 #ifdef TEUCHOS_DEBUG
431  TEUCHOS_TEST_FOR_EXCEPT(0==Op);
432 #endif
433 
434  // Set the verbosity settings for the wrapped LOWSF object!
435  lowsf_.getConstObj()->setOStream(this->getOStream());
436  lowsf_.getConstObj()->setVerbLevel(this->getVerbLevel());
437 
438  typedef DefaultScaledAdjointLinearOp<Scalar> ALO;
439  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALOWS;
440  const RCP<const ALO> alo =
441  rcp_dynamic_cast<const ALO>(fwdOpSrc->getOp().assert_not_null());
442  ALOWS &alows = dyn_cast<ALOWS>(*Op);
443 
444  lowsf_.getConstObj()->initializeAndReuseOp(
445  defaultLinearOpSource<Scalar>(alo->getOrigOp()),
446  alows.getNonconstOp().get());
447 }
448 
449 template<class Scalar>
450 void
453  LinearOpWithSolveBase<Scalar> *Op,
454  RCP<const LinearOpSourceBase<Scalar> > *fwdOpSrc,
455  RCP<const PreconditionerBase<Scalar> > *prec,
456  RCP<const LinearOpSourceBase<Scalar> > *approxFwdOpSrc,
457  ESupportSolveUse *supportSolveUse
458  ) const
459 {
460  using Teuchos::dyn_cast;
461 
462 #ifdef TEUCHOS_DEBUG
463  TEUCHOS_TEST_FOR_EXCEPT(0==Op);
464 #endif
465  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALOWS;
466  ALOWS &alowsOp = dyn_cast<ALOWS>(*Op);
467  RCP<const LinearOpSourceBase<Scalar> > inner_fwdOpSrc;
468  RCP<const PreconditionerBase<Scalar> > inner_prec;
469  RCP<const LinearOpSourceBase<Scalar> > inner_approxFwdOpSrc;
470  lowsf_.getConstObj()->uninitializeOp(
471  alowsOp.getNonconstOp().get(),
472  fwdOpSrc ? &inner_fwdOpSrc : NULL,
473  prec ? &inner_prec : NULL,
474  approxFwdOpSrc ? &inner_approxFwdOpSrc : NULL,
475  supportSolveUse);
476  if (fwdOpSrc)
477  *fwdOpSrc =
478  defaultLinearOpSource<Scalar>(adjoint(inner_fwdOpSrc->getOp()));
479  if (prec)
480  *prec = adjointPreconditioner(inner_prec);
481  if (fwdOpSrc)
482  *approxFwdOpSrc =
483  defaultLinearOpSource<Scalar>(adjoint(inner_approxFwdOpSrc->getOp()));
484 }
485 
486 template<class Scalar>
487 bool
490  const EPreconditionerInputType precOpType
491  ) const
492 {
493  return lowsf_.getConstObj()->supportsPreconditionerInputType(precOpType);
494 }
495 
496 template<class Scalar>
497 void
500  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
501  const RCP<const PreconditionerBase<Scalar> > &prec,
502  LinearOpWithSolveBase<Scalar> *Op,
503  const ESupportSolveUse supportSolveUse
504  ) const
505 {
506  using Teuchos::dyn_cast;
507  using Teuchos::rcp_dynamic_cast;
508 
509 #ifdef TEUCHOS_DEBUG
510  TEUCHOS_TEST_FOR_EXCEPT(0==Op);
511 #endif
512 
513  // Set the verbosity settings for the wrapped LOWSF object!
514  lowsf_.getConstObj()->setOStream(this->getOStream());
515  lowsf_.getConstObj()->setVerbLevel(this->getVerbLevel());
516 
517  typedef DefaultScaledAdjointLinearOp<Scalar> ALO;
519  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALOWS;
520  const RCP<const ALO> alo =
521  rcp_dynamic_cast<const ALO>(fwdOpSrc->getOp().assert_not_null());
522  const RCP<const AP> ap = rcp_dynamic_cast<const AP>(prec);
523  ALOWS &alows = dyn_cast<ALOWS>(*Op);
524 
525  lowsf_.getConstObj()->initializePreconditionedOp(
526  defaultLinearOpSource<Scalar>(alo->getOp()),
527  ap->getPreconditioner(),
528  alows.getNonconstOp().get(),
529  supportSolveUse);
530 }
531 
532 template<class Scalar>
533 void
536  const RCP<const LinearOpSourceBase<Scalar> > &fwdOpSrc,
537  const RCP<const LinearOpSourceBase<Scalar> > &approxFwdOpSrc,
538  LinearOpWithSolveBase<Scalar> *Op,
539  const ESupportSolveUse supportSolveUse
540  ) const
541 {
542  using Teuchos::dyn_cast;
543  using Teuchos::rcp_dynamic_cast;
544 
545 #ifdef TEUCHOS_DEBUG
546  TEUCHOS_TEST_FOR_EXCEPT(0==Op);
547 #endif
548 
549  // Set the verbosity settings for the wrapped LOWSF object!
550  lowsf_.getConstObj()->setOStream(this->getOStream());
551  lowsf_.getConstObj()->setVerbLevel(this->getVerbLevel());
552 
553  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALO;
554  typedef DefaultAdjointLinearOpWithSolve<Scalar> ALOWS;
555  const RCP<const ALO> alo =
556  rcp_dynamic_cast<const ALO>(fwdOpSrc->getOp().assert_not_null());
557  const RCP<const ALO> aalo =
558  rcp_dynamic_cast<const ALO>(approxFwdOpSrc->getOp().assert_not_null());
559  ALOWS &alows = dyn_cast<ALOWS>(*Op);
560 
561  lowsf_.getConstObj()->initializeApproxPreconditionedOp(
562  defaultLinearOpSource<Scalar>(alo->getOp()),
563  defaultLinearOpSource<Scalar>(aalo->getOp()),
564  alows.getNonconstOp().get(),
565  supportSolveUse);
566 }
567 
568 // protected
569 
570 template<class Scalar>
571 void
574 {
575  lowsf_.getConstObj()->setVerbLevel(this->getVerbLevel());
576  lowsf_.getConstObj()->setOStream(this->getOStream());
577 }
578 
579 } // namespace Thyra
580 
581 #endif
Concrete PreconditionerBase subclass that wraps a preconditioner operator in MultiVectorLinearOp.
virtual void uninitializeOp(LinearOpWithSolveBase< Scalar > *Op, RCP< const LinearOpSourceBase< Scalar > > *fwdOpSrc, RCP< const PreconditionerBase< Scalar > > *prec, RCP< const LinearOpSourceBase< Scalar > > *approxFwdOpSrc, ESupportSolveUse *supportSolveUse) const
RCP< LinearOpWithSolveFactoryBase< Scalar > > getUnderlyingLOWSF()
virtual void initializeApproxPreconditionedOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, const RCP< const LinearOpSourceBase< Scalar > > &approxFwdOpSrc, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
Create a LinearOpWithSolveFactory for an adjoint linear op.
RCP< AdjointLinearOpWithSolveFactory< Scalar > > nonconstAdjointLinearOpWithSolveFactory(const RCP< LinearOpWithSolveFactoryBase< Scalar > > &lowsf)
Nonmember constructor.
virtual bool supportsPreconditionerInputType(const EPreconditionerInputType precOpType) const
virtual bool isCompatible(const LinearOpSourceBase< Scalar > &fwdOpSrc) const
virtual void unsetPreconditionerFactory(RCP< PreconditionerFactoryBase< Scalar > > *precFactory, std::string *precFactoryName)
Throws exception.
RCP< const AdjointLinearOpWithSolveFactory< Scalar > > adjointLinearOpWithSolveFactory(const RCP< const LinearOpWithSolveFactoryBase< Scalar > > &lowsf)
Nonmember constructor.
void informUpdatedVerbosityState() const
Overridden from Teuchos::VerboseObjectBase.
void setParameterList(RCP< ParameterList > const &paramList)
void initialize(const RCP< const LinearOpWithSolveFactoryBase< Scalar > > &lowsf)
Initialize given a single const LOWSFB object.
virtual bool acceptsPreconditionerFactory() const
returns false.
virtual void setPreconditionerFactory(const RCP< PreconditionerFactoryBase< Scalar > > &precFactory, const std::string &precFactoryName)
Throws exception.
virtual void initializePreconditionedOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, const RCP< const PreconditionerBase< Scalar > > &prec, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
Concrete PreconditionerFactoryBase subclass that wraps a preconditioner in AdjointPreconditioner.
Teuchos::ConstNonconstObjectContainer< LinearOpWithSolveFactoryBase< Scalar > > LOWSF_t
virtual RCP< PreconditionerFactoryBase< Scalar > > getPreconditionerFactory() const
Returns null .
virtual void initializeOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, LinearOpWithSolveBase< Scalar > *Op, const ESupportSolveUse supportSolveUse) const
virtual RCP< LinearOpWithSolveBase< Scalar > > createOp() const
virtual void initializeAndReuseOp(const RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, LinearOpWithSolveBase< Scalar > *Op) const
void nonconstInitialize(const RCP< LinearOpWithSolveFactoryBase< Scalar > > &lowsf)
Initialize given a single non-const LOWSFB object.