예제 #1
0
def test_scenario_es_init():
    sc = scenario_tools.DeflexScenario(name="test", year=2012, debug=True)
    es1 = sc.initialise_energy_system()
    sc = scenario_tools.DeflexScenario(name="test", year=2012)
    es2 = sc.initialise_energy_system()
    sc = scenario_tools.DeflexScenario(name="test", year=2013)
    es3 = sc.initialise_energy_system()
    eq_(len(es1.timeindex), 3)
    eq_(len(es2.timeindex), 8784)
    eq_(len(es3.timeindex), 8760)
예제 #2
0
def test_scenario_creation_main():
    sc = scenario_tools.DeflexScenario(name="test", year=2014)
    csv_path = os.path.join(os.path.dirname(__file__), "data",
                            "deflex_2014_de21_test_csv")
    sc.load_csv(csv_path)
    basic_scenario.create_scenario = MagicMock(
        return_value=sc.table_collection)
    cfg.tmp_set(
        "paths",
        "scenario",
        os.path.join(os.path.expanduser("~"), "deflex_tmp_test_dir"),
    )

    fn = basic_scenario.create_basic_scenario(2014, "de21", only_out="csv")
    ok_(fn.xls is None)
    eq_(fn.csv[-52:], "deflex_tmp_test_dir/deflex/2014/deflex_2014_de21_csv")

    fn = basic_scenario.create_basic_scenario(2014, "de21", only_out="xls")
    ok_(fn.csv is None)
    eq_(fn.xls[-52:], "deflex_tmp_test_dir/deflex/2014/deflex_2014_de21.xls")

    fn = basic_scenario.create_basic_scenario(2014,
                                              "de21",
                                              csv_dir="fancy_csv",
                                              xls_name="fancy.xls")
    eq_(fn.xls[-41:], "deflex_tmp_test_dir/deflex/2014/fancy.xls")
    eq_(fn.csv[-41:], "deflex_tmp_test_dir/deflex/2014/fancy_csv")
예제 #3
0
def test_corrupt_data():
    sc = scenario_tools.DeflexScenario(year=2014)
    csv_path = os.path.join(os.path.dirname(__file__), "data",
                            "deflex_2014_de02_test_csv")
    sc.load_csv(csv_path)
    msg = "Missing time series for solar"
    with assert_raises_regexp(ValueError, msg):
        sc.table2es()
예제 #4
0
def test_scenario_building():
    sc = scenario_tools.DeflexScenario(name="test", year=2014)
    csv_path = os.path.join(os.path.dirname(__file__), "data",
                            "deflex_2014_de21_test_csv")
    sc.load_csv(csv_path)
    sc.table2es()
    sc.check_table("volatile_series")
    sc.table_collection["volatile_series"].loc[5,
                                               ("DE01", "wind")] = float("nan")
    with assert_raises_regexp(ValueError,
                              "Nan Values in the volatile_series table"):
        sc.check_table("volatile_series")
예제 #5
0
def test_excel_reader():
    sc = scenario_tools.DeflexScenario(name="test", year=2014)
    xls_fn = os.path.join(os.path.dirname(__file__), "data",
                          "deflex_2013_de02_test.xls")
    sc.load_excel(xls_fn)
    sc.table2es()
    csv_path = os.path.join(os.path.expanduser("~"),
                            "deflex_2014_de02_nose_test_csv")
    sc.to_csv(csv_path)
    sc.to_csv(csv_path)
    xls_fn = os.path.join(os.path.expanduser("~"),
                          "deflex_2014_de02_nose_test.xls")
    sc.to_excel(xls_fn)
    shutil.rmtree(csv_path)
    os.remove(xls_fn)
예제 #6
0
def test_clean_time_series():
    sc = scenario_tools.DeflexScenario(name="test", year=2014)
    csv_path = os.path.join(os.path.dirname(__file__), "data",
                            "deflex_2014_de21_test_csv")
    sc.load_csv(csv_path)
    # before cleaning
    ok_(("DE05", "solar") in sc.table_collection["volatile_series"])
    ok_(("DE04", "district heating") in sc.table_collection["demand_series"])

    sc.table_collection["volatile_source"]["DE05", "solar"] = 0
    sc.table_collection["demand_series"]["DE04", "district heating"] = 0
    basic_scenario.clean_time_series(sc.table_collection)

    # after cleaning
    ok_(("DE05", "solar") not in sc.table_collection["volatile_series"])
    ok_(("DE04",
         "district heating") not in sc.table_collection["demand_series"])
