def thermal_networks_in_individual(locator, weather_features, DCN_barcode, DHN_barcode, district_heating_network, district_cooling_network, column_names_buildings_heating, column_names_buildings_cooling): # local variables ground_temp = weather_features.ground_temp # EVALUATE CASES TO CREATE A NETWORK OR NOT if district_heating_network: # network exists if not os.path.exists( locator.get_optimization_network_results_summary( 'DH', DHN_barcode)): total_demand = createTotalNtwCsv(DHN_barcode, locator, column_names_buildings_heating) num_total_buildings = len(column_names_buildings_heating) buildings_in_heating_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_heating(locator, total_demand, buildings_in_heating_network, DHN_barcode=DHN_barcode) DH_network_summary_individual = summarize_network.network_main( locator, buildings_in_heating_network, ground_temp, num_total_buildings, "DH", DHN_barcode) else: DH_network_summary_individual = pd.read_csv( locator.get_optimization_network_results_summary( 'DH', DHN_barcode)) else: DH_network_summary_individual = None if district_cooling_network: # network exists if not os.path.exists( locator.get_optimization_network_results_summary( 'DC', DCN_barcode)): total_demand = createTotalNtwCsv(DCN_barcode, locator, column_names_buildings_cooling) num_total_buildings = len(column_names_buildings_cooling) buildings_in_cooling_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_cooling(locator, total_demand, buildings_in_cooling_network, DCN_barcode=DCN_barcode) DC_network_summary_individual = summarize_network.network_main( locator, buildings_in_cooling_network, ground_temp, num_total_buildings, 'DC', DCN_barcode) else: DC_network_summary_individual = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode)) else: DC_network_summary_individual = None return DH_network_summary_individual, DC_network_summary_individual
def preproccessing(locator, total_demand, buildings_heating_demand, buildings_cooling_demand, weather_file, district_heating_network, district_cooling_network): """ This function aims at preprocessing all data for the optimization. :param locator: path to locator function :param total_demand: dataframe with total demand and names of all building in the area :param building_names: dataframe with names of all buildings in the area :param weather_file: path to wather file :type locator: class :type total_demand: list :type building_names: list :type weather_file: string :return: - extraCosts: extra pareto optimal costs due to electricity and process heat ( these are treated separately and not considered inside the optimization) - extraCO2: extra pareto optimal emissions due to electricity and process heat ( these are treated separately and not considered inside the optimization) - extraPrim: extra pareto optimal primary energy due to electricity and process heat ( these are treated separately and not considered inside the optimization) - solar_features: extraction of solar features form the results of the solar technologies calculation. :rtype: float, float, float, float """ # local variables network_depth_m = Z0 print("PRE-PROCESSING 1/2: weather properties") T_ambient = epwreader.epw_reader(weather_file)['drybulb_C'] ground_temp = calc_ground_temperature(locator, T_ambient, depth_m=network_depth_m) print("PRE-PROCESSING 2/2: thermal networks") # at first estimate a distribution with all the buildings connected if district_heating_network: num_tot_buildings = len(buildings_heating_demand) DHN_barcode = ''.join(str(1) for e in range(num_tot_buildings)) substation.substation_main_heating(locator, total_demand, buildings_heating_demand, DHN_barcode=DHN_barcode) summarize_network.network_main(locator, buildings_heating_demand, ground_temp, num_tot_buildings, "DH", DHN_barcode) # "_all" key for all buildings if district_cooling_network: num_tot_buildings = len(buildings_cooling_demand) DCN_barcode = ''.join(str(1) for e in range(num_tot_buildings)) substation.substation_main_cooling(locator, total_demand, buildings_cooling_demand, DCN_barcode=DCN_barcode) summarize_network.network_main(locator, buildings_cooling_demand, ground_temp, num_tot_buildings, "DC", DCN_barcode) # "_all" key for all buildings network_features = NetworkOptimizationFeatures(district_heating_network, district_cooling_network, locator) return network_features
def checkNtw(individual, DHN_network_list, DCN_network_list, locator, gv, config, building_names): """ This function calls the distribution routine if necessary :param individual: network configuration considered :param ntwList: list of DHN configurations previously encounterd in the master :param locator: path to the folder :type individual: list :type ntwList: list :type locator: string :return: None :rtype: Nonetype """ DHN_barcode, DCN_barcode, DHN_configuration, DCN_configuration = supportFn.individual_to_barcode( individual, building_names) if not (DHN_barcode in DHN_network_list) and DHN_barcode.count("1") > 0: DHN_network_list.append(DHN_barcode) total_demand = supportFn.createTotalNtwCsv(DHN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DHN_barcode) if not (DCN_barcode in DCN_network_list) and DCN_barcode.count("1") > 0: DCN_network_list.append(DCN_barcode) total_demand = supportFn.createTotalNtwCsv(DCN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DCN_barcode)
def checkNtw(individual, ntwList, locator, gv): """ This function calls the distribution routine if necessary :param individual: network configuration considered :param ntwList: list of DHN configurations previously encounterd in the master :param locator: path to the folder :type individual: list :type ntwList: list :type locator: string :return: None :rtype: Nonetype """ indCombi = sFn.individual_to_barcode(individual, gv) print indCombi, 2 if not (indCombi in ntwList) and indCombi.count("1") > 0: ntwList.append(indCombi) if indCombi.count("1") == 1: total_demand = pd.read_csv( os.path.join(locator.get_optimization_network_results_folder(), "Total_%(indCombi)s.csv" % locals())) building_names = total_demand.Name.values print "Direct launch of distribution summary routine for", indCombi nM.network_main(locator, total_demand, building_names, gv, indCombi) else: total_demand = sFn.createTotalNtwCsv(indCombi, locator) building_names = total_demand.Name.values # Run the substation and distribution routines print "Re-run the substation routine for new distribution configuration", indCombi sMain.substation_main(locator, total_demand, building_names, gv, indCombi) print "Launch distribution summary routine" nM.network_main(locator, total_demand, building_names, gv, indCombi)
def preproccessing(locator, total_demand, buildings_heating_demand, buildings_cooling_demand, weather_file, district_heating_network, district_cooling_network): """ This function aims at preprocessing all data for the optimization. :param locator: path to locator function :param total_demand: dataframe with total demand and names of all building in the area :param building_names: dataframe with names of all buildings in the area :param weather_file: path to wather file :type locator: class :type total_demand: list :type building_names: list :type weather_file: string :return: - extraCosts: extra pareto optimal costs due to electricity and process heat ( these are treated separately and not considered inside the optimization) - extraCO2: extra pareto optimal emissions due to electricity and process heat ( these are treated separately and not considered inside the optimization) - extraPrim: extra pareto optimal primary energy due to electricity and process heat ( these are treated separately and not considered inside the optimization) - solar_features: extraction of solar features form the results of the solar technologies calculation. :rtype: float, float, float, float """ print("PRE-PROCESSING 0/4: initialize directory") shutil.rmtree(locator.get_optimization_master_results_folder()) shutil.rmtree(locator.get_optimization_network_results_folder()) shutil.rmtree(locator.get_optimization_slave_results_folder()) shutil.rmtree(locator.get_optimization_substations_folder()) print("PRE-PROCESSING 1/4: weather features") # at first estimate a distribution with all the buildings connected weather_features = WeatherFeatures(weather_file, locator) print("PRE-PROCESSING 2/4: conversion systems database") # at first estimate a distribution with all the buildings connected supply_systems = SupplySystemsDatabase(locator) print("PRE-PROCESSING 3/4: feedstocks systems database") # at first estimate a distribution with all the buildings connected prices = Prices(supply_systems) lca = LcaCalculations(supply_systems) print("PRE-PROCESSING 4/4: network features") # at first estimate a distribution with all the buildings connected if district_heating_network: num_tot_buildings = len(buildings_heating_demand) DHN_barcode = ''.join(str(1) for e in range(num_tot_buildings)) substation.substation_main_heating(locator, total_demand, buildings_heating_demand, DHN_barcode=DHN_barcode) summarize_network.network_main(locator, buildings_heating_demand, weather_features.ground_temp, num_tot_buildings, "DH", DHN_barcode) # "_all" key for all buildings if district_cooling_network: num_tot_buildings = len(buildings_cooling_demand) DCN_barcode = ''.join(str(1) for e in range(num_tot_buildings)) substation.substation_main_cooling(locator, total_demand, buildings_cooling_demand, DCN_barcode=DCN_barcode) summarize_network.network_main(locator, buildings_cooling_demand, weather_features.ground_temp, num_tot_buildings, "DC", DCN_barcode) # "_all" key for all buildings network_features = NetworkOptimizationFeatures(district_heating_network, district_cooling_network, locator) return weather_features, network_features, prices, lca
def extract_loads_individual(locator, DCN_barcode, DHN_barcode, district_heating_network, district_cooling_network, column_names_buildings_heating, column_names_buildings_cooling): # local variables weather_file = locator.get_weather_file() network_depth_m = Z0 T_ambient = epwreader.epw_reader(weather_file)['drybulb_C'] ground_temp = calc_ground_temperature(locator, T_ambient, depth_m=network_depth_m) # EVALUATE CASES TO CREATE A NETWORK OR NOT if district_heating_network: # network exists network_file_name_heating = "DH_Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary( 'DH', DHN_barcode)): total_demand = createTotalNtwCsv(DHN_barcode, locator, column_names_buildings_heating) num_total_buildings = len(column_names_buildings_heating) buildings_in_heating_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_heating(locator, total_demand, buildings_in_heating_network, DHN_barcode=DHN_barcode) results = summarize_network.network_main( locator, buildings_in_heating_network, ground_temp, num_total_buildings, "DH", DHN_barcode) else: results = pd.read_csv( locator.get_optimization_network_results_summary( 'DH', DHN_barcode)) Q_DHNf_W = results['Q_DHNf_W'].values Q_heating_max_W = Q_DHNf_W.max() Qcdata_netw_total_kWh = results['Qcdata_netw_total_kWh'].values Q_wasteheat_datacentre_max_W = Qcdata_netw_total_kWh.max() else: Q_heating_max_W = 0.0 Q_wasteheat_datacentre_max_W = 0.0 network_file_name_heating = "" if district_cooling_network: # network exists network_file_name_cooling = "DC_Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary( 'DC', DCN_barcode)): total_demand = createTotalNtwCsv(DCN_barcode, locator, column_names_buildings_cooling) num_total_buildings = len(column_names_buildings_cooling) buildings_in_cooling_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_cooling(locator, total_demand, buildings_in_cooling_network, DCN_barcode=DCN_barcode) results = summarize_network.network_main( locator, buildings_in_cooling_network, ground_temp, num_total_buildings, 'DC', DCN_barcode) else: results = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode)) # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = results[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W"].values Q_cooling_max_W = Q_DCNf_W.max() else: Q_cooling_max_W = 0.0 network_file_name_cooling = "" Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) return Q_cooling_nom_W, Q_heating_nom_W, Q_wasteheat_datacentre_max_W, network_file_name_cooling, network_file_name_heating
def evaluation_main(individual, building_names, locator, solar_features, network_features, gv, config, prices, lca, ind_num, gen): """ This function evaluates an individual :param individual: list with values of the individual :param building_names: list with names of buildings :param locator: locator class :param solar_features: solar features call to class :param network_features: network features call to class :param gv: global variables class :param optimization_constants: class containing constants used in optimization :param config: configuration file :param prices: class of prices used in optimization :type individual: list :type building_names: list :type locator: string :type solar_features: class :type network_features: class :type gv: class :type optimization_constants: class :type config: class :type prices: class :return: Resulting values of the objective function. costs, CO2, prim :rtype: tuple """ # Check the consistency of the individual or create a new one individual = check_invalid(individual, len(building_names), config) # Initialize objective functions costs, CO2 and primary energy costs_USD = 0 GHG_tonCO2 = 0 PEN_MJoil = 0 Q_heating_uncovered_design_W = 0 Q_heating_uncovered_annual_W = 0 # Create the string representation of the individual DHN_barcode, DCN_barcode, DHN_configuration, DCN_configuration = supportFn.individual_to_barcode( individual, building_names) if DHN_barcode.count("1") == gv.num_tot_buildings: network_file_name_heating = "Network_summary_result_all.csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() elif DHN_barcode.count("1") == 0: network_file_name_heating = "Network_summary_result_all.csv" Q_heating_max_W = 0 else: network_file_name_heating = "Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary(DHN_barcode)): total_demand = supportFn.createTotalNtwCsv(DHN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DHN_barcode) Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() if DCN_barcode.count("1") == gv.num_tot_buildings: network_file_name_cooling = "Network_summary_result_all.csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() elif DCN_barcode.count("1") == 0: network_file_name_cooling = "Network_summary_result_all.csv" Q_cooling_max_W = 0 else: network_file_name_cooling = "Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary(DCN_barcode)): total_demand = supportFn.createTotalNtwCsv(DCN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DCN_barcode) if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) # Modify the individual with the extra GHP constraint try: check.GHPCheck(individual, locator, Q_heating_nom_W, gv) except: print "No GHP constraint check possible \n" # Export to context master_to_slave_vars = calc_master_to_slave_variables( individual, Q_heating_max_W, Q_cooling_max_W, building_names, ind_num, gen) master_to_slave_vars.network_data_file_heating = network_file_name_heating master_to_slave_vars.network_data_file_cooling = network_file_name_cooling master_to_slave_vars.total_buildings = len(building_names) master_to_slave_vars.DHN_barcode = DHN_barcode master_to_slave_vars.DCN_barcode = DCN_barcode if master_to_slave_vars.number_of_buildings_connected_heating > 1: if DHN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DHN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DHN_barcode) if master_to_slave_vars.number_of_buildings_connected_cooling > 1: if DCN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DCN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DCN_barcode) # Thermal Storage Calculations; Run storage optimization costs_storage_USD, GHG_storage_tonCO2, PEN_storage_MJoil = storage_main.storage_optimization( locator, master_to_slave_vars, lca, prices, config) costs_USD += costs_storage_USD GHG_tonCO2 += GHG_storage_tonCO2 PEN_MJoil += PEN_storage_MJoil # District Heating Calculations if config.district_heating_network: if DHN_barcode.count("1") > 0: (PEN_heating_MJoil, GHG_heating_tonCO2, costs_heating_USD, Q_heating_uncovered_design_W, Q_heating_uncovered_annual_W ) = heating_main.heating_calculations_of_DH_buildings( locator, master_to_slave_vars, gv, config, prices, lca) else: GHG_heating_tonCO2 = 0 costs_heating_USD = 0 PEN_heating_MJoil = 0 else: GHG_heating_tonCO2 = 0 costs_heating_USD = 0 PEN_heating_MJoil = 0 costs_USD += costs_heating_USD GHG_tonCO2 += GHG_heating_tonCO2 PEN_MJoil += PEN_heating_MJoil # District Cooling Calculations if gv.ZernezFlag == 1: costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil = 0, 0, 0 elif config.district_cooling_network: reduced_timesteps_flag = False (costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil ) = cooling_main.cooling_calculations_of_DC_buildings( locator, master_to_slave_vars, network_features, gv, prices, lca, config, reduced_timesteps_flag) else: costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil = 0, 0, 0 costs_USD += costs_cooling_USD GHG_tonCO2 += GHG_cooling_tonCO2 PEN_MJoil += PEN_cooling_MJoil # District Electricity Calculations (costs_electricity_USD, GHG_electricity_tonCO2, PEN_electricity_MJoil ) = electricity_main.electricity_calculations_of_all_buildings( DHN_barcode, DCN_barcode, locator, master_to_slave_vars, network_features, gv, prices, lca, config) costs_USD += costs_electricity_USD GHG_tonCO2 += GHG_electricity_tonCO2 PEN_MJoil += PEN_electricity_MJoil # Natural Gas Import Calculations. Prices, GHG and PEN are already included in the various sections. # This is to save the files for further processing and plots natural_gas_main.natural_gas_imports(master_to_slave_vars, locator, config) # Capex Calculations print "Add extra costs" (costs_additional_USD, GHG_additional_tonCO2, PEN_additional_MJoil) = cost_model.addCosts( building_names, locator, master_to_slave_vars, Q_heating_uncovered_design_W, Q_heating_uncovered_annual_W, solar_features, network_features, gv, config, prices, lca) costs_USD += costs_additional_USD GHG_tonCO2 += GHG_additional_tonCO2 PEN_MJoil += PEN_additional_MJoil summarize_individual.summarize_individual_main(master_to_slave_vars, building_names, individual, solar_features, locator, config) # Converting costs into float64 to avoid longer values costs_USD = np.float64(costs_USD) GHG_tonCO2 = np.float64(GHG_tonCO2) PEN_MJoil = np.float64(PEN_MJoil) print('Total costs = ' + str(costs_USD)) print('Total CO2 = ' + str(GHG_tonCO2)) print('Total prim = ' + str(PEN_MJoil)) # Saving capacity details of the individual return costs_USD, GHG_tonCO2, PEN_MJoil, master_to_slave_vars, individual
def preproccessing(locator, total_demand, building_names, weather_file, gv): """ This function aims at preprocessing all data for the optimization. :param locator: path to locator function :param total_demand: dataframe with total demand and names of all building in the area :param building_names: dataframe with names of all buildings in the area :param weather_file: path to wather file :param gv: path to global variables class :type locator: class :type total_demand: list :type building_names: list :type weather_file: string :type gv: class :return: extraCosts: extra pareto optimal costs due to electricity and process heat ( these are treated separately and not considered inside the optimization) extraCO2: extra pareto optimal emissions due to electricity and process heat ( these are treated separately and not considered inside the optimization) extraPrim: extra pareto optimal primary energy due to electricity and process heat ( these are treated separately and not considered inside the optimization) solar_features: extraction of solar features form the results of the solar technologies calculation. :rtype: float, float, float, float """ # GET ENERGY POTENTIALS # geothermal T_ambient = epwreader.epw_reader(weather_file)['drybulb_C'] gv.ground_temperature = geothermal.calc_ground_temperature( T_ambient.values, gv) # solar print "Solar features extraction" solar_features = SolarFeatures(locator) # GET LOADS IN SUBSTATIONS # prepocess space heating, domestic hot water and space cooling to substation. print "Run substation model for each building separately" substation.substation_main( locator, total_demand, building_names, gv, Flag=True) # True if disconected buildings are calculated # GET COMPETITIVE ALTERNATIVES TO A NETWORK # estimate what would be the operation of single buildings only for heating. # For cooling all buildings are assumed to be connected to the cooling distribution on site. print "Run decentralized model for buildings" #decentralized_buildings.decentralized_main(locator, building_names, gv) # GET DH NETWORK # at first estimate a distribution with all the buildings connected at it. print "Create distribution file with all buildings connected" summarize_network.network_main(locator, total_demand, building_names, gv, "all") #"_all" key for all buildings # GET EXTRAS # estimate the extra costs, emissions and primary energy of electricity. print "electricity" elecCosts, elecCO2, elecPrim = electricity.calc_pareto_electricity( locator, gv) # estimate the extra costs, emissions and primary energy for process heat print "Process-heat" hpCosts, hpCO2, hpPrim = process_heat.calc_pareto_Qhp( locator, total_demand, gv) extraCosts = elecCosts + hpCosts extraCO2 = elecCO2 + hpCO2 extraPrim = elecPrim + hpPrim return extraCosts, extraCO2, extraPrim, solar_features
def extract_loads_individual(locator, config, individual_with_name_dict, DCN_barcode, DHN_barcode, district_heating_network, district_cooling_network, column_names_buildings_heating, column_names_buildings_cooling): # local variables weather_file = locator.get_weather_file() network_depth_m = Z0 T_ambient = epwreader.epw_reader(weather_file)['drybulb_C'] ground_temp = calc_ground_temperature(locator, T_ambient, depth_m=network_depth_m) # EVALUATE CASES TO CREATE A NETWORK OR NOT if district_heating_network: # network exists if DHN_barcode.count("1") == len(column_names_buildings_heating): network_file_name_heating = "DH_Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary( 'DH', DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() elif DHN_barcode.count("1") == 0: # no network at all network_file_name_heating = "DH_Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" Q_heating_max_W = 0 else: network_file_name_heating = "DH_Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary( 'DH', DHN_barcode)): total_demand = createTotalNtwCsv( DHN_barcode, locator, column_names_buildings_heating) num_total_buildings = len(column_names_buildings_heating) buildings_in_heating_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_heating( locator, total_demand, buildings_in_heating_network, DHN_barcode=DHN_barcode) summarize_network.network_main(locator, buildings_in_heating_network, ground_temp, num_total_buildings, "DH", DHN_barcode) Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary( 'DH', DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() else: Q_heating_max_W = 0.0 network_file_name_heating = "" if district_cooling_network: # network exists if DCN_barcode.count("1") == len(column_names_buildings_cooling): network_file_name_cooling = "DC_Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if individual_with_name_dict['HPServer'] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W_no_server_demand = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W" ]).values Q_DCNf_W_with_server_demand = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_DCNf_W = Q_DCNf_W_no_server_demand + ( (Q_DCNf_W_with_server_demand - Q_DCNf_W_no_server_demand) * (individual_with_name_dict['HPShare'])) else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() elif DCN_barcode.count("1") == 0: network_file_name_cooling = "DC_Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" Q_cooling_max_W = 0.0 else: network_file_name_cooling = "DC_Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary( 'DC', DCN_barcode)): total_demand = createTotalNtwCsv( DCN_barcode, locator, column_names_buildings_cooling) num_total_buildings = len(column_names_buildings_cooling) buildings_in_cooling_network = total_demand.Name.values # Run the substation and distribution routines substation.substation_main_cooling( locator, total_demand, buildings_in_cooling_network, DCN_barcode=DCN_barcode) summarize_network.network_main(locator, buildings_in_cooling_network, ground_temp, num_total_buildings, 'DC', DCN_barcode) Q_DCNf_W_no_server_demand = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values Q_DCNf_W_with_server_demand = pd.read_csv( locator.get_optimization_network_results_summary( 'DC', DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration if district_heating_network and individual_with_name_dict[ 'HPServer'] == 1: Q_DCNf_W = Q_DCNf_W_no_server_demand + ( (Q_DCNf_W_with_server_demand - Q_DCNf_W_no_server_demand) * (individual_with_name_dict['HPShare'])) else: Q_DCNf_W = Q_DCNf_W_with_server_demand Q_cooling_max_W = Q_DCNf_W.max() else: Q_cooling_max_W = 0.0 network_file_name_cooling = "" Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) return Q_cooling_nom_W, Q_heating_nom_W, network_file_name_cooling, network_file_name_heating
def supply_calculation(individual, building_names, total_demand, locator, extra_costs, extra_CO2, extra_primary_energy, solar_features, network_features, gv, config, prices, lca): """ This function evaluates one supply system configuration of the case study. :param individual: a list that indicates the supply system configuration :type individual: list :param building_names: names of all building in the district :type building_names: ndarray :param locator: :param extra_costs: cost of decentralized supply systems :param extra_CO2: CO2 emission of decentralized supply systems :param extra_primary_energy: Primary energy of decentralized supply systems :param solar_features: Energy production potentials of solar technologies, including area of installed panels and annual production :type solar_features: dict :param network_features: hourly network operating conditions (thermal/pressure losses) and capital costs :type network_features: dict :param gv: :param config: :param prices: :return: """ individual = evaluation.check_invalid(individual, len(building_names), config) # Initialize objective functions costs, CO2 and primary energy costs_USD = 0.0 GHG_tonCO2 = extra_CO2 PEN_MJoil = extra_primary_energy Q_uncovered_design_W = 0.0 Q_uncovered_annual_W = 0.0 # Create the string representation of the individual DHN_barcode, DCN_barcode, DHN_configuration, DCN_configuration = supportFn.individual_to_barcode( individual, building_names) # read the total loads from buildings connected to thermal networks if DHN_barcode.count("1") == gv.num_tot_buildings: network_file_name_heating = "Network_summary_result_all.csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() elif DHN_barcode.count("1") == 0: network_file_name_heating = "Network_summary_result_all.csv" Q_heating_max_W = 0.0 else: # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DHN_barcode) network_file_name_heating = "Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() if DCN_barcode.count("1") == gv.num_tot_buildings: network_file_name_cooling = "Network_summary_result_all.csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() elif DCN_barcode.count("1") == 0: network_file_name_cooling = "Network_summary_result_none.csv" Q_cooling_max_W = 0 else: # Run the substation and distribution routines substation.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) summarize_network.network_main(locator, total_demand, building_names, config, gv, DCN_barcode) network_file_name_cooling = "Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) # Modify the individual with the extra GHP constraint try: check.GHPCheck(individual, locator, Q_heating_nom_W, gv) except: print "No GHP constraint check possible \n" # Export to context individual_number = calc_individual_number(locator) master_to_slave_vars = evaluation.calc_master_to_slave_variables( individual, Q_heating_max_W, Q_cooling_max_W, building_names, individual_number, GENERATION_NUMBER) master_to_slave_vars.network_data_file_heating = network_file_name_heating master_to_slave_vars.network_data_file_cooling = network_file_name_cooling master_to_slave_vars.total_buildings = len(building_names) if master_to_slave_vars.number_of_buildings_connected_heating > 1: if DHN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DHN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DHN_barcode) if master_to_slave_vars.number_of_buildings_connected_cooling > 1: if DCN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DCN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DCN_barcode) costs_storage_USD, GHG_storage_tonCO2, PEN_storage_MJoil = storage_main.storage_optimization( locator, master_to_slave_vars, lca, prices, config) costs_USD += costs_storage_USD GHG_tonCO2 += GHG_storage_tonCO2 PEN_MJoil += PEN_storage_MJoil # slave optimization of heating networks if config.district_heating_network: if DHN_barcode.count("1") > 0: (PEN_heating_MJoil, GHG_heating_tonCO2, costs_heating_USD, Q_uncovered_design_W, Q_uncovered_annual_W ) = heating_main.heating_calculations_of_DH_buildings( locator, master_to_slave_vars, gv, config, prices, lca) else: GHG_heating_tonCO2 = 0.0 costs_heating_USD = 0.0 PEN_heating_MJoil = 0.0 else: GHG_heating_tonCO2 = 0.0 costs_heating_USD = 0.0 PEN_heating_MJoil = 0.0 costs_USD += costs_heating_USD GHG_tonCO2 += GHG_heating_tonCO2 PEN_MJoil += PEN_heating_MJoil # slave optimization of cooling networks if gv.ZernezFlag == 1: costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil = 0.0, 0.0, 0.0 elif config.district_cooling_network and DCN_barcode.count("1") > 0: reduced_timesteps_flag = config.supply_system_simulation.reduced_timesteps (costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil ) = cooling_main.cooling_calculations_of_DC_buildings( locator, master_to_slave_vars, network_features, gv, prices, lca, config, reduced_timesteps_flag) # if reduced_timesteps_flag: # # reduced timesteps simulation for a month (May) # coolCosts = coolCosts * (8760/(3624/2880)) # coolCO2 = coolCO2 * (8760/(3624/2880)) # coolPrim = coolPrim * (8760/(3624/2880)) # # FIXME: check results else: costs_cooling_USD, GHG_cooling_tonCO2, PEN_cooling_MJoil = 0.0, 0.0, 0.0 # District Electricity Calculations costs_electricity_USD, GHG_electricity_tonCO2, PEN_electricity_MJoil = electricity_main.electricity_calculations_of_all_buildings( DHN_barcode, DCN_barcode, locator, master_to_slave_vars, network_features, gv, prices, lca, config) costs_USD += costs_electricity_USD GHG_tonCO2 += GHG_electricity_tonCO2 PEN_MJoil += PEN_electricity_MJoil natural_gas_main.natural_gas_imports(master_to_slave_vars, locator, config) # print "Add extra costs" # add costs of disconnected buildings (best configuration) (costs_additional_USD, GHG_additional_tonCO2, PEN_additional_MJoil) = cost_model.addCosts( building_names, locator, master_to_slave_vars, Q_uncovered_design_W, Q_uncovered_annual_W, solar_features, network_features, gv, config, prices, lca) costs_USD += costs_additional_USD GHG_tonCO2 += GHG_additional_tonCO2 PEN_MJoil += PEN_additional_MJoil costs_USD = (np.float64(costs_USD) / 1e6).round(2) # $ to Mio$ GHG_tonCO2 = (np.float64(GHG_tonCO2) / 1e6).round(2) # kg to kilo-ton PEN_MJoil = (np.float64(PEN_MJoil) / 1e6).round(2) # MJ to TJ # add electricity costs corresponding to # print ('Additional costs = ' + str(addCosts)) # print ('Additional CO2 = ' + str(addCO2)) # print ('Additional prim = ' + str(addPrim)) print('Total annualized costs [USD$(2015) Mio/yr] = ' + str(costs_USD)) print('Green house gas emission [kton-CO2/yr] = ' + str(GHG_tonCO2)) print('Primary energy [TJ-oil-eq/yr] = ' + str(PEN_MJoil)) results = { 'TAC_Mio_per_yr': [costs_USD.round(2)], 'CO2_kton_per_yr': [GHG_tonCO2.round(2)], 'Primary_Energy_TJ_per_yr': [PEN_MJoil.round(2)] } results_df = pd.DataFrame(results) results_path = os.path.join( locator.get_optimization_slave_results_folder(GENERATION_NUMBER), 'ind_' + str(individual_number) + '_results.csv') results_df.to_csv(results_path) with open(locator.get_optimization_checkpoint_initial(), "wb") as fp: pop = [] g = GENERATION_NUMBER epsInd = [] invalid_ind = [] fitnesses = [] capacities = [] disconnected_capacities = [] halloffame = [] halloffame_fitness = [] euclidean_distance = [] spread = [] cp = dict(population=pop, generation=g, epsIndicator=epsInd, testedPop=invalid_ind, population_fitness=fitnesses, capacities=capacities, disconnected_capacities=disconnected_capacities, halloffame=halloffame, halloffame_fitness=halloffame_fitness, euclidean_distance=euclidean_distance, spread=spread) json.dump(cp, fp) return costs_USD, GHG_tonCO2, PEN_MJoil, master_to_slave_vars, individual
def supply_calculation(individual, building_names, total_demand, locator, extra_costs, extra_CO2, extra_primary_energy, solar_features, network_features, gv, config, prices, lca): """ This function evaluates one supply system configuration of the case study. :param individual: a list that indicates the supply system configuration :type individual: list :param building_names: names of all building in the district :type building_names: ndarray :param locator: :param extra_costs: cost of decentralized supply systems :param extra_CO2: CO2 emission of decentralized supply systems :param extra_primary_energy: Primary energy of decentralized supply systems :param solar_features: Energy production potentials of solar technologies, including area of installed panels and annual production :type solar_features: dict :param network_features: hourly network operating conditions (thermal/pressure losses) and capital costs :type network_features: dict :param gv: :param config: :param prices: :return: """ individual = evaluation.check_invalid(individual, len(building_names), config) # Initialize objective functions costs, CO2 and primary energy costs = 0 CO2 = extra_CO2 prim = extra_primary_energy QUncoveredDesign = 0 QUncoveredAnnual = 0 # Create the string representation of the individual DHN_barcode, DCN_barcode, DHN_configuration, DCN_configuration = sFn.individual_to_barcode( individual, building_names) # read the total loads from buildings connected to thermal networks if DHN_barcode.count("1") == gv.num_tot_buildings: network_file_name_heating = "Network_summary_result_all.csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() elif DHN_barcode.count("1") == 0: network_file_name_heating = "Network_summary_result_all.csv" Q_heating_max_W = 0 else: # Run the substation and distribution routines sMain.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) nM.network_main(locator, total_demand, building_names, config, gv, DHN_barcode) network_file_name_heating = "Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() if DCN_barcode.count("1") == gv.num_tot_buildings: network_file_name_cooling = "Network_summary_result_all.csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() elif DCN_barcode.count("1") == 0: network_file_name_cooling = "Network_summary_result_none.csv" Q_cooling_max_W = 0 else: # Run the substation and distribution routines sMain.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) nM.network_main(locator, total_demand, building_names, config, gv, DCN_barcode) network_file_name_cooling = "Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) # Modify the individual with the extra GHP constraint try: cCheck.GHPCheck(individual, locator, Q_heating_nom_W, gv) except: print "No GHP constraint check possible \n" # Export to context individual_number = calc_individual_number(locator) master_to_slave_vars = evaluation.calc_master_to_slave_variables( individual, Q_heating_max_W, Q_cooling_max_W, building_names, individual_number, GENERATION_NUMBER) master_to_slave_vars.network_data_file_heating = network_file_name_heating master_to_slave_vars.network_data_file_cooling = network_file_name_cooling master_to_slave_vars.total_buildings = len(building_names) if master_to_slave_vars.number_of_buildings_connected_heating > 1: if DHN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DHN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DHN_barcode) if master_to_slave_vars.number_of_buildings_connected_cooling > 1: if DCN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DCN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DCN_barcode) # slave optimization of heating networks if config.optimization.isheating: if DHN_barcode.count("1") > 0: (slavePrim, slaveCO2, slaveCosts, QUncoveredDesign, QUncoveredAnnual) = sM.slave_main(locator, master_to_slave_vars, solar_features, gv, config, prices) else: slaveCO2 = 0 slaveCosts = 0 slavePrim = 0 else: slaveCO2 = 0 slaveCosts = 0 slavePrim = 0 costs += slaveCosts CO2 += slaveCO2 prim += slavePrim # slave optimization of cooling networks if gv.ZernezFlag == 1: coolCosts, coolCO2, coolPrim = 0, 0, 0 elif config.optimization.iscooling and DCN_barcode.count("1") > 0: reduced_timesteps_flag = config.supply_system_simulation.reduced_timesteps (coolCosts, coolCO2, coolPrim) = coolMain.coolingMain(locator, master_to_slave_vars, network_features, gv, prices, lca, config, reduced_timesteps_flag) # if reduced_timesteps_flag: # # reduced timesteps simulation for a month (May) # coolCosts = coolCosts * (8760/(3624/2880)) # coolCO2 = coolCO2 * (8760/(3624/2880)) # coolPrim = coolPrim * (8760/(3624/2880)) # # FIXME: check results else: coolCosts, coolCO2, coolPrim = 0, 0, 0 # print "Add extra costs" # add costs of disconnected buildings (best configuration) (addCosts, addCO2, addPrim) = eM.addCosts(DHN_barcode, DCN_barcode, building_names, locator, master_to_slave_vars, QUncoveredDesign, QUncoveredAnnual, solar_features, network_features, gv, config, prices, lca) # recalculate the addCosts by substracting the decentralized costs and add back to corresponding supply system Cost_diff, CO2_diff, Prim_diff = calc_decentralized_building_costs( config, locator, master_to_slave_vars, DHN_barcode, DCN_barcode, building_names) addCosts = addCosts + Cost_diff addCO2 = addCO2 + CO2_diff addPrim = addPrim + Prim_diff # add Capex and Opex of PV data_electricity = pd.read_csv( os.path.join( locator. get_optimization_slave_electricity_activation_pattern_cooling( individual_number, GENERATION_NUMBER))) total_area_for_pv = data_electricity['Area_PV_m2'].max() # remove the area installed with solar collectors sc_installed_area = 0 if config.supply_system_simulation.decentralized_systems == 'Single-effect Absorption Chiller': for (index, building_name) in zip(DCN_barcode, building_names): if index == "0": sc_installed_area = sc_installed_area + pd.read_csv( locator.PV_results(building_name))['Area_PV_m2'].max() pv_installed_area = total_area_for_pv - sc_installed_area Capex_a_PV, Opex_fixed_PV = calc_Cinv_pv(pv_installed_area, locator, config) pv_annual_production_kWh = (data_electricity['E_PV_W'].sum()) / 1000 # electricity calculations data_network_electricity = pd.read_csv( os.path.join( locator. get_optimization_slave_electricity_activation_pattern_cooling( individual_number, GENERATION_NUMBER))) data_cooling = pd.read_csv( os.path.join( locator.get_optimization_slave_cooling_activation_pattern( individual_number, GENERATION_NUMBER))) total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values total_electricity_demand_W = data_network_electricity['E_total_req_W'] E_decentralized_appliances_W = np.zeros(8760) for i, name in zip( DCN_barcode, building_names ): # adding the electricity demand from the decentralized buildings if i is '0': building_demand = pd.read_csv(locator.get_demand_results_folder() + '//' + name + ".csv", usecols=['E_sys_kWh']) E_decentralized_appliances_W += building_demand['E_sys_kWh'] * 1000 total_electricity_demand_W = total_electricity_demand_W.add( E_decentralized_appliances_W) E_for_hot_water_demand_W = np.zeros(8760) for i, name in zip( DCN_barcode, building_names ): # adding the electricity demand for hot water from all buildings building_demand = pd.read_csv(locator.get_demand_results_folder() + '//' + name + ".csv", usecols=['E_ww_kWh']) E_for_hot_water_demand_W += building_demand['E_ww_kWh'] * 1000 total_electricity_demand_W = total_electricity_demand_W.add( E_for_hot_water_demand_W) # Electricity of Energy Systems lca = lca_calculations(locator, config) E_VCC_W = data_cooling['Opex_var_VCC'] / lca.ELEC_PRICE E_VCC_backup_W = data_cooling['Opex_var_VCC_backup'] / lca.ELEC_PRICE E_ACH_W = data_cooling['Opex_var_ACH'] / lca.ELEC_PRICE E_CT_W = abs(data_cooling['Opex_var_CT']) / lca.ELEC_PRICE total_electricity_demand_W = total_electricity_demand_W.add(E_VCC_W) total_electricity_demand_W = total_electricity_demand_W.add(E_VCC_backup_W) total_electricity_demand_W = total_electricity_demand_W.add(E_ACH_W) total_electricity_demand_W = total_electricity_demand_W.add(E_CT_W) E_from_CHP_W = data_network_electricity[ 'E_CHP_to_directload_W'] + data_network_electricity['E_CHP_to_grid_W'] E_from_PV_W = data_network_electricity[ 'E_PV_to_directload_W'] + data_network_electricity['E_PV_to_grid_W'] E_CHP_to_directload_W = np.zeros(8760) E_CHP_to_grid_W = np.zeros(8760) E_PV_to_directload_W = np.zeros(8760) E_PV_to_grid_W = np.zeros(8760) E_from_grid_W = np.zeros(8760) # modify simulation timesteps if reduced_timesteps_flag == False: start_t = 0 stop_t = 8760 else: # timesteps in May start_t = 2880 stop_t = 3624 timesteps = range(start_t, stop_t) for hour in timesteps: E_hour_W = total_electricity_demand_W[hour] if E_hour_W > 0: if E_from_PV_W[hour] > E_hour_W: E_PV_to_directload_W[hour] = E_hour_W E_PV_to_grid_W[hour] = E_from_PV_W[ hour] - total_electricity_demand_W[hour] E_hour_W = 0 else: E_hour_W = E_hour_W - E_from_PV_W[hour] E_PV_to_directload_W[hour] = E_from_PV_W[hour] if E_from_CHP_W[hour] > E_hour_W: E_CHP_to_directload_W[hour] = E_hour_W E_CHP_to_grid_W[hour] = E_from_CHP_W[hour] - E_hour_W E_hour_W = 0 else: E_hour_W = E_hour_W - E_from_CHP_W[hour] E_CHP_to_directload_W[hour] = E_from_CHP_W[hour] E_from_grid_W[hour] = E_hour_W date = data_network_electricity.DATE.values results = pd.DataFrame( { "DATE": date, "E_total_req_W": total_electricity_demand_W, "E_from_grid_W": E_from_grid_W, "E_VCC_W": E_VCC_W, "E_VCC_backup_W": E_VCC_backup_W, "E_ACH_W": E_ACH_W, "E_CT_W": E_CT_W, "E_PV_to_directload_W": E_PV_to_directload_W, "E_CHP_to_directload_W": E_CHP_to_directload_W, "E_CHP_to_grid_W": E_CHP_to_grid_W, "E_PV_to_grid_W": E_PV_to_grid_W, "E_for_hot_water_demand_W": E_for_hot_water_demand_W, "E_decentralized_appliances_W": E_decentralized_appliances_W, "E_total_to_grid_W_negative": -E_PV_to_grid_W - E_CHP_to_grid_W } ) # let's keep this negative so it is something exported, we can use it in the graphs of likelihood if reduced_timesteps_flag: reduced_el_costs = ((results['E_from_grid_W'].sum() + results['E_total_to_grid_W_negative'].sum()) * lca.ELEC_PRICE) electricity_costs = reduced_el_costs * (8760 / (stop_t - start_t)) else: electricity_costs = ((results['E_from_grid_W'].sum() + results['E_total_to_grid_W_negative'].sum()) * lca.ELEC_PRICE) # emission from data data_emissions = pd.read_csv( os.path.join( locator.get_optimization_slave_investment_cost_detailed( individual_number, GENERATION_NUMBER))) update_PV_emission = abs( 2 * data_emissions['CO2_PV_disconnected']).values[0] # kg-CO2 update_PV_primary = abs( 2 * data_emissions['Eprim_PV_disconnected']).values[0] # MJ oil-eq costs += addCosts + coolCosts + electricity_costs + Capex_a_PV + Opex_fixed_PV CO2 = CO2 + addCO2 + coolCO2 - update_PV_emission prim = prim + addPrim + coolPrim - update_PV_primary # Converting costs into float64 to avoid longer values costs = (np.float64(costs) / 1e6).round(2) # $ to Mio$ CO2 = (np.float64(CO2) / 1e6).round(2) # kg to kilo-ton prim = (np.float64(prim) / 1e6).round(2) # MJ to TJ # add electricity costs corresponding to # print ('Additional costs = ' + str(addCosts)) # print ('Additional CO2 = ' + str(addCO2)) # print ('Additional prim = ' + str(addPrim)) print('Total annualized costs [USD$(2015) Mio/yr] = ' + str(costs)) print('Green house gas emission [kton-CO2/yr] = ' + str(CO2)) print('Primary energy [TJ-oil-eq/yr] = ' + str(prim)) results = { 'TAC_Mio_per_yr': [costs.round(2)], 'CO2_kton_per_yr': [CO2.round(2)], 'Primary_Energy_TJ_per_yr': [prim.round(2)] } results_df = pd.DataFrame(results) results_path = os.path.join( locator.get_optimization_slave_results_folder(GENERATION_NUMBER), 'ind_' + str(individual_number) + '_results.csv') results_df.to_csv(results_path) with open(locator.get_optimization_checkpoint_initial(), "wb") as fp: pop = [] g = GENERATION_NUMBER epsInd = [] invalid_ind = [] fitnesses = [] capacities = [] disconnected_capacities = [] halloffame = [] halloffame_fitness = [] euclidean_distance = [] spread = [] cp = dict(population=pop, generation=g, epsIndicator=epsInd, testedPop=invalid_ind, population_fitness=fitnesses, capacities=capacities, disconnected_capacities=disconnected_capacities, halloffame=halloffame, halloffame_fitness=halloffame_fitness, euclidean_distance=euclidean_distance, spread=spread) json.dump(cp, fp) return costs, CO2, prim, master_to_slave_vars, individual
def evaluation_main(individual, building_names, locator, extraCosts, extraCO2, extraPrim, solar_features, network_features, gv, config, prices, lca, ind_num, gen): """ This function evaluates an individual :param individual: list with values of the individual :param building_names: list with names of buildings :param locator: locator class :param extraCosts: costs calculated before optimization of specific energy services (process heat and electricity) :param extraCO2: green house gas emissions calculated before optimization of specific energy services (process heat and electricity) :param extraPrim: primary energy calculated before optimization ofr specific energy services (process heat and electricity) :param solar_features: solar features call to class :param network_features: network features call to class :param gv: global variables class :param optimization_constants: class containing constants used in optimization :param config: configuration file :param prices: class of prices used in optimization :type individual: list :type building_names: list :type locator: string :type extraCosts: float :type extraCO2: float :type extraPrim: float :type solar_features: class :type network_features: class :type gv: class :type optimization_constants: class :type config: class :type prices: class :return: Resulting values of the objective function. costs, CO2, prim :rtype: tuple """ # Check the consistency of the individual or create a new one individual = check_invalid(individual, len(building_names), config) # Initialize objective functions costs, CO2 and primary energy costs = extraCosts CO2 = extraCO2 prim = extraPrim QUncoveredDesign = 0 QUncoveredAnnual = 0 # Create the string representation of the individual DHN_barcode, DCN_barcode, DHN_configuration, DCN_configuration = sFn.individual_to_barcode( individual, building_names) if DHN_barcode.count("1") == gv.num_tot_buildings: network_file_name_heating = "Network_summary_result_all.csv" Q_DHNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() elif DHN_barcode.count("1") == 0: network_file_name_heating = "Network_summary_result_all.csv" Q_heating_max_W = 0 else: network_file_name_heating = "Network_summary_result_" + hex( int(str(DHN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary(DHN_barcode)): total_demand = sFn.createTotalNtwCsv(DHN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines sMain.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) nM.network_main(locator, total_demand, building_names, config, gv, DHN_barcode) Q_DHNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DHN_barcode), usecols=["Q_DHNf_W"]).values Q_heating_max_W = Q_DHNf_W.max() if DCN_barcode.count("1") == gv.num_tot_buildings: network_file_name_cooling = "Network_summary_result_all.csv" if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_all_results_summary('all'), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() elif DCN_barcode.count("1") == 0: network_file_name_cooling = "Network_summary_result_all.csv" Q_cooling_max_W = 0 else: network_file_name_cooling = "Network_summary_result_" + hex( int(str(DCN_barcode), 2)) + ".csv" if not os.path.exists( locator.get_optimization_network_results_summary(DCN_barcode)): total_demand = sFn.createTotalNtwCsv(DCN_barcode, locator) building_names = total_demand.Name.values # Run the substation and distribution routines sMain.substation_main(locator, total_demand, building_names, DHN_configuration, DCN_configuration, Flag=True) nM.network_main(locator, total_demand, building_names, config, gv, DCN_barcode) if individual[ N_HEAT * 2] == 1: # if heat recovery is ON, then only need to satisfy cooling load of space cooling and refrigeration Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=["Q_DCNf_space_cooling_and_refrigeration_W"]).values else: Q_DCNf_W = pd.read_csv( locator.get_optimization_network_results_summary(DCN_barcode), usecols=[ "Q_DCNf_space_cooling_data_center_and_refrigeration_W" ]).values Q_cooling_max_W = Q_DCNf_W.max() Q_heating_nom_W = Q_heating_max_W * (1 + Q_MARGIN_FOR_NETWORK) Q_cooling_nom_W = Q_cooling_max_W * (1 + Q_MARGIN_FOR_NETWORK) # Modify the individual with the extra GHP constraint try: cCheck.GHPCheck(individual, locator, Q_heating_nom_W, gv) except: print "No GHP constraint check possible \n" # Export to context master_to_slave_vars = calc_master_to_slave_variables( individual, Q_heating_max_W, Q_cooling_max_W, building_names, ind_num, gen) master_to_slave_vars.network_data_file_heating = network_file_name_heating master_to_slave_vars.network_data_file_cooling = network_file_name_cooling master_to_slave_vars.total_buildings = len(building_names) if master_to_slave_vars.number_of_buildings_connected_heating > 1: if DHN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DHN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DHN_barcode) if master_to_slave_vars.number_of_buildings_connected_cooling > 1: if DCN_barcode.count("0") == 0: master_to_slave_vars.fNameTotalCSV = locator.get_total_demand() else: master_to_slave_vars.fNameTotalCSV = os.path.join( locator.get_optimization_network_totals_folder(), "Total_%(DCN_barcode)s.csv" % locals()) else: master_to_slave_vars.fNameTotalCSV = locator.get_optimization_substations_total_file( DCN_barcode) if config.optimization.isheating: if DHN_barcode.count("1") > 0: (slavePrim, slaveCO2, slaveCosts, QUncoveredDesign, QUncoveredAnnual) = sM.slave_main(locator, master_to_slave_vars, solar_features, gv, config, prices, lca) else: slaveCO2 = 0 slaveCosts = 0 slavePrim = 0 else: slaveCO2 = 0 slaveCosts = 0 slavePrim = 0 costs += slaveCosts CO2 += slaveCO2 prim += slavePrim if gv.ZernezFlag == 1: coolCosts, coolCO2, coolPrim = 0, 0, 0 elif config.optimization.iscooling: reduced_timesteps_flag = False (coolCosts, coolCO2, coolPrim) = coolMain.coolingMain(locator, master_to_slave_vars, network_features, gv, prices, lca, config, reduced_timesteps_flag) else: coolCosts, coolCO2, coolPrim = 0, 0, 0 print "Add extra costs" (addCosts, addCO2, addPrim) = eM.addCosts(DHN_barcode, DCN_barcode, building_names, locator, master_to_slave_vars, QUncoveredDesign, QUncoveredAnnual, solar_features, network_features, gv, config, prices, lca) costs += addCosts + coolCosts CO2 += addCO2 + coolCO2 prim += addPrim + coolPrim # Converting costs into float64 to avoid longer values costs = np.float64(costs) CO2 = np.float64(CO2) prim = np.float64(prim) print('Total costs = ' + str(costs)) print('Total CO2 = ' + str(CO2)) print('Total prim = ' + str(prim)) return costs, CO2, prim, master_to_slave_vars, individual