<commit_before># -*- coding: utf-8 -*-
"""
Sewage source heat exchanger
"""
from __future__ import division
import pandas as pd
import numpy as np
import scipy
from cea.constants import HEX_WIDTH_M,VEL_FLOW_MPERS, HEAT_CAPACITY_OF_WATER_JPERKGK, H0_KWPERM2K, MIN_FLOW_MPERS, T_MIN, AT_MIN_K
import cea.config
import cea.globalvar
import cea.inputlocator

__author__ = "<NAME>"
__copyright__ = "Copyright 2015, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


def calc_sewage_heat_exchanger(locator, config):
    """
    Calaculate the heat extracted from the sewage HEX.

    :param locator: an InputLocator instance set to the scenario to work on
    :param Length_HEX_available: HEX length available
    :type Length_HEX_available: float
    :param gv: globalvar.py

    Save the results to `SWP.csv`
    """

    # local variables
    mcpwaste = []
    twaste = []
    mXt = []
    counter = 0
    names = pd.read_csv(locator.get_total_demand()).Name
    sewage_water_ratio = config.sewage.sewage_water_ratio
    heat_exchanger_length = config.sewage.heat_exchanger_length

    for building_name in names:
        building = pd.read_csv(locator.get_demand_results_file(building_name))
        mcp_combi, t_to_sewage = np.vectorize(calc_Sewagetemperature)( building.Qwwf_kWh, building.Qww_kWh, building.Twwf_sup_C,
                                                     building.Twwf_re_C, building.mcptw_kWperC, building.mcpwwf_kWperC, sewage_water_ratio)
        mcpwaste.append(mcp_combi)
        twaste.append(t_to_sewage)
        mXt.append(mcp_combi*t_to_sewage)
        counter = counter +1
    mcpwaste_zone = np.sum(mcpwaste, axis =0)
    mXt_zone = np.sum(mXt, axis =0)
    twaste_zone = [ x * (y**-1) * 0.8 if y != 0 else 0 for x,y in zip (mXt_zone, mcpwaste_zone)] # lossess in the grid of 20%

    Q_source, t_source, t_out, tin_e, tout_e  = np.vectorize(calc_sewageheat)(mcpwaste_zone, twaste_zone, HEX_WIDTH_M,
                                                                              VEL_FLOW_MPERS, HEAT_CAPACITY_OF_WATER_JPERKGK, H0_KWPERM2K, MIN_FLOW_MPERS,
                                                                              heat_exchanger_length, T_MIN, AT_MIN_K)
    SW_gen = locator.get_sewage_heat_potential()
    pd.DataFrame( { "Qsw_kW" : Q_source, "ts_C" : t_source, "tout_sw_C" : t_out, "tin_sw_C" : twaste_zone,
                    "tout_HP_C" : tout_e, "tin_HP_C" : tin_e}).to_csv( SW_gen, index=False, float_format='%.3f')

# Calc Sewage heat

def calc_Sewagetemperature(Qwwf, Qww, tsww, trww, mcptw, mcpww, SW_ratio):
    """
    Calculate sewage temperature and flow rate released from DHW usages and Fresh Water (FW) in buildings.

    :param Qwwf: final DHW heat requirement
    :type Qwwf: float
    :param Qww: DHW heat requirement
    :type Qww: float
    :param tsww: DHW supply temperature
    :type tsww: float
    :param trww: DHW return temperature
    :type trww: float
    :param totwater: fresh water flow rate
    :type totwater: float
    :param mcpww: DHW heat capacity
    :type mcpww: float
    :param SW_ratio: ratio of decrease/increase in sewage water due to solids and also water intakes.
    :type SW_ratio: float

    :returns mcp_combi: sewage water heat capacity [kW_K]
    :rtype mcp_combi: float
    :returns t_to_sewage: sewage water temperature
    :rtype t_to_sewage: float
    """

    if Qwwf > 0:
        Qloss_to_spur = Qwwf - Qww
        t_spur = tsww - Qloss_to_spur / mcpww
        m_DHW = mcpww * SW_ratio
        m_TW = mcptw * SW_ratio
        mcp_combi = m_DHW + m_TW
        t_combi = ( m_DHW * t_spur + m_TW * trww ) / mcp_combi
        t_to_sewage = 0.90 * t_combi                  # assuming 10% thermal loss throuhg piping
    else:
        t_to_sewage = trww
        mcp_combi = mcptw * SW_ratio  # in [kW_K]
    return mcp_combi, t_to_sewage # in lh or kgh and in C

