Skip to content

Commit

Permalink
use std::deque for delay buffers (#3522)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Oct 27, 2015
1 parent d96c198 commit 7c86a6a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
40 changes: 18 additions & 22 deletions SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp
Expand Up @@ -423,10 +423,9 @@ void SystemDefaultImplementation::getRHS(double* f)

void SystemDefaultImplementation::intDelay(vector<unsigned int> expr, vector<double> delay_max)
{
_time_buffer.set_capacity(1024);
FOREACH(unsigned int expr_id, expr)
{
buffer_type delay_buffer(1024);
buffer_type delay_buffer;
_delay_buffer[expr_id]=delay_buffer;
}
vector<double>::iterator iter = std::max_element(delay_max.begin(),delay_max.end());
Expand All @@ -436,34 +435,31 @@ void SystemDefaultImplementation::intDelay(vector<unsigned int> expr, vector<do
void SystemDefaultImplementation::storeDelay(unsigned int expr_id, double expr_value, double time)
{
map<unsigned int,buffer_type>::iterator iter;
if((iter = _delay_buffer.find(expr_id))!=_delay_buffer.end())
{

if ((iter = _delay_buffer.find(expr_id)) != _delay_buffer.end()) {
iter->second.push_back(expr_value);
//buffer_type::iterator pos = find_if(_time_buffer.begin(),_time_buffer.end(),bind2nd(std::greater<double>(),time-(_delay_max+UROUND)));
//if(pos!=_time_buffer.end())
//{
// buffer_type::iterator first = _time_buffer.begin(); // first time entry
// unsigned int n = std::distance(first,pos);
// iter->second.erase_begin(n-1);
//}

}
else
throw ModelicaSimulationError(MODEL_EQ_SYSTEM,"invalid delay expression id");
}

void SystemDefaultImplementation::storeTime(double time)
{

// delete up to last value <= time - _delay_max
buffer_type::iterator first = _time_buffer.begin();
buffer_type::iterator pos = find_if(first, _time_buffer.end(),
bind2nd(std::greater<double>(),
time - _delay_max));
if (pos != first && --pos != first) {
difference_type n = std::distance(first, pos);
_time_buffer.erase(first, first + n);
map<unsigned int, buffer_type>::iterator iter;
for (iter = _delay_buffer.begin(); iter != _delay_buffer.end(); iter++) {
first = iter->second.begin();
iter->second.erase(first, first + n);
}
}
// store new value
_time_buffer.push_back(time);
// buffer_type::iterator pos = find_if(_time_buffer.begin(),_time_buffer.end(),bind2nd(std::greater<double>(),time-(_delay_max+UROUND)));
//if(pos!=_time_buffer.end())
//{
// buffer_type::iterator first = _time_buffer.begin(); // first time entry
// unsigned int n = std::distance(first,pos);
// _time_buffer.erase_begin(n-1);
//}
}

double SystemDefaultImplementation::delay(unsigned int expr_id,double expr_value,double delayTime, double delayMax)
Expand Down Expand Up @@ -513,7 +509,7 @@ double SystemDefaultImplementation::delay(unsigned int expr_id,double expr_value
if(pos!=_time_buffer.end())
{
buffer_type::iterator first = _time_buffer.begin(); // first time entry
std::iterator_traits<buffer_type::iterator>::difference_type index = std::distance(first,pos); //index of found time
difference_type index = std::distance(first, pos); //index of found time
t1 = *pos;
res1 = iter->second[index];
if(index == 0)
Expand Down
Expand Up @@ -5,7 +5,7 @@
*/
#include "TextfileWriter.h"


#include <boost/circular_buffer.hpp>

class BufferReaderWriter : public ContainerManager
{
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Include/Core/Modelica.h
Expand Up @@ -7,20 +7,19 @@
#include <string>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#include <cmath>
#include <numeric>
#include <functional>

#include <boost/circular_buffer.hpp>
#define BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/storage.hpp>
//#include <boost/timer/timer.hpp>

/*Namespaces*/
using std::abs;
Expand Down Expand Up @@ -177,6 +176,7 @@ using namespace boost::numeric;
#include <Core/Utils/extension/barriers.hpp>
#endif //USE_THREAD

//#include <boost/timer/timer.hpp>
//using boost::timer::cpu_timer;
//using boost::timer::cpu_times;
//using boost::timer::nanosecond_type;
Expand Down
Expand Up @@ -188,7 +188,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
*__z, ///< "Extended state vector", containing all states and algebraic variables of all types
*__zDot; ///< "Extended vector of derivatives", containing all right hand sides of differential and algebraic equations

typedef boost::circular_buffer<double> buffer_type;
typedef std::deque<double> buffer_type;
typedef std::iterator_traits<buffer_type::iterator>::difference_type difference_type;
map<unsigned int, buffer_type> _delay_buffer;
buffer_type _time_buffer;
double _delay_max;
Expand Down

0 comments on commit 7c86a6a

Please sign in to comment.