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/maininfo.cc
[mareframe] / trunk / gadget / maininfo.cc Repository:
ViewVC logotype

Annotation of /trunk/gadget/maininfo.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : agomez 1 #include "maininfo.h"
2 :     #include "errorhandler.h"
3 :     #include "gadget.h"
4 :     #include "runid.h"
5 :     #include "global.h"
6 :    
7 :     void MainInfo::showCorrectUsage(char* error) {
8 :     RUNID.Print(cerr);
9 :     cerr << "\nError in command line value - unrecognised option " << error << endl
10 :     << "Common options are -l or -s, -i <filename> -o <filename>\n"
11 :     << "For more information try running Gadget with the -h switch\n";
12 :     exit(EXIT_FAILURE);
13 :     }
14 :    
15 :     void MainInfo::showUsage() {
16 :     RUNID.Print(cout);
17 :     cout << "\nOptions for running Gadget:\n"
18 :     << " -l perform a likelihood (optimising) model run\n"
19 :     << " -s perform a single (simulation) model run\n"
20 :     << " -n perform a network run (using paramin)\n"
21 :     << " -v --version display version information and exit\n"
22 :     << " -h --help display this help screen and exit\n"
23 :     << "\nOptions for specifying the input to Gadget models:\n"
24 :     << " -i <filename> read model parameters from <filename>\n"
25 :     << " -opt <filename> read optimising parameters from <filename>\n"
26 :     << " -main <filename> read model information from <filename>\n"
27 :     << " (default filename is 'main')\n"
28 :     << " -m <filename> read other commandline parameters from <filename>\n"
29 :     << "\nOptions for specifying the output from Gadget models:\n"
30 :     << " -p <filename> print final model parameters to <filename>\n"
31 :     << " (default filename is 'params.out')\n"
32 :     << " -o <filename> print likelihood output to <filename>\n"
33 :     << " -print <number> print -o output every <number> iterations\n"
34 :     << " -precision <number> set the precision to <number> in output files\n"
35 :     << "\nOptions for debugging Gadget models:\n"
36 :     << " -log <filename> print logging information to <filename>\n"
37 :     << " -printinitial <filename> print initial model information to <filename>\n"
38 :     << " -printfinal <filename> print final model information to <filename>\n"
39 :     << "\nFor more information see the Gadget web page at http://www.hafro.is/gadget\n\n";
40 :     exit(EXIT_SUCCESS);
41 :     }
42 :    
43 :     MainInfo::MainInfo()
44 :     : givenOptInfo(0), givenInitialParam(0), runoptimise(0),
45 :     runstochastic(0), runnetwork(0), runprint(1), forceprint(0),
46 :     printInitialInfo(0), printFinalInfo(0), printLogLevel(0), maxratio(0.95) {
47 :    
48 :     char tmpname[10];
49 :     strncpy(tmpname, "", 10);
50 :     strcpy(tmpname, "main");
51 :    
52 :     strOptInfoFile = NULL;
53 :     strInitialParamFile = NULL;
54 :     strPrintInitialFile = NULL;
55 :     strPrintFinalFile = NULL;
56 :     strMainGadgetFile = NULL;
57 :     setMainGadgetFile(tmpname);
58 :     }
59 :    
60 :     MainInfo::~MainInfo() {
61 :     if (strOptInfoFile != NULL) {
62 :     delete[] strOptInfoFile;
63 :     strOptInfoFile = NULL;
64 :     }
65 :     if (strInitialParamFile != NULL) {
66 :     delete[] strInitialParamFile;
67 :     strInitialParamFile = NULL;
68 :     }
69 :     if (strPrintInitialFile != NULL) {
70 :     delete[] strPrintInitialFile;
71 :     strPrintInitialFile = NULL;
72 :     }
73 :     if (strPrintFinalFile != NULL) {
74 :     delete[] strPrintFinalFile;
75 :     strPrintFinalFile = NULL;
76 :     }
77 :     if (strMainGadgetFile != NULL) {
78 :     delete[] strMainGadgetFile;
79 :     strMainGadgetFile = NULL;
80 :     }
81 :     }
82 :    
83 :     void MainInfo::read(int aNumber, char* const aVector[]) {
84 :    
85 :     if (aNumber == 1) {
86 :     handle.logMessage(LOGWARN, "Warning - no command line options specified, using default values");
87 :     return;
88 :     }
89 :    
90 :     int k = 1;
91 :     while (k < aNumber) {
92 :     if (strcasecmp(aVector[k], "-l") == 0) {
93 :     runoptimise = 1;
94 :    
95 :     } else if (strcasecmp(aVector[k], "-n") == 0) {
96 :     runnetwork = 1;
97 :    
98 :     #ifndef GADGET_NETWORK
99 :     handle.logMessage(LOGFAIL, "Error - Gadget cannot currently run in network mode for paramin\nGadget must be recompiled to enable the network communication");
100 :     #endif
101 :    
102 :     } else if (strcasecmp(aVector[k], "-s") == 0) {
103 :     runstochastic = 1;
104 :    
105 :     } else if (strcasecmp(aVector[k], "-m") == 0) {
106 :     ifstream infile;
107 :     CommentStream incomment(infile);
108 :     if (k == aNumber - 1)
109 :     this->showCorrectUsage(aVector[k]);
110 :     k++;
111 :     infile.open(aVector[k], ios::in);
112 :     handle.checkIfFailure(infile, aVector[k]);
113 :     if (infile.fail())
114 :     this->showCorrectUsage(aVector[k]);
115 :     this->read(incomment);
116 :     infile.close();
117 :     infile.clear();
118 :    
119 :     } else if (strcasecmp(aVector[k], "-i") == 0) {
120 :     if (k == aNumber - 1)
121 :     this->showCorrectUsage(aVector[k]);
122 :     k++;
123 :     this->setInitialParamFile(aVector[k]);
124 :    
125 :     } else if (strcasecmp(aVector[k], "-o") == 0) {
126 :     if (k == aNumber - 1)
127 :     this->showCorrectUsage(aVector[k]);
128 :     k++;
129 :     printinfo.setOutputFile(aVector[k]);
130 :    
131 :     } else if (strcasecmp(aVector[k], "-p") == 0) {
132 :     if (k == aNumber - 1)
133 :     this->showCorrectUsage(aVector[k]);
134 :     k++;
135 :     printinfo.setParamOutFile(aVector[k]);
136 :    
137 :     } else if (strcasecmp(aVector[k], "-forceprint") == 0) {
138 :     forceprint = 1;
139 :    
140 :     } else if (strcasecmp(aVector[k], "-co") == 0) {
141 :     handle.logMessage(LOGFAIL, "The -co switch is no longer supported");
142 :    
143 :     } else if (strcasecmp(aVector[k], "-printinitial") == 0) {
144 :     if (k == aNumber - 1)
145 :     this->showCorrectUsage(aVector[k]);
146 :     k++;
147 :     this->setPrintInitialFile(aVector[k]);
148 :    
149 :     } else if (strcasecmp(aVector[k], "-printfinal") == 0) {
150 :     if (k == aNumber - 1)
151 :     this->showCorrectUsage(aVector[k]);
152 :     k++;
153 :     this->setPrintFinalFile(aVector[k]);
154 :    
155 :     } else if (strcasecmp(aVector[k], "-main") == 0) {
156 :     if (k == aNumber - 1)
157 :     this->showCorrectUsage(aVector[k]);
158 :     k++;
159 :     this->setMainGadgetFile(aVector[k]);
160 :    
161 :     } else if (strcasecmp(aVector[k], "-opt") == 0) {
162 :     if (k == aNumber - 1)
163 :     this->showCorrectUsage(aVector[k]);
164 :     k++;
165 :     this->setOptInfoFile(aVector[k]);
166 :    
167 :     } else if ((strcasecmp(aVector[k], "-printlikelihood") == 0) || (strcasecmp(aVector[k], "-likelihoodprint") == 0)) {
168 :     handle.logMessage(LOGFAIL, "The -printlikelihood switch is no longer supported\nSpecify a likelihoodprinter class in the model print file instead");
169 :    
170 :     } else if (strcasecmp(aVector[k], "-printlikesummary") == 0) {
171 :     handle.logMessage(LOGFAIL, "The -printlikesummary switch is no longer supported\nSpecify a likelihoodsummaryprinter class in the model print file instead");
172 :    
173 :     } else if (strcasecmp(aVector[k], "-printonelikelihood") == 0) {
174 :     handle.logMessage(LOGFAIL, "The -printonelikelihood switch is no longer supported\nSpecify a likelihoodprinter class in the model print file instead");
175 :    
176 :     } else if ((strcasecmp(aVector[k], "-print") == 0) || (strcasecmp(aVector[k], "-print1") == 0)) {
177 :     if (k == aNumber - 1)
178 :     this->showCorrectUsage(aVector[k]);
179 :     k++;
180 :     printinfo.setPrintIteration(atoi(aVector[k]));
181 :    
182 :     } else if (strcasecmp(aVector[k], "-precision") == 0) {
183 :     if (k == aNumber - 1)
184 :     this->showCorrectUsage(aVector[k]);
185 :     k++;
186 :     printinfo.setPrecision(atoi(aVector[k]));
187 :    
188 :     } else if ((strcasecmp(aVector[k], "-v") == 0) || (strcasecmp(aVector[k], "--version") == 0)) {
189 :     RUNID.Print(cout);
190 :     exit(EXIT_SUCCESS);
191 :    
192 :     } else if ((strcasecmp(aVector[k], "-h") == 0) || (strcasecmp(aVector[k], "--help") == 0)) {
193 :     this->showUsage();
194 :    
195 :     } else if (strcasecmp(aVector[k], "-log") == 0) {
196 :     if (k == aNumber - 1)
197 :     this->showCorrectUsage(aVector[k]);
198 :     k++;
199 :     handle.setLogFile(aVector[k]);
200 :     printLogLevel = 5;
201 :    
202 :     } else if (strcasecmp(aVector[k], "-nowarnings") == 0) {
203 :     //handle.logMessage(LOGWARN, "The -nowarnings switch is no longer supported\nSpecify a logging level using the -loglevel <number> instead");
204 :     printLogLevel = 1;
205 :    
206 :     } else if (strcasecmp(aVector[k], "-loglevel") == 0) {
207 :     if (k == aNumber - 1)
208 :     this->showCorrectUsage(aVector[k]);
209 :     k++;
210 :     printLogLevel = atoi(aVector[k]);
211 :    
212 :     } else if (strcasecmp(aVector[k], "-noprint") == 0) {
213 :     //JMB disable model printing from the commandline
214 :     runprint = 0;
215 :    
216 :     } else if (strcasecmp(aVector[k], "-seed") == 0) {
217 :     //JMB experimental setting of random number seed from the commandline
218 :     //if the "seed" is also specified in the optinfo file then that will override
219 :     //any seed that is specified on the commandline
220 :     if (k == aNumber - 1)
221 :     this->showCorrectUsage(aVector[k]);
222 :     k++;
223 :     srand(atoi(aVector[k]));
224 :    
225 :     } else if (strcasecmp(aVector[k], "-maxratio") == 0) {
226 :     //JMB experimental setting of maximum ratio of stock consumed in one timestep
227 :     if (k == aNumber - 1)
228 :     this->showCorrectUsage(aVector[k]);
229 :     k++;
230 :     maxratio = atof(aVector[k]);
231 :    
232 :     } else
233 :     this->showCorrectUsage(aVector[k]);
234 :    
235 :     k++;
236 :     }
237 :     }
238 :    
239 :     void MainInfo::checkUsage(const char* const inputdir, const char* const workingdir) {
240 :     int check = 0;
241 :     if (runnetwork)
242 :     check = max(0, printLogLevel);
243 :     else if (runstochastic)
244 :     check = max(3, printLogLevel);
245 :     else
246 :     check = max(2, printLogLevel);
247 :     handle.setLogLevel(check);
248 :    
249 :     //JMB dont print output if doing a network run
250 :     if (!runnetwork)
251 :     RUNID.Print(cout);
252 :     handle.logMessage(LOGINFO, "Starting Gadget from directory:", workingdir);
253 :     handle.logMessage(LOGINFO, "using data from directory:", inputdir);
254 :     handle.logMessage(LOGMESSAGE, ""); //write a blank line to the log file
255 :     if (strcasecmp(inputdir, workingdir) != 0)
256 :     handle.logMessage(LOGWARN, "Warning - working directory different from input directory");
257 :    
258 :     if (runnetwork) {
259 :     handle.logMessage(LOGINFO, "\n** Gadget running in network mode for Paramin **");
260 :     if (printInitialInfo) {
261 :     handle.logMessage(LOGINFO, "Warning - cannot print initial model information");
262 :     printInitialInfo = 0;
263 :     }
264 :     if (printFinalInfo) {
265 :     handle.logMessage(LOGINFO, "Warning - cannot print final model information");
266 :     printFinalInfo = 0;
267 :     }
268 :     }
269 :    
270 :     //JMB check to see if we can actually open required files ...
271 :     ifstream tmpin;
272 :     if (chdir(inputdir) != 0)
273 :     handle.logMessage(LOGFAIL, "Error - failed to change input directory to", inputdir);
274 :     if (givenInitialParam) {
275 :     //JMB check to see that the input and output filenames are different
276 :     //Otherwise Gadget will over-write the inputfile with a blank outputfile ...
277 :     if (strcasecmp(strInitialParamFile, printinfo.getParamOutFile()) == 0)
278 :     handle.logFileMessage(LOGFAIL, "the parameter input and output filenames are the same");
279 :    
280 :     tmpin.open(strInitialParamFile, ios::in);
281 :     handle.checkIfFailure(tmpin, strInitialParamFile);
282 :     tmpin.close();
283 :     tmpin.clear();
284 :     }
285 :     if (givenOptInfo) {
286 :     tmpin.open(strOptInfoFile, ios::in);
287 :     handle.checkIfFailure(tmpin, strOptInfoFile);
288 :     tmpin.close();
289 :     tmpin.clear();
290 :     }
291 :     ofstream tmpout;
292 :     if (chdir(workingdir) != 0)
293 :     handle.logMessage(LOGFAIL, "Error - failed to change working directory to", workingdir);
294 :     if (printInitialInfo) {
295 :     tmpout.open(strPrintInitialFile, ios::out);
296 :     handle.checkIfFailure(tmpout, strPrintInitialFile);
297 :     tmpout.close();
298 :     tmpout.clear();
299 :     }
300 :     if (printFinalInfo) {
301 :     tmpout.open(strPrintFinalFile, ios::out);
302 :     handle.checkIfFailure(tmpout, strPrintFinalFile);
303 :     tmpout.close();
304 :     tmpout.clear();
305 :     }
306 :     printinfo.checkPrintInfo(runnetwork);
307 :    
308 :     //JMB check the value of maxratio
309 :     if ((maxratio < rathersmall) || (maxratio > 1.0)) {
310 :     handle.logMessage(LOGWARN, "Warning - value of maxratio outside bounds", maxratio);
311 :     maxratio = 0.95;
312 :     }
313 :    
314 :     if ((!runstochastic) && (runnetwork)) {
315 :     handle.logMessage(LOGWARN, "\nWarning - Gadget for the paramin network should be used with -s option\nGadget will now set the -s switch to perform a simulation run");
316 :     runstochastic = 1;
317 :     }
318 :    
319 :     if ((runstochastic) && (runoptimise)) {
320 :     handle.logMessage(LOGWARN, "\nWarning - Gadget has been started with both the -s switch and the -l switch\nHowever, it is not possible to do both a simulation run and a likelihood run!\nGadget will perform only the simulation run (and ignore the -l switch)");
321 :     runoptimise = 0;
322 :     }
323 :    
324 :     if ((!runstochastic) && (!runoptimise)) {
325 :     handle.logMessage(LOGWARN, "\nWarning - Gadget has been started without either the -s switch or the -l switch\nGadget will now set the -s switch to perform a simulation run");
326 :     runstochastic = 1;
327 :     }
328 :    
329 :     handle.setRunOptimise(runoptimise);
330 :     if ((printLogLevel == 1) && (!runoptimise))
331 :     handle.logMessage(LOGWARN, "\n** Gadget cannot disable warnings for a simulation run **");
332 :    
333 :     if ((handle.checkLogFile()) && (runoptimise) && (printLogLevel >= 5))
334 :     handle.logMessage(LOGWARN, "\n** logging model information from a Gadget optimisation is not recommended **");
335 :    
336 :     if ((handle.checkLogFile()) && (runnetwork))
337 :     handle.logMessage(LOGWARN, "\n** logging model information from a Gadget network run is not recommended **");
338 :    
339 :     //check the printing options
340 :     if (forceprint)
341 :     handle.logMessage(LOGMESSAGE, "\nPrinting model output has been enabled from the command line");
342 :     if (!runprint)
343 :     handle.logMessage(LOGMESSAGE, "\nPrinting model output has been disabled from the command line");
344 :    
345 :     check = runprint;
346 :     if (runnetwork)
347 :     check = 0;
348 :     else if ((runoptimise) && (!forceprint))
349 :     check = 0;
350 :     runprint = check;
351 :    
352 :     handle.logMessage(LOGMESSAGE, ""); //write blank line to log file
353 :     }
354 :    
355 :     void MainInfo::read(CommentStream& infile) {
356 :     int dummy = 0;
357 :     char text[MaxStrLength];
358 :     strncpy(text, "", MaxStrLength);
359 :     infile >> ws;
360 :     while (!infile.eof()) {
361 :     infile >> text >> ws;
362 :    
363 :     if (strcasecmp(text, "-i") == 0) {
364 :     infile >> text >> ws;
365 :     this->setInitialParamFile(text);
366 :     } else if (strcasecmp(text, "-o") == 0) {
367 :     infile >> text >> ws;
368 :     printinfo.setOutputFile(text);
369 :     } else if (strcasecmp(text, "-co") == 0) {
370 :     handle.logMessage(LOGFAIL, "The -co switch is no longer supported");
371 :     } else if (strcasecmp(text, "-p") == 0) {
372 :     infile >> text >> ws;
373 :     printinfo.setParamOutFile(text);
374 :     } else if (strcasecmp(text, "-main") == 0) {
375 :     infile >> text >> ws;
376 :     this->setMainGadgetFile(text);
377 :     } else if (strcasecmp(text, "-printinitial") == 0) {
378 :     infile >> text >> ws;
379 :     this->setPrintInitialFile(text);
380 :     } else if (strcasecmp(text, "-printfinal") == 0) {
381 :     infile >> text >> ws;
382 :     this->setPrintFinalFile(text);
383 :     } else if (strcasecmp(text, "-opt") == 0) {
384 :     infile >> text >> ws;
385 :     this->setOptInfoFile(text);
386 :     } else if (strcasecmp(text, "-forceprint") == 0) {
387 :     forceprint = 1;
388 :     } else if (strcasecmp(text, "-noprint") == 0) {
389 :     runprint = 0;
390 :     } else if ((strcasecmp(text, "-print") == 0) || (strcasecmp(text, "-print1") == 0)) {
391 :     infile >> dummy >> ws;
392 :     printinfo.setPrintIteration(dummy);
393 :     } else if (strcasecmp(text, "-precision") == 0) {
394 :     infile >> dummy >> ws;
395 :     printinfo.setPrecision(dummy);
396 :     } else if (strcasecmp(text, "-log") == 0) {
397 :     infile >> text >> ws;
398 :     handle.setLogFile(text);
399 :     printLogLevel = 5;
400 :     } else if (strcasecmp(text, "-nowarnings") == 0) {
401 :     printLogLevel = 1;
402 :     } else if (strcasecmp(text, "-loglevel") == 0) {
403 :     infile >> printLogLevel >> ws;
404 :     } else if (strcasecmp(text, "-seed") == 0) {
405 :     infile >> dummy >> ws;
406 :     srand(dummy);
407 :     } else if (strcasecmp(text, "-maxratio") == 0) {
408 :     infile >> maxratio >> ws;
409 :     } else if (strcasecmp(text, "-printlikesummary") == 0) {
410 :     handle.logMessage(LOGWARN, "The -printlikesummary switch is no longer supported\nSpecify a likelihoodsummaryprinter class in the model print file instead");
411 :     } else if (strcasecmp(text, "-printlikelihood") == 0) {
412 :     handle.logMessage(LOGWARN, "The -printlikelihood switch is no longer supported\nSpecify a likelihoodprinter class in the model print file instead");
413 :     } else if (strcasecmp(text, "-printonelikelihood") == 0) {
414 :     handle.logMessage(LOGWARN, "The -printonelikelihood switch is no longer supported\nSpecify a likelihoodprinter class in the model print file instead");
415 :     } else
416 :     this->showCorrectUsage(text);
417 :     }
418 :     }
419 :    
420 :     void MainInfo::setPrintInitialFile(char* filename) {
421 :     if (strPrintInitialFile != NULL) {
422 :     delete[] strPrintInitialFile;
423 :     strPrintInitialFile = NULL;
424 :     }
425 :     strPrintInitialFile = new char[strlen(filename) + 1];
426 :     strcpy(strPrintInitialFile, filename);
427 :     printInitialInfo = 1;
428 :     }
429 :    
430 :     void MainInfo::setPrintFinalFile(char* filename) {
431 :     if (strPrintFinalFile != NULL) {
432 :     delete[] strPrintFinalFile;
433 :     strPrintFinalFile = NULL;
434 :     }
435 :     strPrintFinalFile = new char[strlen(filename) + 1];
436 :     strcpy(strPrintFinalFile, filename);
437 :     printFinalInfo = 1;
438 :     }
439 :    
440 :     void MainInfo::setMainGadgetFile(char* filename) {
441 :     if (strMainGadgetFile != NULL) {
442 :     delete[] strMainGadgetFile;
443 :     strMainGadgetFile = NULL;
444 :     }
445 :     strMainGadgetFile = new char[strlen(filename) + 1];
446 :     strcpy(strMainGadgetFile, filename);
447 :     }
448 :    
449 :     void MainInfo::setInitialParamFile(char* filename) {
450 :     if (strInitialParamFile != NULL) {
451 :     delete[] strInitialParamFile;
452 :     strInitialParamFile = NULL;
453 :     }
454 :     strInitialParamFile = new char[strlen(filename) + 1];
455 :     strcpy(strInitialParamFile, filename);
456 :     givenInitialParam = 1;
457 :     }
458 :    
459 :     void MainInfo::setOptInfoFile(char* filename) {
460 :     if (strOptInfoFile != NULL) {
461 :     delete[] strOptInfoFile;
462 :     strOptInfoFile = NULL;
463 :     }
464 :     strOptInfoFile = new char[strlen(filename) + 1];
465 :     strcpy(strOptInfoFile, filename);
466 :     givenOptInfo = 1;
467 :     }

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

Powered By FusionForge