Пример #1
0
def test_get_switch_criteria():
    """testing
    """
    crit_switch_happening = {'heating': ['sectorA']}

    crit = fuel_service_switch.get_switch_criteria(
        enduse='heating',
        sector='sectorA',
        crit_switch_happening=crit_switch_happening,
        base_yr=2015,
        curr_yr=2015)

    assert crit == False

    crit = fuel_service_switch.get_switch_criteria(
        enduse='heating',
        sector='sectorA',
        crit_switch_happening=crit_switch_happening,
        base_yr=2015,
        curr_yr=2025)

    assert crit == True

    crit = fuel_service_switch.get_switch_criteria(
        enduse='heating',
        sector='sectorB',
        crit_switch_happening=crit_switch_happening,
        base_yr=2015,
        curr_yr=2025)

    assert crit == False
Пример #2
0
def sig_param_calc_incl_fuel_switch(narrative_timesteps,
                                    base_yr,
                                    crit_switch_happening,
                                    technologies,
                                    enduse,
                                    sector,
                                    fuel_switches,
                                    s_tech_by_p,
                                    s_fueltype_by_p,
                                    share_s_tech_ey_p,
                                    fuel_tech_p_by,
                                    regions=False,
                                    crit_all_the_same=True):
    """Calculate sigmoid diffusion paramaters considering
    fuel and service switches. Test if service switch and
    fuel switch are defined simultaneously (raise error if true).

    Arguments
    ---------
    narrative_timesteps : list
        All defined narrative years
    base_yr : int
        Base year
    crit_switch_happening : dict
        Wheter switch is defined or not per enduse
    technologies : dict
        technologies
    enduse : str
        enduse
    fuel_switches : dict
        fuel switches
    s_tech_by_p : dict
        Service share per technology in base year
    s_fueltype_by_p : dict
        Service share per fueltype for base year
    share_s_tech_ey_p : dict
        Service share per technology for end year
    fuel_tech_p_by : dict
        Fuel share per technology in base year
    regions : list
        Regions
    sector : str
        Sector
    crit_all_the_same : bool,default=True
        Criteria wheter regional specific parameter calculations

    Returns
    -------
    sig_param_tech : dict
        Sigmoid parameters for all affected technologies
    service_switches_out : list
        Service switches
    """
    sig_param_tech = {}
    if sum(s_tech_by_p.values()) == 0:  # no fuel is defined for enduse
        #logging.info("no fuel is defined for enduse `{}` and sector `{}`".format(enduse, sector))
        pass
    else:
        if share_s_tech_ey_p == {}:
            pass
        else:
            if sector not in list(
                    share_s_tech_ey_p.keys()) or enduse not in list(
                        share_s_tech_ey_p[sector].keys()):
                pass  # Nothing defined for this sector or enduse
            else:
                share_s_tech_ey_p = share_s_tech_ey_p[sector][enduse]

        # ----------------------------------------
        # Test if fuel switch is defined for enduse
        # Get affected technologies in fuel switch
        # ----------------------------------------
        _, crit_fuel_switch = s_generate_sigmoid.get_tech_installed(
            enduse, fuel_switches)

        # Test if switch is defined
        crit_switch_service = fuel_service_switch.get_switch_criteria(
            enduse, sector, crit_switch_happening)

        if not crit_switch_service and not crit_fuel_switch:
            pass  # no switches defined
        elif crit_switch_service and crit_fuel_switch:
            raise Exception(
                "Error: a fuel and service switch is defined at the same time %s",
                enduse)
        else:
            any_region = regions[0]  # Only calculate for one reg
            switch_yrs = narrative_timesteps[enduse]  # Years of narrative

            # Iterate over years defined in narrative
            for switch_yr_cnt, switch_yr in enumerate(switch_yrs):
                sig_param_tech[switch_yr] = {}

                # -------------------------
                # Get starting year of narrative
                # -------------------------
                if switch_yr_cnt == 0:
                    switch_yr_start = base_yr
                    s_tech_last_narrative_step = s_tech_by_p

                else:  # If more than one switch_yr, then take previous year
                    switch_yr_start = switch_yrs[switch_yr_cnt - 1]
                    s_tech_last_narrative_step = s_tech_switched_p[any_region][
                        switch_yr_start]

                # ------------------------------------------
                # Service switch
                #
                # Calculate service shares considering service
                # switches and the diffusion parameters
                # ------------------------------------------
                if crit_switch_service:

                    # Calculate only from service switch
                    s_tech_switched_p = share_s_tech_ey_p

                    # Calculate sigmoid diffusion parameters
                    l_values_sig = s_generate_sigmoid.get_l_values(
                        technologies=technologies,
                        technologies_to_consider=s_tech_last_narrative_step.
                        keys(),
                        regions=regions)
                elif crit_fuel_switch:
                    s_tech_switched_p = defaultdict(dict)
                    l_values_sig = {}
                else:
                    s_tech_switched_p = defaultdict(dict)

                # ------------------------------------------
                # Fuel switch
                # ------------------------------------------
                if crit_fuel_switch:
                    """Calculate future service share after fuel switches
                    and calculte sigmoid diffusion paramters."""
                    # Get fuel switches of enduse and switch_yr
                    enduse_fuel_switches = fuel_service_switch.get_switches_of_enduse(
                        fuel_switches, enduse, switch_yr, crit_region=False)

                    if crit_all_the_same:
                        logging.debug(
                            "... calculating fuel switches (not regional specific): {}"
                            .format(enduse))
                        # Calculate service demand after fuel switches for each technology
                        s_tech_switched_p_values_all_regs = s_generate_sigmoid.calc_service_fuel_switched(
                            enduse_fuel_switches, technologies,
                            s_fueltype_by_p, s_tech_last_narrative_step,
                            fuel_tech_p_by, 'actual_switch')

                        # Calculate L for every technology for sigmod diffusion
                        l_values_all_regs = s_generate_sigmoid.tech_l_sigmoid(
                            s_tech_switched_p_values_all_regs,
                            enduse_fuel_switches, technologies,
                            s_tech_last_narrative_step.keys(), s_fueltype_by_p,
                            s_tech_last_narrative_step, fuel_tech_p_by)

                        for region in regions:
                            s_tech_switched_p[region][
                                switch_yr] = s_tech_switched_p_values_all_regs
                            l_values_sig[region] = l_values_all_regs

                        s_tech_switched_p = dict(s_tech_switched_p)
                    else:
                        logging.info(
                            "... calculating fuel switches (regional specific): {}"
                            .format(enduse))
                        for region in regions:

                            # Calculate service demand after fuel switches for each technology
                            s_tech_switched_p[region][
                                switch_yr] = s_generate_sigmoid.calc_service_fuel_switched(
                                    enduse_fuel_switches, technologies,
                                    s_fueltype_by_p,
                                    s_tech_last_narrative_step, fuel_tech_p_by,
                                    'actual_switch')

                            # Calculate L for every technology for sigmod diffusion
                            l_values_sig[
                                region] = s_generate_sigmoid.tech_l_sigmoid(
                                    s_tech_switched_p[region][switch_yr],
                                    enduse_fuel_switches, technologies,
                                    s_tech_last_narrative_step.keys(),
                                    s_fueltype_by_p,
                                    s_tech_last_narrative_step, fuel_tech_p_by)

                        s_tech_switched_p = dict(s_tech_switched_p)

                # -----------------------------------------------
                # Calculates parameters for sigmoid diffusion of
                # technologies which are switched to/installed.
                # -----------------------------------------------
                #logging.debug("---------- switches %s %s %s", enduse, crit_switch_service, crit_fuel_switch)
                if crit_all_the_same:
                    print(
                        "... calc parameters of `{}` for year `{}`  {}".format(
                            enduse, switch_yr, sector))
                    sig_param_tech_all_regs_value = s_generate_sigmoid.tech_sigmoid_parameters(
                        switch_yr, switch_yr_start, technologies,
                        l_values_sig[any_region], s_tech_last_narrative_step,
                        s_tech_switched_p[any_region][switch_yr])

                    for region in regions:
                        sig_param_tech[switch_yr][
                            region] = sig_param_tech_all_regs_value
                else:
                    logging.info(
                        "... calc region specific parameters of `{}` for year `{}` sector: {}"
                        .format(enduse, switch_yr, sector))
                    for region in regions:
                        sig_param_tech[switch_yr][
                            region] = s_generate_sigmoid.tech_sigmoid_parameters(
                                switch_yr, switch_yr_start, technologies,
                                l_values_sig[region], s_tech_by_p,
                                s_tech_switched_p[region][switch_yr])

    return sig_param_tech
