KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
pragmatic_adapt_3d.h
Go to the documentation of this file.
1 #if !defined(KRATOS_PRAGMATIC_ADAPTOR_H_INCLUDED )
2 #define KRATOS_PRAGMATIC_ADAPTOR_H_INCLUDED
3 
4 
5 
6 // System includes
7 #include <string>
8 #include <iostream>
9 #include <cstdlib>
10 #include <boost/timer.hpp>
11 
12 
13 
14 #include "tetgen.h" // Defined tetgenio, tetrahedralize().
15 
16 // Project includes
17 #include "includes/define.h"
18 #include "includes/model_part.h"
19 
20 
21 #include "include/Mesh.h"
22 //#include "include/VTKTools.h"
23 #include "include/MetricField.h"
24 
25 #include "include/Coarsen.h"
26 #include "include/Refine.h"
27 #include "include/Smooth.h"
28 #include "include/Swapping.h"
29 #include "include/ticker.h"
30 
31 #include <mpi.h>
32 
33 
34 namespace Kratos
35 {
36 
38 {
39 public:
40 // void cout_quality(const Mesh<double> *mesh, std::string operation)
41 // {
42 // double qmean = mesh->get_qmean();
43 // double qmin = mesh->get_qmin();
44 //
45 // int rank;
46 // MPI_Comm_rank(MPI_COMM_WORLD, &rank);
47 //
48 // if(rank==0)
49 // std::cout<<operation<<": step in quality (mean, min): ("<<qmean<<", "<<qmin<<")"<<std::endl;
50 // }
51 
52  void AdaptMesh(ModelPart& rmodel_part)
53  {
54 
55 // int required_thread_support=MPI_THREAD_SINGLE;
56 // int provided_thread_support;
57 // MPI_Init_thread(&argc, &argv, required_thread_support, &provided_thread_support);
58 // assert(required_thread_support==provided_thread_support);
59 
60  int rank;
61  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
62 
63  bool verbose = true;
64 
65  // Benchmark times.
66  double time_coarsen=0, time_refine=0, time_swap=0, time_smooth=0, time_adapt=0, tic;
67 
68  //**************************************
69  //here i must import mesh from Kratos
70 
71  mesh->create_boundary();
72 
73 
74  //**************************************
75  //here i shall assign the target metric field
76 
77  MetricField<double,3> metric_field(*mesh);
78 
79  size_t NNodes = mesh->get_number_nodes();
80  double eta=0.05;
81 
82  for(size_t i=0; i<NNodes; i++)
83  {
84  double target_h = 1.0; //TODO: read target H from the nodes!!...
85  double d = 1/sqrt(h);
86  double m[] = {d,0.0,0.0,d, 0.0, d};
87 
88 // double x = 2*mesh->get_coords(i)[0] - 1;
89 // double y = 2*mesh->get_coords(i)[1];
90 //
91 // double m[] = {0.2*(-8*x + 4*sin(5*y))/pow(pow(2*x - sin(5*y), 2) + 0.01, 2) - 250.0*sin(50*x), 2.0*(2*x - sin(5*y))*cos(5*y)/pow(pow(2*x - sin(5*y), 2) + 0.01, 2), 0,
92 // -5.0*(2*x - sin(5*y))*pow(cos(5*y), 2)/pow(pow(2*x - sin(5*y), 2) + 0.01, 2) + 2.5*sin(5*y)/(pow(2*x - sin(5*y), 2) + 0.01), 0,
93 // 0
94 // };
95 
96 /*
97  for(int j=0; j<5; j++)
98  m[j]/=eta;
99  m[5] = 1.0;*/
100 
101  metric_field.set_metric(m, i);
102  }
103  metric_field.apply_max_aspect_ratio(10);
104  metric_field.update_mesh();
105 
106 // VTKTools<double>::export_vtu("../data/test_adapt_3d-initial", mesh);
107 
108 // cout_quality(mesh, "Initial quality");
109 
110  // See Eqn 7; X Li et al, Comp Methods Appl Mech Engrg 194 (2005) 4915-4950
111  double L_up = sqrt(2.0);
112  double L_low = L_up/2;
113 
114  Coarsen<double, 3> coarsen(*mesh);
115  Smooth<double, 3> smooth(*mesh);
116  Refine<double, 3> refine(*mesh);
117  Swapping<double, 3> swapping(*mesh);
118 
119  time_adapt = get_wtime();
120 
121  double L_max = mesh->maximal_edge_length();
122  double alpha = sqrt(2.0)/2;
123  double L_ref = std::max(alpha*L_max, L_up);
124 
125  if(verbose)
126  std::cout<<"Phase I\n";
127 
128  for(size_t i=0; i<10; i++)
129  {
130  // Coarsen
131  tic = get_wtime();
132  coarsen.coarsen(L_low, L_ref, true);
133  time_coarsen += get_wtime() - tic;
134 
135 // if(verbose)
136 // cout_quality(mesh, "Coarsen");
137 
138  // Refine
139  tic = get_wtime();
140  refine.refine(L_ref);
141  time_refine += get_wtime() - tic;
142 
143 // if(verbose)
144 // cout_quality(mesh, "refine");
145 
146  // Swap
147  tic = get_wtime();
148  swapping.swap(0.1);
149  time_swap += get_wtime() - tic;
150 
151 // if(verbose)
152 // cout_quality(mesh, "Swap");
153 
154  // Smooth
155  tic = get_wtime();
156  smooth.smooth(1);
157  time_smooth += get_wtime()-tic;
158 
159 // if(verbose)
160 // cout_quality(mesh, "Smooth");
161 
162  alpha = (1.0-1e-2*i*i)*sqrt(2.0)/2;
163  L_max = mesh->maximal_edge_length();
164  L_ref = std::max(alpha*L_max, L_up);
165 
166 
167  if(L_max>1.0 and (L_max-L_up)<0.01)
168  break;
169  }
170 
171  if(verbose)
172  std::cout<<"Phase II\n";
173 
174  for(size_t i=0; i<5; i++)
175  {
176  tic = get_wtime();
177  coarsen.coarsen(L_up, L_up, true);
178  time_coarsen += get_wtime() - tic;
179 // if(verbose)
180 // cout_quality(mesh, "coarsen");
181 
182  tic = get_wtime();
183  swapping.swap(0.7);
184 // if(verbose)
185 // cout_quality(mesh, "Swap");
186  time_swap += get_wtime() - tic;
187 
188  tic = get_wtime();
189  smooth.smooth(1);
190 // if(verbose)
191 // cout_quality(mesh, "Smooth");
192  time_smooth += get_wtime()-tic;
193  }
194 
195  double time_defrag = get_wtime();
196  mesh->defragment();
197  time_defrag = get_wtime()-time_defrag;
198 
199  if(verbose)
200  {
201  mesh->verify();
202 
203  VTKTools<double>::export_vtu("../data/test_adapt_3d-basic", mesh);
204  }
205 
206  tic = get_wtime();
207  smooth.smooth(20);
208  time_smooth += get_wtime()-tic;
209 
210 // if(verbose)
211 // cout_quality(mesh, "Final smooth");
212 
213  time_adapt = get_wtime()-time_adapt;
214 
215  if(verbose)
216  {
217  if(rank==0)
218  std::cout<<"After optimisation based smoothing:\n";
219  mesh->verify();
220  }
221 
222  VTKTools<double>::export_vtu("../data/test_adapt_3d", mesh);
223 
224  double qmean = mesh->get_qmean();
225  double qmin = mesh->get_qmin();
226 
227  long double volume = mesh->calculate_volume();
228  long double area = mesh->calculate_area();
229 
230  delete mesh;
231 
232  if(rank==0)
233  {
234  std::cout<<"BENCHMARK: time_coarsen time_refine time_swap time_smooth time_defrag time_adapt time_other\n";
235  double time_other = (time_adapt-(time_coarsen+time_refine+time_swap+time_smooth+time_defrag));
236  std::cout<<"BENCHMARK: "
237  <<std::setw(12)<<time_coarsen<<" "
238  <<std::setw(11)<<time_refine<<" "
239  <<std::setw(9)<<time_swap<<" "
240  <<std::setw(11)<<time_smooth<<" "
241  <<std::setw(11)<<time_defrag<<" "
242  <<std::setw(10)<<time_adapt<<" "
243  <<std::setw(10)<<time_other<<"\n";
244 
245  std::cout<<"Expecting qmean>0.7, qmin>0.2: ";
246  if((qmean>0.8)&&(qmin>0.2))
247  std::cout<<"pass"<<std::endl;
248  else
249  std::cout<<"fail (qmean="<<qmean<<", qmin="<<qmin<<")"<<std::endl;
250 
251 // std::cout<<"Expecting volume == 1: ";
252 // if(fabs(volume-1)<DBL_EPSILON)
253 // std::cout<<"pass"<<std::endl;
254 // else
255 // std::cout<<"fail (volume="<<volume<<")"<<std::endl;
256 //
257 // std::cout<<"Expecting area == 6: ";
258 // if(fabs(area-6)<DBL_EPSILON)
259 // std::cout<<"pass"<<std::endl;
260 // else
261 // std::cout<<"fail (area="<<area<<")"<<std::endl;
262  }
263 
264  return 0;
265  }
266 };
267 
268 }
269 
270 #endif // KRATOS_PRAGMATIC_ADAPTOR_H_INCLUDED defined
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Definition: pragmatic_adapt_3d.h:38
void AdaptMesh(ModelPart &rmodel_part)
Definition: pragmatic_adapt_3d.h:52
static double max(double a, double b)
Definition: GeometryFunctions.h:79
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
alpha
Definition: generate_convection_diffusion_explicit_element.py:113
h
Definition: generate_droplet_dynamics.py:91
int d
Definition: ode_solve.py:397
mesh
Definition: read_stl.py:7
int m
Definition: run_marine_rain_substepping.py:8
volume
Definition: sp_statistics.py:15
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31