Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DefaultModelEvaluatorWithSolveFactory.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_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
11 #define THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
12 
13 
14 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
15 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
16 #include "Teuchos_Time.hpp"
17 
18 
19 namespace Thyra {
20 
21 
30 template<class Scalar>
32  : virtual public ModelEvaluatorDelegatorBase<Scalar>
33 {
34 public:
35 
38 
41 
44  const RCP<ModelEvaluator<Scalar> > &thyraModel,
45  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
46  );
47 
49  void initialize(
50  const RCP<ModelEvaluator<Scalar> > &thyraModel,
51  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
52  );
53 
55  void uninitialize(
56  RCP<ModelEvaluator<Scalar> > *thyraModel = NULL,
57  RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory = NULL
58  );
59 
61 
64 
66  std::string description() const;
67 
69 
72 
75 
77 
80 
83 
85 
86 private:
87 
90 
92  ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
94  void evalModelImpl(
97  ) const;
98 
100 
101 private:
102 
104 
105 };
106 
107 
108 // /////////////////////////////////
109 // Implementations
110 
111 
112 // Constructors/initializers/accessors/utilities
113 
114 
115 template<class Scalar>
117 {}
118 
119 
120 template<class Scalar>
122  const RCP<ModelEvaluator<Scalar> > &thyraModel,
123  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
124  )
125 {
126  initialize(thyraModel,W_factory);
127 }
128 
129 
130 template<class Scalar>
132  const RCP<ModelEvaluator<Scalar> > &thyraModel,
133  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
134  )
135 {
137  W_factory_ = W_factory;
138 }
139 
140 
141 template<class Scalar>
143  RCP<ModelEvaluator<Scalar> > *thyraModel,
145  )
146 {
147  if(thyraModel) *thyraModel = this->getUnderlyingModel();
148  if(W_factory) *W_factory = W_factory_;
150  W_factory_ = Teuchos::null;
151 }
152 
153 
154 // Public functions overridden from Teuchos::Describable
155 
156 
157 template<class Scalar>
159 {
161  thyraModel = this->getUnderlyingModel();
162  std::ostringstream oss;
163  oss << "Thyra::DefaultModelEvaluatorWithSolveFactory{";
164  oss << "thyraModel=";
165  if(thyraModel.get())
166  oss << "\'"<<thyraModel->description()<<"\'";
167  else
168  oss << "NULL";
169  oss << ",W_factory=";
170  if(W_factory_.get())
171  oss << "\'"<<W_factory_->description()<<"\'";
172  else
173  oss << "NULL";
174  oss << "}";
175  return oss.str();
176 }
177 
178 
179 // Overridden from ModelEvaluator.
180 
181 
182 template<class Scalar>
185 {
187  W_factory_.get()==NULL, std::logic_error
188  ,"Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W(): "
189  "Error, the client did not set a LinearOpWithSolveFactoryBase object for W!"
190  );
191  W_factory_->setOStream(this->getOStream());
192  W_factory_->setVerbLevel(this->getVerbLevel());
193  return W_factory_->createOp();
194 }
195 
196 
197 // Overridden from ModelEvaluatorDelegatorBase.
198 
199 
200 template<class Scalar>
203 {
204  return W_factory_;
205 }
206 
207 
208 // Private functions overridden from ModelEvaluatorDefaultBase.
209 
210 
211 template<class Scalar>
214 {
215  typedef ModelEvaluatorBase MEB;
217  thyraModel = this->getUnderlyingModel();
218  const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
219  MEB::OutArgsSetup<Scalar> outArgs;
220  outArgs.setModelEvalDescription(this->description());
221  outArgs.set_Np_Ng(wrappedOutArgs.Np(),wrappedOutArgs.Ng());
222  outArgs.setSupports(wrappedOutArgs);
223  outArgs.setSupports(MEB::OUT_ARG_W,
224  wrappedOutArgs.supports(MEB::OUT_ARG_W_op)&&W_factory_.get()!=NULL);
225  return outArgs;
226 }
227 
228 
229 template<class Scalar>
230 void DefaultModelEvaluatorWithSolveFactory<Scalar>::evalModelImpl(
231  const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
232  const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
233  ) const
234 {
235  typedef ModelEvaluatorBase MEB;
236  using Teuchos::rcp;
237  using Teuchos::rcp_const_cast;
238  using Teuchos::rcp_dynamic_cast;
239  using Teuchos::OSTab;
240 
241  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
242  "Thyra::DefaultModelEvaluatorWithSolveFactory",inArgs,outArgs
243  );
244 
245  Teuchos::Time timer("");
246 
248  VOTSLOWSF;
249  VOTSLOWSF W_factory_outputTempState(W_factory_,out,verbLevel);
250 
251  // InArgs
252 
253  MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
254 
255  wrappedInArgs.setArgs(inArgs,true);
256 
257  // OutArgs
258 
259  MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
260 
261  wrappedOutArgs.setArgs(outArgs,true);
262 
263  RCP<LinearOpWithSolveBase<Scalar> > W;
264  RCP<const LinearOpBase<Scalar> > fwdW;
265  if( outArgs.supports(MEB::OUT_ARG_W) && (W = outArgs.get_W()).get() ) {
266  Thyra::uninitializeOp<Scalar>(*W_factory_, W.ptr(), outArg(fwdW));
267 
268  {
269  // Handle this case later if we need to!
270  const bool both_W_and_W_op_requested = nonnull(outArgs.get_W_op());
271  TEUCHOS_TEST_FOR_EXCEPT(both_W_and_W_op_requested);
272  }
273 
274  RCP<LinearOpBase<Scalar> > nonconst_fwdW;
275  if(fwdW.get()) {
276  nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
277  }
278  else {
279  nonconst_fwdW = thyraModel->create_W_op();
280  fwdW = nonconst_fwdW;
281  }
282 
283  wrappedOutArgs.set_W_op(nonconst_fwdW);
284  }
285 
286  // Do the evaluation
287 
288  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
289  *out << "\nEvaluating the output functions on model \'"
290  << thyraModel->description() << "\' ...\n";
291  timer.start(true);
292 
293  thyraModel->evalModel(wrappedInArgs,wrappedOutArgs);
294 
295  timer.stop();
296  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
297  OSTab(out).o() << "\nTime to evaluate underlying model = "
298  << timer.totalElapsedTime()<<" sec\n";
299 
300  // Postprocess arguments
301 
302  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
303  *out << "\nPost processing the output objects ...\n";
304  timer.start(true);
305 
306  if( W.get() ) {
307  Thyra::initializeOp<Scalar>(*W_factory_, fwdW, W.ptr());
308  W->setVerbLevel(this->getVerbLevel());
309  W->setOStream(this->getOStream());
310  }
311 
312  timer.stop();
313  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
314  OSTab(out).o() << "\nTime to process output objects = "
315  << timer.totalElapsedTime()<<" sec\n";
316 
317  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
318 
319 }
320 
321 
322 } // namespace Thyra
323 
324 
325 #endif // THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
void initialize(int *argc, char ***argv)
Pure abstract base interface for evaluating a stateless &quot;model&quot; that can be mapped into a number of d...
basic_OSTab< char > OSTab
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
This is a base class that delegetes almost all function to a wrapped model evaluator object...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void initialize(const RCP< ModelEvaluator< Scalar > > &model)
Initialize given a non-const model evaluator.
T * get() const
RCP< const LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Factory interface for creating LinearOpWithSolveBase objects from compatible LinearOpBase objects...
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
Base subclass for ModelEvaluator that defines some basic types.
bool nonnull(const boost::shared_ptr< T > &p)
void initialize(const RCP< ModelEvaluator< Scalar > > &thyraModel, const RCP< LinearOpWithSolveFactoryBase< Scalar > > &W_factory)
This class wraps any ModelEvaluator object and uses a compatible LinearOpWithSolveFactory object to c...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...