Scalar translateTimeRange( Scalar t, const TimeRange<Scalar>& sourceRange, const TimeRange<Scalar>& destinationRange ) { Scalar r = destinationRange.length()/sourceRange.length(); return r*t+destinationRange.lower()-r*sourceRange.lower(); }
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, copyAndScaleInvalid ) { TimeRange<double> tr; TimeRange<double> newTr = tr.copyAndScale(5.0); TEST_EQUALITY_CONST( newTr.isValid(), false ); TEST_EQUALITY( newTr.lower(), tr.lower() ); TEST_EQUALITY( newTr.upper(), tr.upper() ); TEST_EQUALITY( newTr.length(), tr.length() ); }
void SecondsMarkerProvider::GetMarkers(TimeRange const& range, AudioMarkerVector &out) const { if (!enabled->GetBool()) return; if ((range.length() + 999) / 1000 > (int)markers.size()) markers.resize((range.length() + 999) / 1000, Marker(pen.get())); size_t i = 0; for (int time = ((range.begin() + 999) / 1000) * 1000; time < range.end(); time += 1000) { markers[i].position = time; out.push_back(&markers[i++]); } }
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, copyAndScale ) { TimeRange<double> tr(1.0,2.0); TimeRange<double> newTr = tr.copyAndScale(5.0); TEST_EQUALITY_CONST( newTr.isValid(), true ); TEST_EQUALITY_CONST( newTr.lower(), 5.0 ); TEST_EQUALITY_CONST( newTr.upper(), 10.0 ); TEST_EQUALITY_CONST( newTr.length(), 5.0 ); }
void AudioController::PlayRange(const TimeRange &range) { if (!IsAudioOpen()) return; player->Play(SamplesFromMilliseconds(range.begin()), SamplesFromMilliseconds(range.length())); playback_mode = PM_Range; playback_timer.Start(20); AnnouncePlaybackPosition(range.begin()); }
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, newTimeRange ) { TimeRange<double> tr; // it should be initialized as [0,-1] TEST_EQUALITY_CONST( tr.isValid(), false ); TEST_COMPARE( tr.lower(), >, tr.upper() ); TEST_EQUALITY_CONST( tr.isInRange(0.5), false ); TEST_EQUALITY_CONST( tr.isInRange(0.0), false ); TEST_EQUALITY_CONST( tr.isInRange(-1.0), false ); TEST_EQUALITY_CONST( tr.length(), -1.0 ); }
void Rythmos::assertNoTimePointsInsideCurrentTimeRange( const InterpolationBufferBase<Scalar>& interpBuffer, const Array<Scalar>& time_vec ) { typedef ScalarTraits<Scalar> ST; const int numTimePoints = time_vec.size(); const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange(); if (currentTimeRange.length() >= ST::zero()) { for ( int i = 0; i < numTimePoints; ++i ) { TEST_FOR_EXCEPTION( currentTimeRange.isInRange(time_vec[i]), std::out_of_range, "Error, time_vec["<<i<<"] = " << time_vec[i] << " is in TimeRange of " << interpBuffer.description() << " = [" << currentTimeRange.lower() << "," << currentTimeRange.upper() << "]!" ); } } }
void Rythmos::assertNoTimePointsBeforeCurrentTimeRange( const InterpolationBufferBase<Scalar> &interpBuffer, const Array<Scalar>& time_vec, const int &startingTimePointIndex ) { typedef ScalarTraits<Scalar> ST; const int numTimePoints = time_vec.size(); const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange(); if (currentTimeRange.length() >= ST::zero()) { for ( int i = 0; i < numTimePoints; ++i ) { TEST_FOR_EXCEPTION( time_vec[i] < currentTimeRange.lower(), std::out_of_range, "Error, time_vec["<<i<<"] = " << time_vec[i] << " < currentTimeRange.lower() = " << currentTimeRange.lower() << " for " << interpBuffer.description() << "!" ); } } }
TEUCHOS_UNIT_TEST( Rythmos_ExplicitRKStepper, getTimeRange ) { { RCP<SinCosModel> model = sinCosModel(false); RCP<ExplicitRKStepper<double> > stepper = explicitRKStepper<double>(model); Thyra::ModelEvaluatorBase::InArgs<double> ic = model->getNominalValues(); stepper->setInitialCondition(ic); TimeRange<double> tr = stepper->getTimeRange(); TEST_EQUALITY_CONST( tr.isValid(), true ); TEST_EQUALITY_CONST( tr.lower(), 0.0 ); TEST_EQUALITY_CONST( tr.upper(), 0.0 ); TEST_EQUALITY_CONST( tr.length(), 0.0 ); } { RCP<SinCosModel> model = sinCosModel(false); RCP<ExplicitRKStepper<double> > stepper = explicitRKStepper<double>(); stepper->setModel(model); TimeRange<double> tr; TEST_NOTHROW( tr = stepper->getTimeRange() ); TEST_EQUALITY_CONST( tr.isValid(), false ); } }
RCP<Thyra::VectorBase<Scalar> > computeArea( const Thyra::ModelEvaluator<Scalar>& me, const TimeRange<Scalar>& tr, const GaussQuadrature1D<Scalar>& gq ) { typedef Teuchos::ScalarTraits<Scalar> ST; RCP<Thyra::VectorBase<Scalar> > area = Thyra::createMember(me.get_x_space()); V_S(outArg(*area),ST::zero()); RCP<const TimeRange<Scalar> > sourceRange = gq.getRange(); RCP<const Array<Scalar> > sourcePts = gq.getPoints(); RCP<const Array<Scalar> > sourceWts = gq.getWeights(); Array<Scalar> destPts(*sourcePts); for (unsigned int i=0 ; i<sourcePts->size() ; ++i) { destPts[i] = translateTimeRange<Scalar>((*sourcePts)[i],*sourceRange,tr); } Scalar r = tr.length()/sourceRange->length(); for (unsigned int i=0 ; i<destPts.size() ; ++i) { RCP<Thyra::VectorBase<Scalar> > tmpVec = eval_f_t<Scalar>(me,destPts[i]); Vp_StV(outArg(*area),r*(*sourceWts)[i],*tmpVec); } return area; }
void AudioController::SaveClip(wxString const& filename, TimeRange const& range) const { int64_t start_sample = SamplesFromMilliseconds(range.begin()); int64_t end_sample = SamplesFromMilliseconds(range.end()); if (filename.empty() || start_sample > provider->GetNumSamples() || range.length() == 0) return; agi::io::Save outfile(STD_STR(filename), true); std::ofstream& out(outfile.Get()); size_t bytes_per_sample = provider->GetBytesPerSample() * provider->GetChannels(); size_t bufsize = (end_sample - start_sample) * bytes_per_sample; int intval; short shortval; out << "RIFF"; out.write((char*)&(intval=bufsize+36),4); out<< "WAVEfmt "; out.write((char*)&(intval=16),4); out.write((char*)&(shortval=1),2); out.write((char*)&(shortval=provider->GetChannels()),2); out.write((char*)&(intval=provider->GetSampleRate()),4); out.write((char*)&(intval=provider->GetSampleRate()*provider->GetChannels()*provider->GetBytesPerSample()),4); out.write((char*)&(intval=provider->GetChannels()*provider->GetBytesPerSample()),2); out.write((char*)&(shortval=provider->GetBytesPerSample()<<3),2); out << "data"; out.write((char*)&bufsize,4); //samples per read size_t spr = 65536 / bytes_per_sample; std::vector<char> buf(bufsize); for(int64_t i = start_sample; i < end_sample; i += spr) { size_t len = std::min<size_t>(spr, end_sample - i); provider->GetAudio(&buf[0], i, len); out.write(&buf[0], len * bytes_per_sample); } }
bool Rythmos::getCurrentPoints( const InterpolationBufferBase<Scalar> &interpBuffer, const Array<Scalar>& time_vec, Array<RCP<const Thyra::VectorBase<Scalar> > >* x_vec, Array<RCP<const Thyra::VectorBase<Scalar> > >* xdot_vec, int *nextTimePointIndex_inout ) { typedef ScalarTraits<Scalar> ST; using Teuchos::as; const int numTotalTimePoints = time_vec.size(); // Validate input #ifdef RYTHMOS_DEBUG TEST_FOR_EXCEPT(nextTimePointIndex_inout==0); TEUCHOS_ASSERT( 0 <= *nextTimePointIndex_inout && *nextTimePointIndex_inout < numTotalTimePoints ); TEUCHOS_ASSERT( x_vec == 0 || as<int>(x_vec->size()) == numTotalTimePoints ); TEUCHOS_ASSERT( xdot_vec == 0 || as<int>(xdot_vec->size()) == numTotalTimePoints ); #endif // RYTHMOS_DEBUG int &nextTimePointIndex = *nextTimePointIndex_inout; const int initNextTimePointIndex = nextTimePointIndex; const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange(); if (currentTimeRange.length() >= ST::zero()) { // Load a temp array with all of the current time points that fall in the // current time range. Array<Scalar> current_time_vec; { // scope for i to remove shadow warning. int i; for ( i = 0; i < numTotalTimePoints-nextTimePointIndex; ++i ) { const Scalar t = time_vec[nextTimePointIndex]; #ifdef RYTHMOS_DEBUG TEUCHOS_ASSERT( t >= currentTimeRange.lower() ); #endif // RYTHMOS_DEBUG if ( currentTimeRange.isInRange(t) ) { ++nextTimePointIndex; current_time_vec.push_back(t); } else { break; } } #ifdef RYTHMOS_DEBUG // Here I am just checking that the loop worked as expected with the data // in the current time range all comming first. TEUCHOS_ASSERT( nextTimePointIndex-initNextTimePointIndex == i ); #endif } // Get points in current time range if any such points exist const int numCurrentTimePoints = current_time_vec.size(); if ( numCurrentTimePoints > 0 ) { // Get the state(s) for current time points from the stepper and put // them into temp arrays Array<RCP<const Thyra::VectorBase<Scalar> > > current_x_vec; Array<RCP<const Thyra::VectorBase<Scalar> > > current_xdot_vec; if (x_vec || xdot_vec) { interpBuffer.getPoints( current_time_vec, x_vec ? ¤t_x_vec : 0, xdot_vec ? ¤t_xdot_vec : 0, 0 // accuracy_vec ); } // Copy the gotten x and xdot vectors from the temp arrays to the output // arrays. for ( int i = initNextTimePointIndex; i < nextTimePointIndex; ++i ) { if (x_vec) (*x_vec)[i] = current_x_vec[i-initNextTimePointIndex]; if (xdot_vec) (*xdot_vec)[i] = current_xdot_vec[i-initNextTimePointIndex]; } } } return ( nextTimePointIndex == initNextTimePointIndex ? false : true ); }
TEUCHOS_UNIT_TEST( BasicDiscreteAdjointStepperTester, rawNonlinearAdjoint ) { using Teuchos::outArg; using Teuchos::describe; using Teuchos::getParametersFromXmlString; typedef Thyra::ModelEvaluatorBase MEB; // out << "\nA) Create the nonlinear ME ...\n"; // RCP<VanderPolModel> stateModel = vanderPolModel( getParametersFromXmlString( "<ParameterList>" " <Parameter name=\"Implicit model formulation\" type=\"bool\" value=\"1\"/>" "</ParameterList>" ) ); // out << "\nB) Create the nonlinear solver ...\n"; // RCP<TimeStepNonlinearSolver<double> > nlSolver = timeStepNonlinearSolver<double>( getParametersFromXmlString( "<ParameterList>" " <Parameter name=\"Default Tol\" type=\"double\" value=\"1.0e-10\"/>" " <Parameter name=\"Default Max Iters\" type=\"int\" value=\"20\"/>" "</ParameterList>" ) ); // out << "\nC) Create the integrator for the forward state problem ...\n"; // RCP<IntegratorBuilder<double> > ib = integratorBuilder<double>( Teuchos::getParametersFromXmlString( "<ParameterList>" " <ParameterList name=\"Stepper Settings\">" " <ParameterList name=\"Stepper Selection\">" " <Parameter name=\"Stepper Type\" type=\"string\" value=\"Backward Euler\"/>" " </ParameterList>" " </ParameterList>" " <ParameterList name=\"Integration Control Strategy Selection\">" " <Parameter name=\"Integration Control Strategy Type\" type=\"string\"" " value=\"Simple Integration Control Strategy\"/>" " <ParameterList name=\"Simple Integration Control Strategy\">" " <Parameter name=\"Take Variable Steps\" type=\"bool\" value=\"false\"/>" " <Parameter name=\"Fixed dt\" type=\"double\" value=\"0.5\"/>" // Gives 2 time steps! " </ParameterList>" " </ParameterList>" " <ParameterList name=\"Interpolation Buffer Settings\">" " <ParameterList name=\"Trailing Interpolation Buffer Selection\">" " <Parameter name=\"Interpolation Buffer Type\" type=\"string\" value=\"Interpolation Buffer\"/>" " </ParameterList>" " </ParameterList>" "</ParameterList>" ) ); MEB::InArgs<double> ic = stateModel->getNominalValues(); RCP<IntegratorBase<double> > integrator = ib->create(stateModel, ic, nlSolver); //integrator->setVerbLevel(Teuchos::VERB_EXTREME); // ToDo: Set the trailing IB to pick up the entire state solution! // out << "\nD) Solve the basic forward problem ...\n"; // const TimeRange<double> fwdTimeRange = integrator->getFwdTimeRange(); const double t_final = fwdTimeRange.upper(); RCP<const Thyra::VectorBase<double> > x_final, x_dot_final; get_fwd_x_and_x_dot( *integrator, t_final, outArg(x_final), outArg(x_dot_final) ); out << "\nt_final = " << t_final << "\n"; out << "\nx_final: " << *x_final; out << "\nx_dot_final: " << *x_dot_final; // out << "\nE) Create the basic adjoint model (no distributed response) ...\n"; // RCP<AdjointModelEvaluator<double> > adjModel = adjointModelEvaluator<double>( stateModel, fwdTimeRange ); adjModel->setFwdStateSolutionBuffer(integrator); // out << "\nF) Create a stepper and integrator for the adjoint ...\n"; // RCP<Thyra::LinearNonlinearSolver<double> > adjTimeStepSolver = Thyra::linearNonlinearSolver<double>(); RCP<Rythmos::StepperBase<double> > adjStepper = integrator->getStepper()->cloneStepperAlgorithm(); // out << "\nG) Set up the initial condition for the adjoint at the final time ...\n"; // const RCP<const Thyra::VectorSpaceBase<double> > f_space = stateModel->get_f_space(); // lambda(t_final) = x_final const RCP<Thyra::VectorBase<double> > lambda_ic = createMember(f_space); V_V( lambda_ic.ptr(), *x_final ); // lambda_dot(t_final,i) = 0.0 const RCP<Thyra::VectorBase<double> > lambda_dot_ic = createMember(f_space); Thyra::V_S( lambda_dot_ic.ptr(), 0.0 ); MEB::InArgs<double> adj_ic = adjModel->getNominalValues(); adj_ic.set_x(lambda_ic); adj_ic.set_x_dot(lambda_dot_ic); out << "\nadj_ic: " << describe(adj_ic, Teuchos::VERB_EXTREME); RCP<Rythmos::IntegratorBase<double> > adjIntegrator = ib->create(adjModel, adj_ic, adjTimeStepSolver); // out << "\nH) Integrate the adjoint backwards in time (using backward time) ...\n"; // adjStepper->setInitialCondition(adj_ic); adjIntegrator->setStepper(adjStepper, fwdTimeRange.length()); const double adj_t_final = fwdTimeRange.length(); RCP<const Thyra::VectorBase<double> > lambda_final, lambda_dot_final; get_fwd_x_and_x_dot( *adjIntegrator, adj_t_final, outArg(lambda_final), outArg(lambda_dot_final) ); out << "\nadj_t_final = " << adj_t_final << "\n"; out << "\nlambda_final: " << *lambda_final; out << "\nlambda_dot_final: " << *lambda_dot_final; }