Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_StepperAsModelEvaluator.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_STEPPER_AS_MODEL_EVALUATOR_HPP
31 #define RYTHMOS_STEPPER_AS_MODEL_EVALUATOR_HPP
32 
33 
34 #include "Thyra_ResponseOnlyModelEvaluatorBase.hpp"
35 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
36 
37 #include "Rythmos_StepperBase.hpp"
38 #include "Rythmos_IntegratorBase.hpp"
39 #include "Teuchos_StandardMemberCompositionMacros.hpp"
40 #include "Teuchos_Assert.hpp"
41 
42 
43 namespace Rythmos {
44 
45 
52 template<class Scalar>
54  : virtual public Thyra::ResponseOnlyModelEvaluatorBase<Scalar>
55 {
56 public:
57 
60 
63 
65  void initialize(
66  const RCP<StepperBase<Scalar> > &stepper,
67  const RCP<IntegratorBase<Scalar> > &integrator,
68  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
69  );
70 
72 
75 
77  int Np() const;
79  int Ng() const;
81  RCP<const Thyra::VectorSpaceBase<Scalar> >
82  get_p_space(int l) const;
84  RCP<const Thyra::VectorSpaceBase<Scalar> >
85  get_g_space(int j) const;
87  Thyra::ModelEvaluatorBase::InArgs<Scalar> createInArgs() const;
88 
90 
91 private:
92 
95 
97  Thyra::ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
99  void evalModelImpl(
100  const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
101  const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
102  ) const;
103 
105 
106 private:
107 
108  // //////////////////////
109  // Private types
110 
111  typedef Array<RCP<const Thyra::VectorSpaceBase<Scalar> > > SpaceArray_t;
112 
113 
114  // //////////////////////
115  // Private data members
116 
117  RCP<StepperBase<Scalar> > stepper_;
118  RCP<IntegratorBase<Scalar> > integrator_;
119  Thyra::ModelEvaluatorBase::InArgs<Scalar> initialCondition_;
120 
121  int Np_;
122  int Ng_;
123 
124  SpaceArray_t g_space_;
125  SpaceArray_t p_space_;
126 
127  mutable Thyra::ModelEvaluatorBase::InArgs<Scalar> currentInitialCondition_;
128 
129 };
130 
131 
133 template<class Scalar>
134 RCP<StepperAsModelEvaluator<Scalar> >
135 stepperAsModelEvaluator(
136  const RCP<StepperBase<Scalar> > &stepper,
137  const RCP<IntegratorBase<Scalar> > &integrator,
138  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
139  )
140 {
141  using Teuchos::rcp;
142  RCP<StepperAsModelEvaluator<Scalar> >
143  stepperAsModelEvaluator = rcp(new StepperAsModelEvaluator<Scalar>());
144  stepperAsModelEvaluator->initialize(stepper,integrator,initialCondition);
145  return stepperAsModelEvaluator;
146 }
147 
148 
149 // ////////////////////////
150 // Definitions
151 
152 
153 // Constructors, Initialization, Misc.
154 
155 
156 template<class Scalar>
158  : Np_(0), Ng_(0)
159 {}
160 
161 
162 template<class Scalar>
164  const RCP<StepperBase<Scalar> > &stepper,
165  const RCP<IntegratorBase<Scalar> > &integrator,
166  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
167  )
168 {
169 
170 #ifdef HAVE_RYTHMOS_DEBUG
171  TEUCHOS_TEST_FOR_EXCEPT(is_null(stepper));
172  TEUCHOS_TEST_FOR_EXCEPT(is_null(stepper->getModel()));
173  TEUCHOS_TEST_FOR_EXCEPT(is_null(integrator));
174 #endif
175  stepper_ = stepper;
176  integrator_ = integrator;
177  initialCondition_ = initialCondition;
178  currentInitialCondition_ = initialCondition;
179 
180  const RCP<const Thyra::ModelEvaluator<Scalar> >
181  stepperModel = stepper_->getModel();
182 
183  Np_ = stepperModel->Np();
184  p_space_.clear();
185  for ( int l = 0; l < Np_; ++l ) {
186  p_space_.push_back(stepperModel->get_p_space(l));
187  }
188 
189  Ng_ = 1;
190  g_space_.clear();
191  g_space_.push_back(stepper_->getModel()->get_x_space());
192 
193 }
194 
195 
196 // Public functions overridden from ModelEvaulator
197 
198 
199 template<class Scalar>
201 {
202  return Np_;
203 }
204 
205 
206 template<class Scalar>
208 {
209  return Ng_;
210 }
211 
212 
213 template<class Scalar>
214 RCP<const Thyra::VectorSpaceBase<Scalar> >
216 {
217 #ifdef HAVE_RYTHMOS_DEBUG
218  TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
219 #endif
220  return p_space_[l];
221 }
222 
223 
224 template<class Scalar>
225 RCP<const Thyra::VectorSpaceBase<Scalar> >
227 {
228 #ifdef HAVE_RYTHMOS_DEBUG
229  TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( j, 0, Ng_ );
230 #endif
231  return g_space_[j];
232 }
233 
234 
235 template<class Scalar>
236 Thyra::ModelEvaluatorBase::InArgs<Scalar>
238 {
239  typedef Thyra::ModelEvaluatorBase MEB;
240  MEB::InArgsSetup<Scalar> inArgs;
241  inArgs.setModelEvalDescription(this->description());
242  inArgs.set_Np(Np_);
243  inArgs.setSupports(MEB::IN_ARG_t);
244  return inArgs;
245 }
246 
247 
248 // Private functions overridden from ModelEvaulatorDefaultBase
249 
250 
251 template<class Scalar>
252 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
254 {
255  typedef Thyra::ModelEvaluatorBase MEB;
256  MEB::OutArgsSetup<Scalar> outArgs;
257  outArgs.setModelEvalDescription(this->description());
258  outArgs.set_Np_Ng(Np_,Ng_);
259  return outArgs;
260 }
261 
262 
263 template<class Scalar>
264 void StepperAsModelEvaluator<Scalar>::evalModelImpl(
265  const Thyra::ModelEvaluatorBase::InArgs<Scalar>& inArgs,
266  const Thyra::ModelEvaluatorBase::OutArgs<Scalar>& outArgs
267  ) const
268 {
269 
270  using Teuchos::as;
271  using Teuchos::describe;
272  typedef Teuchos::VerboseObjectTempState<InterpolationBufferBase<Scalar> > VOTSSB;
273 
274  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_GEN_BEGIN(
275  "Rythmos::StepperAsModelEvaluator", inArgs, outArgs, Teuchos::null
276  );
277  VOTSSB integrator_outputTempState(integrator_,out,incrVerbLevel(verbLevel,-1));
278  //VOTSSB stepper_outputTempState(stepper_,out,incrVerbLevel(verbLevel,-1));
279 
280  // InArgs
281 
282  const Scalar finalTime = inArgs.get_t();
283  for ( int l = 0; l < Np_; ++l ) {
284  currentInitialCondition_.set_p(l,inArgs.get_p(l));
285  }
286 
287  // OutArgs
288 
289  RCP<Thyra::VectorBase<Scalar> >
290  g_out = outArgs.get_g(0);
291 
292  TEUCHOS_TEST_FOR_EXCEPT_MSG(
293  is_null(g_out), "You must ask for g(0) when you call this function!"
294  );
295 
296 #ifdef HAVE_RYTHMOS_DEBUG
297 
298  THYRA_ASSERT_VEC_SPACES(
299  "StepperAsModelEvaluator<Scalar>::evalModel(...)",
300  *g_out->space(), *stepper_->get_x_space() );
301 
302 #endif
303 
304  // Set up the integrator
305 
306  stepper_->setInitialCondition(currentInitialCondition_);
307  integrator_->setStepper(stepper_,finalTime);
308 
309  // Compute the desired response
310 
311  if (!is_null(g_out)) {
312 
313  // Get x and xdot at the end time
314  Array<Scalar> time_vec = Teuchos::tuple<Scalar>(finalTime);
315  Array<RCP<const Thyra::VectorBase<Scalar> > > x_vec;
316  integrator_->getFwdPoints( time_vec, &x_vec, 0, 0 );
317 
318  Thyra::V_V( g_out.ptr(), *x_vec[0] );
319 
320  }
321 
322  THYRA_MODEL_EVALUATOR_DECORATOR_EVAL_MODEL_END();
323 
324 }
325 
326 
327 } // namespace Rythmos
328 
329 
330 #endif // RYTHMOS_STEPPER_AS_MODEL_EVALUATOR_HPP
Base class for defining stepper functionality.
Abstract interface for time integrators.
Concrete Thyra::ModelEvaluator subclass that takes a parameterized stepper and turns it into a model ...
RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
void initialize(const RCP< StepperBase< Scalar > > &stepper, const RCP< IntegratorBase< Scalar > > &integrator, const Thyra::ModelEvaluatorBase::InArgs< Scalar > &initialCondition)
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const