-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSORTIE.cpp
More file actions
365 lines (329 loc) · 12.6 KB
/
SORTIE.cpp
File metadata and controls
365 lines (329 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h> //for getting dates
#include <queue>
#include "SimManager.h"
//Interface stuff
#include "Interface.h"
#include "Messages.h"
#include "ParsingFunctions.h"
//---------------------------------------------------------------------------
//***************************************
// MAIN MODEL CONTROL CODE
// This calls the sim manager and gets things rockin', also acting as the
// liaison with the appropriate interface. We expect two possible argument
// structures coming into main: in the first, there are no arguments after
// the application name. This starts the application using the interactive
// text interface. In the second, there is one argument, a filename, after the
// application name. This causes the application to start and immediately
// begin processing that file (i.e., command-line style).
//
// In either mode, you can pause an in-process run. The method depends on the
// active interface DLL (with the DOS one, hit any key; with the Java one,
// create a file called "messages.txt" with a 'p' in the first character spot).
// In interactive mode, you are prompted for your next steps. In command line
// mode, the model pauses until you type either "run" or "quit" (although it
// may not tell you this). It will finish its run and exit as normal, assuming
// it is not interrupted again.
//
// So to be clear: in command-line mode, the model seeks to finish its
// assigned run and will quit as soon as it does so, or as soon as it runs into
// a fatal error. In interactive mode, the model will remain active until the
// user explicitly tells it to quit by typing "quit".
//
// Copyright 2003 Charles D. Canham.
//***************************************
int main( int argc, char * argv[] )
{
using namespace std;
modelMsg stcMsg; //for capturing messages
modelErr stcError; //for sending errors
string strArg1, //this represents the first argument to the command
strArg2; //this represents the second argument to the command
string::size_type pos;
time_t timer;
struct tm * stcTime;
unsigned long iRunTime = 0; //time of run in seconds
int iNumTimesteps; //for figuring out the number of timesteps
//Capture the application path
string sPath = argv[0];
//cout << argc << " arguments.\n";
//for (int i = 0; i < argc; i++)
//cout << "Argument " << (i+1) << ": " << argv[i] << "\n";
//if (sPath.find("coremodel.exe") != string::npos) {
// sPath = sPath.substr(0, sPath.find("coremodel.exe"));
//}
//The above won't work if launched with just the string "coremodel"
if (sPath.find("coremodel") != string::npos) {
sPath = sPath.substr(0, sPath.find("coremodel"));
}
//***************************************
// Version numbers
//***************************************
int iMajorVersion = 7, iMinorVersion = 12;
clSimManager p_oSimManager( iMajorVersion, iMinorVersion, sPath);
//***************************************
// Command line mode or text interface mode?
// If the model was started with an argument, assume it is
// a parameter file string and this should run in pure command line mode.
//***************************************
//No additional arguments beyond the application name - start the model in
//text interface mode
if ( 1 == argc )
{
DoIntroduction();
}
//One or two additional argument beyond the application name - we're in
//command line mode with a file name and possibly a number of timesteps
else if ( 2 == argc || 3 == argc )
{
try
{
//Redirect cout to a log file - use a local object to do this to make
//sure that we direct it back in the destructor (otherwise an error can
//cause this to crash)
//Update - I'm not redirecting to a log file anymore. This is for running
//this as a job on Condor.
timer = time( NULL );
stcTime = localtime( & timer );
//Write the log header
cout << "\n\nFile: " << argv[1] << "\nRun started on: " << asctime( stcTime ) << "\n\n";
//Get the file type
fileType iFileType = p_oSimManager.GetFileType( argv[1] );
if ( batchfile == iFileType )
{
p_oSimManager.RunBatch( argv[1] );
if ( p_oSimManager.GetSimState() == clSimManager::Run_Complete
|| p_oSimManager.GetSimState() == clSimManager::No_Data )
{
timer = time( NULL );
stcTime = localtime( & timer );
cout << "\n\nRun ended on: " << asctime( stcTime ) << "\n\n";
return 0;
}
}
else if ( parfile == iFileType || detailed_output == iFileType )
{
p_oSimManager.ReadFile( argv[1] );
if ( p_oSimManager.GetSimState() != clSimManager::Initialized )
{
//An error occurred - exit
return 0;
}
//If there's an extra argument, get the number of timesteps
int iNumTimesteps = 0;
if ( 3 == argc )
{
iNumTimesteps = atoi( argv[2] );
if ( 0 == iNumTimesteps && argv[2] [0] != '0' )
{
cerr << "Couldn't read number of timesteps.\n";
return 0;
}
}
iRunTime = p_oSimManager.RunSim( iNumTimesteps );
//If the run was finished, exit; this way, if the user is treating
//this strictly as command-line, SORTIE will do its run and finish.
//If the user broke in and paused, then they will enter interactive
//mode in the while loop below until they hit "run" some more
if ( ( p_oSimManager.GetSimState() == clSimManager::Run_Complete
&& p_oSimManager.GetCurrentTimestep() == p_oSimManager.GetNumberOfTimesteps() )
|| p_oSimManager.GetSimState() == clSimManager::No_Data )
{
stringstream msg;
msg << p_oSimManager.GetCurrentTimestep() << "," << p_oSimManager.GetNumberOfTimesteps() << "," << iRunTime;
stcMsg.iMessageCode = RUN_COMPLETE;
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
return 0;
}
}
else
{
cout << "The file passed to SORTIE is not of a valid type. Exiting.\n";
//An error occurred - exit
return 0;
}
} //end of try block
catch ( modelErr & err )
{
ExternalErrorHandler( err, p_oSimManager.GetSimState(), false );
//An error occurred - exit
return 0;
}
} //end of if (2 == argc || 3 == argc)
else
{
//More arguments - quit 'cause we don't know what's going on
cerr << "Too many arguments.\n";
return 0;
}
////////////////////////////////////////////////////////////
// Main loop - this will accept and process commands
// until the user wants to quit; this is automatically entered
// in interface mode, and can be entered in command-line
// mode if the user paused
////////////////////////////////////////////////////////////
while ( 1 )
{
stcMsg.iMessageCode = UNKNOWN;
try
{
stcMsg = GetMessage();
if ( QUIT == stcMsg.iMessageCode ) {
stringstream msg;
msg << p_oSimManager.GetCurrentTimestep() << "," << p_oSimManager.GetNumberOfTimesteps() << "," << iRunTime;
stcMsg.iMessageCode = RUN_COMPLETE;
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
return 0;
}
//parse out any additional arguments if not "input" - skip input because
//filenames may have spaces and won't have any additional args
if ( INPUT_FILE == stcMsg.iMessageCode )
{
strArg1 = stcMsg.sMoreInfo;
strArg2 = "";
}
else
{
strArg1 = stcMsg.sMoreInfo;
pos = strArg1.find_first_of( " " );
if ( string::npos != pos )
{
strArg2 = strArg1.substr( pos + 1 );
strArg1 = strArg1.substr( 0, pos );
}
else
strArg2 = "";
}
/** Input command */
if ( INPUT_FILE == stcMsg.iMessageCode )
{
//Make sure we got a file name
if ( "" == strArg1 )
{
stcError.iErrorCode = NEED_FILE;
stcError.sFunction = "main";
ExternalErrorHandler( stcError, p_oSimManager.GetSimState(), false );
}
else
{
p_oSimManager.ReadFile( ( char * ) strArg1.c_str() );
//Let the user know the status of the model if ready
if ( clSimManager::Initialized == p_oSimManager.GetSimState() )
{
stcMsg.iMessageCode = MODEL_READY;
SendMessage( stcMsg );
}
else
{
stcMsg.iMessageCode = MODEL_NOT_READY;
SendMessage( stcMsg );
}
} //end of else
} //end of if ("input" = strCommand)
/** Run command */
else if ( RUN == stcMsg.iMessageCode )
{
//Make sure we're ready
if ( clSimManager::No_Data == p_oSimManager.GetSimState() )
{
stcError.iErrorCode = MODEL_NOT_READY;
ExternalErrorHandler( stcError, p_oSimManager.GetSimState(), false );
}
else
{
//Are there any timesteps? If not don't pass anything
if ( "" == strArg1 )
{
iRunTime = p_oSimManager.RunSim();
if (p_oSimManager.GetBatchNumber() != 0) {
p_oSimManager.RunBatch(NULL);
}
if ( 2 == argc || 3 == argc )
{
//If we're in batch mode and the run is finished, exit
if ( ( p_oSimManager.GetSimState() == clSimManager::Run_Complete
&& p_oSimManager.GetCurrentTimestep() == p_oSimManager.GetNumberOfTimesteps()
&& p_oSimManager.GetBatchNumber() == 0)
|| p_oSimManager.GetSimState() == clSimManager::No_Data )
{
timer = time( NULL );
stcTime = localtime( & timer );
cout << "\n\nRun ended on: " << asctime( stcTime ) << "\n\n";
//flushall();
return 0;
}
}
if ( p_oSimManager.GetSimState() == clSimManager::Run_Complete )
{
stringstream msg;
msg << p_oSimManager.GetCurrentTimestep() << "," << p_oSimManager.GetNumberOfTimesteps() << "," << iRunTime;
stcMsg.iMessageCode = RUN_COMPLETE;
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
}
}
else
{
iNumTimesteps = atoi( strArg1.c_str() );
//If it equals 0 make sure that's what the user meant - else
//return an error
if ( 0 == iNumTimesteps && strArg1.length() > 1 )
{
stcMsg.iMessageCode = BAD_ARGUMENT;
stcMsg.sMoreInfo = "Couldn't read number of timesteps";
SendMessage( stcMsg );
}
else
{
iRunTime = p_oSimManager.RunSim( iNumTimesteps );
if ( 2 == argc || 3 == argc )
{
//If we're in batch mode and the run is finished, exit
if ( ( p_oSimManager.GetSimState() == clSimManager::Run_Complete
&& p_oSimManager.GetCurrentTimestep() == p_oSimManager.GetNumberOfTimesteps() )
|| p_oSimManager.GetSimState() == clSimManager::No_Data )
{
timer = time( NULL );
stcTime = localtime( & timer );
cout << "\n\nRun ended on: " << asctime( stcTime ) << "\n\n";
//flushall();
return 0;
}
}
if ( p_oSimManager.GetSimState() == clSimManager::Run_Complete )
{
stringstream msg;
msg << p_oSimManager.GetCurrentTimestep() << "," << p_oSimManager.GetNumberOfTimesteps() << "," << iRunTime;
stcMsg.iMessageCode = RUN_COMPLETE;
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
}
}
}
}
}
/** Other (hopefully registered) command */
else
{
stcMsg.iMessageCode = BAD_COMMAND;
SendMessage( stcMsg );
} //end of else
} //end of try block
catch ( modelErr & err )
{
ExternalErrorHandler( err, p_oSimManager.GetSimState(), false );
}
catch ( modelMsg & msg )
{
SendMessage( msg );
}
} //end of while (1)
return 0;
}
//---------------------------------------------------------------------------