AMR-Solver  1.0
Block-based Octree AMR grid flow solver
vtk.cpp
Go to the documentation of this file.
1 #include "octree.h"
2 #include <stdio.h>
3 #include <iostream>
4 
5 namespace myOctree {
6 
8 long int get_point(int i, int j, int k, int Npx, int Npy) {
9 
10  int I = i+1;
11  int J = j+1;
12  int K = k+1;
13 
14  long int n;
15 
16  n = k * Npx * Npy + j * Npx + I;
17 
18  return n-1;
19 }
20 
22 void write_vtk(std::list<Octree*>& nodes) {
23 
24 
25  int Npx = nx_block + 1;
26  int Npy = ny_block + 1;
27  int Npz = nz_block + 1;
28 
29  std::cerr << "\n" << "Writing VTK" << std::endl;
30 
31  char filename[30];
32  sprintf(filename, "output.vtk");
33  FILE *fp = fopen(filename, "w");
34 
35  fprintf(fp,"# vtk DataFile Version 3.0\n");
36  fprintf(fp,"particle point data\n");
37  fprintf(fp,"ASCII\n");
38  fprintf(fp,"DATASET UNSTRUCTURED_GRID\n");
39  long int nPoints = Npx * Npy * Npz * nodes.size();
40  long int nCells = nx_block * ny_block * nz_block * nodes.size();
41  long int point[8];
42  int node_count = 0;
43 
44 
45  fprintf(fp,"POINTS %ld double\n", nPoints);
46 
47  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
48 
49  Block* block_data = (*iterator)->get_block_data();
50  int N = block_data->mesh->N;
51  double dx = block_data->dx;
52  double dy = block_data->dy;
53  double dz = block_data->dz;
54  double x_min = block_data->x_min;
55  double y_min = block_data->y_min;
56  double z_min = block_data->z_min;
57 
58  //std::cerr << dz << std::endl;
59 
60 
61 // printf("dx=%g dy=%g, dz=%g N =%d\n", dx, dy, dz, N);
62 // printf("east bc = %d\n",(*iterator)->east_bc);
63 // printf("west bc = %d\n",(*iterator)->west_bc);
64 // printf("north bc = %d\n",(*iterator)->north_bc);
65 // printf("south bc = %d\n",(*iterator)->south_bc);
66 // printf("top bc = %d\n",(*iterator)->top_bc);
67 // printf("bottom bc = %d\n",(*iterator)->bottom_bc);
68 
69  for(int k = 0; k<Npz; k++) {
70  for(int j = 0; j<Npy; j++) {
71  for(int i = 0; i<Npx ; i++) {
72  fprintf(fp,"%2.8lf %2.8lf %2.8lf\n",x_min + i*dx, y_min + j*dy, z_min + k*dz);
73  }
74  }
75  }
76 
77 
78  }
79 
80  fprintf(fp,"\n\nCELLS %ld %ld\n", nCells, 9*nCells);
81 
82  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
83 
84  for(int k = 0; k<nz_block; k++) {
85  for(int j = 0; j<ny_block; j++) {
86  for(int i = 0; i<nx_block ; i++) {
87 
88  point[0] = node_count*Npx*Npy*Npz + get_point(i,j,k,Npx,Npy);
89  point[1] = node_count*Npx*Npy*Npz + get_point(i+1,j,k,Npx,Npy);
90  point[2] = node_count*Npx*Npy*Npz + get_point(i+1,j+1,k,Npx,Npy);
91  point[3] = node_count*Npx*Npy*Npz + get_point(i,j+1,k,Npx,Npy);
92  point[4] = node_count*Npx*Npy*Npz + get_point(i,j,k+1,Npx,Npy);
93  point[5] = node_count*Npx*Npy*Npz + get_point(i+1,j,k+1,Npx,Npy);
94  point[6] = node_count*Npx*Npy*Npz + get_point(i+1,j+1,k+1,Npx,Npy);
95  point[7] = node_count*Npx*Npy*Npz + get_point(i,j+1,k+1,Npx,Npy);
96 
97  fprintf(fp,"8 %ld %ld %ld %ld %ld %ld %ld %ld \n",point[0], point[1], point[2], point[3], point[4], point[5], point[6], point[7]);
98  }
99  }
100  }
101 
102  node_count++;
103 
104  }
105 
106 
107  fprintf(fp,"\n\nCELL_TYPES %ld\n", nCells);
108 
109  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
110 
111  for(int k = 0; k<nz_block; k++) {
112  for(int j = 0; j<ny_block; j++) {
113  for(int i = 0; i<nx_block; i++) {
114 
115  fprintf(fp,"12\n");
116  }
117  }
118  }
119 
120 
121  }
122 
123  fprintf(fp,"\n\nCELL_DATA %ld\n", nCells);
124  fprintf(fp,"SCALARS input_field_data double 1\n");
125  fprintf(fp,"LOOKUP_TABLE default\n");
126 
127  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
128 
129 
130  Block* block_data = (*iterator)->get_block_data();
131 
132 
133  for(int k = pad; k<(nz_block+pad); k++) {
134  for(int j = pad; j<(ny_block+pad); j++) {
135  for(int i = pad; i<(nx_block+pad); i++) {
136 
137  fprintf(fp,"%lf \n",block_data->field->val[i][j][k]);
138  }
139  }
140  }
141 
142  }
143 
144 
145  for (int f = 0 ; f < scalar_fields.size() ; f++) {
146  fprintf(fp,"\nSCALARS %s double 1\n",scalar_fields[f].c_str());
147  fprintf(fp,"LOOKUP_TABLE default\n");
148 
149  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
150 
151 
152  Block* block_data = (*iterator)->get_block_data();
153 
154 
155  for(int k = pad; k<(nz_block+pad); k++) {
156  for(int j = pad; j<(ny_block+pad); j++) {
157  for(int i = pad; i<(nx_block+pad); i++) {
158 
159  fprintf(fp,"%lf \n",block_data->scalarfields[f]->val[i][j][k]);
160  }
161  }
162  }
163 
164  }
165  }
166 
167  fprintf(fp,"\nVECTORS location_data double\n");
168  //fprintf(fp,"LOOKUP_TABLE default\n");
169 
170  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
171 
172 
173  Block* block_data = (*iterator)->get_block_data();
174 
175  //std::cerr << block_data->z_max << std::endl;
176 
177  for(int k = pad; k<(nz_block+pad); k++) {
178  for(int j = pad; j<(ny_block+pad); j++) {
179  for(int i = pad; i<(nx_block+pad); i++) {
180  fprintf(fp,"%lf %lf %lf\n",block_data->mesh->x[i][j][k], block_data->mesh->y[i][j][k], block_data->mesh->z[i][j][k]);
181  }
182  }
183  }
184  }
185 
186  for (int f = 0 ; f < vector_fields.size() ; f++) {
187  fprintf(fp,"\nVECTORS %s double\n",vector_fields[f].c_str());
188  //fprintf(fp,"LOOKUP_TABLE default\n");
189 
190  for (std::list<Octree*>::iterator iterator = nodes.begin(), end = nodes.end(); iterator != end; ++iterator) {
191 
192  Block* block_data = (*iterator)->get_block_data();
193 
194  for(int k = pad; k<(nz_block+pad); k++) {
195  for(int j = pad; j<(ny_block+pad); j++) {
196  for(int i = pad; i<(nx_block+pad); i++) {
197 
198  fprintf(fp,"%lf %lf %lf\n",block_data->vectorfields[f]->x[i][j][k], block_data->vectorfields[f]->y[i][j][k], block_data->vectorfields[f]->z[i][j][k]);
199  }
200  }
201  }
202 
203  }
204  }
205 
206 }
207 
208 }
This class is a generic data block of a octree node in the block-based AMR mesh.
Definition: block.h:26
std::vector< std::string > scalar_fields
Definition: block.cpp:8
double dy
Definition: block.h:57
VecField * mesh
Definition: block.h:30
int ny_block
Definition: block.cpp:11
double x_min
Definition: block.h:43
VecField ** vectorfields
Definition: block.h:35
double *** z
Definition: vecfield.h:27
double dx
Definition: block.h:57
std::vector< std::string > vector_fields
Definition: block.cpp:9
long int get_point(int i, int j, int k, int Npx, int Npy)
Definition: vtk.cpp:8
int pad
Definition: block.cpp:7
double *** val
Definition: field.h:20
int nz_block
Definition: block.cpp:12
double z_min
Definition: block.h:45
int nx_block
Definition: block.cpp:10
double y_min
Definition: block.h:44
AMR grid stuff.
Definition: adapt.cpp:6
std::list< Octree * > nodes
Definition: octreegrid.cpp:14
double dz
Definition: block.h:57
double *** x
Definition: vecfield.h:25
double *** y
Definition: vecfield.h:26
Field * field
Definition: block.h:31
void write_vtk(std::list< Octree * > &nodes)
Definition: vtk.cpp:22
Field ** scalarfields
Definition: block.h:34