Thyra  Version of the Day
Thyra_DefaultModelEvaluatorWithSolveFactory.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
43 #define THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
44 
45 
46 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
47 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
48 #include "Teuchos_Time.hpp"
49 
50 
51 namespace Thyra {
52 
53 
62 template<class Scalar>
64  : virtual public ModelEvaluatorDelegatorBase<Scalar>
65 {
66 public:
67 
70 
73 
76  const RCP<ModelEvaluator<Scalar> > &thyraModel,
77  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
78  );
79 
81  void initialize(
82  const RCP<ModelEvaluator<Scalar> > &thyraModel,
83  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
84  );
85 
87  void uninitialize(
88  RCP<ModelEvaluator<Scalar> > *thyraModel = NULL,
89  RCP<LinearOpWithSolveFactoryBase<Scalar> > *W_factory = NULL
90  );
91 
93 
96 
98  std::string description() const;
99 
101 
104 
107 
109 
112 
115 
117 
118 private:
119 
122 
124  ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
126  void evalModelImpl(
129  ) const;
130 
132 
133 private:
134 
136 
137 };
138 
139 
140 // /////////////////////////////////
141 // Implementations
142 
143 
144 // Constructors/initializers/accessors/utilities
145 
146 
147 template<class Scalar>
149 {}
150 
151 
152 template<class Scalar>
154  const RCP<ModelEvaluator<Scalar> > &thyraModel,
155  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
156  )
157 {
158  initialize(thyraModel,W_factory);
159 }
160 
161 
162 template<class Scalar>
164  const RCP<ModelEvaluator<Scalar> > &thyraModel,
165  const RCP<LinearOpWithSolveFactoryBase<Scalar> > &W_factory
166  )
167 {
169  W_factory_ = W_factory;
170 }
171 
172 
173 template<class Scalar>
175  RCP<ModelEvaluator<Scalar> > *thyraModel,
177  )
178 {
179  if(thyraModel) *thyraModel = this->getUnderlyingModel();
180  if(W_factory) *W_factory = W_factory_;
182  W_factory_ = Teuchos::null;
183 }
184 
185 
186 // Public functions overridden from Teuchos::Describable
187 
188 
189 template<class Scalar>
191 {
193  thyraModel = this->getUnderlyingModel();
194  std::ostringstream oss;
195  oss << "Thyra::DefaultModelEvaluatorWithSolveFactory{";
196  oss << "thyraModel=";
197  if(thyraModel.get())
198  oss << "\'"<<thyraModel->description()<<"\'";
199  else
200  oss << "NULL";
201  oss << ",W_factory=";
202  if(W_factory_.get())
203  oss << "\'"<<W_factory_->description()<<"\'";
204  else
205  oss << "NULL";
206  oss << "}";
207  return oss.str();
208 }
209 
210 
211 // Overridden from ModelEvaluator.
212 
213 
214 template<class Scalar>
217 {
219  W_factory_.get()==NULL, std::logic_error
220  ,"Thyra::DefaultModelEvaluatorWithSolveFactory<Scalar>::create_W(): "
221  "Error, the client did not set a LinearOpWithSolveFactoryBase object for W!"
222  );
223  W_factory_->setOStream(this->getOStream());
224  W_factory_->setVerbLevel(this->getVerbLevel());
225  return W_factory_->createOp();
226 }
227 
228 
229 // Overridden from ModelEvaluatorDelegatorBase.
230 
231 
232 template<class Scalar>
235 {
236  return W_factory_;
237 }
238 
239 
240 // Private functions overridden from ModelEvaluatorDefaultBase.
241 
242 
243 template<class Scalar>
246 {
247  typedef ModelEvaluatorBase MEB;
249  thyraModel = this->getUnderlyingModel();
250  const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
251  MEB::OutArgsSetup<Scalar> outArgs;
252  outArgs.setModelEvalDescription(this->description());
253  outArgs.set_Np_Ng(wrappedOutArgs.Np(),wrappedOutArgs.Ng());
254  outArgs.setSupports(wrappedOutArgs);
255  outArgs.setSupports(MEB::OUT_ARG_W,
256  wrappedOutArgs.supports(MEB::OUT_ARG_W_op)&&W_factory_.get()!=NULL);
257  return outArgs;
258 }
259 
260 
261 template<class Scalar>
265  ) const
266 {
267  typedef ModelEvaluatorBase MEB;
268  using Teuchos::rcp;
269  using Teuchos::rcp_const_cast;
270  using Teuchos::rcp_dynamic_cast;
271  using Teuchos::OSTab;
272 
273  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
274  "Thyra::DefaultModelEvaluatorWithSolveFactory",inArgs,outArgs
275  );
276 
277  Teuchos::Time timer("");
278 
280  VOTSLOWSF;
281  VOTSLOWSF W_factory_outputTempState(W_factory_,out,verbLevel);
282 
283  // InArgs
284 
285  MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
286 
287  wrappedInArgs.setArgs(inArgs,true);
288 
289  // OutArgs
290 
291  MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
292 
293  wrappedOutArgs.setArgs(outArgs,true);
294 
297  if( outArgs.supports(MEB::OUT_ARG_W) && (W = outArgs.get_W()).get() ) {
298  Thyra::uninitializeOp<Scalar>(*W_factory_, W.ptr(), outArg(fwdW));
299 
300  {
301  // Handle this case later if we need to!
302  const bool both_W_and_W_op_requested = nonnull(outArgs.get_W_op());
303  TEUCHOS_TEST_FOR_EXCEPT(both_W_and_W_op_requested);
304  }
305 
306  RCP<LinearOpBase<Scalar> > nonconst_fwdW;
307  if(fwdW.get()) {
308  nonconst_fwdW = rcp_const_cast<LinearOpBase<Scalar> >(fwdW);
309  }
310  else {
311  nonconst_fwdW = thyraModel->create_W_op();
312  fwdW = nonconst_fwdW;
313  }
314 
315  wrappedOutArgs.set_W_op(nonconst_fwdW);
316  }
317 
318  // Do the evaluation
319 
320  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
321  *out << "\nEvaluating the output functions on model \'"
322  << thyraModel->description() << "\' ...\n";
323  timer.start(true);
324 
325  thyraModel->evalModel(wrappedInArgs,wrappedOutArgs);
326 
327  timer.stop();
328  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
329  OSTab(out).o() << "\nTime to evaluate underlying model = "
330  << timer.totalElapsedTime()<<" sec\n";
331 
332  // Postprocess arguments
333 
334  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
335  *out << "\nPost processing the output objects ...\n";
336  timer.start(true);
337 
338  if( W.get() ) {
339  Thyra::initializeOp<Scalar>(*W_factory_, fwdW, W.ptr());
340  W->setVerbLevel(this->getVerbLevel());
341  W->setOStream(this->getOStream());
342  }
343 
344  timer.stop();
345  if(out.get() && includesVerbLevel(verbLevel,Teuchos::VERB_LOW))
346  OSTab(out).o() << "\nTime to process output objects = "
347  << timer.totalElapsedTime()<<" sec\n";
348 
349  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
350 
351 }
352 
353 
354 } // namespace Thyra
355 
356 
357 #endif // THYRA_DEFAULT_MODEL_EVALUATOR_WITH_SOLVE_FACTORY_HPP
Pure abstract base interface for evaluating a stateless "model" that can be mapped into a number of d...
RCP< LinearOpWithSolveBase< Scalar > > get_W() const
Precondition: supports(OUT_ARG_W)==true.
basic_OSTab< char > OSTab
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
VERB_LOW
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)
virtual EVerbosityLevel getVerbLevel() const
void initialize(const RCP< ModelEvaluator< Scalar > > &model)
Initialize given a non-const model evaluator.
virtual RCP< FancyOStream > getOStream() const
RCP< const LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
T * get() const
void start(bool reset=false)
double stop()
Factory interface for creating LinearOpWithSolveBase objects from compatible LinearOpBase objects...
bool supports(EOutArgsMembers arg) const
Determine if an input argument is supported or not.
Base class for all linear operators.
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
double totalElapsedTime(bool readCurrentTime=false) const
Base subclass for ModelEvaluator that defines some basic types.
virtual RCP< const ModelEvaluator< Scalar > > getUnderlyingModel() const
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...
RCP< LinearOpBase< Scalar > > get_W_op() const
Precondition: supports(OUT_ARG_W_op)==true.
Ptr< T > ptr() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...