-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodelib.c
More file actions
338 lines (302 loc) · 13.4 KB
/
modelib.c
File metadata and controls
338 lines (302 loc) · 13.4 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* @author Daniel Bubenicek (xbuben05)
* Projekt do predmetu IMS na FIT VUT v Brne (2019/2020)
* @date 5.12.2019
*/
#include <stdlib.h>
#include <time.h>
#include "modelib.h"
#include "otherlib.h"
#include "loglib.h"
const int MsourceTypesNumber = 8;
double
McorrectInstalledPower(enum MsourceTypes type, unsigned long *actualInstalledSourcePowerKw);
long MrandomRange(long lower, long upper) {
static int init = 1;
if(init)
{
srand(time(0)); //for little bit more realistic random
init = 0;
}
return (rand() % (upper - lower + 1)) + lower;
}
double MgetCFBySourceType(enum MsourceTypes type, double powerAmountKWH) {
long ret;
switch (type)
{
case coal:
ret = MrandomRange(8500, 10500);
break;
case nuclear:
case solar:
ret = 0;
break;
case gas:
ret = MrandomRange(4500,6500);
break;
case hydro:
ret = MrandomRange(45, 135);
break;
case wind:
ret = MrandomRange(7, 18);
break;
case biomass:
ret = MrandomRange(90, 450);
break;
case other:
ret = MrandomRange(1877, 2515);//aritmetický průměr
break;
default:
ret = -1;
break;
}
return ret/10.0*powerAmountKWH; //the CFs upper are 10x bigger than real ones to easily generate random nums, so it needs to be divided
}
//NOTE: Uhlí má při produkci elektřiny tak brutální emise, že bych deharmonizací všechno spíš znepřesnil, plyn se řídí uhlím
double MgetSourceTypeBuildCF(enum MsourceTypes type, unsigned long installedPowerKW) {
long ret;
switch (type) {
case coal:
ret = 1; //v pomeru k produkci zanedbatelna a nedohledatelna (rozhodně pod 1 %, vzhledem k číslům odhaduji tak kolem 0.001%)
break;
case nuclear:
ret = MrandomRange(2279 * Mpow10(3), 2416 * Mpow10(3)) + 739; //přičítaná hodnota je za výrobu, vzhledem k výrobě je tak malá, že by nešla rozpočítat
break;
case solar:
ret = MrandomRange(107 * Mpow10(2), 148 * Mpow10(2));
break;
case gas:
ret = 1; //viz uhlí
break;
case hydro:
ret = MrandomRange(1752 * Mpow10(1), 55188 * Mpow10(1));
break;
case wind:
ret = MrandomRange(1406 * Mpow10(3), 3514 * Mpow10(3));
break;
case biomass:
ret = MrandomRange(876 * Mpow10(3), 105 * Mpow10(5));
break;
case other:
ret = MrandomRange(5 * Mpow10(4), 1 * Mpow10(5)); //aritmetický průměr ostatních
break;
default:
ret = -1;
break;
}
return ((double)ret) * installedPowerKW;
}
float MgetYearlyChangePercentageProduce(float startPercentage, float finalPercentage)
{
return ((float) (finalPercentage - startPercentage)) / (float) MlimitYears;
}
void MsetYearlyChangePercentageProduces()
{
MyearlyChangePercentageProduceCoal = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_COAL, MfinalPercentageProduceCoal);
MyearlyChangePercentageProduceNuclear = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_NUCLEAR, MfinalPercentageProduceNuclear);
MyearlyChangePercentageProduceWind = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_WIND, MfinalPercentageProduceWind);
MyearlyChangePercentageProduceHydro = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_HYDRO, MfinalPercentageProduceHydro);
MyearlyChangePercentageProduceBiomass = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_BIOMASS, MfinalPercentageProduceBiomass);
MyearlyChangePercentageProduceSolar = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_SOLAR, MfinalPercentageProduceSolar);
MyearlyChangePercentageProduceGas = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_GAS, MfinalPercentageProduceGas);
MyearlyChangePercentageProduceOther = MgetYearlyChangePercentageProduce(INIT_PERCENTAGE_PRODUCE_OTHER, MfinalPercentageProduceOther);
}
bool MinitSimulation(char *filename)
{
err = 0;
parseConfiguration(filename);
if(err)
return false;
Myears = 0;
Mdays = 0;
MdailyCF = 0;
MyearCF = 0;
MfinalCFkg = 0;
MyearlyProductionGWH = 0;
MdailyProductionKWH = INIT_DAILY_PRODUCE_KWH;
MactualPercentageProduceCoal = INIT_PERCENTAGE_PRODUCE_COAL;
MactualPercentageProduceNuclear = INIT_PERCENTAGE_PRODUCE_NUCLEAR;
MactualPercentageProduceWind = INIT_PERCENTAGE_PRODUCE_WIND;
MactualPercentageProduceHydro = INIT_PERCENTAGE_PRODUCE_HYDRO;
MactualPercentageProduceBiomass = INIT_PERCENTAGE_PRODUCE_BIOMASS;
MactualPercentageProduceSolar = INIT_PERCENTAGE_PRODUCE_SOLAR;
MactualPercentageProduceGas = INIT_PERCENTAGE_PRODUCE_GAS;
MactualPercentageProduceOther = INIT_PERCENTAGE_PRODUCE_OTHER;
MactualInstalledPowerKWCoal = INIT_INSTALLED_POWER_COAL;
MactualInstalledPowerKWNuclear = INIT_INSTALLED_POWER_NUCLEAR;
MactualInstalledPowerKWWind = INIT_INSTALLED_POWER_WIND;
MactualInstalledPowerKWHydro = INIT_INSTALLED_POWER_HYDRO;
MactualInstalledPowerKWBiomass = INIT_INSTALLED_POWER_BIOMASS;
MactualInstalledPowerKWSolar = INIT_INSTALLED_POWER_SOLAR;
MactualInstalledPowerKWGas = INIT_INSTALLED_POWER_GAS;
MactualInstalledPowerKWOther = INIT_INSTALLED_POWER_OTHER;
MsetYearlyChangePercentageProduces();
//koeficient vyuziti pro celou simulaci bude vychazet z koeficientu pocatecnich
MutilizationRatioCoal = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_COAL / 100 / 24 /INIT_INSTALLED_POWER_COAL;
MutilizationRatioNuclear = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_NUCLEAR / 100 / 24 /INIT_INSTALLED_POWER_NUCLEAR;
MutilizationRatioWind = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_WIND / 100 / 24 /INIT_INSTALLED_POWER_WIND;
MutilizationRatioHydro = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_HYDRO / 100 / 24 /INIT_INSTALLED_POWER_HYDRO;
MutilizationRatioBiomass = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_BIOMASS / 100 / 24 /INIT_INSTALLED_POWER_BIOMASS;
MutilizationRatioSolar = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_SOLAR / 100 / 24 /INIT_INSTALLED_POWER_SOLAR;
MutilizationRatioGas = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_GAS / 100 / 24 /INIT_INSTALLED_POWER_GAS;
MutilizationRatioOther = INIT_DAILY_PRODUCE_KWH * (float) INIT_PERCENTAGE_PRODUCE_OTHER / 100 / 24 /INIT_INSTALLED_POWER_OTHER;
return true;
}
void MstartSimulation()
{
while (MlimitYears >= Myears)
{
MyearlyProductionGWH = 0;
MsimulateYear();
logYear(Mverbose);
if(MlimitYears != Myears)
{
MupdateProductionRatio();
MyearCF += McorrectInstalledPowerAll();
MyearCF += MrenewPowerPlantsAll();
MfinalCFkg += MyearCF / 1000; //g -> kg
}
logYear(Mverbose);
Myears++;
}
Myears--; //correction because of the while cycle
logTotal(Mverbose);
}
void MsimulateYear()
{
int daysInYear = 365;
if(!Myears%4) //leap year
daysInYear = 366;
MyearCF = 0;
for (int i = 0; i < daysInYear; ++i)
{
MyearlyProductionGWH += MdailyProductionKWH / Mpow10(6); //kwh -> gwh
MsimulateDay();
MyearCF += (unsigned long long) MdailyCF; //not important loose of precision
MdailyProductionKWH += DAILY_INCREASE_PRODUCE_KWH;
logDay(Mverbose);
Mdays++;
}
}
void MsimulateDay()
{
MdailyCF = MgetCFBySourceType(coal, MgetActualDailyProductionBySource(MactualPercentageProduceCoal));
MdailyCF += MgetCFBySourceType(nuclear, MgetActualDailyProductionBySource(MactualPercentageProduceNuclear));
MdailyCF += MgetCFBySourceType(wind, MgetActualDailyProductionBySource(MactualPercentageProduceWind));
MdailyCF += MgetCFBySourceType(hydro, MgetActualDailyProductionBySource(MactualPercentageProduceHydro));
MdailyCF += MgetCFBySourceType(biomass, MgetActualDailyProductionBySource(MactualPercentageProduceBiomass));
MdailyCF += MgetCFBySourceType(solar, MgetActualDailyProductionBySource(MactualPercentageProduceSolar));
MdailyCF += MgetCFBySourceType(gas, MgetActualDailyProductionBySource(MactualPercentageProduceGas));
MdailyCF += MgetCFBySourceType(other, MgetActualDailyProductionBySource(MactualPercentageProduceOther));
}
unsigned long long MrenewPowerPlantsAll() {
double cf = 0;
cf += MgetSourceTypeBuildCF(coal, MactualInstalledPowerKWCoal/AVERAGE_LIFESPAN_COAL);
cf += MgetSourceTypeBuildCF(nuclear, MactualInstalledPowerKWNuclear/AVERAGE_LIFESPAN_NUCLEAR);
cf += MgetSourceTypeBuildCF(wind, MactualInstalledPowerKWWind/AVERAGE_LIFESPAN_WIND);
cf += MgetSourceTypeBuildCF(hydro, MactualInstalledPowerKWHydro/AVERAGE_LIFESPAN_HYDRO);
cf += MgetSourceTypeBuildCF(biomass, MactualInstalledPowerKWBiomass/AVERAGE_LIFESPAN_BIOMASS);
cf += MgetSourceTypeBuildCF(solar, MactualInstalledPowerKWSolar/AVERAGE_LIFESPAN_SOLAR);
cf += MgetSourceTypeBuildCF(gas, MactualInstalledPowerKWGas/AVERAGE_LIFESPAN_GAS);
cf += MgetSourceTypeBuildCF(other, MactualInstalledPowerKWOther/AVERAGE_LIFESPAN_OTHER);
return (unsigned long long) cf;
}
unsigned long long McorrectInstalledPowerAll()
{
double cf = 0;
cf += McorrectInstalledPower(coal, &MactualInstalledPowerKWCoal);
cf += McorrectInstalledPower(nuclear, &MactualInstalledPowerKWNuclear);
cf += McorrectInstalledPower(wind, &MactualInstalledPowerKWWind);
cf += McorrectInstalledPower(hydro, &MactualInstalledPowerKWHydro);
cf += McorrectInstalledPower(biomass, &MactualInstalledPowerKWBiomass);
cf += McorrectInstalledPower(solar, &MactualInstalledPowerKWSolar);
cf += McorrectInstalledPower(gas, &MactualInstalledPowerKWGas);
cf += McorrectInstalledPower(other, &MactualInstalledPowerKWOther);
return (unsigned long long) cf;
}
double McorrectInstalledPower(enum MsourceTypes type, unsigned long *actualInstalledSourcePowerKw)
{
unsigned long necessaryInstalledPower = MgetNecessaryInstalledPowerKW(type);
if(necessaryInstalledPower <= *actualInstalledSourcePowerKw)
{
*actualInstalledSourcePowerKw = necessaryInstalledPower; //downgrade
return 0;
}
*actualInstalledSourcePowerKw += (necessaryInstalledPower - *actualInstalledSourcePowerKw);
return MgetSourceTypeBuildCF(type, necessaryInstalledPower - *actualInstalledSourcePowerKw);
}
void MupdateProductionRatio()
{
MactualPercentageProduceCoal += MyearlyChangePercentageProduceCoal;
MactualPercentageProduceNuclear += MyearlyChangePercentageProduceNuclear;
MactualPercentageProduceWind += MyearlyChangePercentageProduceWind;
MactualPercentageProduceHydro += MyearlyChangePercentageProduceHydro;
MactualPercentageProduceBiomass += MyearlyChangePercentageProduceBiomass;
MactualPercentageProduceSolar += MyearlyChangePercentageProduceSolar;
MactualPercentageProduceGas += MyearlyChangePercentageProduceGas;
MactualPercentageProduceOther += MyearlyChangePercentageProduceOther;
if(MactualPercentageProduceCoal < 0)
MactualPercentageProduceCoal = 0;
if(MactualPercentageProduceNuclear < 0)
MactualPercentageProduceNuclear = 0;
if(MactualPercentageProduceWind < 0)
MactualPercentageProduceWind = 0;
if(MactualPercentageProduceHydro < 0)
MactualPercentageProduceHydro = 0;
if(MactualPercentageProduceBiomass < 0)
MactualPercentageProduceBiomass = 0;
if(MactualPercentageProduceSolar < 0)
MactualPercentageProduceSolar = 0;
if(MactualPercentageProduceGas < 0)
MactualPercentageProduceGas = 0;
if(MactualPercentageProduceOther < 0)
MactualPercentageProduceOther = 0;
}
double MgetActualDailyProductionBySource(float actualSourcePercentage)
{
return actualSourcePercentage / 100.0 * MdailyProductionKWH;
}
unsigned long MgetNecessaryInstalledPowerKW(enum MsourceTypes type)
{
float coefficient = -1;
float percentage = -1;
switch (type)
{
case coal:
coefficient = MutilizationRatioCoal;
percentage = MactualPercentageProduceCoal;
break;
case nuclear:
coefficient = MutilizationRatioNuclear;
percentage = MactualPercentageProduceNuclear;
break;
case solar:
coefficient = MutilizationRatioSolar;
percentage = MactualPercentageProduceSolar;
break;
case wind:
coefficient = MutilizationRatioWind;
percentage = MactualPercentageProduceWind;
break;
case gas:
coefficient = MutilizationRatioGas;
percentage = MactualPercentageProduceGas;
break;
case hydro:
coefficient = MutilizationRatioHydro;
percentage = MactualPercentageProduceHydro;
break;
case biomass:
coefficient = MutilizationRatioBiomass;
percentage = MactualPercentageProduceBiomass;
break;
case other:
coefficient = MutilizationRatioOther;
percentage = MactualPercentageProduceOther;
break;
}
if (coefficient < 0 || percentage < 0)
return -1;
// instalovaný výkon*koeficient využití = průměrný výkon
return MdailyProductionKWH / 24 * percentage / 100 / coefficient;
}