5#define BOOST_TEST_DYN_LINK
6#include <boost/test/unit_test.hpp>
10#include "Gaussian.hpp"
11#include "FiniteDifference/FD_simple.hpp"
14BOOST_AUTO_TEST_SUITE(FDOrder1TestSuite)
15 const size_t Field = 0;
16 const size_t AnalyticalGradient = 1;
17 const size_t NumericalGradient = 2;
18 const size_t Error = 3;
21 const double EPSILON = std::numeric_limits<double>::epsilon();
23 BOOST_AUTO_TEST_CASE(Forward_difference_1D_test)
25 double l2_norms [] = {0.573022, 0.288525, 0.171455};
26 const size_t grid_dim = 1;
28 const double box_lower = -1.0;
29 const double box_upper = 1.0;
35 double mu = 0.5 * (box_upper - abs(box_lower));
36 double sigma = 0.1 * (box_upper - box_lower);
39 for (
size_t N = 32; N <= 128; N *= 2, ++count)
41 const size_t sz[grid_dim] = {N};
42 grid_in_type g_dist(sz, box, ghost);
44 g_dist.setPropNames({
"Field",
"AnalyticalGradient",
"NumericalGradient",
"Error"});
46 auto gdom = g_dist.getDomainGhostIterator();
49 auto key = gdom.get();
52 g_dist.getProp<
Field>(key) = gaussian(p, mu, sigma);
56 auto dom = g_dist.getDomainIterator();
62 for (
int d = 0; d < grid_dim; d++)
65 g_dist.getProp<AnalyticalGradient>(key)[d] = hermite_polynomial(p.
get(d), sigma, 1) * g_dist.getProp<
Field>(key);
67 g_dist.getProp<NumericalGradient>(key)[d] = FD_forward<Field>(g_dist, key, d);
74 get_relative_error<NumericalGradient, AnalyticalGradient, Error>(g_dist);
78 BOOST_CHECK_MESSAGE(lNorms.l2 < l2_norms[count] + 0.00001 + EPSILON,
"Checking L2-norm forward FD");
80 std::cout << N <<
", " << lNorms.l2 <<
", " << lNorms.linf << std::endl;
84 BOOST_AUTO_TEST_CASE(Backward_difference_1D_test)
86 double l2_norms [] = {0.573022, 0.288525, 0.171455};
87 const size_t grid_dim = 1;
89 const double box_lower = -1.0;
90 const double box_upper = 1.0;
96 double mu = 0.5 * (box_upper - abs(box_lower));
97 double sigma = 0.1 * (box_upper - box_lower);
100 for (
size_t N = 32; N <= 128; N *= 2, ++count)
102 const size_t sz[grid_dim] = {N};
103 grid_in_type g_dist(sz, box, ghost);
105 g_dist.setPropNames({
"Field",
"AnalyticalGradient",
"NumericalGradient",
"Error"});
107 auto gdom = g_dist.getDomainGhostIterator();
108 while (gdom.isNext())
110 auto key = gdom.get();
113 g_dist.getProp<
Field>(key) = gaussian(p, mu, sigma);
117 auto dom = g_dist.getDomainIterator();
120 auto key = dom.get();
123 for (
int d = 0; d < grid_dim; d++)
126 g_dist.getProp<AnalyticalGradient>(key)[d] = hermite_polynomial(p.
get(d), sigma, 1) * g_dist.getProp<
Field>(key);
128 g_dist.getProp<NumericalGradient>(key)[d] = FD_backward<Field>(g_dist, key, d);
135 get_relative_error<NumericalGradient, AnalyticalGradient, Error>(g_dist);
139 BOOST_CHECK_MESSAGE(lNorms.l2 < l2_norms[count] + 0.00001 + EPSILON,
"Checking L2-norm backward FD");
141 std::cout << N <<
", " << lNorms.l2 <<
", " << lNorms.linf << std::endl;
145 BOOST_AUTO_TEST_CASE(Central_difference_1D_test)
147 double l2_norms [] = {0.182302, 0.0405274, 0.00968203};
148 const size_t grid_dim = 1;
150 const double box_lower = -1.0;
151 const double box_upper = 1.0;
157 double mu = 0.5 * (box_upper - abs(box_lower));
158 double sigma = 0.1 * (box_upper - box_lower);
161 for (
size_t N = 32; N <= 128; N *= 2, ++count)
163 const size_t sz[grid_dim] = {N};
164 grid_in_type g_dist(sz, box, ghost);
166 g_dist.setPropNames({
"Field",
"AnalyticalGradient",
"NumericalGradient",
"Error"});
168 auto gdom = g_dist.getDomainGhostIterator();
169 while (gdom.isNext())
171 auto key = gdom.get();
174 g_dist.getProp<
Field>(key) = gaussian(p, mu, sigma);
178 auto dom = g_dist.getDomainIterator();
181 auto key = dom.get();
184 for (
int d = 0; d < grid_dim; d++)
187 g_dist.getProp<AnalyticalGradient>(key)[d] = hermite_polynomial(p.
get(d), sigma, 1) * g_dist.getProp<
Field>(key);
189 g_dist.getProp<NumericalGradient>(key)[d] = FD_central<Field>(g_dist, key, d);
196 get_relative_error<NumericalGradient, AnalyticalGradient, Error>(g_dist);
200 BOOST_CHECK_MESSAGE(lNorms.l2 < l2_norms[count] + 0.00001 + EPSILON,
"Checking L2-norm central FD");
202 std::cout << N <<
", " << lNorms.l2 <<
", " << lNorms.linf << std::endl;
206BOOST_AUTO_TEST_SUITE_END()
Header file containing small mathematical help-functions that are needed e.g. for the Sussman redista...
Header file containing functions for computing the error and the L_2 / L_infinity norm.
This class represent an N-dimensional box.
Class for computing the l2/l_infinity norm for distributed grids and vectors based on given errors.
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.
This class implement the point shape in an N-dimensional space.
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
This is a distributed grid.
Out-of-bound policy kill the program.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...