-
Notifications
You must be signed in to change notification settings - Fork 5
/
ST_colonization.c
279 lines (253 loc) · 8.58 KB
/
ST_colonization.c
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
/**
* \file ST_colonization.c
* \brief Function definitions and private variables for the
* [colonization](\ref COLONIZATION) module.
*
* \author Chandler Haukap
* \date January 2020
* \ingroup COLONIZATION_PRIVATE
*/
#include <stdlib.h>
#include "ST_colonization.h"
#include "sw_src/include/myMemory.h"
#include "sw_src/include/filefuncs.h"
#include "ST_functions.h"
#include "ST_globals.h"
#define MAX_COLONIZATION_EVENTS 50
/* --------------------------- Private Functions --------------------------- */
void _copyEvent(ColonizationEvent* dest, ColonizationEvent* src);
/**
* \brief All of the events this module will simulate, sorted by their start
* year.
*
* \ingroup COLONIZATION_PRIVATE
*/
ColonizationEvent* _allEvents = 0;
/**
* \brief The number of [events](\ref ColonizationEvent) in the
* \ref _allEvents array.
*
* \ingroup COLONIZATION_PRIVATE
*/
int _numberOfEvents = 0;
/**
* \brief The main function of the colonization module.
*
* This function will perform all colonization events, meaning if an event is
* supposed to occur in the given year it will provides seeds for the requested
* species in the requested cell.
*
* \param year is the current year. Year is input as a parameter, rather than
* infered from a cell, to make sure that there is no descrepency
* between the current year of any [cell](\ref CellType).
*
* \return TRUE if colonization occurred in any cell.
* \return FALSE if nothing colonized in any cell.
*
* \sideeffect
* If the species was turned off this function will also turn it on.
*
* \author Chandler Haukap
* \date January 2020
* \ingroup COLONIZATION
*/
Bool colonize(int year) {
Bool somethingColonized = FALSE;
ColonizationEvent* event;
int i = 0, cell;
// While the startYear is earlier than the current year.
while(i < _numberOfEvents && _allEvents[i].startYear <= year) {
// If the event is occurring this year
if(_allEvents[i].startYear + _allEvents[i].duration > year) {
event = &_allEvents[i];
for(cell = event->fromCell; cell <= event->toCell; ++cell){
load_cell(cell / grid_Cols, cell % grid_Cols);
// Setting both of these things to TRUE should ensure the species has a
// chance to establish.
Species[event->species]->seedsPresent = TRUE;
Species[event->species]->use_me = TRUE;
Species[event->species]->use_dispersal = TRUE;
unload_cell();
}
somethingColonized = TRUE;
}
++i;
}
return somethingColonized;
}
/**
* \brief Initialize the colonization module from a file.
*
* This function will allocate memory for every
* [colonization event](\ref ColonizationEvent) the user specified in the input
* file.
*
* It will then populate the \ref _allEvents array with the events, sorted from
* earliest startYear to latest.
*
* \param fileName The name of the file to open. This can be a relative path
* or an absolute path.
*
* \author Chandler Haukap
* \date January 2020
* \ingroup COLONIZATION
*/
void initColonization(char* fileName) {
char inbuf[128];
char name[5];
int valuesRead;
int fromCell, toCell;
int startYear;
int duration;
int i, j;
_numberOfEvents = 0;
// Open the file.
FILE* file = fopen(fileName, "r");
if(!file) {
LogError(&LogInfo, LOGERROR, "Error in Colonization module:"
"\n\tCannot open input file %s", fileName);
}
// A temporary variable to store colonization events
ColonizationEvent* tempEvents = Mem_Calloc(MAX_COLONIZATION_EVENTS,
sizeof(ColonizationEvent),
"initColonization", &LogInfo);
// Throw away the header
GetALine(file, inbuf, 128);
// Read the entire file.
while(GetALine(file, inbuf, 128)) {
// Assume the event includes multiple cells.
valuesRead = sscanf(inbuf, "%d-%d,%d,%d,%s", &fromCell, &toCell,&startYear,
&duration, name);
if(valuesRead == 5) {
// if fromCell > toCell, we will swap them. For example "8-4" --> "4-8".
if(toCell < fromCell) {
int temp = toCell;
toCell = fromCell;
fromCell = temp;
}
} else {
// If the event only occurs in a single cell.
valuesRead = sscanf(inbuf, "%d,%d,%d,%s", &fromCell, &startYear,
&duration, name);
// -1 means all cells.
if(fromCell == -1){
fromCell = 0;
toCell = (grid_Rows * grid_Cols) - 1;
// Otherwise this event must occur in only 1 cell.
} else {
toCell = fromCell;
}
}
/* ----------------------- Input Validation ------------------------ */
if(_numberOfEvents >= MAX_COLONIZATION_EVENTS) {
LogError(&LogInfo, LOGWARN, "Error reading colonization file:"
"A maximum of %d colonization events can be specified. "
"The rest of the events will be ignored.",
MAX_COLONIZATION_EVENTS);
break;
}
if(valuesRead != 4 && valuesRead != 5) {
free(tempEvents);
LogError(&LogInfo, LOGERROR, "Error reading colonization file:\n\tIncorrect"
" number of input arguments.\n\tIs it possible you put a space"
" somewhere? Remember this file cannot contain spaces.");
return;
}
if(fromCell < 0 || toCell > ((grid_Rows * grid_Cols) - 1)) {
free(tempEvents);
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tInvalid cell range ( %d - %d ) specified.", fromCell, toCell);
return;
}
if(startYear < 1 || startYear > SuperGlobals.runModelYears) {
free(tempEvents);
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tInvalid start year (%d) specified.", startYear);
return;
}
// For the species info we need to have a cell loaded.
load_cell(toCell / grid_Cols, toCell % grid_Cols);
if(Species_Name2Index(name) == -1) {
free(tempEvents);
LogError(&LogInfo, LOGERROR, "Error reading colonization file:"
"\n\tUnrecognized species name (%s).", name);
return;
}
/* ------------- Add the event to our temporary array -------------- */
tempEvents[_numberOfEvents].fromCell = fromCell;
tempEvents[_numberOfEvents].toCell = toCell;
tempEvents[_numberOfEvents].duration = duration;
tempEvents[_numberOfEvents].species = Species_Name2Index(name);
tempEvents[_numberOfEvents].startYear = startYear;
_numberOfEvents++;
unload_cell();
}
// Ensure we never leak memory
if(_allEvents) {
free(_allEvents);
_allEvents = 0;
}
// If there were no events specified we are done.
if(_numberOfEvents == 0) {
free(tempEvents);
fclose(file);
return;
}
/* ------------ Allocate and assign the file level variables ----------- */
// This algorithm sorts the events by startYear as is copies them over.
// Sorting the entries now will save time later.
int lowestYear, lowestYearIndex;
_allEvents = Mem_Calloc(_numberOfEvents, sizeof(ColonizationEvent),
"initColonization", &LogInfo);
for(i = 0; i < _numberOfEvents; ++i) {
lowestYear = 0;
lowestYearIndex = -1;
// Find the lowest start year.
for(j = 0; j < _numberOfEvents; ++j){
// (If this is the lowest start year we've seen) && it hasn't been used.
if((tempEvents[j].startYear < lowestYear || lowestYearIndex == -1) &&
tempEvents[j].startYear != -1) {
lowestYear = tempEvents[j].startYear;
lowestYearIndex = j;
}
}
_copyEvent(&_allEvents[i], &tempEvents[lowestYearIndex]);
// Mark this event as processed.
tempEvents[lowestYearIndex].startYear = -1;
}
// Free up the memory we used.
free(tempEvents);
fclose(file);
}
/**
* \brief Free this module's memory
*
* This function is safe to call more than once, but it only needs to be called
* once per simulation.
*
* \author Chandler Haukap
* \date January 2020
* \ingroup COLONIZATION
*/
void freeColonizationMemory(void) {
if(_allEvents){
free(_allEvents);
_allEvents = 0;
}
}
/**
* \brief Copy one [event](\ref ColonizationEvent)'s information to an other.
*
* Note that both events MUST be allocated prior to calling this function.
*
* \author Chandler Haukap
* \date January 2020
* \ingroup COLONIZATION_PRIVATE
*/
void _copyEvent(ColonizationEvent* dest, ColonizationEvent* src) {
dest->fromCell = src->fromCell;
dest->toCell = src->toCell;
dest->duration = src->duration;
dest->species = src->species;
dest->startYear = src->startYear;
}