Skip to content

Commit 69b3dd6

Browse files
authored
Merge pull request #323 from jonastahl/master
Add server for Weburbs
2 parents e542b31 + 1b16760 commit 69b3dd6

File tree

13 files changed

+677
-90
lines changed

13 files changed

+677
-90
lines changed

.github/workflows/docker.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Build and Push Docker Image
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
buildOptimizer:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Docker meta
14+
id: meta
15+
uses: docker/metadata-action@v5
16+
with:
17+
# list of Docker images to use as base name for tags
18+
images: |
19+
ghcr.io/${{ github.repository }}
20+
# generate Docker tags based on the following events/attributes
21+
tags: |
22+
type=semver,pattern={{version}}
23+
type=semver,pattern={{major}}.{{minor}}
24+
type=semver,pattern={{major}}
25+
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v3
31+
32+
- name: Login to GHCR
33+
uses: docker/login-action@v3
34+
with:
35+
registry: ghcr.io
36+
username: ${{ github.repository_owner }}
37+
password: ${{ secrets.GH_PAT }}
38+
39+
- name: Build and push
40+
uses: docker/build-push-action@v6
41+
with:
42+
file: ./Dockerfile
43+
context: .
44+
cache-from: |
45+
user/app:cache
46+
type=local,src=.
47+
push: true
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY urbs-env.txt .
6+
RUN python -m venv urbs-env
7+
8+
RUN python -m venv urbs-env \
9+
&& ./urbs-env/bin/pip install --upgrade pip \
10+
&& ./urbs-env/bin/pip install -r urbs-env.txt \
11+
&& ./urbs-env/bin/pip install flask waitress
12+
13+
14+
RUN apt-get update && apt-get install -y \
15+
gcc libglpk-dev glpk-utils
16+
RUN ./urbs-env/bin/pip install glpk
17+
18+
COPY urbs urbs
19+
COPY runme.py .
20+
COPY server.py .
21+
22+
CMD ["./urbs-env/bin/python", "-m", "waitress", "--port=5000", "server:app"]

