Beispiel #1
0
def run(sc_file):
    print()
    bd = os.path.dirname(sc_file)
    sc = scenario_factory.Scenario()
    sc.load_JSON(sc_file)
    print(sc.title)

    plot_samples_carpet(sc, bd)
    plt.show()
Beispiel #2
0
def run(sc_file):
    print()
    bd = os.path.dirname(sc_file)
    sc = scenario_factory.Scenario()
    sc.load_JSON(sc_file)
    print(sc.title)

    sample_data = np.load(p(bd, sc.run_pre_samplesfile))[:, :96]
    unctrl = np.load(p(bd, sc.run_unctrl_datafile))[:, 0, :96]

    fig, ax = plt.subplots(len(sample_data))
    if len(sample_data) == 1:
        ax = [ax]
    for i, samples in enumerate(sample_data):
        t = np.arange(samples.shape[-1])
        for s in samples:
            ax[i].plot(t, s)
        ax[i].plot(np.arange(unctrl[i].shape[-1]), unctrl[i], 'k', lw=1.0)

    plt.show()
Beispiel #3
0
import sys
import os
import datetime
import pickle

import numpy as np

import scenario_factory
import simulator

sc_file = sys.argv[1]
sc = scenario_factory.Scenario()
sc.load_JSON(sc_file)
sc.run_pre_ts = datetime.datetime.now()

d = os.path.dirname(sc_file)
sam_dfn = str(os.path.join(d, '.'.join((str(sc.seed), 'samples', 'npz'))))
if os.path.exists(sam_dfn):
    raise RuntimeError('File already exists: %s' % sam_dfn)
sta_dfn = str(os.path.join(d, '.'.join((str(sc.seed), 'states', 'pickle'))))
if os.path.exists(sta_dfn):
    raise RuntimeError('File already exists: %s' % sta_dfn)
ssd_dfn = str(
    os.path.join(d, '.'.join((str(sc.seed), 'samples_sim_data', 'npz'))))
if os.path.exists(ssd_dfn):
    raise RuntimeError('File already exists: %s' % ssd_dfn)

sample_data, states_data, sample_sim_data = simulator.run_pre(sc)