예제 #7
0
def test_build_model_manually():
    sc = scenario_tools.DeflexScenario(name="my_test", year=2014, debug=True)
    xls_fn = os.path.join(os.path.dirname(__file__), "data",
                          "deflex_2013_de02_test.xls")
    sc.load_excel(xls_fn)
    nodes = sc.create_nodes()
    sc.add_nodes(nodes)
    dump_fn = os.path.join(os.path.expanduser("~"), "nose_test.deflex")
    sc.dump_es(dump_fn)
    sc.create_model()
    sc.solve(solver="cbc", with_duals=True)
    eq_(sc.es.results["meta"]["scenario"]["name"], "my_test")
    sc.dump_es(dump_fn)
    sc.plot_nodes()
    sc.results_fn = dump_fn
    sc.restore_es(dump_fn)
    eq_(sc.meta["scenario"]["year"], 2014)
    os.remove(dump_fn)
예제 #8
0
def test_scenario_es_init_error():
    sc = scenario_tools.DeflexScenario()
    msg = ("You cannot create an EnergySystem with self.year=2012, of type"
           " <class 'str'")
    with assert_raises_regexp(TypeError, msg):
        sc.initialise_es("2012")
예제 #9
0
def model_scenario(
    xls_file=None,
    csv_path=None,
    res_path=None,
    name="noname",
    rmap=None,
    year="unknown",
    es=None,
    plot_graph=False,
    extra_regions=None,
):
    """
    Compute a deflex scenario.

    Parameters
    ----------
    xls_file : str
        Full filename to a valid xls-file.
    csv_path : str
        Full path to a valid csv-collection.
    res_path : str
        Path to store the output file. If None the results will be stored along
        with the scenarios.
    name : str
        The name of the scenario.
    year : int
        The year or year-range of the scenario.
    rmap : str
        The name of the used region map.
    es : oemof.solph.EnergySystem
        A valid energy system if needed.
    plot_graph : bool
        Set to True to plot the energy system.
    extra_regions : list
        Use separate resource buses for these regions.

    Returns
    -------

    Examples
    --------
    >>> model_scenario('/my/path/to/scenario.xls', name='my_scenario',
    ...                rmap='deXX', year=2025)  # doctest: +SKIP
    """
    stopwatch()

    if xls_file is not None and csv_path is not None:
        raise ValueError("It is not allowed to define more than one input.")

    meta = {
        "year": year,
        "model_base": "deflex",
        "map": rmap,
        "solver": cfg.get("general", "solver"),
        "start_time": datetime.now(),
    }

    sc = scenario_tools.DeflexScenario(name=name, year=2014, meta=meta)
    if es is not None:
        sc.es = es

    if csv_path is not None:
        if res_path is None:
            res_path = os.path.dirname(csv_path)
        logging.info("Read scenario from csv collection: {0}".format(
            stopwatch()))
        sc.load_csv(csv_path)
    elif xls_file is not None:
        if res_path is None:
            res_path = os.path.dirname(xls_file)
        logging.info("Read scenario from xls-file: {0}".format(stopwatch()))
        sc.load_excel(xls_file)

    if extra_regions is not None:
        sc.extra_regions = extra_regions

    logging.info("Add nodes to the EnergySystem: {0}".format(stopwatch()))
    sc.table2es()

    # Save energySystem to '.graphml' file if plot_graph is True
    if plot_graph:
        sc.plot_nodes(
            filename=os.path.join(res_path, name),
            remove_nodes_with_substrings=["bus_cs"],
        )

    logging.info("Create the concrete model: {0}".format(stopwatch()))
    sc.create_model()

    logging.info("Solve the optimisation model: {0}".format(stopwatch()))
    sc.solve(solver=cfg.get("general", "solver"))

    logging.info("Solved. Dump results: {0}".format(stopwatch()))
    res_path = os.path.join(res_path,
                            "results_{0}".format(cfg.get("general", "solver")))
    os.makedirs(res_path, exist_ok=True)
    out_file = os.path.join(res_path, name + ".esys")
    logging.info("Dump file to {0}".format(out_file))
    sc.meta["end_time"] = datetime.now()
    sc.dump_es(out_file)

    logging.info("All done. deflex finished without errors: {0}".format(
        stopwatch()))