def map_dual(c, attr):
     # If c is a pypsa component name the dual is store at n.pnl(c)
     # or n.df(c). For the second case the index of the constraints have to
     # be a subset of n.df(c).index otherwise the dual is stored at
     # n.duals[c].df
     constraints = get_con(n, c, attr, pop=pop)
     is_pnl = isinstance(constraints, pd.DataFrame)
     sign = 1 if 'upper' in attr or attr == 'marginal_price' else -1
     n.dualvalues.at[(c, attr), 'pnl'] = is_pnl
     to_component = c in n.all_components
     if is_pnl:
         n.dualvalues.at[(c, attr), 'in_comp'] = to_component
         # changed for netzbooster
         duals = constraints.apply(lambda x: x.map(sign * constraints_dual))
         if c not in n.duals and not to_component:
             n.duals[c] = Dict(df=pd.DataFrame(), pnl={})
         pnl = n.pnl(c) if to_component else n.duals[c].pnl
         set_from_frame(pnl, attr, duals)
     else:
         # here to_component can change
         duals = constraints.map(sign * constraints_dual)
         if to_component:
             to_component = (duals.index.isin(n.df(c).index).all())
         n.dualvalues.at[(c, attr), 'in_comp'] = to_component
         if c not in n.duals and not to_component:
             n.duals[c] = Dict(df=pd.DataFrame(), pnl={})
         df = n.df(c) if to_component else n.duals[c].df
         df[attr] = duals
 def map_solution(c, attr):
     variables = get_var(n, c, attr, pop=pop)
     predefined = True
     if (c, attr) not in lookup.index:
         predefined = False
         n.sols[c] = n.sols[c] if c in n.sols else Dict(df=pd.DataFrame(), pnl={})
     n.solutions.at[(c, attr), 'in_comp'] = predefined
     if isinstance(variables, pd.DataFrame):
         # case that variables are timedependent
         n.solutions.at[(c, attr), 'pnl'] = True
         pnl = n.pnl(c) if predefined else n.sols[c].pnl
         values = variables.apply(lambda x: x.map(variables_sol))
         # values = variables.stack().map(variables_sol).unstack()
         if c in n.passive_branch_components and attr == "s":
             set_from_frame(pnl, 'p0', values)
             set_from_frame(pnl, 'p1', - values)
         elif c == 'Link' and attr == "p":
             set_from_frame(pnl, 'p0', values)
             for i in ['1'] + additional_linkports(n):
                 i_eff = '' if i == '1' else i
                 eff = get_as_dense(n, 'Link', f'efficiency{i_eff}', sns)
                 set_from_frame(pnl, f'p{i}', - values * eff)
                 pnl[f'p{i}'].loc[sns, n.links.index[n.links[f'bus{i}'] == ""]] = \
                     n.component_attrs['Link'].loc[f'p{i}','default']
         else:
             set_from_frame(pnl, attr, values)
     else:
         # case that variables are static
         n.solutions.at[(c, attr), 'pnl'] = False
         sol = variables.map(variables_sol)
         if predefined:
             non_ext = n.df(c)[attr]
             n.df(c)[attr + '_opt'] = sol.reindex(non_ext.index).fillna(non_ext)
         else:
             n.sols[c].df[attr] = sol
import xarray as xr
import numpy as np

if __name__ == '__main__':
    if 'snakemake' not in globals():
        from helper import mock_snakemake
        snakemake = mock_snakemake(
            'build_solar_thermal_profiles',
            simpl='',
            clusters=48,
        )

    if 'snakemake' not in globals():
        from vresutils import Dict
        import yaml
        snakemake = Dict()
        with open('config.yaml') as f:
            snakemake.config = yaml.safe_load(f)
        snakemake.input = Dict()
        snakemake.output = Dict()

    config = snakemake.config['solar_thermal']

    time = pd.date_range(freq='h', **snakemake.config['snapshots'])
    cutout_config = snakemake.config['atlite']['cutout']
    cutout = atlite.Cutout(cutout_config).sel(time=time)

    clustered_regions = gpd.read_file(
        snakemake.input.regions_onshore).set_index('name').buffer(0).squeeze()

    I = cutout.indicatormatrix(clustered_regions)
