OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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"
27
28#include "level_set/redistancing_Sussman/HelpFunctions.hpp" // for printing to_string_with_precision
29
42template <size_t PropNumeric, size_t PropAnalytic, size_t Error, typename gridtype>
43void 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}
74template <size_t PropNumeric, size_t PropAnalytic, size_t Error, typename gridtype>
75void 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) =
93 abs((grid.template getProp<PropNumeric> (key) - grid.template getProp<PropAnalytic> (key)) /
94 (grid.template getProp<PropAnalytic> (key) + std::numeric_limits<analytic_type>::epsilon()));
95 ++dom;
96 }
97}
98
103template <typename lnorm_type=double>
105{
106public:
107 LNorms()
108 {
109 l2 = 0;
110 linf = 0;
111 }
112 // Member variables
113 lnorm_type l2; // L2 norm
114 lnorm_type linf; // L_infinity norm
115
116 // Member functions
123 template <size_t Error, typename gridtype>
124 void get_l_norms_grid(gridtype & grid)
125 {
126 auto dom = grid.getDomainIterator();
127 typedef typename std::remove_const_t<std::remove_reference_t<decltype(
128 grid.template get<Error>(dom.get()))>> error_type;
129
130 error_type maxError = 0;
131 error_type sumErrorSq = 0;
132
133 while(dom.isNext())
134 {
135 auto key = dom.get();
136 sumErrorSq += grid.template getProp<Error> (key) * grid.template getProp<Error> (key);
137 if (grid.template getProp<Error> (key) > maxError) maxError = grid.template getProp<Error> (key); // update maxError
138 ++dom;
139 }
140 auto &v_cl = create_vcluster();
141 v_cl.sum(sumErrorSq);
142 v_cl.max(maxError);
143 v_cl.execute();
144 l2 = (lnorm_type) sqrt( sumErrorSq / (error_type)grid.size());
145 linf = (lnorm_type) maxError;
146 }
147
154 template <size_t Error, typename vectortype>
155 void get_l_norms_vector(vectortype & vd)
156 {
157 auto dom = vd.getDomainIterator();
158 typedef typename std::remove_const_t<std::remove_reference_t<decltype(
159 vd.template getProp<Error>(dom.get()))>> error_type;
160
161 error_type maxError = 0;
162 error_type sumErrorSq = 0;
163 int count_particles = 0;
164 while(dom.isNext())
165 {
166 auto key = dom.get();
167 sumErrorSq += vd.template getProp<Error> (key) * vd.template getProp<Error> (key);
168 if (vd.template getProp<Error> (key) > maxError) maxError = vd.template getProp<Error> (key); // update maxError
169 ++dom;
170 ++count_particles;
171 }
172 auto &v_cl = create_vcluster();
173 v_cl.sum(sumErrorSq);
174 v_cl.sum(count_particles);
175 v_cl.max(maxError);
176 v_cl.execute();
177 l2 = (lnorm_type) sqrt( sumErrorSq / (error_type)count_particles);
178 linf = (lnorm_type) maxError;
179 }
180
188 template <size_t Error, typename grid_type>
190 {
191 auto dom = grid.getDomainIterator();
192 typedef typename std::remove_const_t<std::remove_reference_t<decltype(
193 grid.template getProp<Error>(dom.get()))>> error_type;
194
195 error_type maxError = 0;
196 error_type sumErrorSq = 0;
197 int count_points = 0;
198 while(dom.isNext())
199 {
200 auto key = dom.get();
201 sumErrorSq += grid.template getProp<Error> (key) * grid.template getProp<Error> (key);
202 if (grid.template getProp<Error> (key) > maxError) maxError = grid.template getProp<Error> (key); // update maxError
203 ++dom;
204 ++count_points;
205 }
206 auto &v_cl = create_vcluster();
207 v_cl.sum(sumErrorSq);
208 v_cl.sum(count_points);
209 v_cl.max(maxError);
210 v_cl.execute();
211 l2 = (lnorm_type) sqrt( sumErrorSq / (error_type)count_points);
212 linf = (lnorm_type) maxError;
213 }
214
215
225 template <typename T>
226 void write_to_file(const T col_0,
227 const int precision,
228 const std::string & filename,
229 const std::string & path_output)
230 {
231 auto &v_cl = create_vcluster();
232 if (v_cl.rank() == 0)
233 {
234 std::string path_output_lnorm = path_output + "/" + filename + ".csv";
235 create_file_if_not_exist(path_output_lnorm);
236
237 std::ofstream l_out;
238 l_out.open(path_output_lnorm, std::ios_base::app); // append instead of overwrite
239 l_out << std::to_string(col_0)
240 << ',' << to_string_with_precision(l2, precision)
241 << ',' << to_string_with_precision(linf, precision) << std::endl;
242 l_out.close();
243 }
244 }
245
246private:
247
248
249};
250
251
252
253
254
255
256
257
258
259#endif //ACCURACY_TESTS_LNORMS_HPP
260
Header file containing small mathematical help-functions that are needed e.g. for the Sussman redista...
std::string to_string_with_precision(const T myValue, const size_t n=6)
Converts value into string maintaining a desired precision.
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_relative_error(gridtype &grid)
At each grid node, the relative error is computed and stored in another property.
Definition LNorms.hpp:75
Header file containing functions for creating files and folders.
static void create_file_if_not_exist(std::string path)
Creates a file if not already existent.
Class for computing the l2/l_infinity norm for distributed grids and vectors based on given errors.
Definition LNorms.hpp:105
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:155
void get_l_norms(grid_type &grid)
Computes the L_2 and L_infinity norm from the error stored in a property on sparse grids,...
Definition LNorms.hpp:189
void write_to_file(const T col_0, 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:226
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:124
This is a distributed grid.