iamlikeme / rainflow

Implementation of the rainflow-counting algorythm in Python
MIT License
105 stars 34 forks source link

counting_cycles does not produce correct results #4

Closed SaetreS closed 7 years ago

SaetreS commented 7 years ago

The rainflow counting does not produce the same results as per the example given in ASTM E1049:85 - 2011.

The ASTM example uses the follow series: y = [-2, 1, -3, 5, -1, 3, -4, 4, -2]

which results into: count = [(1.0, 0.0), (2.0, 0.0), (3.0, 0.5), (4.0, 1.5), (5.0, 0.0), (6.0, 0.5), (7.0, 0.0), (8.0, 1.0), (9.0, 0.5), (10.0, 0)]

the count_cycles function should therefore produce: count = [(3, 0.5), (4, 1.5), (6, 0.5), (8, 1.0), (9, 0.5)]

the current result from the count_cycles function: count = [(4, 1.5), (8, 1.0), (9, 0.5)]

below is a simple unittest which reproduces the error.

import unittest
import rainflow

class Test_rainflow(unittest.TestCase):
    def setUp(self):
        pass

    def test_astm_example(self):
        self.y = [-2, 1, -3, 5, -1, 3, -4, 4, -2]
        astm_result = [(3, 0.5), (4, 1.5), (6, 0.5), (8, 1.0), (9, 0.5)]
        self.assertEqual(rainflow.count_cycles(self.y), astm_result)

if __name__ == '__main__':
    unittest.main()
iamlikeme commented 7 years ago

Hi, this behavior is by design and has to do with how load reversals are detected (see rainflow.reversals). By definition, load reversal occurs at a point if the load gradients before and after that point change sign. Since we don't know the load gradient before the first point in a series and after the last point in the series, we cannot classify these two points as reversals.

This is an interpretation of the ASTM standard which is not very clear on this issue.

In order to produce the correct cycle counts for the ASTM example, we have to prepend and append points, like so (see also tests/test_rainflow.py):

>>> y = [-2, 1, -3, 5, -1, 3, -4, 4, -2]  # ASME example load series
>>> rainflow.count_cycles([0] + y + [0])  # This produces expected cycle counts
[(3, 0.5), (4, 1.5), (6, 0.5), (8, 1.0), (9, 0.5)]