Skip to content

Commit

Permalink
Make C++11 compatible. Code format.
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkn committed Dec 25, 2023
1 parent dafade1 commit 8beb2b9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 143 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Example:
int main()
{
    RainFlow::Series series { -2, 1, -3, 5, -1, 3, -4, 4, -2 };
    auto counts { RainFlow::count_cycles( series, 2.0 ) };
    auto counts = RainFlow::count_cycles( series, 2.0 );
    /* counts:
        { 2, 0.0 },
        { 4, 2.0 },
Expand Down Expand Up @@ -56,7 +56,7 @@ Example:
int main()
{
    RainFlow::Series series { -2, 1, -3, 5, -1, 3, -4, 4, -2 };
    auto cycles { RainFlow::extract_cycles( series ) };
    auto cycles = RainFlow::extract_cycles( series );
    /* cycles:
        { 3, -0.5, 0.5, 0, 1 },
        { 4, -1.0, 0.5, 1, 2 },
Expand Down
13 changes: 6 additions & 7 deletions example/example_rainflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
#include <iterator>
#include <fstream>

int main( int argc, char *argv[] )
{
if( argc < 2 ) {
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: example_rainflow [FILE]" << std::endl;
return 1;
}

std::ifstream is { argv[1] };
if( ! is ) {
if (! is) {
std::cerr << "Cannot open the file: " << argv[1] << std::endl;
return 1;
}

std::istream_iterator<double> start { is }, end;
RainFlow::Series series { start, end };

auto binSize { 10 };
auto counts { RainFlow::count_cycles( series, binSize ) };
auto binSize = 10;
auto counts = RainFlow::count_cycles(series, binSize);

std::cout << "counted cycles (binSize=" << binSize << "):" << std::endl;
for( auto count : counts )
for (auto count: counts)
std::cout << '(' << count.first << ", " << count.second << ')' << std::endl;

return 0;
Expand Down
125 changes: 61 additions & 64 deletions src/rainflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,131 +8,128 @@ struct Reversal {

typedef std::deque<Reversal> Reversals;

static Reversals reversals( const RainFlow::Series& series )
{
static Reversals reversals(const RainFlow::Series& series) {
Reversals reversals;

if( series.size() <= 2 )
if (series.size() <= 2)
return reversals;

auto index { 0UL };
auto x_last { series[0] };
auto x { series[1] };
auto index = 0UL;
auto x_last = series[0];
auto x = series[1];

reversals.push_back( { index, x_last } );
reversals.push_back({ index, x_last });

auto d_last { x - x_last };
auto x_next { x };
auto d_last = x - x_last;
auto x_next = x;

while( ++index < series.size() - 1 ) {
while (++index < series.size() - 1) {
x_next = series[index + 1];
if( x_next == x )
if (x_next == x)
continue;

auto d_next { x_next - x };
if( d_next * d_last < 0 )
reversals.push_back( { index, x } );
auto d_next = x_next - x;
if (d_next * d_last < 0)
reversals.push_back({ index, x });

x_last = x;
x = x_next;
d_last = d_next;
}

if( series.size() > 2 )
reversals.push_back( { index, x_next } );
if (series.size() > 2)
reversals.push_back({ index, x_next });

return reversals;
}

RainFlow::Cycles RainFlow::extract_cycles( const RainFlow::Series& series )
{
RainFlow::Cycles RainFlow::extract_cycles(const RainFlow::Series& series) {
RainFlow::Cycles cycles;
Reversals points;

auto format_output = []( Reversals::iterator it, double count ) {
auto i1 { it->index };
auto x1 { it->value }; ++it;
auto i2 { it->index };
auto x2 { it->value };
auto rng { std::abs( x1 - x2 ) };
auto mean { ( x1 + x2 ) / 2 };
auto format_output = [] (Reversals::iterator it, double count) {
auto i1 = it->index;
auto x1 = it->value; ++it;
auto i2 = it->index;
auto x2 = it->value;
auto rng = std::abs(x1 - x2);
auto mean = (x1 + x2) / 2;
return RainFlow::Cycle { rng, mean, count, i1, i2 };
};

for( auto point : reversals( series ) ) {
points.push_back( point );
for (auto point: reversals(series)) {
points.push_back(point);

while( points.size() >= 3 ) {
while (points.size() >= 3) {
// Form ranges X and Y from the three most recent points
auto end { points.end() - 1 };
auto x3 { end->value }; --end;
auto x2 { end->value }; --end;
auto x1 { end->value };

auto X { std::abs( x3 - x2 ) };
auto Y { std::abs( x2 - x1 ) };
if( X < Y ) {
auto end = points.end() - 1;
auto x3 = end->value; --end;
auto x2 = end->value; --end;
auto x1 = end->value;

auto X = std::abs(x3 - x2);
auto Y = std::abs(x2 - x1);

if (X < Y) {
// Read the next point
break;
}
else if( points.size() == 3 ) {
} else if (points.size() == 3) {
// Y contains the starting point
// Count Y as one-half cycle and discard the first point
cycles.push_back( format_output( points.begin(), 0.5 ) );
cycles.push_back(format_output(points.begin(), 0.5));
points.pop_front();
}
else {
} else {
// Count Y as one cycle and discard the peak and the valley of Y
cycles.push_back( format_output( points.end() - 3, 1.0 ) );
auto last { points.back() };
cycles.push_back(format_output(points.end() - 3, 1.0));
auto last = points.back();
points.pop_back();
points.pop_back();
points.pop_back();
points.push_back( last );
points.push_back(last);
}
}
}

// Count the remaining ranges as one-half cycles
while( points.size() > 1 ) {
cycles.push_back( format_output( points.begin(), 0.5 ) );
while (points.size() > 1) {
cycles.push_back(format_output(points.begin(), 0.5));
points.pop_front();
}

return cycles;
}

RainFlow::Counts RainFlow::count_cycles( const RainFlow::Series& series, double binSize )
{
RainFlow::Counts RainFlow::count_cycles(const RainFlow::Series& series, double binSize) {
RainFlow::Counts counts;

if( ! std::isnan( binSize ) ) {
auto nmax { 0.0 };
if (! std::isnan(binSize)) {
auto nmax = 0.0;

for( auto cycle : extract_cycles( series ) ) {
auto n { std::ceil( cycle.range / binSize ) };
if( n > nmax )
for (auto cycle: extract_cycles(series)) {
auto n = std::ceil(cycle.range / binSize);
if (n > nmax)
nmax = n;

auto range { n * binSize };
if( counts.find( range ) == counts.end() )
auto range = n * binSize;
if (counts.find(range) == counts.end()) {
counts[range] = cycle.count;
else
} else {
counts[range] += cycle.count;
}
}

for( auto n = 1; n < nmax; ++n ) {
auto range { n * binSize };
if( counts.find( range ) == counts.end() )
for (auto n = 1; n < nmax; ++n) {
auto range = n * binSize;
if (counts.find(range) == counts.end())
counts[range] = 0.0;
}
}
else {
for( auto cycle : extract_cycles( series ) ) {
if( counts.find( cycle.range ) == counts.end() )
} else {
for (auto cycle: extract_cycles(series)) {
if (counts.find(cycle.range) == counts.end()) {
counts[cycle.range] = cycle.count;
else
} else {
counts[cycle.range] += cycle.count;
}
}
}

Expand Down
Loading

0 comments on commit 8beb2b9

Please sign in to comment.