Dendro  5.01
Dendro in Greek language means tree. The Dendro library is a large scale (262K cores on ORNL's Titan) distributed memory adaptive octree framework. The main goal of Dendro is to perform large scale multiphysics simulations efficeiently in mordern supercomputers. Dendro consists of efficient parallel data structures and algorithms to perform variational ( finite element) methods and finite difference mthods on 2:1 balanced arbitary adaptive octrees which enables the users to perform simulations raning from black holes (binary black hole mergers) to blood flow in human body, where applications ranging from relativity, astrophysics to biomedical engineering.
oct2vtk.h
1 //
2 // Created by milinda on 5/30/17.
9 //
10 
11 #ifndef SFCSORTBENCH_OCT2VTK_H
12 #define SFCSORTBENCH_OCT2VTK_H
13 
14 #define FNAME_LENGTH 256
15 #define VTK_HEXAHEDRON 12
16 
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"
22 
23 #define DENDRO_FORMAT_ASCII "ascii"
24 #define DENDRO_FORMAT_BINARY "binary"
25 
26 
27 
28 
29 #include "TreeNode.h"
30 #include "mesh.h"
31 #include <iostream>
32 #include <fstream>
33 #include "mpi.h"
34 #include "cencode.h"
35 #include "zlib.h"
36 #include <iostream>
37 #include <string>
38 
39 namespace io
40 {
41  namespace vtk
42  {
43 
44  static FILE *fp = NULL;
45 
46  static void write_vtu_header(void)
47  {
48  fprintf(fp, "# vtk DataFile Version 2.0\n");
49  fprintf(fp, "Dendro-5.0\n");
50 
51 #ifdef DENDRO_VTU_ASCII
52  fprintf(fp, "ASCII\n");
53 #else
54  fprintf(fp, "BINARY\n");
55 #endif
56 
57  }
58 
59 
60  static int vtk_write_compressed (FILE * vtkfile, char *numeric_data,size_t byte_length)
61  {
62  int retval, fseek1, fseek2;
63  size_t iz;
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;
71  uLongf comp_length;
72  base64_encodestate encode_state;
73 
74  /* compute block sizes */
75  blocksize = (size_t) (1 << 15); /* 32768 */
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);
81 
82  /* allocate compression and base64 arrays */
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));
86 
87  /* figure out the size of the header and write a dummy */
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;
95  }
96  base64_init_encodestate (&encode_state);
97  base_length = base64_encode_block ((char *) compression_header,
98  header_size, base_data, &encode_state);
99  base_length +=
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);
105 
106  /* write the regular data blocks */
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);
120  }
121 
122  /* write odd-sized last block if necessary */
123  if (lastsize > 0) {
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);
135  }
136 
137  /* write base64 end block */
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);
142 
143  /* seek back, write header block, seek forward */
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);
148  base_length +=
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);
155 
156  /* clean up and return */
157  free (compression_header);
158  free (comp_data);
159  free (base_data);
160  if (fseek1 != 0 || fseek2 != 0 || ferror (vtkfile)) {
161  return -1;
162  }
163  return 0;
164  }
165 
166 
167  static int vtk_write_binary (FILE * vtkfile, char *numeric_data, size_t byte_length)
168  {
169 
170 #ifdef DENDRO_VTU_ZLIB
171  return vtk_write_compressed(vtkfile,numeric_data,byte_length);
172 #else
173  size_t chunks, chunksize, remaining, writenow;
174  size_t code_length, base_length;
175  uint32_t int_header;
176  char *base_data;
177  base64_encodestate encode_state;
178 
179  /* VTK format used 32bit header info */
180  assert (byte_length <= (size_t) UINT32_MAX);
181 
182  /* This value may be changed although this is not tested with VTK */
183  chunksize = (size_t) 1 << 15; /* 32768 */
184  int_header = (uint32_t) byte_length;
185 
186  /* Allocate sufficient memory for base64 encoder */
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));// (code_length*sizeof(char));
190 
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);
196 
197  chunks = 0;
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;
206  ++chunks;
207  }
208 
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);
213 
214  free(base_data);
215  if (ferror (vtkfile)) {
216  return -1;
217  }
218  return 0;
219 #endif
220 
221 
222  }
223 
224 
225  static std::string getFileName(const std::string& s) {
226 
227  char sep = '/';
228 
229 #ifdef _WIN32
230  sep = '\\';
231 #endif
232 
233  size_t i = s.rfind(sep, s.length());
234  if (i != std::string::npos) {
235  return(s.substr(i+1, s.length() - i));
236  }
237 
238  return(s);
239  }
240 
241 
242 
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);
252 
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);
262 
263 
270  void oct2vtu(const ot::TreeNode *pNodes,const unsigned int nSize,const char* fPrefix, MPI_Comm comm);
271 
272 
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);
282 
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);
292 
293 
305  void waveletsToVTU(ot::Mesh* mesh,const double ** zippedVec, const double **unzippedVec,const unsigned int * varIds,const unsigned int numVars,const char* fileName);
306 
307 
308 
309  }
310 }
311 
312 
313 
314 #endif //SFCSORTBENCH_OCT2VTK_H
A class to manage octants.
Definition: TreeNode.h:35
Definition: mesh.h:179
contains functions to dump out raw data asci/binary. Can be useful for debugging purposes.
Definition: rawIO.h:19
Definition: cencode.h:16