Teuchos::RCP< Thyra::VectorBase<Scalar> > Thyra::createMember( const VectorSpaceBase<Scalar> &vs, const std::string &label ) { return createMember(Teuchos::rcpFromRef(vs), label); }
void DefaultDiagonalLinearOp<Scalar>::initialize( const RCP<const VectorSpaceBase<Scalar> > &space ) { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPT(space.get()==NULL); #endif initialize(createMember(space)); // Note that the space is guaranteed to be remembered here! }
TEUCHOS_UNIT_TEST( Rythmos_ForwardSensitivityExplicitModelEvaluator, spaces ) { RCP<ForwardSensitivityExplicitModelEvaluator<double> > model = forwardSensitivityExplicitModelEvaluator<double>(); RCP<SinCosModel> innerModel = sinCosModel(); { RCP<ParameterList> pl = Teuchos::parameterList(); pl->set("Accept model parameters",true); pl->set("Implicit model formulation",false); innerModel->setParameterList(pl); } model->initializeStructure(innerModel, 0 ); // x_space: { RCP<const Thyra::VectorSpaceBase<double> > x_space = model->get_x_space(); TEST_EQUALITY_CONST( x_space->dim(), 6 ); RCP<VectorBase<double> > x = createMember(*x_space); RCP<Thyra::DefaultMultiVectorProductVector<double> > x_bar = Teuchos::rcp_dynamic_cast<Thyra::DefaultMultiVectorProductVector<double> >( x, false ); TEST_ASSERT( !is_null(x_bar) ); RCP<Thyra::MultiVectorBase<double> > X = x_bar->getNonconstMultiVector(); // Test that the vectors produced have the right number of columns TEST_EQUALITY_CONST( X->domain()->dim(), 3 ); // Test that the vectors produced have the right number of rows TEST_EQUALITY_CONST( X->range()->dim(), 2 ); } // f_space: { RCP<const Thyra::VectorSpaceBase<double> > f_space = model->get_f_space(); TEST_EQUALITY_CONST( f_space->dim(), 6 ); RCP<VectorBase<double> > f = createMember(*f_space); RCP<Thyra::DefaultMultiVectorProductVector<double> > f_bar = Teuchos::rcp_dynamic_cast<Thyra::DefaultMultiVectorProductVector<double> >( f, false ); TEST_ASSERT( !is_null(f_bar) ); RCP<Thyra::MultiVectorBase<double> > F = f_bar->getNonconstMultiVector(); // Test that the vectors produced have the right number of columns TEST_EQUALITY_CONST( F->domain()->dim(), 3 ); // Test that the vectors produced have the right number of rows TEST_EQUALITY_CONST( F->range()->dim(), 2 ); } }
Teuchos::RCP<VectorBase<Scalar> > readVectorFromFile( const MultiVectorFileIOBase<Scalar> &fileIO, const std::string &fileNameBase, const VectorSpaceBase<Scalar> &vecSpc ) { Teuchos::RCP<VectorBase<Scalar> > v = createMember(vecSpc); fileIO.readMultiVectorFromFile(fileNameBase,&*v); return v; }
ModelEvaluatorBase::InArgs<double> PolynomialModel::getExactSolution(double t) const { TEST_FOR_EXCEPTION( !isInitialized_, std::logic_error, "Error, setPolynomial must be called first!\n" ); ModelEvaluatorBase::InArgs<double> inArgs = inArgs_; double exact_t = t; inArgs.set_t(exact_t); RCP<VectorBase<double> > exact_x = createMember(x_space_); { // scope to delete DetachedVectorView Thyra::DetachedVectorView<double> exact_x_view(*exact_x); exact_x_view[0] = 0.0; // TODO } inArgs.set_x(exact_x); return(inArgs); }
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultSpmdVector, getMultiVectorLocalData, Scalar ) { out << "Test that we can grab MV data from Vector ...\n"; typedef typename ScalarTraits<Scalar>::magnitudeType ScalarMag; RCP<const VectorSpaceBase<Scalar> > vs = createSpmdVectorSpace<Scalar>(g_localDim); const int procRank = Teuchos::DefaultComm<Ordinal>::getComm()->getRank(); RCP<VectorBase<Scalar> > v = createMember(*vs); const ScalarMag tol = 100.0*ScalarTraits<Scalar>::eps(); const Ordinal globalOffset = procRank * g_localDim; out << "Get non-const MV local data and set it ...\n"; { ArrayRCP<Scalar> localValues; Ordinal leadingDim = -1; rcp_dynamic_cast<SpmdMultiVectorBase<Scalar> >(v,true)->getNonconstLocalData( outArg(localValues), outArg(leadingDim)); TEST_EQUALITY(localValues.size(), g_localDim); TEST_EQUALITY(leadingDim, g_localDim); for (int i = 0; i < localValues.size(); ++i) { localValues[i] = globalOffset + i + 1; } } const Ordinal n = vs->dim(); TEST_FLOATING_EQUALITY(sum<Scalar>(*v), as<Scalar>((n*(n+1))/2.0), tol); out << "Get const MV local data and check it ...\n"; { ArrayRCP<const Scalar> localValues; Ordinal leadingDim = -1; rcp_dynamic_cast<const SpmdMultiVectorBase<Scalar> >(v,true)->getLocalData( outArg(localValues), outArg(leadingDim)); TEST_EQUALITY(localValues.size(), g_localDim); TEST_EQUALITY(leadingDim, g_localDim); for (int i = 0; i < localValues.size(); ++i) { TEST_EQUALITY(localValues[i], as<Scalar>(globalOffset + i + 1)); } } }
TEUCHOS_UNIT_TEST( Rythmos_BackwardEulerStepper, restart ) { RCP<const MomentoBase<double> > stepper_momento; // place to store solution at time = 1.0 RCP<const VectorBase<double> > x_norestart; RCP<const VectorBase<double> > xdot_norestart; // Create Backward Euler stepper // Step to t = 0.5, pull out the momento, then step the rest of the way to t = 1.0. { RCP<SinCosModel> model = sinCosModel(true); Thyra::ModelEvaluatorBase::InArgs<double> model_ic = model->getNominalValues(); RCP<Thyra::NonlinearSolverBase<double> > neSolver = timeStepNonlinearSolver<double>(); RCP<BackwardEulerStepper<double> > stepper = backwardEulerStepper<double>(model,neSolver); stepper->setInitialCondition(model_ic); double dt = 0.1; // Step to t=0.5 for (int i=0 ; i<5 ; ++i) { double dt_taken = stepper->takeStep(dt,STEP_TYPE_FIXED); TEST_ASSERT( dt_taken == dt ); } // Pull out the momento stepper_momento = stepper->getMomento(); // Step to t=1.0 for (int i=0 ; i<5 ; ++i) { double dt_taken = stepper->takeStep(dt,STEP_TYPE_FIXED); TEST_ASSERT( dt_taken == dt ); } { // Pull out the final solution Array<double> time_vec; time_vec.push_back(1.0); Array<RCP<const VectorBase<double> > > x_vec; Array<RCP<const VectorBase<double> > > xdot_vec; Array<double> accuracy_vec; stepper->getPoints(time_vec,&x_vec,&xdot_vec,&accuracy_vec); x_norestart = x_vec[0]; xdot_norestart = xdot_vec[0]; } } // Create a new Backward Euler Stepper { RCP<BackwardEulerStepper<double> > stepper = backwardEulerStepper<double>(); RCP<SinCosModel> model = sinCosModel(true); RCP<Thyra::NonlinearSolverBase<double> > neSolver = timeStepNonlinearSolver<double>(); // Put the momento back into the stepper stepper->setMomento(stepper_momento.ptr(),model,neSolver); double dt = 0.1; // Step to t=1.0 for (int i=0 ; i<5 ; ++i) { double dt_taken = stepper->takeStep(dt,STEP_TYPE_FIXED); TEST_ASSERT( dt_taken == dt ); } { // Pull out the final solution after restart Array<double> time_vec; time_vec.push_back(1.0); Array<RCP<const VectorBase<double> > > x_vec; Array<RCP<const VectorBase<double> > > xdot_vec; Array<double> accuracy_vec; stepper->getPoints(time_vec,&x_vec,&xdot_vec,&accuracy_vec); RCP<const VectorBase<double> > x_restart = x_vec[0]; RCP<const VectorBase<double> > xdot_restart = xdot_vec[0]; // Verify that x_restart == x_norestart RCP<VectorBase<double> > x_diff = createMember(x_restart->space()); V_VmV( x_diff.ptr(), *x_norestart, *x_restart ); double x_normDiff = norm(*x_diff); double tol = 1.0e-10; TEST_COMPARE( x_normDiff, <, tol ); // Verify that xdot_restart == xdot_norestart RCP<VectorBase<double> > xdot_diff = createMember(x_restart->space()); V_VmV( xdot_diff.ptr(), *xdot_norestart, *xdot_restart ); double xdot_normDiff = norm(*xdot_diff); TEST_COMPARE( xdot_normDiff, <, tol ); } } }
void QScriptValueImpl::setProperty(QScriptNameIdImpl *nameId, const QScriptValueImpl &value, const QScriptValue::PropertyFlags &flags) { if (!isObject()) return; QScriptValueImpl base; QScript::Member member; QScriptValue::ResolveFlags mode = QScriptValue::ResolveLocal; // if we are not setting a setter or getter, look in prototype too if (!(flags & (QScriptValue::PropertyGetter | QScriptValue::PropertySetter))) mode |= QScriptValue::ResolvePrototype; if (resolve(nameId, &member, &base, mode, QScript::ReadWrite)) { // we resolved an existing property with that name if (flags & (QScriptValue::PropertyGetter | QScriptValue::PropertySetter)) { // setting the getter or setter of a property in this object if (member.isNativeProperty()) { if (value.isValid()) { qWarning("QScriptValue::setProperty() failed: " "cannot set getter or setter of native property `%s'", qPrintable(nameId->s)); } return; } if (member.isSetter()) { // the property we resolved is a setter if (!(flags & QScriptValue::PropertySetter) && !member.isGetter()) { // find the getter, if not, create one if (!m_object_value->findGetter(&member)) { if (!value.isValid()) return; // don't create property for invalid value createMember(nameId, &member, flags); } } } else if (member.isGetter()) { // the property we resolved is a getter if (!(flags & QScriptValue::PropertyGetter)) { // find the setter, if not, create one if (!m_object_value->findSetter(&member)) { if (!value.isValid()) return; // don't create property for invalid value createMember(nameId, &member, flags); } } } else { // the property is a normal property -- change the flags uint newFlags = flags & ~QScript::Member::InternalRange; newFlags |= QScript::Member::ObjectProperty; member.resetFlags(newFlags); base.m_object_value->m_members[member.id()].resetFlags(newFlags); } Q_ASSERT(member.isValid()); if (!value.isValid()) { // remove the property removeMember(member); return; } } else { // setting the value if (member.isGetterOrSetter()) { // call the setter QScriptValueImpl setter; if (member.isObjectProperty() && !member.isSetter()) { if (!base.m_object_value->findSetter(&member)) { qWarning("QScriptValue::setProperty() failed: " "property '%s' has a getter but no setter", qPrintable(nameId->s)); return; } } base.get(member, &setter); setter.call(*this, QScriptValueImplList() << value); return; } else { if (base.m_object_value != m_object_value) { if (!value.isValid()) return; // don't create property for invalid value createMember(nameId, &member, flags); base = *this; } else { if (!value.isValid()) { // remove the property removeMember(member); return; } } if (flags != QScriptValue::KeepExistingFlags) { // change flags if (member.isNativeProperty()) { qWarning("QScriptValue::setProperty(%s): " "cannot change flags of a native property", qPrintable(nameId->s)); } else { uint newFlags = member.flags() & QScript::Member::InternalRange; newFlags |= flags & ~QScript::Member::InternalRange; base.m_object_value->m_members[member.id()].resetFlags(newFlags); } } } } } else { // property does not exist if (!value.isValid()) return; // don't create property for invalid value createMember(nameId, &member, flags & ~QScript::Member::InternalRange); base = *this; } base.put(member, value); }
TEUCHOS_UNIT_TEST( Rythmos_GlobalErrorEstimator, SinCos ) { typedef Teuchos::ScalarTraits<double> ST; // Forward Solve, storing data in linear interpolation buffer int storageLimit = 100; double finalTime = 0.1; double dt = 0.1; RCP<IntegratorBuilder<double> > ib = integratorBuilder<double>(); { RCP<ParameterList> ibPL = Teuchos::parameterList(); ibPL->sublist("Integrator Settings").sublist("Integrator Selection").set("Integrator Type","Default Integrator"); ibPL->sublist("Integrator Settings").set("Final Time",finalTime); ibPL->sublist("Integration Control Strategy Selection").set("Integration Control Strategy Type","Simple Integration Control Strategy"); ibPL->sublist("Integration Control Strategy Selection").sublist("Simple Integration Control Strategy").set("Take Variable Steps",false); ibPL->sublist("Integration Control Strategy Selection").sublist("Simple Integration Control Strategy").set("Fixed dt",dt); ibPL->sublist("Stepper Settings").sublist("Stepper Selection").set("Stepper Type","Backward Euler"); //ibPL->sublist("Stepper Settings").sublist("Stepper Selection").set("Stepper Type","Implicit RK"); //ibPL->sublist("Stepper Settings").sublist("Runge Kutta Butcher Tableau Selection").set("Runge Kutta Butcher Tableau Type","Backward Euler"); ibPL->sublist("Interpolation Buffer Settings").sublist("Trailing Interpolation Buffer Selection").set("Interpolation Buffer Type","Interpolation Buffer"); ibPL->sublist("Interpolation Buffer Settings").sublist("Trailing Interpolation Buffer Selection").sublist("Interpolation Buffer").set("StorageLimit",storageLimit); ibPL->sublist("Interpolation Buffer Settings").sublist("Interpolator Selection").set("Interpolator Type","Linear Interpolator"); ib->setParameterList(ibPL); } RCP<SinCosModel> fwdModel = sinCosModel(true); // implicit formulation Thyra::ModelEvaluatorBase::InArgs<double> fwdIC = fwdModel->getNominalValues(); RCP<Thyra::NonlinearSolverBase<double> > fwdNLSolver = timeStepNonlinearSolver<double>(); RCP<IntegratorBase<double> > fwdIntegrator = ib->create(fwdModel,fwdIC,fwdNLSolver); RCP<const VectorBase<double> > x_final; { Array<double> time_vec; time_vec.push_back(finalTime); Array<RCP<const Thyra::VectorBase<double> > > x_final_array; fwdIntegrator->getFwdPoints(time_vec,&x_final_array,NULL,NULL); x_final = x_final_array[0]; } // Verify x_final is correct { // Defaults from SinCos Model: double f = 1.0; double L = 1.0; double a = 0.0; double x_ic_0 = 0.0; double x_ic_1 = 1.0; double x_0 = dt/(1.0+std::pow(dt*f/L,2))*(x_ic_0/dt+x_ic_1+dt*std::pow(f/L,2)*a); double x_1 = dt/(1.0+std::pow(dt*f/L,2))*(-std::pow(f/L,2)*x_ic_0+x_ic_1/dt+std::pow(f/L,2)*a); double tol = 1.0e-10; Thyra::ConstDetachedVectorView<double> x_final_view( *x_final ); TEST_FLOATING_EQUALITY( x_final_view[0], x_0, tol ); TEST_FLOATING_EQUALITY( x_final_view[1], x_1, tol ); } // Copy InterpolationBuffer data into Cubic Spline interpolation buffer for use in Adjoint Solve TimeRange<double> fwdTimeRange; RCP<InterpolationBufferBase<double> > fwdCubicSplineInterpBuffer; { RCP<PointwiseInterpolationBufferAppender<double> > piba = pointwiseInterpolationBufferAppender<double>(); RCP<InterpolationBuffer<double> > sinkInterpBuffer = interpolationBuffer<double>(); sinkInterpBuffer->setStorage(storageLimit); RCP<CubicSplineInterpolator<double> > csi = cubicSplineInterpolator<double>(); sinkInterpBuffer->setInterpolator(csi); RCP<const InterpolationBufferBase<double> > sourceInterpBuffer; { RCP<TrailingInterpolationBufferAcceptingIntegratorBase<double> > tibaib = Teuchos::rcp_dynamic_cast<TrailingInterpolationBufferAcceptingIntegratorBase<double> >(fwdIntegrator,true); sourceInterpBuffer = tibaib->getTrailingInterpolationBuffer(); } fwdTimeRange = sourceInterpBuffer->getTimeRange(); piba->append(*sourceInterpBuffer, fwdTimeRange, Teuchos::outArg(*sinkInterpBuffer)); fwdCubicSplineInterpBuffer = sinkInterpBuffer; TimeRange<double> sourceRange = sourceInterpBuffer->getTimeRange(); TimeRange<double> sinkRange = sinkInterpBuffer->getTimeRange(); TEST_EQUALITY( sourceRange.lower(), sinkRange.lower() ); TEST_EQUALITY( sourceRange.upper(), sinkRange.upper() ); } // Adjoint Solve, reading forward solve data from Cubic Spline interpolation buffer { RCP<ParameterList> ibPL = Teuchos::parameterList(); ibPL->sublist("Integrator Settings").sublist("Integrator Selection").set("Integrator Type","Default Integrator"); ibPL->sublist("Integrator Settings").set("Final Time",finalTime); ibPL->sublist("Integration Control Strategy Selection").set("Integration Control Strategy Type","Simple Integration Control Strategy"); ibPL->sublist("Integration Control Strategy Selection").sublist("Simple Integration Control Strategy").set("Take Variable Steps",false); ibPL->sublist("Integration Control Strategy Selection").sublist("Simple Integration Control Strategy").set("Fixed dt",dt); ibPL->sublist("Stepper Settings").sublist("Stepper Selection").set("Stepper Type","Backward Euler"); //ibPL->sublist("Stepper Settings").sublist("Stepper Selection").set("Stepper Type","Implicit RK"); //ibPL->sublist("Stepper Settings").sublist("Runge Kutta Butcher Tableau Selection").set("Runge Kutta Butcher Tableau Type","Implicit 1 Stage 2nd order Gauss"); ibPL->sublist("Interpolation Buffer Settings").sublist("Trailing Interpolation Buffer Selection").set("Interpolation Buffer Type","Interpolation Buffer"); ibPL->sublist("Interpolation Buffer Settings").sublist("Trailing Interpolation Buffer Selection").sublist("Interpolation Buffer").set("StorageLimit",storageLimit); ibPL->sublist("Interpolation Buffer Settings").sublist("Interpolator Selection").set("Interpolator Type","Linear Interpolator"); ib->setParameterList(ibPL); } RCP<Thyra::ModelEvaluator<double> > adjModel; { RCP<Rythmos::AdjointModelEvaluator<double> > model = Rythmos::adjointModelEvaluator<double>( fwdModel, fwdTimeRange ); //model->setFwdStateSolutionBuffer(fwdCubicSplineInterpBuffer); adjModel = model; } Thyra::ModelEvaluatorBase::InArgs<double> adjIC = adjModel->getNominalValues(); double phi_ic_0 = 2.0; double phi_ic_1 = 3.0; { // Initial conditions for adjoint: const RCP<const Thyra::VectorSpaceBase<double> > f_space = fwdModel->get_f_space(); const RCP<Thyra::VectorBase<double> > x_ic = createMember(f_space); { Thyra::DetachedVectorView<double> x_ic_view( *x_ic ); x_ic_view[0] = phi_ic_0; x_ic_view[1] = phi_ic_1; } const RCP<Thyra::VectorBase<double> > xdot_ic = createMember(f_space); V_S( Teuchos::outArg(*xdot_ic), ST::zero() ); adjIC.set_x(x_ic); adjIC.set_x_dot(xdot_ic); } RCP<Thyra::LinearNonlinearSolver<double> > adjNLSolver = Thyra::linearNonlinearSolver<double>(); RCP<IntegratorBase<double> > adjIntegrator = ib->create(adjModel,adjIC,adjNLSolver); RCP<const VectorBase<double> > phi_final; { Array<double> time_vec; time_vec.push_back(finalTime); Array<RCP<const Thyra::VectorBase<double> > > phi_final_array; adjIntegrator->getFwdPoints(time_vec,&phi_final_array,NULL,NULL); phi_final = phi_final_array[0]; } // Verify phi_final is correct { // Defaults from SinCos Model: double f = 1.0; double L = 1.0; double h = -dt; double phi_0 = 1.0/(1.0+std::pow(f*h/L,2.0))*(phi_ic_0+std::pow(f/L,2.0)*h*phi_ic_1); double phi_1 = 1.0/(1.0+std::pow(f*h/L,2.0))*(-h*phi_ic_0+phi_ic_1); double tol = 1.0e-10; Thyra::ConstDetachedVectorView<double> phi_final_view( *phi_final ); TEST_FLOATING_EQUALITY( phi_final_view[0], phi_0, tol ); TEST_FLOATING_EQUALITY( phi_final_view[1], phi_1, tol ); } // Compute error estimate //TEST_ASSERT( false ); }