Streaming rainflow cycle counting and range-mean spectra for measured load histories
git --version
moon version --allgit clone https://github.com/Mr-Shi123/MoonRainflow.git
cd MoonRainflow
moon run cmd/demo --target wasm-gcmoon new rainflow-example
cd rainflow-example
moon add Mr-Shi123/moonrainflowimport {
"Mr-Shi123/moonrainflow" @rain,
}
pkgtype(kind: "executable")///|
fn example() -> Unit raise @rain.RainError {
let cycles = @rain.extract([0.0, 4.0, 0.0])
let grid = @rain.Spectrum::new([0.0, 4.0], [0.0, 4.0])
grid.add_chunk(cycles)
println("Cycle records: \{cycles.length()}")
println("Half-cycle units: \{grid.total_units()}")
}
///|
fn main {
example() catch {
e => abort("invalid example: \{e}")
}
}moon run cmd/main --target wasm-gcCycle records: 2
Half-cycle units: 2moon run cmd/demo --target wasm-gcmoon build --target js --release cmd/bridge
node cmd/rainflow.mjs examples/mathworks-nine.txt
node cmd/rainflow.mjs measurements.txt > cycles.csvnode cmd/rainflow.mjs --min-range 4 examples/mathworks-nine.txt > large-cycles.csvnode cmd/rainflow.mjs --summary --min-range 4 examples/mathworks-nine.txt > cycles.csvSamples: 9
Counted cycles: 7 records; 4 equivalent cycles
Exported cycles: 6 records; 3.5 equivalent cycleslet counter = @rain.Counter::new()
let grid = @rain.Spectrum::new([0.0, 4.0, 8.0], [-1.0, 0.0, 1.0])
grid.add_chunk(counter.push_chunk([-2.0, 1.0, -3.0, 5.0]))
grid.add_chunk(counter.finish())let counter = @rain.Counter::new()
let confirmed = counter.push_chunk([0.0, 5.0, 1.0]) // No settled cycles yet.
let provisional = counter.preview_finish() // Two half cycles: ranges 5 and 4.
confirmed.append(counter.push(6.0))
confirmed.append(counter.finish())
// confirmed: a full cycle of range 4, then a half cycle of range 6.let grid = @rain.Spectrum::new([0.0, 4.0, 8.0], [-1.0, 0.0, 1.0])
for cycle in @rain.extract([-2.0, 1.0, -3.0, 5.0, -1.0, 3.0, -4.0, 4.0, -2.0]) {
grid.add(cycle)
}
let by_range = grid.range_units() // [1L, 6L]
let by_mean = grid.mean_units() // [2L, 5L]let ranges = [0.0, 5.0]
let means = [-5.0, 5.0, 105.0]
let combined = @rain.Spectrum::new(ranges, means)
for samples in [[0.0, 4.0], [100.0, 104.0]] {
let run = @rain.Spectrum::new(ranges, means)
for cycle in @rain.extract(samples) { run.add(cycle) }
combined.merge(run)
}
// combined.matrix() == [[1L, 1L]]: one half cycle in each mean bin.let summary = @rain.CycleSummary::new()
summary.add_chunk(@rain.extract([-2.0, 1.0, -3.0, 5.0, -1.0, 3.0, -4.0, 4.0, -2.0]))
let totals = summary.snapshot()
// records = 7, half_records = 6, full_records = 1, half_units = 8
println(totals.equivalent_cycles()) // 4
println(totals.to_csv())let fine = @rain.Spectrum::new([0.0, 4.0, 8.0], [-1.0, 0.0, 1.0])
for cycle in @rain.extract([-2.0, 1.0, -3.0, 5.0, -1.0, 3.0, -4.0, 4.0, -2.0]) {
fine.add(cycle)
}
let coarse = fine.coarsen([0.0, 8.0], [-1.0, 1.0])
// coarse.matrix() == [[7L]], outside_units() == 1L, total_units() == 8L
println(coarse.to_csv())node scripts/check_source_size.mjs
moon fmt --check
moon test --target wasm-gc --deny-warn
moon test --target js --deny-warn
moon build --target js --release cmd/bridge --deny-warn
node --test scripts/cli.test.mjspub(all) suberror RainError {
InvalidSample(Int64)
ClosedCounter
IndexLimit
InvalidEdges(String)
CountLimit
}pub struct Counter {
// private fields
}pub struct Cycle {
start_index : Int64
end_index : Int64
range : Double
mean : Double
half_units : Int
} derive(Eq)pub struct CycleSummary {
// private fields
}pub struct CycleTotals {
records : Int64
half_records : Int64
full_records : Int64
half_units : Int64
min_range : Double?
max_range : Double?
min_mean : Double?
max_mean : Double?
}pub struct Spectrum {
// private fields
}pub struct SpectrumCell {
range_index : Int
mean_index : Int
range_lower : Double
range_upper : Double
mean_lower : Double
mean_upper : Double
half_units : Int64
}Install
Download zipStreaming rainflow cycle counting and range-mean spectra for measured load histories