VTK  9.3.0
PIOData.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#if !defined(_PIODATA_H)
4#define _PIODATA_H
5
6#include "vtkABINamespace.h"
7
8#include <cstdint>
9#include <fstream>
10#include <iostream>
11#include <list>
12#include <map>
13#include <set>
14#include <sstream>
15#include <string.h>
16#include <string>
17#include <valarray>
18
19VTK_ABI_NAMESPACE_BEGIN
21{
22 bool operator()(const char* p, const char* q) const { return strcmp(p, q) < 0; };
23};
24
25// Class Declarations
26
27class PIO_DATA;
28class PIO_FIELD;
29
31{
32public:
33 char* pio_name;
34 int index;
35 int64_t length;
36 int64_t position;
37 int64_t chksum;
38 size_t cdata_len;
40 friend class PIO_DATA;
41
42protected:
43 double* data;
44 char* cdata;
45}; // End class PIO_FIELD
46
47// Typedefs for the mapping between the names of the PIO blocks in the PIO file and
48// the PIO_FIELD's used to store the data values in the PIO blocks.
49typedef std::multimap<const char*, PIO_FIELD*, Cstring_less> VAR_MAP;
50typedef VAR_MAP::iterator VMI;
51typedef VAR_MAP::const_iterator CVMI;
52typedef std::pair<VMI, VMI> VMP;
53typedef std::pair<CVMI, CVMI> CVMP;
54
56{
57public:
58 PIO_DATA(const char* piofile = nullptr, const std::list<std::string>* fields_to_read = nullptr,
59 bool _defer_read_data = true, const std::set<const char*, Cstring_less>* rdata = nullptr,
60 const std::set<const char*, Cstring_less>* cdata = nullptr);
62 bool GetPIOfileTime(const char*, double&);
63 void print(std::ostream&);
64 void print(const char*);
65 bool set_scalar_field(std::valarray<int>&, const char*);
66 bool set_scalar_field(std::valarray<int64_t>&, const char*);
67 bool set_scalar_field(std::valarray<uint64_t>&, const char*);
68 bool set_scalar_field(std::valarray<double>&, const char*);
69 bool set_vector_field(std::valarray<std::valarray<double>>&, const char*);
70 inline bool good_read() { return (pio_field != nullptr) ? true : false; }
71 VAR_MAP VarMMap; // Multimap from pio_name to a PIO_FIELD class
72 const char* get_name() const { return name; }
73 bool get_reverse_endian() const { return reverse_endian; }
74 int get_PIO_VERSION() const { return PIO_VERSION; }
75 int get_PIO_NAME_LENGTH() const { return PIO_NAME_LENGTH; }
76 int get_PIO_HEADER_LENGTH() const { return PIO_HEADER_LENGTH; }
77 int get_PIO_INDEX_LENGTH() const { return PIO_INDEX_LENGTH; }
78 const char* get_pio_dandt() const { return pio_dandt; }
79 int get_pio_num() const { return pio_num; }
80 int get_pio_num_with_size(int64_t n) const;
81 int get_pio_signature() const { return pio_signature; }
82 PIO_FIELD* get_pio_field() const { return pio_field; }
83 void GetPIOData(PIO_FIELD&, const double*&, const char*&);
84 void GetPIOData(PIO_FIELD&, const double*&);
85 void GetPIOData(PIO_FIELD&, const char*&);
86 const double* GetPIOData(PIO_FIELD&);
87 void GetPIOData(const char*, const double*&, const char*&);
88 void GetPIOData(const char*, const double*&);
89 void GetPIOData(const char*, const char*&);
90 const double* GetPIOData(const char*);
91 double GetPIOData(const char*, int);
92 bool reconstruct_chunk_field(int64_t numcell, std::valarray<double>& va, const char* prefix,
93 const char* var, int materialId);
94 void AddRealData(const char* _name)
95 {
96 if (RealData.find(_name) == RealData.end())
97 RealData.insert(strdup(_name));
98 }
99 void AddCharData(const char* _name)
100 {
101 if (CharData.find(_name) == CharData.end())
102 CharData.insert(strdup(_name));
103 }
104 void FreePIOData(PIO_FIELD& pio_field);
106
107private:
108 std::set<const char*, Cstring_less> RealData;
109 std::set<const char*, Cstring_less> CharData;
110 const char* name;
111 std::istream* Infile;
112 bool reverse_endian;
113 int PIO_VERSION;
114 int PIO_NAME_LENGTH;
115 int PIO_HEADER_LENGTH;
116 int PIO_INDEX_LENGTH;
117 const char* pio_dandt; // Date and Time
118 int pio_num;
119 int64_t pio_position;
120 int pio_signature;
121 PIO_FIELD* pio_field;
122 bool defer_read_data;
123 size_t matident_len;
124 size_t timertype_len;
125
126 char* buf;
127 size_t size_buf;
128 void ReadPioFieldData(PIO_FIELD& pio_field);
129 bool read(const char*, const std::list<std::string>* fields_to_read = nullptr);
130 bool read(const std::list<std::string>* fields_to_read = nullptr);
131 inline void byte_flip(char* word, int64_t size)
132 {
133 if (size_buf < (size_t)size)
134 {
135 delete[] buf;
136 size_buf = size;
137 buf = new char[size_buf];
138 }
139 memcpy((void*)buf, (const void*)word, size);
140 for (int64_t i = 0; i < size; ++i)
141 word[i] = buf[size - 1 - i];
142 } // End byte_flip
143
144 template <class T>
145 inline T read_pio_word(T& val)
146 {
147 double word;
148 this->Infile->read((char*)&word, sizeof(word));
149 if (reverse_endian)
150 byte_flip((char*)&word, sizeof(word));
151 val = T(word);
152 return val;
153 } // End read_pio_word
154
155 inline bool read_pio_bool()
156 {
157 double word;
158 this->Infile->read((char*)&word, sizeof(word));
159 if (reverse_endian)
160 byte_flip((char*)&word, sizeof(word));
161 return (word != 0) ? true : false;
162 } // End read_pio_bool
163
164 inline void fstr2Cstr(char* s, size_t len) const
165 {
166 s[len] = '\0';
167 size_t i = len - 1;
168 do
169 {
170 if (s[i--] == ' ')
171 s[i + 1] = '\0';
172 } while (i != 0);
173 } // End fstr2Cstr
174
175 inline char* read_pio_char_string(size_t len)
176 {
177 if (size_buf <= len)
178 {
179 delete[] buf;
180 size_buf = len + 1;
181 buf = new char[size_buf];
182 }
183 this->Infile->read(buf, len);
184 buf[len] = '\0';
185 fstr2Cstr(buf, len);
186 char* val = new char[strlen(buf) + 1];
187 strcpy(val, buf);
188 return val;
189 } // End read_pio_char_string
190
191 inline void insert_VAR_MAP_pairs()
192 {
193 for (int i = 0; i < pio_num; ++i)
194 {
195 if (pio_field[i].read_field_data)
196 {
197#if !defined __SUNPRO_CC
198 VarMMap.insert(std::make_pair(pio_field[i].pio_name, pio_field + i));
199#else
200 VAR_MAP::value_type type(pio_field[i].pio_name, pio_field + i);
201 VarMMap.insert(type);
202#endif
203 }
204 }
205 } // End insert_VAR_MAP_pairs
206
207 inline bool read_field(const char* pio_name, const std::list<std::string>* fields_to_read)
208 {
209 std::string spio_name = std::string(pio_name);
210 if (fields_to_read == nullptr)
211 return true;
212 else
213 {
214 for (std::list<std::string>::const_iterator pos = fields_to_read->begin();
215 pos != fields_to_read->end(); ++pos)
216 {
217 if (spio_name == *pos)
218 return true;
219 }
220 }
221 return false;
222 }
223}; // End class PIO_DATA
224
225// Locations of various data items from the input arrays, amhc_i, amhc_r8,
226// amch_l, and controller_r8
227enum
228{
229 Ntime = 0, // time = controller_r8[Ntime];
230 Nnumdim = 42, // numdim = amhc_i[Nnumdim]
231 Nmesh0 = 16, // N[0] = amhc_i[Nmesh0]
232 Nmesh1 = 17, // N[1] = amhc_i[Nmesh1]
233 Nmesh2 = 29, // N[2] = amhc_i[Nmesh2]
234 Nd0 = 21, // d[0] = amhc_r8[Nd0]
235 Nd1 = 22, // d[1] = amhc_r8[Nd1]
236 Nd2 = 38, // d[2] = amhc_r8[Nd2]
237 NZero0 = 19, // Zero[0] = amhc_r8[NZero0]
238 NZero1 = 20, // Zero[1] = amhc_r8[NZero1]
239 NZero2 = 35, // Zero[2] = amhc_r8[NZero2]
240 Ncylin = 1, // cylindrically (axisymmetric) symmetric
241 // geometry if amhc_l[Ncylin]!=0
242 Nsphere = 8 // spherically symmetirc geometry if
243 // amhc_l[Nsphere]!=0
245
246// Prototypes
247bool GetPIOfileTime(const char*, double&);
248bool IsPIOfile(const char*);
249VTK_ABI_NAMESPACE_END
250#endif
bool IsPIOfile(const char *)
bool GetPIOfileTime(const char *, double &)
std::pair< VMI, VMI > VMP
Definition PIOData.h:52
std::pair< CVMI, CVMI > CVMP
Definition PIOData.h:53
VAR_MAP::iterator VMI
Definition PIOData.h:50
std::multimap< const char *, PIO_FIELD *, Cstring_less > VAR_MAP
Definition PIOData.h:49
VAR_MAP::const_iterator CVMI
Definition PIOData.h:51
@ NZero2
Definition PIOData.h:239
@ Nsphere
Definition PIOData.h:242
@ Nmesh2
Definition PIOData.h:233
@ Nmesh1
Definition PIOData.h:232
@ NZero1
Definition PIOData.h:238
@ Nmesh0
Definition PIOData.h:231
@ Nd1
Definition PIOData.h:235
@ Nd2
Definition PIOData.h:236
@ Nd0
Definition PIOData.h:234
@ Nnumdim
Definition PIOData.h:230
@ Ncylin
Definition PIOData.h:240
@ NZero0
Definition PIOData.h:237
@ Ntime
Definition PIOData.h:229
void print(std::ostream &)
void AddCharData(const char *_name)
Definition PIOData.h:99
int get_pio_signature() const
Definition PIOData.h:81
void GetPIOData(PIO_FIELD &, const double *&)
int get_PIO_INDEX_LENGTH() const
Definition PIOData.h:77
void GetPIOData(PIO_FIELD &, const double *&, const char *&)
void AddRealData(const char *_name)
Definition PIOData.h:94
void GetPIOData(const char *, const double *&)
PIO_FIELD * get_pio_field() const
Definition PIOData.h:82
bool GetPIOfileTime(const char *, double &)
void GetPIOData(const char *, const char *&)
double GetPIOData(const char *, int)
const double * GetPIOData(PIO_FIELD &)
PIO_DATA(const char *piofile=nullptr, const std::list< std::string > *fields_to_read=nullptr, bool _defer_read_data=true, const std::set< const char *, Cstring_less > *rdata=nullptr, const std::set< const char *, Cstring_less > *cdata=nullptr)
const char * get_name() const
Definition PIOData.h:72
bool good_read()
Definition PIOData.h:70
int get_pio_num() const
Definition PIOData.h:79
void GetPIOData(PIO_FIELD &, const char *&)
bool verbose
Definition PIOData.h:105
void FreePIOData(PIO_FIELD &pio_field)
bool set_vector_field(std::valarray< std::valarray< double > > &, const char *)
bool set_scalar_field(std::valarray< double > &, const char *)
bool get_reverse_endian() const
Definition PIOData.h:73
int get_pio_num_with_size(int64_t n) const
int get_PIO_HEADER_LENGTH() const
Definition PIOData.h:76
int get_PIO_NAME_LENGTH() const
Definition PIOData.h:75
void GetPIOData(const char *, const double *&, const char *&)
bool set_scalar_field(std::valarray< uint64_t > &, const char *)
bool set_scalar_field(std::valarray< int64_t > &, const char *)
void print(const char *)
const double * GetPIOData(const char *)
bool reconstruct_chunk_field(int64_t numcell, std::valarray< double > &va, const char *prefix, const char *var, int materialId)
VAR_MAP VarMMap
Definition PIOData.h:71
bool set_scalar_field(std::valarray< int > &, const char *)
const char * get_pio_dandt() const
Definition PIOData.h:78
int get_PIO_VERSION() const
Definition PIOData.h:74
int64_t chksum
Definition PIOData.h:37
int index
Definition PIOData.h:34
bool read_field_data
Definition PIOData.h:39
char * pio_name
Definition PIOData.h:33
int64_t length
Definition PIOData.h:35
char * cdata
Definition PIOData.h:44
int64_t position
Definition PIOData.h:36
double * data
Definition PIOData.h:43
size_t cdata_len
Definition PIOData.h:38
@ type
Definition vtkX3D.h:516
bool operator()(const char *p, const char *q) const
Definition PIOData.h:22