VTK
LSDynaFamily.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: LSDynaFamily.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*----------------------------------------------------------------------------
16  Copyright (c) Sandia Corporation
17  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
18 ----------------------------------------------------------------------------*/
19 
20 // .NAME LSDynaFamily
21 // .SECTION Description
22 // A class to abstract away I/O from families of output files.
23 // This performs the actual reads and writes plus any required byte swapping.
24 // Also contains a subclass, LSDynaFamilyAdaptLevel, used to store
25 // file+offset
26 // information for each mesh adaptation's state info.
27 
28 #ifndef __LSDynaFamily_h
29 #define __LSDynaFamily_h
30 
31 #include "vtkType.h"
32 #include "LSDynaExport.h"
33 
34 #include <string.h>
35 #include <stdio.h>
36 #include <iostream>
37 #include <string>
38 #include <vector>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 
43 //this is needs to be moved over to fseekpos and ftellpos
44 //in the future
45 #ifndef _WIN32
46 # include <unistd.h>
47 typedef off_t vtkLSDynaOff_t; // sanity
48 typedef int vtkLSDynaFile_t;
49 # define VTK_LSDYNA_BADFILE -1
50 # define VTK_LSDYNA_TELL(fid) lseek( fid, 0, SEEK_CUR )
51 # define VTK_LSDYNA_SEEK(fid,off,whence) lseek( fid, off, whence )
52 # define VTK_LSDYNA_SEEKTELL(fid,off,whence) lseek( fid, off, whence )
53 # define VTK_LSDYNA_READ(fid,ptr,cnt) read(fid,ptr,cnt)
54 # define VTK_LSDYNA_ISBADFILE(fid) (fid < 0)
55 # define VTK_LSDYNA_CLOSEFILE(fid) close(fid)
56 #else // _WIN32
57 typedef long vtkLSDynaOff_t; // insanity
58 typedef FILE* vtkLSDynaFile_t;
59 # define VTK_LSDYNA_BADFILE 0
60 # define VTK_LSDYNA_TELL(fid) ftell( fid )
61 # define VTK_LSDYNA_SEEK(fid,off,whence) fseek( fid, off, whence )
62 # define VTK_LSDYNA_SEEKTELL(fid,off,whence) fseek( fid, off, whence ), ftell( fid )
63 # define VTK_LSDYNA_READ(fid,ptr,cnt) fread(ptr,1,cnt,fid)
64 # define VTK_LSDYNA_ISBADFILE(fid) (fid == 0)
65 # define VTK_LSDYNA_CLOSEFILE(fid) fclose(fid)
66 #endif
67 #ifdef VTKSNL_HAVE_ERRNO_H
68 # include <errno.h>
69 #endif
70 
72 {
73 public:
74  LSDynaFamily();
75  ~LSDynaFamily();
76 
78  {
81  };
82 
85 
88 
90 
91  enum SectionType {
92  // These are the "section" marks:
93  // They are absolute (independent of current timestep).
97  // These are the "subsection" marks:
98  // == ControlSection has no subsections
99  // == StaticSection has these "absolute" marks:
109  // == TimeStepSection has these marks, relative to timestep 0 (so they are
110  // not valid for an arbitrary timestep, but may easily be used to compute
111  // an offset for any time step by adding a multiple of the state size):
115  // THIS MUST BE LAST
117  };
118 
120  {
121  public:
123 
125  {
127  mark.FileNumber = 0;
128  mark.Offset = 0;
129  for ( int i=0; i<LSDynaFamily::NumberOfSectionTypes; ++i )
130  {
131  this->Marks[i] = mark;
132  }
133  }
134  };
135 
136  static const char* SectionTypeNames[];
137 
138  enum WordType {
142  };
143 
144  static const float EOFMarker;
145  static const char* SectionTypeToString( SectionType s );
146 
147  int SkipToWord( SectionType sType, vtkIdType sId, vtkIdType wordNumber );
148  int MarkTimeStep();
149  int SkipWords( vtkIdType numWords );
150  int BufferChunk( WordType wType, vtkIdType chunkSizeInWords );
151  int ClearBuffer();
152 
153  //Description:
154  //Setup reading of a number of words to be split across multiple
155  //bufferChunk. This is used to read really large buffer sections
156  //in more reasonable sizes. The paramters are used to specify the total buffer
157  //size. The buffer size will always be evenly divisable by numComps and total
158  //word size of all buffers will be numTuples*numComps
159  vtkIdType InitPartialChunkBuffering(const vtkIdType& numTuples, const vtkIdType& numComps );
160  vtkIdType GetNextChunk( const WordType& wType);
161 
162  inline char* GetNextWordAsChars();
163  inline double GetNextWordAsFloat();
164  inline vtkIdType GetNextWordAsInt();
165 
166  //Get the raw chunk buffer as a buffer of type T
167  template<typename T>
168  T* GetBufferAs();
169 
170  // Not needed (yet):
171  // void GetCurrentWord( SectionType& stype, vtkIdType& sId, vtkIdType& wN );
172  int AdvanceFile();
173  void MarkSectionStart( int adapteLevel, SectionType m );
174 
175  int JumpToMark( SectionType m );
176  int DetermineStorageModel();
177 
178  void SetStateSize( vtkIdType sz );
179  vtkIdType GetStateSize() const;
180 
182  std::string GetFileName( int i );
183  vtkIdType GetFileSize( int i );
184 
185  int GetCurrentAdaptLevel() const { return this->FAdapt; }
186  int TimeAdaptLevel( int i ) const { return this->TimeAdaptLevels[i]; }
187 
188  vtkIdType GetCurrentFWord() const { return this->FWord; }
189 
190  int GetWordSize() const;
191  // Reset erases all information about the current database.
192  // It does not free memory allocated for the current chunk.
193  void Reset();
194 
196  void DumpMarks( std::ostream& os );
197 
198  //Closes the current file descripter. This is called after
199  //we are done reading in request data
200  void CloseFileHandles();
201 
202  void OpenFileHandles();
203 
204 protected:
211  std::vector<std::string> Files;
214  std::vector<vtkIdType> FileSizes;
216  std::vector<int> FileAdaptLevels;
219  std::vector<int> Adaptations;
226  int FAdapt;
231  //std::vector<double> TimeValues;
237  int WordSize;
242  std::vector<LSDynaFamilyAdaptLevel> AdaptationsMarkers;
245  std::vector<LSDynaFamilySectionMark> TimeStepMarks;
247  std::vector<int> TimeAdaptLevels;
249  unsigned char* Chunk;
253  // How much of the the allocated space is filled with valid data (assert
254  // ChunkValid <= ChunkAlloc).
258 
260  struct BufferingInfo;
261  BufferingInfo* BufferInfo;
262 };
263 
264 //-----------------------------------------------------------------------------
266 {
267  if ( this->ChunkWord >= this->ChunkValid ) fprintf( stderr, "Read char past end of buffer\n" );
268  return (char*) (&this->Chunk[ (this->ChunkWord++)*this->WordSize ]);
269 }
270 
271 //-----------------------------------------------------------------------------
273 {
274  if ( this->ChunkWord >= this->ChunkValid ) fprintf( stderr, "Read float past end of buffer\n" );
275  switch (this->WordSize)
276  {
277  case 4:
278  {
279  vtkTypeFloat32 value;
280  memcpy(&value, &this->Chunk[ this->ChunkWord++ << 2 ], sizeof(value));
281  return value;
282  }
283  case 8:
284  default:
285  {
286  vtkTypeFloat64 value;
287  memcpy(&value, &this->Chunk[ this->ChunkWord++ << 3 ], sizeof(value));
288  return value;
289  }
290  }
291 }
292 
293 //-----------------------------------------------------------------------------
295 {
296  if ( this->ChunkWord >= this->ChunkValid )
297  {
298  fprintf( stderr, "Read int past end of buffer\n" );
299  }
300  switch (this->WordSize)
301  {
302  case 4:
303  {
304  vtkTypeInt32 value;
305  memcpy(&value, &this->Chunk[ this->ChunkWord++ << 2 ], sizeof(value));
306  return value;
307  }
308  case 8:
309  default:
310  {
312  memcpy(&value, &this->Chunk[ this->ChunkWord++ << 3 ], sizeof(value));
313  return value;
314  }
315  }
316 }
317 
318 //-----------------------------------------------------------------------------
319 template<typename T>
321 {
322  return reinterpret_cast<T*>(this->Chunk);
323 }
324 
325 
326 #endif // __LSDynaFamily_h
static const float EOFMarker
Definition: LSDynaFamily.h:144
T * GetBufferAs()
Definition: LSDynaFamily.h:320
void SetDatabaseBaseName(std::string bn)
void MarkSectionStart(int adapteLevel, SectionType m)
BufferingInfo * BufferInfo
Definition: LSDynaFamily.h:260
std::vector< int > TimeAdaptLevels
The adaptation level associated with each time step.
Definition: LSDynaFamily.h:247
vtkIdType StateSize
How many words is a timestep on disk?
Definition: LSDynaFamily.h:239
vtkIdType ChunkValid
Definition: LSDynaFamily.h:255
std::vector< LSDynaFamilyAdaptLevel > AdaptationsMarkers
A vector of arrays of offsets to various header information sections (that do not vary with timestep)...
Definition: LSDynaFamily.h:242
int AdvanceFile()
std::vector< std::string > Files
The list of files that make up the database.
Definition: LSDynaFamily.h:211
void SetDatabaseDirectory(std::string dd)
vtkIdType GetNextWordAsInt()
Definition: LSDynaFamily.h:294
void DumpMarks(std::ostream &os)
Print all adaptation and time step marker information.
vtkIdType GetFileSize(int i)
vtkLSDynaFile_t FD
The currently open file descriptor.
Definition: LSDynaFamily.h:221
int GetWordSize() const
vtkIdType GetNumberOfFiles()
int FAdapt
The current adaptation level.
Definition: LSDynaFamily.h:226
int vtkIdType
Definition: vtkType.h:287
vtkIdType InitPartialChunkBuffering(const vtkIdType &numTuples, const vtkIdType &numComps)
std::string GetDatabaseDirectory()
vtkIdType GetStateSize() const
std::vector< vtkIdType > FileSizes
The size of each file in the database.
Definition: LSDynaFamily.h:214
vtkIdType ChunkAlloc
The allocated size (in words) of Chunk.
Definition: LSDynaFamily.h:257
std::vector< int > Adaptations
Which files mark the start of a new mesh adaptation.
Definition: LSDynaFamily.h:219
int DetermineStorageModel()
unsigned char * Chunk
A buffer containing file contents of file FNum starting with word FWord.
Definition: LSDynaFamily.h:249
void CloseFileHandles()
std::string GetFileName(int i)
int MarkTimeStep()
std::vector< LSDynaFamilySectionMark > TimeStepMarks
An array of bookmarks pointing to the start of state information for each timestep.
Definition: LSDynaFamily.h:245
int JumpToMark(SectionType m)
int WordSize
Whether words are 4 or 8 bytes.
Definition: LSDynaFamily.h:237
vtkIdType ChunkWord
A pointer to the next word in Chunk that will be returned when the reader requests a word...
Definition: LSDynaFamily.h:252
int ScanDatabaseDirectory()
int ClearBuffer()
char * GetNextWordAsChars()
Definition: LSDynaFamily.h:265
LSDynaFamilySectionMark Marks[NumberOfSectionTypes]
Definition: LSDynaFamily.h:122
void OpenFileHandles()
int vtkLSDynaFile_t
Definition: LSDynaFamily.h:48
vtkIdType FWord
The offset of Chunk in currently open file.
Definition: LSDynaFamily.h:228
int SkipToWord(SectionType sType, vtkIdType sId, vtkIdType wordNumber)
int SwapEndian
Whether files are reverse endian-ness of architecture.
Definition: LSDynaFamily.h:235
std::string DatabaseBaseName
The name (title string) of the database.
Definition: LSDynaFamily.h:209
std::string GetDatabaseBaseName()
std::string DatabaseDirectory
The directory containing d3plot files.
Definition: LSDynaFamily.h:206
int SkipWords(vtkIdType numWords)
vtkIdType GetNextChunk(const WordType &wType)
static const char * SectionTypeNames[]
Definition: LSDynaFamily.h:136
vtkIdType FNum
The index of currently open file descriptor into list of files.
Definition: LSDynaFamily.h:223
vtkIdType TimeStep
A comprehensive list of all time values across all files (and mesh adaptations)
Definition: LSDynaFamily.h:233
bool FileHandlesClosed
Definition: LSDynaFamily.h:259
double GetNextWordAsFloat()
Definition: LSDynaFamily.h:272
off_t vtkLSDynaOff_t
Definition: LSDynaFamily.h:47
std::vector< int > FileAdaptLevels
The adaptation level associated with each file.
Definition: LSDynaFamily.h:216
int GetCurrentAdaptLevel() const
Definition: LSDynaFamily.h:185
void SetStateSize(vtkIdType sz)
int BufferChunk(WordType wType, vtkIdType chunkSizeInWords)
int TimeAdaptLevel(int i) const
Definition: LSDynaFamily.h:186
static const char * SectionTypeToString(SectionType s)
vtkIdType GetCurrentFWord() const
Definition: LSDynaFamily.h:188