Units and sign
Power in whole watts with consumption positive, compile-time literals, Deviation relative to a baseline, Energy in watt-seconds, and named rounding.
One sign: consumption is positive#
Power is an exact amount of power in whole watts, stored as an i64. Positive is consumption, negative is feed-in. This is UFTP 3.1 § 4.2.1: "A positive value indicates that power flows towards the prosumer (consumption), a negative value indicates flow towards the grid (production)."
"3 kW less consumption" and "3 kW more feed-in" are the same flexibility and give the same numbers. A protocol with another sign convention changes sign at its own boundary, once.
use flexibility::{kw, Direction, Power};
assert_eq!(kw!(4.2), Power::from_watts(4_200));
assert_eq!(Power::feed_in(3_000)?, Power::from_watts(-3_000));
assert_eq!(Power::consumption(500)?.direction(), Direction::Consumption);
assert_eq!(kw!(-3).magnitude(), 3_000);Prefer Power::consumption and Power::feed_in (which take an unsigned amount) over negating by hand.
Literals: w!, kw!, mw!#
The literal macros read the literal's text at compile time, never a float, so kw!(4.2) is exactly 4 200 W. A value that is not a whole number of watts does not compile:
use flexibility::{kw, mw, w, Power};
assert_eq!(w!(500), Power::from_watts(500));
assert_eq!(kw!(4.2), Power::from_watts(4_200));
assert_eq!(kw!(-3), Power::from_watts(-3_000));
assert_eq!(mw!(1.5), Power::from_watts(1_500_000));
// let _ = kw!(4.0001); // does not compile: 4 200.1 W is not whole watts
// let _ = w!(0.5); // does not compile eitherText in and out#
Power parses and displays W, kW and MW, exactly. The space before the unit is optional. {:#} prints whole watts.
use flexibility::{kw, Power};
assert_eq!("-3 kW".parse::<Power>()?, Power::from_watts(-3_000));
assert_eq!("1.5 MW".parse::<Power>()?, Power::from_watts(1_500_000));
assert_eq!(kw!(4.2).to_string(), "4.2 kW");
assert_eq!(format!("{:#}", kw!(4.2)), "4200 W");
assert!("4.0001 kW".parse::<Power>().is_err()); // refused, never roundedAbsolute and relative: Deviation#
A limit or a meter value is an absolute power. A flexibility order is relative: "3 kW less than the baseline". Mixing the two is a common settlement bug, so the relative quantity has a type of its own, Deviation, and it is always displayed with a sign.
The only crossings name the baseline:
use flexibility::kw;
let measured = kw!(4.1);
let baseline = kw!(7.4);
let d = measured.deviation_from(baseline)?; // measured − baseline
assert_eq!(d.to_string(), "-3.3 kW");
assert_eq!(baseline.checked_add_deviation(d)?, measured);Deviation parses +2.2 kW, -3.3 kW and 0 W; Deviation::from_watts builds one from signed watts.
Energy: whole watt-seconds#
An ISP is a whole number of minutes and power is whole watts, so power × ISP is always exact. Energy stores signed watt-seconds and displays in Wh, kWh or MWh when that is exact to six decimals, otherwise in Ws.
use flexibility::{w, Energy, IspDuration};
let e = w!(1).energy(IspDuration::FIFTEEN_MINUTES)?; // 1 W for 15 minutes
assert_eq!(e, Energy::from_watt_seconds(900));
assert_eq!(e.to_string(), "0.25 Wh");Series<Power>::energy() sums a whole series exactly.
Division names its rounding#
Going from energy back to power divides. Power::average takes a Rounding: Exact refuses a remainder, HalfAwayFromZero rounds to the nearest watt with halves away from zero, and TowardZero never exceeds the exact magnitude (the strict side for a cap read from a coarse encoding).
use flexibility::{Energy, IspDuration, Power, Rounding};
let wh = Energy::from_wh(1)?; // 1 Wh in a quarter hour is 4 W, exactly
assert_eq!(Power::average(wh, IspDuration::FIFTEEN_MINUTES, Rounding::Exact)?, Power::from_watts(4));
let ws = Energy::from_watt_seconds(1);
assert!(Power::average(ws, IspDuration::FIFTEEN_MINUTES, Rounding::Exact).is_err());Overflow is an error#
All arithmetic is checked: checked_add, checked_sub, checked_neg and the crossings return Error::Overflow naming the operands, never a wrapped value.
use flexibility::{w, Power};
let err = Power::from_watts(i64::MAX).checked_add(w!(1)).unwrap_err();
assert_eq!(err.to_string(), "power overflow: 9223372036854775807 + 1");Next: Time and ISPs, where these values live.