-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawlog.cpp
More file actions
540 lines (421 loc) · 14.4 KB
/
drawlog.cpp
File metadata and controls
540 lines (421 loc) · 14.4 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*******************************************************************
*
* DESCRIPTION: Draws the content of the log
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
* Version 2: Daniel Rodriguez
* Version 3: Gabriel Wainer
* Version 4: Alejandro Troccoli (Parallel )
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
* mailto://drodrigu@dc.uba.ar
* mailto://gabrielw@dc.uba.ar
*
* DATE: 27/06/1998
* DATE: 28/04/1999 (v2)
*
*******************************************************************/
// ** include files **//
#include <cstdlib>
#include <strstream>
#include <fstream>
#include <cstring>
#include "ini.h" // Class Ini
#include "VTime.hh" // Class VTime
#include "logparser.h"
#include "cellstate.h"
using namespace std;
static const string defaultPortName("out"); /* AtomicCell::outPort */
// ** main ** //
VTime getNextMsgLine( istream& file, const string& modelName, char* buffer, const string &portName );
void printState( const CellState &state, const VTime &time )
{
if (!Impresion::Default.FlatLog())
cout << "Time: " << time.asString() << endl ;
else
cout << endl;
state.print(cout, '?');
}
void showHelp()
{
cout << "drawlog -[?hmtclwp0n]\n\n";
cout << "where:\n";
cout << "\t?\tShow this message\n";
cout << "\th\tShow this message\n";
cout << "\tm\tSpecify file containing the model (.ma)\n";
cout << "\tt\tInitial time\n";
cout << "\ti\tTime interval (After the initial time, draw after every time interval)\n";
cout << "\tc\tSpecify the coupled model to draw\n";
cout << "\tl\tLog file containing the output generated by SIMU\n";
cout << "\tw\tWidth (in characters) used to represent numeric values\n";
cout << "\tp\tPrecision used to represent numeric values (in characters)\n";
cout << "\t0\tDon't print the zero value\n";
cout << "\tf\tOnly cell values on a specified slice in 3D models\n";
cout << "\tn\tSpecify the neighbor port to show (default: " << defaultPortName << ")\n";
exit(1);
}
int splitLine( char *line, char *pos, char *value)
{
char *posi = pos, *val = value;
// Primero leo POS
while ( *line != 0 && *line != '=' )
{
if (*line != ' '){
*posi = *line;
posi++;
}
line++;
}
if (*line == 0)
return 0;
*posi = 0;
line++;
// Ahora leo el value
while ( *line != 0 )
{
if (*line != ' '){
*val = *line;
val++;
}
line++;
}
*val = 0;
if (pos[0] == '(' && strlen(value) > 0)
return 1;
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// MAIN - DRAWLOG
//////////////////////////////////////////////////////////////////////////////
int main( int argc, char *argv[] )
{
try
{
VTime initial( VTime::Zero );
VTime timeInterval(VTime::InvalidTime);
string modelName, iniName, logName("-"), strWidth(""), strPrec(""), strPlane("");
string strPort("");
// parameter parsing
while( --argc )
if( *argv[ argc ] == '-' )
switch( argv[ argc ][ 1 ] )
{
case 'm': /* file .ma */
iniName = argv[ argc ] + 2;
break;
case 't': /* intital time */
initial = argv[ argc ] + 2 ;
break;
case 'i': /* time interval */
timeInterval = argv[ argc ] + 2;
break;
case 'c': /* coupled */
modelName = lowerCase(argv[ argc ] + 2);
break;
case 'l': /* log filename */
logName = argv[ argc ] + 2 ;
break;
case 'w': /* Set width */
strWidth = argv [ argc ] + 2;
Impresion::Default.Width( str2Int(strWidth) );
break;
case 'p': /* Set precision */
strPrec = argv [ argc ] + 2;
Impresion::Default.Precision ( str2Int(strPrec) );
break;
case '0': /* Don't print zero */
Impresion::Default.PrintZero(false);
break;
case '?':
case 'h':
showHelp();
break;
case 'f': /* Only cell values on 3D models */
Impresion::Default.FlatLog(true);
strPlane = argv [ argc ] + 2;
Impresion::Default.FlatLogPlane( str2Int(strPlane) );
break;
case 'n': /* The port to show */
strPort = argv [ argc ] + 2;
break;
default:
cout << "Warning... invalid parameter " << argv[ argc ] << "!" << endl ;
showHelp();
}
else
cout << "Warning... invalid parameter " << argv[ argc ] << "!" << endl ;
// parameter validation
if( iniName == "" || modelName == "" )
{
cout << "Drawlog - Parallel Version" << endl;
cout << "Usage: " << argv[ 0 ] << " -mfile.ma -cCoupledCellName [ -tInitialtime ]\n"
"\t-lmessage.log [ -wWidth ] [ -pPrecision ] [ -0 ]\n"
"\t-nNCPort" << endl;
return 1 ;
}
Ini iniFile ;
Ini modelsLogFiles;
iniFile.parse( iniName ) ;
//If reading from files
if ( logName != "-")
modelsLogFiles.parse( logName );
// dimension
nTupla nt;
register unsigned cols = 0, rows = 0;
if (iniFile.exists( modelName, "width" ))
{
cols = str2Int( iniFile.definition( modelName, "width" ).front() );
rows = str2Int( iniFile.definition( modelName, "height" ).front() );
nt.add(rows, cols);
}
else if (iniFile.exists( modelName, "dim" ))
{
Ini::IdList dimension = iniFile.definition( modelName, "dim" );
CellPosition cp( iniFile.join(dimension) );
nt = cp;
cols = nt.get(DIM_WIDTH);
rows = nt.get(DIM_HEIGHT);
}
if ( nt.contains(0) )
MASSERT_ERR( "Attemp to draw a model where a component of its dimension is 0" );
//////////////////////////////////////////////////////////////
// validate port name
//////////////////////////////////////////////////////////////
if (strPort.empty())
strPort = defaultPortName;
else if (strPort != defaultPortName) {
MASSERTMSG( iniFile.exists( modelName, "neighborports" ),
"Port requested (-n) but no NeighborPort keyword in the model" );
Ini::IdList::const_iterator cursor;
const Ini::IdList &nc_ports( iniFile.definition( modelName, "neighborports" ) ) ;
cursor = nc_ports.begin();
while (cursor != nc_ports.end() && *cursor != strPort)
cursor++;
MASSERTMSG( cursor != nc_ports.end(), "Port '" + strPort +"' not declared" );
strPort = "out_" + strPort; /* Add the prefix (AtomicCell::NCOutPrefix) */
}
//////////////////////////////////////////////////////////////
CellState state( nt ) ;
//////////////////////////////////////////////////////////////
// default initial value
string initialValue( iniFile.definition( modelName, "initialvalue" ).front() ) ;
Real value ( str2Real( initialValue ) );
CellPosition counter( nt.dimension(), 0);
register bool overflow = false;
while (!overflow)
{
state[ counter ] = value;
overflow = counter.next( nt );
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// loading the initial state
/////////////////////////////////////////////////////////////
if( iniFile.exists( modelName, "initialrow" ) ) // The dim = 2
{
const Ini::IdList &values( iniFile.definition( modelName, "initialrow" ) ) ;
register unsigned row = 0;
Ini::IdList::const_iterator cursor = values.begin();
while (cursor != values.end() )
{
// el primer valor es la fila
row = str2Int( (*cursor) ) ;
MASSERTMSG( row <= rows-1, "The number of row for initialRowValue is out of range. It's " + int2Str(row) + " and must be in [ 0, " + int2Str( rows - 1 ) + "]" );
cursor++ ;
// Los siguientes elementos son la descripcion de la fila
register unsigned col = 0;
while ( col < cols )
{
MASSERTMSG( cursor != values.end(), "Insuficient data for initialRowValue. Last row with " + int2Str(col) + " elements. Note: May be a middle row definition with less elements.");
string rowVal( (*cursor) ) ;
nTupla nt3;
nt3.add(row, col);
state[ nt3 ] = str2Real( rowVal ) ;
col++ ;
cursor++ ;
}
}
}
if( iniFile.exists( modelName, "initialRowValue" ) ) // The dim = 2
{
const Ini::IdList &rowsList( iniFile.definition(modelName, "initialRowValue" ) ) ;
register unsigned row = 0;
Real val;
for ( Ini::IdList::const_iterator cursor = rowsList.begin(); cursor != rowsList.end(); cursor++ )
{
// the first value is the row number
row = str2Int( (*cursor) ) ;
MASSERTMSG( row < rows, "The number of row for initialRowValue is out of range. It's " + int2Str(row) + " and must be in [ 0, " + int2Str( rows-1 ) + "]" ) ;
cursor++ ;
MASSERTMSG( cursor != rowsList.end(), "Invalid initial row value for initialRowValue (must be a pair rowNumber rowValues)!" );
// the second is the description of the row
string rowVal( *cursor ) ;
MASSERTMSG( rowVal.size() == cols, "The size of the rows for the initial values of the CoupledCell must be equal to the width value !" );
register unsigned col = 0;
for (string::iterator rowCurs = rowVal.begin(); rowCurs != rowVal.end(); rowCurs++ )
{
if (*rowCurs >= '0' && *rowCurs <= '9')
val.value( *rowCurs - '0');
else
val = Real::tundef;
nTupla nt4;
nt4.add(row, col);
state[ nt4 ] = val;
col++;
}
}
}
if( iniFile.exists( modelName, "initialCellsValue" ) )
{
string fileName( iniFile.definition( modelName, "initialCellsValue" ).front() );
FILE *fileIn;
char line[250], pos[200], value[50];
fileName = trimSpaces( fileName );
fileIn = fopen( fileName.c_str(), "r" );
MASSERTMSG( fileIn != NULL, "Can't open the file '" + fileName + "' defined by the initialCellsValue clause");
while (!feof(fileIn))
{
fgets(line, 255, fileIn);
if (line != NULL & splitLine(line, pos, value)){
CellPosition cp2(pos);
state[ cp2 ] = str2Real(value);
}
}
fclose( fileIn );
}
if( iniFile.exists( modelName, "initialMapValue" ) )
{
string fileName( iniFile.definition( modelName, "initialMapValue" ).front() );
FILE *fileIn;
char line[250];
fileName = trimSpaces( fileName );
fileIn = fopen( fileName.c_str(), "r" );
MASSERTMSG( fileIn != NULL, "Can't open the file '" + fileName + "' defined by the initialMapValue clause");
CellPosition counter( nt.dimension(), 0 );
register bool overflow = false;
while (!overflow)
{
MASSERTMSG( !feof( fileIn ) && fgets(line, 255, fileIn), "Insuficient data in file specified with InitialMapValue");
state[ counter ] = str2Real(line);
overflow = counter.next( nt );
}
fclose( fileIn );
}
//////////////////////////////////////////////////////////////
// Now, the initial state is already loaded.
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Open the logfiles and load the first lines of each
//////////////////////////////////////////////////////////////
VTime currentTime( VTime::Inf ), nextShowTime( initial ), nextTime (VTime::Inf);
istream **logStreams ;
VTime *fileTimes;
char **lines;
int filecounter, filecount;
if ( logName != "-" )
{
MASSERT(modelsLogFiles.exists( "logfiles" , modelName ) );
const Ini::IdList &files( modelsLogFiles.definition( "logfiles", modelName ) ) ;
filecount = files.size();
logStreams = new istream*[filecount];
lines= new char*[filecount];
fileTimes = new VTime[filecount];
Ini::IdList::const_iterator cursor = files.begin();
for ( filecounter = 0; cursor != files.end() ; cursor++, filecounter++ )
{
logStreams[filecounter] = new fstream((*cursor).c_str(), ios::in);
lines[filecounter] = new char[2048];
lines[filecounter][0] = '\0';
fileTimes[filecounter] = getNextMsgLine(*(logStreams[filecounter]), modelName, lines[filecounter], strPort);
if ( fileTimes[filecounter] < currentTime )
currentTime = fileTimes[filecounter];
}
}
else
{
filecount = 1;
logStreams = new istream*[1];
lines = new char* [1];
fileTimes = new VTime[1];
logStreams[0] = new fstream("/dev/stdin", ios::in);
lines[0] = new char[2048];
lines[0][0] = '\0';
fileTimes[0] = getNextMsgLine(*(logStreams[0]), modelName, lines[0], strPort);
if ( fileTimes[0] < currentTime )
currentTime = fileTimes[0];
}
//////////////////////////////////////////////////////////////
// All the files are now open
//////////////////////////////////////////////////////////////
bool val = false ;
int lineCount(1) ;
CellPosition nt5;
//Initially, make nextTime = currentTime, to go through the loop at least once.
nextTime = currentTime;
do
{
while( nextTime <= nextShowTime )
{
//El nextTime deberia ser mayor o igual al currentTime
MASSERT( currentTime <= nextTime);
currentTime = nextTime;
//On every loop, calculate the new nextTime
nextTime = VTime::Inf;
for (filecounter = 0; filecounter < filecount; filecounter++)
{
if (fileTimes[filecounter] == currentTime)
{
val = parseLine( lines[filecounter], currentTime, modelName, nt5, value, strPort ) ;
//Todas las lineas deberian ser validas
MASSERT(val);
state[ nt5 ] = value ;
fileTimes[filecounter] = getNextMsgLine(*(logStreams[filecounter]), modelName, lines[filecounter], strPort);
}
if ( fileTimes[filecounter] < nextTime )
nextTime = fileTimes[filecounter];
}//for
}
if (!Impresion::Default.FlatLog())
cout << "Line : " << lineCount << " - " ;
printState( state, nextShowTime ) ;
if ( timeInterval == VTime::InvalidTime )
nextShowTime = currentTime ;
else
nextShowTime = nextShowTime + timeInterval;
} while( !(nextTime == VTime::Inf) ) ;
for( filecounter = 0; filecounter < filecount; filecounter++)
{
delete logStreams[filecounter];
delete lines[filecounter];
}
delete logStreams;
delete lines;
delete fileTimes;
} catch( MException &e )
{
e.print(cerr);
} catch( ... )
{
cerr << "Unknown exception! " << endl ;
}
}
/**********************************************************************
*getNextMsgLine: Stores in buffer the next valid msg line and returns the
*time of the message. If no valid line is found, it returns VTime::Inf
***********************************************************************/
VTime getNextMsgLine( istream& file, const string& modelName, char* buffer, const string &portName )
{
bool valid;
VTime time;
CellPosition cellPos;
Real value;
valid = false;
while ( !valid && file.good() && !file.eof() ) {
file.getline( buffer, 2048 );
valid = parseLine( buffer, time, modelName, cellPos, value, portName ) ;
}
if ( !valid )
time = VTime::Inf;
return time;
}