11 #ifndef SFCSORTBENCH_OCT2VTK_H 12 #define SFCSORTBENCH_OCT2VTK_H 14 #define FNAME_LENGTH 256 15 #define VTK_HEXAHEDRON 12 17 #define DENDRO_NODE_COORD_TYPE "UInt32" 18 #define DENDRO_NODE_ID_TYPE "UInt64" 19 #define DENDRO_NODE_VAR_INT "UInt32" 20 #define DENDRO_NODE_VAR_FLOAT "Float32" 21 #define DENDRO_NODE_VAR_DOUBLE "Float64" 23 #define DENDRO_FORMAT_ASCII "ascii" 24 #define DENDRO_FORMAT_BINARY "binary" 44 static FILE *fp = NULL;
46 static void write_vtu_header(
void)
48 fprintf(fp,
"# vtk DataFile Version 2.0\n");
49 fprintf(fp,
"Dendro-5.0\n");
51 #ifdef DENDRO_VTU_ASCII 52 fprintf(fp,
"ASCII\n");
54 fprintf(fp,
"BINARY\n");
60 static int vtk_write_compressed (FILE * vtkfile,
char *numeric_data,
size_t byte_length)
62 int retval, fseek1, fseek2;
64 size_t blocksize, lastsize;
65 size_t theblock, numregularblocks, numfullblocks;
66 size_t header_entries, header_size;
67 size_t code_length, base_length;
68 long header_pos, final_pos;
69 char *comp_data, *base_data;
70 uint32_t *compression_header;
75 blocksize = (size_t) (1 << 15);
76 lastsize = byte_length % blocksize;
77 numregularblocks = byte_length / blocksize;
78 numfullblocks = numregularblocks + (lastsize > 0 ? 1 : 0);
79 header_entries = 3 + numfullblocks;
80 header_size = header_entries *
sizeof (uint32_t);
83 code_length = 2 * std::max (blocksize, header_size) + 4 + 1;
84 comp_data =(
char*) malloc (code_length*
sizeof(
char));
85 base_data =(
char*) malloc(code_length*
sizeof(
char));
88 compression_header = (uint32_t*)malloc (header_entries*
sizeof(uint32_t));
89 compression_header[0] = (uint32_t) numfullblocks;
90 compression_header[1] = (uint32_t) blocksize;
91 compression_header[2] = (uint32_t)
92 (lastsize > 0 || byte_length == 0 ? lastsize : blocksize);
93 for (iz = 3; iz < header_entries; ++iz) {
94 compression_header[iz] = 0;
96 base64_init_encodestate (&encode_state);
97 base_length = base64_encode_block ((
char *) compression_header,
98 header_size, base_data, &encode_state);
100 base64_encode_blockend (base_data + base_length, &encode_state);
101 assert (base_length < code_length);
102 base_data[base_length] =
'\0';
103 header_pos = ftell (vtkfile);
104 (void) fwrite (base_data, 1, base_length, vtkfile);
107 base64_init_encodestate (&encode_state);
108 for (theblock = 0; theblock < numregularblocks; ++theblock) {
109 comp_length = code_length;
110 retval = compress2 ((Bytef *) comp_data, &comp_length,
111 (
const Bytef *) (numeric_data + theblock * blocksize),
112 (uLong) blocksize, Z_BEST_COMPRESSION);
113 assert (retval == Z_OK);
114 compression_header[3 + theblock] = comp_length;
115 base_length = base64_encode_block (comp_data, comp_length,
116 base_data, &encode_state);
117 assert (base_length < code_length);
118 base_data[base_length] =
'\0';
119 (void) fwrite (base_data, 1, base_length, vtkfile);
124 comp_length = code_length;
125 retval = compress2 ((Bytef *) comp_data, &comp_length,
126 (
const Bytef *) (numeric_data + theblock * blocksize),
127 (uLong) lastsize, Z_BEST_COMPRESSION);
128 assert (retval == Z_OK);
129 compression_header[3 + theblock] = comp_length;
130 base_length = base64_encode_block (comp_data, comp_length,
131 base_data, &encode_state);
132 assert (base_length < code_length);
133 base_data[base_length] =
'\0';
134 (void) fwrite (base_data, 1, base_length, vtkfile);
138 base_length = base64_encode_blockend (base_data, &encode_state);
139 assert (base_length < code_length);
140 base_data[base_length] =
'\0';
141 (void) fwrite (base_data, 1, base_length, vtkfile);
144 final_pos = ftell (vtkfile);
145 base64_init_encodestate (&encode_state);
146 base_length = base64_encode_block ((
char *) compression_header,
147 header_size, base_data, &encode_state);
149 base64_encode_blockend (base_data + base_length, &encode_state);
150 assert (base_length < code_length);
151 base_data[base_length] =
'\0';
152 fseek1 = fseek (vtkfile, header_pos, SEEK_SET);
153 (void) fwrite (base_data, 1, base_length, vtkfile);
154 fseek2 = fseek (vtkfile, final_pos, SEEK_SET);
157 free (compression_header);
160 if (fseek1 != 0 || fseek2 != 0 || ferror (vtkfile)) {
167 static int vtk_write_binary (FILE * vtkfile,
char *numeric_data,
size_t byte_length)
170 #ifdef DENDRO_VTU_ZLIB 171 return vtk_write_compressed(vtkfile,numeric_data,byte_length);
173 size_t chunks, chunksize, remaining, writenow;
174 size_t code_length, base_length;
180 assert (byte_length <= (
size_t) UINT32_MAX);
183 chunksize = (size_t) 1 << 15;
184 int_header = (uint32_t) byte_length;
187 code_length = 2 * std::max (chunksize,
sizeof (int_header));
188 code_length = std::max (code_length, (
size_t)4) + 1;
189 base_data = (
char*)calloc(code_length,
sizeof(
char));
191 base64_init_encodestate (&encode_state);
192 base_length =base64_encode_block ((
char *) &int_header,
sizeof (int_header), base_data,&encode_state);
193 assert (base_length < code_length);
194 base_data[base_length] =
'\0';
195 (void) fwrite (base_data, 1, base_length, vtkfile);
198 remaining = byte_length;
199 while (remaining > 0) {
200 writenow = std::min (remaining, chunksize);
201 base_length = base64_encode_block (numeric_data + chunks * chunksize,writenow, base_data, &encode_state);
202 assert (base_length < code_length);
203 base_data[base_length] =
'\0';
204 (void) fwrite (base_data, 1, base_length, vtkfile);
205 remaining -= writenow;
209 base_length = base64_encode_blockend (base_data, &encode_state);
210 assert (base_length < code_length);
211 base_data[base_length] =
'\0';
212 (void) fwrite (base_data, 1, base_length, vtkfile);
215 if (ferror (vtkfile)) {
225 static std::string getFileName(
const std::string& s) {
233 size_t i = s.rfind(sep, s.length());
234 if (i != std::string::npos) {
235 return(s.substr(i+1, s.length() - i));
251 void mesh2vtk(
const ot::Mesh *pMesh,
const char *fPrefix,
unsigned int numFieldData,
const char** filedDataNames,
double * filedData,
unsigned int numPointdata,
const char **pointDataNames,
double **pointData);
261 void mesh2vtu(
const ot::Mesh *pMesh,
const char *fPrefix,
unsigned int numFieldData,
const char** filedDataNames,
const double * filedData,
unsigned int numPointdata,
const char **pointDataNames,
const double **pointData);
270 void oct2vtu(
const ot::TreeNode *pNodes,
const unsigned int nSize,
const char* fPrefix, MPI_Comm comm);
281 void mesh2vtuCoarse(
const ot::Mesh *pMesh,
const char *fPrefix,
unsigned int numFieldData,
const char** filedDataNames,
const double * filedData,
unsigned int numPointdata,
const char **pointDataNames,
const double **pointData);
291 void mesh2vtuFine(
const ot::Mesh *pMesh,
const char *fPrefix,
unsigned int numFieldData,
const char** filedDataNames,
const double * filedData,
unsigned int numPointdata,
const char **pointDataNames,
const double **pointData);
305 void waveletsToVTU(
ot::Mesh* mesh,
const double ** zippedVec,
const double **unzippedVec,
const unsigned int * varIds,
const unsigned int numVars,
const char* fileName);
314 #endif //SFCSORTBENCH_OCT2VTK_H A class to manage octants.
Definition: TreeNode.h:35
contains functions to dump out raw data asci/binary. Can be useful for debugging purposes.
Definition: rawIO.h:19