<commit_before>"""
Electricity imports and exports script

This file takes in the values of the electricity activation pattern (which is only considering buildings present in
network and corresponding district energy systems) and adds in the electricity requirement of decentralized buildings
and recalculates the imports from grid and exports to the grid
"""
from __future__ import division
from __future__ import print_function

import os

import numpy as np
import pandas as pd

import cea.technologies.solar.photovoltaic as pv
from cea.constants import HOURS_IN_YEAR
from cea.optimization.master.emissions_model import calc_emissions_Whyr_to_tonCO2yr, calc_pen_Whyr_to_MJoilyr

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


def electricity_calculations_of_all_buildings(locator, master_to_slave_vars,
                                              district_heating_generation_dispatch,
                                              districy_heating_electricity_requirements_dispatch,
                                              district_cooling_generation_dispatch,
                                              districy_cooling_electricity_requirements_dispatch
                                              ):
    # local variables
    building_names = master_to_slave_vars.building_names_electricity

    # GET ENERGY GENERATION OF THE ELECTRICAL GRID
    district_microgrid_generation_dispatch = calc_district_system_electricity_generated(locator,
                                                                                        master_to_slave_vars)

    # GET ENERGY REQUIREMENTS
    district_electricity_demands, \
    E_sys_req_W = calc_district_system_electricity_requirements(master_to_slave_vars,
                                                                building_names,
                                                                locator,
                                                                districy_heating_electricity_requirements_dispatch,
                                                                districy_cooling_electricity_requirements_dispatch
                                                                )
    # GET ACTIVATION CURVE
    # INITIALIZE VARIABLES:
    if master_to_slave_vars.DHN_exists:
        E_CHP_gen_W = district_heating_generation_dispatch['E_CHP_gen_W']
        E_PVT_gen_W = district_heating_generation_dispatch['E_PVT_gen_W']
        E_Furnace_dry_gen_W = district_heating_generation_dispatch['E_Furnace_dry_gen_W']
        E_Furnace_wet_gen_W = district_heating_generation_dispatch['E_Furnace_wet_gen_W']
    else:
        E_CHP_gen_W = E_PVT_gen_W = E_Furnace_dry_gen_W = E_Furnace_wet_gen_W = np.zeros(HOURS_IN_YEAR)

    if master_to_slave_vars.DCN_exists:
        E_Trigen_NG_gen_W = district_cooling_generation_dispatch['E_Trigen_NG_gen_W']
    else:
        E_Trigen_NG_gen_W = np.zeros(HOURS_IN_YEAR)

    E_PV_gen_W = district_microgrid_generation_dispatch['E_PV_gen_W']

    E_CHP_gen_directload_W, \
    E_CHP_gen_export_W, \
    E_Trigen_gen_directload_W, \
    E_Trigen_gen_export_W, \
    E_Furnace_dry_gen_directload_W, \
    E_Furnace_dry_gen_export_W, \
    E_Furnace_wet_gen_directload_W, \
    E_Furnace_wet_gen_export_W, \
    E_PV_gen_directload_W, \
    E_PV_gen_export_W, \
    E_PVT_gen_directload_W, \
    E_PVT_gen_export_W, \
    E_GRID_directload_W = np.vectorize(electricity_activation_curve)(E_CHP_gen_W,
                                                                     E_PVT_gen_W,
                                                                     E_Furnace_dry_gen_W,
                                                                     E_Furnace_wet_gen_W,
                                                                     E_Trigen_NG_gen_W,
                                                                     E_PV_gen_W,
                                                                     E_sys_req_W)

    district_electricity_dispatch = {'E_CHP_gen_directload_W': E_CHP_gen_directload_W,
                                     'E_CHP_gen_export_W': E_CHP_gen_export_W,
                                     'E_Trigen_gen_directload_W': E_Trigen_gen_directload_W,
                                     'E_Trigen_gen_export_W': E_Trigen_gen_export_W,
                                     'E_Furnace_dry_gen_directload_W': E_Furnace_dry_gen_directload_W,
                                     'E_Furnace_dry_gen_export_W': E_Furnace_dry_gen_export_W,
                                     'E_Furnace_wet_gen_directload_W': E_Furnace_wet_gen_directload_W,
                                     'E_Furnace_wet_gen_export_W': E_Furnace_wet_gen_export_W,
                                     'E_PV_gen_directload_W': E_PV_gen_directload_W,
                                     'E_PV_gen_export_W': E_PV_gen_export_W,
                                     'E_PVT_gen_directload_W': E_PVT_gen_directload_W,
                                     'E_PVT_gen_export_W': E_PVT_gen_export_W,
                                     'E_GRID_directload_W': E_GRID_directload_W
                                     }

    # CALC COSTS and Capacities
    district_microgrid_costs, \
    district_electricity_capacity_installed = calc_electricity_performance_costs(locator,
                                                                                 E_GRID_directload_W,
                                                                                 master_to_slave_vars)

    return district_microgrid_costs, \
           district_electricity_dispatch, \
           district_electricity_demands, \
           district_electricity_capacity_installed


