diff --git a/epanet/epanet.py b/epanet/epanet.py index 76830d6..35a702f 100644 --- a/epanet/epanet.py +++ b/epanet/epanet.py @@ -27,6 +27,11 @@ class Output: self._check(self._lib.ENR_open(self._handle, ctypes.c_char_p(self._path.encode()))) + def __del__(self): + # throw exception in destructor ? :) + self._check(self._lib.ENR_close(ctypes.byref(self._handle))) + + def _check(self, result): if result != 0: msg = ctypes.c_char_p() @@ -108,7 +113,6 @@ class Output: return { 'nodes' : nodes, 'links': links } - # { pump_index, link_index, utilization, avg.efficiency, avg.kW/flow, avg.kwatts, max.kwatts, cost/day } def energy_usage(self) -> list[dict[str, Any]]: size = self.net_size()['pump'] usages = [] @@ -126,6 +130,18 @@ class Output: return usages + def reactions(self) -> dict[str, float]: + values = ctypes.POINTER(ctypes.c_float)() + length = ctypes.c_int() + self._check(self._lib.ENR_getNetReacts(self._handle, ctypes.byref(values), ctypes.byref(length))) + assert length.value == 4 + category = ['bulk', 'wall', 'tank', 'source'] + d = {} + for i in range(4): + d[category[i]] = values[i] + return d + + def dump(self, cache: bool = True) -> str: data = {} data |= { 'version' : self.version() } @@ -134,17 +150,13 @@ class Output: data |= { 'times' : self.times() } data |= { 'element_name' : self.element_name() } data |= { 'energy_usage' : self.energy_usage() } + data |= { 'reactions' : self.reactions() } if cache: with open(self._path + '.json', 'w') as f: json.dump(data, f) return json.dumps(data) - def __del__(self): - # throw exception in destructor ? :) - self._check(self._lib.ENR_close(ctypes.byref(self._handle))) - - def dump_output(path: str) -> str: opt = Output(path) return opt.dump()