np.savez(sam_dfn, **sample_data)
with open(sta_dfn, 'wb') as outfile:
Beispiel #4
0
def stats(fn):
    sc_file = fn
    bd = os.path.dirname(sc_file)
    sc = scenario_factory.Scenario()
    sc.load_JSON(sc_file)
    print(sc.title)

    unctrl = load(p(bd, sc.run_unctrl_datafile))

    block = load(p(bd, sc.run_ctrl_datafile))
    post = load(p(bd, sc.run_post_datafile))
    sched = load(p(bd, sc.sched_file))

    ctrl = np.zeros(unctrl.shape)
    idx = 0
    for l in (block, post):
        ctrl[:,:,idx:idx + l.shape[-1]] = l
        idx += l.shape[-1]

    if sched.shape[-1] == unctrl.shape[-1] / 15:
        print('Extending schedules shape by factor 15')
        sched = sched.repeat(15, axis=1)
    t_start, b_start, b_end = sc.t_start, sc.t_block_start, sc.t_block_end
    div = 1
    if (b_end - t_start).total_seconds() / 60 == sched.shape[-1] * 15:
        div = 15
    elif (b_end - t_start).total_seconds() / 60 == sched.shape[-1] * 60:
        div = 60
    b_s = (b_start - sc.t_start).total_seconds() / 60 / div
    b_e = (b_end - sc.t_start).total_seconds() / 60 / div
    ctrl_sched = np.zeros((unctrl.shape[0], unctrl.shape[-1]))
    ctrl_sched = np.ma.array(ctrl_sched)
    ctrl_sched[:,:b_s] = np.ma.masked
    ctrl_sched[:,b_s:b_e] = sched[:,b_s:b_e]
    ctrl_sched[:,b_e:] = np.ma.masked

    # plot_each_device(sc, unctrl, ctrl, sched)
    minutes = (sc.t_end - sc.t_start).total_seconds() / 60
    assert unctrl.shape[-1] == ctrl.shape[-1] == ctrl_sched.shape[-1]
    shape = unctrl.shape[-1]
    if minutes == shape:
        print('data is 1-minute resolution, will be resampled by 15')
        res = 15
    elif minutes == shape * 15:
        print('data is 15-minute resolution, all fine')
        res = 1
    else:
        raise RuntimeError('unsupported data resolution: %.2f' % (minutes / shape))
    unctrl = resample(unctrl, res)
    ctrl = resample(ctrl, res)
    ctrl_sched = resample(ctrl_sched, res)

    # code above is from analyze
    ###########################################################################
    # code below calculates stats

    t_day_start = sc.t_block_start - timedelta(hours=sc.t_block_start.hour,
                                         minutes=sc.t_block_start.minute)
    skip = (t_day_start - sc.t_start).total_seconds() / 60 / 15
    i_block_start = (sc.t_block_start - t_day_start).total_seconds() / 60 / 15
    i_block_end = (sc.t_block_end - t_day_start).total_seconds() / 60 / 15

    P_el_unctrl = unctrl[:,0,skip + i_block_start:skip + i_block_end].sum(0)
    P_el_ctrl = ctrl[:,0,skip + i_block_start:skip + i_block_end].sum(0)
    P_el_sched = ctrl_sched[:,skip + i_block_start:skip + i_block_end].sum(0)

    slp = _read_slp(sc, bd)[skip + i_block_start:skip + i_block_end]

    # Stats
    pairs = [
        # (P_el_sched, P_el_ctrl, 'P_el_sched', 'P_el_ctrl'),
        (P_el_sched, P_el_unctrl, 'P_el_sched', 'P_el_unctrl'),

        (P_el_unctrl, P_el_ctrl, 'P_el_unctrl', 'P_el_ctrl'),
    ]
    st = [sc.title]
    for target, data, tname, dname in pairs:
        diff = obj(target, data)
        perf = max(0, 1 - _f(target, data))
        perf_abs = perf * 100.0
        # if perf_abs > 100.0:
        #     perf_abs = 100 - min(100, max(0, perf_abs - 100))
        print('obj(%s, %s) = %.2f kW (%.2f %%)' % (tname, dname, diff, perf_abs))
        st.append(perf_abs)

    # # SLP
    # diff_ctrl = (P_el_ctrl - P_el_unctrl) / 1000.0
    # slp_ctrl = slp + diff_ctrl
    # slp_range = abs(slp.max() - slp.min())
    # slp_range_ctrl = abs(slp_ctrl.max() - slp_ctrl.min())
    # reduction = (1 - norm(0, slp_range, slp_range_ctrl)) * 100
    # print('slp range = %.2f kW' % slp_range)
    # print('slp range (ctrl) = %.2f kW' % slp_range_ctrl)
    # print('reduction = %.2f %%' % reduction)
    # st.append(reduction)

    # SLP (only schedule)
    diff_sched = (P_el_sched - P_el_unctrl) / 1000.0
    slp_sched = slp + diff_sched
    slp_range = abs(slp.max() - slp.min())
    slp_range_sched = abs(slp_sched.max() - slp_sched.min())
    reduction = (1 - norm(0, slp_range, slp_range_sched)) * 100
    print('slp range = %.2f kW' % slp_range)
    print('slp range (sched) = %.2f kW' % slp_range_sched)
    print('reduction = %.2f %%' % reduction)
    st.append(reduction)


    print()
    return st