def calc_electricity_performance_emissions(lca, E_PV_gen_export_W, E_GRID_directload_W):
    # SOlar technologies
    GHG_PV_gen_export_tonCO2 = calc_emissions_Whyr_to_tonCO2yr(sum(E_PV_gen_export_W), lca.EL_TO_CO2)
    GHG_PV_gen_directload_tonCO2 = 0.0  # because the price of fuel is already included

    PEN_PV_gen_export_MJoil = calc_pen_Whyr_to_MJoilyr(sum(E_PV_gen_export_W), lca.EL_TO_OIL_EQ)
    PEN_PV_gen_directload_MJoil = 0.0  # because the price of fuel is already included

    GHG_PV_connected_tonCO2 = GHG_PV_gen_directload_tonCO2 - GHG_PV_gen_export_tonCO2
    PEN_PV_connected_MJoil = PEN_PV_gen_directload_MJoil - PEN_PV_gen_export_MJoil

    # GRid
    GHG_GRID_directload_tonCO2 = calc_emissions_Whyr_to_tonCO2yr(sum(E_GRID_directload_W), lca.EL_TO_CO2)
    PEN_GRID_directload_MJoil = calc_pen_Whyr_to_MJoilyr(sum(E_GRID_directload_W), lca.EL_TO_OIL_EQ)

    # calculate emissions of generation units BUT solar (the last will be calculated in the next STEP)
    # PEN_HPSolarandHeatRecovery_MJoil = E_aux_solar_and_heat_recovery_W * lca.EL_TO_OIL_EQ * WH_TO_J / 1.0E6
    # GHG_HPSolarandHeatRecovery_tonCO2 = E_aux_solar_and_heat_recovery_W * lca.EL_TO_CO2 * WH_TO_J / 1E6

    performance_electricity = {
        # emissions
        "GHG_PV_connected_tonCO2": GHG_PV_connected_tonCO2,
        "GHG_GRID_connected_tonCO2": GHG_GRID_directload_tonCO2,

        # primary energy
        "PEN_PV_connected_MJoil": PEN_PV_connected_MJoil,
        "PEN_GRID_connected_MJoil": PEN_GRID_directload_MJoil
    }
<commit_msg>Update electricity_main.py<commit_after>"""
Electricity imports and exports script

This file takes in the values of the electricity activation pattern (which is only considering buildings present in
network and corresponding district energy systems) and adds in the electricity requirement of decentralized buildings
and recalculates the imports from grid and exports to the grid
"""
from __future__ import division
from __future__ import print_function

import os

import numpy as np
import pandas as pd

import cea.technologies.solar.photovoltaic as pv
from cea.constants import HOURS_IN_YEAR
from cea.optimization.master.emissions_model import calc_emissions_Whyr_to_tonCO2yr

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


