VTK  9.3.20240418
LSDynaFamily.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 // .NAME LSDynaFamily
6 // .SECTION Description
7 // A class to abstract away I/O from families of output files.
8 // This performs the actual reads and writes plus any required byte swapping.
9 // Also contains a subclass, LSDynaFamilyAdaptLevel, used to store
10 // file+offset
11 // information for each mesh adaptation's state info.
12 
13 #ifndef __LSDynaFamily_h
14 #define __LSDynaFamily_h
15 
16 #include "vtkType.h"
17 
18 #include <fcntl.h>
19 #include <iostream>
20 #include <stdio.h>
21 #include <string.h>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <vector>
26 
27 // this is needs to be moved over to fseekpos and ftellpos
28 // in the future
29 #ifndef _WIN32
30 #include <unistd.h>
31 typedef off_t vtkLSDynaOff_t; // sanity
32 typedef int vtkLSDynaFile_t;
33 #define VTK_LSDYNA_BADFILE -1
34 #define VTK_LSDYNA_TELL(fid) lseek(fid, 0, SEEK_CUR)
35 #define VTK_LSDYNA_SEEK(fid, off, whence) lseek(fid, off, whence)
36 #define VTK_LSDYNA_SEEKTELL(fid, off, whence) lseek(fid, off, whence)
37 #define VTK_LSDYNA_READ(fid, ptr, cnt) read(fid, ptr, cnt)
38 #define VTK_LSDYNA_ISBADFILE(fid) (fid < 0)
39 #define VTK_LSDYNA_CLOSEFILE(fid) close(fid)
40 #else // _WIN32
41 typedef long vtkLSDynaOff_t; // insanity
42 typedef FILE* vtkLSDynaFile_t;
43 #define VTK_LSDYNA_BADFILE 0
44 #define VTK_LSDYNA_TELL(fid) ftell(fid)
45 #define VTK_LSDYNA_SEEK(fid, off, whence) fseek(fid, off, whence)
46 #define VTK_LSDYNA_SEEKTELL(fid, off, whence) fseek(fid, off, whence), ftell(fid)
47 #define VTK_LSDYNA_READ(fid, ptr, cnt) fread(ptr, 1, cnt, fid)
48 #define VTK_LSDYNA_ISBADFILE(fid) (fid == 0)
49 #define VTK_LSDYNA_CLOSEFILE(fid) fclose(fid)
50 #endif
51 #ifdef VTKSNL_HAVE_ERRNO_H
52 #include <errno.h>
53 #endif
54 
55 VTK_ABI_NAMESPACE_BEGIN
57 {
58 public:
61 
63  {
66  };
67 
70 
73 
75 
77  {
78  // These are the "section" marks:
79  // They are absolute (independent of current timestep).
83  // These are the "subsection" marks:
84  // == ControlSection has no subsections
85  // == StaticSection has these "absolute" marks:
95  // == TimeStepSection has these marks, relative to timestep 0 (so they are
96  // not valid for an arbitrary timestep, but may easily be used to compute
97  // an offset for any time step by adding a multiple of the state size):
101  // THIS MUST BE LAST
103  };
104 
106  {
107  public:
109 
111  {
113  mark.FileNumber = 0;
114  mark.Offset = 0;
115  for (int i = 0; i < LSDynaFamily::NumberOfSectionTypes; ++i)
116  {
117  this->Marks[i] = mark;
118  }
119  }
120  };
121 
122  static const char* SectionTypeNames[];
123 
124  enum WordType
125  {
128  Int
129  };
130 
131  static const float EOFMarker;
132  static const char* SectionTypeToString(SectionType s);
133 
134  int SkipToWord(SectionType sType, vtkIdType sId, vtkIdType wordNumber);
136  int SkipWords(vtkIdType numWords);
137  int BufferChunk(WordType wType, vtkIdType chunkSizeInWords);
138  int ClearBuffer();
139 
140  // Description:
141  // Setup reading of a number of words to be split across multiple
142  // bufferChunk. This is used to read really large buffer sections
143  // in more reasonable sizes. The parameters are used to specify the total buffer
144  // size. The buffer size will always be evenly divisible by numComps and total
145  // word size of all buffers will be numTuples*numComps
146  vtkIdType InitPartialChunkBuffering(const vtkIdType& numTuples, const vtkIdType& numComps);
148 
149  inline char* GetNextWordAsChars();
150  inline double GetNextWordAsFloat();
151  inline vtkIdType GetNextWordAsInt();
152 
153  // Get the raw chunk buffer as a buffer of type T
154  template <typename T>
155  T* GetBufferAs();
156 
157  // Not needed (yet):
158  // void GetCurrentWord( SectionType& stype, vtkIdType& sId, vtkIdType& wN );
159  int AdvanceFile();
160  void MarkSectionStart(int adaptLevel, SectionType m);
161 
164 
167 
171 
172  int GetCurrentAdaptLevel() const { return this->FAdapt; }
173  int TimeAdaptLevel(int i) const { return this->TimeAdaptLevels[i]; }
174 
175  vtkIdType GetCurrentFWord() const { return this->FWord; }
176 
177  int GetWordSize() const;
178  // Reset erases all information about the current database.
179  // It does not free memory allocated for the current chunk.
180  void Reset();
181 
183  void DumpMarks(std::ostream& os);
184 
185  // Closes the current file descriptor. This is called after
186  // we are done reading in request data
188 
190 
191 protected:
198  std::vector<std::string> Files;
201  std::vector<vtkIdType> FileSizes;
203  std::vector<int> FileAdaptLevels;
206  std::vector<int> Adaptations;
213  int FAdapt;
218  // std::vector<double> TimeValues;
224  int WordSize;
229  std::vector<LSDynaFamilyAdaptLevel> AdaptationsMarkers;
232  std::vector<LSDynaFamilySectionMark> TimeStepMarks;
234  std::vector<int> TimeAdaptLevels;
236  unsigned char* Chunk;
240  // How much of the allocated space is filled with valid data (assert
241  // ChunkValid <= ChunkAlloc).
245 
247  struct BufferingInfo;
248  BufferingInfo* BufferInfo;
249 };
250 
251 //-----------------------------------------------------------------------------
253 {
254  if (this->ChunkWord >= this->ChunkValid)
255  fprintf(stderr, "Read char past end of buffer\n");
256  return (char*)(&this->Chunk[(this->ChunkWord++) * this->WordSize]);
257 }
258 
259 //-----------------------------------------------------------------------------
261 {
262  if (this->ChunkWord >= this->ChunkValid)
263  fprintf(stderr, "Read float past end of buffer\n");
264  switch (this->WordSize)
265  {
266  case 4:
267  {
268  vtkTypeFloat32 value;
269  memcpy(&value, &this->Chunk[this->ChunkWord++ << 2], sizeof(value));
270  return value;
271  }
272  case 8:
273  default:
274  {
275  vtkTypeFloat64 value;
276  memcpy(&value, &this->Chunk[this->ChunkWord++ << 3], sizeof(value));
277  return value;
278  }
279  }
280 }
281 
282 //-----------------------------------------------------------------------------
284 {
285  if (this->ChunkWord >= this->ChunkValid)
286  {
287  fprintf(stderr, "Read int past end of buffer\n");
288  }
289  switch (this->WordSize)
290  {
291  case 4:
292  {
293  vtkTypeInt32 value;
294  memcpy(&value, &this->Chunk[this->ChunkWord++ << 2], sizeof(value));
295  return value;
296  }
297  case 8:
298  default:
299  {
301  memcpy(&value, &this->Chunk[this->ChunkWord++ << 3], sizeof(value));
302  return value;
303  }
304  }
305 }
306 
307 //-----------------------------------------------------------------------------
308 template <typename T>
310 {
311  return reinterpret_cast<T*>(this->Chunk);
312 }
313 
314 VTK_ABI_NAMESPACE_END
315 #endif // __LSDynaFamily_h
int vtkLSDynaFile_t
Definition: LSDynaFamily.h:32
off_t vtkLSDynaOff_t
Definition: LSDynaFamily.h:31
LSDynaFamilySectionMark Marks[NumberOfSectionTypes]
Definition: LSDynaFamily.h:108
vtkIdType GetCurrentFWord() const
Definition: LSDynaFamily.h:175
int BufferChunk(WordType wType, vtkIdType chunkSizeInWords)
int ScanDatabaseDirectory()
vtkIdType GetNextWordAsInt()
Definition: LSDynaFamily.h:283
static const char * SectionTypeNames[]
Definition: LSDynaFamily.h:122
unsigned char * Chunk
A buffer containing file contents of file FNum starting with word FWord.
Definition: LSDynaFamily.h:236
bool FileHandlesClosed
Definition: LSDynaFamily.h:246
BufferingInfo * BufferInfo
Definition: LSDynaFamily.h:247
vtkIdType StateSize
How many words is a timestep on disk?
Definition: LSDynaFamily.h:226
void SetDatabaseBaseName(const std::string &bn)
int WordSize
Whether words are 4 or 8 bytes.
Definition: LSDynaFamily.h:224
@ ElementDeletionState
Definition: LSDynaFamily.h:98
std::vector< int > Adaptations
Which files mark the start of a new mesh adaptation.
Definition: LSDynaFamily.h:206
std::vector< int > TimeAdaptLevels
The adaptation level associated with each time step.
Definition: LSDynaFamily.h:234
void OpenFileHandles()
int GetCurrentAdaptLevel() const
Definition: LSDynaFamily.h:172
void CloseFileHandles()
std::vector< LSDynaFamilyAdaptLevel > AdaptationsMarkers
A vector of arrays of offsets to various header information sections (that do not vary with timestep)...
Definition: LSDynaFamily.h:229
static const float EOFMarker
Definition: LSDynaFamily.h:131
int DetermineStorageModel()
int SkipToWord(SectionType sType, vtkIdType sId, vtkIdType wordNumber)
vtkIdType TimeStep
A comprehensive list of all time values across all files (and mesh adaptations)
Definition: LSDynaFamily.h:220
int AdvanceFile()
double GetNextWordAsFloat()
Definition: LSDynaFamily.h:260
vtkIdType GetNumberOfFiles()
int FAdapt
The current adaptation level.
Definition: LSDynaFamily.h:213
int SkipWords(vtkIdType numWords)
int SwapEndian
Whether files are reverse endian-ness of architecture.
Definition: LSDynaFamily.h:222
vtkIdType GetNextChunk(const WordType &wType)
std::string GetDatabaseBaseName()
int MarkTimeStep()
std::string GetDatabaseDirectory()
vtkIdType FNum
The index of currently open file descriptor into list of files.
Definition: LSDynaFamily.h:210
std::vector< vtkIdType > FileSizes
The size of each file in the database.
Definition: LSDynaFamily.h:201
int GetWordSize() const
void SetDatabaseDirectory(const std::string &dd)
std::string DatabaseDirectory
The directory containing d3plot files.
Definition: LSDynaFamily.h:193
vtkLSDynaFile_t FD
The currently open file descriptor.
Definition: LSDynaFamily.h:208
vtkIdType GetStateSize() const
vtkIdType ChunkWord
A pointer to the next word in Chunk that will be returned when the reader requests a word.
Definition: LSDynaFamily.h:239
vtkIdType GetFileSize(int i)
vtkIdType InitPartialChunkBuffering(const vtkIdType &numTuples, const vtkIdType &numComps)
std::vector< int > FileAdaptLevels
The adaptation level associated with each file.
Definition: LSDynaFamily.h:203
void DumpMarks(std::ostream &os)
Print all adaptation and time step marker information.
vtkIdType ChunkAlloc
The allocated size (in words) of Chunk.
Definition: LSDynaFamily.h:244
std::string DatabaseBaseName
The name (title string) of the database.
Definition: LSDynaFamily.h:196
int ClearBuffer()
int TimeAdaptLevel(int i) const
Definition: LSDynaFamily.h:173
T * GetBufferAs()
Definition: LSDynaFamily.h:309
void MarkSectionStart(int adaptLevel, SectionType m)
static const char * SectionTypeToString(SectionType s)
vtkIdType ChunkValid
Definition: LSDynaFamily.h:242
std::string GetFileName(int i)
void SetStateSize(vtkIdType sz)
std::vector< LSDynaFamilySectionMark > TimeStepMarks
An array of bookmarks pointing to the start of state information for each timestep.
Definition: LSDynaFamily.h:232
int JumpToMark(SectionType m)
char * GetNextWordAsChars()
Definition: LSDynaFamily.h:252
std::vector< std::string > Files
The list of files that make up the database.
Definition: LSDynaFamily.h:198
vtkIdType FWord
The offset of Chunk in currently open file.
Definition: LSDynaFamily.h:215
@ value
Definition: vtkX3D.h:220
@ string
Definition: vtkX3D.h:490
int vtkIdType
Definition: vtkType.h:315