-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCaseStudy_Runner.py
157 lines (122 loc) · 4.74 KB
/
CaseStudy_Runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
"""
RIVM note: Script copied from EMPA plastic-dpmfa package:
https://github.com/empa-tsl/plastic-dpmfa
plastic-dpmfa/setup_model_new.py
only global variables (name of database, name/location of folder) are added
Original:
Created on 09.04.2020
@author: Delphine Kawecki
"""
import os
# Set working directory to where the scripts are located
os.chdir("N:/Documents/MyFolder")
import config
import csv
import numpy as np
from dpmfa import simulator as sc
from dpmfa import components as cp
import setup_model_new as su # or if cloned, change to location of cloned package
# Fetch configurations
startYear = config.startyear
Tperiods = config.endyear - config.startyear + 1
Speriod = config.Speriod
RUNS = config.RUNS
seed = config.seed
reg = config.reg
mat = "RUBBER"
sel = "Tyre wear"
mainfolder = os.getcwd()
# Steps to find or create folders
if config.OS_env == 'win':
inputfolder = ".\\input\\" + reg + "\\"
else:
inputfolder = "./input/" + reg + "/"
db_name = reg + "_"+ sel + ".db"
os.chdir(inputfolder)
pathtoDB = os.path.abspath(db_name)
#%%
#########################################
# Run the model
#########################################
## Initiate model parameters
modelname = sel + " " + mat + " in " + reg
endYear = startYear + Tperiods - 1
# for plots
xScale=np.arange(startYear,startYear+Tperiods)
# Run setup model
model = su.setupModel(pathtoDB,modelname,RUNS,mat, startYear, endYear)
# check validity
#model.checkModelValidity()
#model.debugModel()
# set up the simulator object
simulator = sc.Simulator(RUNS, Tperiods, seed, True, True)
# define what model needs to be run
simulator.setModel(model)
simulator.runSimulation()
#simulator.debugSimulator()
print('Simulation succesful...\n')
# Change directory back to the main folder (where the scripts are)
os.chdir(mainfolder)
# If it does not exist, create an output folder
outputfolder = ".\\output\\" + sel + "\\" + mat + "\\"
if not os.path.exists(outputfolder):
os.makedirs(outputfolder)
# If it does not exist, create a CSV folder in the output folder
csvfolder = ".\\output\\" + sel + "\\" + mat + "\\" + "csv\\"
if not os.path.exists(csvfolder):
os.makedirs(csvfolder)
#%%
### Get inflows, outflows, stocks and sinks
loggedInflows = simulator.getLoggedInflows()
loggedOutflows = simulator.getLoggedOutflows()
stocks = simulator.getStocks()
sinks = simulator.getSinks()
#%%
## display mean ± std for each outflow
print('-----------------------')
for Speriod in range(Tperiods):
print('Logged Outflows period '+str(Speriod)+' (year: '+str(startYear+Speriod)+'):')
print('')
# loop over the list of compartments with loggedoutflows
for Comp in loggedOutflows:
print('Flows from ' + Comp.name +':' )
# in this case name is the key, value is the matrix(data), in this case .items is needed
for Target_name, value in Comp.outflowRecord.items():
print(' --> ' + str(Target_name)+ ': Mean = '+str(round(np.mean(value[:,Speriod]),0))+' ± '+str(round(np.std(value[:,Speriod]),0)) + ' kiloton' )
print('')
print('-----------------------')
#%%
### export data
# In CSV files, columns are years, rows are runs
# export outflows to csv
for Comp in loggedOutflows: # loggedOutflows is the compartment list of compartments with loggedoutflows
for Target_name, value in Comp.outflowRecord.items(): # in this case name is the key, value is the matrix(data), in this case .items is needed
with open(os.path.join(csvfolder,"loggedOutflows_"+ reg + '_' + sel + '_' + mat +"_" + Comp.name + "_to_" + Target_name + ".csv"), 'w') as RM :
a = csv.writer(RM, delimiter=' ')
data = np.asarray(value)
a.writerows(data)
#%%
# export inflows to csv
for Comp in loggedInflows: # loggedOutflows is the compartment list of compartmensts with loggedoutflows
with open(os.path.join(csvfolder,"loggedInflows_"+ reg + '_' + sel + '_' + mat+ "_" + Comp +".csv"), 'w') as RM :
a = csv.writer(RM, delimiter=' ')
data = np.asarray(loggedInflows[Comp])
a.writerows(data)
#%%
# export sinks to csv
for sink in sinks:
if isinstance(sink,cp.Stock):
continue
with open(os.path.join(csvfolder,"sinks_" + reg + '_' + sel + '_' + mat + "_" + sink.name +".csv"), 'w') as RM :
a = csv.writer(RM, delimiter=' ')
data = np.asarray(sink.inventory)
a.writerows(data)
#%%
# export stocks to csv
for stock in stocks:
with open(os.path.join(csvfolder,"stocks_"+ reg + '_' + sel + '_' + mat + "_" + stock.name +".csv"), 'w') as RM :
a = csv.writer(RM, delimiter=' ')
data = np.asarray(stock.inventory)
a.writerows(data)
print('Written all results to csv files.\n')