move implementation to .cpp files.
This commit is contained in:
parent
3f8d379c7f
commit
355e68c63b
145
opm/core/simulator/AdaptiveSimulatorTimer.cpp
Normal file
145
opm/core/simulator/AdaptiveSimulatorTimer.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
Copyright 2014 IRIS AS
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
||||
#include <opm/core/simulator/AdaptiveSimulatorTimer.hpp>
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
AdaptiveSimulatorTimer::
|
||||
AdaptiveSimulatorTimer( const double start_time, const double total_time, const double lastDt )
|
||||
: start_time_( start_time )
|
||||
, total_time_( total_time )
|
||||
, current_time_( start_time_ )
|
||||
, dt_( computeInitialTimeStep( lastDt ) )
|
||||
, current_step_( 0 )
|
||||
, steps_()
|
||||
, suggestedMax_( 0.0 )
|
||||
, suggestedAverage_( 0.0 )
|
||||
{
|
||||
// reserve memory for sub steps
|
||||
steps_.reserve( 10 );
|
||||
}
|
||||
|
||||
AdaptiveSimulatorTimer& AdaptiveSimulatorTimer::operator++ ()
|
||||
{
|
||||
++current_step_;
|
||||
current_time_ += dt_;
|
||||
// store used time step sizes
|
||||
steps_.push_back( dt_ );
|
||||
return *this;
|
||||
}
|
||||
|
||||
void AdaptiveSimulatorTimer::
|
||||
provideTimeStepEstimate( const double dt_estimate )
|
||||
{
|
||||
// store some information about the time steps suggested
|
||||
suggestedMax_ = std::max( dt_estimate, suggestedMax_ );
|
||||
suggestedAverage_ += dt_estimate;
|
||||
|
||||
double remaining = (total_time_ - current_time_);
|
||||
|
||||
if( remaining > 0 ) {
|
||||
|
||||
// set new time step (depending on remaining time)
|
||||
if( 1.5 * dt_estimate > remaining ) {
|
||||
dt_ = remaining;
|
||||
return ;
|
||||
}
|
||||
|
||||
// check for half interval step to avoid very small step at the end
|
||||
// remaining *= 0.5;
|
||||
|
||||
if( 2.25 * dt_estimate > remaining ) {
|
||||
dt_ = 0.5 * remaining;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
// otherwise set dt_estimate as is
|
||||
dt_ = dt_estimate;
|
||||
}
|
||||
|
||||
int AdaptiveSimulatorTimer::
|
||||
currentStepNum () const { return current_step_; }
|
||||
|
||||
double AdaptiveSimulatorTimer::currentStepLength () const
|
||||
{
|
||||
assert( ! done () );
|
||||
return dt_;
|
||||
}
|
||||
|
||||
double AdaptiveSimulatorTimer::totalTime() const { return total_time_; }
|
||||
|
||||
double AdaptiveSimulatorTimer::simulationTimeElapsed() const { return current_time_; }
|
||||
|
||||
bool AdaptiveSimulatorTimer::done () const { return (current_time_ >= total_time_) ; }
|
||||
|
||||
double AdaptiveSimulatorTimer::averageStepLength() const
|
||||
{
|
||||
const int size = steps_.size();
|
||||
if( size == 0 ) return 0.0;
|
||||
|
||||
const double sum = std::accumulate(steps_.begin(), steps_.end(), 0.0);
|
||||
return sum / double(size);
|
||||
}
|
||||
|
||||
/// \brief return max step length used so far
|
||||
double AdaptiveSimulatorTimer::maxStepLength () const
|
||||
{
|
||||
if( steps_.size() == 0 ) return 0.0;
|
||||
return *(std::max_element( steps_.begin(), steps_.end() ));
|
||||
}
|
||||
|
||||
/// \brief return min step length used so far
|
||||
double AdaptiveSimulatorTimer::minStepLength () const
|
||||
{
|
||||
if( steps_.size() == 0 ) return 0.0;
|
||||
return *(std::min_element( steps_.begin(), steps_.end() ));
|
||||
}
|
||||
|
||||
/// \brief return max suggested step length
|
||||
double AdaptiveSimulatorTimer::suggestedMax () const { return suggestedMax_; }
|
||||
|
||||
/// \brief return average suggested step length
|
||||
double AdaptiveSimulatorTimer::suggestedAverage () const
|
||||
{
|
||||
const int size = steps_.size();
|
||||
return (size > 0 ) ? (suggestedAverage_ / double(size)) : suggestedAverage_;
|
||||
}
|
||||
|
||||
/// \brief report start and end time as well as used steps so far
|
||||
void AdaptiveSimulatorTimer::report(std::ostream& os) const
|
||||
{
|
||||
const double factor = 86400.0;
|
||||
os << "Sub steps started at time = " << start_time_/factor << " (days)" << std::endl;
|
||||
for( size_t i=0; i<steps_.size(); ++i )
|
||||
{
|
||||
os << " step[ " << i << " ] = " << steps_[ i ]/factor << " (days)" << std::endl;
|
||||
}
|
||||
std::cout << "sub steps end time = " << simulationTimeElapsed()/factor << " (days)" << std::endl;
|
||||
}
|
||||
|
||||
} // namespace Opm
|
@ -42,124 +42,46 @@ namespace Opm
|
||||
/// \param start_time start time of timer
|
||||
/// \param total_time total time of timer
|
||||
/// \param lastDt last suggested length of time step interval
|
||||
AdaptiveSimulatorTimer( const double start_time, const double total_time, const double lastDt )
|
||||
: start_time_( start_time )
|
||||
, total_time_( total_time )
|
||||
, current_time_( start_time_ )
|
||||
, dt_( computeInitialTimeStep( lastDt ) )
|
||||
, current_step_( 0 )
|
||||
, steps_()
|
||||
, suggestedMax_( 0.0 )
|
||||
, suggestedAverage_( 0.0 )
|
||||
{
|
||||
// reserve memory for sub steps
|
||||
steps_.reserve( 10 );
|
||||
}
|
||||
AdaptiveSimulatorTimer( const double start_time, const double total_time, const double lastDt );
|
||||
|
||||
/// \brief advance time by currentStepLength
|
||||
AdaptiveSimulatorTimer& operator++ ()
|
||||
{
|
||||
++current_step_;
|
||||
current_time_ += dt_;
|
||||
// store used time step sizes
|
||||
steps_.push_back( dt_ );
|
||||
return *this;
|
||||
}
|
||||
AdaptiveSimulatorTimer& operator++ ();
|
||||
|
||||
/// \brief provide and estimate for new time step size
|
||||
void provideTimeStepEstimate( const double dt_estimate )
|
||||
{
|
||||
// store some information about the time steps suggested
|
||||
suggestedMax_ = std::max( dt_estimate, suggestedMax_ );
|
||||
suggestedAverage_ += dt_estimate;
|
||||
|
||||
double remaining = (total_time_ - current_time_);
|
||||
|
||||
if( remaining > 0 ) {
|
||||
|
||||
// set new time step (depending on remaining time)
|
||||
if( 1.5 * dt_estimate > remaining ) {
|
||||
dt_ = remaining;
|
||||
return ;
|
||||
}
|
||||
|
||||
// check for half interval step to avoid very small step at the end
|
||||
// remaining *= 0.5;
|
||||
|
||||
if( 2.25 * dt_estimate > remaining ) {
|
||||
dt_ = 0.5 * remaining;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
// otherwise set dt_estimate as is
|
||||
dt_ = dt_estimate;
|
||||
}
|
||||
void provideTimeStepEstimate( const double dt_estimate );
|
||||
|
||||
/// \brief \copydoc SimulationTimer::currentStepNum
|
||||
int currentStepNum () const { return current_step_; }
|
||||
int currentStepNum () const;
|
||||
|
||||
/// \brief \copydoc SimulationTimer::currentStepLength
|
||||
double currentStepLength () const
|
||||
{
|
||||
assert( ! done () );
|
||||
return dt_;
|
||||
}
|
||||
double currentStepLength () const;
|
||||
|
||||
/// \brief \copydoc SimulationTimer::totalTime
|
||||
double totalTime() const { return total_time_; }
|
||||
double totalTime() const;
|
||||
|
||||
/// \brief \copydoc SimulationTimer::simulationTimeElapsed
|
||||
double simulationTimeElapsed() const { return current_time_; }
|
||||
double simulationTimeElapsed() const;
|
||||
|
||||
/// \brief \copydoc SimulationTimer::done
|
||||
bool done () const { return (current_time_ >= total_time_) ; }
|
||||
bool done () const;
|
||||
|
||||
/// \brief return average step length used so far
|
||||
double averageStepLength() const
|
||||
{
|
||||
const int size = steps_.size();
|
||||
if( size == 0 ) return 0.0;
|
||||
|
||||
const double sum = std::accumulate(steps_.begin(), steps_.end(), 0.0);
|
||||
return sum / double(size);
|
||||
}
|
||||
double averageStepLength() const;
|
||||
|
||||
/// \brief return max step length used so far
|
||||
double maxStepLength () const
|
||||
{
|
||||
if( steps_.size() == 0 ) return 0.0;
|
||||
return *(std::max_element( steps_.begin(), steps_.end() ));
|
||||
}
|
||||
double maxStepLength () const;
|
||||
|
||||
/// \brief return min step length used so far
|
||||
double minStepLength () const
|
||||
{
|
||||
if( steps_.size() == 0 ) return 0.0;
|
||||
return *(std::min_element( steps_.begin(), steps_.end() ));
|
||||
}
|
||||
double minStepLength () const;
|
||||
|
||||
/// \brief return max suggested step length
|
||||
double suggestedMax () const { return suggestedMax_; }
|
||||
double suggestedMax () const;
|
||||
|
||||
/// \brief return average suggested step length
|
||||
double suggestedAverage () const
|
||||
{
|
||||
const int size = steps_.size();
|
||||
return (size > 0 ) ? (suggestedAverage_ / double(size)) : suggestedAverage_;
|
||||
}
|
||||
double suggestedAverage () const;
|
||||
|
||||
/// \brief report start and end time as well as used steps so far
|
||||
void report(std::ostream& os) const
|
||||
{
|
||||
const double factor = 86400.0;
|
||||
os << "Sub steps started at time = " << start_time_/factor << " (days)" << std::endl;
|
||||
for( size_t i=0; i<steps_.size(); ++i )
|
||||
{
|
||||
os << " step[ " << i << " ] = " << steps_[ i ]/factor << " (days)" << std::endl;
|
||||
}
|
||||
std::cout << "sub steps end time = " << simulationTimeElapsed()/factor << " (days)" << std::endl;
|
||||
}
|
||||
void report(std::ostream& os) const;
|
||||
|
||||
protected:
|
||||
const double start_time_;
|
||||
|
@ -17,14 +17,15 @@
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include <opm/core/simulator/PIDTimeStepControl.hpp>
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
/// \brief constructor
|
||||
/// \param tol tolerance for the relative changes of the numerical solution to be accepted
|
||||
/// in one time step (default is 1e-3)
|
||||
PIDTimeStepControl::PIDTimeStepControl( const double, const bool verbose )
|
||||
PIDTimeStepControl::PIDTimeStepControl( const double tol, const bool verbose )
|
||||
: p0_()
|
||||
, sat0_()
|
||||
, tol_( tol )
|
||||
@ -32,7 +33,6 @@ namespace Opm
|
||||
, verbose_( verbose )
|
||||
{}
|
||||
|
||||
/// \brief \copydoc TimeStepControlInterface::initialize
|
||||
void PIDTimeStepControl::initialize( const SimulatorState& state )
|
||||
{
|
||||
// store current state for later time step computation
|
||||
@ -40,16 +40,16 @@ namespace Opm
|
||||
sat0_ = state.saturation();
|
||||
}
|
||||
|
||||
/// \brief \copydoc TimeStepControlInterface::computeTimeStepSize
|
||||
double PIDTimeStepControl::computeTimeStepSize( const double dt, const int /* iterations */, const SimulatorState& state ) const
|
||||
double PIDTimeStepControl::
|
||||
computeTimeStepSize( const double dt, const int /* iterations */, const SimulatorState& state ) const
|
||||
{
|
||||
const size_t size = p0_.size();
|
||||
const std::size_t size = p0_.size();
|
||||
assert( state.pressure().size() == size );
|
||||
assert( state.saturation().size() == size );
|
||||
assert( sat0_.size() == size );
|
||||
|
||||
// compute u^n - u^n+1
|
||||
for( size_t i=0; i<size; ++i )
|
||||
for( std::size_t i=0; i<size; ++i )
|
||||
{
|
||||
p0_[ i ] -= state.pressure()[ i ];
|
||||
sat0_[ i ] -= state.saturation()[ i ];
|
||||
@ -118,4 +118,3 @@ namespace Opm
|
||||
}
|
||||
|
||||
} // end namespace Opm
|
||||
#endif
|
||||
|
@ -19,6 +19,8 @@
|
||||
#ifndef OPM_TIMESTEPCONTROLINTERFACE_HEADER_INCLUDED
|
||||
#define OPM_TIMESTEPCONTROLINTERFACE_HEADER_INCLUDED
|
||||
|
||||
#include <opm/core/simulator/SimulatorState.hpp>
|
||||
|
||||
namespace Opm
|
||||
{
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user