def calc_sewageheat( mcp, tin, w_HEX, Vf, cp, h0, min_m, L_HEX, tmin, ATmin):
    """
    Calculates the operation of sewage heat exchanger.

    :param mcp: heat capacity of total sewage in a zone
    :type mcp: float
    :param tin: sewage inlet temperature of a zone
    :type tin: float
    :param w_HEX: width of the sewage HEX
    :type w_HEX: float
    :param Vf: sewage flow rate [m/s]
    :type Vf: float
    :param cp: water specific heat capacity
    :type cp: float
    :param h0: sewage heat transfer coefficient
    :type h0: float
    :param min_m: sewage minimum flow rate in [lps]
    :type min_m: float
    :param L_HEX: HEX length available
    :type L_HEX: float
    :param tmin: minimum temperature of extraction
    :type tmin: float
    :param ATmin: minimum area of heat exchange
    :type ATmin: float

    :returns Q_source: heat supplied by sewage
    :rtype: float
    :returns t_source: sewage heat supply temperature
    :rtype t_source: float
    :returns tb2: sewage return temperature
    :rtype tbs: float
    :returns ta1: temperature inlet of the cold stream (from the HP)
    :rtype ta1: float
    :returns ta2: temperature outlet of the cold stream (to the HP)
    :rtype ta2: float

    ..[<NAME> al., 2016] <NAME>, <NAME>, <NAME>, <NAME> (2016). City Enegy
    Analyst (CEA): Integrated framework for analysis and optimization of building energy systems in neighborhoods and
    city districts. Energy and Buildings.
    """

    mcp_min = min_m * (cp/1000) # minimum sewage heat capacity in [kW/K]
    mcp_max = Vf * w_HEX * 0.20 * 1000 * (cp/1000)   # 20 cm is the depth of the active water in contact with the HEX
    A_HEX = w_HEX * L_HEX   # area of heat exchange
    if mcp > mcp_max:
        mcp = mcp_max
    if mcp_min < mcp <= mcp_max:
        # B is the sewage, A is the heat pump
        mcpa = mcp * 1.1 # the flow in the heat pumps slightly above the flow on the sewage side
        tb1 = tin
        ta1 = tin - ( ( tin - tmin ) + ATmin / 2 )
        alpha = h0 * A_HEX * ( 1 / mcpa - 1 / mcp )<commit_msg>addressing review comments<commit_after># -*- coding: utf-8 -*-
"""
Sewage source heat exchanger
"""
from __future__ import division
import pandas as pd
import numpy as np
import scipy
from cea.constants import HEX_WIDTH_M,VEL_FLOW_MPERS, HEAT_CAPACITY_OF_WATER_JPERKGK, H0_KWPERM2K, MIN_FLOW_MPERS, T_MIN, AT_MIN_K
import cea.config
import cea.globalvar
import cea.inputlocator

__author__ = "<NAME>"
__copyright__ = "Copyright 2015, Architecture and Building Systems - ETH Zurich"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


def calc_sewage_heat_exchanger(locator, config):
    """
    Calaculate the heat extracted from the sewage HEX.

    :param locator: an InputLocator instance set to the scenario to work on
    :param Length_HEX_available: HEX length available
    :type Length_HEX_available: float
    :param gv: globalvar.py

    Save the results to `SWP.csv`
    """

    # local variables
    mcpwaste = []
    twaste = []
    mXt = []
    counter = 0
    names = pd.read_csv(locator.get_total_demand()).Name
    sewage_water_ratio = config.sewage.sewage_water_ratio
    heat_exchanger_length = config.sewage.heat_exchanger_length

    for building_name in names:
        building = pd.read_csv(locator.get_demand_results_file(building_name))
        mcp_combi, t_to_sewage = np.vectorize(calc_Sewagetemperature)( building.Qwwf_kWh, building.Qww_kWh, building.Twwf_sup_C,
                                                     building.Twwf_re_C, building.mcptw_kWperC, building.mcpwwf_kWperC, sewage_water_ratio)
        mcpwaste.append(mcp_combi)
        twaste.append(t_to_sewage)
        mXt.append(mcp_combi*t_to_sewage)
        counter = counter +1
    mcpwaste_zone = np.sum(mcpwaste, axis =0)
    mXt_zone = np.sum(mXt, axis =0)
    twaste_zone = [ x * (y**-1) * 0.8 if y != 0 else 0 for x,y in zip (mXt_zone, mcpwaste_zone)] # lossess in the grid of 20%

    Q_source, t_source, t_out, tin_e, tout_e  = np.vectorize(calc_sewageheat)(mcpwaste_zone, twaste_zone, HEX_WIDTH_M,
                                                                              VEL_FLOW_MPERS, H0_KWPERM2K, MIN_FLOW_MPERS,
                                                                              heat_exchanger_length, T_MIN, AT_MIN_K)
    SW_gen = locator.get_sewage_heat_potential()
    pd.DataFrame( { "Qsw_kW" : Q_source, "ts_C" : t_source, "tout_sw_C" : t_out, "tin_sw_C" : twaste_zone,
                    "tout_HP_C" : tout_e, "tin_HP_C" : tin_e}).to_csv( SW_gen, index=False, float_format='%.3f')

