from matplotlib import pyplot as plt
import pandas as pd
import pypsa
test_file = "test.nc"
load = pd.Series(
[0, 0, 0, 0, 0, 0, 0.2, 0.5, 0.7, 0.85, 0.95, 1.0, 0.95, 0.85, 0.7, 0.5, 0.2, 0, 0, 0, 0, 0, 0, 0],
index=pd.date_range("0:00", "23:00", freq="1h")
)
load.index.name = "time"
nu = pypsa.Network()
nu.set_snapshots(load.index)
nu.add("Bus", "bus")
nu.add(
"Generator", "generator", bus="bus",
p_nom_extendable=True,
capital_cost=1,
)
nu.add("Load", "load", bus="bus", p_set=load)
nu.consistency_check()
out = nu.lopf(nu.snapshots)
assert out == ("ok", "optimal")
print(f"writing output to {test_file} ... ", end="", flush=True)
nu.export_to_netcdf(test_file)
print("done")
nu.generators_t.p["generator"].plot()
del nu
nu2 = pypsa.Network()
print(f"loading input from {test_file} ... ", end="", flush=True)
nu2.import_from_netcdf(test_file)
print("done")
plt.figure()
nu2.generators_t.p["generator"].plot()
plt.plot()
import pandas as pd
import pypsa
# check he uses the correct PyPSA, cloned from github
print(f"PyPSA location: {pypsa.__file__}")
# test_file = "test.nc" # test netCDF
test_file = "test.hdf5" # test hdf5
load = pd.Series(
[0, 0, 0, 0, 0, 0, 0.2, 0.5, 0.7, 0.85, 0.95, 1.0, 0.95, 0.85, 0.7, 0.5, 0.2, 0, 0, 0, 0, 0, 0, 0],
index=pd.date_range("0:00", "23:00", freq="1h")
)
load.index.name = "time"
nu = pypsa.Network()
nu.set_snapshots(load.index)
nu.add("Bus", "bus")
nu.add(
"Generator", "generator", bus="bus",
p_nom_extendable=True,
capital_cost=1,
)
nu.add("Load", "load", bus="bus", p_set=load)
nu.consistency_check()
out = nu.lopf(nu.snapshots)
assert out == ("ok", "optimal")
print(f"writing output to {test_file} ... ", end="", flush=True)
if test_file.endswith(".nc"):
nu.export_to_netcdf(test_file)
elif test_file.endswith(".hdf5"):
nu.export_to_hdf5(test_file)
else:
raise NotImplementedError(f"unknown file extension for {test_file!r}")
print("done")
print(nu.generators_t.p)
del nu
nu2 = pypsa.Network()
print(f"loading input from {test_file} ... ", end="", flush=True)
if test_file.endswith(".nc"):
nu2.import_from_netcdf(test_file)
elif test_file.endswith(".hdf5"):
nu2.import_from_hdf5(test_file)
else:
raise NotImplementedError(f"unknown file extension for {test_file!r}")
print("done")
print(nu2.generators_t.p)
=======================================================
= Output
=======================================================
PyPSA location: **REMOVED BUT CORRECT**
INFO:pypsa.pf:Slack bus for sub-network 0 is bus
INFO:pypsa.opf:Performed preliminary steps
INFO:pypsa.opf:Building pyomo model using `angles` formulation
INFO:pypsa.opf:Solving model using glpk
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Name: unknown
Lower bound: 1.0
Upper bound: 1.0
Number of objectives: 1
Number of constraints: 97
Number of variables: 50
Number of nonzeros: 121
Sense: minimize
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Error rc: 0
Time: 0.009018421173095703
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0
INFO:pypsa.opf:Optimization successful
writing output to test.hdf5 ... WARNING:pypsa.io:HDF5 file format for PyPSA is now deprecated,
because HDF5 fails for tables with more than 1000 columns.
Please use netCDF instead.
INFO:pypsa.io:Exported network test.hdf5 has loads, buses, generators
done
name generator
time
2018-03-26 00:00:00 0.00
2018-03-26 01:00:00 0.00
2018-03-26 02:00:00 0.00
2018-03-26 03:00:00 0.00
2018-03-26 04:00:00 0.00
2018-03-26 05:00:00 0.00
2018-03-26 06:00:00 0.20
2018-03-26 07:00:00 0.50
2018-03-26 08:00:00 0.70
2018-03-26 09:00:00 0.85
2018-03-26 10:00:00 0.95
2018-03-26 11:00:00 1.00
2018-03-26 12:00:00 0.95
2018-03-26 13:00:00 0.85
2018-03-26 14:00:00 0.70
2018-03-26 15:00:00 0.50
2018-03-26 16:00:00 0.20
2018-03-26 17:00:00 0.00
2018-03-26 18:00:00 0.00
2018-03-26 19:00:00 0.00
2018-03-26 20:00:00 0.00
2018-03-26 21:00:00 0.00
2018-03-26 22:00:00 0.00
2018-03-26 23:00:00 0.00
loading input from test.hdf5 ... WARNING:pypsa.io:HDF5 file format for PyPSA is now deprecated,
because HDF5 fails for tables with more than 1000 columns.
Please use netCDF instead.
ERROR:pypsa.io:Error, no buses found
done
Empty DataFrame
Columns: []
Index: [2018-03-26 00:00:00, 2018-03-26 01:00:00, 2018-03-26 02:00:00, 2018-03-26 03:00:00, 2018-03-26 04:00:00, 2018-03-26 05:00:00, 2018-03-26 06:00:00, 2018-03-26 07:00:00, 2018-03-26 08:00:00, 2018-03-26 09:00:00, 2018-03-26 10:00:00, 2018-03-26 11:00:00, 2018-03-26 12:00:00, 2018-03-26 13:00:00, 2018-03-26 14:00:00, 2018-03-26 15:00:00, 2018-03-26 16:00:00, 2018-03-26 17:00:00, 2018-03-26 18:00:00, 2018-03-26 19:00:00, 2018-03-26 20:00:00, 2018-03-26 21:00:00, 2018-03-26 22:00:00, 2018-03-26 23:00:00]