Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ print(results.head())

See [`examples/shanghai_household.py`](examples/shanghai_household.py) for a complete runnable example.


### Use measured or external weather data

Pass a timezone-indexed `pandas.DataFrame` with `ghi`, `dni` and `dhi` in
Expand All @@ -89,6 +90,12 @@ Naive timestamps are localised to `tz`; timezone-aware timestamps are
converted to it. The frame is not resampled, so the output contains one row
per input timestamp. See the function docstring for validation rules.



Dispatch inputs must be one-dimensional, finite, non-negative power profiles.
The profiles must contain at least one value, and `dt_h` must be a finite
positive timestep in hours. Invalid inputs raise `ValueError` rather than
silently producing misleading energy balances.
## Why this doesn't exist yet

PV-yield tools (pvlib) model generation beautifully but stop at the meter. Battery tools (PyBaMM) model electrochemistry beautifully but stop at the cell terminals. Techno-economic studies usually bolt a *constant* battery onto a PV profile and call it a sizing study — while the literature (e.g. recent solar-plus-storage sizing work) explicitly flags that neglecting long-term cycling and degradation biases lifecycle cost. pvfade is the missing coupling layer: physics-informed fade inside the economic loop.
Expand Down
14 changes: 12 additions & 2 deletions src/pvfade/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ def self_consumption_dispatch(
load = np.asarray(load_kw, dtype=float)
if pv.shape != load.shape:
raise ValueError("pv_kw and load_kw must have the same length")
if pv.ndim != 1 or load.ndim != 1:
raise ValueError("pv_kw and load_kw must be one-dimensional")
if pv.size == 0:
raise ValueError("pv_kw and load_kw must contain at least one value")
if not np.isfinite(pv).all() or not np.isfinite(load).all():
raise ValueError("pv_kw and load_kw must contain only finite values")
if np.any(pv < 0) or np.any(load < 0):
raise ValueError("pv_kw and load_kw must be non-negative")

usable = spec.nominal_kwh if usable_kwh is None else usable_kwh
if usable <= 0:
raise ValueError("usable_kwh must be positive")
if not np.isfinite(usable) or usable <= 0:
raise ValueError("usable_kwh must be a finite positive value")
if not np.isfinite(initial_soc_frac):
raise ValueError("initial_soc_frac must be finite")
if not np.isfinite(dt_h) or dt_h <= 0:
raise ValueError("dt_h must be a finite positive value")
soc_lo = spec.soc_min * usable
soc_hi = spec.soc_max * usable
soc = float(np.clip(initial_soc_frac, spec.soc_min, spec.soc_max)) * usable
Expand Down
25 changes: 25 additions & 0 deletions tests/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ def test_dispatch_rejects_mismatched_lengths():
_spec())


@pytest.mark.parametrize(
"pv, load, message",
[
(pd.DataFrame([[1.0]]), pd.DataFrame([[1.0]]), "one-dimensional"),
(pd.Series([], dtype=float), pd.Series([], dtype=float), "at least one"),
(pd.Series([np.nan]), pd.Series([1.0]), "finite"),
(pd.Series([1.0]), pd.Series([np.inf]), "finite"),
],
)
def test_dispatch_rejects_invalid_profiles(pv, load, message):
with pytest.raises(ValueError, match=message):
self_consumption_dispatch(pv, load, _spec())


def test_dispatch_rejects_invalid_timing_and_capacity():
pv = pd.Series([1.0])
load = pd.Series([1.0])
with pytest.raises(ValueError, match="usable_kwh"):
self_consumption_dispatch(pv, load, _spec(), usable_kwh=np.inf)
with pytest.raises(ValueError, match="initial_soc_frac"):
self_consumption_dispatch(pv, load, _spec(), initial_soc_frac=np.nan)
with pytest.raises(ValueError, match="dt_h"):
self_consumption_dispatch(pv, load, _spec(), dt_h=0.0)


def test_simulate_lifetime_fades_over_time():
pv = pd.Series(np.full(8760, 2.0))
load = pd.Series(np.full(8760, 1.5))
Expand Down
Loading