Input/simple.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"c_timesteps": 6,
3+
"global": {
4+
"CO2 limit": 150000000,
5+
"Cost limit": 35000000000
6+
},
7+
"site": {
8+
"Main": {
9+
"area": 20,
10+
"commodity": {
11+
"Solar": {
12+
"Type": "SupIm",
13+
"supim": [
14+
0, 0, 0, 1, 1, 1
15+
]
16+
},
17+
"Wind": {
18+
"Type": "SupIm",
19+
"supim": [
20+
1, 1, 1, 0, 0, 0
21+
]
22+
},
23+
"Elec": {
24+
"Type": "Demand",
25+
"demand": [
26+
10, 10, 10, 10, 10, 10
27+
]
28+
},
29+
"CO2": {
30+
"Type": "Env",
31+
"price": 0,
32+
"max": "inf",
33+
"maxperhour": "inf"
34+
}
35+
},
36+
"process": {
37+
"Wind park": {
38+
"inst-cap": 0,
39+
"cap-lo": 0,
40+
"cap-up": 20,
41+
"max-grad": "inf",
42+
"min-fraction": 3,
43+
"inv-cost": 1000,
44+
"fix-cost": 20,
45+
"var-cost": 0,
46+
"wacc": 0,
47+
"depreciation": 25,
48+
"commodity": {
49+
"Wind": {
50+
"Direction": "In",
51+
"ratio": 1
52+
},
53+
"Elec": {
54+
"Direction": "Out",
55+
"ratio": 1
56+
}
57+
}
58+
},
59+
"Photovoltaics": {
60+
"inst-cap": 0,
61+
"cap-lo": 0,
62+
"cap-up": 20,
63+
"max-grad": "inf",
64+
"min-fraction": 4,
65+
"inv-cost": 300,
66+
"fix-cost": 7,
67+
"var-cost": 0,
68+
"wacc": 0,
69+
"depreciation": 25,
70+
"area-per-cap": 1,
71+
"commodity": {
72+
"Solar": {
73+
"Direction": "In",
74+
"ratio": 1
75+
},
76+
"Elec": {
77+
"Direction": "Out",
78+
"ratio": 1
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}

Input/simple.xlsx

411 KB
Binary file not shown.

docker-compose.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
services:
3+
app:
4+
image: ghcr.io/jonastahl/idp-urbs:0.0.4
5+
build: .
6+
ports:
7+
- "5000:5000"

query.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl http://localhost:5000/simulate -XPOST -H "Content-type: application/json" -T Input/simple.json

runexcel.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import shutil
3+
import urbs
4+
5+
input_files = 'simple.xlsx' # for single year file name, for intertemporal folder name
6+
input_dir = 'Input'
7+
input_path = os.path.join(input_dir, input_files)
8+
9+
result_name = 'Run'
10+
result_dir = urbs.prepare_result_directory(result_name) # name + time stamp
11+
12+
# copy input file to result directory
13+
try:
14+
shutil.copytree(input_path, os.path.join(result_dir, input_dir))
15+
except NotADirectoryError:
16+
shutil.copyfile(input_path, os.path.join(result_dir, input_files))
17+
# copy run file to result directory
18+
shutil.copy(__file__, result_dir)
19+
20+
# objective function
21+
objective = 'cost' # set either 'cost' or 'CO2' as objective
22+
23+
# Choose Solver (cplex, glpk, gurobi, ...)
24+
solver = 'glpk'
25+
26+
# simulation timesteps
27+
(offset, length) = (3500, 24) # time step selection
28+
timesteps = range(offset, offset+length+1)
29+
dt = 1 # length of each time step (unit: hours)
30+
31+
# detailed reporting commodity/sites
32+
report_tuples = []
33+
34+
# optional: define names for sites in report_tuples
35+
report_sites_name = {}
36+
37+
# plotting commodities/sites
38+
plot_tuples = []
39+
40+
# optional: define names for sites in plot_tuples
41+
plot_sites_name = {}
42+
43+
# plotting timesteps
44+
plot_periods = {
45+
'all': timesteps[1:]
46+
}
47+
48+
# add or change plot colors
49+
my_colors = {}
50+
for country, color in my_colors.items():
51+
urbs.COLORS[country] = color
52+
53+
# select scenarios to be run
54+
scenarios = [
55+
urbs.scenario_base
56+
]
57+
58+
for scenario in scenarios:
59+
prob = urbs.run_scenario(input_path, solver, timesteps, scenario,
60+
result_dir, dt, objective,
61+
plot_tuples=plot_tuples,
62+
plot_sites_name=plot_sites_name,
63+
plot_periods=plot_periods,
64+
report_tuples=report_tuples,
65+
report_sites_name=report_sites_name)

runme.py

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,68 @@
22
import shutil
33
import urbs
44

5-
input_files = 'single_year_example.xlsx' # for single year file name, for intertemporal folder name
6-
input_dir = 'Input'
7-
input_path = os.path.join(input_dir, input_files)
5+
def run():
6+
input_files = 'single_year_example.xlsx' # for single year file name, for intertemporal folder name
7+
input_dir = 'Input'
8+
input_path = os.path.join(input_dir, input_files)
89

9-
result_name = 'Run'
10-
result_dir = urbs.prepare_result_directory(result_name) # name + time stamp
10+
result_name = 'Run'
11+
result_dir = urbs.prepare_result_directory(result_name) # name + time stamp
1112

12-
# copy input file to result directory
13-
try:
14-
shutil.copytree(input_path, os.path.join(result_dir, input_dir))
15-
except NotADirectoryError:
16-
shutil.copyfile(input_path, os.path.join(result_dir, input_files))
17-
# copy run file to result directory
18-
shutil.copy(__file__, result_dir)
13+
# copy input file to result directory
14+
try:
15+
shutil.copytree(input_path, os.path.join(result_dir, input_dir))
16+
except NotADirectoryError:
17+
shutil.copyfile(input_path, os.path.join(result_dir, input_files))
18+
# copy run file to result directory
19+
shutil.copy(__file__, result_dir)
1920

20-
# objective function
21-
objective = 'cost' # set either 'cost' or 'CO2' as objective
21+
# objective function
22+
objective = 'cost' # set either 'cost' or 'CO2' as objective
2223

23-
# Choose Solver (cplex, glpk, gurobi, ...)
24-
solver = 'gurobi'
24+
# Choose Solver (cplex, glpk, gurobi, ...)
25+
solver = 'gurobi'
2526

26-
# simulation timesteps
27-
(offset, length) = (0, 8760) # time step selection
28-
timesteps = range(offset, offset+length+1)
29-
dt = 1 # length of each time step (unit: hours)
27+
# simulation timesteps
28+
(offset, length) = (0, 8760) # time step selection
29+
timesteps = range(offset, offset+length+1)
30+
dt = 1 # length of each time step (unit: hours)
3031

31-
# detailed reporting commodity/sites
32-
report_tuples = []
32+
# detailed reporting commodity/sites
33+
report_tuples = []
3334

34-
# optional: define names for sites in report_tuples
35-
report_sites_name = {}
35+
# optional: define names for sites in report_tuples
36+
report_sites_name = {}
3637

37-
# plotting commodities/sites
38-
plot_tuples = []
38+
# plotting commodities/sites
39+
plot_tuples = []
3940

40-
# optional: define names for sites in plot_tuples
41-
plot_sites_name = {}
41+
# optional: define names for sites in plot_tuples
42+
plot_sites_name = {}
4243

43-
# plotting timesteps
44-
plot_periods = {
45-
'all': timesteps[1:]
46-
}
44+
# plotting timesteps
45+
plot_periods = {
46+
'all': timesteps[1:]
47+
}
4748

48-
# add or change plot colors
49-
my_colors = {}
50-
for country, color in my_colors.items():
51-
urbs.COLORS[country] = color
49+
# add or change plot colors
50+
my_colors = {}
51+
for country, color in my_colors.items():
52+
urbs.COLORS[country] = color
5253

53-
# select scenarios to be run
54-
scenarios = [
55-
urbs.scenario_base
56-
]
54+
# select scenarios to be run
55+
scenarios = [
56+
urbs.scenario_base
57+
]
5758

58-
for scenario in scenarios:
59-
prob = urbs.run_scenario(input_path, solver, timesteps, scenario,
60-
result_dir, dt, objective,
61-
plot_tuples=plot_tuples,
62-
plot_sites_name=plot_sites_name,
63-
plot_periods=plot_periods,
64-
report_tuples=report_tuples,
65-
report_sites_name=report_sites_name)
59+
for scenario in scenarios:
60+
prob = urbs.run_scenario(input_path, solver, timesteps, scenario,
61+
result_dir, dt, objective,
62+
plot_tuples=plot_tuples,
63+
plot_sites_name=plot_sites_name,
64+
plot_periods=plot_periods,
65+
report_tuples=report_tuples,
66+
report_sites_name=report_sites_name)
67+
68+
if __name__ == "__main__":
69+
run()

0 commit comments

Comments
 (0)