Пример #3
0
def sig_param_calc_incl_fuel_switch(base_yr,
                                    crit_switch_happening,
                                    technologies,
                                    enduse,
                                    fuel_switches,
                                    service_switches,
                                    s_tech_by_p,
                                    s_fueltype_by_p,
                                    share_s_tech_ey_p,
                                    fuel_tech_p_by,
                                    regions=False,
                                    sector=False,
                                    regional_specific=False):
    """Calculate sigmoid diffusion paramaters considering
    fuel or service switches. Test if service switch and
    fuel switch are defined simultaneously (raise error if true).

    Arguments
    ---------
    base_yr : int
        Base year
    technologies : dict
        technologies
    enduse : str
        enduse
    fuel_switches : dict
        fuel switches
    service_switches : dict
        service switches
    s_tech_by_p : dict
        Service share per technology in base year
    s_fueltype_by_p : dict
        Service share per fueltype for base year
    share_s_tech_ey_p : dict
        Service share per technology for end year
    fuel_tech_p_by : dict
        Fuel share per technology in base year
    regions : dict
        Regions
    regional_specific : bool, default=False
        criteria

    Returns
    -------
    sig_param_tech : dict
        Sigmoid parameters for all affected technologies
    service_switches_out : list
        Service switches
    """
    # ----------------------------------------
    # Test if fuel switch is defined for enduse
    # Get affected technologies in fuel switch
    # ----------------------------------------
    tech_switch_affected = s_generate_sigmoid.get_tech_installed(
        enduse, fuel_switches)

    if len(tech_switch_affected) > 0:
        crit_fuel_switch = True
    else:
        crit_fuel_switch = False

    # ------------------------------------------
    # Test if service swich is defined for enduse
    # ------------------------------------------
    service_switches_enduse = fuel_service_switch.get_fuel_switches_enduse(
        service_switches, enduse, regional_specific)

    # ------------------------------------------
    # Initialisations
    # ------------------------------------------
    sig_param_tech = {}
    service_switches_out = {}
    if regional_specific:
        for region in regions:
            sig_param_tech[region] = []
            service_switches_out[region] = service_switches_enduse[region]
    else:
        service_switches_out = service_switches_enduse

    # Test if swithc is defined
    crit_switch_service = fuel_service_switch.get_switch_criteria(
        enduse, sector, crit_switch_happening)

    # ------------------------------------------
    # SERVICE switch
    #
    # Calculate service shares considering service
    # switches and the diffusion parameters
    # ------------------------------------------
    if crit_switch_service:

        # Calculate only from service switch
        s_tech_switched_p = share_s_tech_ey_p

        all_techs = s_tech_by_p.keys()

        # Calculate sigmoid diffusion parameters
        l_values_sig = s_generate_sigmoid.get_l_values(
            technologies,
            all_techs,
            regions=regions,
            regional_specific=regional_specific)

    # ------------------------------------------
    # FUEL switch
    # ------------------------------------------
    if crit_fuel_switch:
        """
        Calculate future service share after fuel switches
        and calculte sigmoid diffusion paramters.
        """
        # Get fuel switches of enduse
        enduse_fuel_switches = fuel_service_switch.get_fuel_switches_enduse(
            fuel_switches, enduse)

        if regional_specific:
            l_values_sig = {}
            s_tech_switched_p = {}

            for reg in regions:

                # Calculate service demand after fuel switches for each technology
                s_tech_switched_p[
                    reg] = s_generate_sigmoid.calc_service_fuel_switched(
                        enduse_fuel_switches, technologies, s_fueltype_by_p,
                        s_tech_by_p, fuel_tech_p_by, 'actual_switch')

                # Calculate L for every technology for sigmod diffusion
                l_values_sig[reg] = s_generate_sigmoid.tech_l_sigmoid(
                    s_tech_switched_p[reg], enduse_fuel_switches, technologies,
                    s_tech_by_p.keys(), s_fueltype_by_p, s_tech_by_p,
                    fuel_tech_p_by)
        else:
            # Calculate future service demand after fuel switches for each technology
            s_tech_switched_p = s_generate_sigmoid.calc_service_fuel_switched(
                enduse_fuel_switches, technologies, s_fueltype_by_p,
                s_tech_by_p, fuel_tech_p_by, 'actual_switch')

            # Calculate L for every technology for sigmod diffusion
            l_values_sig = s_generate_sigmoid.tech_l_sigmoid(
                s_tech_switched_p, enduse_fuel_switches, technologies,
                s_tech_by_p.keys(), s_fueltype_by_p, s_tech_by_p,
                fuel_tech_p_by)

        # Get year of switches
        for fuelswitch in enduse_fuel_switches:
            yr_until_switched = fuelswitch.switch_yr
            break

        # Convert serivce shares to service switches
        service_switches_out = convert_sharesdict_to_service_switches(
            yr_until_switched=yr_until_switched,
            enduse=enduse,
            s_tech_switched_p=s_tech_switched_p,
            regions=regions,
            regional_specific=regional_specific)

        # Calculate only from fuel switch
        share_s_tech_ey_p = fuel_service_switch.switches_to_dict(
            service_switches_out, regional_specific)

    if crit_switch_service or crit_fuel_switch:
        logging.info("---------- switches %s %s %s", enduse,
                     crit_switch_service, crit_fuel_switch)

        # Calculates parameters for sigmoid diffusion of
        # technologies which are switched to/installed. With
        # `regional_specific` the assumption can be changed that
        # the technology diffusion is the same over all the uk
        sig_param_tech = {}

        if regional_specific:

            # Get year of switches
            for region in regions:
                for switch in service_switches_out[region]:

                    if switch.enduse == enduse:
                        yr_until_switched = switch.switch_yr
                        break
                break

            for reg in regions:
                logging.info("calculating sigmoid parameters %s %s", enduse,
                             reg)
                sig_param_tech[
                    reg] = s_generate_sigmoid.tech_sigmoid_parameters(
                        yr_until_switched, base_yr, technologies,
                        l_values_sig[reg], s_tech_by_p, s_tech_switched_p[reg])
        else:

            # Get year of switches
            for switch in service_switches_out:
                yr_until_switched = switch.switch_yr
                break

            # Calclulate sigmoid parameters for every installed technology
            sig_param_tech = s_generate_sigmoid.tech_sigmoid_parameters(
                yr_until_switched, base_yr, technologies, l_values_sig,
                s_tech_by_p, s_tech_switched_p)
        logging.info("... moving on")
    else:
        pass  #no switches are defined

    return sig_param_tech  #, service_switches_out