Beispiel #4
0
               fontsize=18,
               loc=(0.01, 0.01),
               facecolor='white',
               frameon=True)

    path_cb_plot = snakemake.config['results_dir'] + snakemake.config[
        'run'] + '/graphs/'
    plt.savefig(path_cb_plot + 'carbon_budget_plot.pdf', dpi=300)


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        import yaml
        snakemake = Dict()
        with open('config.yaml', encoding='utf8') as f:
            snakemake.config = yaml.safe_load(f)
        snakemake.input = Dict()
        snakemake.output = Dict()
        snakemake.wildcards = Dict()
        #snakemake.wildcards['sector_opts']='3H-T-H-B-I-solar3-dist1-cb48be3'

        for item in ["costs", "energy"]:
            snakemake.input[item] = snakemake.config[
                'summary_dir'] + '/{name}/csvs/{item}.csv'.format(
                    name=snakemake.config['run'], item=item)
            snakemake.output[item] = snakemake.config[
                'summary_dir'] + '/{name}/graphs/{item}.pdf'.format(
                    name=snakemake.config['run'], item=item)
        snakemake.input["balances"] = snakemake.config[
Beispiel #5
0
    print(missing)

    transport_data.loc[missing, "average fuel efficiency"] = transport_data[
        "average fuel efficiency"].mean()

    transport_data.to_csv(snakemake.output.transport_name)

    return transport_data


if __name__ == "__main__":

    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        snakemake = Dict()
        snakemake.output = Dict(energy_name="data/energy_totals.csv")
        snakemake.output = Dict(co2_name="data/co2_totals.csv")
        snakemake.output = Dict(transport_name="data/transport_data.csv")

    year = 2011

    eurostat = build_eurostat(year)

    swiss_df = build_swiss()

    odyssee = build_odyssee()

    build_energy_totals(year)

    swiss_co2 = build_swiss_co2()
    #pd.Series nuts3 code -> polygon
    nuts3 = pd.Series(vshapes.nuts3(tolerance=None, minarea=0.))

    #takes 10 minutes
    pop_map = pd.DataFrame()

    for country in countries:
        print(country)
        country_nuts = mapping.index[mapping == country]
        trans_matrix = vtransfer.Shapes2Shapes(np.asarray(nuts3[country_nuts]),
                                               grid_cells)
        #CH has missing bits
        country_pop = pop[country_nuts].fillna(0.)
        pop_map[country] = np.array(
            trans_matrix.multiply(np.asarray(country_pop)).sum(axis=1))[:, 0]

    with pd.HDFStore(snakemake.output.pop_map_name, mode='w',
                     complevel=4) as store:
        store['population_gridcell_map'] = pop_map


if __name__ == "__main__":

    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        snakemake = Dict()
        snakemake.output = Dict(pop_map_name='data/population_gridcell_map.h5')

    build_population_map()
def assign_solution_netzbooster(n,
                                sns,
                                variables_sol,
                                constraints_dual,
                                keep_references=False,
                                keep_shadowprices=None):
    """
    Helper function. Assigns the solution of a succesful optimization to the
    network.

    """
    def set_from_frame(pnl, attr, df):
        if attr not in pnl:  #use this for subnetworks_t
            pnl[attr] = df.reindex(n.snapshots)
        elif pnl[attr].empty:
            pnl[attr] = df.reindex(n.snapshots)
        else:
            pnl[attr].loc[sns, :] = df.reindex(columns=pnl[attr].columns)

    pop = not keep_references

    def map_solution(c, attr):
        variables = get_var(n, c, attr, pop=pop)
        predefined = True
        if (c, attr) not in lookup.index:
            predefined = False
            n.sols[c] = n.sols[c] if c in n.sols else Dict(df=pd.DataFrame(),
                                                           pnl={})
        n.solutions.at[(c, attr), 'in_comp'] = predefined
        if isinstance(variables, pd.DataFrame):
            # case that variables are timedependent
            n.solutions.at[(c, attr), 'pnl'] = True
            pnl = n.pnl(c) if predefined else n.sols[c].pnl
            values = variables.apply(lambda x: x.map(variables_sol))
            # values = variables.stack().map(variables_sol).unstack()
            if c in n.passive_branch_components and attr == "s":
                set_from_frame(pnl, 'p0', values)
                set_from_frame(pnl, 'p1', -values)
            elif c == 'Link' and attr == "p":
                set_from_frame(pnl, 'p0', values)
                for i in ['1'] + additional_linkports(n):
                    i_eff = '' if i == '1' else i
                    eff = get_as_dense(n, 'Link', f'efficiency{i_eff}', sns)
                    set_from_frame(pnl, f'p{i}', -values * eff)
                    pnl[f'p{i}'].loc[sns, n.links.index[n.links[f'bus{i}'] == ""]] = \
                        n.component_attrs['Link'].loc[f'p{i}','default']
            else:
                set_from_frame(pnl, attr, values)
        else:
            # case that variables are static
            n.solutions.at[(c, attr), 'pnl'] = False
            sol = variables.map(variables_sol)
            if predefined:
                non_ext = n.df(c)[attr]
                n.df(c)[attr + '_opt'] = sol.reindex(
                    non_ext.index).fillna(non_ext)
            else:
                n.sols[c].df[attr] = sol

    n.sols = Dict()
    n.solutions = pd.DataFrame(index=n.variables.index,
                               columns=['in_comp', 'pnl'])
    for c, attr in n.variables.index:
        map_solution(c, attr)

    # if nominal capcity was no variable set optimal value to nominal
    for c, attr in lookup.query('nominal').index.difference(n.variables.index):
        n.df(c)[attr + '_opt'] = n.df(c)[attr]

    # recalculate storageunit net dispatch
    if not n.df('StorageUnit').empty:
        c = 'StorageUnit'
        n.pnl(c)['p'] = n.pnl(c)['p_dispatch'] - n.pnl(c)['p_store']

    # duals
    if keep_shadowprices == False:
        keep_shadowprices = []

    sp = n.constraints.index
    if isinstance(keep_shadowprices, list):
        sp = sp[sp.isin(keep_shadowprices, level=0)]

    def map_dual(c, attr):
        # If c is a pypsa component name the dual is store at n.pnl(c)
        # or n.df(c). For the second case the index of the constraints have to
        # be a subset of n.df(c).index otherwise the dual is stored at
        # n.duals[c].df
        constraints = get_con(n, c, attr, pop=pop)
        is_pnl = isinstance(constraints, pd.DataFrame)
        sign = 1 if 'upper' in attr or attr == 'marginal_price' else -1
        n.dualvalues.at[(c, attr), 'pnl'] = is_pnl
        to_component = c in n.all_components
        if is_pnl:
            n.dualvalues.at[(c, attr), 'in_comp'] = to_component
            # changed for netzbooster
            duals = constraints.apply(lambda x: x.map(sign * constraints_dual))
            if c not in n.duals and not to_component:
                n.duals[c] = Dict(df=pd.DataFrame(), pnl={})
            pnl = n.pnl(c) if to_component else n.duals[c].pnl
            set_from_frame(pnl, attr, duals)
        else:
            # here to_component can change
            duals = constraints.map(sign * constraints_dual)
            if to_component:
                to_component = (duals.index.isin(n.df(c).index).all())
            n.dualvalues.at[(c, attr), 'in_comp'] = to_component
            if c not in n.duals and not to_component:
                n.duals[c] = Dict(df=pd.DataFrame(), pnl={})
            df = n.df(c) if to_component else n.duals[c].df
            df[attr] = duals

    n.duals = Dict()
    n.dualvalues = pd.DataFrame(index=sp, columns=['in_comp', 'pnl'])
    # extract shadow prices attached to components
    for c, attr in sp:
        map_dual(c, attr)

    #correct prices for snapshot weightings
    n.buses_t.marginal_price.loc[sns] = n.buses_t.marginal_price.loc[
        sns].divide(n.snapshot_weightings.loc[sns], axis=0)

    # discard remaining if wanted
    if not keep_references:
        for c, attr in n.constraints.index.difference(sp):
            get_con(n, c, attr, pop)

    #load
    if len(n.loads):
        set_from_frame(n.pnl('Load'), 'p',
                       get_as_dense(n, 'Load', 'p_set', sns))

    #clean up vars and cons
    for c in list(n.vars):
        if n.vars[c].df.empty and n.vars[c].pnl == {}: n.vars.pop(c)
    for c in list(n.cons):
        if n.cons[c].df.empty and n.cons[c].pnl == {}: n.cons.pop(c)

    # recalculate injection
    ca = [('Generator', 'p', 'bus'), ('Store', 'p', 'bus'),
          ('Load', 'p', 'bus'), ('StorageUnit', 'p', 'bus'),
          ('Link', 'p0', 'bus0'), ('Link', 'p1', 'bus1')]
    for i in additional_linkports(n):
        ca.append(('Link', f'p{i}', f'bus{i}'))

    sign = lambda c: n.df(c).sign if 'sign' in n.df(c
                                                    ) else -1  #sign for 'Link'
    n.buses_t.p = pd.concat(
            [n.pnl(c)[attr].mul(sign(c)).rename(columns=n.df(c)[group])
             for c, attr, group in ca], axis=1).groupby(level=0, axis=1).sum()\
            .reindex(columns=n.buses.index, fill_value=0)

    def v_ang_for_(sub):
        buses_i = sub.buses_o
        if len(buses_i) == 1:
            return pd.DataFrame(0, index=sns, columns=buses_i)
        sub.calculate_B_H(skip_pre=True)
        Z = pd.DataFrame(np.linalg.pinv((sub.B).todense()), buses_i, buses_i)
        Z -= Z[sub.slack_bus]
        return n.buses_t.p.reindex(columns=buses_i) @ Z

    n.buses_t.v_ang = (pd.concat(
        [v_ang_for_(sub) for sub in n.sub_networks.obj],
        axis=1).reindex(columns=n.buses.index, fill_value=0))
from pypsa.pf import get_switchable_as_dense as get_as_dense, _as_snapshots
import pandas as pd
from numpy import inf
import numpy as np
import os
import logging
logger = logging.getLogger(__name__)
from tempfile import mkstemp
import gc

# for testing
if 'snakemake' not in globals():
    os.chdir("/home/ws/bw0928/mnt/lisa/netzbooster")
    from vresutils import Dict
    import yaml
    snakemake = Dict()
    snakemake.input = ["networks/prenetwork4.nc"]
    snakemake.output = ["networks/postnetwork4.nc"]
    with open('config.yaml', encoding='utf8') as f:
        snakemake.config = yaml.safe_load(f)

lookup = pd.read_csv('variables.csv', index_col=['component', 'variable'])


# functions ------------------------------------------------------------------
def assign_solution_netzbooster(n,
                                sns,
                                variables_sol,
                                constraints_dual,
                                keep_references=False,
                                keep_shadowprices=None):
Beispiel #9
0
    ax.set_ylabel("Power [GW]")
    fig.tight_layout()

    fig.savefig("{}{}/maps/series-{}-{}-{}-{}-{}.pdf".format(
        snakemake.config['results_dir'], snakemake.config['run'],
        snakemake.wildcards["lv"], carrier, start, stop, name),
                transparent=True)


# %%
if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        import yaml
        snakemake = Dict()
        with open('config.yaml') as f:
            snakemake.config = yaml.safe_load(f)
        snakemake.config['run'] = "retro_vs_noretro"
        snakemake.wildcards = {"lv": "1.0"}  # lv1.0, lv1.25, lvopt
        name = "elec_s_48_lv{}__Co2L0-3H-T-H-B".format(
            snakemake.wildcards["lv"])
        suffix = "_retro_tes"
        name = name + suffix
        snakemake.input = Dict()
        snakemake.output = Dict(
            map=(snakemake.config['results_dir'] + snakemake.config['run'] +
                 "/maps/{}".format(name)),
            today=(snakemake.config['results_dir'] + snakemake.config['run'] +
                   "/maps/{}.pdf".format(name)))
        snakemake.input.scenario = "lv" + snakemake.wildcards["lv"]
Beispiel #10
0
    #pd.Series nuts3 code -> 2-letter country codes
    mapping = vmapping.countries_to_nuts3()

    #Swiss fix
    pop["CH040"] = pop["CH04"]
    pop["CH070"] = pop["CH07"]

    #Separately researched for Montenegro, Albania, Bosnia, Serbia
    pop["ME000"] = 650
    pop["AL1"] = 2893
    pop["BA1"] = 3871
    pop["RS1"] = 7210

    population = 1e3*pop.groupby(mapping).sum()

    population.name = "population"

    population.to_csv(snakemake.output.csv_name)


if __name__ == "__main__":

    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        snakemake = Dict()
        snakemake.output = Dict(csv_name="data/population.csv")

    build_population()
    ax.set_xlabel("")

    ax.grid(axis="y")

    ax.legend(handles, labels, ncol=4, loc="upper left")

    fig.tight_layout()

    fig.savefig(snakemake.output.energy, transparent=True)


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils import Dict
        import yaml
        snakemake = Dict()
        with open('../config.yaml') as f:
            snakemake.config = yaml.load(f)
        snakemake.input = Dict(costs="../" + snakemake.config['results_dir'] +
                               'version-{version}/costs.csv'.format(
                                   version=snakemake.config['version']))
        snakemake.output = Dict(costs="../" + snakemake.config['results_dir'] +
                                'version-{version}/graphs/costs.pdf'.format(
                                    version=snakemake.config['version']))

    plot_costs()

    plot_energy()
Beispiel #12
0
if 'snakemake' not in globals():
    from vresutils import Dict
    from snakemake.rules import expand
    import yaml
    snakemake = Dict()
    snakemake.wildcards = Dict(  #cost=#'IRP2016-Apr2016',
        cost='csir-aggressive',
        mask='redz',
        sectors='E',
        opts='Co2L',
        attr='p_nom')
    snakemake.input = Dict(
        network=
        '../results/version-0.5/networks/{cost}_{mask}_{sectors}_{opts}.nc'.
        format(**snakemake.wildcards),
        supply_regions='../data/supply_regions/supply_regions.shp',
        resarea="../data/bundle/REDZ_DEA_Unpublished_Draft_2015")
    snakemake.output = (expand(
        '../results/plots/network_{cost}_{mask}_{sectors}_{opts}_{attr}.pdf',
        **snakemake.wildcards
    ) + expand(
        '../results/plots/network_{cost}_{mask}_{sectors}_{opts}_{attr}_ext.pdf',
        **snakemake.wildcards))
    snakemake.params = Dict(ext=['png'])
    with open('../config.yaml') as f:
        snakemake.config = yaml.load(f)
else:
    import matplotlib as mpl
    mpl.use('Agg')

from add_electricity import add_emission_prices