View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.analysis.integration;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.exception.NullArgumentException;
28  import org.hipparchus.util.FastMath;
29  import org.junit.jupiter.api.Test;
30  
31  public class LebedevQuadratureTest {
32  
33      @Test
34      void verifyAllOrders() {
35          for (int order : LebedevQuadrature.AVAILABLE_ORDERS) {
36              LebedevQuadrature.Rule rule = LebedevQuadrature.getRule(order);
37              assertEquals(order, rule.getOrder(), "order mismatch for " + order);
38              double wsum = 0;
39              double maxNormErr = 0;
40              for (int i = 0; i < order; i++) {
41                  double x = rule.getX()[i];
42                  double y = rule.getY()[i];
43                  double z = rule.getZ()[i];
44                  double norm = FastMath.sqrt(x * x + y * y + z * z);
45                  maxNormErr = FastMath.max(maxNormErr, FastMath.abs(norm - 1.0));
46                  wsum += rule.getW()[i];
47              }
48              assertEquals(1.0, wsum, 1e-9, "weight sum mismatch for order " + order);
49              assertTrue(maxNormErr < 1e-9, "points not on unit sphere for order " + order);
50          }
51      }
52  
53      @Test
54      void testUnsupportedQuadratureOrder() {
55          // GIVEN
56          int unsupportedOrder = 5811;
57  
58          // WHEN
59          MathIllegalArgumentException e = assertThrows(
60                  MathIllegalArgumentException.class,
61                  () -> LebedevQuadrature.getRule(unsupportedOrder));
62  
63          // THEN
64          assertEquals(
65                  LocalizedCoreFormats.UNSUPPORTED_QUADRATURE_ORDER,
66                  e.getSpecifier());
67  
68          Object[] parts = e.getParts();
69          assertEquals(unsupportedOrder, parts[0]);
70          assertEquals(Arrays.toString(LebedevQuadrature.AVAILABLE_ORDERS), parts[1]);
71      }
72  
73      @Test
74      void testUnsupportedQuadratureSymmetryCode() {
75          // GIVEN
76          final int unsupportedCode = 7;
77  
78          // WHEN
79          MathIllegalArgumentException e = assertThrows(
80                  MathIllegalArgumentException.class,
81                  () -> LebedevQuadrature.genOh(unsupportedCode, 0, 0, 0, null, null, null, null, 0));
82  
83          // THEN
84          assertEquals(
85                  LocalizedCoreFormats.GEN_OH_ILLEGAL_CODE,
86                  e.getSpecifier());
87  
88          Object[] parts = e.getParts();
89          assertEquals(unsupportedCode, parts[0]);
90      }
91  
92      @Test
93      void testRuleAtLeast() {
94          assertEquals(6,    LebedevQuadrature.getRuleAtLeast(1).getOrder());
95          assertEquals(6,    LebedevQuadrature.getRuleAtLeast(6).getOrder());
96          assertEquals(14,   LebedevQuadrature.getRuleAtLeast(7).getOrder());
97          assertEquals(5810, LebedevQuadrature.getRuleAtLeast(5810).getOrder());
98          // beyond the largest rule, the largest one is returned
99          assertEquals(5810, LebedevQuadrature.getRuleAtLeast(10000).getOrder());
100     }
101 
102     @Test
103     void testIntegrateMonomials() {
104         // exact surface integrals over the unit sphere:
105         // ∫ 1 = 4π, ∫ x² = 4π/3, ∫ x²y²z² = 4π/105 (degree 6, exact for order ≥ 26)
106         // tolerance is a few ulps of the accumulated sum
107         assertEquals(4 * FastMath.PI,
108                      LebedevQuadrature.integrate((x, y, z) -> 1.0, 6), 1.0e-14);
109         assertEquals(4 * FastMath.PI / 3,
110                      LebedevQuadrature.integrate((x, y, z) -> x * x, 14), 1.0e-14);
111         assertEquals(4 * FastMath.PI / 105,
112                      LebedevQuadrature.integrate((x, y, z) -> x * x * y * y * z * z, 26), 1.0e-14);
113         // odd function integrates to zero
114         assertEquals(0.0,
115                      LebedevQuadrature.integrate((x, y, z) -> x * y * y, 110), 1.0e-14);
116     }
117 
118     @Test
119     void testIntegrateNullFunction() {
120         assertThrows(NullArgumentException.class, () -> LebedevQuadrature.integrate(null, 6));
121     }
122 
123 }