OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
LNorms.hpp
Go to the documentation of this file.
1 //
2 // Created by jstark on 2020-05-17.
3 //
14 #ifndef ACCURACY_TESTS_LNORMS_HPP
15 #define ACCURACY_TESTS_LNORMS_HPP
16 
17 #include <iostream>
18 #include <typeinfo>
19 #include <cmath>
20 #include <cstdio>
21 #include <stdlib.h>
22 
23 // Include OpenFPM header files
24 #include "Vector/vector_dist.hpp"
25 #include "Grid/grid_dist_id.hpp"
26 #include "util/PathsAndFiles.hpp"
27 
28 #include "level_set/redistancing_Sussman/HelpFunctions.hpp" // for printing to_string_with_precision
29 
42 template <size_t PropNumeric, size_t PropAnalytic, size_t Error, typename gridtype>
43 void get_absolute_error(gridtype & grid)
44 {
45  auto dom = grid.getDomainIterator();
46  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
47  grid.template get<PropNumeric>(dom.get()))>> numeric_type;
48  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
49  grid.template get<PropAnalytic>(dom.get()))>> analytic_type;
50 
51  if(!(std::is_same<numeric_type, analytic_type>::value))
52  {
53  std::cout << "Type of numerical and analytical solution must be the same! Aborting..." << std::endl;
54  abort();
55  } while(dom.isNext())
56  {
57  auto key = dom.get();
58  grid.template getProp<Error> (key) = abs(grid.template getProp<PropAnalytic> (key) - (grid.template getProp<PropNumeric> (key)));
59  ++dom;
60  }
61 }
74 template <size_t PropNumeric, size_t PropAnalytic, size_t Error, typename gridtype>
75 void get_relative_error(gridtype & grid)
76 {
77  auto dom = grid.getDomainIterator();
78  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
79  grid.template get<PropNumeric>(dom.get()))>> numeric_type;
80  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
81  grid.template get<PropAnalytic>(dom.get()))>> analytic_type;
82 
83  if(!(std::is_same<numeric_type, analytic_type>::value))
84  {
85  std::cout << "Type of numerical and analytical solution must be the same! Aborting..." << std::endl;
86  abort();
87  }
88 
89  while(dom.isNext())
90  {
91  auto key = dom.get();
92  grid.template getProp<Error> (key) = abs( 1 - (grid.template getProp<PropNumeric> (key) / (grid.template
93  getProp<PropAnalytic> (key))) );
94  ++dom;
95  }
96 }
97 
102 template <typename lnorm_type=double>
103 class LNorms
104 {
105 public:
106  LNorms()
107  {
108  l2 = 0;
109  linf = 0;
110  }
111  // Member variables
112  lnorm_type l2; // L2 norm
113  lnorm_type linf; // L_infinity norm
114 
115  // Member functions
122  template <size_t Error, typename gridtype>
123  void get_l_norms_grid(gridtype & grid)
124  {
125  auto dom = grid.getDomainIterator();
126  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
127  grid.template get<Error>(dom.get()))>> error_type;
128 
129  error_type maxError = 0;
130  error_type sumErrorSq = 0;
131 
132  while(dom.isNext())
133  {
134  auto key = dom.get();
135  sumErrorSq += grid.template getProp<Error> (key) * grid.template getProp<Error> (key);
136  if (grid.template getProp<Error> (key) > maxError) maxError = grid.template getProp<Error> (key); // update maxError
137  ++dom;
138  }
139  auto &v_cl = create_vcluster();
140  v_cl.sum(sumErrorSq);
141  v_cl.max(maxError);
142  v_cl.execute();
143  l2 = (lnorm_type) sqrt( sumErrorSq / (error_type)grid.size());
144  linf = (lnorm_type) maxError;
145  }
152  template <size_t Error, typename vectortype>
153  void get_l_norms_vector(vectortype & vd)
154  {
155  auto dom = vd.getDomainIterator();
156  typedef typename std::remove_const_t<std::remove_reference_t<decltype(
157  vd.template getProp<Error>(dom.get()))>> error_type;
158 
159  error_type maxError = 0;
160  error_type sumErrorSq = 0;
161  int count = 0;
162  while(dom.isNext())
163  {
164  auto key = dom.get();
165  sumErrorSq += vd.template getProp<Error> (key) * vd.template getProp<Error> (key);
166  if (vd.template getProp<Error> (key) > maxError) maxError = vd.template getProp<Error> (key); // update maxError
167  ++dom;
168  ++count;
169  }
170  auto &v_cl = create_vcluster();
171  v_cl.sum(sumErrorSq);
172  v_cl.sum(count);
173  v_cl.max(maxError);
174  v_cl.execute();
175  l2 = (lnorm_type) sqrt( sumErrorSq / (error_type)count);
176  linf = (lnorm_type) maxError;
177  }
186  void write_to_file(const size_t N,
187  const int precision,
188  const std::string & filename,
189  const std::string & path_output)
190  {
191  auto &v_cl = create_vcluster();
192  if (v_cl.rank() == 0)
193  {
194  std::string path_output_lnorm = path_output + "/" + filename + ".csv";
195  create_file_if_not_exist(path_output_lnorm);
196 
197  std::ofstream l_out;
198  l_out.open(path_output_lnorm, std::ios_base::app); // append instead of overwrite
199  l_out << std::to_string(N)
200  << ',' << to_string_with_precision(l2, precision)
201  << ',' << to_string_with_precision(linf, precision) << std::endl;
202  l_out.close();
203  }
204  }
205 
206 private:
207 
208 
209 };
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 #endif //ACCURACY_TESTS_LNORMS_HPP
220 
void get_relative_error(gridtype &grid)
At each grid node, the relative error is computed and stored in another property.
Definition: LNorms.hpp:75
std::string to_string_with_precision(const T myValue, const size_t n=6)
Converts value into string maintaining a desired precision.
static void create_file_if_not_exist(std::string path)
Creates a file if not already existent.
void write_to_file(const size_t N, const int precision, const std::string &filename, const std::string &path_output)
Writes the N (number of grid points on a square grid) and L-norms as strings to a csv-file.
Definition: LNorms.hpp:186
void get_absolute_error(gridtype &grid)
At each grid node, the absolute error is computed and stored in another property.
Definition: LNorms.hpp:43
void get_l_norms_grid(gridtype &grid)
Computes the L_2 and L_infinity norm on the basis of the precomputed error on a grid.
Definition: LNorms.hpp:123
void get_l_norms_vector(vectortype &vd)
Computes the L_2 and L_infinity norm on the basis of the precomputed error on a particle vector.
Definition: LNorms.hpp:153
Header file containing functions for creating files and folders.
Class for computing the l2/l_infinity norm for distributed grids and vectors based on given errors.
Definition: LNorms.hpp:103
Header file containing small mathematical help-functions that are needed e.g. for the Sussman redista...