Commit 0dbd5e27 authored by Madhur Panwar's avatar Madhur Panwar
Browse files

storage plots removing {max(model.time) + 1}th timestep from charge-discharge...

storage plots removing {max(model.time) + 1}th timestep from charge-discharge plot, energy_balance plots made

TODO: legend distribution in energy_balance plots, markers to distinguish bw overlapping plots?, kwargs in arguments, docstrings
parent ef910585
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -387,12 +387,13 @@ def plot_storages(results: dict, to_plot: dict = None, size: tuple = (10, 5)) ->

    for storage in attributes['storages']:
        fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=size)
        fig.text(0.524, 0.02, 'Timesteps', ha='center', va='center')
        fig.text(0.524, 0.02, 'Timesteps (-)', ha='center', va='center')
        fig.text(0.01, 0.5, 'Storage state (in %)', ha='center', va='center', rotation='vertical')
        fig.text(0.5, 0.97, storage, ha='center', va='center')
        ax = axes[0]
        ax1 = axes[1]


        ser_storage_state = attributes['storage_level'][storage]
        ser_gross_charge = attributes['energy_to_storage'][storage]
        ser_gross_discharge = -attributes['energy_from_storage'][storage]
@@ -416,24 +417,30 @@ def plot_storages(results: dict, to_plot: dict = None, size: tuple = (10, 5)) ->
            df = (df.div(capacity)).mul(100)

        if to_plot['pl_state']:
            df['storage state'].plot(kind='bar', xticks=df.index, title=' ', ax=ax, color='deepskyblue', legend=True)
            df['storage state'].plot(kind='bar',  title=' ', ax=ax, color='deepskyblue', legend=True)
        if to_plot['pl_gross_ch']:
            df['charge from stream'].plot(drawstyle='steps-post', xticks=df.index, title=' ', color='lightgreen',
            df['charge from stream'].plot(drawstyle='steps-post',  title=' ', color='lightgreen',
                                          linewidth=3, ax=ax1, legend = True)
        if to_plot['pl_net_ch']:
            df['net charge'].plot(drawstyle='steps-post', xticks=df.index, title=' ', color='green',
            df['net charge'].plot(drawstyle='steps-post',  title=' ', color='green',
                                  linestyle='--', linewidth=2, ax=ax1, legend=True)
        if to_plot['pl_gross_dch']:
            df['discharge to stream'].plot(drawstyle='steps-post', xticks=df.index, title=' ', color='orange',
            df['discharge to stream'].plot(drawstyle='steps-post',  title=' ', color='orange',
                                           linewidth=3, ax=ax1, legend=True)
        if to_plot['pl_net_dch']:
            df['net discharge'].plot(drawstyle='steps-post', xticks=df.index, title=' ', color='red',
            df['net discharge'].plot(drawstyle='steps-post',  title=' ', color='red',
                                     linestyle='--', linewidth=2, ax=ax1, legend=True)
        if to_plot['pl_decay']:
            df['standing loss'].plot(kind='bar', xticks=df.index, title=' ', color='saddlebrown',
            df['standing loss'].plot(kind='bar',  title=' ', color='saddlebrown',
                                     ax=ax, legend=True)
        ax1.set_xticks(df.index[:-1])
        ax.set_xticks(df.index)


        if to_plot['pl_state'] or to_plot['pl_gross_ch'] or to_plot['pl_net_ch'] or to_plot['pl_gross_dch'] \
                or to_plot['pl_net_dch'] or to_plot['pl_decay']:
            plt.legend(loc='best')
            for ax in fig.axes:
                plt.sca(ax)
                plt.xticks(rotation=0)
            plt.show()
+74 −75
Original line number Diff line number Diff line
@@ -7,106 +7,105 @@ from pyehub.energy_hub.ehub_model import EHubModel

# sns.set()

def plot_energy_balance(model, results: dict) -> None:
    # solution_section = results['solution']
    # attributes = to_dataframes(solution_section)

def plot_energy_balance(model, results: dict, size: tuple = (8, 5), lw: float = 3, dl: float = 3 ) -> None:
    attributes = results['solution']
    streams_wo_sources = [x for x in model.streams if x not in model.sources]

    list_streams = attributes['streams'] # all the streams
    list_import_streams  = attributes['import_streams'] # streams importing energy from outside world, i.e. 'can import = 1'
    list_export_streams = attributes['export_streams'] # streams exporting energy to the outside world, i.e. 'can export = 1'
    list_LOAD_streams = attributes['demands'] # streams with LOADS

    list_storages = attributes['storages'] # list with all the storage names
    # list_dict_storage = model._data.request['storages']
    dict_storage_stream = {x['name']: x['stream'] for x in model._data._request['storages']}

    # interactions with the outside world (imported and exported energy into/out of streams)
    df_energy_imported = attributes['energy_imported'] #+ df with series' names as ones in list_import_streams,
    # e.g. df_energy_imported[list_import_streams[0]] gives the series of energy imported as 'list_import_streams[0]' stream
    df_energy_exported = attributes['energy_exported'] #- df with series' names as ones in list_export_streams,
    # e.g. df_energy_exported[list_export_streams[0]] gives the series of energy exported as 'list_export_streams[0]' stream

    # interactions with storages
    df_energy_from_storage = attributes['energy_from_storage']  # df with series' names as the ones in list_storages,
    # e.g. df_energy_from_storage[list_storage[0]] gives the energy entering in dict_storage_stream[list_storage[0]]
    # stream from list_storage[0] storage
    df_energy_to_storage = attributes['energy_to_storage']  # df with series' names as the ones in list_storages,
    # e.g. df_energy_to_storage[list_storage[0]] gives the energy leaving from dict_storage_stream[list_storage[0]]
    # stream to list_storage[0] storage

    # plot title 'stream'

    for stream in model.streams:
        stream = 'Irradiation'
        ax = plt.gca()
        # plot title 'stream'
        dict_data = {}
        for t in model.time:
            # get the load for a demand stream
    for stream in streams_wo_sources:
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=size)

        dict_data_pos = {}
        dict_data_neg = {}

        for t in model.time:
            curr_neg = curr_pos = 0
            if stream in model.demands:
                load = model.LOADS[stream][t]
                curr_neg += (-load)
                place_in_dict(t, model, dict_data, 'LOAD', curr_neg)
                # pick color (initialise from a pallete) , plot load -ve label: 'LOAD'

            if stream in model.sources:
                load = model.LOADS[stream][t]
                curr_neg+=(-load)
                place_in_dict(t, model, dict_data, 'LOAD', curr_neg)
                # pick color (initialise from a pallete) , plot load -ve label: 'LOAD'

            # storage interactions
                place_in_dict(t, model, dict_data_neg, 'LOAD', curr_neg)

            for storage in model._get_storages_from_stream(stream): # storages interacting with current 'stream'
            for storage in model._get_storages_from_stream(stream):
                q_out = float(attributes['energy_to_storage'][t][storage.name])
                curr_neg += (-q_out)
                q_in = float(attributes['energy_from_storage'][t][storage.name])
                curr_pos += q_in
                place_in_dict(t, model, dict_data, 'To '+storage.name+' (S)', curr_neg)
                place_in_dict(t, model, dict_data, 'From '+storage.name+' (S)', curr_pos)
                # next color, style as dashed
                # plot q_out -ve label: 'To 'storage''
                # plot q_in +ve label: 'From 'storage''

                place_in_dict(t, model, dict_data_neg, 'To '+storage.name+' (S)', curr_neg)
                place_in_dict(t, model, dict_data_pos, 'From '+storage.name+' (S)', curr_pos)

            # attributes with float

            for tech in model.technologies:  # _get_techs_from_output_stream(stream):
                # TODO: Account for techs which have two input streams
            for tech in model.technologies:
                conversion_rate = float(attributes['CONVERSION_EFFICIENCY'][tech][stream])
                if conversion_rate < 0:
                    energy_input = float(attributes['energy_input'][t][tech])
                    curr_neg+=(-energy_input)
                    place_in_dict(t, model, dict_data, 'To '+tech+' (C)', curr_neg)
                    # next color
                    # plot energy_input -ve label: 'To 'tech''
                # TODO: Account for CHP which has two output streams
                    place_in_dict(t, model, dict_data_neg, 'To '+tech+' (C)', curr_neg)

                if conversion_rate > 0:
                    energy_input = float(attributes['energy_input'][t][tech])
                    energy_output = energy_input * conversion_rate
                    curr_pos+=energy_output
                    place_in_dict(t, model, dict_data, 'From '+tech+' (C)', curr_pos)
                    # next color
                    # plot energy_output +ve label: 'From 'tech''
                    place_in_dict(t, model, dict_data_pos, 'From '+tech+' (C)', curr_pos)

            if stream in model.export_streams:
                energy_exported = attributes['energy_exported'][t][stream]
                curr_neg += (-energy_exported)
                place_in_dict(t, model, dict_data, 'Exported', curr_neg)
                # next color
                # plot energy_exported -ve label: 'Exported'
                place_in_dict(t, model, dict_data_neg, 'Exported', curr_neg)

            if stream in model.import_streams:
                energy_imported = attributes['energy_imported'][t][stream]
                curr_pos += energy_imported
                place_in_dict(t, model, dict_data, 'Imported', curr_pos)
                # next color
                # plot energy_imported +ve label: 'Imported'
                print()

                place_in_dict(t, model, dict_data_pos, 'Imported', curr_pos)

        x = y = 1
        # mk_index_c = 1
        # mk_index_s = 1
        for label, data in dict_data_neg.items():
            if label.endswith('LOAD'):
                ds = ()
                # mk = 's'
            if label.endswith('(C)'):
                ds = (dl, x)
                x += 1
                # mk = str(mk_index_c % 5)
                # mk_index_c += 1
            if label.endswith('(S)'):
                ds = (dl, y, 1, y)
                y += 1
                # mk = str(mk_index_s % 5)
                # mk_index_s += 1
            if label.endswith('Exported'):
                ds = (dl, 1, 1.5, 1, 1.5, 1, 1.5, 1, 1.5, 1, 1.5, 1)
            if label.endswith('Imported'):
                ds = (dl, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            plt.plot(data, label=label, axes=ax, linewidth=lw, dashes=ds)
            plt.legend(loc='best')
            ax.set(title=stream, xticks=model.time, xlabel='Timesteps', ylabel='')

        x = y = 1.2
        for label, data in dict_data_pos.items():
            if label.endswith('LOAD'):
                ds = ()
                # mk = 's'
            if label.endswith('(C)'):
                ds = (dl, x)
                x += 1
                # mk = str(mk_index_c % 5)
                # mk_index_c += 1
            if label.endswith('(S)'):
                ds = (dl, y, 1, y)
                y += 1
                # mk = str(mk_index_s % 5)
                # mk_index_s += 1
            if label.endswith('Exported'):
                ds = (dl, 1, 1.5, 1, 1.5, 1, 1.5, 1, 1.5, 1, 1.5, 1)
            if label.endswith('Imported'):
                ds = (dl, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            # plt.plot(data, label=label, axes=ax, linewidth=lw, marker=mk, dashes=ds)
            plt.plot(data, label=label, axes=ax, linewidth=lw, dashes=ds)
            plt.legend(loc='best')
            ax.set(title=stream, xticks=model.time, xlabel='Timesteps (-)', ylabel=stream+' (kWh)')

        plt.show()

def place_in_dict(t, model, dictionary: dict, key: str, value):
    """append a value in the list corresponding to key if key exists in dictionary.
+13 B (29.7 KiB)

File changed.

No diff preview for this file type.