/
vtkLSDynaReader.cxx
5114 lines (4593 loc) · 161 KB
/
vtkLSDynaReader.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: vtkLSDynaReader.cxx
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.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
// NOTE TO DEVELOPERS: ========================================================
//
// This is a really big file.
// It contains several private classes:
// - vtkLSDynaFamily:
// A class to abstract away I/O from families of output files.
// This performs the actual reads and writes plus any required byte swapping.
// Also contains a subclass, vtkLSDynaFamilyAdaptLevel, used to store
// file+offset
// information for each mesh adaptation's state info.
// - vtkLSDynaReaderPrivate:
// A class to hold metadata about a particular file (such as time steps,
// the start of state information for each time step, the number of
// adaptive remeshes, and the large collection of constants that determine
// the available attributes). It contains an vtkLSDynaFamily instance.
// - vtkLSDynaReaderXMLParser:
// A class to parse XML summary files containing part names and their IDs.
// This class is used by vtkLSDynaReader::ReadInputDeckXML().
// - vtkLSDynaReader:
// The implementation of the actual public VTK interface.
// These classes are preceded by some file-static constants and utility routines.
#include <vtkConfigure.h>
#include "vtkLSDynaReader.h"
#include "vtksys/SystemTools.hxx"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <assert.h>
#ifndef WIN32
# include <unistd.h>
typedef off_t vtkLSDynaOff_t; // sanity
typedef int vtkLSDynaFile_t;
# define VTK_LSDYNA_BADFILE -1
# define VTK_LSDYNA_TELL(fid) lseek( fid, 0, SEEK_CUR )
# define VTK_LSDYNA_SEEK(fid,off,whence) lseek( fid, off, whence )
# define VTK_LSDYNA_SEEKTELL(fid,off,whence) lseek( fid, off, whence )
# define VTK_LSDYNA_READ(fid,ptr,cnt) read(fid,ptr,cnt)
# define VTK_LSDYNA_ISBADFILE(fid) (fid < 0)
#else // WIN32
# include <stdio.h>
typedef long vtkLSDynaOff_t; // insanity
typedef FILE* vtkLSDynaFile_t;
# define VTK_LSDYNA_BADFILE 0
# define VTK_LSDYNA_TELL(fid) ftell( fid )
# define VTK_LSDYNA_SEEK(fid,off,whence) fseek( fid, off, whence )
# define VTK_LSDYNA_SEEKTELL(fid,off,whence) fseek( fid, off, whence ), ftell( fid )
# define VTK_LSDYNA_READ(fid,ptr,cnt) fread(ptr,1,cnt,fid)
# define VTK_LSDYNA_ISBADFILE(fid) (fid == 0)
#endif
#ifdef VTKSNL_HAVE_ERRNO_H
# include <errno.h>
#endif
#include <vtkstd/string>
#include <vtkstd/set>
#include <vtkstd/vector>
#include <vtkstd/algorithm>
#include <vtkstd/map>
#include <vtkCellData.h>
#include <vtkCellType.h>
#include <vtkDataObject.h>
#include <vtkDoubleArray.h>
#include <vtkIdTypeArray.h>
#include <vtkIntArray.h>
#include <vtkFloatArray.h>
#include <vtkPoints.h>
#include <vtkPointData.h>
#include <vtkInformation.h>
#include <vtkInformationDoubleVectorKey.h>
#include <vtkInformationVector.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkMultiThreshold.h>
#include <vtkObjectFactory.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkSystemIncludes.h>
#include <vtkThreshold.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLParser.h>
// Define this to print out information on the structure of the reader's output
#undef VTK_LSDYNA_DBG_MULTIBLOCK
#ifdef VTK_LSDYNA_DBG_MULTIBLOCK
#include <vtkMultiGroupDataInformation.h>
#endif // VTK_LSDYNA_DBG_MULTIBLOCK
vtkStandardNewMacro(vtkLSDynaReader);
// Names of vtkDataArrays provided with grid:
#define LS_ARRAYNAME_USERID "UserID"
#define LS_ARRAYNAME_MATERIAL "Material"
#define LS_ARRAYNAME_DEATH "Death"
#define LS_ARRAYNAME_SPECIES_BLNK "SpeciesXX"
#define LS_ARRAYNAME_SPECIES_FMT "Species%02d"
#define LS_ARRAYNAME_SPECIES_01 "Species01"
#define LS_ARRAYNAME_SPECIES_02 "Species02"
#define LS_ARRAYNAME_SPECIES_03 "Species03"
#define LS_ARRAYNAME_SPECIES_04 "Species04"
#define LS_ARRAYNAME_SPECIES_05 "Species05"
#define LS_ARRAYNAME_SPECIES_06 "Species06"
#define LS_ARRAYNAME_SPECIES_07 "Species07"
#define LS_ARRAYNAME_SPECIES_08 "Species08"
#define LS_ARRAYNAME_SPECIES_09 "Species09"
#define LS_ARRAYNAME_SPECIES_10 "Species10"
#define LS_ARRAYNAME_TEMPERATURE "Temperature"
#define LS_ARRAYNAME_DEFLECTION "Deflection"
#define LS_ARRAYNAME_VELOCITY "Velocity"
#define LS_ARRAYNAME_ACCELERATION "Acceleration"
#define LS_ARRAYNAME_PRESSURE "Pressure"
#define LS_ARRAYNAME_VORTICITY "Vorticity"
#define LS_ARRAYNAME_RESULTANTVORTICITY "ResVorticity"
#define LS_ARRAYNAME_ENSTROPHY "Enstrophy"
#define LS_ARRAYNAME_HELICITY "Helicity"
#define LS_ARRAYNAME_STREAMFUNCTION "StreamFunc"
#define LS_ARRAYNAME_ENTHALPY "Enthalpy"
#define LS_ARRAYNAME_DENSITY "Density"
#define LS_ARRAYNAME_TURBULENTKE "TurbulentKE"
#define LS_ARRAYNAME_DISSIPATION "Dissipation"
#define LS_ARRAYNAME_EDDYVISCOSITY "EddyVisc"
#define LS_ARRAYNAME_RADIUSOFINFLUENCE "InfluenceRadius"
#define LS_ARRAYNAME_NUMNEIGHBORS "NumberOfNeighbors"
#define LS_ARRAYNAME_SEGMENTID "SegmentID"
#define LS_ARRAYNAME_STRAIN "Strain"
#define LS_ARRAYNAME_STRESS "Stress"
#define LS_ARRAYNAME_EPSTRAIN "EffPlastStrn"
#define LS_ARRAYNAME_INTEGRATIONPOINT "IntPtData"
#define LS_ARRAYNAME_RESULTANTS "Resultants"
#define LS_ARRAYNAME_ELEMENTMISC "ElementMisc"
#define LS_ARRAYNAME_INTERNALENERGY "InternalEnergy"
#define LS_ARRAYNAME_AXIALFORCE "AxialForce"
#define LS_ARRAYNAME_SHEARRESULTANT "ShearResultant"
#define LS_ARRAYNAME_BENDINGRESULTANT "BendingResultant"
#define LS_ARRAYNAME_TORSIONRESULTANT "TorsionResultant"
#define LS_ARRAYNAME_NORMALRESULTANT "NormalResultant"
#define LS_ARRAYNAME_AXIALSTRAIN "AxialStrain"
#define LS_ARRAYNAME_AXIALSTRESS "AxialStress"
#define LS_ARRAYNAME_SHEARSTRAIN "ShearStrain"
#define LS_ARRAYNAME_SHEARSTRESS "ShearStress"
#define LS_ARRAYNAME_PLASTICSTRAIN "PlasticStrain"
#define LS_ARRAYNAME_THICKNESS "Thickness"
// Possible material deletion options
#define LS_MDLOPT_NONE 0
#define LS_MDLOPT_POINT 1
#define LS_MDLOPT_CELL 2
#ifdef VTK_LSDYNA_DBG_MULTIBLOCK
static void vtkDebugMultiBlockStructure( vtkIndent indent, vtkMultiGroupDataSet* mbds );
#endif // VTK_LSDYNA_DBG_MULTIBLOCK
static const char* vtkLSDynaCellTypes[] =
{
"Point",
"Beam",
"Shell",
"Thick Shell",
"Solid",
"Rigid Body",
"Road Surface"
};
static void vtkLSGetLine( ifstream& deck, vtkstd::string& line )
{
#if !defined(_WIN32) && !defined(WIN32) && !defined(_MSC_VER) && !defined(__BORLANDC__)
// One line implementation for everyone but Windows (MSVC6 and BCC32 are the troublemakers):
vtkstd::getline( deck, line, '\n' );
#else
// Feed Windows its food cut up into little pieces
int linechar;
line = "";
while ( deck.good() )
{
linechar = deck.get();
if ( linechar == '\r' || linechar == '\n' )
return;
line += linechar;
}
#endif // !defined(_WIN32) || !defined(_MSC_VER) || (_MSC_VER < 1200) || (_MSC_VER >= 1300)
}
// Read in lines until one that's
// - not empty, and
// - not a comment
// is encountered. Return with that text stored in \a line.
// If an error or EOF is hit, return 0. Otherwise, return 1.
static int vtkLSNextSignificantLine( ifstream& deck, vtkstd::string& line )
{
while ( deck.good() )
{
vtkLSGetLine( deck, line );
if ( ! line.empty() && line[0] != '$' )
{
return 1;
}
}
return 0;
}
static void vtkLSTrimWhitespace( vtkstd::string& line )
{
vtkstd::string::size_type llen = line.length();
while ( llen &&
( line[llen - 1] == ' ' ||
line[llen - 1] == '\t' ||
line[llen - 1] == '\r' ||
line[llen - 1] == '\n' ) )
{
--llen;
}
vtkstd::string::size_type nameStart = 0;
while ( nameStart < llen &&
( line[nameStart] == ' ' ||
line[nameStart] == '\t' ) )
{
++nameStart;
}
line = line.substr( nameStart, llen - nameStart );
}
static void vtkLSDowncaseFirstWord( vtkstd::string& downcased, const vtkstd::string& line )
{
vtkstd::string::size_type i;
vtkstd::string::value_type chr;
int leadingSpace = 0;
downcased = "";
for ( i = 0; i < line.length(); ++i )
{
chr = tolower( line[i] );
if ( chr == ' ' || chr == '\t' )
{
if ( leadingSpace )
{ // We've trimmed leading whitespace already, so we're done with the word.
return;
}
}
else
{
leadingSpace = 1;
if ( chr == ',' )
{ // We're at a separator (other than whitespace). No need to continue.
return;
}
}
downcased += chr;
}
}
void vtkLSSplitString( vtkstd::string& input, vtkstd::vector<vtkstd::string>& splits, const char* separators )
{
vtkstd::string::size_type posBeg = 0;
vtkstd::string::size_type posEnd;
do {
posEnd = input.find_first_of( separators, posBeg );
if ( posEnd > posBeg )
{
// don't include empty entries in splits.
// NOTE: This means ",comp,1, ,3" with separators ", " yields "comp","1","3", not "","comp","1","","","3".
splits.push_back( input.substr( posBeg, posEnd - posBeg ) );
}
posBeg = input.find_first_not_of( separators, posEnd );
} while ( posBeg != vtkstd::string::npos );
}
vtkstd::string vtkLSGetFamilyFileName( const char* basedir, const vtkstd::string& dbname, int adaptationLvl, int number )
{
vtkstd::string blorb;
blorb = basedir + dbname;
if ( adaptationLvl > 0 )
{
// convert adaptationLvl from an integer to "aa", "ab", "ac", ...
// and tack it onto the end of our blorb.
vtkstd::string slvl;
int a = adaptationLvl - 1;
while ( a )
{
slvl += char(97 + (a % 26) );
a = a / 26;
}
while ( slvl.size() < 2 )
{
slvl += 'a';
}
vtkstd::reverse( slvl.begin(), slvl.end() );
blorb += slvl;
}
if ( number > 0 )
{
char n[4];
sprintf(n, "%02d", number);
blorb += n;
}
return blorb;
}
struct vtkLSDynaFamilySectionMark
{
vtkIdType FileNumber;
vtkIdType Offset;
};
//******************************************************************
//******************************************************************
//******************************************************************
class vtkLSDynaFamily
{
public:
vtkLSDynaFamily();
~vtkLSDynaFamily();
void SetDatabaseDirectory( vtkstd::string dd );
vtkstd::string GetDatabaseDirectory();
void SetDatabaseBaseName( vtkstd::string bn );
vtkstd::string GetDatabaseBaseName();
int ScanDatabaseDirectory();
enum SectionType {
// These are the "section" marks:
// They are absolute (independent of current timestep).
ControlSection=0,
StaticSection,
TimeStepSection,
// These are the "subsection" marks:
// == ControlSection has no subsections
// == StaticSection has these "absolute" marks:
MaterialTypeData,
FluidMaterialIdData,
SPHElementData,
GeometryData,
UserIdData,
AdaptedParentData,
SPHNodeData,
RigidSurfaceData,
EndOfStaticSection,
// == TimeStepSection has these marks, relative to timestep 0 (so they are
// not valid for an arbitrary timestep, but may easily be used to compute
// an offset for any time step by adding a multiple of the state size):
ElementDeletionState,
SPHNodeState,
RigidSurfaceState,
// THIS MUST BE LAST
NumberOfSectionTypes
};
class vtkLSDynaFamilyAdaptLevel
{
public:
vtkLSDynaFamilySectionMark Marks[NumberOfSectionTypes];
vtkLSDynaFamilyAdaptLevel()
{
vtkLSDynaFamilySectionMark mark;
mark.FileNumber = 0;
mark.Offset = 0;
for ( int i=0; i<vtkLSDynaFamily::NumberOfSectionTypes; ++i )
{
this->Marks[i] = mark;
}
}
};
static const char* SectionTypeNames[];
enum WordType {
Char,
Float,
Int
};
static const float EOFMarker;
static const char* SectionTypeToString( SectionType s );
int SkipToWord( SectionType sType, vtkIdType sId, vtkIdType wordNumber );
int MarkTimeStep();
int SkipWords( vtkIdType numWords );
int BufferChunk( WordType wType, vtkIdType chunkSizeInWords );
inline char* GetNextWordAsChars();
inline double GetNextWordAsFloat();
inline vtkIdType GetNextWordAsInt();
// Not needed (yet):
// void GetCurrentWord( SectionType& stype, vtkIdType& sId, vtkIdType& wN );
int AdvanceFile();
void MarkSectionStart( int adapteLevel, SectionType m );
int JumpToMark( SectionType m );
int DetermineStorageModel();
void SetStateSize( vtkIdType sz );
vtkIdType GetStateSize() const;
vtkIdType GetNumberOfFiles();
vtkstd::string GetFileName( int i );
int GetCurrentAdaptLevel() const { return this->FAdapt; }
int TimeAdaptLevel( int i ) const { return this->TimeAdaptLevels[i]; }
/// FIXME: Remove this when done debugging.
vtkIdType GetCurrentFWord() const { return this->FWord; }
int GetWordSize() const;
// Reset erases all information about the current database.
// It does not free memory allocated for the current chunk.
void Reset();
/// Print all adaptation and time step marker information.
void DumpMarks( ostream& os );
protected:
/// The directory containing d3plot files
vtkstd::string DatabaseDirectory;
/// The name (title string) of the database. This is the first 10 words
/// (40 or 80 bytes) of the first file.
vtkstd::string DatabaseBaseName;
/// The list of files that make up the database.
vtkstd::vector<vtkstd::string> Files;
/// The size of each file in the database. Note that they can be padded,
/// so this is >= the amount of data in each file.
vtkstd::vector<vtkLSDynaOff_t> FileSizes;
/// The adaptation level associated with each file.
vtkstd::vector<int> FileAdaptLevels;
/// Which files mark the start of a new mesh adaptation. There is at
/// least one entry and the first entry is always 0.
vtkstd::vector<int> Adaptations;
/// The currently open file descriptor
vtkLSDynaFile_t FD;
/// The index of currently open file descriptor into list of files
vtkIdType FNum;
/// The current adaptation level. This is checked whenever a file is
/// advanced so we can skip its control+geometry headers.
int FAdapt;
/// The offset of Chunk in currently open file
vtkIdType FWord;
/// A comprehensive list of all time values across all files (and mesh
/// adaptations)
//vtkstd::vector<double> TimeValues;
/// The current timestep
vtkIdType TimeStep;
/// Whether files are reverse endian-ness of architecture
int SwapEndian;
/// Whether words are 4 or 8 bytes
int WordSize;
/// How many words is a timestep on disk?
vtkIdType StateSize;
/// A vector of arrays of offsets to various header information sections
/// (that do not vary with timestep), one for each mesh adaptation.
vtkstd::vector<vtkLSDynaFamilyAdaptLevel> AdaptationsMarkers;
/// An array of bookmarks pointing to the start of state information for
/// each timestep.
vtkstd::vector<vtkLSDynaFamilySectionMark> TimeStepMarks;
/// The adaptation level associated with each time step.
vtkstd::vector<int> TimeAdaptLevels;
/// A buffer containing file contents of file FNum starting with word FWord.
unsigned char* Chunk;
/// A pointer to the next word in Chunk that will be returned when the
/// reader requests a word.
vtkIdType ChunkWord;
// How much of the the allocated space is filled with valid data (assert
// ChunkValid <= ChunkAlloc).
vtkIdType ChunkValid;
/// The allocated size (in words) of Chunk.
vtkIdType ChunkAlloc;
};
const char* vtkLSDynaFamily::SectionTypeNames[] =
{
"ControlSection",
"StaticSection",
"TimeStepSection",
"MaterialTypeData",
"FluidMaterialIdData",
"SPHElementData",
"GeometryData",
"UserIdData",
"AdaptedParentData",
"SPHNodeData",
"RigidSurfaceData",
"EndOfStaticSection",
"ElementDeletionState",
"SPHNodeState",
"RigidSurfaceState"
};
const float vtkLSDynaFamily::EOFMarker = -999999.0f;
vtkLSDynaFamily::vtkLSDynaFamily()
{
this->FD = VTK_LSDYNA_BADFILE; // No file open
this->FAdapt = -1; // Invalid adaptation
this->FNum = -1; // No files in filelist
this->FWord = 0; // At start of file
this->SwapEndian = -1; // No endian-ness determined
this->WordSize = 0; // No word size determined
this->TimeStep = 0; // Initial time step
this->StateSize = 0; // Time steps take up no room on disk
this->AdaptationsMarkers.push_back( vtkLSDynaFamilyAdaptLevel() );
this->Chunk = 0;
this->ChunkWord = 0;
this->ChunkAlloc = 0;
}
vtkLSDynaFamily::~vtkLSDynaFamily()
{
if ( ! VTK_LSDYNA_ISBADFILE(this->FD) )
{
#ifndef WIN32
close( this->FD );
#else
fclose( this->FD );
#endif // WIN32
}
if ( this->Chunk )
{
delete [] this->Chunk;
}
}
void vtkLSDynaFamily::SetDatabaseDirectory( vtkstd::string dd )
{
this->DatabaseDirectory = dd;
}
vtkstd::string vtkLSDynaFamily::GetDatabaseDirectory()
{
return this->DatabaseDirectory;
}
void vtkLSDynaFamily::SetDatabaseBaseName( vtkstd::string bn )
{
this->DatabaseBaseName = bn;
}
vtkstd::string vtkLSDynaFamily::GetDatabaseBaseName()
{
return this->DatabaseBaseName;
}
int vtkLSDynaFamily::ScanDatabaseDirectory()
{
// FIXME: None of this need be cleared if we are trying to track a
// simulation in progress. But it won't hurt to redo the scan from the
// beginning... it will just take longer.
this->Files.clear();
this->FileSizes.clear();
this->FileAdaptLevels.clear();
this->TimeAdaptLevels.clear();
this->Adaptations.clear();
this->TimeStepMarks.clear();
vtkstd::string tmpFile;
int filenum = 0;
int adaptLevel = 0;
int tryAdapt = 0; // don't try an adaptive step unless we have one good file at the current level.
bool adapted = true; // true when advancing over a mesh adaptation.
struct stat st;
while ( tryAdapt >= 0 )
{
tmpFile = vtkLSGetFamilyFileName( this->DatabaseDirectory.c_str(),
this->DatabaseBaseName,
adaptLevel,
filenum );
if ( stat( tmpFile.c_str(), &st ) == 0 )
{
if ( adapted )
{
this->Adaptations.push_back( (int)this->Files.size() );
adapted = false;
}
this->Files.push_back( tmpFile );
this->FileSizes.push_back( st.st_size );
this->FileAdaptLevels.push_back( adaptLevel );
tryAdapt = 1;
++filenum;
}
else
{
--tryAdapt;
++adaptLevel;
filenum = 0;
adapted = true;
}
}
return this->Files.size() == 0;
}
const char* vtkLSDynaFamily::SectionTypeToString( SectionType s )
{
return SectionTypeNames[s];
}
int vtkLSDynaFamily::SkipToWord( SectionType sType, vtkIdType sId, vtkIdType wordNumber )
{
vtkLSDynaFamilySectionMark mark;
if ( sType != TimeStepSection && sType < ElementDeletionState )
{
assert( sId < (int)this->Adaptations.size() );
if ( sId < 0 )
sId = 0;
mark = this->AdaptationsMarkers[sId].Marks[ sType ];
mark.Offset += wordNumber;
}
else
{
// NOTE: SkipToWord cannot jump outside of the current adaptation level!
// You must use SetTimeStep() to do that -- it will call ReadHeaderInformation().
mark = this->AdaptationsMarkers[this->FAdapt].Marks[ sType ];
mark.Offset += wordNumber;
if ( sId >= (vtkIdType) this->TimeStepMarks.size() )
{
return 1;
}
mark.FileNumber = this->TimeStepMarks[ sId ].FileNumber;
mark.Offset = this->TimeStepMarks[ sId ].Offset +
( this->AdaptationsMarkers[this->FAdapt].Marks[sType].Offset -
this->AdaptationsMarkers[this->FAdapt].Marks[TimeStepSection].Offset ) +
wordNumber;
}
// if the skip is too big for one file, advance to the correct file
while ( (mark.FileNumber < (vtkIdType) this->Files.size()) && (mark.Offset > this->FileSizes[ mark.FileNumber ]) )
{
mark.Offset -= this->FileSizes[ mark.FileNumber ];
mark.FileNumber++;
}
if ( mark.FileNumber > (vtkIdType) this->Files.size() )
{
// when stepping past the end of the entire database (as opposed
// to a single file), return a different value
return 2;
}
if ( this->FNum < 0 || (this->FNum != mark.FileNumber) )
{
if ( this->FNum >= 0 )
{
if ( ! VTK_LSDYNA_ISBADFILE(this->FD) )
{
#ifndef WIN32
close( this->FD );
#else
fclose( this->FD );
#endif // WIN32
}
}
#ifndef WIN32
this->FD = open( this->Files[ mark.FileNumber ].c_str(), O_RDONLY );
#else
this->FD = fopen( this->Files[ mark.FileNumber ].c_str(), "rb" );
#endif // WIN32
if ( VTK_LSDYNA_ISBADFILE(this->FD) )
{
return errno;
}
this->FNum = mark.FileNumber;
this->FAdapt = this->FileAdaptLevels[ this->FNum ];
}
vtkLSDynaOff_t offset = mark.Offset * this->WordSize;
// FIXME: Handle case where wordNumber + mark.Offset > (7=factor)*512*512
if ( VTK_LSDYNA_SEEKTELL(this->FD,offset,SEEK_SET) != offset )
{
return errno;
}
this->FWord = mark.Offset;
return 0;
}
// FIXME: Assumes there is a valid file open and that
// lseek will return the byte just past the time value word.
// the BufferChunks buffer).
int vtkLSDynaFamily::MarkTimeStep()
{
vtkLSDynaFamilySectionMark mark;
mark.FileNumber = this->FNum;
mark.Offset = VTK_LSDYNA_TELL( this->FD ) / this->GetWordSize() - 1;
this->TimeStepMarks.push_back( mark );
this->TimeAdaptLevels.push_back( this->FAdapt );
return 0;
}
// FIXME: Assumes you never skip past EOF
int vtkLSDynaFamily::SkipWords( vtkIdType numWords )
{
if ( this->FNum < 0 || VTK_LSDYNA_ISBADFILE(this->FD) )
{
return -1;
}
vtkIdType offset = numWords*this->WordSize;
if ( VTK_LSDYNA_SEEKTELL(this->FD,offset,SEEK_CUR) != offset )
{
return errno;
}
this->FWord = VTK_LSDYNA_TELL(this->FD);
return 0;
}
int vtkLSDynaFamily::BufferChunk( WordType wType, vtkIdType chunkSizeInWords )
{
if ( chunkSizeInWords == 0 )
return 0;
if ( this->ChunkAlloc < chunkSizeInWords )
{
if ( this->Chunk )
{
delete [] this->Chunk;
}
this->ChunkAlloc = chunkSizeInWords;
this->Chunk = new unsigned char[ this->ChunkAlloc*this->WordSize ];
}
this->FWord = VTK_LSDYNA_TELL(this->FD);
// Eventually, we must check the return value and see if the read
// came up short (EOF). If it did, then we must advance to the next
// file.
vtkIdType bytesLeft = chunkSizeInWords*this->WordSize;
vtkIdType bytesRead;
unsigned char* buf = this->Chunk;
this->ChunkValid = 0;
this->ChunkWord = 0;
while ( bytesLeft )
{
bytesRead = VTK_LSDYNA_READ(this->FD,(void*) buf,bytesLeft);
this->ChunkValid += bytesRead;
if ( bytesRead < bytesLeft )
{
if ( bytesRead <= 0 )
{ // try advancing to next file
#ifndef WIN32
close( this->FD );
#else
fclose( this->FD );
#endif // WIN32
if ( ++this->FNum == (vtkIdType) this->Files.size() )
{ // no more files to read. Oops.
this->FNum = -1;
this->FAdapt = -1;
return 1;
}
#ifndef WIN32
this->FD = open( this->Files[ this->FNum ].c_str(), O_RDONLY );
#else
this->FD = fopen( this->Files[ this->FNum ].c_str(), "rb" );
#endif // WIN32
this->FWord = 0;
if ( VTK_LSDYNA_ISBADFILE(this->FD) )
{ // bad file (permissions, deleted) or request (too big)
this->FNum = -1;
this->FAdapt = -1;
return errno;
}
}
}
bytesLeft -= bytesRead;
buf += bytesRead;
}
if ( this->SwapEndian && wType != vtkLSDynaFamily::Char )
{
unsigned char tmp[4];
vtkIdType i;
unsigned char* cur = this->Chunk;
// Currently, wType is unused, but if I ever have to support cray
// floating point types, this will need to be different
switch (this->WordSize)
{
case 4:
for (i=0; i<chunkSizeInWords; ++i)
{
tmp[0] = cur[0];
tmp[1] = cur[1];
cur[0] = cur[3];
cur[1] = cur[2];
cur[2] = tmp[1];
cur[3] = tmp[0];
cur += this->WordSize;
}
break;
case 8:
default:
for (i=0; i<chunkSizeInWords; ++i)
{
tmp[0] = cur[0];
tmp[1] = cur[1];
tmp[2] = cur[2];
tmp[3] = cur[3];
cur[0] = cur[7];
cur[1] = cur[6];
cur[2] = cur[5];
cur[3] = cur[4];
cur[4] = tmp[3];
cur[5] = tmp[2];
cur[6] = tmp[1];
cur[7] = tmp[0];
cur += this->WordSize;
}
break;
}
}
return 0;
}
inline char* vtkLSDynaFamily::GetNextWordAsChars()
{
if ( this->ChunkWord >= this->ChunkValid ) fprintf( stderr, "Read char past end of buffer\n" );
return (char*) (&this->Chunk[ (this->ChunkWord++)*this->WordSize ]);
}
inline double vtkLSDynaFamily::GetNextWordAsFloat()
{
if ( this->ChunkWord >= this->ChunkValid ) fprintf( stderr, "Read float past end of buffer\n" );
switch (this->WordSize)
{
case 4:
return double( *(float*)(&this->Chunk[ this->ChunkWord++ << 2 ]) );
case 8:
default:
return *(double*)(&this->Chunk[ this->ChunkWord++ << 3 ]);
}
}
inline vtkIdType vtkLSDynaFamily::GetNextWordAsInt()
{
if ( this->ChunkWord >= this->ChunkValid )
{
fprintf( stderr, "Read int past end of buffer\n" );
}
switch (this->WordSize)
{
case 4:
return vtkIdType( *(int*)(&this->Chunk[ this->ChunkWord++ << 2 ]) );
case 8:
default:
return *(vtkIdType*)(&this->Chunk[ this->ChunkWord++ << 3 ]);
}
}
int vtkLSDynaFamily::AdvanceFile()
{
if ( this->FNum < 0 && VTK_LSDYNA_ISBADFILE(this->FD) )
{
if ( this->Files.size() > 0 )
{
this->FNum = 0;
this->FAdapt = 0;
return 0;
}
else
{
return 1;
}
}
if ( ! VTK_LSDYNA_ISBADFILE(this->FD) )
{
#ifndef WIN32
close( this->FD );
#else
fclose( this->FD );
#endif // WIN32
//this->FD = VTK_LSDYNA_BADFILE;
}
this->FWord = 0;
this->ChunkValid = 0;
if ( this->FNum + 1 < (vtkIdType) this->Files.size() )
{
this->FNum++;
this->FAdapt = this->FileAdaptLevels[ this->FNum ];
}
else
{
this->FD = VTK_LSDYNA_BADFILE;
return 1;
}
#ifndef WIN32
this->FD = open( this->Files[ this->FNum ].c_str(), O_RDONLY );
#else
this->FD = fopen( this->Files[ this->FNum ].c_str(), "rb" );
#endif // WIN32
if ( VTK_LSDYNA_ISBADFILE(this->FD) )
{
return errno;
}
return 0;
}
void vtkLSDynaFamily::MarkSectionStart( int adaptLevel, SectionType m )
{
vtkIdType myWord;
if ( ! VTK_LSDYNA_ISBADFILE(this->FD) )
{
myWord = VTK_LSDYNA_TELL(this->FD) / this->WordSize;
}
else
{
myWord = 0;
}
// OK, mark it.
vtkLSDynaFamilySectionMark mark;
mark.FileNumber = this->FNum;
mark.Offset = myWord;
while ( adaptLevel >= (int) this->AdaptationsMarkers.size() )
{
this->AdaptationsMarkers.push_back( vtkLSDynaFamilyAdaptLevel() );
}
this->AdaptationsMarkers[adaptLevel].Marks[m] = mark;
//fprintf( stderr, "Mark \"%s\" is (%d,%d)\n", SectionTypeToString(m), mark.FileNumber, mark.Offset );
}
int vtkLSDynaFamily::JumpToMark( SectionType m )
{
return this->SkipToWord( m, this->TimeStep, 0 );
}
int vtkLSDynaFamily::DetermineStorageModel()
{
double test;
this->WordSize = 4;
this->SwapEndian = 0;
this->JumpToMark( ControlSection ); // opens file 0, since marks are all zeroed
this->BufferChunk( Float, 128 ); // does no swapping, buffers enough for 64 8-byte words
this->ChunkWord = 14;
test = this->GetNextWordAsFloat();
if ( test > 900. && test < 1000. )
{
this->JumpToMark( ControlSection ); // seek to start of file
return 0;
}
this->ChunkWord = 14;
this->WordSize = 8;
test = this->GetNextWordAsFloat();
if ( test > 900. && test < 1000. )
{
this->JumpToMark( ControlSection ); // seek to start of file
return 0;
}
// OK, try swapping endianness
this->SwapEndian = 1;
this->WordSize = 4;
this->JumpToMark( ControlSection ); // seek to start of file
this->BufferChunk( Float, 128 );
this->ChunkWord = 14;
test = this->GetNextWordAsFloat();
if ( test > 900. && test < 1000. )
{
this->JumpToMark( ControlSection ); // seek to start of file
return 0;
}
this->ChunkWord = 14;
this->WordSize = 8;
test = this->GetNextWordAsFloat();
if ( test > 900. && test < 1000. )
{
this->JumpToMark( ControlSection ); // seek to start of file
return 0;
}
// Oops, couldn't identify storage model
#ifndef WIN32