13 |
Ecosystem* EcoSystem; |
Ecosystem* EcoSystem; |
14 |
StochasticData* data = 0; |
StochasticData* data = 0; |
15 |
Ecosystem** EcoSystems; |
Ecosystem** EcoSystems; |
16 |
|
MainInfo _main; |
17 |
|
|
18 |
volatile int interrupted = 0; |
volatile int interrupted = 0; |
19 |
volatile int interrupted_print = 0; |
volatile int interrupted_print = 0; |
21 |
|
|
22 |
int main(int aNumber, char* const aVector[]) { |
int main(int aNumber, char* const aVector[]) { |
23 |
|
|
24 |
MainInfo main; |
|
25 |
|
|
26 |
// omp_set_num_threads(4); |
// omp_set_num_threads(4); |
27 |
|
|
54 |
if (chdir(workingdir) != 0) //JMB change back to where we were ... |
if (chdir(workingdir) != 0) //JMB change back to where we were ... |
55 |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
56 |
|
|
57 |
main.read(aNumber, aVector); |
_main.read(aNumber, aVector); |
58 |
main.checkUsage(inputdir, workingdir); |
_main.checkUsage(inputdir, workingdir); |
59 |
|
|
60 |
if (chdir(inputdir) != 0) |
if (chdir(inputdir) != 0) |
61 |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
62 |
EcoSystem = new Ecosystem(main); |
EcoSystem = new Ecosystem(_main); |
63 |
|
|
64 |
#ifdef INTERRUPT_HANDLER |
#ifdef INTERRUPT_HANDLER |
65 |
//JMB dont register interrupt if doing a network run |
//JMB dont register interrupt if doing a network run |
66 |
if (!(main.runNetwork())) |
if (!(_main.runNetwork())) |
67 |
registerInterrupts(&interrupted); |
registerInterrupts(&interrupted); |
68 |
#endif |
#endif |
69 |
|
|
70 |
if (chdir(workingdir) != 0) |
if (chdir(workingdir) != 0) |
71 |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
72 |
if ((main.getPI()).getPrint()) |
if ((_main.getPI()).getPrint()) |
73 |
EcoSystem->writeInitialInformation((main.getPI()).getOutputFile()); |
EcoSystem->writeInitialInformation((_main.getPI()).getOutputFile()); |
74 |
|
|
75 |
if (main.runStochastic()) { |
if (_main.runStochastic()) { |
76 |
if (main.runNetwork()) { |
if (_main.runNetwork()) { |
77 |
#ifdef GADGET_NETWORK //to help compiling when pvm libraries are unavailable |
#ifdef GADGET_NETWORK //to help compiling when pvm libraries are unavailable |
78 |
EcoSystem->Initialise(); |
EcoSystem->Initialise(); |
79 |
data = new StochasticData(); |
data = new StochasticData(); |
80 |
while (data->getDataFromNetwork()) { |
while (data->getDataFromNetwork()) { |
81 |
EcoSystem->Update(data); |
EcoSystem->Update(data); |
82 |
EcoSystem->Simulate(main.runPrint()); |
EcoSystem->Simulate(_main.runPrint()); |
83 |
data->sendDataToNetwork(EcoSystem->getLikelihood()); |
data->sendDataToNetwork(EcoSystem->getLikelihood()); |
84 |
data->readNextLineFromNetwork(); |
data->readNextLineFromNetwork(); |
85 |
} |
} |
86 |
delete data; |
delete data; |
87 |
#endif |
#endif |
88 |
|
|
89 |
} else if (main.getInitialParamGiven()) { |
} else if (_main.getInitialParamGiven()) { |
90 |
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file |
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file |
91 |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
92 |
data = new StochasticData(main.getInitialParamFile()); |
data = new StochasticData(_main.getInitialParamFile()); |
93 |
if (chdir(workingdir) != 0) |
if (chdir(workingdir) != 0) |
94 |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
95 |
|
|
97 |
EcoSystem->checkBounds(); |
EcoSystem->checkBounds(); |
98 |
|
|
99 |
EcoSystem->Initialise(); |
EcoSystem->Initialise(); |
100 |
if (main.printInitial()) { |
if (_main.printInitial()) { |
101 |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
102 |
EcoSystem->writeStatus(main.getPrintInitialFile()); |
EcoSystem->writeStatus(_main.getPrintInitialFile()); |
103 |
} |
} |
104 |
|
|
105 |
EcoSystem->Simulate(main.runPrint()); |
EcoSystem->Simulate(_main.runPrint()); |
106 |
if ((main.getPI()).getPrint()) |
if ((_main.getPI()).getPrint()) |
107 |
EcoSystem->writeValues(); |
EcoSystem->writeValues(); |
108 |
|
|
109 |
while (data->isDataLeft()) { |
while (data->isDataLeft()) { |
110 |
data->readNextLine(); |
data->readNextLine(); |
111 |
EcoSystem->Update(data); |
EcoSystem->Update(data); |
112 |
EcoSystem->checkBounds(); |
EcoSystem->checkBounds(); |
113 |
EcoSystem->Simulate(main.runPrint()); |
EcoSystem->Simulate(_main.runPrint()); |
114 |
if ((main.getPI()).getPrint()) |
if ((_main.getPI()).getPrint()) |
115 |
EcoSystem->writeValues(); |
EcoSystem->writeValues(); |
116 |
} |
} |
117 |
delete data; |
delete data; |
121 |
handle.logMessage(LOGWARN, "Warning - no parameter input file given, using default values"); |
handle.logMessage(LOGWARN, "Warning - no parameter input file given, using default values"); |
122 |
|
|
123 |
EcoSystem->Initialise(); |
EcoSystem->Initialise(); |
124 |
if (main.printInitial()) { |
if (_main.printInitial()) { |
125 |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
126 |
EcoSystem->writeStatus(main.getPrintInitialFile()); |
EcoSystem->writeStatus(_main.getPrintInitialFile()); |
127 |
} |
} |
128 |
|
|
129 |
EcoSystem->Simulate(main.runPrint()); |
EcoSystem->Simulate(_main.runPrint()); |
130 |
if ((main.getPI()).getPrint()) |
if ((_main.getPI()).getPrint()) |
131 |
EcoSystem->writeValues(); |
EcoSystem->writeValues(); |
132 |
} |
} |
133 |
|
|
134 |
} else if (main.runOptimise()) { |
} else if (_main.runOptimise()) { |
135 |
if (EcoSystem->numVariables() == 0) |
if (EcoSystem->numVariables() == 0) |
136 |
handle.logMessage(LOGFAIL, "Error - no parameters can be optimised"); |
handle.logMessage(LOGFAIL, "Error - no parameters can be optimised"); |
137 |
|
|
138 |
if (main.getInitialParamGiven()) { |
if (_main.getInitialParamGiven()) { |
139 |
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file |
if (chdir(inputdir) != 0) //JMB need to change back to inputdir to read the file |
140 |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir); |
141 |
data = new StochasticData(main.getInitialParamFile()); |
data = new StochasticData(_main.getInitialParamFile()); |
142 |
if (chdir(workingdir) != 0) |
if (chdir(workingdir) != 0) |
143 |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir); |
144 |
|
|
148 |
handle.logMessage(LOGFAIL, "Error - no parameter input file specified"); |
handle.logMessage(LOGFAIL, "Error - no parameter input file specified"); |
149 |
|
|
150 |
EcoSystem->Initialise(); |
EcoSystem->Initialise(); |
151 |
if (main.printInitial()) { |
if (_main.printInitial()) { |
152 |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
EcoSystem->Reset(); //JMB only need to call reset() before the print commands |
153 |
EcoSystem->writeStatus(main.getPrintInitialFile()); |
EcoSystem->writeStatus(_main.getPrintInitialFile()); |
154 |
} |
} |
155 |
|
|
156 |
#ifdef _OPENMP |
#ifdef _OPENMP |
157 |
if (main.runParallel()){ |
if (_main.runParallel()){ |
158 |
int numThr = omp_get_max_threads ( ); |
int numThr = omp_get_max_threads ( ); |
159 |
handle.logMessage(LOGINFO, "Info - initialise ", numThr," EcoSystems "); |
handle.logMessage(LOGINFO, "Info - initialise ", numThr," EcoSystems "); |
160 |
EcoSystems = new Ecosystem*[numThr]; |
EcoSystems = new Ecosystem*[numThr]; |
161 |
int i; |
int i; |
162 |
#pragma omp parallel for shared(EcoSystems,main,data,handle) |
#pragma omp parallel for shared(EcoSystems,_main,data,handle) |
163 |
for (i=0; i<numThr; i++) |
for (i=0; i<numThr; i++) |
164 |
{ |
{ |
165 |
handle.logMessage(LOGINFO, "Info - Ecosystem ", i); |
handle.logMessage(LOGINFO, "Info - Ecosystem ", i); |
166 |
if (main.getInitialParamGiven()) { |
if (_main.getInitialParamGiven()) { |
167 |
EcoSystems[i] = new Ecosystem(main); |
EcoSystems[i] = new Ecosystem(_main); |
168 |
EcoSystems[i]->Update(data); |
EcoSystems[i]->Update(data); |
169 |
EcoSystems[i]->checkBounds(); |
EcoSystems[i]->checkBounds(); |
170 |
} |
} |
174 |
#endif |
#endif |
175 |
EcoSystem->Optimise(); |
EcoSystem->Optimise(); |
176 |
delete data; |
delete data; |
177 |
if (main.getForcePrint()) |
if (_main.getForcePrint()) |
178 |
EcoSystem->Simulate(main.getForcePrint()); |
EcoSystem->Simulate(_main.getForcePrint()); |
179 |
} |
} |
180 |
|
|
181 |
handle.logMessage(LOGMESSAGE, ""); //write blank line to log file |
handle.logMessage(LOGMESSAGE, ""); //write blank line to log file |
182 |
if (main.printFinal() && !(main.runNetwork())) |
if (_main.printFinal() && !(_main.runNetwork())) |
183 |
EcoSystem->writeStatus(main.getPrintFinalFile()); |
EcoSystem->writeStatus(_main.getPrintFinalFile()); |
184 |
|
|
185 |
//JMB print final values of parameters |
//JMB print final values of parameters |
186 |
if (!(main.runNetwork())) |
if (!(_main.runNetwork())) |
187 |
EcoSystem->writeParams((main.getPI()).getParamOutFile(), (main.getPI()).getPrecision()); |
EcoSystem->writeParams((_main.getPI()).getParamOutFile(), (_main.getPI()).getPrecision()); |
188 |
|
|
189 |
if (check) |
if (check) |
190 |
free(workingdir); |
free(workingdir); |