Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_simple_interpolator.cpp

raw
1 #include "stk_middle_mesh/conservative_transfer_adaptor.hpp"
2 #include "stk_middle_mesh/simple_interpolator.hpp"
3 #include "stk_middle_mesh/stencil_interpolator.hpp"
4 #include "gtest/gtest.h"
5
6 namespace stk {
7 namespace middle_mesh {
8 namespace impl {
9
10 namespace {
11
12 class SimpleInterpolatorTester : public ::testing::Test
13 {
14 protected:
15 template <typename T>
16 void initialize(double xmin, double xmax, int numelSrc, int numelDest, int basisDegreeSrc, int basisDegreeDest,
17 int quadDegree, T func)
18 {
19 disc1d::impl::MeshSpec1D specSrc{numelSrc, xmin, xmax}, specDest{numelDest, xmin, xmax};
20
21 mDiscSrc = disc1d::impl::make_disc1_d(specSrc, basisDegreeSrc, quadDegree);
22 mDiscDest = disc1d::impl::make_disc1_d(specDest, basisDegreeDest, quadDegree);
23
24 mInterpolator = std::make_shared<disc1d::impl::SimpleInterpolator>(mDiscSrc, mDiscDest);
25
26 mFSrc = disc1d::impl::make_grid_function(mDiscSrc, func);
27 mFDestEx = disc1d::impl::make_grid_function(mDiscDest, func);
28 }
29
30 template <typename T>
31 void initialize(double xmin, double xmax, int numelSrc, int numelDest, int basisDegree, int quadDegree, T func)
32 {
33 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, basisDegree, quadDegree, func);
34 }
35
36 disc1d::impl::Discretization1DPtr mDiscSrc;
37 disc1d::impl::Discretization1DPtr mDiscDest;
38 std::shared_ptr<disc1d::impl::SimpleInterpolator> mInterpolator;
39 std::vector<double> mFSrc;
40 std::vector<double> mFDestEx;
41 };
42
43 class StencilInterpolatorTester : public ::testing::Test
44 {
45 protected:
46 template <typename T>
47 void initialize(double xmin, double xmax, int numelSrc, int numelDest, int basisDegreeSrc, int basisDegreeDest,
48 int quadDegree, int stencilsize, T func)
49 {
50 disc1d::impl::MeshSpec1D specSrc{numelSrc, xmin, xmax}, specDest{numelDest, xmin, xmax};
51
52 mDiscSrc = disc1d::impl::make_disc1_d(specSrc, basisDegreeSrc, quadDegree);
53 mDiscDest = disc1d::impl::make_disc1_d(specDest, basisDegreeDest, quadDegree);
54
55 mInterpolator = std::make_shared<disc1d::impl::StencilInterpolator>(mDiscSrc, mDiscDest, stencilsize);
56
57 mFSrc = disc1d::impl::make_grid_function(mDiscSrc, func);
58 mFDestEx = disc1d::impl::make_grid_function(mDiscDest, func);
59 }
60
61 template <typename T>
62 void initialize(double xmin, double xmax, int numelSrc, int numelDest, int basisDegree, int quadDegree,
63 int stencilsize, T func)
64 {
65 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, basisDegree, quadDegree, stencilsize, func);
66 }
67
68 disc1d::impl::Discretization1DPtr mDiscSrc;
69 disc1d::impl::Discretization1DPtr mDiscDest;
70 std::shared_ptr<disc1d::impl::StencilInterpolator> mInterpolator;
71 std::vector<double> mFSrc;
72 std::vector<double> mFDestEx;
73 };
74
75 class ConvergenceRateCalculator
76 {
77 public:
78 void add_measurement(double h, double error)
79 {
80 m_sizes.push_back(h);
81 m_errors.push_back(error);
82 }
83
84 void output_data()
85 {
86 std::cout << "mesh h error ratio slope" << std::endl;
87 for (size_t i = 0; i < m_errors.size(); ++i)
88 {
89 // double ratio = i == 0 ? 0 : m_errors[i-1]/m_errors[i];
90 // double slope = i == 0 ? 0 : (std::log(m_errors[i]) - std::log(m_errors[i-1]))/(std::log(m_sizes[i]) -
91 // std::log(m_sizes[i-1]));
92 std::printf("%d %6.5e %6.5e %9.5f %6.5f\n", int(i), m_sizes[i], m_errors[i], get_ratio(i), get_slope(i));
93 }
94 }
95
96 double get_ratio(int mesh)
97 {
98 if (mesh == 0)
99 return 0;
100 else
101 return m_errors[mesh - 1] / m_errors[mesh];
102 }
103
104 double get_slope(int mesh)
105 {
106 if (mesh == 0)
107 return 0;
108 else
109 return (std::log(m_errors[mesh]) - std::log(m_errors[mesh - 1])) /
110 (std::log(m_sizes[mesh]) - std::log(m_sizes[mesh - 1]));
111 }
112
113 private:
114 std::vector<double> m_errors;
115 std::vector<double> m_sizes;
116 };
117
118 double compute_error_norm(disc1d::impl::Discretization1DPtr disc, const std::vector<double>& fEx,
119 const std::vector<double>& f)
120 {
121 std::vector<double> error(disc->get_num_dofs());
122 for (int i = 0; i < disc->get_num_dofs(); ++i)
123 error[i] = f[i] - fEx[i];
124
125 auto mError = apply_mass_matrix(disc, error);
126
127 double errorNorm = 0;
128 for (int i = 0; i < disc->get_num_dofs(); ++i)
129 errorNorm += error[i] * error[i];
130
131 errorNorm = std::sqrt(errorNorm);
132
133 return errorNorm;
134 }
135
136 } // namespace
137
138 TEST_F(SimpleInterpolatorTester, Polynomial)
139 {
140 double xmin = 0, xmax = 2;
141 int numelSrc = 5, numelDest = 11;
142 int basisDegree = 2, quadDegree = 3;
143
144 for (int polyDegree = 0; polyDegree <= 2; ++polyDegree)
145 {
146 auto f = [polyDegree](double x) { return std::pow(x, polyDegree); };
147 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, quadDegree, f);
148
149 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42);
150 mInterpolator->interpolate(mFSrc, fDest);
151
152 std::vector<double> fDestMatrix(mDiscDest->get_num_dofs(), 42);
153 disc1d::impl::apply_interpolation_from_matrix(*mInterpolator, mFSrc, fDestMatrix);
154
155 for (int i = 0; i < mDiscDest->get_num_dofs(); ++i)
156 {
157 EXPECT_NEAR(fDest[i], mFDestEx[i], 1e-13);
158 EXPECT_NEAR(fDestMatrix[i], mFDestEx[i], 1e-13);
159 }
160 }
161 }
162
163 TEST_F(StencilInterpolatorTester, Polynomial)
164 {
165 double xmin = 0, xmax = 2;
166 int numelSrc = 5, numelDest = 11;
167 int basisDegree = 1, quadDegree = 3;
168 int stencilsize = 4;
169
170 for (int polyDegree = 0; polyDegree <= 3; ++polyDegree)
171 {
172 auto f = [polyDegree](double x) { return std::pow(x, polyDegree); };
173 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, quadDegree, stencilsize, f);
174
175 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42);
176 mInterpolator->interpolate(mFSrc, fDest);
177
178 std::vector<double> fDestMatrix(mDiscDest->get_num_dofs(), 42);
179 disc1d::impl::apply_interpolation_from_matrix(*mInterpolator, mFSrc, fDestMatrix);
180
181 for (int i = 0; i < mDiscDest->get_num_dofs(); ++i)
182 {
183 EXPECT_NEAR(fDest[i], mFDestEx[i], 1e-13);
184 EXPECT_NEAR(fDestMatrix[i], mFDestEx[i], 1e-13);
185 }
186 }
187 }
188
189 TEST_F(SimpleInterpolatorTester, ConvergenceRate)
190 {
191 double xmin = 0, xmax = 2;
192 int numelSrc = 5, numelDest = 11;
193 int basisDegree = 2, quadDegree = 3;
194 int nmeshes = 6;
195
196 ConvergenceRateCalculator errorSlope, conservationSlope;
197 for (int mesh = 0; mesh < nmeshes; ++mesh)
198 {
199 // std::cout << "mesh " << mesh << std::endl;
200 double mean = (xmax + xmin) / 2;
201 double sigma = (xmax - xmin) / 4;
202 double pi = std::atan(1) * 4;
203
204 auto f = [&](double x) {
205 return x * std::exp(-(x - mean) * (x - mean) / (2 * sigma * sigma)) / (sigma * std::sqrt(2 * pi));
206 };
207 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, quadDegree, f);
208
209 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42), error(mDiscDest->get_num_dofs());
210 mInterpolator->interpolate(mFSrc, fDest);
211
212 double errorNorm = compute_error_norm(mDiscDest, mFDestEx, fDest);
213 double integralSrc = disc1d::impl::integrate_function(mDiscSrc, mFSrc);
214 double integralDest = disc1d::impl::integrate_function(mDiscDest, fDest);
215 double conservationError = std::abs(integralSrc - integralDest);
216
217 errorSlope.add_measurement(1.0 / numelSrc, errorNorm);
218 conservationSlope.add_measurement(1.0 / numelSrc, conservationError);
219
220 numelSrc *= 2;
221 numelDest *= 2;
222 }
223
224 EXPECT_NEAR(errorSlope.get_slope(nmeshes - 1), 2.5, 0.1);
225 EXPECT_NEAR(conservationSlope.get_slope(nmeshes - 1), 4.02, 0.1);
226
227 std::cout << "\nsolution error:" << std::endl;
228 errorSlope.output_data();
229
230 std::cout << "\nconservation error:" << std::endl;
231 conservationSlope.output_data();
232 }
233
234 TEST_F(StencilInterpolatorTester, ConvergenceRate)
235 {
236 double xmin = 0, xmax = 2;
237 int numelSrc = 5, numelDest = 11;
238 int basisDegree = 2, quadDegree = 3;
239 int stencilsize = 4;
240 int nmeshes = 6;
241
242 ConvergenceRateCalculator errorSlope, conservationSlope;
243 for (int mesh = 0; mesh < nmeshes; ++mesh)
244 {
245 // std::cout << "mesh " << mesh << std::endl;
246 double mean = (xmax + xmin) / 2;
247 double sigma = (xmax - xmin) / 4;
248 double pi = std::atan(1) * 4;
249
250 auto f = [&](double x) {
251 return x * std::exp(-(x - mean) * (x - mean) / (2 * sigma * sigma)) / (sigma * std::sqrt(2 * pi));
252 };
253 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, quadDegree, stencilsize, f);
254
255 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42), error(mDiscDest->get_num_dofs());
256 mInterpolator->interpolate(mFSrc, fDest);
257
258 double errorNorm = compute_error_norm(mDiscDest, mFDestEx, fDest);
259 double integralSrc = disc1d::impl::integrate_function(mDiscSrc, mFSrc);
260 double integralDest = disc1d::impl::integrate_function(mDiscDest, fDest);
261 double conservationError = std::abs(integralSrc - integralDest);
262
263 errorSlope.add_measurement(1.0 / numelSrc, errorNorm);
264 conservationSlope.add_measurement(1.0 / numelSrc, conservationError);
265
266 numelSrc *= 2;
267 numelDest *= 2;
268 }
269
270 EXPECT_NEAR(errorSlope.get_slope(nmeshes - 1), 4.5, 0.1);
271 EXPECT_NEAR(conservationSlope.get_slope(nmeshes - 1), 3.9, 0.1);
272
273 std::cout << "\nsolution error:" << std::endl;
274 errorSlope.output_data();
275
276 std::cout << "\nconservation error:" << std::endl;
277 conservationSlope.output_data();
278 }
279
280 TEST_F(StencilInterpolatorTester, ConservativeConvergenceRate)
281 {
282 double xmin = 0, xmax = 2;
283 int numelSrc = 5, numelDest = 11;
284 int basisDegreeSrc = 2, basisDegreeDest = 2;
285 int quadDegree = 5;
286 int stencilsize = 4;
287 int nmeshes = 6;
288
289 ConvergenceRateCalculator errorSlope, conservationSlope;
290 for (int mesh = 0; mesh < nmeshes; ++mesh)
291 {
292 // std::cout << "mesh " << mesh << std::endl;
293 double mean = (xmax + xmin) / 2;
294 double sigma = (xmax - xmin) / 4;
295 double pi = std::atan(1) * 4;
296
297 auto f = [&](double x) {
298 return x * std::exp(-(x - mean) * (x - mean) / (2 * sigma * sigma)) / (sigma * std::sqrt(2 * pi));
299 };
300 initialize(xmin, xmax, numelSrc, numelDest, basisDegreeSrc, basisDegreeDest, quadDegree, stencilsize, f);
301 disc1d::impl::ConservativeTransferAdaptor interpolator(mDiscSrc, mDiscDest, mInterpolator);
302
303 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42), error(mDiscDest->get_num_dofs());
304 interpolator.interpolate(mFSrc, fDest);
305
306 double errorNorm = compute_error_norm(mDiscDest, mFDestEx, fDest);
307 double integralSrc = disc1d::impl::integrate_function(mDiscSrc, mFSrc);
308 double integralDest = disc1d::impl::integrate_function(mDiscDest, fDest);
309 double conservationError = std::abs(integralSrc - integralDest);
310
311 errorSlope.add_measurement(1.0 / numelSrc, errorNorm);
312 conservationSlope.add_measurement(1.0 / numelSrc, conservationError);
313
314 EXPECT_NEAR(conservationError, 0, 1e-13);
315
316 numelSrc *= 2;
317 numelDest *= 2;
318 }
319
320 std::cout << "\nsolution error:" << std::endl;
321 errorSlope.output_data();
322
323 std::cout << "\nconservation error:" << std::endl;
324 conservationSlope.output_data();
325 }
326
327 TEST_F(SimpleInterpolatorTester, Conservation)
328 {
329 double xmin = 0, xmax = 2;
330 int numelSrc = 5, numelDest = 11;
331 int basisDegree = 1, quadDegree = 3;
332
333 double mean = (xmax + xmin) / 2;
334 double sigma = (xmax - xmin) / 4;
335 double pi = std::atan(1) * 4;
336
337 auto f = [&](double x) {
338 return std::exp(-(x - mean) * (x - mean) / (2 * sigma * sigma)) / (sigma * std::sqrt(2 * pi));
339 };
340 initialize(xmin, xmax, numelSrc, numelDest, basisDegree, quadDegree, f);
341
342 std::vector<double> fDest(mDiscDest->get_num_dofs(), 42);
343 mInterpolator->interpolate(mFSrc, fDest);
344
345 double integralSrc = disc1d::impl::integrate_function(mDiscSrc, mFSrc);
346 double integralDest = disc1d::impl::integrate_function(mDiscDest, fDest);
347 double integralDestEx = disc1d::impl::integrate_function(mDiscDest, mFDestEx);
348
349 std::cout << "integral_src = " << integralSrc << ", integral_dest = " << integralDest
350 << ", diff = " << std::abs(integralSrc - integralDest)
351 << ", relative error = " << std::abs(integralSrc - integralDest) / integralSrc << std::endl;
352 std::cout << "integral_dest_ex = " << integralDestEx << std::endl;
353 }
354
355 } // namespace impl
356 } // namespace middle_mesh
357 } // namespace stk