10 #ifndef TEMPUS_TEST_SINCOS_MODEL_IMPL_HPP
11 #define TEMPUS_TEST_SINCOS_MODEL_IMPL_HPP
13 #include "Teuchos_StandardParameterEntryValidators.hpp"
15 #include "Thyra_DefaultSpmdVectorSpace.hpp"
16 #include "Thyra_DetachedVectorView.hpp"
17 #include "Thyra_DetachedMultiVectorView.hpp"
18 #include "Thyra_DefaultSerialDenseLinearOpWithSolveFactory.hpp"
19 #include "Thyra_DefaultMultiVectorLinearOpWithSolve.hpp"
20 #include "Thyra_DefaultLinearOpSource.hpp"
21 #include "Thyra_VectorStdOps.hpp"
22 #include "Thyra_MultiVectorStdOps.hpp"
23 #include "Thyra_DefaultMultiVectorProductVector.hpp"
27 namespace Tempus_Test {
29 template <
class Scalar>
32 isInitialized_ =
false;
38 acceptModelParams_ =
false;
39 useDfDpAsTangent_ =
false;
49 x_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
50 f_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
52 p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(np_);
53 g_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(ng_);
55 setParameterList(pList_);
58 DxDp_space_ = Thyra::multiVectorProductVectorSpace(x_space_, np_);
61 template <
class Scalar>
66 "Error, setupInOutArgs_ must be called first!\n");
69 inArgs.
set_t(exact_t);
73 exact_x_view[0] = a_ + b_ * sin((f_ / L_) * t + phi_);
74 exact_x_view[1] = b_ * (f_ / L_) * cos((f_ / L_) * t + phi_);
76 inArgs.
set_x(exact_x);
80 exact_x_dot_view[0] = b_ * (f_ / L_) * cos((f_ / L_) * t + phi_);
82 -b_ * (f_ / L_) * (f_ / L_) * sin((f_ / L_) * t + phi_);
96 template <
class Scalar>
101 "Error, setupInOutArgs_ must be called first!\n");
103 if (!acceptModelParams_) {
112 inArgs.
set_t(exact_t);
117 exact_s_view[0] = 1.0;
118 exact_s_view[1] = 0.0;
121 exact_s_view[0] = (b / L) * t * cos((f / L) * t + phi);
122 exact_s_view[1] = (b / L) * cos((f / L) * t + phi) -
123 (b * f * t / (L * L)) * sin((f / L) * t + phi);
126 exact_s_view[0] = -(b * f * t / (L * L)) * cos((f / L) * t + phi);
127 exact_s_view[1] = -(b * f / (L * L)) * cos((f / L) * t + phi) +
128 (b * f * f * t / (L * L * L)) * sin((f / L) * t + phi);
131 inArgs.
set_x(exact_s);
136 exact_s_dot_view[0] = 0.0;
137 exact_s_dot_view[1] = 0.0;
140 exact_s_dot_view[0] = (b / L) * cos((f / L) * t + phi) -
141 (b * f * t / (L * L)) * sin((f / L) * t + phi);
142 exact_s_dot_view[1] =
143 -(2.0 * b * f / (L * L)) * sin((f / L) * t + phi) -
144 (b * f * f * t / (L * L * L)) * cos((f / L) * t + phi);
147 exact_s_dot_view[0] =
148 -(b * f / (L * L)) * cos((f / L) * t + phi) +
149 (b * f * f * t / (L * L * L)) * sin((f / L) * t + phi);
150 exact_s_dot_view[1] =
151 (2.0 * b * f * f / (L * L * L)) * sin((f / L) * t + phi) +
152 (b * f * f * f * t / (L * L * L * L)) * cos((f / L) * t + phi);
159 template <
class Scalar>
166 template <
class Scalar>
173 template <
class Scalar>
178 "Error, setupInOutArgs_ must be called first!\n");
179 return nominalValues_;
182 template <
class Scalar>
188 this->get_W_factory();
206 V_V(multivec->col(0).
ptr(), *vec);
212 V_V(multivec->col(1).
ptr(), *vec);
216 Thyra::linearOpWithSolve<Scalar>(*W_factory, matrix);
226 template <
class Scalar>
231 Thyra::createMembers(x_space_, dim_);
235 template <
class Scalar>
240 Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>();
255 template <
class Scalar>
265 template <
class Scalar>
273 template <
class Scalar>
280 using Teuchos::rcp_dynamic_cast;
284 "Error, setupInOutArgs_ must be called first!\n");
293 if (acceptModelParams_) {
295 inArgs.
get_p(0).assert_not_null();
303 if (acceptModelParams_) {
304 if (inArgs.
get_p(1) != Teuchos::null)
306 rcp_dynamic_cast<const DMVPV>(inArgs.
get_p(1))->getMultiVector();
307 if (inArgs.
get_p(2) != Teuchos::null)
309 rcp_dynamic_cast<const DMVPV>(inArgs.
get_p(2))->getMultiVector();
317 if (acceptModelParams_) {
326 f_out_view[0] = x_in_view[1];
327 f_out_view[1] = (f / L) * (f / L) * (a - x_in_view[0]);
334 matrix_view(0, 0) = 0.0;
335 matrix_view(0, 1) = +beta;
336 matrix_view(1, 0) = -beta * (f / L) * (f / L);
337 matrix_view(1, 1) = 0.0;
342 DfDp_out_view(0, 0) = 0.0;
343 DfDp_out_view(0, 1) = 0.0;
344 DfDp_out_view(0, 2) = 0.0;
345 DfDp_out_view(1, 0) = (f / L) * (f / L);
346 DfDp_out_view(1, 1) = (2.0 * f / (L * L)) * (a - x_in_view[0]);
347 DfDp_out_view(1, 2) = -(2.0 * f * f / (L * L * L)) * (a - x_in_view[0]);
350 if (useDfDpAsTangent_ && !
is_null(DxDp_in)) {
352 DfDp_out_view(0, 0) += DxDp(1, 0);
353 DfDp_out_view(0, 1) += DxDp(1, 1);
354 DfDp_out_view(0, 2) += DxDp(1, 2);
355 DfDp_out_view(1, 0) += -(f / L) * (f / L) * DxDp(0, 0);
356 DfDp_out_view(1, 1) += -(f / L) * (f / L) * DxDp(0, 1);
357 DfDp_out_view(1, 2) += -(f / L) * (f / L) * DxDp(0, 2);
364 x_dot_in = inArgs.
get_x_dot().assert_not_null();
369 f_out_view[0] = x_dot_in_view[0] - x_in_view[1];
370 f_out_view[1] = x_dot_in_view[1] - (f / L) * (f / L) * (a - x_in_view[0]);
377 matrix_view(0, 0) = alpha;
378 matrix_view(0, 1) = -beta;
379 matrix_view(1, 0) = +beta * (f / L) * (f / L);
380 matrix_view(1, 1) = alpha;
385 DfDp_out_view(0, 0) = 0.0;
386 DfDp_out_view(0, 1) = 0.0;
387 DfDp_out_view(0, 2) = 0.0;
388 DfDp_out_view(1, 0) = -(f / L) * (f / L);
389 DfDp_out_view(1, 1) = -(2.0 * f / (L * L)) * (a - x_in_view[0]);
390 DfDp_out_view(1, 2) = +(2.0 * f * f / (L * L * L)) * (a - x_in_view[0]);
393 if (useDfDpAsTangent_ && !
is_null(DxdotDp_in)) {
395 DfDp_out_view(0, 0) += DxdotDp(0, 0);
396 DfDp_out_view(0, 1) += DxdotDp(0, 1);
397 DfDp_out_view(0, 2) += DxdotDp(0, 2);
398 DfDp_out_view(1, 0) += DxdotDp(1, 0);
399 DfDp_out_view(1, 1) += DxdotDp(1, 1);
400 DfDp_out_view(1, 2) += DxdotDp(1, 2);
402 if (useDfDpAsTangent_ && !
is_null(DxDp_in)) {
404 DfDp_out_view(0, 0) += -DxDp(1, 0);
405 DfDp_out_view(0, 1) += -DxDp(1, 1);
406 DfDp_out_view(0, 2) += -DxDp(1, 2);
407 DfDp_out_view(1, 0) += (f / L) * (f / L) * DxDp(0, 0);
408 DfDp_out_view(1, 1) += (f / L) * (f / L) * DxDp(0, 1);
409 DfDp_out_view(1, 2) += (f / L) * (f / L) * DxDp(0, 2);
415 if (acceptModelParams_) {
417 if (g_out != Teuchos::null) Thyra::assign(g_out.
ptr(), *x_in);
420 outArgs.
get_DgDp(0, 0).getMultiVector();
421 if (DgDp_out != Teuchos::null) Thyra::assign(DgDp_out.ptr(), Scalar(0.0));
424 outArgs.
get_DgDx(0).getMultiVector();
425 if (DgDx_out != Teuchos::null) {
427 DgDx_out_view(0, 0) = 1.0;
428 DgDx_out_view(0, 1) = 0.0;
429 DgDx_out_view(1, 0) = 0.0;
430 DgDx_out_view(1, 1) = 1.0;
435 template <
class Scalar>
439 if (!acceptModelParams_) {
440 return Teuchos::null;
445 else if (l == 1 || l == 2)
447 return Teuchos::null;
450 template <
class Scalar>
454 if (!acceptModelParams_) {
455 return Teuchos::null;
461 p_strings->push_back(
"Model Coefficient: a");
462 p_strings->push_back(
"Model Coefficient: f");
463 p_strings->push_back(
"Model Coefficient: L");
466 p_strings->push_back(
"DxDp");
468 p_strings->push_back(
"Dx_dotDp");
472 template <
class Scalar>
482 template <
class Scalar>
485 if (isInitialized_) {
493 MEB::InArgsSetup<Scalar> inArgs;
494 inArgs.setModelEvalDescription(this->description());
495 inArgs.setSupports(MEB::IN_ARG_t);
496 inArgs.setSupports(MEB::IN_ARG_x);
497 inArgs.setSupports(MEB::IN_ARG_beta);
498 inArgs.setSupports(MEB::IN_ARG_x_dot);
499 inArgs.setSupports(MEB::IN_ARG_alpha);
500 if (acceptModelParams_) {
508 MEB::OutArgsSetup<Scalar> outArgs;
509 outArgs.setModelEvalDescription(this->description());
510 outArgs.setSupports(MEB::OUT_ARG_f);
511 outArgs.setSupports(MEB::OUT_ARG_W_op);
512 if (acceptModelParams_) {
513 outArgs.set_Np_Ng(Np_, Ng_);
514 outArgs.setSupports(MEB::OUT_ARG_DfDp, 0, MEB::DERIV_MV_JACOBIAN_FORM);
515 outArgs.setSupports(MEB::OUT_ARG_DgDp, 0, 0, MEB::DERIV_MV_JACOBIAN_FORM);
516 outArgs.setSupports(MEB::OUT_ARG_DgDx, 0, MEB::DERIV_MV_GRADIENT_FORM);
522 nominalValues_ = inArgs_;
524 nominalValues_.set_t(t0_ic_);
528 x_ic_view[0] = a_ + b_ * sin((f_ / L_) * t0_ic_ + phi_);
529 x_ic_view[1] = b_ * (f_ / L_) * cos((f_ / L_) * t0_ic_ + phi_);
531 nominalValues_.set_x(x_ic);
532 if (acceptModelParams_) {
540 nominalValues_.set_p(0, p_ic);
545 x_dot_ic_view[0] = b_ * (f_ / L_) * cos((f_ / L_) * t0_ic_ + phi_);
547 -b_ * (f_ / L_) * (f_ / L_) * sin((f_ / L_) * t0_ic_ + phi_);
549 nominalValues_.set_x_dot(x_dot_ic);
552 isInitialized_ =
true;
555 template <
class Scalar>
563 if (paramList != Teuchos::null) tmpPL = paramList;
565 this->setMyParamList(tmpPL);
567 bool acceptModelParams = get<bool>(*pl,
"Accept model parameters");
568 bool haveIC = get<bool>(*pl,
"Provide nominal values");
569 bool useDfDpAsTangent = get<bool>(*pl,
"Use DfDp as Tangent");
570 if ((acceptModelParams != acceptModelParams_) || (haveIC != haveIC_)) {
571 isInitialized_ =
false;
573 acceptModelParams_ = acceptModelParams;
575 useDfDpAsTangent_ = useDfDpAsTangent;
576 a_ = get<Scalar>(*pl,
"Coeff a");
577 f_ = get<Scalar>(*pl,
"Coeff f");
578 L_ = get<Scalar>(*pl,
"Coeff L");
579 x0_ic_ = get<Scalar>(*pl,
"IC x0");
580 x1_ic_ = get<Scalar>(*pl,
"IC x1");
581 t0_ic_ = get<Scalar>(*pl,
"IC t0");
582 calculateCoeffFromIC_();
586 template <
class Scalar>
593 pl->
set(
"Accept model parameters",
false);
594 pl->
set(
"Provide nominal values",
true);
595 pl->
set(
"Use DfDp as Tangent",
false);
596 pl->
set<std::string>(
"Output File Name",
"Tempus_BDF2_SinCos");
597 Teuchos::setDoubleParameter(
"Coeff a", 0.0,
"Coefficient a in model", &*pl);
598 Teuchos::setDoubleParameter(
"Coeff f", 1.0,
"Coefficient f in model", &*pl);
599 Teuchos::setDoubleParameter(
"Coeff L", 1.0,
"Coefficient L in model", &*pl);
600 Teuchos::setDoubleParameter(
"IC x0", 0.0,
"Initial Condition for x0", &*pl);
601 Teuchos::setDoubleParameter(
"IC x1", 1.0,
"Initial Condition for x1", &*pl);
602 Teuchos::setDoubleParameter(
"IC t0", 0.0,
"Initial time t0", &*pl);
603 Teuchos::setIntParameter(
"Number of Time Step Sizes", 1,
604 "Number time step sizes for convergence study",
611 template <
class Scalar>
614 phi_ = atan(((f_ / L_) / x1_ic_) * (x0_ic_ - a_)) - (f_ / L_) * t0_ic_;
615 b_ = x1_ic_ / ((f_ / L_) * cos((f_ / L_) * t0_ic_ + phi_));
618 template <
class Scalar>
624 this->get_W_factory();
642 V_V(multivec->col(0).
ptr(), *vec);
648 V_V(multivec->col(1).
ptr(), *vec);
652 Thyra::linearOpWithSolve<Scalar>(*W_factory, matrix);
656 template <
class Scalar>
661 Thyra::createMembers(this->f_space_, this->dim_);
665 template <
class Scalar>
676 inArgs.setModelEvalDescription(this->description());
680 template <
class Scalar>
685 MEB::OutArgsSetup<Scalar> outArgs;
686 outArgs.setModelEvalDescription(this->description());
689 outArgs.setSupports(MEB::OUT_ARG_W_op);
690 outArgs.set_Np_Ng(this->Np_, 0);
694 template <
class Scalar>
700 using Teuchos::rcp_dynamic_cast;
704 "Error, setupInOutArgs_ must be called first!\n");
713 if (this->acceptModelParams_) {
715 inArgs.
get_p(0).assert_not_null();
732 matrix_view(0, 0) = 0.0;
733 matrix_view(1, 0) = +beta;
734 matrix_view(0, 1) = -beta * (f / L) * (f / L);
735 matrix_view(1, 1) = 0.0;
742 x_dot_in = inArgs.
get_x_dot().assert_not_null();
749 matrix_view(0, 0) = alpha;
750 matrix_view(1, 0) = -beta;
751 matrix_view(0, 1) = +beta * (f / L) * (f / L);
752 matrix_view(1, 1) = alpha;
759 #endif // TEMPUS_TEST_SINCOS_MODEL_IMPL_HPP
bool is_null(const boost::shared_ptr< T > &p)
Derivative< Scalar > get_DfDp(int l) const
RCP< const VectorBase< Scalar > > get_x_dot() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Evaluation< VectorBase< Scalar > > get_g(int j) const
Evaluation< VectorBase< Scalar > > get_f() const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Derivative< Scalar > get_DgDp(int j, int l) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
void set_x(const RCP< const VectorBase< Scalar > > &x)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSolution(double t) const
SinCosModel(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
void set_x_dot(const RCP< const VectorBase< Scalar > > &x_dot)
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const
void calculateCoeffFromIC_()
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
void setupInOutArgs_() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getExactSensSolution(int j, double t) const
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
RCP< LinearOpBase< Scalar > > get_W_op() const
RCP< const VectorBase< Scalar > > get_x() const
RCP< const VectorBase< Scalar > > get_p(int l) const
RCP< MultiVectorBase< Scalar > > getMultiVector() const
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const ¶mList)
Derivative< Scalar > get_DgDx(int j) const