AMR-Solver  1.0
Block-based Octree AMR grid flow solver
adapt.cpp
Go to the documentation of this file.
1 #include "octreegrid.h"
2 #include "vtk.h"
3 #include <complex>
4 #include <iostream>
5 
6 namespace myOctree {
7 
9 
10 
12 //void set_refinement_criteria() {
13 //
14 // create_list_of_leaf_nodes();
15 //
16 // for (std::list<Octree*>::iterator i = leaf_nodes.begin(), end = leaf_nodes.end(); i != end; ++i) {
17 //
18 // if((*i)->get_level() >= max_level)
19 // continue;
20 //
21 // if((*i)->contains(0.1,0.9,0.99))
22 // (*i)->set_to_refine_with_nesting();
23 // if((*i)->contains(1.1,1.1,0.99))
24 // (*i)->set_to_refine_with_nesting();
25 // // if((*i)->contains(2.1,0.9,0.99))
26 // // (*i)->set_to_refine_with_nesting();
27 // // if((*i)->contains(5.9,0.1,0.99))
28 // // (*i)->set_to_refine_with_nesting();
29 // // if((*i)->contains(12.123,1.789,0.99))
30 // // (*i)->set_to_refine_with_nesting();
31 // // if((*i)->contains(18.9,2.9,0.99))
32 // // (*i)->set_to_refine_with_nesting();
33 //
34 // }
35 //}
36 
37 
39 void refine_nodes() {
40 
42 
43  for (std::list<Octree*>::iterator i = leaf_nodes.begin(), end = leaf_nodes.end(); i != end; ++i) {
44 
45  if((*i)->get_level() >= max_level)
46  continue;
47 
48  if((*i)->setToRefine)
49  (*i)->refine();
50 
51  }
52 
53 }
54 
55 
57 //void set_coarse_criteria() {
58 //
59 // create_list_of_leaf_nodes();
60 //
61 // for (std::list<Octree*>::iterator i = leaf_nodes.begin(), end = leaf_nodes.end(); i != end; ++i) {
62 //
63 // if(!((*i)->isRootNode())) {
64 //
65 // //setting coarsening criteria to siblings
66 // if((*i)->contains(1.1,0.9,0.99))
67 // (*i)->set_to_coarsen_with_nesting();
68 //
69 // if((*i)->contains(0.9,1.1,0.99))
70 // (*i)->set_to_coarsen_with_nesting();
71 //
72 // if((*i)->contains(1.1,1.1,0.99))
73 // (*i)->set_to_coarsen_with_nesting();
74 //
75 // if((*i)->contains(0.9,0.9,0.99))
76 // (*i)->set_to_coarsen_with_nesting();
77 //
78 // }
79 //
80 // }
81 //}
82 
83 
85 void coarsen_nodes() {
86 
88 
89  for (std::list<Octree*>::iterator i = nodes.begin(), end = nodes.end(); i != end;) {
90 
91  if((*i)->setToCoarsen && !((*i)->isRootNode()) && ((*i)->isLeafNode())) {
92 
93  //printf("deleting\n");
94  //setting the children of its parent to NULL
95  for(int n=0; n<2; n++) {
96  for(int m=0; m<2; m++) {
97  for(int l=0; l<2; l++) {
98  (*i)->get_parent()->set_child_null_at(l, m, n);
99  }
100  }
101  }
102 
103  i = nodes.erase(i);
104  }
105  else {
106  ++i;
107  }
108  }
109 
110 }
111 
112 
117 
118  for (std::list<Octree*>::iterator i = nodes.begin(), end = nodes.end(); i != end; ++i) {
119 
120  (*i)->setToRefine = false;
121  }
122 
123 }
124 
129 
130  for (std::list<Octree*>::iterator i = nodes.begin(), end = nodes.end(); i != end; ++i) {
131 
132  (*i)->setToCoarsen = false;
133  }
134 }
135 
136 
139 
140  double x_grad, y_grad, z_grad;
141  double dx, dy, dz;
142  double mg;
143  double tg;
144  double max_of_max_gradient = 0.0;
145 
146 
148 
149 
150  //calculation of max_total_gradient
151  for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {
152 
153  //field based on which gradient is to calculated
154  Field* f = (*it)->get_block_data()->field;
155  dx = (*it)->get_block_data()->dx;
156  dy = (*it)->get_block_data()->dy;
157  dz = (*it)->get_block_data()->dz;
158 
159  mg = 0.0;
160 
161  for(int i=pad;i<(f->Nx-pad);i++) {
162  for(int j=pad;j<(f->Ny-pad);j++) {
163  for(int k=pad;k<(f->Nz-pad);k++) {
164 
165  //central difference gradient calculation
166  x_grad = (f->val[i+1][j][k] - f->val[i-1][j][k]) / (2.0*dx);
167  y_grad = (f->val[i][j+1][k] - f->val[i][j-1][k]) / (2.0*dy);
168  z_grad = (f->val[i][j][k+1] - f->val[i][j][k-1]) / (2.0*dz);
169 
170  tg = std::abs(x_grad) + std::abs(y_grad) + std::abs(z_grad);
171  if(tg>mg) mg = tg;
172  //if((*it)->get_block_data()->total_gradient!=0.0) printf("Yes not zero\n");
173  }
174  }
175  }
176 
177  (*it)->get_block_data()->max_gradient = mg;
178  if(mg>max_of_max_gradient) max_of_max_gradient = mg;
179  }
180 
181  //setting flags
182  for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {
183 
184  if((*it)->get_block_data()->max_gradient > max_of_max_gradient*0.2) {
185  (*it)->set_to_refine_with_nesting();
186  //printf("yes refine\n");
187  }
188  }
189 }
190 
191 
194 
195  double x_grad, y_grad, z_grad;
196  double dx, dy, dz;
197  double mg;
198  double tg;
199  double max_of_max_gradient = 0.0;
200 
201 
203 
204  //calculation of max_total_gradient
205  for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {
206 
207  //field based on which gradient is to calculated
208  Field* f = (*it)->get_block_data()->field;
209  dx = (*it)->get_block_data()->dx;
210  dy = (*it)->get_block_data()->dy;
211  dz = (*it)->get_block_data()->dz;
212 
213  mg = 0.0;
214 
215  for(int i=pad;i<(f->Nx-pad);i++) {
216  for(int j=pad;j<(f->Ny-pad);j++) {
217  for(int k=pad;k<(f->Nz-pad);k++) {
218 
219  //central difference gradient calculation
220  x_grad = (f->val[i+1][j][k] - f->val[i-1][j][k]) / (2.0*dx);
221  y_grad = (f->val[i][j+1][k] - f->val[i][j-1][k]) / (2.0*dy);
222  z_grad = (f->val[i][j][k+1] - f->val[i][j][k-1]) / (2.0*dz);
223 
224  tg = std::abs(x_grad) + std::abs(y_grad) + std::abs(z_grad);
225  if(tg>mg) mg = tg;
226  //if((*it)->get_block_data()->total_gradient!=0.0) printf("Yes not zero\n");
227  }
228  }
229  }
230 
231  (*it)->get_block_data()->max_gradient = mg;
232  if(mg>max_of_max_gradient) max_of_max_gradient = mg;
233  }
234 
235  //setting flags
236  for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {
237 
238  //printf("mg mmg = %lf %lf\n",(*it)->get_block_data()->max_gradient,max_of_max_gradient);
239 
240 
241  //Not set to coarsen if it is a root node
242  if((*it)->get_block_data()->max_gradient < max_of_max_gradient*0.2 && (!(*it)->isRootNode())) {
243  (*it)->set_to_coarsen_with_nesting();
244  //printf("yes coarsen\n");
245  }
246  }
247 
248 }
249 
254 
256 
257 
258  for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {
259 
260  if((*it)->setToCoarsen == true) {
261 
262  for(int n=0; n<2; n++) {
263  for(int m=0; m<2; m++) {
264  for(int l=0; l<2; l++) {
265  if((*it)->get_parent()->get_child_at(l,m,n)->setToCoarsen == false)
266  (*it)->setToCoarsen = false;
267  }
268  }
269  }
270  }
271  }
272 
273 }
274 
275 }
void reset_refine_flags()
Definition: adapt.cpp:116
std::list< Octree * > leaf_nodes
Definition: octreegrid.cpp:15
void reset_coarsen_flags()
Definition: adapt.cpp:128
void recheck_siblings_coarsen_flags()
Definition: adapt.cpp:253
int max_level
Definition: adapt.cpp:8
void set_refine_flag_based_on_gradient()
Definition: adapt.cpp:138
void set_coarsen_flag_based_on_gradient()
Definition: adapt.cpp:193
Template class for any scalar field variable in the domain.
Definition: field.h:11
int pad
Definition: block.cpp:7
double *** val
Definition: field.h:20
void coarsen_nodes()
Definition: adapt.cpp:85
void create_list_of_leaf_nodes()
Definition: octreegrid.cpp:21
AMR grid stuff.
Definition: adapt.cpp:6
std::list< Octree * > nodes
Definition: octreegrid.cpp:14
void refine_nodes()
Definition: adapt.cpp:39