forked from matthb2/ParaViewSyncIOReaderPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtkPhastaSyncIOReader.cxx
1826 lines (1510 loc) · 45.9 KB
/
vtkPhastaSyncIOReader.cxx
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkPhastaSyncIOReader.cxx,v $
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPhastaSyncIOReader.h"
#include "vtkByteSwap.h"
#include "vtkCellType.h" //added for constants such as VTK_TETRA etc...
#include "vtkDataArray.h"
#include "vtkIntArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkPointSet.h"
#include "vtkSmartPointer.h"
#include "vtkUnstructuredGrid.h"
//change
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkPVXMLElement.h"
#include "vtkPVXMLParser.h"
#include "vtkCellData.h"
#include "vtkFieldData.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiPieceDataSet.h"
//change end
#include <map>
#include <vector>
#include <string>
#include <sstream>
//CHANGE////////////////////////////////////////////////////////////////
#include "rdtsc.h"
#define clockRate 2670000000.0
unsigned long long start, end;
//double opentime_total = 0.0;
int LAST_FILE_ID;
void startTimer(unsigned long long* start) {
*start = rdtsc();
}
void endTimer(unsigned long long* end) {
*end = rdtsc();
}
void computeTime(unsigned long long* start, unsigned long long* end) {
double time = (double)((*end-*start)/clockRate);
opentime_total += time;
}
#define VERSION_INFO_HEADER_SIZE 8192
#define DB_HEADER_SIZE 1024
#define TWO_MEGABYTE 2097152
#define ENDIAN_TEST_NUMBER 12180 // Troy's Zip Code!!
#define MAX_PHASTA_FILES 64
#define MAX_PHASTA_FILE_NAME_LENGTH 1024
#define MAX_FIELDS_NUMBER 48
#define MAX_FIELDS_NAME_LENGTH 128
#define DefaultMHSize (4*1024*1024)
int MasterHeaderSize = DefaultMHSize;
int diff_endian = 0;
long long counter = 0;
enum PhastaIO_Errors
{
MAX_PHASTA_FILES_EXCEEDED = -1,
UNABLE_TO_OPEN_FILE = -2,
NOT_A_MPI_FILE = -3,
GPID_EXCEEDED = -4,
DATA_TYPE_ILLEGAL = -5,
};
//CHANGE END////////////////////////////////////////////////////////////
struct vtkPhastaSyncIOReaderInternal
{
struct FieldInfo
{
int StartIndexInPhastaArray;
int NumberOfComponents;
int DataDependency; // 0-nodal, 1-elemental
std::string DataType; // "int" or "double"
std::string PhastaFieldTag;
FieldInfo() : StartIndexInPhastaArray(-1), NumberOfComponents(-1), DataDependency(-1), DataType(""), PhastaFieldTag("")
{
}
};
typedef std::map<std::string, FieldInfo> FieldInfoMapType;
FieldInfoMapType FieldInfoMap;
};
// Begin of copy from phastaIO
//CHANGE////////////////////////////////////////////////////////////////
/***********************************************************************/
/***************** NEW PHASTA IO CODE STARTS HERE **********************/
/***********************************************************************/
int partID_counter;
typedef struct
{
bool Wrong_Endian; /* default to false */
char filename[MAX_PHASTA_FILE_NAME_LENGTH]; /* defafults to 1024 */
int nppp;
int nPPF;
int nFiles;
int nFields;
unsigned long long my_offset;
char * master_header;
double * double_chunk;
int * int_chunk;
double * read_double_chunk;
int * read_int_chunk;
unsigned long long **my_offset_table;
unsigned long long **my_read_table;
int field_count;
int part_count;
int read_field_count;
int read_part_count;
int GPid;
int start_id;
unsigned long long next_start_address;
int myrank;
int numprocs;
int local_myrank;
int local_numprocs;
} phastaio_file_t;
//default: Paraview disabled
typedef struct
{
int fileID;
int nppf, nfields;
int GPid;
int read_field_count;
char * masterHeader;
unsigned long long **offset_table;
unsigned long long my_offset;
}serial_file;
serial_file *SerialFile;
phastaio_file_t *PhastaIOActiveFiles[MAX_PHASTA_FILES];
int PhastaIONextActiveIndex = 0; /* indicates next index to allocate */
//CHANGE END////////////////////////////////////////////////////////////
#define swap_char(A,B) { ucTmp = A; A = B ; B = ucTmp; }
std::map< int , char* > LastHeaderKey;
std::vector< FILE* > fileArray;
std::vector< int > byte_order;
std::vector< int > header_type;
int DataSize=0;
int LastHeaderNotFound = 0;
int Wrong_Endian = 0 ;
int Strict_Error = 0 ;
int binary_format = 0;
vtkStandardNewMacro(vtkPhastaSyncIOReader)
// the caller has the responsibility to delete the returned string
char* vtkPhastaSyncIOReader::StringStripper( const char istring[] )
{
int length = strlen( istring );
char* dest = new char [ length + 1 ];
strcpy( dest, istring );
dest[ length ] = '\0';
if ( char* p = strpbrk( dest, " ") )
{
*p = '\0';
}
return dest;
}
int vtkPhastaSyncIOReader::cscompare( const char teststring[],
const char targetstring[] )
{
char* s1 = const_cast<char*>(teststring);
char* s2 = const_cast<char*>(targetstring);
while( *s1 == ' ') { s1++; }
while( *s2 == ' ') { s2++; }
while( ( *s1 )
&& ( *s2 )
&& ( *s2 != '?')
&& ( tolower( *s1 )==tolower( *s2 ) ) )
{
s1++;
s2++;
while( *s1 == ' ') { s1++; }
while( *s2 == ' ') { s2++; }
}
if ( !( *s1 ) || ( *s1 == '?') )
{
return 1;
}
else
{
return 0;
}
}
void vtkPhastaSyncIOReader::isBinary( const char iotype[] )
{
char* fname = StringStripper( iotype );
if ( cscompare( fname, "binary" ) )
{
binary_format = 1;
}
else
{
binary_format = 0;
}
delete [] fname;
}
size_t vtkPhastaSyncIOReader::typeSize( const char typestring[] )
{
char* ts1 = StringStripper( typestring );
if ( cscompare( "integer", ts1 ) )
{
delete [] ts1;
return sizeof(int);
}
else if ( cscompare( "double", ts1 ) )
{
delete [] ts1;
return sizeof( double );
}
else if ( cscompare( "float", ts1 ) )
{
delete [] ts1;
return sizeof( float );
}
else
{
delete [] ts1;
fprintf(stderr,"unknown type : %s\n",ts1);
return 0;
}
}
int vtkPhastaSyncIOReader::readHeader( FILE* fileObject,
const char phrase[],
int* params,
int expect )
{
char* text_header;
char* token;
char Line[1024];
char junk;
int FOUND = 0 ;
int real_length;
int skip_size, integer_value;
int rewind_count=0;
if( !fgets( Line, 1024, fileObject ) && feof( fileObject ) )
{
rewind( fileObject );
clearerr( fileObject );
rewind_count++;
fgets( Line, 1024, fileObject );
}
while( !FOUND && ( rewind_count < 2 ) )
{
if ( ( Line[0] != '\n' ) && ( real_length = strcspn( Line, "#" )) )
{
text_header = new char [ real_length + 1 ];
strncpy( text_header, Line, real_length );
text_header[ real_length ] =static_cast<char>(NULL);
token = strtok ( text_header, ":" );
if( cscompare( phrase , token ) )
{
FOUND = 1 ;
token = strtok( NULL, " ,;<>" );
skip_size = atoi( token );
int i;
for( i=0; i < expect && ( token = strtok( NULL," ,;<>") ); i++)
{
params[i] = atoi( token );
}
if ( i < expect )
{
fprintf(stderr,"Expected # of ints not found for: %s\n",phrase );
}
}
else if ( cscompare(token,"byteorder magic number") )
{
if ( binary_format )
{
fread((void*)&integer_value,sizeof(int),1,fileObject);
fread( &junk, sizeof(char), 1 , fileObject );
if ( 362436 != integer_value )
{
Wrong_Endian = 1;
}
}
else
{
fscanf(fileObject, "%d\n", &integer_value );
}
}
else
{
/* some other header, so just skip over */
token = strtok( NULL, " ,;<>" );
skip_size = atoi( token );
if ( binary_format)
{
fseek( fileObject, skip_size, SEEK_CUR );
}
else
{
for( int gama=0; gama < skip_size; gama++ )
{
fgets( Line, 1024, fileObject );
}
}
}
delete [] text_header;
}
if ( !FOUND )
{
if( !fgets( Line, 1024, fileObject ) && feof( fileObject ) )
{
rewind( fileObject );
clearerr( fileObject );
rewind_count++;
fgets( Line, 1024, fileObject );
}
}
}
if ( !FOUND )
{
fprintf(stderr, "Error: Cound not find: %s\n", phrase);
return 1;
}
return 0;
}
void vtkPhastaSyncIOReader::SwapArrayByteOrder_( void* array,
int nbytes,
int nItems )
{
/* This swaps the byte order for the array of nItems each
of size nbytes , This will be called only locally */
int i,j;
unsigned char ucTmp;
unsigned char* ucDst = (unsigned char*)array;
for(i=0; i < nItems; i++)
{
for(j=0; j < (nbytes/2); j++)
{
swap_char( ucDst[j] , ucDst[(nbytes - 1) - j] );
}
ucDst += nbytes;
}
}
//CHANGE///////////////////////////////////////////////////////
void vtkPhastaSyncIOReader::queryphmpiio_(const char filename[],int *nfields, int *nppf)
{
FILE * fileHandle;
char* fname = StringStripper( filename );
fileHandle = fopen (fname,"rb");
if (fileHandle == NULL ) {
printf("\n File %s doesn't exist! Please check!\n",fname);
exit(1);
}
else
{
SerialFile =(serial_file *)calloc( 1, sizeof( serial_file) );
SerialFile->masterHeader = (char *)malloc(MasterHeaderSize);
fread(SerialFile->masterHeader,1,MasterHeaderSize,fileHandle);
char read_out_tag[MAX_FIELDS_NAME_LENGTH];
char * token;
int magic_number;
memcpy( read_out_tag,
SerialFile->masterHeader,
MAX_FIELDS_NAME_LENGTH-1 );
if ( cscompare ("MPI_IO_Tag",read_out_tag) )
{
// Test endianess ...
memcpy ( &magic_number,
SerialFile->masterHeader+sizeof("MPI_IO_Tag :"),
sizeof(int) );
if ( magic_number == ENDIAN_TEST_NUMBER ) diff_endian = 0;
else diff_endian = 1;
char version[MAX_FIELDS_NAME_LENGTH/4];
int mhsize;
memcpy(version,
SerialFile->masterHeader + MAX_FIELDS_NAME_LENGTH/2,
MAX_FIELDS_NAME_LENGTH/4 - 1); //TODO: why -1?
if( cscompare ("version",version) )
{
// if there is "version" tag in the file, then it is newer format
// read master header size from here, otherwise use default
// TODO: if version is "1", we know mhsize is at 3/4 place...
token = strtok(version, ":");
token = strtok(NULL, " ,;<>" );
int iversion = atoi(token);
if( iversion == 1) {
memcpy( &mhsize,
SerialFile->masterHeader + MAX_FIELDS_NAME_LENGTH/4*3 + sizeof("mhsize : ")-1,
sizeof(int));
if ( diff_endian)
SwapArrayByteOrder_(&mhsize, sizeof(int), 1);
free(SerialFile->masterHeader);
SerialFile->masterHeader = (char *)malloc(mhsize);
fseek(fileHandle, 0, SEEK_SET);
fread(SerialFile->masterHeader,1,mhsize,fileHandle);
}
//TODO: check if this is a valid int??
MasterHeaderSize = mhsize;
}
else { // else it's version 0's format w/o version tag, implicating MHSize=4M
MasterHeaderSize = DefaultMHSize;
//printf("-----> version = 0; mhsize = %d\n", MasterHeaderSize);
}
// END OF CHANGE FOR VERSION
//
memcpy( read_out_tag,
SerialFile->masterHeader+MAX_FIELDS_NAME_LENGTH+1,
MAX_FIELDS_NAME_LENGTH );
// Read in # fields ...
token = strtok ( read_out_tag, ":" );
token = strtok( NULL," ,;<>" );
*nfields = atoi( token );
SerialFile->nfields=*nfields;
memcpy( read_out_tag,
SerialFile->masterHeader+
*nfields * MAX_FIELDS_NAME_LENGTH +
MAX_FIELDS_NAME_LENGTH * 2,
MAX_FIELDS_NAME_LENGTH);
token = strtok ( read_out_tag, ":" );
token = strtok( NULL," ,;<>" );
*nppf = atoi( token );
SerialFile->nppf=*nppf;
}
else
{
printf("The file you opened is not new format, please check!\n");
}
fclose(fileHandle);
}
delete [] fname;
}
void vtkPhastaSyncIOReader::finalizephmpiio_( int *fileDescriptor )
{
//printf("total open time is %lf\n", opentime_total);
// free master header, offset table [][], and serial file struc
free( SerialFile->masterHeader);
int j;
for ( j = 0; j < SerialFile->nfields; j++ )
{
free( SerialFile->offset_table[j] );
}
free( SerialFile->offset_table);
free( SerialFile );
}
char* StrReverse(char* str)
{
char *temp, *ptr;
int len, i;
temp=str;
for(len=0; *temp !='\0';temp++, len++);
ptr=(char*)malloc(sizeof(char)*(len+1));
for(i=len-1; i>=0; i--)
ptr[len-i-1]=str[i];
ptr[len]='\0';
return ptr;
}
//CHANGE END//////////////////////////////////////////////////
void vtkPhastaSyncIOReader::openfile( const char filename[],
const char mode[],
int* fileDescriptor )
//CHANGE////////////////////////////////////////////////////
{
//printf("in open(): counter = %ld\n", counter++);
FILE* file=NULL ;
*fileDescriptor = 0;
char* fname = StringStripper( filename );
char* imode = StringStripper( mode );
int string_length = strlen( fname );
char* buffer = (char*) malloc ( string_length+1 );
strcpy ( buffer, fname );
buffer[ string_length ] = '\0';
char* tempbuf = StrReverse(buffer);
free(buffer);
buffer = tempbuf;
//printf("buffer is %s\n",buffer);
char* st2 = strtok ( buffer, "." );
//st2 = strtok (NULL, ".");
//printf("st2 is %s\n",st2);
string_length = strlen(st2);
char* buffer2 = (char*)malloc(string_length+1);
strcpy(buffer2,st2);
buffer2[string_length]='\0';
char* tempbuf2 = StrReverse(buffer2);
free(buffer2);
buffer2 = tempbuf2;
//printf("buffer2 is %s\n",buffer2);
SerialFile->fileID = atoi(buffer2);
if ( char* p = strpbrk(buffer, "@") )
*p = '\0';
startTimer(&start);
if ( cscompare( "read", imode ) ) file = fopen(fname, "rb" );
else if( cscompare( "write", imode ) ) file = fopen(fname, "wb" );
else if( cscompare( "append", imode ) ) file = fopen(fname, "ab" );
endTimer(&end);
computeTime(&start, &end);
if ( !file ){
fprintf(stderr,"unable to open file : %s\n",fname ) ;
} else {
fileArray.push_back( file );
byte_order.push_back( false );
header_type.push_back( sizeof(int) );
*fileDescriptor = fileArray.size();
}
////////////////////////////////////////////////
//unsigned long long **header_table;
SerialFile->offset_table = ( unsigned long long ** )calloc(SerialFile->nfields,
sizeof(unsigned long long *));
int j;
for ( j = 0; j < SerialFile->nfields; j++ )
{
SerialFile->offset_table[j]=( unsigned long long * ) calloc( SerialFile->nppf ,
sizeof( unsigned long long));
}
// Read in the offset table ...
for ( j = 0; j < SerialFile->nfields; j++ )
{
memcpy( SerialFile->offset_table[j],
SerialFile->masterHeader +
VERSION_INFO_HEADER_SIZE +
j * SerialFile->nppf * sizeof(unsigned long long),
SerialFile->nppf * sizeof(unsigned long long) );
if(diff_endian) {
SwapArrayByteOrder_( SerialFile->offset_table[j],
sizeof(unsigned long long int),
SerialFile->nppf);
}
// Swap byte order if endianess is different ...
/*if ( PhastaIOActiveFiles[i]->Wrong_Endian )
{
SwapArrayByteOrder_( PhastaIOActiveFiles[i]->my_read_table[j],
sizeof(long long int),
PhastaIOActiveFiles[i]->nppp );
}
*/
}
////////////////////////////////////////////////
delete [] fname;
delete [] imode;
//free(fname);
//free(imode);
free(buffer);
free(buffer2);
}
//CHANGE END////////////////////////////////////////////////
void vtkPhastaSyncIOReader::closefile( int* fileDescriptor,
const char mode[] )
//CHANGE///////////////////////////////////////////////
{
char* imode = StringStripper( mode );
if( cscompare( "write", imode )
|| cscompare( "append", imode ) ) {
fflush( fileArray[ *fileDescriptor - 1 ] );
}
fclose( fileArray[ *fileDescriptor - 1 ] );
delete [] imode;
}
//CHANGE END///////////////////////////////////////////
void vtkPhastaSyncIOReader::readheader( int* fileDescriptor,
const char keyphrase[],
void* valueArray,
int* nItems,
const char datatype[],
const char iotype[] )
//CHANGE////////////////////////////////////////////////////
{
int filePtr = *fileDescriptor - 1;
FILE* fileObject;
int* valueListInt;
if ( *fileDescriptor < 1 || *fileDescriptor > (int)fileArray.size() ) {
fprintf(stderr,"No file associated with Descriptor %d\n",*fileDescriptor);
fprintf(stderr,"openfile_ function has to be called before \n") ;
fprintf(stderr,"acessing the file\n ") ;
fprintf(stderr,"fatal error: cannot continue, returning out of call\n");
return;
}
LastHeaderKey[ filePtr ] = const_cast< char* >( keyphrase );
LastHeaderNotFound = false;
fileObject = fileArray[ filePtr ] ;
Wrong_Endian = byte_order[ filePtr ];
isBinary( iotype );
typeSize( datatype ); //redundant call, just avoid a compiler warning.
// right now we are making the assumption that we will only write integers
// on the header line.
valueListInt = static_cast< int* >( valueArray );
/////////////////////////////////////////////////////////
int j;
bool FOUND = false ;
unsigned int skip_size;
char * token;
char readouttag[MAX_FIELDS_NUMBER][MAX_FIELDS_NAME_LENGTH];
int string_length = strlen( keyphrase );
char* buffer = (char*) malloc ( string_length+1 );
strcpy ( buffer, keyphrase );
buffer[ string_length ] = '\0';
char* st2 = strtok ( buffer, "@" );
st2 = strtok (NULL, "@");
SerialFile->GPid = atoi(st2);
if ( char* p = strpbrk(buffer, "@") )
*p = '\0';
//printf("field is %s and nfields is %d\n",keyphrase,SerialFile->nfields);
for ( j = 0; j<SerialFile->nfields; j++ )
{
memcpy( readouttag[j],
SerialFile->masterHeader + j*MAX_FIELDS_NAME_LENGTH+MAX_FIELDS_NAME_LENGTH*2+1,
MAX_FIELDS_NAME_LENGTH-1 );
}
for ( j = 0; j<SerialFile->nfields; j++ )
{
token = strtok ( readouttag[j], ":" );
if ( cscompare( buffer, token ) )
{
SerialFile->read_field_count = j;
FOUND = true;
break;
}
}
if (!FOUND)
{
printf("Not found %s \n",keyphrase);
return;
}
int read_part_count = SerialFile->GPid - ( SerialFile->fileID - 1 ) * SerialFile->nppf - 1;
SerialFile->my_offset = SerialFile->offset_table[SerialFile->read_field_count][read_part_count];
//printf("GP id is %d and fileID is %d and nppf is %d; ",SerialFile->GPid,SerialFile->fileID,SerialFile->nppf);
//printf("read field count is %d and read part count is %d; ",SerialFile->read_field_count,read_part_count);
char read_out_header[MAX_FIELDS_NAME_LENGTH];
fseek(fileObject, SerialFile->my_offset+1, SEEK_SET);
fread( read_out_header, 1, MAX_FIELDS_NAME_LENGTH-1, fileObject );
token = strtok ( read_out_header, ":" );
if( cscompare( keyphrase , token ) )
{
FOUND = true ;
token = strtok( NULL, " ,;<>" );
skip_size = atoi( token );
for( j=0; j < *nItems && ( token = strtok( NULL," ,;<>") ); j++ )
valueListInt[j] = atoi( token );
//printf("$$Keyphrase is %s Value list [0] is %d \n",keyphrase,valueListInt[0] );
if ( j < *nItems )
{
fprintf( stderr, "Expected # of ints not found for: %s\n", keyphrase );
}
}
/////////////////////////////////////////////////////////
byte_order[ filePtr ] = Wrong_Endian ;
//if ( ierr ) LastHeaderNotFound = true;
free(buffer);
return;
}
//CHANGE END////////////////////////////////////////////////
void vtkPhastaSyncIOReader::readdatablock( int* fileDescriptor,
const char keyphrase[],
void* valueArray,
int* nItems,
const char datatype[],
const char iotype[] )
//CHANGE//////////////////////////////////////////////////////
{
int filePtr = *fileDescriptor - 1;
FILE* fileObject;
char junk;
if ( *fileDescriptor < 1 || *fileDescriptor > (int)fileArray.size() ) {
fprintf(stderr,"No file associated with Descriptor %d\n",*fileDescriptor);
fprintf(stderr,"openfile_ function has to be called before \n") ;
fprintf(stderr,"acessing the file\n ") ;
fprintf(stderr,"fatal error: cannot continue, returning out of call\n");
return;
}
// error check..
// since we require that a consistant header always preceed the data block
// let us check to see that it is actually the case.
if ( ! cscompare( LastHeaderKey[ filePtr ], keyphrase ) ) {
fprintf(stderr, "Header not consistant with data block\n");
fprintf(stderr, "Header: %s\n", LastHeaderKey[ filePtr ] );
fprintf(stderr, "DataBlock: %s\n ", keyphrase );
fprintf(stderr, "Please recheck read sequence \n");
if( Strict_Error ) {
fprintf(stderr, "fatal error: cannot continue, returning out of call\n");
return;
}
}
if ( LastHeaderNotFound ) return;
fileObject = fileArray[ filePtr ];
Wrong_Endian = byte_order[ filePtr ];
//printf("in readdatablock(): wrong_endian = %d\n", Wrong_Endian);
size_t type_size = typeSize( datatype );
int nUnits = *nItems;
isBinary( iotype );
if ( binary_format ) {
fseek(fileObject, SerialFile->my_offset+DB_HEADER_SIZE, SEEK_SET);
fread( valueArray, type_size, nUnits, fileObject );
//fread( &junk, sizeof(char), 1 , fileObject );
//if ( Wrong_Endian ) SwapArrayByteOrder_( valueArray, type_size, nUnits );
if ( diff_endian )
SwapArrayByteOrder_( valueArray, type_size, nUnits ); // fj
} else {
char* ts1 = StringStripper( datatype );
if ( cscompare( "integer", ts1 ) ) {
for( int n=0; n < nUnits ; n++ )
fscanf(fileObject, "%d\n",(int*)((int*)valueArray+n) );
} else if ( cscompare( "double", ts1 ) ) {
for( int n=0; n < nUnits ; n++ )
fscanf(fileObject, "%lf\n",(double*)((double*)valueArray+n) );
}
delete [] ts1;
}
return;
}
// End of copy from phastaIO
vtkPhastaSyncIOReader::vtkPhastaSyncIOReader()
{
//this->DebugOn(); // TODO: comment out this line to turn off debug
this->GeometryFileName = NULL;
this->FieldFileName = NULL;
this->SetNumberOfInputPorts(0);
this->Internal = new vtkPhastaSyncIOReaderInternal;
//////////
this->Parser = 0;
this->FileName = 0;
//////////
}
vtkPhastaSyncIOReader::~vtkPhastaSyncIOReader()
{
if (this->GeometryFileName)
{
delete [] this->GeometryFileName;
}
if (this->FieldFileName)
{
delete [] this->FieldFileName;
}
delete this->Internal;
////////////////////
if (this->Parser)
this->Parser->Delete();
////////////////////
}
void vtkPhastaSyncIOReader::ClearFieldInfo()
{
this->Internal->FieldInfoMap.clear();
}
void vtkPhastaSyncIOReader::SetFieldInfo(const char* paraviewFieldTag,
const char* phastaFieldTag,
int index,
int numOfComps,
int dataDependency,
const char* dataType)
{
//printf("In P setfino\n");
//CHANGE/////////////////////////
partID_counter=0;
//CHANGE END/////////////////////
vtkPhastaSyncIOReaderInternal::FieldInfo &info =
this->Internal->FieldInfoMap[paraviewFieldTag];
info.PhastaFieldTag = phastaFieldTag;
info.StartIndexInPhastaArray = index;
info.NumberOfComponents = numOfComps;
info.DataDependency = dataDependency;
info.DataType = dataType;
}
int vtkPhastaSyncIOReader::RequestData(vtkInformation*,
vtkInformationVector**,
vtkInformationVector* outputVector)
{
vtkDebugMacro("In P RequestData");
int firstVertexNo = 0;
int fvn = 0;
int noOfNodes, noOfCells, noOfDatas;
// get the data object
//TODO Just Testing
vtkSmartPointer<vtkInformation> outInfo =
outputVector->GetInformationObject(0);
//change/////// This part not working
// get the current piece being requested
int piece =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
//printf("piece is %d\n",piece);
//partID_counter++;
partID_counter=PART_ID;
//change end////////////////
vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
//printf("This geom file is %s\n",this->GeometryFileName);
int numPieces=NUM_PIECES, numFiles=NUM_FILES, timeStep=TIME_STEP;
//int numPieces=8, numFiles=4, timeStep=50400;
int numPiecesPerFile = numPieces/numFiles;
int fileID;
fileID = int((partID_counter-1)/numPiecesPerFile)+1;
vtkDebugMacro(<< "FILE_PATH: " << FILE_PATH);
// FILE_PATH is set to be the path of .pht file ?
//sprintf(this->FieldFileName,"%s%s.%d.%d",FILE_PATH,"/restart-dat",timeStep,fileID); // this is Ning's version
// the file id of fieldfilename need to be changed to file id now
char* str = this->FieldFileName;
for (int i = strlen(this->FieldFileName); i >= 0; i--) {
if(str[i] != '.')
str[i] = 0;
else
break;
}
sprintf(str, "%s%d", str, FILE_ID);
vtkDebugMacro(<<"tweaked FieldFileName="<<this->FieldFileName);
///////////////////////////////////////////////////////////
vtkPoints *points;
output->Allocate(10000, 2100);
points = vtkPoints::New(VTK_DOUBLE);
vtkDebugMacro(<<"Reading Phasta file...");
if(!this->GeometryFileName || !this->FieldFileName )
{
vtkErrorMacro(<<"All input parameters not set.");
return 0;
}
vtkDebugMacro(<< "Updating ensa with ....");
vtkDebugMacro(<< "Geom File : " << this->GeometryFileName);
vtkDebugMacro(<< "Field File : " << this->FieldFileName);
fvn = firstVertexNo;
this->ReadGeomFile(this->GeometryFileName, firstVertexNo, points, noOfNodes, noOfCells);
/* set the points over here, this is because vtkUnStructuredGrid
only insert points once, next insertion overwrites the previous one */
// acbauer is not sure why the above comment is about...
output->SetPoints(points);
points->Delete();
if (!this->Internal->FieldInfoMap.size())
{
vtkDataSetAttributes* field = output->GetPointData();
this->ReadFieldFile(this->FieldFileName, fvn, field, noOfNodes);