PRISMS-PF  v2.1
test_FloodFiller.h
Go to the documentation of this file.
1 // Unit tests for the class "FloodFiller"
2 #include "../../include/FloodFiller.h"
3 
4 template <int dim>
5 class InitialConditionFloodFill : public dealii::Function<dim>
6 {
7 public:
8  InitialConditionFloodFill () : dealii::Function<dim>(1){
9  std::srand(dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)+1);
10  }
11  double value (const dealii::Point<dim> &p, const unsigned int component=0) const{
12 
13  double val;
14  if (p[1] < 0.6 and p[0] > p[1]+1.0e-10){
15  val = 1.0;
16  }
17  else if (p[0] < 0.1 and p[1] > 0.9){
18  val = 1.0;
19  }
20  else {
21  val = 0.0;
22  }
23 
24  //std::cout << p[0] << "\t" << p[1] << "\t" << val << std::endl;
25  return val;
26  };
27 
28 };
29 
30 template <int dim>
31 void setExpectedVertexLists(std::vector<std::vector<dealii::Point<dim>>> & expected_vertex_list0, std::vector<std::vector<dealii::Point<dim>>> & expected_vertex_list1){
32  // expected_vertex_list0
33  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
34  {dealii::Point<dim> p(0.0,0.0); vertex_set[0] = p;}
35  {dealii::Point<dim> p(0.25,0.0); vertex_set[1] = p;}
36  {dealii::Point<dim> p(0.0,0.25); vertex_set[2] = p;}
37  {dealii::Point<dim> p(0.25,0.25); vertex_set[3] = p;}
38  expected_vertex_list0.push_back(vertex_set);}
39 
40  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
41  {dealii::Point<dim> p(0.25,0.0); vertex_set[0] = p;}
42  {dealii::Point<dim> p(0.5,0.0); vertex_set[1] = p;}
43  {dealii::Point<dim> p(0.25,0.25); vertex_set[2] = p;}
44  {dealii::Point<dim> p(0.5,0.25); vertex_set[3] = p;}
45  expected_vertex_list0.push_back(vertex_set);}
46 
47  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
48  {dealii::Point<dim> p(0.5,0.0); vertex_set[0] = p;}
49  {dealii::Point<dim> p(0.75,0.0); vertex_set[1] = p;}
50  {dealii::Point<dim> p(0.5,0.25); vertex_set[2] = p;}
51  {dealii::Point<dim> p(0.75,0.25); vertex_set[3] = p;}
52  expected_vertex_list0.push_back(vertex_set);}
53 
54  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
55  {dealii::Point<dim> p(0.75,0.0); vertex_set[0] = p;}
56  {dealii::Point<dim> p(1.0,0.0); vertex_set[1] = p;}
57  {dealii::Point<dim> p(0.75,0.25); vertex_set[2] = p;}
58  {dealii::Point<dim> p(1.0,0.25); vertex_set[3] = p;}
59  expected_vertex_list0.push_back(vertex_set);}
60 
61  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
62  {dealii::Point<dim> p(0.25,0.25); vertex_set[0] = p;}
63  {dealii::Point<dim> p(0.5,0.25); vertex_set[1] = p;}
64  {dealii::Point<dim> p(0.25,0.5); vertex_set[2] = p;}
65  {dealii::Point<dim> p(0.5,0.5); vertex_set[3] = p;}
66  expected_vertex_list0.push_back(vertex_set);}
67 
68  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
69  {dealii::Point<dim> p(0.5,0.25); vertex_set[0] = p;}
70  {dealii::Point<dim> p(0.75,0.25); vertex_set[1] = p;}
71  {dealii::Point<dim> p(0.5,0.5); vertex_set[2] = p;}
72  {dealii::Point<dim> p(0.75,0.5); vertex_set[3] = p;}
73  expected_vertex_list0.push_back(vertex_set);}
74 
75  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
76  {dealii::Point<dim> p(0.75,0.25); vertex_set[0] = p;}
77  {dealii::Point<dim> p(1.0,0.25); vertex_set[1] = p;}
78  {dealii::Point<dim> p(0.75,0.5); vertex_set[2] = p;}
79  {dealii::Point<dim> p(1.0,0.5); vertex_set[3] = p;}
80  expected_vertex_list0.push_back(vertex_set);}
81 
82  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
83  {dealii::Point<dim> p(0.5,0.5); vertex_set[0] = p;}
84  {dealii::Point<dim> p(0.75,0.5); vertex_set[1] = p;}
85  {dealii::Point<dim> p(0.5,0.75); vertex_set[2] = p;}
86  {dealii::Point<dim> p(0.75,0.75); vertex_set[3] = p;}
87  expected_vertex_list0.push_back(vertex_set);}
88 
89  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
90  {dealii::Point<dim> p(0.75,0.5); vertex_set[0] = p;}
91  {dealii::Point<dim> p(1.0,0.5); vertex_set[1] = p;}
92  {dealii::Point<dim> p(0.75,0.75); vertex_set[2] = p;}
93  {dealii::Point<dim> p(1.0,0.75); vertex_set[3] = p;}
94  expected_vertex_list0.push_back(vertex_set);}
95 
96  // expected_vertex_list1
97  {std::vector<dealii::Point<dim>> vertex_set(dealii::Utilities::fixed_power<dim>(2.0));
98  {dealii::Point<dim> p(0.0,0.75); vertex_set[0] = p;}
99  {dealii::Point<dim> p(0.25,0.75); vertex_set[1] = p;}
100  {dealii::Point<dim> p(0.0,1.0); vertex_set[2] = p;}
101  {dealii::Point<dim> p(0.25,1.0); vertex_set[3] = p;}
102 
103  expected_vertex_list1.push_back(vertex_set);}
104 }
105 
106 template <int dim>
107 bool compareUnsortedVectors(std::vector<std::vector<dealii::Point<dim>>> vec1, std::vector<std::vector<dealii::Point<dim>>> vec2, double tolerance){
108  if (vec1.size() != vec2.size()){
109  return false;
110  }
111 
112  for (unsigned int i=0; i<vec1.size(); i++){
113  bool element_found = false;
114  for (unsigned int j=0; j<vec2.size(); j++){
115  bool elements_equal = true;
116  for (unsigned int v=0; v < dealii::Utilities::fixed_power<dim>(2.0); v++){
117  if (vec1[i][v].distance(vec2[j][v]) > tolerance){
118  elements_equal = false;
119  break;
120  }
121  }
122  element_found = elements_equal;
123  if (element_found){
124  break;
125  }
126  }
127  if (!element_found){
128  return false;
129  }
130  }
131 
132  return true;
133 }
134 
135 template <int dim,typename T>
137 
138  char buffer[100];
139 
140  std::cout << "\nTesting 'FloodFiller'... " << std::endl;
141 
142  bool pass = false;
143 
144  // Create the test mesh
145  parallel::distributed::Triangulation<dim> triangulation(MPI_COMM_WORLD);
146  GridGenerator::hyper_cube(triangulation);
147  triangulation.refine_global(2);
148 
149  const unsigned int degree = 1;
150 
151  FESystem<dim> fe(FE_Q<dim>(QGaussLobatto<1>(degree+1)),1);
152  DoFHandler<dim> dof_handler(triangulation);
153  dof_handler.distribute_dofs(fe);
154  IndexSet locally_relevant_dofs;
155  DoFTools::extract_locally_relevant_dofs (dof_handler, locally_relevant_dofs);
156 
157 
158  typename MatrixFree<dim,double>::AdditionalData additional_data;
159  #if (DEAL_II_VERSION_MAJOR < 9 && DEAL_II_VERSION_MINOR < 5)
160  additional_data.mpi_communicator = MPI_COMM_WORLD;
161  #endif
162  additional_data.tasks_parallel_scheme = MatrixFree<dim,double>::AdditionalData::partition_partition;
163  additional_data.mapping_update_flags = (update_values | update_gradients | update_JxW_values | update_quadrature_points);
164  QGaussLobatto<1> quadrature (degree+1);
165 
166  ConstraintMatrix constraints;
167  constraints.clear();
168 
169  dealii::MatrixFree<dim,double> matrixFreeObject;
170  matrixFreeObject.clear();
171  matrixFreeObject.reinit (dof_handler, constraints, quadrature, additional_data);
172 
173  vectorType *solution_field;
174  solution_field = new vectorType;
175  matrixFreeObject.initialize_dof_vector(*solution_field, 0); *solution_field = 0;
176 
177  // Set the field value
178  VectorTools::interpolate (dof_handler, InitialConditionFloodFill<dim>(), *solution_field);
179 
180  solution_field->update_ghost_values();
181 
182  //solution_field->print(std::cout);
183 
184  // Create a FloodFiller object
185  QGaussLobatto<dim> quadrature2 (degree+1);
186 
187  FloodFiller<dim, degree> test_object(fe, quadrature2);
188  std::vector<GrainSet<dim>> grain_sets;
189  test_object.calcGrainSets(fe, dof_handler, solution_field, 0.1, 1.1, 0, grain_sets);
190 
191  std::vector<std::vector<dealii::Point<dim>>> expected_vertex_list0, expected_vertex_list1;
192 
193  setExpectedVertexLists(expected_vertex_list0, expected_vertex_list1);
194 
195 
196  bool result = false;
197  bool result0, result1;
198  if (grain_sets.size() == 2){
199 
200  std::vector<std::vector<dealii::Point<dim>>> vertex_list0 = grain_sets[0].getVertexList();
201 
202  std::vector<std::vector<dealii::Point<dim>>> vertex_list1 = grain_sets[1].getVertexList();
203 
204  // Get the order in the canonical order
205  if (vertex_list0.size() == 1){
206  vertex_list0.swap(vertex_list1);
207  }
208 
209  if (vertex_list0.size() == 9){
210  result0 = compareUnsortedVectors(vertex_list0, expected_vertex_list0, 1.0e-3);
211  std::cout << "Subtest result for grain 0: " << result0 << std::endl;
212  }
213  else {
214  result0 = false;
215  }
216 
217  if (vertex_list1.size() == 1){
218  result1 = compareUnsortedVectors(vertex_list1, expected_vertex_list1, 1.0e-3);
219  std::cout << "Subtest result for grain 1: " << result1 << std::endl;
220  }
221  else {
222  result1 = false;
223  }
224 
225 
226  result = result0 and result1;
227  }
228 
229  pass = result;
230 
231  sprintf (buffer, "Test result for 'FloodFiller': %u\n", pass);
232  std::cout << buffer;
233 
234  return pass;
235 }
void calcGrainSets(dealii::FESystem< dim > &fe, dealii::DoFHandler< dim > &dof_handler, vectorType *solution_field, double threshold_lower, double threshold_upper, unsigned int order_parameter_index, std::vector< GrainSet< dim >> &grain_sets)
Definition: FloodFiller.cc:5
void setExpectedVertexLists(std::vector< std::vector< dealii::Point< dim >>> &expected_vertex_list0, std::vector< std::vector< dealii::Point< dim >>> &expected_vertex_list1)
bool compareUnsortedVectors(std::vector< std::vector< dealii::Point< dim >>> vec1, std::vector< std::vector< dealii::Point< dim >>> vec2, double tolerance)
dealii::parallel::distributed::Vector< double > vectorType
Definition: FloodFiller.h:15
bool test_FloodFiller()
double value(const dealii::Point< dim > &p, const unsigned int component=0) const