Skip to content

Commit

Permalink
black model from spreadsheet tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
simnh committed Mar 25, 2019
1 parent 1e324bd commit 158818d
Showing 1 changed file with 139 additions and 83 deletions.
222 changes: 139 additions & 83 deletions docs/tutorials/model-from-tabular-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,28 @@
"metadata": {},
"outputs": [],
"source": [
"scenario_name = 'base-scenario'\n",
"scenario_name = \"base-scenario\"\n",
"\n",
"# datapath for input data from the oemof tabular pacakge\n",
"datapath = os.path.join(\n",
" pkg.resource_filename(\"oemof.tabular\", \"\").replace('src/oemof/tabular', \"\"), \n",
" \"data/data.xls\")\n",
" pkg.resource_filename(\"oemof.tabular\", \"\").replace(\n",
" \"src/oemof/tabular\", \"\"\n",
" ),\n",
" \"data/data.xls\",\n",
")\n",
"\n",
"# results path\n",
"results_path = os.path.join(\n",
" os.path.expanduser(\"~\"), \n",
" \"oemof-results\", \n",
" os.path.expanduser(\"~\"), \"oemof-results\"\n",
")\n",
"\n",
"scenario_path = os.path.join(\n",
" results_path,\n",
" scenario_name, \n",
" \"output\"\n",
" results_path, scenario_name, \"output\"\n",
")\n",
"\n",
"if not os.path.exists(scenario_path):\n",
" os.makedirs(scenario_path)\n",
"print(scenario_path)"
"print(scenario_path)\n"
]
},
{
Expand All @@ -101,20 +101,23 @@
"outputs": [],
"source": [
"profiles = pd.read_excel(\n",
" datapath, sheet_name=\"profiles\", index_col=[0], parse_dates=True\n",
" datapath,\n",
" sheet_name=\"profiles\",\n",
" index_col=[0],\n",
" parse_dates=True,\n",
")\n",
"profiles.index.freq = \"1H\"\n",
"\n",
"bus = pd.read_excel(\n",
" datapath, sheet_name=\"bus\", index_col=0\n",
")\n",
"bus = pd.read_excel(datapath, sheet_name=\"bus\", index_col=0)\n",
"\n",
"volatile = pd.read_excel(\n",
" datapath, sheet_name=\"volatile-generator\", index_col=0\n",
")\n",
"\n",
"dispatchable = pd.read_excel(\n",
" datapath, sheet_name=\"dispatchable-generator\", index_col=0\n",
" datapath,\n",
" sheet_name=\"dispatchable-generator\",\n",
" index_col=0,\n",
")\n",
"\n",
"storage = pd.read_excel(\n",
Expand Down Expand Up @@ -147,7 +150,7 @@
"\n",
"load = pd.read_excel(\n",
" datapath, sheet_name=\"load\", index_col=0\n",
")"
")\n"
]
},
{
Expand Down Expand Up @@ -195,8 +198,11 @@
"metadata": {},
"outputs": [],
"source": [
"buses = {name: Bus(label=name, balanced=bool(arg.balanced)) for name, arg in bus.iterrows()}\n",
"es.add(*buses.values())"
"buses = {\n",
" name: Bus(label=name, balanced=bool(arg.balanced))\n",
" for name, arg in bus.iterrows()\n",
"}\n",
"es.add(*buses.values())\n"
]
},
{
Expand Down Expand Up @@ -225,13 +231,16 @@
"metadata": {},
"outputs": [],
"source": [
"for name, l in load.iterrows(): \n",
"for name, l in load.iterrows():\n",
" es.add(\n",
" fc.Load(\n",
" label=name, \n",
" bus=buses[l.bus], # reference the bus in the buses dictionary\n",
" label=name,\n",
" bus=buses[\n",
" l.bus\n",
" ], # reference the bus in the buses dictionary\n",
" amount=l.amount, # amount column\n",
" profile=profiles[l.profile])\n",
" profile=profiles[l.profile],\n",
" )\n",
" )\n"
]
},
Expand Down Expand Up @@ -259,32 +268,43 @@
"metadata": {},
"outputs": [],
"source": [
"for name, g in dispatchable.iterrows(): \n",
"for name, g in dispatchable.iterrows():\n",
" es.add(\n",
" fc.Dispatchable(\n",
" label=name,\n",
" bus=buses[g.bus],\n",
" carrier=g.carrier,\n",
" tech=g.tech,\n",
" marginal_cost=(\n",
" carrier.at[g.carrier, 'cost'] / \n",
" technology.at[(g.carrier, g.tech), 'efficiency']),\n",
" #efficiency=technology.at[(g.carrier, g.tech), 'efficiency'],\n",
" carrier.at[g.carrier, \"cost\"]\n",
" / technology.at[\n",
" (g.carrier, g.tech), \"efficiency\"\n",
" ]\n",
" ),\n",
" # efficiency=technology.at[(g.carrier, g.tech), 'efficiency'],\n",
" expandable=g.expandable,\n",
" capacity=g.capacity,\n",
" capacity_potential=g.capacity_potential,\n",
" capacity_cost=annuity(\n",
" technology.at[(g.carrier, g.tech), 'capex'], # to $/MW \n",
" technology.at[(g.carrier, g.tech), 'lifetime'], \n",
" 0.07) * 1000,\n",
" technology.at[\n",
" (g.carrier, g.tech), \"capex\"\n",
" ], # to $/MW\n",
" technology.at[\n",
" (g.carrier, g.tech), \"lifetime\"\n",
" ],\n",
" 0.07,\n",
" )\n",
" * 1000,\n",
" output_parameters={\n",
" 'emission_factor': (\n",
" carrier.at[g.carrier, 'emission_factor'] / \n",
" technology.at[(g.carrier, g.tech), 'efficiency'])\n",
" }\n",
" \"emission_factor\": (\n",
" carrier.at[g.carrier, \"emission_factor\"]\n",
" / technology.at[\n",
" (g.carrier, g.tech), \"efficiency\"\n",
" ]\n",
" )\n",
" },\n",
" )\n",
" )\n",
" "
" )\n"
]
},
{
Expand All @@ -308,7 +328,7 @@
"metadata": {},
"outputs": [],
"source": [
"for name, v in volatile.iterrows(): \n",
"for name, v in volatile.iterrows():\n",
" es.add(\n",
" fc.Volatile(\n",
" label=name,\n",
Expand All @@ -319,12 +339,16 @@
" capacity=v.capacity,\n",
" capacity_potential=v.capacity_potential,\n",
" capacity_cost=annuity(\n",
" technology.at[(v.carrier, v.tech), 'capex'], \n",
" technology.at[(v.carrier, v.tech), 'lifetime'], \n",
" 0.07) * 1000, # $/kW -> $/MW \n",
" profile=profiles[v.profile]\n",
" technology.at[(v.carrier, v.tech), \"capex\"],\n",
" technology.at[\n",
" (v.carrier, v.tech), \"lifetime\"\n",
" ],\n",
" 0.07,\n",
" )\n",
" * 1000, # $/kW -> $/MW\n",
" profile=profiles[v.profile],\n",
" )\n",
" )"
" )\n"
]
},
{
Expand Down Expand Up @@ -364,19 +388,30 @@
" capacity=s.capacity,\n",
" storage_capacity=s.storage_capacity,\n",
" expandable=s.expandable,\n",
" efficiency=\n",
" technology.at[(s.carrier, s.tech), 'efficiency'],\n",
" efficiency=technology.at[\n",
" (s.carrier, s.tech), \"efficiency\"\n",
" ],\n",
" loss_rate=s.loss_rate,\n",
" storage_capacity_cost=annuity(\n",
" technology.at[(s.carrier, s.tech), 'storage_capex'],\n",
" technology.at[(s.carrier, s.tech), 'lifetime'],\n",
" 0.07) * 1000, #$/kW -> $/MW\n",
" capacity_cost=annuity( \n",
" technology.at[(s.carrier, s.tech), 'capex'],\n",
" technology.at[(s.carrier, s.tech), 'lifetime'],\n",
" 0.07) * 1000, #$/kW -> $/MW\n",
" technology.at[\n",
" (s.carrier, s.tech), \"storage_capex\"\n",
" ],\n",
" technology.at[\n",
" (s.carrier, s.tech), \"lifetime\"\n",
" ],\n",
" 0.07,\n",
" )\n",
" * 1000, # $/kW -> $/MW\n",
" capacity_cost=annuity(\n",
" technology.at[(s.carrier, s.tech), \"capex\"],\n",
" technology.at[\n",
" (s.carrier, s.tech), \"lifetime\"\n",
" ],\n",
" 0.07,\n",
" )\n",
" * 1000, # $/kW -> $/MW\n",
" )\n",
" )"
" )\n"
]
},
{
Expand Down Expand Up @@ -420,33 +455,44 @@
"metadata": {},
"outputs": [],
"source": [
"for name, c in conversion.iterrows(): \n",
"for name, c in conversion.iterrows():\n",
" es.add(\n",
" fc.Conversion(\n",
" label=name,\n",
" from_bus=buses[c.from_bus],\n",
" to_bus=buses[c.to_bus],\n",
" carrier=c.carrier,\n",
" tech=c.tech,\n",
" efficiency=technology.at[(c.carrier, c.tech), 'efficiency'],\n",
" efficiency=technology.at[\n",
" (c.carrier, c.tech), \"efficiency\"\n",
" ],\n",
" marginal_cost=(\n",
" carrier.at[c.carrier, 'cost'] / \n",
" technology.at[(c.carrier, c.tech), 'efficiency']),\n",
" carrier.at[c.carrier, \"cost\"]\n",
" / technology.at[\n",
" (c.carrier, c.tech), \"efficiency\"\n",
" ]\n",
" ),\n",
" expandable=c.expandable,\n",
" capacity=c.capacity,\n",
" capacity_potential=c.capacity_potential,\n",
" capacity_cost=annuity(\n",
" technology.at[(c.carrier, c.tech), 'capex'], \n",
" technology.at[(c.carrier, c.tech), 'lifetime'], \n",
" 0.07) * 1000, # $/kW -> $/MW\n",
" technology.at[(c.carrier, c.tech), \"capex\"],\n",
" technology.at[\n",
" (c.carrier, c.tech), \"lifetime\"\n",
" ],\n",
" 0.07,\n",
" )\n",
" * 1000, # $/kW -> $/MW\n",
" output_parameters={\n",
" 'emission_factor': (\n",
" carrier.at[c.carrier, 'emission_factor'] / \n",
" technology.at[(c.carrier, c.tech), 'efficiency']\n",
" )\n",
" }\n",
" ) \n",
" )"
" \"emission_factor\": (\n",
" carrier.at[c.carrier, \"emission_factor\"]\n",
" / technology.at[\n",
" (c.carrier, c.tech), \"efficiency\"\n",
" ]\n",
" )\n",
" },\n",
" )\n",
" )\n"
]
},
{
Expand All @@ -462,17 +508,16 @@
"metadata": {},
"outputs": [],
"source": [
"for name, c in commodity.iterrows(): \n",
"for name, c in commodity.iterrows():\n",
" es.add(\n",
" fc.Commodity(\n",
" label=name,\n",
" bus=buses[c.bus],\n",
" carrier=c.carrier,\n",
" tech=c.tech,\n",
" amount=c.amount\n",
" amount=c.amount,\n",
" )\n",
" )\n",
" "
" )\n"
]
},
{
Expand Down Expand Up @@ -501,19 +546,18 @@
"outputs": [],
"source": [
"for name, e in excess.iterrows():\n",
" es.add(\n",
" fc.Excess(label=name, bus=buses[e.bus])\n",
" )\n",
" \n",
" es.add(fc.Excess(label=name, bus=buses[e.bus]))\n",
"\n",
"for name, s in shortage.iterrows():\n",
" es.add(\n",
" fc.Shortage(\n",
" label=name, \n",
" carrier='electricity',\n",
" tech='shortage',\n",
" bus=buses[s.bus], \n",
" marginal_cost=s.marginal_cost)\n",
" )"
" label=name,\n",
" carrier=\"electricity\",\n",
" tech=\"shortage\",\n",
" bus=buses[s.bus],\n",
" marginal_cost=s.marginal_cost,\n",
" )\n",
" )\n"
]
},
{
Expand Down Expand Up @@ -582,10 +626,14 @@
"# writing results with the standard oemof-tabular output formatt\n",
"pp.write_results(m, scenario_path)\n",
"\n",
"print(\"Optimization done. Results are in {}.\".format(results_path))\n",
"print(\n",
" \"Optimization done. Results are in {}.\".format(\n",
" results_path\n",
" )\n",
")\n",
"\n",
"# write the lp-file\n",
"#m.write(io_options={'symbolic_solver_labels': True})"
"# m.write(io_options={'symbolic_solver_labels': True})\n"
]
},
{
Expand All @@ -610,18 +658,26 @@
"source": [
"import os\n",
"from plotly import offline, plotly\n",
"from oemof.tabular.tools.plots import hourly_plot, stacked_plot\n",
"from oemof.tabular.tools.plots import (\n",
" hourly_plot,\n",
" stacked_plot,\n",
")\n",
"\n",
"offline.init_notebook_mode()\n",
"\n",
"\n",
"offline.plot(\n",
" hourly_plot(\n",
" scenario_name,\n",
" \"LA-electricity\",\n",
" os.path.join(os.path.expanduser(\"~\"), \"oemof-results\"),\n",
" os.path.join(\n",
" os.path.expanduser(\"~\"), \"oemof-results\"\n",
" ),\n",
" plot_filling_levels=False,\n",
" ),\n",
" filename=os.path.join(scenario_path, \"hourly-plot.html\"),\n",
" filename=os.path.join(\n",
" scenario_path, \"hourly-plot.html\"\n",
" ),\n",
")\n"
]
}
Expand Down

0 comments on commit 158818d

Please sign in to comment.