9 #ifndef TEMPUS_TEST_VANDERPOL_MODEL_IMPL_HPP
10 #define TEMPUS_TEST_VANDERPOL_MODEL_IMPL_HPP
12 #include "Teuchos_StandardParameterEntryValidators.hpp"
14 #include "Thyra_DefaultSpmdVectorSpace.hpp"
15 #include "Thyra_DetachedVectorView.hpp"
16 #include "Thyra_DetachedMultiVectorView.hpp"
17 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
18 #include "Thyra_DefaultMultiVectorLinearOpWithSolve.hpp"
19 #include "Thyra_DefaultLinearOpSource.hpp"
20 #include "Thyra_VectorStdOps.hpp"
25 namespace Tempus_Test {
27 template<
class Scalar>
31 isInitialized_ =
false;
37 acceptModelParams_ =
false;
45 x_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
46 f_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
48 p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(np_);
49 g_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(ng_);
51 setParameterList(pList_);
54 template<
class Scalar>
55 Thyra::ModelEvaluatorBase::InArgs<Scalar>
59 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
60 "Error - No exact solution for van der Pol problem!\n");
64 template<
class Scalar>
65 Thyra::ModelEvaluatorBase::InArgs<Scalar>
69 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
70 "Error - No exact sensitivities for van der Pol problem!\n");
74 template<
class Scalar>
75 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
83 template<
class Scalar>
84 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
92 template<
class Scalar>
93 Thyra::ModelEvaluatorBase::InArgs<Scalar>
97 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
98 "Error, setupInOutArgs_ must be called first!\n");
99 return nominalValues_;
103 template<
class Scalar>
104 Teuchos::RCP<Thyra::LinearOpWithSolveBase<Scalar> >
109 RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> > W_factory = this->get_W_factory();
110 RCP<Thyra::LinearOpBase<Scalar> > matrix = this->create_W_op();
117 RCP<Thyra::MultiVectorBase<Scalar> > multivec = Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(matrix,
true);
119 RCP<Thyra::VectorBase<Scalar> > vec = Thyra::createMember(x_space_);
121 Thyra::DetachedVectorView<Scalar> vec_view( *vec );
125 V_V(multivec->col(0).ptr(),*vec);
127 Thyra::DetachedVectorView<Scalar> vec_view( *vec );
131 V_V(multivec->col(1).ptr(),*vec);
134 RCP<Thyra::LinearOpWithSolveBase<Scalar> > W =
135 Thyra::linearOpWithSolve<Scalar>(*W_factory, matrix);
141 template<
class Scalar>
142 Teuchos::RCP<Thyra::LinearOpBase<Scalar> >
146 Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > matrix = Thyra::createMembers(x_space_, dim_);
151 template<
class Scalar>
152 Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> >
156 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > W_factory =
157 Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>();
162 template<
class Scalar>
163 Thyra::ModelEvaluatorBase::InArgs<Scalar>
175 template<
class Scalar>
176 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
185 template<
class Scalar>
189 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
190 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
194 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
195 "Error, setupInOutArgs_ must be called first!\n");
197 const RCP<const Thyra::VectorBase<Scalar> > x_in =
198 inArgs.get_x().assert_not_null();
199 Thyra::ConstDetachedVectorView<Scalar> x_in_view( *x_in );
202 Scalar epsilon = epsilon_;
203 if (acceptModelParams_) {
204 const RCP<const Thyra::VectorBase<Scalar> > p_in =
205 inArgs.get_p(0).assert_not_null();
206 Thyra::ConstDetachedVectorView<Scalar> p_in_view( *p_in );
207 epsilon = p_in_view[0];
210 Scalar beta = inArgs.get_beta();
212 const RCP<Thyra::VectorBase<Scalar> > f_out = outArgs.get_f();
213 const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op();
214 RCP<Thyra::MultiVectorBase<Scalar> > DfDp_out;
215 if (acceptModelParams_) {
216 Thyra::ModelEvaluatorBase::Derivative<Scalar> DfDp = outArgs.get_DfDp(0);
217 DfDp_out = DfDp.getMultiVector();
220 if (inArgs.get_x_dot().is_null()) {
223 if (!is_null(f_out)) {
224 Thyra::DetachedVectorView<Scalar> f_out_view( *f_out );
225 f_out_view[0] = x_in_view[1];
227 ((1.0-x_in_view[0]*x_in_view[0])*x_in_view[1]-x_in_view[0])/epsilon;
229 if (!is_null(W_out)) {
230 RCP<Thyra::MultiVectorBase<Scalar> > matrix =
231 Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
true);
232 Thyra::DetachedMultiVectorView<Scalar> matrix_view( *matrix );
233 matrix_view(0,0) = 0.0;
234 matrix_view(0,1) = +beta;
236 -beta*(2.0*x_in_view[0]*x_in_view[1]+1.0)/epsilon;
238 beta*(1.0 - x_in_view[0]*x_in_view[0])/epsilon;
241 if (!is_null(DfDp_out)) {
242 Thyra::DetachedMultiVectorView<Scalar> DfDp_out_view( *DfDp_out );
243 DfDp_out_view(0,0) = 0.0;
245 -((1.0-x_in_view[0]*x_in_view[0])*x_in_view[1]-x_in_view[0])
251 RCP<const Thyra::VectorBase<Scalar> > x_dot_in;
252 x_dot_in = inArgs.get_x_dot().assert_not_null();
253 Scalar alpha = inArgs.get_alpha();
254 if (!is_null(f_out)) {
255 Thyra::DetachedVectorView<Scalar> f_out_view( *f_out );
256 Thyra::ConstDetachedVectorView<Scalar> x_dot_in_view( *x_dot_in );
257 f_out_view[0] = x_dot_in_view[0] - x_in_view[1];
258 f_out_view[1] = x_dot_in_view[1]
259 - ((1.0-x_in_view[0]*x_in_view[0])*x_in_view[1]-x_in_view[0])/epsilon;
261 if (!is_null(W_out)) {
262 RCP<Thyra::MultiVectorBase<Scalar> > matrix =
263 Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
true);
264 Thyra::DetachedMultiVectorView<Scalar> matrix_view( *matrix );
265 matrix_view(0,0) = alpha;
266 matrix_view(0,1) = -beta;
268 beta*(2.0*x_in_view[0]*x_in_view[1]+1.0)/epsilon;
269 matrix_view(1,1) = alpha
270 - beta*(1.0 - x_in_view[0]*x_in_view[0])/epsilon;
273 if (!is_null(DfDp_out)) {
274 Thyra::DetachedMultiVectorView<Scalar> DfDp_out_view( *DfDp_out );
275 DfDp_out_view(0,0) = 0.0;
277 ((1.0-x_in_view[0]*x_in_view[0])*x_in_view[1]-x_in_view[0])
283 template<
class Scalar>
284 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
288 if (!acceptModelParams_) {
289 return Teuchos::null;
291 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
295 template<
class Scalar>
296 Teuchos::RCP<const Teuchos::Array<std::string> >
300 if (!acceptModelParams_) {
301 return Teuchos::null;
303 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
304 Teuchos::RCP<Teuchos::Array<std::string> > p_strings =
305 Teuchos::rcp(
new Teuchos::Array<std::string>());
306 p_strings->push_back(
"Model Coefficient: epsilon");
310 template<
class Scalar>
311 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
315 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( j, 0, Ng_ );
321 template<
class Scalar>
326 if (isInitialized_) {
332 Thyra::ModelEvaluatorBase::InArgsSetup<Scalar> inArgs;
333 inArgs.setModelEvalDescription(this->description());
334 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_t );
335 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_x );
336 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_beta );
337 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_x_dot );
338 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_alpha );
339 if (acceptModelParams_) {
347 Thyra::ModelEvaluatorBase::OutArgsSetup<Scalar> outArgs;
348 outArgs.setModelEvalDescription(this->description());
349 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_f );
350 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_W_op );
351 if (acceptModelParams_) {
352 outArgs.set_Np_Ng(Np_,Ng_);
353 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_DfDp,0,
354 Thyra::ModelEvaluatorBase::DERIV_MV_JACOBIAN_FORM );
360 nominalValues_ = inArgs_;
364 nominalValues_.set_t(t0_ic_);
365 const RCP<Thyra::VectorBase<Scalar> > x_ic = createMember(x_space_);
367 Thyra::DetachedVectorView<Scalar> x_ic_view( *x_ic );
368 x_ic_view[0] = x0_ic_;
369 x_ic_view[1] = x1_ic_;
371 nominalValues_.set_x(x_ic);
372 if (acceptModelParams_) {
373 const RCP<Thyra::VectorBase<Scalar> > p_ic = createMember(p_space_);
375 Thyra::DetachedVectorView<Scalar> p_ic_view( *p_ic );
376 p_ic_view[0] = epsilon_;
378 nominalValues_.set_p(0,p_ic);
380 const RCP<Thyra::VectorBase<Scalar> > x_dot_ic = createMember(x_space_);
382 Thyra::DetachedVectorView<Scalar> x_dot_ic_view( *x_dot_ic );
383 x_dot_ic_view[0] = x1_ic_;
384 x_dot_ic_view[1] = ((1.0-x0_ic_*x0_ic_)*x1_ic_-x0_ic_)/epsilon_;
386 nominalValues_.set_x_dot(x_dot_ic);
389 isInitialized_ =
true;
393 template<
class Scalar>
399 using Teuchos::ParameterList;
400 Teuchos::RCP<ParameterList> tmpPL = Teuchos::rcp(
new ParameterList(
"VanDerPolModel"));
401 if (paramList != Teuchos::null) tmpPL = paramList;
402 tmpPL->validateParametersAndSetDefaults(*this->getValidParameters());
403 this->setMyParamList(tmpPL);
404 Teuchos::RCP<ParameterList> pl = this->getMyNonconstParamList();
405 bool acceptModelParams = get<bool>(*pl,
"Accept model parameters");
406 bool haveIC = get<bool>(*pl,
"Provide nominal values");
407 if ( (acceptModelParams != acceptModelParams_) ||
410 isInitialized_ =
false;
412 acceptModelParams_ = acceptModelParams;
414 epsilon_ = get<Scalar>(*pl,
"Coeff epsilon");
415 x0_ic_ = get<Scalar>(*pl,
"IC x0");
416 x1_ic_ = get<Scalar>(*pl,
"IC x1");
417 t0_ic_ = get<Scalar>(*pl,
"IC t0");
421 template<
class Scalar>
422 Teuchos::RCP<const Teuchos::ParameterList>
426 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
427 if (is_null(validPL)) {
428 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
429 pl->set(
"Accept model parameters",
false);
430 pl->set(
"Provide nominal values",
true);
431 Teuchos::setDoubleParameter(
432 "Coeff epsilon", 1.0e-06,
"Coefficient a in model", &*pl);
433 Teuchos::setDoubleParameter(
434 "IC x0", 2.0,
"Initial Condition for x0", &*pl);
435 Teuchos::setDoubleParameter(
436 "IC x1", 0.0,
"Initial Condition for x1", &*pl);
437 Teuchos::setDoubleParameter(
438 "IC t0", 0.0,
"Initial time t0", &*pl);
439 Teuchos::setIntParameter(
440 "Number of Time Step Sizes", 1,
"Number time step sizes for convergence study", &*pl);
447 #endif // TEMPUS_TEST_VANDERPOL_MODEL_IMPL_HPP
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSolution(double t) const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
VanDerPolModel(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const ¶mList)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSensSolution(int j, double t) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
void setupInOutArgs_() const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const