Skip to content

Commit

Permalink
Readded soc function
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianp14 committed Jun 18, 2024
1 parent 7a36d00 commit 74cd7b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 6 additions & 6 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_update(self, battery, power, duration, exp_charge_energy, exp_charge_le
charge_energy = battery.update(power=power, duration=duration * 3600)
assert charge_energy == exp_charge_energy * 3600
assert battery.state()["charge_level"] == exp_charge_level
assert math.isclose(battery.state()["soc"], exp_soc)
assert math.isclose(battery.soc(), exp_soc)

@pytest.mark.parametrize(
"power, duration, exp_charge_energy, exp_charge_level, exp_soc",
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_update_c_rate(
charge_energy = battery_c.update(power=power, duration=duration * 60)
assert charge_energy == exp_charge_energy * 3600
assert battery_c.state()["charge_level"] == exp_charge_level
assert math.isclose(battery_c.state()["soc"], exp_soc)
assert math.isclose(battery_c.soc(), exp_soc)

def test_update_fails_if_duration_not_positive(self, battery):
with pytest.raises(ValueError):
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_apply_no_charge_mode(self, battery, policy, power, duration, exp_delta,
# duration in hours and energy delta in Wh
delta = policy.apply(power, duration * 3600, battery)
assert delta == exp_delta * 3600
assert math.isclose(battery.state()["soc"], exp_soc)
assert math.isclose(battery.soc(), exp_soc)

@pytest.mark.parametrize(
"power, duration, exp_delta, exp_soc",
Expand All @@ -136,7 +136,7 @@ def test_apply_charge(self, battery, policy_charge, power, duration, exp_delta,
# duration in hours and energy delta in Wh
delta = policy_charge.apply(power, duration * 3600, battery)
assert delta == exp_delta * 3600
assert math.isclose(battery.state()["soc"], exp_soc)
assert math.isclose(battery.soc(), exp_soc)

@pytest.mark.parametrize(
"power, duration, exp_delta, exp_soc",
Expand All @@ -159,7 +159,7 @@ def test_apply_discharge(self, battery, policy_discharge, power, duration, exp_d
# duration in hours and energy delta in Wh
delta = policy_discharge.apply(power, duration * 3600, battery)
assert delta == exp_delta * 3600
assert math.isclose(battery.state()["soc"], exp_soc)
assert math.isclose(battery.soc(), exp_soc)

@pytest.mark.parametrize(
"power, duration, exp_delta, exp_soc",
Expand All @@ -178,7 +178,7 @@ def test_apply_islanded(self, battery, policy_islanded, power, duration, exp_del
# duration in hours and energy delta in Wh
delta = policy_islanded.apply(power, duration * 3600, battery)
assert delta == exp_delta * 3600
assert math.isclose(battery.state()["soc"], exp_soc)
assert math.isclose(battery.soc(), exp_soc)

@pytest.mark.parametrize(
"power, duration, exp_delta",
Expand Down
24 changes: 17 additions & 7 deletions vessim/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def update(self, power: float, duration: int) -> float:
The total energy in Ws that has been charged/discharged.
"""

@abstractmethod
def soc(self) -> float:
"""Returns the state-of-charge (SoC) of the battery.
Values should range between 0 (empty) and 1 (full).
"""

def set_parameter(self, key: str, value: Any) -> None:
"""Fuction to let a controller update a storage parameter during a simulation using Mosaik.
Expand Down Expand Up @@ -56,8 +63,8 @@ def __init__(
self.capacity = capacity
assert 0 <= initial_soc <= 1
self.charge_level = capacity * initial_soc
self.soc = initial_soc
assert 0 <= min_soc <= self.soc
self._soc = initial_soc
assert 0 <= min_soc <= self._soc
self.min_soc = min_soc
self.c_rate = c_rate

Expand All @@ -72,7 +79,7 @@ def update(self, power: float, duration: int) -> float:
if duration <= 0.0:
raise ValueError("Duration needs to be a positive value")

assert self.min_soc <= self.soc, "Minimum SoC can not be smaller than the current SoC"
assert self.min_soc <= self._soc, "Minimum SoC can not be smaller than the current SoC"
if self.c_rate is not None:
max_power = self.c_rate * self.capacity
if power >= max_power:
Expand All @@ -99,22 +106,25 @@ def update(self, power: float, duration: int) -> float:
# Battery can not be discharged further than the minimum state-of-charge
charged_energy = abs_min_soc - self.charge_level
self.charge_level = abs_min_soc
self.soc = self.min_soc
self._soc = self.min_soc
elif new_charge_level > self.capacity:
# Battery can not be charged past its capacity
charged_energy = self.capacity - self.charge_level
self.charge_level = self.capacity
self.soc = 1.0
self._soc = 1.0
else:
self.charge_level = new_charge_level
self.soc = self.charge_level / self.capacity
self._soc = self.charge_level / self.capacity

return charged_energy * 3600 # Wh to Ws

def soc(self) -> float:
return self._soc

def state(self) -> dict:
"""Returns state information of the battery as a dict."""
return {
"soc": self.soc,
"soc": self._soc,
"charge_level": self.charge_level,
"capacity": self.capacity,
"min_soc": self.min_soc,
Expand Down

0 comments on commit 74cd7b3

Please sign in to comment.