1 : |
agomez |
1 |
#include "ecosystem.h" |
2 : |
|
|
#include "maininfo.h" |
3 : |
|
|
#include "runid.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
#include "errorhandler.h" |
6 : |
|
|
#include "stochasticdata.h" |
7 : |
|
|
#include "interrupthandler.h" |
8 : |
|
|
#include "global.h" |
9 : |
ulcessvp |
16 |
#ifdef _OPENMP
|
10 : |
ulcessvp |
11 |
#include <omp.h>
|
11 : |
|
|
#endif
|
12 : |
agomez |
1 |
|
13 : |
|
|
Ecosystem* EcoSystem;
|
14 : |
ulcessvp |
11 |
StochasticData* data = 0;
|
15 : |
|
|
Ecosystem** EcoSystems;
|
16 : |
agomez |
1 |
|
17 : |
ulcessvp |
17 |
volatile int interrupted = 0;
|
18 : |
|
|
volatile int interrupted_print = 0;
|
19 : |
agomez |
1 |
|
20 : |
ulcessvp |
17 |
|
21 : |
agomez |
1 |
int main(int aNumber, char* const aVector[]) {
|
22 : |
|
|
|
23 : |
|
|
MainInfo main;
|
24 : |
ulcessvp |
11 |
|
25 : |
|
|
// omp_set_num_threads(4);
|
26 : |
|
|
|
27 : |
agomez |
1 |
int check = 0;
|
28 : |
|
|
|
29 : |
|
|
//Initialise random number generator with system time [MNAA 02.02.26]
|
30 : |
|
|
srand((int)time(NULL));
|
31 : |
|
|
|
32 : |
|
|
//Test to see if the function double lgamma(double) is returning an integer.
|
33 : |
|
|
//lgamma is a non-ansi function and on some platforms when compiled with the
|
34 : |
|
|
//-ansi flag lgamma returns an integer value. [MNAA&AJ 05.2001]
|
35 : |
|
|
assert(lgamma(1.2) != floor(lgamma(1.2)));
|
36 : |
|
|
|
37 : |
|
|
char* workingdir = getenv("GADGET_WORKING_DIR");
|
38 : |
|
|
if (workingdir == 0) {
|
39 : |
|
|
if ((workingdir = (char*)malloc(LongString)) == NULL)
|
40 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to malloc space for current working directory");
|
41 : |
|
|
check = 1;
|
42 : |
|
|
if (getcwd(workingdir, LongString) == NULL)
|
43 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to get current working directory");
|
44 : |
|
|
}
|
45 : |
|
|
if (chdir(workingdir) != 0)
|
46 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
|
47 : |
|
|
|
48 : |
|
|
char* inputdir = getenv("GADGET_DATA_DIR");
|
49 : |
|
|
if (inputdir == 0)
|
50 : |
|
|
inputdir = workingdir;
|
51 : |
|
|
if (chdir(inputdir) != 0)
|
52 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir);
|
53 : |
|
|
if (chdir(workingdir) != 0) //JMB change back to where we were ...
|
54 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
|
55 : |
|
|
|
56 : |
|
|
main.read(aNumber, aVector);
|
57 : |
|
|
main.checkUsage(inputdir, workingdir);
|
58 : |
|
|
|
59 : |
|
|
if (chdir(inputdir) != 0)
|
60 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir);
|
61 : |
|
|
EcoSystem = new Ecosystem(main);
|
62 : |
|
|
|
63 : |
|
|
#ifdef INTERRUPT_HANDLER
|
64 : |
|
|
//JMB dont register interrupt if doing a network run
|
65 : |
|
|
if (!(main.runNetwork()))
|
66 : |
ulcessvp |
17 |
registerInterrupts(&interrupted);
|
67 : |
agomez |
1 |
#endif
|
68 : |
|
|
|
69 : |
|
|
if (chdir(workingdir) != 0)
|
70 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
|
71 : |
|
|
if ((main.getPI()).getPrint())
|
72 : |
|
|
EcoSystem->writeInitialInformation((main.getPI()).getOutputFile());
|
73 : |
|
|
|
74 : |
|
|
if (main.runStochastic()) {
|
75 : |
|
|
if (main.runNetwork()) {
|
76 : |
|
|
#ifdef GADGET_NETWORK //to help compiling when pvm libraries are unavailable
|
77 : |
|
|
EcoSystem->Initialise();
|
78 : |
|
|
data = new StochasticData();
|
79 : |
|
|
while (data->getDataFromNetwork()) {
|
80 : |
|
|
EcoSystem->Update(data);
|
81 : |
|
|
EcoSystem->Simulate(main.runPrint());
|
82 : |
|
|
data->sendDataToNetwork(EcoSystem->getLikelihood());
|
83 : |
|
|
data->readNextLineFromNetwork();
|
84 : |
|
|
}
|
85 : |
|
|
delete data;
|
86 : |
|
|
#endif
|
87 : |
|
|
|
88 : |
|
|
} else if (main.getInitialParamGiven()) {
|
89 : |
|
|
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file
|
90 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir);
|
91 : |
|
|
data = new StochasticData(main.getInitialParamFile());
|
92 : |
|
|
if (chdir(workingdir) != 0)
|
93 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
|
94 : |
|
|
|
95 : |
|
|
EcoSystem->Update(data);
|
96 : |
|
|
EcoSystem->checkBounds();
|
97 : |
|
|
|
98 : |
|
|
EcoSystem->Initialise();
|
99 : |
|
|
if (main.printInitial()) {
|
100 : |
|
|
EcoSystem->Reset(); //JMB only need to call reset() before the print commands
|
101 : |
|
|
EcoSystem->writeStatus(main.getPrintInitialFile());
|
102 : |
|
|
}
|
103 : |
|
|
|
104 : |
|
|
EcoSystem->Simulate(main.runPrint());
|
105 : |
|
|
if ((main.getPI()).getPrint())
|
106 : |
|
|
EcoSystem->writeValues();
|
107 : |
|
|
|
108 : |
|
|
while (data->isDataLeft()) {
|
109 : |
|
|
data->readNextLine();
|
110 : |
|
|
EcoSystem->Update(data);
|
111 : |
|
|
EcoSystem->checkBounds();
|
112 : |
|
|
EcoSystem->Simulate(main.runPrint());
|
113 : |
|
|
if ((main.getPI()).getPrint())
|
114 : |
|
|
EcoSystem->writeValues();
|
115 : |
|
|
}
|
116 : |
|
|
delete data;
|
117 : |
|
|
|
118 : |
|
|
} else {
|
119 : |
|
|
if (EcoSystem->numVariables() != 0)
|
120 : |
|
|
handle.logMessage(LOGWARN, "Warning - no parameter input file given, using default values");
|
121 : |
|
|
|
122 : |
|
|
EcoSystem->Initialise();
|
123 : |
|
|
if (main.printInitial()) {
|
124 : |
|
|
EcoSystem->Reset(); //JMB only need to call reset() before the print commands
|
125 : |
|
|
EcoSystem->writeStatus(main.getPrintInitialFile());
|
126 : |
|
|
}
|
127 : |
|
|
|
128 : |
|
|
EcoSystem->Simulate(main.runPrint());
|
129 : |
|
|
if ((main.getPI()).getPrint())
|
130 : |
|
|
EcoSystem->writeValues();
|
131 : |
|
|
}
|
132 : |
|
|
|
133 : |
|
|
} else if (main.runOptimise()) {
|
134 : |
ulcessvp |
11 |
if (EcoSystem->numVariables() == 0)
|
135 : |
|
|
handle.logMessage(LOGFAIL, "Error - no parameters can be optimised");
|
136 : |
agomez |
1 |
|
137 : |
ulcessvp |
11 |
if (main.getInitialParamGiven()) {
|
138 : |
|
|
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file
|
139 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir);
|
140 : |
|
|
data = new StochasticData(main.getInitialParamFile());
|
141 : |
|
|
if (chdir(workingdir) != 0)
|
142 : |
|
|
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
|
143 : |
agomez |
1 |
|
144 : |
ulcessvp |
11 |
EcoSystem->Update(data);
|
145 : |
|
|
EcoSystem->checkBounds();
|
146 : |
|
|
} else
|
147 : |
|
|
handle.logMessage(LOGFAIL, "Error - no parameter input file specified");
|
148 : |
agomez |
1 |
|
149 : |
ulcessvp |
11 |
EcoSystem->Initialise();
|
150 : |
|
|
if (main.printInitial()) {
|
151 : |
|
|
EcoSystem->Reset(); //JMB only need to call reset() before the print commands
|
152 : |
|
|
EcoSystem->writeStatus(main.getPrintInitialFile());
|
153 : |
|
|
}
|
154 : |
agomez |
1 |
|
155 : |
ulcessvp |
16 |
#ifdef _OPENMP
|
156 : |
agomez |
20 |
if (main.runParallel()){
|
157 : |
|
|
int numThr = omp_get_max_threads ( );
|
158 : |
|
|
handle.logMessage(LOGINFO, "Info - initialise ", numThr," EcoSystems ");
|
159 : |
|
|
EcoSystems = new Ecosystem*[numThr];
|
160 : |
|
|
int i;
|
161 : |
|
|
#pragma omp parallel for shared(EcoSystems,main,data,handle)
|
162 : |
|
|
for (i=0; i<numThr; i++)
|
163 : |
|
|
{
|
164 : |
|
|
handle.logMessage(LOGINFO, "Info - Ecosystem ", i);
|
165 : |
|
|
if (main.getInitialParamGiven()) {
|
166 : |
|
|
EcoSystems[i] = new Ecosystem(main);
|
167 : |
|
|
EcoSystems[i]->Update(data);
|
168 : |
|
|
EcoSystems[i]->checkBounds();
|
169 : |
|
|
}
|
170 : |
|
|
EcoSystems[i]->Initialise();
|
171 : |
|
|
}
|
172 : |
|
|
}
|
173 : |
ulcessvp |
12 |
#endif
|
174 : |
|
|
EcoSystem->Optimise();
|
175 : |
ulcessvp |
11 |
delete data;
|
176 : |
|
|
if (main.getForcePrint())
|
177 : |
|
|
EcoSystem->Simulate(main.getForcePrint());
|
178 : |
agomez |
1 |
}
|
179 : |
|
|
|
180 : |
|
|
handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
|
181 : |
|
|
if (main.printFinal() && !(main.runNetwork()))
|
182 : |
|
|
EcoSystem->writeStatus(main.getPrintFinalFile());
|
183 : |
|
|
|
184 : |
|
|
//JMB print final values of parameters
|
185 : |
|
|
if (!(main.runNetwork()))
|
186 : |
|
|
EcoSystem->writeParams((main.getPI()).getParamOutFile(), (main.getPI()).getPrecision());
|
187 : |
|
|
|
188 : |
|
|
if (check)
|
189 : |
|
|
free(workingdir);
|
190 : |
|
|
|
191 : |
|
|
delete EcoSystem;
|
192 : |
|
|
handle.logFinish();
|
193 : |
|
|
return EXIT_SUCCESS;
|
194 : |
|
|
}
|