def electricity_calculations_of_all_buildings(locator, master_to_slave_vars,
                                              district_heating_generation_dispatch,
                                              districy_heating_electricity_requirements_dispatch,
                                              district_cooling_generation_dispatch,
                                              districy_cooling_electricity_requirements_dispatch
                                              ):
    # local variables
    building_names = master_to_slave_vars.building_names_electricity

    # GET ENERGY GENERATION OF THE ELECTRICAL GRID
    district_microgrid_generation_dispatch = calc_district_system_electricity_generated(locator,
                                                                                        master_to_slave_vars)

    # GET ENERGY REQUIREMENTS
    district_electricity_demands, \
    E_sys_req_W = calc_district_system_electricity_requirements(master_to_slave_vars,
                                                                building_names,
                                                                locator,
                                                                districy_heating_electricity_requirements_dispatch,
                                                                districy_cooling_electricity_requirements_dispatch
                                                                )
    # GET ACTIVATION CURVE
    # INITIALIZE VARIABLES:
    if master_to_slave_vars.DHN_exists:
        E_CHP_gen_W = district_heating_generation_dispatch['E_CHP_gen_W']
        E_PVT_gen_W = district_heating_generation_dispatch['E_PVT_gen_W']
        E_Furnace_dry_gen_W = district_heating_generation_dispatch['E_Furnace_dry_gen_W']
        E_Furnace_wet_gen_W = district_heating_generation_dispatch['E_Furnace_wet_gen_W']
    else:
        E_CHP_gen_W = E_PVT_gen_W = E_Furnace_dry_gen_W = E_Furnace_wet_gen_W = np.zeros(HOURS_IN_YEAR)

    if master_to_slave_vars.DCN_exists:
        E_Trigen_NG_gen_W = district_cooling_generation_dispatch['E_Trigen_NG_gen_W']
    else:
        E_Trigen_NG_gen_W = np.zeros(HOURS_IN_YEAR)

    E_PV_gen_W = district_microgrid_generation_dispatch['E_PV_gen_W']

    E_CHP_gen_directload_W, \
    E_CHP_gen_export_W, \
    E_Trigen_gen_directload_W, \
    E_Trigen_gen_export_W, \
    E_Furnace_dry_gen_directload_W, \
    E_Furnace_dry_gen_export_W, \
    E_Furnace_wet_gen_directload_W, \
    E_Furnace_wet_gen_export_W, \
    E_PV_gen_directload_W, \
    E_PV_gen_export_W, \
    E_PVT_gen_directload_W, \
    E_PVT_gen_export_W, \
    E_GRID_directload_W = np.vectorize(electricity_activation_curve)(E_CHP_gen_W,
                                                                     E_PVT_gen_W,
                                                                     E_Furnace_dry_gen_W,
                                                                     E_Furnace_wet_gen_W,
                                                                     E_Trigen_NG_gen_W,
                                                                     E_PV_gen_W,
                                                                     E_sys_req_W)

    district_electricity_dispatch = {'E_CHP_gen_directload_W': E_CHP_gen_directload_W,
                                     'E_CHP_gen_export_W': E_CHP_gen_export_W,
                                     'E_Trigen_gen_directload_W': E_Trigen_gen_directload_W,
                                     'E_Trigen_gen_export_W': E_Trigen_gen_export_W,
                                     'E_Furnace_dry_gen_directload_W': E_Furnace_dry_gen_directload_W,
                                     'E_Furnace_dry_gen_export_W': E_Furnace_dry_gen_export_W,
                                     'E_Furnace_wet_gen_directload_W': E_Furnace_wet_gen_directload_W,
                                     'E_Furnace_wet_gen_export_W': E_Furnace_wet_gen_export_W,
                                     'E_PV_gen_directload_W': E_PV_gen_directload_W,
                                     'E_PV_gen_export_W': E_PV_gen_export_W,
                                     'E_PVT_gen_directload_W': E_PVT_gen_directload_W,
                                     'E_PVT_gen_export_W': E_PVT_gen_export_W,
                                     'E_GRID_directload_W': E_GRID_directload_W
                                     }

    # CALC COSTS and Capacities
    district_microgrid_costs, \
    district_electricity_capacity_installed = calc_electricity_performance_costs(locator,
                                                                                 E_GRID_directload_W,
                                                                                 master_to_slave_vars)

    return district_microgrid_costs, \
           district_electricity_dispatch, \
           district_electricity_demands, \
           district_electricity_capacity_installed


def calc_electricity_performance_emissions(lca, E_PV_gen_export_W, E_GRID_directload_W):
    # SOlar technologies
    GHG_PV_gen_export_tonCO2 = calc_emissions_Whyr_to_tonCO2yr(sum(E_PV_gen_export_W), lca.EL_TO_CO2)
    GHG_PV_gen_directload_tonCO2 = 0.0  # because the price of fuel is already included
    GHG_PV_connected_tonCO2 = GHG_PV_gen_directload_tonCO2 - GHG_PV_gen_export_tonCO2

    # GRid
    GHG_GRID_directload_tonCO2 = calc_emissions_Whyr_to_tonCO2yr(sum(E_GRID_directload_W), lca.EL_TO_CO2)

    # calculate emissions of generation units BUT solar (the last will be calculated in the next STEP)
    # PEN_HPSolarandHeatRecovery_MJoil = E_aux_solar_and_heat_recovery_W * lca.EL_TO_OIL_EQ * WH_TO_J / 1.0E6
    # GHG_HPSolarandHeatRecovery_tonCO2 = E_aux_solar_and_heat_recovery_W * lca.EL_TO_CO2 * WH_TO_J / 1E6

    performance_electricity = {
        # emissions
        "GHG_PV_connected_tonCO2": GHG_PV_connected_tonCO2,
        "GHG_GRID_connected_tonCO2": GHG_GRID_directload_tonCO2,

    }

{'fraction_non_alphanumeric': 0.04252487039372285, 'fraction_numerical': 0.004343561720610901, 'mean_word_length': 1.9210149375895231, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 122, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5819653', 'n_tokens_mistral': 4602, 'n_tokens_neox': 4292, 'n_words': 593}