From 8d6feb6a66860b861aec2183b221e30aff185d3e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:04:58 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/configtables/sector.csv | 2 +- .../build_cop_profiles/BaseCopApproximator.py | 76 ++++++++++++------- .../CentralHeatingCopApproximator.py | 67 ++++++++++------ .../DecentralHeatingCopApproximator.py | 43 ++++++----- scripts/build_cop_profiles/run.py | 26 ++++--- 5 files changed, 132 insertions(+), 82 deletions(-) diff --git a/doc/configtables/sector.csv b/doc/configtables/sector.csv index bb74035e..de2873ed 100644 --- a/doc/configtables/sector.csv +++ b/doc/configtables/sector.csv @@ -160,4 +160,4 @@ enhanced_geothermal,,, #NAME?,--,int,The maximum hours the reservoir can be charged under flexible operation #NAME?,--,float,The maximum boost in power output under flexible operation #NAME?,--,"{true, false}",Add option for variable capacity factor (see Ricks et al. 2024) -#NAME?,--,float,Share of sourced heat that is replenished by the earth's core (see details in `build_egs_potentials.py `_) \ No newline at end of file +#NAME?,--,float,Share of sourced heat that is replenished by the earth's core (see details in `build_egs_potentials.py `_) diff --git a/scripts/build_cop_profiles/BaseCopApproximator.py b/scripts/build_cop_profiles/BaseCopApproximator.py index 87343d36..247ff0fe 100644 --- a/scripts/build_cop_profiles/BaseCopApproximator.py +++ b/scripts/build_cop_profiles/BaseCopApproximator.py @@ -1,32 +1,39 @@ +# -*- coding: utf-8 -*- from abc import ABC, abstractmethod from typing import Union -import xarray as xr + import numpy as np +import xarray as xr + class BaseCopApproximator(ABC): """ - Abstract class for approximating the coefficient of performance (COP) of a heat pump.""" - def __init__( - self, - forward_temperature_celsius: Union[xr.DataArray, np.array], - source_inlet_temperature_celsius: Union[xr.DataArray, np.array], - ): - """ - Initialize CopApproximator. + Abstract class for approximating the coefficient of performance (COP) of a + heat pump. + """ + + def __init__( + self, + forward_temperature_celsius: Union[xr.DataArray, np.array], + source_inlet_temperature_celsius: Union[xr.DataArray, np.array], + ): + """ + Initialize CopApproximator. + + Parameters: + ---------- + forward_temperature_celsius : Union[xr.DataArray, np.array] + The forward temperature in Celsius. + return_temperature_celsius : Union[xr.DataArray, np.array] + The return temperature in Celsius. + """ + pass - Parameters: - ---------- - forward_temperature_celsius : Union[xr.DataArray, np.array] - The forward temperature in Celsius. - return_temperature_celsius : Union[xr.DataArray, np.array] - The return temperature in Celsius. - """ - pass - @abstractmethod def approximate_cop(self) -> Union[xr.DataArray, np.array]: - """Approximate heat pump coefficient of performance (COP). + """ + Approximate heat pump coefficient of performance (COP). Returns: ------- @@ -34,28 +41,39 @@ class BaseCopApproximator(ABC): The calculated COP values. """ pass - - def celsius_to_kelvin(t_celsius: Union[float, xr.DataArray, np.array]) -> Union[float, xr.DataArray, np.array]: + + def celsius_to_kelvin( + t_celsius: Union[float, xr.DataArray, np.array] + ) -> Union[float, xr.DataArray, np.array]: if (np.asarray(t_celsius) > 200).any(): - raise ValueError("t_celsius > 200. Are you sure you are using the right units?") + raise ValueError( + "t_celsius > 200. Are you sure you are using the right units?" + ) return t_celsius + 273.15 - - def logarithmic_mean(t_hot: Union[float, xr.DataArray, np.ndarray], t_cold: Union[float, xr.DataArray, np.ndarray]) -> Union[float, xr.DataArray, np.ndarray]: + def logarithmic_mean( + t_hot: Union[float, xr.DataArray, np.ndarray], + t_cold: Union[float, xr.DataArray, np.ndarray], + ) -> Union[float, xr.DataArray, np.ndarray]: if (np.asarray(t_hot <= t_cold)).any(): raise ValueError("t_hot must be greater than t_cold") return (t_hot - t_cold) / np.log(t_hot / t_cold) @staticmethod - def celsius_to_kelvin(t_celsius: Union[float, xr.DataArray, np.array]) -> Union[float, xr.DataArray, np.array]: + def celsius_to_kelvin( + t_celsius: Union[float, xr.DataArray, np.array] + ) -> Union[float, xr.DataArray, np.array]: if (np.asarray(t_celsius) > 200).any(): - raise ValueError("t_celsius > 200. Are you sure you are using the right units?") + raise ValueError( + "t_celsius > 200. Are you sure you are using the right units?" + ) return t_celsius + 273.15 @staticmethod - def logarithmic_mean(t_hot: Union[float, xr.DataArray, np.ndarray], t_cold: Union[float, xr.DataArray, np.ndarray]) -> Union[float, xr.DataArray, np.ndarray]: + def logarithmic_mean( + t_hot: Union[float, xr.DataArray, np.ndarray], + t_cold: Union[float, xr.DataArray, np.ndarray], + ) -> Union[float, xr.DataArray, np.ndarray]: if (np.asarray(t_hot <= t_cold)).any(): raise ValueError("t_hot must be greater than t_cold") return (t_hot - t_cold) / np.log(t_hot / t_cold) - - diff --git a/scripts/build_cop_profiles/CentralHeatingCopApproximator.py b/scripts/build_cop_profiles/CentralHeatingCopApproximator.py index 9b445426..f2b8d80c 100644 --- a/scripts/build_cop_profiles/CentralHeatingCopApproximator.py +++ b/scripts/build_cop_profiles/CentralHeatingCopApproximator.py @@ -1,16 +1,21 @@ +# -*- coding: utf-8 -*- from typing import Union -import xarray as xr -import numpy as np +import numpy as np +import xarray as xr from BaseCopApproximator import BaseCopApproximator + class CentralHeatingCopApproximator(BaseCopApproximator): """ - Approximate the coefficient of performance (COP) for a heat pump in a central heating system (district heating). - - Uses an approximation method proposed by Jensen et al. (2018) and default parameters from Pieper et al. (2020). - The method is based on a thermodynamic heat pump model with some hard-to-know parameters being approximated. + Approximate the coefficient of performance (COP) for a heat pump in a + central heating system (district heating). + + Uses an approximation method proposed by Jensen et al. (2018) and + default parameters from Pieper et al. (2020). The method is based on + a thermodynamic heat pump model with some hard-to-know parameters + being approximated. """ def __init__( @@ -42,11 +47,19 @@ class CentralHeatingCopApproximator(BaseCopApproximator): heat_loss : float, optional The heat loss, by default 0.0. """ - self.t_source_in_kelvin = BaseCopApproximator.celsius_to_kelvin(source_inlet_temperature_celsius) - self.t_sink_out_kelvin = BaseCopApproximator.celsius_to_kelvin(forward_temperature_celsius) + self.t_source_in_kelvin = BaseCopApproximator.celsius_to_kelvin( + source_inlet_temperature_celsius + ) + self.t_sink_out_kelvin = BaseCopApproximator.celsius_to_kelvin( + forward_temperature_celsius + ) - self.t_sink_in_kelvin = BaseCopApproximator.celsius_to_kelvin(return_temperature_celsius) - self.t_source_out = BaseCopApproximator.celsius_to_kelvin(source_outlet_temperature_celsius) + self.t_sink_in_kelvin = BaseCopApproximator.celsius_to_kelvin( + return_temperature_celsius + ) + self.t_source_out = BaseCopApproximator.celsius_to_kelvin( + source_outlet_temperature_celsius + ) self.isentropic_efficiency_compressor_kelvin = isentropic_compressor_efficiency self.heat_loss = heat_loss @@ -87,14 +100,17 @@ class CentralHeatingCopApproximator(BaseCopApproximator): @property def t_sink_mean_kelvin(self) -> Union[xr.DataArray, np.array]: """ - Calculate the logarithmic mean temperature difference between the cold and hot sinks. + Calculate the logarithmic mean temperature difference between the cold + and hot sinks. Returns ------- Union[xr.DataArray, np.array] The mean temperature difference. """ - return BaseCopApproximator.logarithmic_mean(t_cold=self.t_sink_in_kelvin, t_hot=self.t_sink_out_kelvin) + return BaseCopApproximator.logarithmic_mean( + t_cold=self.t_sink_in_kelvin, t_hot=self.t_sink_out_kelvin + ) @property def t_source_mean_kelvin(self) -> Union[xr.DataArray, np.array]: @@ -106,12 +122,15 @@ class CentralHeatingCopApproximator(BaseCopApproximator): Union[xr.DataArray, np.array] The mean temperature of the heat source. """ - return BaseCopApproximator.logarithmic_mean(t_hot=self.t_source_in_kelvin, t_cold=self.t_source_out) + return BaseCopApproximator.logarithmic_mean( + t_hot=self.t_source_in_kelvin, t_cold=self.t_source_out + ) @property def delta_t_lift(self) -> Union[xr.DataArray, np.array]: """ - Calculate the temperature lift as the difference between the logarithmic sink and source temperatures. + Calculate the temperature lift as the difference between the + logarithmic sink and source temperatures. Returns ------- @@ -132,14 +151,14 @@ class CentralHeatingCopApproximator(BaseCopApproximator): ------- np.array The ideal Lorenz COP. - """ return self.t_sink_mean_kelvin / self.delta_t_lift @property def delta_t_refrigerant_source(self) -> Union[xr.DataArray, np.array]: """ - Calculate the temperature difference between the refrigerant source inlet and outlet. + Calculate the temperature difference between the refrigerant source + inlet and outlet. Returns ------- @@ -153,7 +172,8 @@ class CentralHeatingCopApproximator(BaseCopApproximator): @property def delta_t_refrigerant_sink(self) -> Union[xr.DataArray, np.array]: """ - Temperature difference between the refrigerant and the sink based on approximation. + Temperature difference between the refrigerant and the sink based on + approximation. Returns ------- @@ -165,7 +185,8 @@ class CentralHeatingCopApproximator(BaseCopApproximator): @property def ratio_evaporation_compression_work(self) -> Union[xr.DataArray, np.array]: """ - Calculate the ratio of evaporation to compression work based on approximation. + Calculate the ratio of evaporation to compression work based on + approximation. Returns ------- @@ -173,7 +194,7 @@ class CentralHeatingCopApproximator(BaseCopApproximator): The calculated ratio of evaporation to compression work. """ return self._ratio_evaporation_compression_work_approximation() - + @property def delta_t_sink(self) -> Union[xr.DataArray, np.array]: """ @@ -190,7 +211,8 @@ class CentralHeatingCopApproximator(BaseCopApproximator): self, delta_t_source: Union[xr.DataArray, np.array] ) -> Union[xr.DataArray, np.array]: """ - Approximates the temperature difference between the refrigerant and the source. + Approximates the temperature difference between the refrigerant and the + source. Parameters ---------- @@ -212,7 +234,8 @@ class CentralHeatingCopApproximator(BaseCopApproximator): c: float = {"ammonia": 0.016, "isobutane": 2.4}, ) -> Union[xr.DataArray, np.array]: """ - Approximates the temperature difference between the refrigerant and heat sink. + Approximates the temperature difference between the refrigerant and + heat sink. Parameters: ---------- @@ -236,7 +259,6 @@ class CentralHeatingCopApproximator(BaseCopApproximator): The approximate temperature difference at the refrigerant sink is calculated using the following formula: a * (t_sink_out - t_source_out + 2 * delta_t_pinch) + b * delta_t_sink + c - """ if refrigerant not in a.keys(): raise ValueError( @@ -292,4 +314,3 @@ class CentralHeatingCopApproximator(BaseCopApproximator): + b[refrigerant] * self.delta_t_sink + c[refrigerant] ) - diff --git a/scripts/build_cop_profiles/DecentralHeatingCopApproximator.py b/scripts/build_cop_profiles/DecentralHeatingCopApproximator.py index 03ca63fe..b49be54c 100644 --- a/scripts/build_cop_profiles/DecentralHeatingCopApproximator.py +++ b/scripts/build_cop_profiles/DecentralHeatingCopApproximator.py @@ -1,16 +1,18 @@ - +# -*- coding: utf-8 -*- from typing import Union -import xarray as xr -import numpy as np +import numpy as np +import xarray as xr from BaseCopApproximator import BaseCopApproximator + class DecentralHeatingCopApproximator(BaseCopApproximator): """ - Approximate the coefficient of performance (COP) for a heat pump in a decentral heating system (individual/household heating). - + Approximate the coefficient of performance (COP) for a heat pump in a + decentral heating system (individual/household heating). + Uses a quadratic regression on the temperature difference between the source and sink based on empirical data proposed by Staffell et al. 2012 . References @@ -22,7 +24,7 @@ class DecentralHeatingCopApproximator(BaseCopApproximator): self, forward_temperature_celsius: Union[xr.DataArray, np.array], source_inlet_temperature_celsius: Union[xr.DataArray, np.array], - source_type: str + source_type: str, ): """ Initialize the COPProfileBuilder object. @@ -36,18 +38,20 @@ class DecentralHeatingCopApproximator(BaseCopApproximator): source: str The source of the heat pump. Must be either 'air' or 'soil' """ - + self.delta_t = forward_temperature_celsius - source_inlet_temperature_celsius if source_type not in ["air", "soil"]: raise ValueError("'source' must be one of ['air', 'soil']") else: self.source_type = source_type - + def approximate_cop(self) -> Union[xr.DataArray, np.array]: """ - Compute output of quadratic regression for air-/ground-source heat pumps. - - Calls the appropriate method depending on `source`.""" + Compute output of quadratic regression for air-/ground-source heat + pumps. + + Calls the appropriate method depending on `source`. + """ if self.source_type == "air": return self._approximate_cop_air_source() elif self.source_type == "soil": @@ -56,24 +60,25 @@ class DecentralHeatingCopApproximator(BaseCopApproximator): def _approximate_cop_air_source(self) -> Union[xr.DataArray, np.array]: """ Evaluate quadratic regression for an air-sourced heat pump. - + COP = 6.81 - 0.121 * delta_T + 0.000630 * delta_T^2 - + Returns ------- Union[xr.DataArray, np.array] - The calculated COP values.""" + The calculated COP values. + """ return 6.81 - 0.121 * self.delta_t + 0.000630 * self.delta_t**2 - + def _approximate_cop_ground_source(self) -> Union[xr.DataArray, np.array]: """ Evaluate quadratic regression for a ground-sourced heat pump. - + COP = 8.77 - 0.150 * delta_T + 0.000734 * delta_T^2 - + Returns ------- Union[xr.DataArray, np.array] - The calculated COP values.""" + The calculated COP values. + """ return 8.77 - 0.150 * self.delta_t + 0.000734 * self.delta_t**2 - \ No newline at end of file diff --git a/scripts/build_cop_profiles/run.py b/scripts/build_cop_profiles/run.py index 2568476f..c25b80a3 100644 --- a/scripts/build_cop_profiles/run.py +++ b/scripts/build_cop_profiles/run.py @@ -1,10 +1,11 @@ +# -*- coding: utf-8 -*- -import xarray as xr import numpy as np +import xarray as xr +from _helpers import set_scenario_config from CentralHeatingCopApproximator import CentralHeatingCopApproximator from DecentralHeatingCopApproximator import DecentralHeatingCopApproximator -from _helpers import set_scenario_config if __name__ == "__main__": if "snakemake" not in globals(): @@ -19,24 +20,29 @@ if __name__ == "__main__": set_scenario_config(snakemake) for source_type in ["air", "soil"]: - # source inlet temperature (air/soil) is based on weather data - source_inlet_temperature_celsius = xr.open_dataarray(snakemake.input[f"temp_{source_type}_total"]) + # source inlet temperature (air/soil) is based on weather data + source_inlet_temperature_celsius = xr.open_dataarray( + snakemake.input[f"temp_{source_type}_total"] + ) # Approximate COP for decentral (individual) heating cop_individual_heating = DecentralHeatingCopApproximator( forward_temperature_celsius=snakemake.params.heat_pump_sink_T_decentral_heating, source_inlet_temperature_celsius=source_inlet_temperature_celsius, - source_type=source_type + source_type=source_type, ).approximate_cop() - cop_individual_heating.to_netcdf(snakemake.output[f"cop_{source_type}_decentral_heating"]) + cop_individual_heating.to_netcdf( + snakemake.output[f"cop_{source_type}_decentral_heating"] + ) # Approximate COP for central (district) heating cop_central_heating = CentralHeatingCopApproximator( forward_temperature_celsius=snakemake.params.forward_temperature_central_heating, return_temperature_celsius=snakemake.params.return_temperature_central_heating, source_inlet_temperature_celsius=source_inlet_temperature_celsius, - source_outlet_temperature_celsius=source_inlet_temperature_celsius - snakemake.params.heat_source_cooling_central_heating, + source_outlet_temperature_celsius=source_inlet_temperature_celsius + - snakemake.params.heat_source_cooling_central_heating, ).approximate_cop() - cop_central_heating.to_netcdf(snakemake.output[f"cop_{source_type}_central_heating"]) - - + cop_central_heating.to_netcdf( + snakemake.output[f"cop_{source_type}_central_heating"] + )