Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally include open cycles in rainflow cycle counting #19

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions fatpack/rainflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@ def concatenate_reversals(reversals1, reversals2):
return np.concatenate(result)


def find_rainflow_cycles(reversals):
def find_rainflow_cycles(reversals, include_open_cycles=False):
"""Return the rainflow cycles and residue from a sequence of reversals.

Arguments
---------
reversals : 1darray
An 1D-array of reversals.
include_open_cycles : bool, optional
An optional flag to include open cycles (default is False).

Returns
-------
Expand Down Expand Up @@ -303,7 +305,7 @@ def find_rainflow_cycles(reversals):

"""

result = []
cycles_firstpass = []
residue = []
len_residue = 0
for reversal in reversals:
Expand All @@ -313,13 +315,31 @@ def find_rainflow_cycles(reversals):
S0, S1, S2, S3 = residue[-4], residue[-3], residue[-2], residue[-1]
dS1, dS2, dS3 = fabs(S1-S0), fabs(S2-S1), fabs(S3-S2)
if (dS2 <= dS1) and (dS2 <= dS3):
result += [[S1, S2]]
cycles_firstpass += [[S1, S2]]
del residue[-3]
del residue[-2]
len_residue -= 2
else:
break
return np.array(result), np.array(residue)

if include_open_cycles:
cycles_firstpass = np.array(cycles_firstpass)
processed_residue = concatenate_reversals(residue, residue)
cycles_open_sequence = find_rainflow_cycles(processed_residue)[0]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the recursive function call.

found_cycles_firstpass = len(cycles_firstpass.shape) == 2
found_cycles_open_sequence = len(cycles_open_sequence.shape) == 2
if found_cycles_firstpass and found_cycles_open_sequence:
rainflow_cycles = np.concatenate((cycles_firstpass, cycles_open_sequence))
elif found_cycles_firstpass:
rainflow_cycles = cycles_firstpass
elif found_cycles_open_sequence:
rainflow_cycles = cycles_open_sequence
else:
raise ValueError("Could not find any cycles in sequence")
else:
rainflow_cycles = cycles_firstpass

return np.array(rainflow_cycles), np.array(residue)


def find_rainflow_matrix(data_array, rowbins, colbins, return_bins=False):
Expand Down Expand Up @@ -495,19 +515,7 @@ def find_rainflow_ranges(y, k=64, return_means=False):
"""

reversals, _ = find_reversals(y, k)
cycles_firstpass, residue = find_rainflow_cycles(reversals)
processed_residue = concatenate_reversals(residue, residue)
cycles_open_sequence, _ = find_rainflow_cycles(processed_residue)
found_cycles_firstpass = len(cycles_firstpass.shape) == 2
found_cycles_open_sequence = len(cycles_open_sequence.shape) == 2
if found_cycles_firstpass and found_cycles_open_sequence:
cycles = np.concatenate((cycles_firstpass, cycles_open_sequence))
elif found_cycles_firstpass:
cycles = cycles_firstpass
elif found_cycles_open_sequence:
cycles = cycles_open_sequence
else:
raise ValueError("Could not find any cycles in sequence")
cycles, residue = find_rainflow_cycles(reversals, include_open_cycles=True)
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
if return_means:
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
Expand Down Expand Up @@ -562,19 +570,7 @@ def find_rainflow_ranges_strict(y, k=64, return_means=False):
"""

reversals, _ = find_reversals_strict(y, k)
cycles_firstpass, residue = find_rainflow_cycles(reversals)
processed_residue = concatenate_reversals(residue, residue)
cycles_open_sequence, _ = find_rainflow_cycles(processed_residue)
found_cycles_firstpass = len(cycles_firstpass.shape) == 2
found_cycles_open_sequence = len(cycles_open_sequence.shape) == 2
if found_cycles_firstpass and found_cycles_open_sequence:
cycles = np.concatenate((cycles_firstpass, cycles_open_sequence))
elif found_cycles_firstpass:
cycles = cycles_firstpass
elif found_cycles_open_sequence:
cycles = cycles_open_sequence
else:
raise ValueError("Could not find any cycles in sequence")
cycles, residue = find_rainflow_cycles(reversals, include_open_cycles=True)
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
if return_means:
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
Expand Down