forked from iss4e/Robust_Sizing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cc
207 lines (157 loc) · 4.57 KB
/
common.cc
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <climits>
#include "common.h"
double B_inv; // cost per cell
double PV_inv; // cost per unit (kW) of PV
double cells_min;
double cells_max;
double cells_step; // search in step of x cells
double pv_min;
double pv_max;
double pv_step; // search in steps of x kW
double epsilon;
double confidence;
int metric;
int days_in_chunk;
vector<double> load;
vector<double> solar;
vector<double> read_data_from_file(istream &datafile, int limit = INT_MAX) {
vector <double> data;
if (datafile.fail()) {
data.push_back(-1);
cerr << errno << ": read data file failed." << endl;
return data;
}
// read data file into vector
string line;
double value;
for (int i = 0; i < limit && getline(datafile, line); ++i) {
istringstream iss(line);
iss >> value;
data.push_back(value);
}
return data;
}
int process_input(char** argv, bool process_metric_input) {
int i = 0;
string inv_PV_string = argv[++i];
PV_inv = stod(inv_PV_string);
#ifdef DEBUG
cout << "inv_PV_string = " << PV_inv
<< ", PV_inv = " << PV_inv << endl;
#endif
string inv_B_string = argv[++i];
B_inv = stod(inv_B_string)*kWh_in_one_cell; // convert from per-kWh to per-cell cost
#ifdef DEBUG
cout << "inv_B_string = " << inv_B_string
<< ", B_inv = " << B_inv << endl;
#endif
string pv_max_string = argv[++i];
pv_max = stod(pv_max_string);
// set default pv_min and pv_step
pv_min = 0;
pv_step = (pv_max - pv_min) / num_pv_steps;
#ifdef DEBUG
cout << "pv_max_string = " << pv_max_string
<< ", pv_max = " << pv_max
<< ", pv_min = " << pv_min
<< ", pv_step = " << pv_step
<< endl;
#endif
string cells_max_string = argv[++i];
cells_max = stod(cells_max_string) / kWh_in_one_cell;
// set default cells_min and cells_step
cells_min = 0;
cells_step = (cells_max - cells_min) / num_cells_steps;
#ifdef DEBUG
cout << "cells_max_string = " << cells_max_string
<< ", cells_max = " << cells_max
<< ", cells_min = " << cells_min
<< ", cells_step = " << cells_step
<< endl;
#endif
if (process_metric_input) {
string metric_string = argv[++i];
metric = stoi(metric_string);
#ifdef DEBUG
cout << "metric_string = " << metric_string
<< ", metric = " << metric << endl;
#endif
}
string epsilon_string = argv[++i];
epsilon = stod(epsilon_string);
#ifdef DEBUG
cout << "epsilon_string = " << epsilon_string
<< ", epsilon = " << epsilon << endl;
#endif
string confidence_string = argv[++i];
confidence = stod(confidence_string);
#ifdef DEBUG
cout << "confidence_string = " << confidence_string
<< ", confidence = " << confidence << endl;
#endif
string days_in_chunk_string = argv[++i];
days_in_chunk = stoi(days_in_chunk_string);
#ifdef DEBUG
cout << "days_in_chunk_string = " << days_in_chunk_string
<< ", days_in_chunk = " << days_in_chunk << endl;
#endif
string loadfile = argv[++i];
#ifdef DEBUG
cout << "loadfile = " << loadfile << endl;
#endif
if (loadfile == string("--")) {
// read from cin
int limit = stoi(argv[++i]);
#ifdef DEBUG
cout << "reading load data from stdin. limit = " << limit << endl;
#endif
load = read_data_from_file(cin, limit);
} else {
#ifdef DEBUG
cout << "reading load file" << endl;
#endif
// read in data into vector
ifstream loadstream(loadfile.c_str());
load = read_data_from_file(loadstream);
}
#ifdef DEBUG
cout << "checking for errors in load file..." << endl;
#endif
if (load[0] < 0) {
cerr << "error reading load file " << loadfile << endl;
return 1;
}
string solarfile = argv[++i];
#ifdef DEBUG
cout << "solarfile = " << solarfile << endl;
#endif
if (solarfile == string("--")) {
#ifdef DEBUG
cout << "reading solar file" << endl;
#endif
// read from cin
int limit = stoi(argv[++i]);
#ifdef DEBUG
cout << "reading solar data from stdin. limit = " << limit << endl;
#endif
solar = read_data_from_file(cin, limit);
} else {
// read in data into vector
ifstream solarstream(solarfile.c_str());
solar = read_data_from_file(solarstream);
}
#ifdef DEBUG
cout << "checking for errors in solar file..." << endl;
#endif
if (solar[0] < 0) {
cerr << "error reading solar file " << solarfile << endl;
return 1;
}
return 0;
}