# Calc Sewage heat

def calc_Sewagetemperature(Qwwf, Qww, tsww, trww, mcptw, mcpww, SW_ratio):
    """
    Calculate sewage temperature and flow rate released from DHW usages and Fresh Water (FW) in buildings.

    :param Qwwf: final DHW heat requirement
    :type Qwwf: float
    :param Qww: DHW heat requirement
    :type Qww: float
    :param tsww: DHW supply temperature
    :type tsww: float
    :param trww: DHW return temperature
    :type trww: float
    :param totwater: fresh water flow rate
    :type totwater: float
    :param mcpww: DHW heat capacity
    :type mcpww: float
    :param SW_ratio: ratio of decrease/increase in sewage water due to solids and also water intakes.
    :type SW_ratio: float

    :returns mcp_combi: sewage water heat capacity [kW_K]
    :rtype mcp_combi: float
    :returns t_to_sewage: sewage water temperature
    :rtype t_to_sewage: float
    """

    if Qwwf > 0:
        Qloss_to_spur = Qwwf - Qww
        t_spur = tsww - Qloss_to_spur / mcpww
        m_DHW = mcpww * SW_ratio
        m_TW = mcptw * SW_ratio
        mcp_combi = m_DHW + m_TW
        t_combi = ( m_DHW * t_spur + m_TW * trww ) / mcp_combi
        t_to_sewage = 0.90 * t_combi                  # assuming 10% thermal loss throuhg piping
    else:
        t_to_sewage = trww
        mcp_combi = mcptw * SW_ratio  # in [kW_K]
    return mcp_combi, t_to_sewage # in lh or kgh and in C

def calc_sewageheat( mcp, tin, w_HEX, Vf, h0, min_m, L_HEX, tmin, ATmin):
    """
    Calculates the operation of sewage heat exchanger.

    :param mcp: heat capacity of total sewage in a zone
    :type mcp: float
    :param tin: sewage inlet temperature of a zone
    :type tin: float
    :param w_HEX: width of the sewage HEX
    :type w_HEX: float
    :param Vf: sewage flow rate [m/s]
    :type Vf: float
    :param cp: water specific heat capacity
    :type cp: float
    :param h0: sewage heat transfer coefficient
    :type h0: float
    :param min_m: sewage minimum flow rate in [lps]
    :type min_m: float
    :param L_HEX: HEX length available
    :type L_HEX: float
    :param tmin: minimum temperature of extraction
    :type tmin: float
    :param ATmin: minimum area of heat exchange
    :type ATmin: float

    :returns Q_source: heat supplied by sewage
    :rtype: float
    :returns t_source: sewage heat supply temperature
    :rtype t_source: float
    :returns tb2: sewage return temperature
    :rtype tbs: float
    :returns ta1: temperature inlet of the cold stream (from the HP)
    :rtype ta1: float
    :returns ta2: temperature outlet of the cold stream (to the HP)
    :rtype ta2: float

    ..[<NAME> al., 2016] <NAME>, <NAME>, <NAME>, <NAME> (2016). City Enegy
    Analyst (CEA): Integrated framework for analysis and optimization of building energy systems in neighborhoods and
    city districts. Energy and Buildings.
    """

    mcp_min = min_m * (HEAT_CAPACITY_OF_WATER_JPERKGK/1000) # minimum sewage heat capacity in [kW/K]
    mcp_max = Vf * w_HEX * 0.20 * 1000 * (HEAT_CAPACITY_OF_WATER_JPERKGK/1000)   # 20 cm is the depth of the active water in contact with the HEX
    A_HEX = w_HEX * L_HEX   # area of heat exchange
    if mcp > mcp_max:
        mcp = mcp_max
    if mcp_min < mcp <= mcp_max:
        # B is the sewage, A is the heat pump
        mcpa = mcp * 1.1 # the flow in the heat pumps slightly above the flow on the sewage side
        tb1 = tin
        ta1 = tin - ( ( tin - tmin ) + ATmin / 2 )
        alpha = h0 * A_HEX * ( 1 / mcpa - 1 / mcp )
{'fraction_non_alphanumeric': 0.08305238056080914, 'fraction_numerical': 0.011512211166844831, 'mean_word_length': 2.750231267345051, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '119692', 'n_tokens_mistral': 4529, 'n_tokens_neox': 4278, 'n_words': 1294}