Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/ecosystem.cc
[mareframe] / trunk / gadget / ecosystem.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/ecosystem.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (view) (download)

1 : agomez 1 #include "ecosystem.h"
2 :     #include "runid.h"
3 :     #include "global.h"
4 : ulcessvp 19 #include <ctime>
5 : ulcessvp 16 #ifdef _OPENMP
6 : ulcessvp 11 #include <omp.h>
7 :     #endif
8 : agomez 1
9 : ulcessvp 16 #ifdef _OPENMP
10 : ulcessvp 11 extern Ecosystem** EcoSystems;
11 :     //extern StochasticData* data;
12 :     #endif
13 : ulcessvp 17 extern volatile int interrupted_print;
14 : ulcessvp 11
15 : ulcessvp 19 time_t starttime, stoptime;
16 :    
17 : agomez 1 Ecosystem::Ecosystem(const MainInfo& main) : printinfo(main.getPI()) {
18 :    
19 :     funceval = 0;
20 : ulcessvp 17 //interrupted = 0;
21 : agomez 1 likelihood = 0.0;
22 :     keeper = new Keeper;
23 :    
24 :     // initialise counter used when printing output files
25 :     printcount = printinfo.getPrintIteration() - 1;
26 :    
27 :     // read the model specification from the main file
28 :     char* filename = main.getMainGadgetFile();
29 :     ifstream infile;
30 :     infile.open(filename, ios::in);
31 :     CommentStream commin(infile);
32 :     handle.checkIfFailure(infile, filename);
33 :     handle.Open(filename);
34 :     this->readMain(commin, main);
35 :     handle.Close();
36 :     infile.close();
37 :     infile.clear();
38 :    
39 :     // if this is an optimising run then read the optimisation parameters from file
40 :     if (main.runOptimise()) {
41 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
42 :     if (main.getOptInfoGiven()) {
43 :     filename = main.getOptInfoFile();
44 :     infile.open(filename, ios::in);
45 :     handle.checkIfFailure(infile, filename);
46 :     handle.Open(filename);
47 : ulcessvp 11 unsigned seed;
48 :     main.getForcePrint();
49 :     this->readOptimisation(commin, main.getSeed());
50 : agomez 1 handle.Close();
51 :     infile.close();
52 :     infile.clear();
53 :     } else {
54 :     handle.logMessage(LOGINFO, "Warning - no optimisation file specified, using default values");
55 :     optvec.resize(new OptInfoHooke());
56 :     }
57 :     }
58 : agomez 20 #ifdef _OPENMP
59 :     runParallel=main.runParallel();
60 :     if (!omp_in_parallel()){
61 :     if (main.runOptimise())
62 :     {
63 :     switch(runParallel){
64 :     case SPECULATIVE:
65 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run parallel speculative optimisation with ", omp_get_max_threads(), " threads");
66 :     break;
67 :     case REPRODUCIBLE:
68 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run parallel reproducible optimisation with ", omp_get_max_threads(), " threads");
69 :     break;
70 :     default:
71 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to serial optimisation with ", omp_get_num_threads() , "threads");
72 :     }
73 :     }
74 :     else
75 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run simulation ", omp_get_num_threads(), " threads");
76 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
77 :     }
78 :     #else
79 : agomez 1 if (main.runOptimise())
80 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run optimisation");
81 :     else
82 :     handle.logMessage(LOGINFO, "\nFinished reading model data files, starting to run simulation");
83 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
84 : agomez 20 #endif
85 : agomez 1 }
86 :    
87 :     Ecosystem::~Ecosystem() {
88 :     int i;
89 :     for (i = 0; i < optvec.Size(); i++)
90 :     delete optvec[i];
91 :     for (i = 0; i < printvec.Size(); i++)
92 :     delete printvec[i];
93 :     for (i = 0; i < likevec.Size(); i++)
94 :     delete likevec[i];
95 :     for (i = 0; i < tagvec.Size(); i++)
96 :     delete tagvec[i];
97 :     for (i = 0; i < basevec.Size(); i++)
98 :     delete basevec[i];
99 :    
100 :     delete Area;
101 :     delete TimeInfo;
102 :     delete keeper;
103 :     }
104 :    
105 :     void Ecosystem::writeStatus(const char* filename) const {
106 :     ofstream outfile;
107 :     outfile.open(filename, ios::out);
108 :     handle.checkIfFailure(outfile, filename);
109 :     handle.Open(filename);
110 :     RUNID.Print(outfile);
111 :     outfile << "The current simulation time is " << TimeInfo->getYear()
112 :     << ", step " << TimeInfo->getStep() << endl;
113 :    
114 :     int i;
115 :     for (i = 0; i < basevec.Size(); i++)
116 :     basevec[i]->Print(outfile);
117 :     for (i = 0; i < likevec.Size(); i++)
118 :     likevec[i]->Print(outfile);
119 :     for (i = 0; i < tagvec.Size(); i++)
120 :     tagvec[i]->Print(outfile);
121 :    
122 :     handle.Close();
123 :     outfile.close();
124 :     outfile.clear();
125 :     }
126 :    
127 :     void Ecosystem::Reset() {
128 :     int i;
129 :     TimeInfo->Reset();
130 :     for (i = 0; i < likevec.Size(); i++)
131 :     likevec[i]->Reset(keeper);
132 :     for (i = 0; i < basevec.Size(); i++)
133 :     basevec[i]->Reset(TimeInfo);
134 :     }
135 :    
136 :     void Ecosystem::Optimise() {
137 :     int i;
138 :     for (i = 0; i < optvec.Size(); i++) {
139 : ulcessvp 19 time(&starttime);
140 : ulcessvp 16 #ifdef _OPENMP
141 : agomez 20 if (runParallel){
142 : ulcessvp 24 int j, numThr = omp_get_max_threads ( ); //AGT REMOVED
143 :     DoubleVector v = this->getValues();
144 :     for (j = 0; j < numThr; j++)
145 :     EcoSystems[j]->Update(v);
146 : agomez 20 if(runParallel==SPECULATIVE)
147 :     {
148 :     handle.logMessage(LOGINFO, "\nCalling Speculative Optimization");
149 :     optvec[i]->OptimiseLikelihoodOMP();
150 :     }
151 :     else
152 :     {
153 :     handle.logMessage(LOGINFO, "\nCalling Reproducible parallel Optimization");
154 :     optvec[i]->OptimiseLikelihoodREP();
155 :     }
156 :     }
157 :     else
158 :     {
159 :     handle.logMessage(LOGINFO, "\nCalling Reproducible serial Optimization");
160 :     optvec[i]->OptimiseLikelihood();
161 :     }
162 : ulcessvp 12 #else
163 : agomez 20 //#ifdef SPECULATIVE
164 :     // optvec[i]->OptimiseLikelihoodOMP();
165 :     //#else
166 : ulcessvp 12 optvec[i]->OptimiseLikelihood();
167 :     #endif
168 : ulcessvp 19 time(&stoptime);
169 : ulcessvp 11 this->writeOptValues();
170 :     }
171 :     }
172 :    
173 : agomez 1 double Ecosystem::SimulateAndUpdate(const DoubleVector& x) {
174 :     int i, j;
175 :    
176 :     if (funceval == 0) {
177 :     // JMB - only need to create these vectors once
178 :     initialval.resize(keeper->numVariables(), 0.0);
179 :     currentval.resize(keeper->numVariables(), 0.0);
180 :     optflag.resize(keeper->numVariables(), 0);
181 :     keeper->getOptFlags(optflag);
182 : agomez 20
183 : agomez 1 }
184 :    
185 :     j = 0;
186 :     keeper->getCurrentValues(currentval);
187 :     keeper->getInitialValues(initialval);
188 :     for (i = 0; i < currentval.Size(); i++) {
189 :     if (optflag[i]) {
190 :     currentval[i] = x[j] * initialval[i];
191 :     j++;
192 :     }
193 :     }
194 :    
195 :     keeper->Update(currentval);
196 :     this->Simulate(0); //dont print whilst optimising
197 :    
198 :     if (printinfo.getPrint()) {
199 :     printcount++;
200 :     if (printcount == printinfo.getPrintIteration()) {
201 :     keeper->writeValues(likevec, printinfo.getPrecision());
202 :     printcount = 0;
203 :     }
204 :     }
205 :    
206 :     funceval++;
207 :     return likelihood;
208 :     }
209 :    
210 :     void Ecosystem::writeOptValues() {
211 :     int i;
212 :     DoubleVector tmpvec(likevec.Size(), 0.0);
213 :     for (i = 0; i < likevec.Size(); i++)
214 :     tmpvec[i] = likevec[i]->getUnweightedLikelihood();
215 :    
216 : ulcessvp 11 int iters = 0;
217 : ulcessvp 16 #ifdef _OPENMP
218 : agomez 20 if (runParallel){
219 :     int numThr = omp_get_max_threads ( );
220 :     for (i = 0; i < numThr; i++)
221 : ulcessvp 11 iters += EcoSystems[i]->getFuncEval();
222 : agomez 20 }
223 : ulcessvp 11 #endif
224 :     handle.logMessage(LOGINFO, "\nAfter a total of", funceval+iters, "function evaluations the best point found is");
225 : agomez 1 keeper->writeBestValues();
226 :     handle.logMessage(LOGINFO, "\nThe scores from each likelihood component are");
227 :     handle.logMessage(LOGINFO, tmpvec);
228 :     if (!isZero(keeper->getBestLikelihoodScore())) // no better point has been found
229 :     handle.logMessage(LOGINFO, "\nThe overall likelihood score is", keeper->getBestLikelihoodScore());
230 :     else
231 :     handle.logMessage(LOGINFO, "\nThe overall likelihood score is", this->getLikelihood());
232 : ulcessvp 19
233 :     handle.logMessage(LOGINFO, "\n Runtime for the algorithm was ", difftime(stoptime, starttime)," seconds\n");
234 : agomez 1 }
235 :    
236 :     void Ecosystem::writeInitialInformation(const char* const filename) {
237 :     keeper->openPrintFile(filename);
238 :     keeper->writeInitialInformation(likevec);
239 :     }
240 :    
241 :     void Ecosystem::writeValues() {
242 :     keeper->writeValues(likevec, printinfo.getPrecision());
243 :     }
244 :    
245 :     void Ecosystem::writeParams(const char* const filename, int prec) const {
246 : ulcessvp 17 if ((funceval > 0) && (interrupted_print == 0)) {
247 : agomez 1 //JMB - print the final values to any output files specified
248 :     //in case they have been missed by the -print value
249 :     if (printinfo.getPrint())
250 :     keeper->writeValues(likevec, printinfo.getPrecision());
251 :     }
252 : ulcessvp 17 keeper->writeParams(optvec, filename, prec, interrupted_print);
253 : agomez 1 }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge