def setup_class(cls): path = os.path.join(TEST_PATH, "de21_no-heat_csv") sc = st.DeflexScenario() sc.read_csv(path) cls.tables = sc.input_data tmp_tables = {} name = "heat_demand_deflex" fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv") tmp_tables[name] = pd.read_csv(fn, index_col=[0], header=[0, 1]) name = "transformer_balance" fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv") tmp_tables[name] = pd.read_csv(fn, index_col=[0, 1, 2], header=[0]) powerplants.scenario_powerplants = MagicMock( return_value={ "volatile plants": cls.tables["volatile plants"], "power plants": cls.tables["power plants"], }) feedin.scenario_feedin = MagicMock( return_value=cls.tables["volatile series"]) demand_table = { "electricity demand series": cls.tables["electricity demand series"] } demand.scenario_demand = MagicMock(return_value=demand_table) my_parameter = { "copperplate": True, "group_transformer": False, "heat": False, "use_variable_costs": True, "use_CO2_costs": True, "map": "de21", } my_name = "deflex" for k, v in my_parameter.items(): my_name += "_" + str(k) + "-" + str(v) polygons = deflex_regions(rmap=my_parameter["map"], rtype="polygons") lines = deflex_power_lines(my_parameter["map"]).index base = os.path.join(os.path.expanduser("~"), ".tmp_x345234dE_deflex") os.makedirs(base, exist_ok=True) path = os.path.join(base, "deflex_test{0}") name = "deflex_2014_de21_no-heat" scenario_creator.create_basic_reegis_scenario( name=name, regions=polygons, lines=lines, parameter=my_parameter, excel_path=path.format(".xlsx"), csv_path=path.format("_csv"), ) sc_new = st.DeflexScenario() sc_new.read_csv(path.format("_csv")) cls.input_data = sc_new.input_data
def test_scenario_es_init(): data = { "general": pd.Series({ "year": 2013, "name": "test", "number of time steps": 8760 }) } sc = scenario.DeflexScenario(input_data=data) es1 = sc.initialise_energy_system().es sc = scenario.DeflexScenario(input_data=data) sc.input_data["general"]["year"] = 2012 with pytest.warns(UserWarning, match="2012 is a leap year but the"): print(sc.initialise_energy_system().es) sc.input_data["general"]["number of time steps"] = 8784 es2 = sc.initialise_energy_system().es assert len(es1.timeindex) == 8760 assert len(es2.timeindex) == 8784
def test_scenario_building(): sc = scenario.DeflexScenario(name="test", year=2014) csv_path = os.path.join(TEST_PATH, "de22_heat_transmission_csv") sc.read_csv(csv_path) sc.table2es() sc.check_input_data() sc.input_data["volatile series"].loc[5, ("DE01", "wind")] = float("nan") with pytest.raises(ValueError, match=r"NaN values found in table:'volatile series'"): sc.check_input_data()
def test_corrupt_data(): sc = scenario.DeflexScenario(year=2014) csv_path = os.path.join(TEST_PATH, "de03_fictive_csv") sc.read_csv(csv_path) sc.input_data["volatile series"].drop(("DE02", "solar"), inplace=True, axis=1) msg = "Missing time series for solar" with pytest.raises(ValueError, match=msg): sc.table2es()
def test_excel_reader(): sc = scenario.DeflexScenario() xls_fn = fetch_test_files("de02_heat.xlsx") sc.read_xlsx(xls_fn) sc.initialise_energy_system() sc.table2es() csv_path = os.path.join(TEST_PATH, "deflex_2013_de02_tmp_X45_test_csv") sc.to_csv(csv_path) xls_fn = os.path.join(TEST_PATH, "deflex_2014_de02_tmp_X45_test") sc.to_xlsx(xls_fn) xls_fn += ".xlsx" sc.to_xlsx(xls_fn) shutil.rmtree(csv_path) os.remove(xls_fn)
def test_build_model_manually(): sc = scenario.DeflexScenario(debug=True) xls_fn = fetch_test_files("de02_no-heat.xlsx") sc.read_xlsx(xls_fn) sc.initialise_energy_system() test_nodes = sc.create_nodes() sc.add_nodes_to_es(test_nodes) dump_fn = os.path.join(TEST_PATH, "pytest_test") sc.dump(dump_fn) model = sc.create_model() sc.solve(model=model, solver="cbc", with_duals=True) assert ( sc.es.results["meta"]["name"] == "deflex_2014_de02_no-heat_reg-merit") dump_fn += ".dflx" sc.dump(dump_fn) plot_fn = os.path.join(TEST_PATH, "test_plot_X343.graphml") sc.plot_nodes(plot_fn) assert os.path.isfile(plot_fn) scenario.restore_scenario(dump_fn) assert int(sc.meta["year"]) == 2014 os.remove(dump_fn) os.remove(plot_fn)
# the xlsx scenarios are the same. The csv-directories cen be read faster by # the computer but the xlsx-files are easier to read for humans because all # sheets are in one file. # NOTE: Large models will need up to 24 GB of RAM, so start with small models # and increase the size step by step. You can also use large models with less # time steps but you have to adapt the annual limits. # Now choose one example. We will start with a small one: file = "deflex_2014_de02_no-heat_csv" fn = os.path.join(path, file) # *** Long version *** # Create a scenario object sc = scenario.DeflexScenario() # Read the input data. Use the right method (csv/xlsx) for your file type. sc.read_csv(fn) # sc.read_xlsx(fn) # Create the LP model and solve it. sc.compute() # Dump the results to a sub-dir named "results_cbc". dump_file = file.replace("_csv", ".dflx") # dump_file = file.replace(".xlsx") dump_path = os.path.join(path, "results_cbc", dump_file) sc.dump(dump_path) # *** short version ***
def setup_class(cls): path = os.path.join(TEST_PATH, "de22_heat_transmission_csv") sc = st.DeflexScenario() sc.read_csv(path) cls.tables = sc.input_data tmp_tables = {} parameter = { "costs_source": "ewi", "downtime_bioenergy": 0.1, "limited_transformer": "bioenergy", "local_fuels": "district heating", "map": "de22", "mobility_other": "petrol", "round": 1, "separate_heat_regions": "de22", "copperplate": False, "default_transmission_efficiency": 0.9, "group_transformer": False, "heat": True, "use_CO2_costs": True, "use_downtime_factor": True, "use_variable_costs": False, "year": 2014, } config.init(paths=[os.path.dirname(dfile)]) for option, value in parameter.items(): cfg.tmp_set("creator", option, str(value)) config.tmp_set("creator", option, str(value)) name = "heat_demand_deflex" fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv") tmp_tables[name] = pd.read_csv(fn, index_col=[0], header=[0, 1]) name = "transformer_balance" fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv") tmp_tables[name] = pd.read_csv(fn, index_col=[0, 1, 2], header=[0]) powerplants.scenario_powerplants = MagicMock( return_value={ "volatile plants": cls.tables["volatile plants"], "power plants": cls.tables["power plants"], }) powerplants.scenario_chp = MagicMock( return_value={ "heat-chp plants": cls.tables["heat-chp plants"], "power plants": cls.tables["power plants"], }) feedin.scenario_feedin = MagicMock( return_value=cls.tables["volatile series"]) demand_table = { "electricity demand series": cls.tables["electricity demand series"], "heat demand series": cls.tables["heat demand series"], } demand.scenario_demand = MagicMock(return_value=demand_table) name = "deflex_2014_de22_heat_transmission" polygons = deflex_regions(rmap=parameter["map"], rtype="polygons") lines = deflex_power_lines(parameter["map"]).index cls.input_data = scenario_creator.create_scenario( polygons, 2014, name, lines)
def test_build_model(): sc = scenario.DeflexScenario(debug=True) xls_fn = fetch_test_files("de02_heat.xlsx") sc.read_xlsx(xls_fn) sc.compute() assert sc.es.results["meta"]["name"] == "deflex_2014_de02_heat_reg-merit"
def test_scenario_es_init_error(): sc = scenario.DeflexScenario() msg = "There is no input data in the scenario. You cannot initialise an" with pytest.raises(ValueError, match=msg): sc.initialise_energy_system()
def setup_class(cls): cls.sc = scenario.DeflexScenario() fn = os.path.join(TEST_PATH, "de02_no-heat.xlsx") cls.sc.read_xlsx(fn) cls.sc.input_data["general"]["regions"] = float("nan")