-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.cpp
440 lines (357 loc) · 10.6 KB
/
simulation.cpp
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <omp.h>
int NowYear; // 2021 - 2026
int NowMonth; // 0 - 11
float NowPrecip; // inches of rain per month
float NowTemp; // temperature this month
float NowHeight; // grain height in inches
int NowNumDeer; // number of deer in the current population
float NowMeteorProb; // current probability of a meteor shower
float NowGodsRageMeter; // current measurement of how angry gods are
bool NowMeteorDestruction; // current state of the destructive nature of a meteor shower
// 1 == True, 0 == False
omp_lock_t Lock;
int NumInThreadTeam;
int NumAtBarrier;
int NumGone;
unsigned int seed = 0.;
const float GRAIN_GROWS_PER_MONTH = 9.0;
const float ONE_DEER_EATS_PER_MONTH = 1.0;
const float AVG_PRECIP_PER_MONTH = 7.0; // average
const float AMP_PRECIP_PER_MONTH = 6.0; // plus or minus
const float RANDOM_PRECIP = 2.0; // plus or minus noise
const float AVG_TEMP = 60.0; // average
const float AMP_TEMP = 20.0; // plus or minus
const float RANDOM_TEMP = 10.0; // plus or minus noise
const float MIDTEMP = 40.0;
const float MIDPRECIP = 10.0;
float Ranf( unsigned int *seedp, float low, float high );
int Ranf( unsigned int *seedp, int ilow, int ihigh );
void set_temp( unsigned int *seed );
void set_precip( unsigned int *seed );
void AreGodsAngry( unsigned int *seed );
void set_gods_rage_meter( unsigned int *seed );
void set_meteor_destruction_imminence( );
void InitBarrier( );
void WaitBarrier( );
void SQR( );
float _ang( );
void Deer( );
void Grain( );
void MeteorShower( );
void Watcher( );
void InitBarrier( int );
void WaitBarrier( );
int main(int argc, char *argv[]) {
#ifndef _OPENMP
printf(stderr, "OpenMP not supported, exiting program.\n");
return 1;
#endif
// starting date and time:
NowMonth = 0;
NowYear = 2021;
// starting state
NowNumDeer = 1;
NowHeight = 1.;
NowMeteorProb = 0.;
NowMeteorDestruction = 0;
// initialize randomized factors
set_temp(&seed);
set_precip(&seed);
AreGodsAngry(&seed);
// set num of threads to 4, one for each agent
omp_set_num_threads(4);
// number of threads to wait at barrier
InitBarrier( 4 );
// create omp sections, one for each agent
#pragma omp parallel sections
{
#pragma omp section
{
Deer( );
}
#pragma omp section
{
Grain( );
}
#pragma omp section
{
Watcher( );
}
#pragma omp section
{
MeteorShower( );
}
}
return 0;
} // implied barrier -- all functions must return in order
// to allow any of them to get past here
// initialize makeshift barrier
void
InitBarrier( int n )
{
NumInThreadTeam = n;
NumAtBarrier = 0;
omp_init_lock( &Lock );
}
// makeshift barrier
void
WaitBarrier( )
{
omp_set_lock( &Lock );
{
NumAtBarrier++;
if( NumAtBarrier == NumInThreadTeam )
{
NumGone = 0;
NumAtBarrier = 0;
// let all other threads get back to what they were doing
// before this one unlocks, knowing that they might immediately
// call WaitBarrier( ) again:
while( NumGone != NumInThreadTeam-1 );
omp_unset_lock( &Lock );
return;
}
}
omp_unset_lock( &Lock );
while( NumAtBarrier != 0 ); // this waits for the nth thread to arrive
#pragma omp atomic
NumGone++; // this flags how many threads have returned
}
// a function for squaring a value
float
SQR( float x )
{
return x * x;
}
float _ang( )
{
return ( 30. * ( float )NowMonth + 15. ) * ( M_PI / 180. );
}
// set temp factor
void set_temp( unsigned int *seed )
{
float temp = AVG_TEMP - AMP_TEMP * cos( _ang( ) );
NowTemp = temp + Ranf( seed, -RANDOM_TEMP, RANDOM_TEMP );
}
// set precipitation factor
void set_precip( unsigned int *seed )
{
float precip = AVG_PRECIP_PER_MONTH + AMP_PRECIP_PER_MONTH * sin( _ang( ) );
NowPrecip = precip + Ranf( seed, -RANDOM_PRECIP, RANDOM_PRECIP );
if( NowPrecip < 0. )
{
NowPrecip = 0.;
}
}
// decides whether or not a meteor shower is destructive in nature (50% odds)
void
set_meteor_destruction_imminence ( )
{
int prob = rand( ) % 2;
if ( prob == 1 )
{
NowMeteorDestruction = 1;
}
else
{
NowMeteorDestruction = 0;
}
}
// randomizer
float
Ranf( unsigned int *seedp, float low, float high )
{
float r = (float) rand_r( seedp ); // 0 - RAND_MAX
return( low + r * ( high - low ) / (float)RAND_MAX );
}
// randomizer
int
Ranf( unsigned int *seedp, int ilow, int ihigh )
{
float low = (float)ilow;
float high = (float)ihigh + 0.9999f;
return (int)( Ranf(seedp, low, high) );
}
// sets the initial probability of a meteor shower occuring
void
set_meteor_prob( unsigned int *seed )
{
NowMeteorProb = Ranf( seed, 0.f, 1.f );
}
// measures how angry the gods are
void
AreGodsAngry( unsigned int *seed )
{
NowGodsRageMeter = Ranf( seed, 0.f, 4.f );
}
// calculate the deer population based on carrying capacity
// and the occurence of a destructive meteor shower
void
Deer( )
{
int deerPopulation;
int carryingCapacity;
bool meteor;
while ( NowYear < 2027 )
{
deerPopulation = NowNumDeer;
carryingCapacity = NowHeight;
meteor = NowMeteorDestruction;
// if there is an abundance of grain, increment deer population
if ( (float)deerPopulation < carryingCapacity )
{
deerPopulation++;
}
// if there is a shortage of grain, decrement deer population
else if ( (float)deerPopulation > carryingCapacity )
{
deerPopulation--;
}
// pad any negative values to 0
if ( deerPopulation < 0. )
{
deerPopulation = 0.;
}
// in the case of a destructive meteor shower,
// set deer population to 0
if ( meteor == 1 )
{
deerPopulation = 0.;
}
// done computing
WaitBarrier( );
// share computed deer count
NowNumDeer = deerPopulation;
// done assigning
WaitBarrier( );
// done printing
WaitBarrier( );
}
}
// calculate grain height based on precipitation and temperature
void
Grain( )
{
float grainHeight;
float tempFactor;
float precipFactor;
bool meteor;
while ( NowYear < 2027 )
{
tempFactor = exp( -SQR(( NowTemp - MIDTEMP ) / 10. ));
precipFactor = exp( -SQR(( NowPrecip - MIDPRECIP ) / 10. ));
meteor = NowMeteorDestruction;
// compute grain height based on environmental factors
grainHeight = NowHeight;
grainHeight += tempFactor * precipFactor * GRAIN_GROWS_PER_MONTH;
grainHeight -= (float)NowNumDeer * ONE_DEER_EATS_PER_MONTH;
// pad negative grain height to 0
if ( grainHeight < 0. )
{
grainHeight = 0.;
}
// in the case of a destructive meteor shower,
// set grain height to 0
if ( meteor == 1 )
{
grainHeight = 0.;
}
// done computing
WaitBarrier( );
// share computed grain height
NowHeight = grainHeight;
// done assigning
WaitBarrier( );
// done printing
WaitBarrier( );
}
}
// determine whether a meteor shower enters the earth's atmosphere
void
MeteorShower( )
{
float meteorShowerProb;
float godsRageMeter;
while ( NowYear < 2027 )
{
meteorShowerProb = NowMeteorProb;
godsRageMeter = NowGodsRageMeter;
// calculate the final probability of a meteor shower entering the earth's atmosphere
// = initial probability * the measurement of how angry the gods are
meteorShowerProb = meteorShowerProb * godsRageMeter;
// pad any probabilities greater than 1 to just 1
if ( meteorShowerProb >= 1. )
{
meteorShowerProb = 1.;
}
// done computing
WaitBarrier( );
// share computed meteor shower probability
NowMeteorProb = meteorShowerProb;
// done assigning
WaitBarrier( );
// done printing
WaitBarrier( );
}
}
// calculate environmental factors, increment time, and print results
void
Watcher( )
{
// open a file for writing
FILE *data = std::fopen("data.csv", "w+");
// write column headers into opened file
fprintf(data, "Month,Temperature (C),Precipitation (cm),Height (cm),Deer,Meteor Destruction\n");
float tempC;
float heightCM;
float precipCM;
int month;
float godsRageMeter;
float meteorProb;
while ( NowYear < 2027 )
{
// wait for other threads to finish computing
WaitBarrier( );
// wait for other threads to finish assigning variables
WaitBarrier( );
month = NowMonth + 12 * ( NowYear - 2021 );
tempC = ( 5. / 9. ) * ( NowTemp - 32 );
heightCM = 2.54 * NowHeight;
precipCM = 2.54 * NowPrecip;
meteorProb = NowMeteorProb;
// write monthly simulation results into opened file
// printf( "month: %d \t tempC: %.2f \t precipCM: %.2f \t heightCM: %.2f \t NowNumDeer: %d \t meteorDestruction: %d\n", month, tempC, precipCM, heightCM, NowNumDeer, NowMeteorDestruction );
fprintf( data, "%d,%.2f,%.2f,%.2f,%d,%d\n", month, tempC, precipCM, heightCM, NowNumDeer, NowMeteorDestruction );
// increment time
if ( ++NowMonth > 11 )
{
NowMonth = 0.;
NowYear++;
}
// reset the occurence of a destructive meteor shower back to 0 (False)
if ( NowMeteorDestruction == 1 )
{
NowMeteorDestruction = 0;
}
// if the probability of a meteor shower entering the earth's atmopsphere is 1,
// determine if it is destructive
if ( meteorProb == 1. )
{
set_meteor_destruction_imminence( );
}
// recalculate environmental and external factors
set_temp( &seed );
set_precip( &seed );
set_meteor_prob( &seed );
AreGodsAngry( &seed );
// done printing
WaitBarrier( );
}
// close out the file
std::fclose(data);
}