Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_SingleResidualModelEvaluator.hpp
1 //@HEADER
2 // ***********************************************************************
3 //
4 // Rythmos Package
5 // Copyright (2006) 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 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25 //
26 // ***********************************************************************
27 //@HEADER
28 
29 
30 #ifndef RYTHMOS_SINGLE_RESIDUAL_MODEL_EVALUATOR_HPP
31 #define RYTHMOS_SINGLE_RESIDUAL_MODEL_EVALUATOR_HPP
32 
33 
34 #include "Rythmos_SingleResidualModelEvaluatorBase.hpp"
35 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
36 #include "Thyra_ModelEvaluatorHelpers.hpp"
37 #include "Thyra_VectorStdOps.hpp"
38 #include "Thyra_TestingTools.hpp"
39 #include "Teuchos_as.hpp"
40 
41 
42 namespace Rythmos {
43 
44 
66 template<class Scalar>
68  : virtual public SingleResidualModelEvaluatorBase<Scalar>,
69  virtual public Thyra::ModelEvaluatorDelegatorBase<Scalar>
70 {
71 public:
72 
75 
78 
80 
83 
86  const RCP<const Thyra::ModelEvaluator<Scalar> > &daeModel,
87  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
88  const Scalar &coeff_x_dot,
89  const RCP<const Thyra::VectorBase<Scalar> > &x_dot_base,
90  const Scalar &coeff_x,
91  const RCP<const Thyra::VectorBase<Scalar> > &x_base,
92  const Scalar &t_base,
93  const RCP<const Thyra::VectorBase<Scalar> > &x_bar_init
94  );
95 
97  Scalar get_coeff_x_dot() const;
98 
100  RCP<const Thyra::VectorBase<Scalar> >
101  get_x_dot_base() const;
102 
104  Scalar get_coeff_x() const;
105 
107  RCP<const Thyra::VectorBase<Scalar> >
108  get_x_base() const;
109 
111  Scalar get_t_base() const;
112 
114 
117 
119  Thyra::ModelEvaluatorBase::InArgs<Scalar> getNominalValues() const;
121  Thyra::ModelEvaluatorBase::InArgs<Scalar> createInArgs() const;
122 
124 
125 private:
126 
129 
131  Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
133  void evalModelImpl(
134  const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
135  const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
136  ) const;
137 
139 
140 
141 private:
142 
143  Thyra::ModelEvaluatorBase::InArgs<Scalar> basePoint_;
144  Scalar coeff_x_dot_;
145  RCP<const Thyra::VectorBase<Scalar> > x_dot_base_;
146  Scalar coeff_x_;
147  RCP<const Thyra::VectorBase<Scalar> > x_base_;
148  Scalar t_base_;
149 
150  Thyra::ModelEvaluatorBase::InArgs<Scalar> nominalValues_;
151 
152  // cache
153  RCP<Thyra::VectorBase<Scalar> > x_;
154  RCP<Thyra::VectorBase<Scalar> > x_dot_;
155 
156 };
157 
158 
159 // ///////////////////////
160 // Definition
161 
162 
163 // Constructors/initializers/accessors
164 
165 
166 template<class Scalar>
168 {}
169 
170 
171 // Overridden from SingleResidualModelEvaluatorBase
172 
173 
174 template<class Scalar>
176  const RCP<const Thyra::ModelEvaluator<Scalar> > &daeModel,
177  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &basePoint,
178  const Scalar &coeff_x_dot,
179  const RCP<const Thyra::VectorBase<Scalar> > &x_dot_base,
180  const Scalar &coeff_x,
181  const RCP<const Thyra::VectorBase<Scalar> > &x_base,
182  const Scalar &t_base,
183  const RCP<const Thyra::VectorBase<Scalar> > &x_bar_init
184  )
185 {
186  this->Thyra::ModelEvaluatorDelegatorBase<Scalar>::initialize(daeModel);
187  basePoint_ = basePoint;
188  coeff_x_dot_ = coeff_x_dot;
189  x_dot_base_ = x_dot_base;
190  coeff_x_ = coeff_x;
191  x_base_ = x_base;
192  t_base_ = t_base;
193 
194  nominalValues_ = daeModel->getNominalValues();
195  nominalValues_.set_x(x_bar_init);
196 
197  x_dot_ = createMember( daeModel->get_x_space() );
198  x_ = createMember( daeModel->get_x_space() );
199 
200  // ToDo: Check that daeModel supports x_dot, x and maybe t
201 
202 #ifdef THYRA_RYTHMOS_DEBUG
203  std::cout << "----------------------------------------------------------------------" << std::endl;
204  std::cout << "Rythmos::SingleResidualModelEvaluator::initialize" << std::endl;
205  std::cout << "coeff_x_dot_ = " << coeff_x_dot_ << std::endl;
206  std::cout << "x_dot_base_ = ";
207  if ( x_dot_base_.get() )
208  std::cout << "\n" << *x_dot_base_ << std::endl;
209  else
210  std::cout << "null" << std::endl;
211  std::cout << "coeff_x_ = " << coeff_x_ << std::endl;
212  std::cout << "x_base_ = ";
213  if ( x_base_.get() )
214  std::cout << "\n" << *x_base_ << std::endl;
215  else
216  std::cout << "null" << std::endl;
217  std::cout << "t_base_ = " << t_base_ << std::endl;
218  std::cout << "x_bar_init = ";
219  if ( x_bar_init.get() )
220  std::cout << "\n" << *x_bar_init_ << std::endl;
221  else
222  std::cout << "null" << std::endl;
223  std::cout << "x_dot_ = ";
224  if ( x_dot_.get() )
225  std::cout << "\n" << *x_dot_ << std::endl;
226  else
227  std::cout << "null" << std::endl;
228  std::cout << "x_ = ";
229  if ( x_.get() )
230  std::cout << "\n" << *x_ << std::endl;
231  else
232  std::cout << "null" << std::endl;
233  std::cout << "----------------------------------------------------------------------" << std::endl;
234 #endif // THYRA_RYTHMOS_DEBUG
235 }
236 
237 
238 template<class Scalar>
240 {
241  return coeff_x_dot_;
242 }
243 
244 
245 template<class Scalar>
246 RCP<const Thyra::VectorBase<Scalar> >
248 {
249  return x_dot_base_;
250 }
251 
252 
253 template<class Scalar>
255 {
256  return coeff_x_;
257 }
258 
259 
260 template<class Scalar>
261 RCP<const Thyra::VectorBase<Scalar> >
263 {
264  return x_base_;
265 }
266 
267 
268 template<class Scalar>
270 {
271  return t_base_;
272 }
273 
274 
275 // Overridden from ModelEvaluator
276 
277 
278 template<class Scalar>
279 Thyra::ModelEvaluatorBase::InArgs<Scalar>
281 {
282  return nominalValues_;
283 }
284 
285 
286 template<class Scalar>
287 Thyra::ModelEvaluatorBase::InArgs<Scalar>
289 {
290  typedef Thyra::ModelEvaluatorBase MEB;
291  MEB::InArgsSetup<Scalar> inArgs;
292  inArgs.setModelEvalDescription(this->description());
293  inArgs.setSupports(MEB::IN_ARG_x);
294  return inArgs;
295 }
296 
297 
298 // Private functions overridden from ModelEvaluatorDefaultBase
299 
300 
301 template<class Scalar>
302 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
304 {
305  typedef Thyra::ModelEvaluatorBase MEB;
306  const RCP<const Thyra::ModelEvaluator<Scalar> >
307  daeModel = this->getUnderlyingModel();
308  MEB::OutArgs<Scalar> daeOutArgs = daeModel->createOutArgs();
309  MEB::OutArgsSetup<Scalar> outArgs;
310  outArgs.setModelEvalDescription(this->description());
311  outArgs.setSupports(MEB::OUT_ARG_f);
312  if (daeOutArgs.supports(MEB::OUT_ARG_W_op)) {
313  outArgs.setSupports(MEB::OUT_ARG_W_op);
314  }
315  if (daeOutArgs.supports(MEB::OUT_ARG_W)) {
316  outArgs.setSupports(MEB::OUT_ARG_W);
317  }
318  return outArgs;
319 }
320 
321 
322 template<class Scalar>
323 void SingleResidualModelEvaluator<Scalar>::evalModelImpl(
324  const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs_bar,
325  const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs_bar
326  ) const
327 {
328 
329  using std::endl;
330  using Teuchos::as;
331  typedef Thyra::ModelEvaluatorBase MEB;
332 
333  const RCP<const Thyra::ModelEvaluator<Scalar> >
334  daeModel = this->getUnderlyingModel();
335 
336  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_BEGIN(
337  "Rythmos::SingleResidualModelEvaluator",inArgs_bar,outArgs_bar
338  );
339 
340  const bool dumpAll = ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_EXTREME) );
341 
342  const Thyra::VectorBase<Scalar> &x_bar = *inArgs_bar.get_x();
343 
344  // x_dot = coeff_x_dot * x_bar + x_dot_base
345 
346  if (x_dot_base_.get())
347  Thyra::V_StVpV( x_dot_.ptr(), coeff_x_dot_, x_bar, *x_dot_base_ );
348  else
349  Thyra::V_StV( x_dot_.ptr(), coeff_x_dot_, x_bar);
350 
351  if (dumpAll) {
352  *out << "\nx_dot_ = coeff_x_dot_ * x_bar + x_dot_base_\n";
353  *out << "\ncoeff_x_dot_ = " << coeff_x_dot_ << endl;
354  *out << "\nx_bar = " << x_bar;
355  *out << "\nx_dot_base_ = ";
356  if ( x_dot_base_.get() )
357  *out << *x_dot_base_;
358  else
359  *out << "null" << endl;
360  *out << "\nx_dot_ = ";
361  if ( x_dot_.get() )
362  *out << *x_dot_;
363  else
364  *out << "null" << endl;
365  }
366 
367  // x = coeff_x * x_bar + x_base
368 
369  if (x_base_.get())
370  Thyra::V_StVpV( x_.ptr(), coeff_x_, x_bar, *x_base_ );
371  else
372  Thyra::V_StV( x_.ptr(), coeff_x_, x_bar);
373 
374  if (dumpAll) {
375  *out << "\nx_ = coeff_x_ * x_bar + x_base_\n";
376  *out << "\ncoeff_x_ = " << coeff_x_ << endl;
377  *out << "\nx_bar = " << x_bar;
378  *out << "\nx_base_ = ";
379  if ( x_base_.get() )
380  *out << *x_base_;
381  else
382  *out << "null" << endl;
383  *out << "\nx_ = ";
384  if ( x_.get() )
385  *out << *x_;
386  else
387  *out << "null" << endl;
388  }
389 
390  // Compute W and f
391 
392  if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW))
393  *out << "\nEvaluating the underlying DAE model at (x_bar_dot,x_bar,t) ...\n";
394 
395  RCP<Thyra::LinearOpWithSolveBase<Scalar> > W;
396 
397  MEB::InArgs<Scalar> daeInArgs = daeModel->createInArgs();
398  daeInArgs.setArgs(basePoint_);
399  daeInArgs.set_x_dot(x_dot_);
400  daeInArgs.set_x(x_);
401  daeInArgs.set_t(t_base_);
402  daeInArgs.set_alpha(coeff_x_dot_);
403  daeInArgs.set_beta(coeff_x_);
404  MEB::OutArgs<Scalar> daeOutArgs = daeModel->createOutArgs();
405  daeOutArgs.set_f(outArgs_bar.get_f()); // can be null
406  if (daeOutArgs.supports(MEB::OUT_ARG_W)) {
407  daeOutArgs.set_W(outArgs_bar.get_W()); // can be null
408  }
409  if (daeOutArgs.supports(MEB::OUT_ARG_W_op)) {
410  daeOutArgs.set_W_op(outArgs_bar.get_W_op()); // can be null
411  }
412  daeModel->evalModel(daeInArgs,daeOutArgs);
413 
414  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
415 
416 }
417 
418 
419 } // namespace Rythmos
420 
421 
422 #endif // RYTHMOS_SINGLE_RESIDUAL_MODEL_EVALUATOR_HPP
RCP< const Thyra::VectorBase< Scalar > > get_x_base() const
void initializeSingleResidualModel(const RCP< const Thyra::ModelEvaluator< Scalar > > &daeModel, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &basePoint, const Scalar &coeff_x_dot, const RCP< const Thyra::VectorBase< Scalar > > &x_dot_base, const Scalar &coeff_x, const RCP< const Thyra::VectorBase< Scalar > > &x_base, const Scalar &t_base, const RCP< const Thyra::VectorBase< Scalar > > &x_bar_init)
RCP< const Thyra::VectorBase< Scalar > > get_x_dot_base() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Decorator subclass for a steady-state version of a DAE for single-residual time stepper methods...
Base class mix-in interface for single-residual model evaluators.
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const