Beispiel #5
0
def run(sc_file):
    print()
    bd = os.path.dirname(sc_file)
    sc = scenario_factory.Scenario()
    sc.load_JSON(sc_file)
    print(sc.title)

    # plot_samples(sc, bd)
    # plt.show()

    unctrl = np.load(p(bd, sc.run_unctrl_datafile))
    pre = np.load(p(bd, sc.run_pre_datafile))
    block = np.load(p(bd, sc.run_ctrl_datafile))
    post = np.load(p(bd, sc.run_post_datafile))
    sched = np.load(p(bd, sc.sched_file))

    ctrl = np.zeros(unctrl.shape)
    idx = 0
    for l in (pre, block, post):
        ctrl[:, :, idx:idx + l.shape[-1]] = l
        idx += l.shape[-1]

    if sched.shape[-1] == unctrl.shape[-1] / 15:
        print('Extending schedules shape by factor 15')
        sched = sched.repeat(15, axis=1)
    ctrl_sched = np.zeros((unctrl.shape[0], unctrl.shape[-1]))
    ctrl_sched = np.ma.array(ctrl_sched)
    ctrl_sched[:, :pre.shape[-1]] = np.ma.masked
    ctrl_sched[:, pre.shape[-1]:pre.shape[-1] + sched.shape[-1]] = sched
    ctrl_sched[:, pre.shape[-1] + sched.shape[-1]:] = np.ma.masked

    # plot_each_device(sc, unctrl, ctrl, sched)
    minutes = (sc.t_end - sc.t_start).total_seconds() / 60
    assert unctrl.shape[-1] == ctrl.shape[-1] == ctrl_sched.shape[-1]
    shape = unctrl.shape[-1]
    if hasattr(sc, 'slp_file'):
        if minutes == shape:
            print('data is 1-minute resolution, will be resampled by 15')
            res = 15
        elif minutes == shape * 15:
            print('data is 15-minute resolution, all fine')
            res = 1
        else:
            raise RuntimeError('unsupported data resolution: %.2f' %
                               (minutes / shape))
        unctrl = resample(unctrl, res)
        ctrl = resample(ctrl, res)
        ctrl_sched = resample(ctrl_sched, res)
        fig = plot_aggregated_SLP(sc, bd, unctrl, ctrl, ctrl_sched, res=15)
    else:
        if minutes == shape:
            print('data is 1-minute resolution, will be resampled by 60')
            res = 60
        elif minutes == shape * 15:
            print('data is 15-minute resolution, will be resampled by 4')
            res = 4
        elif minutes == shape * 60:
            print('data is 60-minute resolution, all fine')
            res = 1
        else:
            raise RuntimeError('unsupported data resolution: %.2f' %
                               (minutes / shape))
        unctrl = resample(unctrl, res)
        ctrl = resample(ctrl, res)
        ctrl_sched = resample(ctrl_sched, res)
        fig = plot_aggregated(sc, bd, unctrl, ctrl, ctrl_sched, res=60)
    # fig.savefig(p(bd, sc.title) + '.pdf')
    # fig.savefig(p(bd, sc.title) + '.png', dpi=300)

    plt.show()
