https://mooseframework.inl.gov
TestPolynomialQuadrature.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 #include "gtest/gtest.h"
10 #include <vector>
11 #include <cmath>
12 #include <iostream>
13 #include "PolynomialQuadrature.h"
14 #include "libmesh/utility.h"
15 
16 using namespace PolynomialQuadrature;
17 
19 {
20  Real tol = 1e-14;
21  Real lower_bound = 0.7;
22  Real upper_bound = 17.3;
23  Real x = 7.78254;
24  Real xref = 2 / (upper_bound - lower_bound) * (x - (upper_bound + lower_bound) / 2);
25 
26  Real poly_val = legendre(5, x, lower_bound, upper_bound);
27  Real val = 1 / 8.0 * (63.0 * Utility::pow<5>(xref) - 70.0 * xref * xref * xref + 15.0 * xref);
28  EXPECT_NEAR(poly_val, val, tol);
29 
30  std::vector<Real> xq_ref(5);
31  xq_ref[0] = -1 / 3.0 * std::sqrt(5.0 + 2.0 * std::sqrt(10.0 / 7.0));
32  xq_ref[1] = -1 / 3.0 * std::sqrt(5.0 - 2.0 * std::sqrt(10.0 / 7.0));
33  xq_ref[2] = 0.0;
34  xq_ref[3] = -xq_ref[0];
35  xq_ref[4] = -xq_ref[1];
36  for (unsigned int n = 0; n < 5; ++n)
37  xq_ref[n] = xq_ref[n] * (upper_bound - lower_bound) / 2.0 + (upper_bound + lower_bound) / 2.0;
38 
39  std::vector<Real> wq_ref(5);
40  wq_ref[0] = (322.0 - 13.0 * std::sqrt(70.0)) / 1800.0;
41  wq_ref[1] = (322.0 + 13.0 * std::sqrt(70.0)) / 1800.0;
42  wq_ref[2] = 64.0 / 225.0;
43  wq_ref[3] = wq_ref[0];
44  wq_ref[4] = wq_ref[1];
45 
46  std::vector<Real> xq;
47  std::vector<Real> wq;
48  gauss_legendre(4, xq, wq, lower_bound, upper_bound);
49  for (unsigned int n = 0; n < 5; ++n)
50  {
51  EXPECT_NEAR(xq[n], xq_ref[n], tol);
52  EXPECT_NEAR(wq[n], wq_ref[n], tol);
53  }
54 }
55 
57 {
58  Real tol = 1e-14;
59  Real mu = 4.7518;
60  Real sig = 0.9438;
61  Real x = 3.9587;
62  Real xref = (x - mu) / sig;
63 
64  Real poly_val = hermite(4, x, mu, sig);
65  Real val = Utility::pow<4>(xref) - 6.0 * xref * xref + 3.0;
66  EXPECT_NEAR(poly_val, val, tol);
67 
68  std::vector<Real> xq_ref(4);
69  xq_ref[0] = -std::sqrt(3.0 + std::sqrt(6.0));
70  xq_ref[1] = -std::sqrt(3.0 - std::sqrt(6.0));
71  xq_ref[2] = -xq_ref[1];
72  xq_ref[3] = -xq_ref[0];
73  for (unsigned int n = 0; n < 4; ++n)
74  xq_ref[n] = xq_ref[n] * sig + mu;
75 
76  std::vector<Real> wq_ref(4);
77  wq_ref[0] = 1.0 / (4.0 * (3.0 + std::sqrt(6.0)));
78  wq_ref[1] = 1.0 / (4.0 * (3.0 - std::sqrt(6.0)));
79  wq_ref[2] = wq_ref[1];
80  wq_ref[3] = wq_ref[0];
81 
82  std::vector<Real> xq;
83  std::vector<Real> wq;
84  gauss_hermite(3, xq, wq, mu, sig);
85  for (unsigned int n = 0; n < 4; ++n)
86  {
87  EXPECT_NEAR(xq[n], xq_ref[n], tol);
88  EXPECT_NEAR(wq[n], wq_ref[n], tol);
89  }
90 
91  {
92  std::vector<Real> xq;
93  std::vector<Real> wq;
94  clenshaw_curtis(10, xq, wq);
95 
96  EXPECT_EQ((std::size_t)11, xq.size());
97  EXPECT_EQ((std::size_t)11, wq.size());
98 
99  EXPECT_NEAR(xq[0], -1.0000000000000000e+00, tol);
100  EXPECT_NEAR(xq[1], -9.5105651629515353e-01, tol);
101  EXPECT_NEAR(xq[2], -8.0901699437494745e-01, tol);
102  EXPECT_NEAR(xq[3], -5.8778525229247314e-01, tol);
103  EXPECT_NEAR(xq[4], -3.0901699437494745e-01, tol);
104  EXPECT_NEAR(xq[5], +0.0000000000000000e+00, tol);
105  EXPECT_NEAR(xq[6], +3.0901699437494745e-01, tol);
106  EXPECT_NEAR(xq[7], +5.8778525229247314e-01, tol);
107  EXPECT_NEAR(xq[8], +8.0901699437494745e-01, tol);
108  EXPECT_NEAR(xq[9], +9.5105651629515353e-01, tol);
109  EXPECT_NEAR(xq[10], +1.0000000000000000e+00, tol);
110 
111  EXPECT_NEAR(wq[0], +5.0505050505050535e-03, tol);
112  EXPECT_NEAR(wq[1], +4.7289527441850783e-02, tol);
113  EXPECT_NEAR(wq[2], +9.2817607212123884e-02, tol);
114  EXPECT_NEAR(wq[3], +1.2679416664184331e-01, tol);
115  EXPECT_NEAR(wq[4], +1.4960663521211851e-01, tol);
116  EXPECT_NEAR(wq[5], +1.5688311688311693e-01, tol);
117  EXPECT_NEAR(wq[6], +1.4960663521211851e-01, tol);
118  EXPECT_NEAR(wq[7], +1.2679416664184331e-01, tol);
119  EXPECT_NEAR(wq[8], +9.2817607212123884e-02, tol);
120  EXPECT_NEAR(wq[9], +4.7289527441850783e-02, tol);
121  EXPECT_NEAR(wq[10], +5.0505050505050535e-03, tol);
122  }
123 }
124 
125 TEST(PolynomialQuadrature, dataStoreLoad_legendre)
126 {
127  using namespace PolynomialQuadrature;
128  const Real lower_bound = 0.7;
129  const Real upper_bound = 17.3;
130  std::unique_ptr<const Polynomial> ptr0 =
131  std::make_unique<const Legendre>(lower_bound, upper_bound);
132 
133  std::stringbuf buffer;
134  std::iostream stream(&buffer);
135  dataStore(stream, ptr0, nullptr);
136 
137  std::unique_ptr<const Polynomial> ptr1;
138  dataLoad(stream, ptr1, nullptr);
139 
140  EXPECT_EQ(ptr0->innerProduct(1), ptr1->innerProduct(1));
141 }
142 
143 TEST(PolynomialQuadrature, dataStoreLoad_hermite)
144 {
145  using namespace PolynomialQuadrature;
146  const Real mu = 4.7518;
147  const Real sig = 0.9438;
148  std::unique_ptr<const Polynomial> ptr0 = std::make_unique<const Hermite>(mu, sig);
149 
150  std::stringbuf buffer;
151  std::iostream stream(&buffer);
152  dataStore(stream, ptr0, nullptr);
153 
154  std::unique_ptr<const Polynomial> ptr1;
155  dataLoad(stream, ptr1, nullptr);
156 
157  EXPECT_EQ(ptr0->innerProduct(1), ptr1->innerProduct(1));
158 }
void gauss_hermite(const unsigned int order, std::vector< Real > &points, std::vector< Real > &weights, const Real mu, const Real sig)
Generalized formula for any polynomial order.
const double tol
void dataStore(std::ostream &stream, FaceCenteredMapFunctor< T, Map > &m, void *context)
const std::vector< double > x
static const std::string mu
Definition: NS.h:123
void clenshaw_curtis(const unsigned int order, std::vector< Real > &points, std::vector< Real > &weights)
Real hermite(const unsigned int order, const Real x, const Real mu=0.0, const Real sig=1.0)
Hermite polynomial of specified order.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Polynomials and quadratures based on defined distributions for Polynomial Chaos.
Real legendre(const unsigned int order, const Real x, const Real lower_bound=-1.0, const Real upper_bound=1.0)
Legendre polynomial of specified order.
void dataLoad(std::istream &stream, FaceCenteredMapFunctor< T, Map > &m, void *context)
TEST(PolynomialQuadrature, legendre)
void gauss_legendre(const unsigned int order, std::vector< Real > &points, std::vector< Real > &weights, const Real lower_bound, const Real upper_bound)
Generalized formula for any polynomial order.