VTK  9.4.20241222
PIODataPIO.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-FileCopyrightText: Copyright (c) 2021, Triad National Security, LLC
3// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-LANL-Triad-USGov
16#if !defined(_PIODATAPIO_H)
17#define _PIODATAPIO_H
18
19#include <PIOData.h>
20#include <fstream>
21#include <iostream>
22#include <list>
23#include <map>
24#include <set>
25#include <sstream>
26#include <string.h>
27#include <string>
28#include <valarray>
29
30VTK_ABI_NAMESPACE_BEGIN
31// Class Declarations
32class PIO_DATA;
33class PIO_DATA_PIO;
34class PIO_FIELD;
35
36class PIO_DATA_PIO : public PIO_DATA
37{
38public:
39 PIO_DATA_PIO(const char* piofile = nullptr,
40 const std::list<std::string>* fields_to_read = nullptr, bool _defer_read_data = true,
41 const std::set<const char*, Cstring_less>* rdata = nullptr,
42 const std::set<const char*, Cstring_less>* cdata = nullptr);
43 ~PIO_DATA_PIO() override;
44 bool GetPIOfileTime(const char*, double&);
45 void print(std::ostream&);
46 void print(const char*);
47 bool set_scalar_field(std::valarray<int>&, const char*) override;
48 bool set_scalar_field(std::valarray<int64_t>&, const char*) override;
49 bool set_scalar_field(std::valarray<double>&, const char*) override;
50 bool set_vector_field(std::valarray<std::valarray<double>>&, const char*) override;
51 inline bool good_read() override { return (pio_field != nullptr) ? true : false; }
52 const char* get_name() const { return name; }
53 bool get_reverse_endian() const { return reverse_endian; }
54 int get_PIO_VERSION() const { return PIO_VERSION; }
55 int get_PIO_NAME_LENGTH() const { return PIO_NAME_LENGTH; }
56 int get_PIO_HEADER_LENGTH() const { return PIO_HEADER_LENGTH; }
57 int get_PIO_INDEX_LENGTH() const { return PIO_INDEX_LENGTH; }
58 const char* get_pio_dandt() const { return pio_dandt; }
59 int get_pio_num() const override;
60 int get_pio_num_with_size(int64_t n) const;
61 int get_pio_signature() const { return pio_signature; }
62 int get_cycle() override;
63 double get_simtime() override;
64 int get_dimension() override;
65 bool get_gridsize(std::valarray<int>&) override;
66 bool get_gridscale(std::valarray<double>&) override;
67 bool get_gridorigin(std::valarray<double>&) override;
68 std::string get_eap_version() override;
69 std::string get_username() override;
70 std::string get_problemname() override;
71 bool get_material_names(std::valarray<std::string>&) override;
72 bool get_tracer_variable_names(std::valarray<std::string>&) override;
73
74 PIO_FIELD* get_pio_field() const override;
75 void GetPIOData(PIO_FIELD&, const double*&, const char*&);
76 void GetPIOData(PIO_FIELD&, const double*&);
77 void GetPIOData(PIO_FIELD&, const char*&);
78 const double* GetPIOData(PIO_FIELD&);
79 void GetPIOData(const char*, const double*&, const char*&);
80 void GetPIOData(const char*, const double*&);
81 void GetPIOData(const char*, const char*&);
82 const double* GetPIOData(const char*);
83 double GetPIOData(const char*, int);
84 bool reconstruct_chunk_field(int64_t numcell, std::valarray<double>& va, const char* prefix,
85 const char* var, int materialId) override;
86 int get_num_components(const char*) const override;
87 int get_num_materials() const override;
88 int64_t get_num_cells() override;
89 bool has_field(const char*) override; // true if field exists
90
91 void AddRealData(const char* _name)
92 {
93 if (RealData.find(_name) == RealData.end())
94 RealData.insert(strdup(_name));
95 }
96 void AddCharData(const char* _name)
97 {
98 if (CharData.find(_name) == CharData.end())
99 CharData.insert(strdup(_name));
100 }
101 void FreePIOData(PIO_FIELD& pio_field);
103
104private:
105 std::set<const char*, Cstring_less> RealData;
106 std::set<const char*, Cstring_less> CharData;
107 const char* name;
108 std::istream* Infile;
109 bool reverse_endian;
110 int PIO_VERSION;
111 int PIO_NAME_LENGTH;
112 int PIO_HEADER_LENGTH;
113 int PIO_INDEX_LENGTH;
114 const char* pio_dandt; // Date and Time
115 int pio_num;
116 int64_t pio_position;
117 int pio_signature;
118 PIO_FIELD* pio_field;
119 bool defer_read_data;
120 size_t matident_len;
121 size_t timertype_len;
122
123 char* buf;
124 size_t size_buf;
125 void ReadPioFieldData(PIO_FIELD& pio_field);
126 bool read(const char*, const std::list<std::string>* fields_to_read = nullptr);
127 bool read(const std::list<std::string>* fields_to_read = nullptr);
128 inline void byte_flip(char* word, int64_t size)
129 {
130 if (size_buf < (size_t)size)
131 {
132 delete[] buf;
133 size_buf = size;
134 buf = new char[size_buf];
135 }
136 memcpy((void*)buf, (const void*)word, size);
137 for (int64_t i = 0; i < size; ++i)
138 word[i] = buf[size - 1 - i];
139 } // End byte_flip
140
141 template <class T>
142 inline T read_pio_word(T& val)
143 {
144 double word;
145 this->Infile->read((char*)&word, sizeof(word));
146 if (reverse_endian)
147 byte_flip((char*)&word, sizeof(word));
148 val = T(word);
149 return val;
150 } // End read_pio_word
151
152 inline bool read_pio_bool()
153 {
154 double word;
155 this->Infile->read((char*)&word, sizeof(word));
156 if (reverse_endian)
157 byte_flip((char*)&word, sizeof(word));
158 return (word != 0) ? true : false;
159 } // End read_pio_bool
160
161 inline void fstr2Cstr(char* s, size_t len) const
162 {
163 s[len] = '\0';
164 size_t i = len - 1;
165 do
166 {
167 if (s[i--] == ' ')
168 s[i + 1] = '\0';
169 } while (i != 0);
170 } // End fstr2Cstr
171
172 inline char* read_pio_char_string(size_t len)
173 {
174 if (size_buf <= len)
175 {
176 delete[] buf;
177 size_buf = len + 1;
178 buf = new char[size_buf];
179 }
180 this->Infile->read(buf, len);
181 buf[len] = '\0';
182 fstr2Cstr(buf, len);
183 char* val = new char[strlen(buf) + 1];
184 strcpy(val, buf);
185 return val;
186 } // End read_pio_char_string
187
188 inline void insert_VAR_MAP_pairs()
189 {
190 for (int i = 0; i < pio_num; ++i)
191 {
192 if (pio_field[i].read_field_data)
193 {
194#if !defined __SUNPRO_CC
195 VarMMap.insert(std::make_pair(pio_field[i].pio_name, pio_field + i));
196#else
197 VAR_MAP::value_type type(pio_field[i].pio_name, pio_field + i);
198 VarMMap.insert(type);
199#endif
200 }
201 }
202 } // End insert_VAR_MAP_pairs
203
204 inline bool read_field(const char* pio_name, const std::list<std::string>* fields_to_read)
205 {
206 std::string spio_name = std::string(pio_name);
207 if (fields_to_read == nullptr)
208 return true;
209 else
210 {
211 for (std::list<std::string>::const_iterator pos = fields_to_read->begin();
212 pos != fields_to_read->end(); ++pos)
213 {
214 if (spio_name == *pos)
215 return true;
216 }
217 }
218 return false;
219 }
220}; // End class PIO_DATA_PIO
221
222// Prototypes
223bool GetPIOfileTime(const char*, double&);
224bool IsPIOfile(const char*);
225VTK_ABI_NAMESPACE_END
226#endif
bool IsPIOfile(const char *)
bool GetPIOfileTime(const char *, double &)
class for reading PIO (Parallel Input Output) data files
Definition PIODataPIO.h:37
bool set_scalar_field(std::valarray< int64_t > &, const char *) override
PIO_FIELD * get_pio_field() const override
int get_PIO_NAME_LENGTH() const
Definition PIODataPIO.h:55
bool reconstruct_chunk_field(int64_t numcell, std::valarray< double > &va, const char *prefix, const char *var, int materialId) override
void print(std::ostream &)
bool get_gridsize(std::valarray< int > &) override
int64_t get_num_cells() override
int get_dimension() override
bool get_gridorigin(std::valarray< double > &) override
bool get_reverse_endian() const
Definition PIODataPIO.h:53
void GetPIOData(PIO_FIELD &, const double *&, const char *&)
int get_PIO_VERSION() const
Definition PIODataPIO.h:54
int get_pio_num() const override
void AddCharData(const char *_name)
Definition PIODataPIO.h:96
void GetPIOData(PIO_FIELD &, const double *&)
void GetPIOData(PIO_FIELD &, const char *&)
int get_num_components(const char *) const override
void AddRealData(const char *_name)
Definition PIODataPIO.h:91
std::string get_username() override
PIO_DATA_PIO(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)
int get_num_materials() const override
bool has_field(const char *) override
void print(const char *)
void FreePIOData(PIO_FIELD &pio_field)
const char * get_pio_dandt() const
Definition PIODataPIO.h:58
std::string get_problemname() override
std::string get_eap_version() override
bool set_scalar_field(std::valarray< int > &, const char *) override
int get_pio_signature() const
Definition PIODataPIO.h:61
bool GetPIOfileTime(const char *, double &)
const double * GetPIOData(PIO_FIELD &)
double GetPIOData(const char *, int)
void GetPIOData(const char *, const double *&)
bool set_scalar_field(std::valarray< double > &, const char *) override
double get_simtime() override
void GetPIOData(const char *, const double *&, const char *&)
bool get_tracer_variable_names(std::valarray< std::string > &) override
int get_PIO_HEADER_LENGTH() const
Definition PIODataPIO.h:56
bool get_material_names(std::valarray< std::string > &) override
const double * GetPIOData(const char *)
bool good_read() override
Definition PIODataPIO.h:51
~PIO_DATA_PIO() override
int get_cycle() override
int get_pio_num_with_size(int64_t n) const
const char * get_name() const
Definition PIODataPIO.h:52
void GetPIOData(const char *, const char *&)
bool set_vector_field(std::valarray< std::valarray< double > > &, const char *) override
int get_PIO_INDEX_LENGTH() const
Definition PIODataPIO.h:57
bool get_gridscale(std::valarray< double > &) override
class for reading PIO (Parallel Input Output) data files
Definition PIOData.h:66
VAR_MAP VarMMap
Definition PIOData.h:98
@ type
Definition vtkX3D.h:516