Beispiel #6
0
def stats(fn):
    sc_file = fn
    bd = os.path.dirname(sc_file)
    sc = scenario_factory.Scenario()
    sc.load_JSON(sc_file)
    print(sc.title)

    unctrl = load(p(bd, sc.run_unctrl_datafile))

    block = load(p(bd, sc.run_ctrl_datafile))
    post = load(p(bd, sc.run_post_datafile))
    sched = load(p(bd, sc.sched_file))

    ctrl = np.zeros(unctrl.shape)
    idx = 0
    for l in (block, post):
        ctrl[:, :, idx:idx + l.shape[-1]] = l
        idx += l.shape[-1]

    if sched.shape[-1] == unctrl.shape[-1] / 15:
        print('Extending schedules shape by factor 15')
        sched = sched.repeat(15, axis=1)
    t_start, b_start, b_end = sc.t_start, sc.t_block_start, sc.t_block_end
    div = 1
    if (b_end - t_start).total_seconds() / 60 == sched.shape[-1] * 15:
        div = 15
    elif (b_end - t_start).total_seconds() / 60 == sched.shape[-1] * 60:
        div = 60
    b_s = (b_start - sc.t_start).total_seconds() / 60 / div
    b_e = (b_end - sc.t_start).total_seconds() / 60 / div
    ctrl_sched = np.zeros((unctrl.shape[0], unctrl.shape[-1]))
    ctrl_sched = np.ma.array(ctrl_sched)
    ctrl_sched[:, :b_s] = np.ma.masked
    ctrl_sched[:, b_s:b_e] = sched[:, b_s:b_e]
    ctrl_sched[:, b_e:] = np.ma.masked

    # plot_each_device(sc, unctrl, ctrl, sched)
    minutes = (sc.t_end - sc.t_start).total_seconds() / 60
    assert unctrl.shape[-1] == ctrl.shape[-1] == ctrl_sched.shape[-1]
    shape = unctrl.shape[-1]
    if minutes == shape:
        print('data is 1-minute resolution, will be resampled by 60')
        res = 60
    elif minutes == shape * 15:
        print('data is 15-minute resolution, will be resampled by 4')
        res = 4
    elif minutes == shape * 60:
        print('data is 60-minute resolution, all fine')
        res = 1
    else:
        raise RuntimeError('unsupported data resolution: %.2f' %
                           (minutes / shape))
    unctrl = resample(unctrl, res)
    ctrl = resample(ctrl, res)
    ctrl_sched = resample(ctrl_sched, res)

    # code above is from analyze
    ###########################################################################
    # code below calculates stats

    print('mean load: %.2f kW' % (unctrl[:, 0, :].sum(0).mean() / 1000.0))

    t_day_start = sc.t_block_start - timedelta(hours=sc.t_block_start.hour,
                                               minutes=sc.t_block_start.minute)
    skip = (t_day_start - sc.t_start).total_seconds() / 60 / 60
    i_block_start = (sc.t_block_start - t_day_start).total_seconds() / 60 / 60
    i_block_end = (sc.t_block_end - t_day_start).total_seconds() / 60 / 60

    P_el_unctrl = unctrl[:, 0, skip:].sum(0)
    P_el_ctrl = ctrl[:, 0, skip:].sum(0)
    P_el_sched = ctrl_sched[:, skip:].sum(0)

    T_storage_ctrl = ctrl[:, 2, skip:]

    # Stats
    target = np.ma.zeros((minutes / 60 - skip, ))
    target[:i_block_start] = np.ma.masked
    target[i_block_start:i_block_end] = np.array(sc.block)
    target[i_block_end:] = np.ma.masked
    # print('target = %s' % target)
    pairs = [
        (target, P_el_sched, 'target', 'P_el_sched'),
        (target, P_el_ctrl, 'target', 'P_el_ctrl'),
        (target, P_el_unctrl, 'target', 'P_el_unctrl'),
        (P_el_sched, P_el_ctrl, 'P_el_sched', 'P_el_ctrl'),
        (P_el_sched, P_el_unctrl, 'P_el_sched', 'P_el_unctrl'),
        (P_el_unctrl, P_el_ctrl, 'P_el_unctrl', 'P_el_ctrl'),
    ]
    mask = target.mask
    st = [sc.title]
    for target, data, tname, dname in pairs:
        target = np.ma.array(target, mask=mask)
        data = np.ma.array(data, mask=mask)
        diff = obj(target, data)
        perf = max(0, 1 - _f(target, data))
        perf_abs = perf * 100.0
        # if perf_abs > 100.0:
        #     perf_abs = 100 - min(100, max(0, perf_abs - 100))
        print('obj(%s, %s) = %.2f kW (%.2f %%)' %
              (tname, dname, diff / 1000.0, perf_abs))
        st.append(perf_abs)

    # Synchronism
    syncs = []
    pairs = [
        (i_block_start, 'block_start'),
        (i_block_end, 'block_end'),
        (23, 'day_end'),
        (47, 'sim_end'),
    ]
    for timestamp, name in pairs:
        s = sync(T_storage_ctrl[:, timestamp] - 273) * 100.0
        syncs.append(s)
        print('sync(%s) = %.2f' % (name, s))

    print()
    return st, syncs