1 : |
agomez |
1 |
#include "commandlineinfo.h" |
2 : |
|
|
#include "commentstream.h"
|
3 : |
|
|
#include "errorhandler.h"
|
4 : |
|
|
#include "paramin.h" |
5 : |
|
|
|
6 : |
|
|
extern ErrorHandler handle;
|
7 : |
|
|
|
8 : |
|
|
void CommandLineInfo::showCorrectUsage(char* error) {
|
9 : |
|
|
cerr << "Error in command line value - unrecognised option " << error << endl
|
10 : |
|
|
<< "Required options are -i <filename> and -function <function>\n"
|
11 : |
|
|
<< "For more information try running Paramin with the -h switch\n";
|
12 : |
|
|
exit(EXIT_FAILURE);
|
13 : |
|
|
}
|
14 : |
|
|
|
15 : |
|
|
void CommandLineInfo::showCorrectUsage() {
|
16 : |
|
|
cerr << "Error on command line when starting Paramin\n"
|
17 : |
|
|
<< "Required options are -i <filename> and -function <function>\n"
|
18 : |
|
|
<< "For more information try running Paramin with the -h switch\n";
|
19 : |
|
|
exit(EXIT_FAILURE);
|
20 : |
|
|
}
|
21 : |
|
|
|
22 : |
|
|
void CommandLineInfo::showUsage() {
|
23 : |
|
|
cout << "Required information for running Paramin:\n"
|
24 : |
|
|
<< " -i <filename> read model parameters from <filename>\n"
|
25 : |
|
|
<< " -function <function> read function and options to be minimised\n"
|
26 : |
|
|
<< " (this must be the last option specified)\n"
|
27 : |
|
|
<< "\nOptional information for controlling Paramin:\n"
|
28 : |
|
|
<< " -opt <filename> read optimising parameters from <filename>\n"
|
29 : |
|
|
<< " -network <filename> read network information from <filename>\n"
|
30 : |
|
|
<< " -o <filename> print model parameters to <filename>\n"
|
31 : |
|
|
<< " (default filename is 'params.out')\n"
|
32 : |
|
|
<< " -scale scale the variables to be optimised\n"
|
33 : |
|
|
<< "\nOther valid command line options:\n"
|
34 : |
|
|
<< " -v --version display version information and exit\n"
|
35 : |
|
|
<< " -h --help display this help screen and exit\n"
|
36 : |
|
|
<< "\nFor more information see the web page at http://www.hafro.is/gadget/paramin.html\n\n";
|
37 : |
|
|
exit(EXIT_SUCCESS);
|
38 : |
|
|
}
|
39 : |
|
|
|
40 : |
|
|
CommandLineInfo::CommandLineInfo() {
|
41 : |
|
|
numProc = 0;
|
42 : |
|
|
scale = 0;
|
43 : |
|
|
waitMaster = 300;
|
44 : |
|
|
hostMultiple = 2.0;
|
45 : |
|
|
runMultiple = 0.5;
|
46 : |
|
|
bestMultiple = 3.0;
|
47 : |
|
|
}
|
48 : |
|
|
|
49 : |
|
|
CommandLineInfo::~CommandLineInfo() {
|
50 : |
|
|
}
|
51 : |
|
|
|
52 : |
|
|
void CommandLineInfo::read(int aNumber, char* const aVector[]) {
|
53 : |
|
|
int k, len;
|
54 : |
|
|
|
55 : |
|
|
if (aNumber > 1) {
|
56 : |
|
|
k = 1;
|
57 : |
|
|
while (k < aNumber) {
|
58 : |
|
|
if (strcasecmp(aVector[k], "-scale") == 0) {
|
59 : |
|
|
scale = 1;
|
60 : |
|
|
|
61 : |
|
|
} else if (strcasecmp(aVector[k], "-i") == 0) {
|
62 : |
|
|
if (k == aNumber - 1)
|
63 : |
|
|
this->showCorrectUsage(aVector[k]);
|
64 : |
|
|
k++;
|
65 : |
|
|
inputfile.resize(aVector[k]);
|
66 : |
|
|
|
67 : |
|
|
} else if (strcasecmp(aVector[k], "-o") == 0) {
|
68 : |
|
|
if (k == aNumber - 1)
|
69 : |
|
|
this->showCorrectUsage(aVector[k]);
|
70 : |
|
|
k++;
|
71 : |
|
|
outputfile.resize(aVector[k]);
|
72 : |
|
|
|
73 : |
|
|
} else if (strcasecmp(aVector[k], "-network") == 0) {
|
74 : |
|
|
if (k == aNumber - 1)
|
75 : |
|
|
this->showCorrectUsage(aVector[k]);
|
76 : |
|
|
k++;
|
77 : |
|
|
networkfile.resize(aVector[k]);
|
78 : |
|
|
this->readNetworkInfo();
|
79 : |
|
|
|
80 : |
|
|
} else if (strcasecmp(aVector[k], "-opt") == 0) {
|
81 : |
|
|
if (k == aNumber - 1)
|
82 : |
|
|
this->showCorrectUsage(aVector[k]);
|
83 : |
|
|
k++;
|
84 : |
|
|
optfile.resize(aVector[k]);
|
85 : |
|
|
|
86 : |
|
|
} else if ((strcasecmp(aVector[k], "-v") == 0) || (strcasecmp(aVector[k], "--version") == 0)) {
|
87 : |
|
|
cout << "Paramin version " << PARAMINVERSION << endl << endl;
|
88 : |
|
|
exit(EXIT_SUCCESS);
|
89 : |
|
|
|
90 : |
|
|
} else if ((strcasecmp(aVector[k], "-h") == 0) || (strcasecmp(aVector[k], "--help") == 0)) {
|
91 : |
|
|
this->showUsage();
|
92 : |
|
|
|
93 : |
|
|
} else if ((strcasecmp(aVector[k], "-func") == 0) || (strcasecmp(aVector[k], "-function") == 0)) {
|
94 : |
|
|
if (k == aNumber - 1)
|
95 : |
|
|
this->showCorrectUsage(aVector[k]);
|
96 : |
|
|
k++;
|
97 : |
|
|
function.resize(aVector[k]);
|
98 : |
|
|
while (k < aNumber - 1) {
|
99 : |
|
|
k++;
|
100 : |
|
|
function.resize(aVector[k]);
|
101 : |
|
|
}
|
102 : |
|
|
|
103 : |
|
|
} else
|
104 : |
|
|
this->showCorrectUsage(aVector[k]);
|
105 : |
|
|
|
106 : |
|
|
k++;
|
107 : |
|
|
}
|
108 : |
|
|
|
109 : |
|
|
// Must have set both name of function and name of the inputfile
|
110 : |
|
|
if (function.Size() < 1)
|
111 : |
|
|
this->showCorrectUsage();
|
112 : |
|
|
if (inputfile.Size() != 1)
|
113 : |
|
|
this->showCorrectUsage();
|
114 : |
|
|
|
115 : |
|
|
} else
|
116 : |
|
|
this->showCorrectUsage();
|
117 : |
|
|
|
118 : |
|
|
// Set the default value for the output
|
119 : |
|
|
if (outputfile.Size() < 1) {
|
120 : |
|
|
char* defaultname = new char[strlen("params.out") + 1];
|
121 : |
|
|
strcpy(defaultname, "params.out");
|
122 : |
|
|
outputfile.resize(defaultname);
|
123 : |
|
|
delete[] defaultname;
|
124 : |
|
|
}
|
125 : |
|
|
}
|
126 : |
|
|
|
127 : |
|
|
const CharPtrVector& CommandLineInfo::getFunction() {
|
128 : |
|
|
return function;
|
129 : |
|
|
}
|
130 : |
|
|
|
131 : |
|
|
char* CommandLineInfo::getInputFilename() {
|
132 : |
|
|
if (inputfile.Size() != 1) {
|
133 : |
|
|
cerr << "Error on commandline - inputfile not specified\n";
|
134 : |
|
|
exit(EXIT_FAILURE);
|
135 : |
|
|
}
|
136 : |
|
|
return inputfile[0];
|
137 : |
|
|
}
|
138 : |
|
|
|
139 : |
|
|
char* CommandLineInfo::getOutputFilename() {
|
140 : |
|
|
if (outputfile.Size() != 1) {
|
141 : |
|
|
cerr << "Error on commandline - outputfile not specified\n";
|
142 : |
|
|
exit(EXIT_FAILURE);
|
143 : |
|
|
}
|
144 : |
|
|
return outputfile[0];
|
145 : |
|
|
}
|
146 : |
|
|
|
147 : |
|
|
char* CommandLineInfo::getOptFilename() {
|
148 : |
|
|
if (optfile.Size() != 1) {
|
149 : |
|
|
cerr << "Error on commandline - optfile not specified\n";
|
150 : |
|
|
exit(EXIT_FAILURE);
|
151 : |
|
|
}
|
152 : |
|
|
return optfile[0];
|
153 : |
|
|
}
|
154 : |
|
|
|
155 : |
|
|
int CommandLineInfo::getOptInfoFileGiven() {
|
156 : |
|
|
return (optfile.Size() > 0);
|
157 : |
|
|
}
|
158 : |
|
|
|
159 : |
|
|
void CommandLineInfo::readNetworkInfo() {
|
160 : |
|
|
|
161 : |
|
|
//Need to add check for values which can not be accepted...
|
162 : |
|
|
int i = 0;
|
163 : |
|
|
char text[MaxStrLength];
|
164 : |
|
|
strncpy(text, "", MaxStrLength);
|
165 : |
|
|
ifstream infile(networkfile[0]);
|
166 : |
|
|
CommentStream commin(infile);
|
167 : |
|
|
commin >> ws;
|
168 : |
|
|
commin >> text >> ws;
|
169 : |
|
|
while (!commin.eof()) {
|
170 : |
|
|
if (strcasecmp(text, "numproc") == 0) {
|
171 : |
|
|
commin >> numProc >> ws;
|
172 : |
|
|
if (numProc < 0) {
|
173 : |
|
|
cerr << "Error in networkfile - numproc must be greater than 0\n";
|
174 : |
|
|
infile.close();
|
175 : |
|
|
infile.clear();
|
176 : |
|
|
exit(EXIT_FAILURE);
|
177 : |
|
|
}
|
178 : |
|
|
|
179 : |
|
|
} else if (strcasecmp(text, "waitformaster") == 0) {
|
180 : |
|
|
commin >> waitMaster >> ws;
|
181 : |
|
|
if (waitMaster < -1) {
|
182 : |
|
|
cerr << "Error in networkfile - waitformaster must be greater than -1\n";
|
183 : |
|
|
infile.close();
|
184 : |
|
|
infile.clear();
|
185 : |
|
|
exit(EXIT_FAILURE);
|
186 : |
|
|
}
|
187 : |
|
|
|
188 : |
|
|
} else if (strcasecmp(text, "runtimemultiple") == 0) {
|
189 : |
|
|
commin >> runMultiple >> ws;
|
190 : |
|
|
if (runMultiple < 0) {
|
191 : |
|
|
cerr << "Error in networkfile - runtimemultiple must be greater than 0\n";
|
192 : |
|
|
infile.close();
|
193 : |
|
|
infile.clear();
|
194 : |
|
|
exit(EXIT_FAILURE);
|
195 : |
|
|
}
|
196 : |
|
|
|
197 : |
|
|
} else if (strcasecmp(text, "hostmultiple") == 0) {
|
198 : |
|
|
commin >> hostMultiple >> ws;
|
199 : |
|
|
} else if (strcasecmp(text, "besttimemultiple") == 0) {
|
200 : |
|
|
commin >> bestMultiple >> ws;
|
201 : |
|
|
} else {
|
202 : |
|
|
cerr << "Error in networkfile - unknown option " << text << "\n";
|
203 : |
|
|
infile.close();
|
204 : |
|
|
infile.clear();
|
205 : |
|
|
exit(EXIT_FAILURE);
|
206 : |
|
|
}
|
207 : |
|
|
commin >> text;
|
208 : |
|
|
i++;
|
209 : |
|
|
}
|
210 : |
|
|
|
211 : |
|
|
if (i == 0) {
|
212 : |
|
|
cout << "Warning - no network info given in networkfile - using default values." << endl;
|
213 : |
|
|
}
|
214 : |
|
|
infile.close();
|
215 : |
|
|
infile.clear();
|
216 : |
|
|
}
|