9 #ifndef TEMPUS_TEST_STEADY_QUADRATIC_MODEL_IMPL_HPP
10 #define TEMPUS_TEST_STEADY_QUADRATIC_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_MultiVectorStdOps.hpp"
21 #include "Thyra_VectorStdOps.hpp"
22 #include "Thyra_DefaultMultiVectorProductVector.hpp"
27 namespace Tempus_Test {
29 template<
class Scalar>
33 isInitialized_ =
false;
39 useDfDpAsTangent_ =
false;
43 x_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
44 f_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim_);
46 p_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(np_);
47 g_space_ = Thyra::defaultSpmdVectorSpace<Scalar>(ng_);
49 setParameterList(pList_);
52 DxDp_space_ = Thyra::multiVectorProductVectorSpace(x_space_, np_);
55 template<
class Scalar>
65 template<
class Scalar>
75 template<
class Scalar>
76 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
84 template<
class Scalar>
85 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
93 template<
class Scalar>
94 Thyra::ModelEvaluatorBase::InArgs<Scalar>
98 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
99 "Error, setupInOutArgs_ must be called first!\n");
100 return nominalValues_;
104 template<
class Scalar>
105 Teuchos::RCP<Thyra::LinearOpWithSolveBase<Scalar> >
110 RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> > W_factory = this->get_W_factory();
111 RCP<Thyra::LinearOpBase<Scalar> > matrix = this->create_W_op();
118 RCP<Thyra::MultiVectorBase<Scalar> > multivec =
119 Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(matrix,
true);
121 RCP<Thyra::VectorBase<Scalar> > vec = Thyra::createMember(x_space_);
123 Thyra::DetachedVectorView<Scalar> vec_view( *vec );
126 V_V(multivec->col(0).ptr(),*vec);
129 RCP<Thyra::LinearOpWithSolveBase<Scalar> > W =
130 Thyra::linearOpWithSolve<Scalar>(
137 template<
class Scalar>
138 Teuchos::RCP<Thyra::LinearOpBase<Scalar> >
142 Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > matrix =
143 Thyra::createMembers(x_space_, dim_);
148 template<
class Scalar>
149 Teuchos::RCP<const Thyra::LinearOpWithSolveFactoryBase<Scalar> >
153 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > W_factory =
154 Thyra::defaultSerialDenseLinearOpWithSolveFactory<Scalar>();
159 template<
class Scalar>
160 Thyra::ModelEvaluatorBase::InArgs<Scalar>
172 template<
class Scalar>
173 Thyra::ModelEvaluatorBase::OutArgs<Scalar>
182 template<
class Scalar>
186 const Thyra::ModelEvaluatorBase::InArgs<Scalar> &inArgs,
187 const Thyra::ModelEvaluatorBase::OutArgs<Scalar> &outArgs
190 typedef Thyra::DefaultMultiVectorProductVector<Scalar> DMVPV;
192 using Thyra::VectorBase;
193 using Thyra::MultiVectorBase;
194 using Teuchos::rcp_dynamic_cast;
195 TEUCHOS_TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error,
196 "Error, setupInOutArgs_ must be called first!\n");
198 const RCP<const VectorBase<Scalar> > x_in = inArgs.get_x().assert_not_null();
199 Thyra::ConstDetachedVectorView<Scalar> x_in_view( *x_in );
202 const RCP<const VectorBase<Scalar> > p_in = inArgs.get_p(0);
203 if (p_in != Teuchos::null) {
204 Thyra::ConstDetachedVectorView<Scalar> p_in_view( *p_in );
208 RCP<const MultiVectorBase<Scalar> > DxDp_in, DxdotDp_in;
209 if (inArgs.get_p(1) != Teuchos::null)
211 rcp_dynamic_cast<const DMVPV>(inArgs.get_p(1))->getMultiVector();
212 if (inArgs.get_p(2) != Teuchos::null)
214 rcp_dynamic_cast<const DMVPV>(inArgs.get_p(2))->getMultiVector();
216 Scalar beta = inArgs.get_beta();
218 const RCP<VectorBase<Scalar> > f_out = outArgs.get_f();
219 const RCP<Thyra::LinearOpBase<Scalar> > W_out = outArgs.get_W_op();
220 RCP<Thyra::MultiVectorBase<Scalar> > DfDp_out;
221 Thyra::ModelEvaluatorBase::Derivative<Scalar> DfDp = outArgs.get_DfDp(0);
222 DfDp_out = DfDp.getMultiVector();
224 if (inArgs.get_x_dot().is_null()) {
227 if (!is_null(f_out)) {
228 Thyra::DetachedVectorView<Scalar> f_out_view( *f_out );
229 f_out_view[0] = x_in_view[0]*x_in_view[0] - b*b;
231 if (!is_null(W_out)) {
232 RCP<Thyra::MultiVectorBase<Scalar> > matrix =
233 Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
true);
234 Thyra::DetachedMultiVectorView<Scalar> matrix_view( *matrix );
235 matrix_view(0,0) = beta*2.0*x_in_view[0];
237 if (!is_null(DfDp_out)) {
238 Thyra::DetachedMultiVectorView<Scalar> DfDp_out_view( *DfDp_out );
239 DfDp_out_view(0,0) = -2.0*b;
242 if (useDfDpAsTangent_ && !is_null(DxDp_in)) {
243 Thyra::ConstDetachedMultiVectorView<Scalar> DxDp( *DxDp_in );
244 DfDp_out_view(0,0) += 2.0*x_in_view[0]*DxDp(0,0);
250 RCP<const VectorBase<Scalar> > x_dot_in;
251 x_dot_in = inArgs.get_x_dot().assert_not_null();
252 Scalar alpha = inArgs.get_alpha();
253 if (!is_null(f_out)) {
254 Thyra::DetachedVectorView<Scalar> f_out_view( *f_out );
255 Thyra::ConstDetachedVectorView<Scalar> x_dot_in_view( *x_dot_in );
256 f_out_view[0] = x_dot_in_view[0] - (x_in_view[0]*x_in_view[0] - b*b);
258 if (!is_null(W_out)) {
259 RCP<Thyra::MultiVectorBase<Scalar> > matrix =
260 Teuchos::rcp_dynamic_cast<Thyra::MultiVectorBase<Scalar> >(W_out,
true);
261 Thyra::DetachedMultiVectorView<Scalar> matrix_view( *matrix );
262 matrix_view(0,0) = alpha - beta*2.0*x_in_view[0];
264 if (!is_null(DfDp_out)) {
265 Thyra::DetachedMultiVectorView<Scalar> DfDp_out_view( *DfDp_out );
266 DfDp_out_view(0,0) = 2.0*b;
269 if (useDfDpAsTangent_ && !is_null(DxdotDp_in)) {
270 Thyra::ConstDetachedMultiVectorView<Scalar> DxdotDp( *DxdotDp_in );
271 DfDp_out_view(0,0) += DxdotDp(0,0);
273 if (useDfDpAsTangent_ && !is_null(DxDp_in)) {
274 Thyra::ConstDetachedMultiVectorView<Scalar> DxDp( *DxDp_in );
275 DfDp_out_view(0,0) += -2.0*x_in_view[0]*DxDp(0,0);
281 RCP<VectorBase<Scalar> > g_out = outArgs.get_g(0);
282 if (g_out != Teuchos::null)
283 Thyra::assign(g_out.ptr(), *x_in);
285 RCP<Thyra::MultiVectorBase<Scalar> > DgDp_out =
286 outArgs.get_DgDp(0,0).getMultiVector();
287 if (DgDp_out != Teuchos::null)
288 Thyra::assign(DgDp_out.ptr(), Scalar(0.0));
290 RCP<Thyra::MultiVectorBase<Scalar> > DgDx_out =
291 outArgs.get_DgDx(0).getMultiVector();
292 if (DgDx_out != Teuchos::null) {
293 Thyra::DetachedMultiVectorView<Scalar> DgDx_out_view( *DgDx_out );
294 DgDx_out_view(0,0) = 1.0;
298 template<
class Scalar>
299 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
303 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
306 else if (l == 1 || l == 2)
308 return Teuchos::null;
311 template<
class Scalar>
312 Teuchos::RCP<const Teuchos::Array<std::string> >
316 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( l, 0, Np_ );
317 Teuchos::RCP<Teuchos::Array<std::string> > p_strings =
318 Teuchos::rcp(
new Teuchos::Array<std::string>());
320 p_strings->push_back(
"Model Coefficient: b");
323 p_strings->push_back(
"DxDp");
325 p_strings->push_back(
"Dx_dotDp");
329 template<
class Scalar>
330 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> >
334 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( j, 0, Ng_ );
340 template<
class Scalar>
345 if (isInitialized_) {
351 Thyra::ModelEvaluatorBase::InArgsSetup<Scalar> inArgs;
352 inArgs.setModelEvalDescription(this->description());
353 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_t );
354 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_x );
355 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_beta );
356 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_x_dot );
357 inArgs.setSupports( Thyra::ModelEvaluatorBase::IN_ARG_alpha );
364 Thyra::ModelEvaluatorBase::OutArgsSetup<Scalar> outArgs;
365 outArgs.setModelEvalDescription(this->description());
366 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_f );
367 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_W_op );
368 outArgs.set_Np_Ng(Np_,Ng_);
369 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_DfDp,0,
370 Thyra::ModelEvaluatorBase::DERIV_MV_JACOBIAN_FORM );
371 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_DgDx,0,
372 Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM );
373 outArgs.setSupports( Thyra::ModelEvaluatorBase::OUT_ARG_DgDp,0,0,
374 Thyra::ModelEvaluatorBase::DERIV_MV_GRADIENT_FORM );
379 nominalValues_ = inArgs_;
380 nominalValues_.set_t(0.0);
381 const Teuchos::RCP<Thyra::VectorBase<Scalar> > x_ic = createMember(x_space_);
383 Thyra::DetachedVectorView<Scalar> x_ic_view( *x_ic );
386 nominalValues_.set_x(x_ic);
387 const Teuchos::RCP<Thyra::VectorBase<Scalar> > p_ic = createMember(p_space_);
389 Thyra::DetachedVectorView<Scalar> p_ic_view( *p_ic );
392 nominalValues_.set_p(0,p_ic);
394 const Teuchos::RCP<Thyra::VectorBase<Scalar> > x_dot_ic =
395 createMember(x_space_);
397 Thyra::DetachedVectorView<Scalar> x_dot_ic_view( *x_dot_ic );
398 x_dot_ic_view[0] = 0.0;
400 nominalValues_.set_x_dot(x_dot_ic);
402 isInitialized_ =
true;
406 template<
class Scalar>
412 using Teuchos::ParameterList;
413 Teuchos::RCP<ParameterList> tmpPL =
414 Teuchos::rcp(
new ParameterList(
"SteadyQuadraticModel"));
415 if (paramList != Teuchos::null) tmpPL = paramList;
416 tmpPL->validateParametersAndSetDefaults(*this->getValidParameters());
417 this->setMyParamList(tmpPL);
418 Teuchos::RCP<ParameterList> pl = this->getMyNonconstParamList();
419 useDfDpAsTangent_ = get<bool>(*pl,
"Use DfDp as Tangent");
420 b_ = get<Scalar>(*pl,
"Coeff b");
421 isInitialized_ =
false;
425 template<
class Scalar>
426 Teuchos::RCP<const Teuchos::ParameterList>
430 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
431 if (is_null(validPL)) {
432 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
433 pl->set(
"Use DfDp as Tangent",
false);
434 Teuchos::setDoubleParameter(
435 "Coeff b", 1.0,
"Coefficient b in model", &*pl);
442 #endif // TEMPUS_TEST_STEADY_QUADRATIC_MODEL_IMPL_HPP
Teuchos::RCP< Thyra::LinearOpBase< Scalar > > create_W_op() const
void evalModelImpl(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &inArgs_bar, const Thyra::ModelEvaluatorBase::OutArgs< Scalar > &outArgs_bar) const
Scalar getSteadyStateSolution() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_p_space(int l) const
Thyra::ModelEvaluatorBase::InArgs< Scalar > createInArgs() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_g_space(int j) const
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const ¶mList)
Teuchos::RCP< Thyra::LinearOpWithSolveBase< Scalar > > create_W() const
Thyra::ModelEvaluatorBase::InArgs< Scalar > getNominalValues() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_f_space() const
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Teuchos::RCP< const Thyra::LinearOpWithSolveFactoryBase< Scalar > > get_W_factory() const
Thyra::ModelEvaluatorBase::OutArgs< Scalar > createOutArgsImpl() const
void setupInOutArgs_() const
SteadyQuadraticModel(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Scalar getSteadyStateSolutionSensitivity() const