def test_get_share_s_tech_ey(): """testing""" service_switches = [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.3, switch_yr=2020) ] service_switches = { 'regA': [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.3, switch_yr=2020) ] } defined_temporal_narrative_points = [2020] result = fuel_service_switch.get_share_s_tech_ey( service_switches=service_switches) assert result['heating']['regA'][2020]['techA'] == 0.3
def convert_sharesdict_to_service_switches(yr_until_switched, enduse, s_tech_switched_p, regions=False, regional_specific=False): """Convert service of technologies to service switches. Arguments --------- yr_until_switched : int Year until switch happens enduse : str Enduse s_tech_switched_p : dict Fraction of total service of technologies after switch regions : dict, default=False All regions regional_specific : bool, default=False Criteria wheter region specific calculations Returns ------- service_switches_after_fuel_switch : dict Changed services witches including fuel switches """ if regional_specific: new_service_switches = {} for reg in regions: new_service_switches[reg] = [] for tech, s_tech_p in s_tech_switched_p[reg].items(): if tech == 'placeholder_tech': pass else: switch_new = read_data.ServiceSwitch( enduse=enduse, technology_install=tech, service_share_ey=s_tech_p, switch_yr=yr_until_switched) new_service_switches[reg].append(switch_new) else: new_service_switches = [] for tech, s_tech_p in s_tech_switched_p.items(): if tech == 'placeholder_tech': pass else: switch_new = read_data.ServiceSwitch( enduse=enduse, technology_install=tech, service_share_ey=s_tech_p, switch_yr=yr_until_switched) new_service_switches.append(switch_new) return new_service_switches
def test_get_enduse_configuration(): """Testing """ fuel_switches = [ read_data.FuelSwitch(enduse='heating', fueltype_replace="", technology_install='boilerB', switch_yr=2020, fuel_share_switched_ey="") ] service_switches = [ read_data.ServiceSwitch(technology_install='boilerA', switch_yr=2050) ] mode_constrained = enduse_func.get_enduse_configuration( mode_constrained=False, enduse='heating', enduse_space_heating=['heating']) assert mode_constrained == False # --- service_switches = [ read_data.ServiceSwitch(enduse='heating', technology_install='boilerA', switch_yr=2050) ] mode_constrained = enduse_func.get_enduse_configuration( mode_constrained=True, enduse='heating', enduse_space_heating=['heating']) assert mode_constrained == True # --- fuel_switches = [ read_data.FuelSwitch(enduse='heating', fueltype_replace="", technology_install='boilerB', switch_yr=2020, fuel_share_switched_ey="") ] service_switches = [] mode_constrained = enduse_func.get_enduse_configuration( mode_constrained=False, enduse='heating', enduse_space_heating=['heating']) assert mode_constrained == False
def test_autocomplete_switches(): """testing""" service_switches = [read_data.ServiceSwitch( enduse='heating', technology_install='techA', switch_yr=2050, service_share_ey=0.6)] defined_temporal_narrative_points = {'heating': [2050]} enduses = {'submodelA': ['heating']} sectors = {'submodelA': [None]} out = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by={'heating': {None: ['techA', 'techB', 'techC']}}, s_tech_by_p={None: {'heating': {'techA': 0.2, 'techB': 0.4, 'techC': 0.4}}}, crit_all_the_same=True, enduses=enduses, sectors=sectors, regions=['regA'], f_diffusion=False, techs_affected_spatial_f=False, service_switches_from_capacity={'regA':[]}) assert out[None]['heating']['regA'][2050]['techA'] == 0.6 assert out[None]['heating']['regA'][2050]['techB'] == 0.2 assert out[None]['heating']['regA'][2050]['techC'] == 0.2 # --- service_switches = [read_data.ServiceSwitch( enduse='heating', technology_install='techA', switch_yr=2050, service_share_ey=0.6)] out = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by={'heating': {None: ['techA', 'techB', 'techC']}}, s_tech_by_p= {None: {'heating': {'techA': 0.2, 'techB': 0.4, 'techC': 0.4}}}, crit_all_the_same=True, enduses=enduses, sectors=sectors, regions=['regA'], f_diffusion={'heating':{'regA':1.0, 'regB': 1.0}}, techs_affected_spatial_f=['techA'], service_switches_from_capacity={'regA': [], 'regB': []}) assert out[None]['heating']['regA'][2050]['techA'] == 0.6 assert out[None]['heating']['regA'][2050]['techB'] == 0.2 assert out[None]['heating']['regA'][2050]['techC'] == 0.2
def test_create_switches_from_s_shares(): enduse_switches = [ read_data.ServiceSwitch(enduse='heating', technology_install='techA', switch_yr=2050, service_share_ey=0.6) ] out = fuel_service_switch.create_switches_from_s_shares( enduse='heating', s_tech_by_p={'heating': { 'techA': 0.2, 'techB': 0.8 }}, switch_technologies=['techA'], specified_tech_enduse_by={'heating': ['techA', 'techB']}, enduse_switches=enduse_switches, s_tot_defined=0.6, sector=None, switch_yr=2050) for switch in out: if switch.technology_install == 'techA': assert switch.service_share_ey == 0.6 if switch.technology_install == 'techB': assert switch.service_share_ey == 0.4
def test_get_fuel_switches_enduse(): service_switches = { 'regA': [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch(enduse='other', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020) ] } out = fuel_service_switch.get_switches_of_enduse(switches=service_switches, enduse='heating') for reg, switches in out.items(): for switch in switches: assert switch.enduse == 'heating' # -- service_switches = { 'regA': [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch(enduse='other', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020) ] } out = fuel_service_switch.get_switches_of_enduse(switches=service_switches, enduse='heating') for reg in out: for switch in out[reg]: assert switch.enduse == 'heating'
def autocomplete_switches(): """Testing """ service_switches = [read_data.ServiceSwitch( 'heating', 'techA', 0.5, 2020)] specified_tech_enduse_by = {'heating': ['techA', 'techB']} s_tech_by_p = {'heating': {'techA': 0.8, 'techB': 0.2}} service_switches = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by=specified_tech_enduse_by, s_tech_by_p=s_tech_by_p) for switch in service_switches: if switch.technology_install == 'techA': assert switch.service_share_ey == 0.5 if switch.technology_install == 'techB': assert switch.service_share_ey == 0.5 #--- service_switches = [read_data.ServiceSwitch( 'heating', 'techA', 0.3, 2020)] specified_tech_enduse_by = {'heating': ['techA', 'techB', 'techC']} s_tech_by_p = {'heating': {'techA': 0.6, 'techB': 0.2,'techC': 0.1}} service_switches = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by=specified_tech_enduse_by, s_tech_by_p=s_tech_by_p) for switch in service_switches: if switch.technology_install == 'techA': assert switch.service_share_ey == 0.3 if switch.technology_install == 'techB': assert switch.service_share_ey == 0.7 * (2.0 / 3.0) if switch.technology_install == 'techC': assert switch.service_share_ey == 0.7 * (1.0 / 3.0)
def test_get_fuel_switches_enduse(): service_switches = [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch(enduse='other', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020) ] out = fuel_service_switch.get_fuel_switches_enduse( switches=service_switches, enduse='heating', regional_specific=False) for switch in out: assert switch.enduse == 'heating' # -- service_switches = { 'regA': [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch(enduse='other', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020) ] } out = fuel_service_switch.get_fuel_switches_enduse( switches=service_switches, enduse='heating', regional_specific=True) for reg in out: for switch in out[reg]: assert switch.enduse == 'heating'
def test_switches_to_dict(): """testing""" service_switches = [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020) ] out = fuel_service_switch.switches_to_dict( service_switches=service_switches, regional_specific=False) assert out == {'techA': 0.5, 'techB': 0.5}
def test_switches_to_dict(): """testing""" service_switches = {'regA': [ read_data.ServiceSwitch( enduse='heating', sector=None, technology_install='techA', service_share_ey=0.5, switch_yr=2020), read_data.ServiceSwitch( enduse='heating', sector=None, technology_install='techB', service_share_ey=0.5, switch_yr=2020)]} out = fuel_service_switch.switches_to_dict( service_switches=service_switches) assert out['regA'] == {'techA': 0.5, 'techB': 0.5}
def test_get_share_s_tech_ey(): """testing""" service_switches = [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.3, switch_yr=2020) ] specified_tech_enduse_by = {'heating': ['techA', 'techB', 'techC']} result = fuel_service_switch.get_share_s_tech_ey( service_switches=service_switches, specified_tech_enduse_by=specified_tech_enduse_by) # -- service_switches = { 'regA': [ read_data.ServiceSwitch(enduse='heating', sector=None, technology_install='techA', service_share_ey=0.3, switch_yr=2020) ] } specified_tech_enduse_by = { 'regA': { 'heating': ['techA', 'techB', 'techC'] } } result = fuel_service_switch.get_share_s_tech_ey( service_switches=service_switches, specified_tech_enduse_by=specified_tech_enduse_by, spatial_exliclit_diffusion=True) assert result['heating']['regA']['techA'] == 0.3
def create_service_switch(narrative_timesteps, enduse, sector, switch, enduse_capacity_switches, technologies, fuel_shares_enduse_by, base_yr, fuel_enduse_y): """Generate service switch based on capacity assumptions Arguments --------- narrative_timesteps : list Narrative timesteps enduse : dict Enduse switch : obj Capacity switch enduse_capacity_switches : list All capacity switches of an enduse (see warning) technologies : dict Technologies fuel_shares_enduse_by : dict Fuel shares per enduse for base year base_yr : int base year fuel_enduse_y : dict Fuels Returns ------ service_switches : dict Service switches Major steps ------- 1. Convert fuel per technology to service in ey 2. Convert installed capacity to service of ey and add service 3. Calculate percentage of service for ey 4. Write out as service switch """ service_switches_enduse = [] for switch_yr in narrative_timesteps: # Get switches of narrative timestep capacity_switches = get_switches_of_enduse(enduse_capacity_switches, enduse, year=switch_yr, crit_region=False) # --------------------------------------------- # Calculate service per technology for switch_yr # --------------------------------------------- service_enduse_tech = {} for fueltype, tech_fuel_shares in fuel_shares_enduse_by.items(): for tech, fuel_share_by in tech_fuel_shares.items(): # Efficiency of year when capacity is fully installed eff_cy = tech_related.calc_eff_cy( base_yr, switch.switch_yr, technologies[tech].eff_by, technologies[tech].eff_ey, technologies[tech].year_eff_ey, technologies[tech].eff_achieved, technologies[tech].diff_method) # Convert to service (fuel * fuelshare * eff) s_tech_ey_y = fuel_enduse_y[fueltype] * fuel_share_by * eff_cy service_enduse_tech[tech] = s_tech_ey_y # ------------------------------------------- # Calculate service of installed capacity of # increased (installed) technologies # ------------------------------------------- for switch in capacity_switches: eff_cy = tech_related.calc_eff_cy( base_yr, switch.switch_yr, technologies[switch.technology_install].eff_by, technologies[switch.technology_install].eff_ey, technologies[switch.technology_install].year_eff_ey, technologies[switch.technology_install].eff_achieved, technologies[switch.technology_install].diff_method) # Convert installed capacity to service installed_capacity_ey = switch.installed_capacity * eff_cy # Add capacity service_enduse_tech[ switch.technology_install] += installed_capacity_ey # ------------------------------------------- # Calculate service in % per enduse # ------------------------------------------- tot_s = sum(service_enduse_tech.values()) for tech, service_tech in service_enduse_tech.items(): service_enduse_tech[tech] = service_tech / tot_s # ------------------------------------------- # Add to switch of technology_install # ------------------------------------------- for tech, s_tech_p in service_enduse_tech.items(): service_switch = read_data.ServiceSwitch(enduse=enduse, sector=sector, technology_install=tech, service_share_ey=s_tech_p, switch_yr=switch_yr) service_switches_enduse.append(service_switch) return service_switches_enduse
def autocomplete_switches(service_switches, specified_tech_enduse_by, s_tech_by_p, enduses, sectors, crit_all_the_same=True, regions=False, f_diffusion=False, techs_affected_spatial_f=False, service_switches_from_capacity=[]): """Add not defined technologies in switches and set correct future service share. If the defined service switches do not sum up to 100% service, the remaining service is distriputed proportionally to all remaining technologies. Arguments --------- service_switches : dict Defined service switches specified_tech_enduse_by : dict Specified technologies of an enduse s_tech_by_p : dict Share of service of technology in base year enduses : list Enduses sectors : list Sectors crit_all_the_same : bool Criteria wheter regionally specific or not regions : list Regions f_diffusion : Diffusion factors techs_affected_spatial_f : list Technologies affected by spatial diffusion service_switches_from_capacity : list Transformed capacity swithces into service switches Returns ------- reg_share_s_tech_ey_p : dict Shares per technology in end year service_switches_out : list Added services switches which now in total sum up to 100% """ reg_share_s_tech_ey_p = {} service_switches_out = {} # Enduses form direct switches switch_enduses = get_all_enduses_of_switches(service_switches) # Enduses from capacity witches any_region = list(service_switches_from_capacity.keys())[0] switch_enduses_capacity_switches = get_all_enduses_of_switches( service_switches_from_capacity[any_region]) # All enduses switch_enduses = set(switch_enduses + switch_enduses_capacity_switches) # Iterate enduses for enduse in switch_enduses: print("... calculating service switches: {} crit_all_the_same: {}". format(enduse, crit_all_the_same)) sectors_of_enduse = get_sectors_of_enduse(enduse, enduses, sectors) for sector in sectors_of_enduse: switches_enduse = init_scripts.get_sector__enduse_switches( sector, enduse, service_switches) # ----------------------------------------- # Append service switches which were # generated from capacity switches # ----------------------------------------- service_switches_out[sector] = {} for region in regions: service_switches_out[sector][ region] = service_switches_from_capacity[region] if crit_all_the_same: updated_switches = [] # Get all narrative years switch_yrs = get_all_narrative_points(switches_enduse, enduse=enduse) # Execut function for every narrative year for switch_yr in switch_yrs: enduse_switches = [] s_tot_defined = 0 switch_technologies = [] for switch in switches_enduse: if switch.enduse == enduse and switch.switch_yr == switch_yr: s_tot_defined += switch.service_share_ey switch_technologies.append( switch.technology_install) enduse_switches.append(switch) # Calculate relative by proportion of not assigned technologies switches_new = create_switches_from_s_shares( enduse=enduse, s_tech_by_p=s_tech_by_p, #??TOTOD TODO TODO switch_technologies=switch_technologies, specified_tech_enduse_by=specified_tech_enduse_by, enduse_switches=enduse_switches, s_tot_defined=s_tot_defined, sector=sector, switch_yr=switch_yr) updated_switches.extend(switches_new) # For every region, set same switches for region in regions: service_switches_out[sector][region].extend( updated_switches) else: for region in regions: # Get all narrative year switch_yrs = get_all_narrative_points(service_switches, enduse=enduse) # Execut function for yvery narrative year for switch_yr in switch_yrs: # Get all switches of this enduse enduse_switches = [] s_tot_defined = 0 switch_technologies = [] for switch in switches_enduse: if switch.enduse == enduse and switch.switch_yr == switch_yr: # Global share of technology diffusion s_share_ey_global = switch.service_share_ey # If technology is affected by spatial exlicit diffusion if switch.technology_install in techs_affected_spatial_f: # Regional diffusion calculation s_share_ey_regional = s_share_ey_global * f_diffusion[ enduse][region] # ------------------------------------- # if larger than max crit, set to 1 # ------------------------------------- max_crit = 1 if s_share_ey_regional > max_crit: s_share_ey_regional = max_crit if s_tot_defined + s_share_ey_regional > 1.0: if round(s_tot_defined + s_share_ey_regional) > 1: # TODO TODO TODO logging.warning("{} {} {}".format( s_tot_defined, s_share_ey_regional, s_tot_defined + s_share_ey_regional)) raise Exception( "Error of regional parameter calcuation. More than one technology switched with larger share" ) else: s_share_ey_regional = max_crit - s_share_ey_regional else: s_share_ey_regional = switch.service_share_ey switch_new = read_data.ServiceSwitch( enduse=switch.enduse, sector=switch.sector, technology_install=switch. technology_install, service_share_ey=s_share_ey_regional, switch_yr=switch.switch_yr) s_tot_defined += s_share_ey_regional switch_technologies.append( switch.technology_install) enduse_switches.append(switch_new) # Create switch switches_new = create_switches_from_s_shares( enduse=enduse, s_tech_by_p=s_tech_by_p, switch_technologies=switch_technologies, specified_tech_enduse_by= specified_tech_enduse_by, enduse_switches=enduse_switches, s_tot_defined=s_tot_defined, sector=sector, switch_yr=switch_yr) service_switches_out[sector][region].extend( switches_new) # Calculate fraction of service for each technology reg_share_s_tech_ey_p[sector] = get_share_s_tech_ey( service_switches_out[sector]) return reg_share_s_tech_ey_p
def create_switches_from_s_shares(enduse, s_tech_by_p, switch_technologies, specified_tech_enduse_by, enduse_switches, s_tot_defined, sector, switch_yr): """ Create swiches from service shares. If not 100% of the service share is defined, the switches get defined which proportionally decrease service of all not switched technologies Arguments ---------- enduse : str Enduse s_tech_by_p : Service share per technology in base year switch_technologies : list Technologies involved in switch specified_tech_enduse_by : list Technologies per enduse enduse_switches : list Switches s_tot_defined : float Service defined sector : str Sector switch_yr : int Year Returns ------- service_switches_out : dict Service switches """ service_switches_out = [] # Calculate relative by proportion of not assigned technologies in base year tech_not_assigned_by_p = {} for tech in specified_tech_enduse_by[enduse][sector]: if tech not in switch_technologies: tech_not_assigned_by_p[tech] = s_tech_by_p[sector][enduse][tech] # Normalise: convert to percentage tot_share_not_assigned = sum(tech_not_assigned_by_p.values()) # If no fuel is assigned to enduse, do not change switches if tot_share_not_assigned == 0: service_switches_out = enduse_switches else: for tech, share_by in tech_not_assigned_by_p.items(): tech_not_assigned_by_p[tech] = share_by / tot_share_not_assigned # Get all defined technologies in base year for tech in specified_tech_enduse_by[enduse][sector]: if tech not in switch_technologies: if s_tot_defined == 1.0: # All technologies are fully defined switch_new = read_data.ServiceSwitch( enduse=enduse, sector=sector, technology_install=tech, service_share_ey=0, switch_yr=switch_yr) else: # Calculate not defined share in switches s_not_assigned = 1 - s_tot_defined # Reduce not assigned share proportionally tech_ey_p = tech_not_assigned_by_p[tech] * s_not_assigned switch_new = read_data.ServiceSwitch( enduse=enduse, sector=sector, technology_install=tech, service_share_ey=tech_ey_p, switch_yr=switch_yr) service_switches_out.append(switch_new) else: # If assigned copy to final switches for switch_new in enduse_switches: if switch_new.technology_install == tech: # If no fuel demand, also no switch possible if tot_share_not_assigned == 0: switch_new.service_share_ey = 0 service_switches_out.append(switch_new) return service_switches_out
def test_autocomplete_switches(): """testing""" service_switches = [ read_data.ServiceSwitch(enduse='heating', technology_install='techA', switch_yr=2050, service_share_ey=0.6) ] out_1, out_2 = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by={'heating': ['techA', 'techB', 'techC']}, s_tech_by_p={'heating': { 'techA': 0.2, 'techB': 0.4, 'techC': 0.4 }}, sector=False, spatial_exliclit_diffusion=False, regions=False, f_diffusion=False, techs_affected_spatial_f=False, service_switches_from_capacity=[]) for switch in out_2: if switch.technology_install == 'techA': assert switch.service_share_ey == 0.6 if switch.technology_install == 'techB': assert switch.service_share_ey == 0.2 if switch.technology_install == 'techC': assert switch.service_share_ey == 0.2 # --- service_switches = [ read_data.ServiceSwitch(enduse='heating', technology_install='techA', switch_yr=2050, service_share_ey=0.6) ] out_1, out_2 = fuel_service_switch.autocomplete_switches( service_switches=service_switches, specified_tech_enduse_by={'heating': ['techA', 'techB', 'techC']}, s_tech_by_p={'heating': { 'techA': 0.2, 'techB': 0.4, 'techC': 0.4 }}, sector=False, spatial_exliclit_diffusion=True, regions=['regA', 'regB'], f_diffusion={'heating': { 'regA': 1.0, 'regB': 1.0 }}, techs_affected_spatial_f=['techA'], service_switches_from_capacity={ 'regA': [], 'regB': [] }) for reg in out_2: for switch in out_2[reg]: if switch.technology_install == 'techA': assert switch.service_share_ey == 0.6 if switch.technology_install == 'techB': assert switch.service_share_ey == 0.2 if switch.technology_install == 'techC': assert switch.service_share_ey == 0.2
def create_service_switch( enduse, sector, switch, enduse_capacity_switches, technologies, other_enduse_mode_info, fuel_shares_enduse_by, base_yr, fuel_enduse_y ): """Generate service switch based on capacity assumptions Arguments --------- enduse : dict Enduse switch : obj Capacity switch enduse_capacity_switches : list All capacity switches of an enduse (see warning) technologies : dict Technologies other_enduse_mode_info : dict OTher diffusion information fuel_shares_enduse_by : dict Fuel shares per enduse for base year base_yr : int base year fuel_enduse_y : dict Fuels Returns ------ service_switches : dict Service switches Major steps ------- 1. Convert fuel per technology to service in ey 2. Convert installed capacity to service of ey and add service 3. Calculate percentage of service for ey 4. Write out as service switch Warning ------- - The year until the switch happens must be the same for all switches """ # ------------ # Calculate year until switch happens # ----------- for switch in enduse_capacity_switches: switch_yr = switch.switch_yr continue # --------------------------------------------- # Calculate service per technology for end year # --------------------------------------------- service_enduse_tech = {} for fueltype, tech_fuel_shares in fuel_shares_enduse_by.items(): for tech, fuel_share_by in tech_fuel_shares.items(): # Efficiency of year when capacity is fully installed eff_ey = tech_related.calc_eff_cy( base_yr, switch.switch_yr, technologies[tech].eff_by, technologies[tech].eff_ey, technologies[tech].year_eff_ey, other_enduse_mode_info, technologies[tech].eff_achieved, technologies[tech].diff_method) # Convert to service (fuel * fuelshare * eff) s_tech_ey_y = fuel_enduse_y[fueltype] * fuel_share_by * eff_ey service_enduse_tech[tech] = s_tech_ey_y # ------------------------------------------- # Calculate service of installed capacity of increased # (installed) technologies # ------------------------------------------- for switch in enduse_capacity_switches: eff_ey = tech_related.calc_eff_cy( base_yr, switch.switch_yr, technologies[switch.technology_install].eff_by, technologies[switch.technology_install].eff_ey, technologies[switch.technology_install].year_eff_ey, other_enduse_mode_info, technologies[switch.technology_install].eff_achieved, technologies[switch.technology_install].diff_method) # Convert installed capacity to service installed_capacity_ey = switch.installed_capacity * eff_ey # Add capacity service_enduse_tech[switch.technology_install] += installed_capacity_ey # ------------------------------------------- # Calculate service in % per enduse # ------------------------------------------- tot_s = sum(service_enduse_tech.values()) for tech, service_tech in service_enduse_tech.items(): service_enduse_tech[tech] = service_tech / tot_s # ------------------------------------------- # Add to switch of technology_install # ------------------------------------------- service_switches_enduse = [] for tech, s_tech_p in service_enduse_tech.items(): service_switch = read_data.ServiceSwitch( enduse=enduse, sector=sector, technology_install=tech, service_share_ey=s_tech_p, switch_yr=switch_yr) service_switches_enduse.append(service_switch) return service_switches_enduse
def autocomplete_switches( service_switches, specified_tech_enduse_by, s_tech_by_p, sector=False, spatial_exliclit_diffusion=False, regions=False, f_diffusion=False, techs_affected_spatial_f=False, service_switches_from_capacity=[] ): """Add not defined technologies in switches and set correct future service share. If the defined service switches do not sum up to 100% service, the remaining service is distriputed proportionally to all remaining technologies. Arguments --------- service_switches : dict Defined service switches specified_tech_enduse_by : dict Specified technologies of an enduse s_tech_by_p : dict Share of service of technology in base year Returns ------- reg_share_s_tech_ey_p : dict Shares per technology in end year service_switches_out : list Added services switches which now in total sum up to 100% """ # Get all enduses defined in switches enduses = set([]) for switch in service_switches: enduses.add(switch.enduse) enduses = list(enduses) if spatial_exliclit_diffusion: service_switches_out = {} for region in regions: service_switches_out[region] = [] # Append regional other capacity switches service_switches_out[region].extend(service_switches_from_capacity[region]) for enduse in enduses: # Get all switches of this enduse enduse_switches = [] s_tot_defined = 0 switch_technologies = [] for switch in service_switches: if switch.enduse == enduse: # Global share of technology diffusion s_share_ey_global = switch.service_share_ey # IF technology is affected by spatial exlicit diffusion if switch.technology_install in techs_affected_spatial_f: # Regional diffusion calculation s_share_ey_regional = s_share_ey_global * f_diffusion[enduse][region] # if larger than max crit, set to 1 TODO max_crit = 1 if s_share_ey_regional > max_crit: s_share_ey_regional = max_crit if s_tot_defined + s_share_ey_regional > 1.0: #TODO IMPROVE THAT ROUNDING 1 .. make nicer if round(s_tot_defined + s_share_ey_regional) > 1: logging.warning("ERROR: MORE THAN ONE TECHNOLOG SWICHED WITH LARGER SHARE: ") logging.warning(" {} {} {}".format(s_tot_defined, s_share_ey_regional, s_tot_defined + s_share_ey_regional)) prnt(".") else: s_share_ey_regional = max_crit - s_share_ey_regional #logging.debug("A %s %s", region, switch.technology_install) #logging.debug(s_share_ey_global) #logging.debug(s_share_ey_regional) else: s_share_ey_regional = switch.service_share_ey switch_new = read_data.ServiceSwitch( enduse=switch.enduse, sector=switch.sector, technology_install=switch.technology_install, service_share_ey=s_share_ey_regional, switch_yr=switch.switch_yr) s_tot_defined += s_share_ey_regional switch_technologies.append(switch.technology_install) enduse_switches.append(switch_new) switch_yr = switch.switch_yr # Create switch switches_new = create_switches_from_s_shares( enduse=enduse, s_tech_by_p=s_tech_by_p, switch_technologies=switch_technologies, specified_tech_enduse_by=specified_tech_enduse_by, enduse_switches=enduse_switches, s_tot_defined=s_tot_defined, sector=sector, switch_yr=switch_yr) service_switches_out[region].extend(switches_new) else: service_switches_out = [] for enduse in enduses: # Get all switches of this enduse enduse_switches = [] s_tot_defined = 0 switch_technologies = [] for switch in service_switches: if switch.enduse == enduse: s_tot_defined += switch.service_share_ey switch_technologies.append(switch.technology_install) enduse_switches.append(switch) switch_yr = switch.switch_yr # Calculate relative by proportion of not assigned technologies switches_new = create_switches_from_s_shares( enduse=enduse, s_tech_by_p=s_tech_by_p, switch_technologies=switch_technologies, specified_tech_enduse_by=specified_tech_enduse_by, enduse_switches=enduse_switches, s_tot_defined=s_tot_defined, sector=sector, switch_yr=switch_yr) service_switches_out.extend(switches_new) service_switches_out.extend(service_switches_from_capacity) # Calculate fraction of service for each technology reg_share_s_tech_ey_p = get_share_s_tech_ey( service_switches_out, specified_tech_enduse_by, spatial_exliclit_diffusion) return reg_share_s_tech_ey_p, service_switches_out