Debian Codegraph @

dune/localfunctions/test/test-lagrange.cc

raw
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5
6 #include <dune/geometry/type.hh>
7
8 #include <dune/localfunctions/utility/field.hh>
9 #include <dune/localfunctions/utility/basisprint.hh>
10
11 #include <dune/localfunctions/lagrange/equidistantpoints.hh>
12 #include <dune/localfunctions/lagrange/lagrangebasis.hh>
13
14 /**
15 * \file
16 * \brief Performs some tests for the generic Lagrange
17 * shape functions on simplices.
18 *
19 * The topology can be chosen at compile time by setting TOPOLOGY
20 * to a Dune::GeometryType like
21 * \code
22 * GeometryTypes::simplex(2)
23 * \endcode
24 * which generates a 2d simplex. If TOPOLOGY is not set, all
25 * topologies up to 4d are tested. Note, this may lead to prolonged
26 * compiler runs.
27 *
28 * For debugging purpuse the functions and the derivatives can be
29 * printed. You have to define the macro TEST_OUTPUT_FUNCTIONS to
30 * activate this function.
31 */
32
33 #if HAVE_GMP
34 typedef Dune::GMPField< 128 > StorageField;
35 typedef Dune::GMPField< 512 > ComputeField;
36 #else
37 typedef double StorageField;
38 typedef double ComputeField;
39 #endif
40
41 template <class Basis,class Points>
42 bool test(const Basis &basis, const Points &points, bool verbose)
43 {
44 bool ret = true;
45 std::vector< Dune::FieldVector< double, 1 > > y( basis.size() );
46 for( unsigned int index = 0; index < points.size(); ++index )
47 {
48 if (verbose)
49 std::cout << index << " " << points[ index ].point() << " "
50 << points[ index ].localKey()
51 << std::endl;
52 basis.evaluate( points[ index ].point(), y );
53 bool first = true;
54 for( unsigned int i = 0; i < y.size(); ++i )
55 {
56 if( fabs( y[ i ] - double( i == index ) ) > 1e-10 )
57 {
58 if (first) {
59 std::cout << "ERROR: "
60 << index << " -> "
61 << "x = " << points[ index ].point()
62 << " (codim = " << points[ index ].localKey().codim() << ", "
63 << "subentity = " << points[ index ].localKey().subEntity() << ", "
64 << "index = " << points[ index ].localKey().index() << "):" << std::endl;
65 first = false;
66 }
67 if (1)
68 std::cout << " y[ " << i << " ] = " << y[ i ] << " "
69 << " error : " << fabs( y[ i ] - double( i == index ) )
70 << std::endl;
71 ret = false;
72 }
73 }
74 }
75 return ret;
76 }
77
78 template <Dune::GeometryType::Id geometryId>
79 bool test(unsigned int order, bool verbose = false)
80 {
81 constexpr Dune::GeometryType geometry = geometryId;
82 typedef Dune::LagrangeBasisFactory<Dune::EquidistantPointSet,geometry.dim(),double,double,StorageField,ComputeField> BasisFactory;
83 typedef Dune::LagrangeCoefficientsFactory< Dune::EquidistantPointSet,geometry.dim(),double > LagrangeCoefficientsFactory;
84
85 bool ret = true;
86
87 for (unsigned int o = 0; o <= order; ++o)
88 {
89 const typename LagrangeCoefficientsFactory::Object *pointsPtr = LagrangeCoefficientsFactory::template create< geometry >( o );
90
91 if ( pointsPtr == 0)
92 continue;
93
94 std::cout << "Testing " << geometry << " with order " << o << std::endl;
95
96 typename BasisFactory::Object &basis = *BasisFactory::template create<geometry>(o);
97
98 ret |= test(basis,*pointsPtr,verbose);
99
100 // define the macro TEST_OUTPUT_FUNCTIONS to output files containing functions and
101 // derivatives in a human readabible form (aka LaTeX source)
102 #ifdef TEST_OUTPUT_FUNCTIONS
103 std::stringstream name;
104 name << "lagrange_" << geometry << "_p" << o << ".basis";
105 std::ofstream out(name.str().c_str());
106 Dune::basisPrint<0,BasisFactory,typename BasisFactory::StorageField,geometry>(out,basis);
107 Dune::basisPrint<1,BasisFactory,typename BasisFactory::StorageField,geometry>(out,basis);
108 #endif // TEST_OUTPUT_FUNCTIONS
109
110 LagrangeCoefficientsFactory::release( pointsPtr );
111 BasisFactory::release( &basis );
112 }
113
114 if (verbose)
115 std::cout << std::endl << std::endl << std::endl;
116 if (!ret) {
117 std::cout << " FAILED !" << std::endl;
118 }
119 return ret;
120 }
121
122 #ifdef CHECKDIM
123 #if CHECKDIM==1
124 #define CHECKDIM1
125 #elif CHECKDIM==2
126 #define CHECKDIM2
127 #elif CHECKDIM==3
128 #define CHECKDIM3
129 #elif CHECKDIM==4
130 #define CHECKDIM4
131 #endif
132 #else
133 #define CHECKDIM1
134 #define CHECKDIM2
135 #define CHECKDIM3
136 #define CHECKDIM4
137 #endif
138
139
140
141 int main ( int argc, char **argv )
142 {
143 using namespace Dune;
144 using namespace Impl;
145
146 unsigned int order = (argc < 2) ? 5 : atoi(argv[1]);
147
148 if (argc < 2)
149 {
150 std::cerr << "Usage: " << argv[ 0 ] << " <p>" << std::endl
151 << "Using default order of " << order << std::endl;
152 }
153 #ifdef TOPOLOGY
154 return (test<TOPOLOGY>(order) ? 0 : 1 );
155 #else
156 bool tests = true;
157
158 #ifdef CHECKDIM1
159 tests &= test<GeometryTypes::cube(1)> (order);
160 tests &= test<GeometryTypes::simplex(1)> (order);
161 #endif
162
163 #ifdef CHECKDIM2
164 tests &= test<GeometryTypes::cube(2)> (order);
165 tests &= test<GeometryTypes::simplex(2)> (order);
166 #endif
167
168 #ifdef CHECKDIM3
169 tests &= test<GeometryTypes::cube(3)> (order);
170 tests &= test<GeometryTypes::prism> (order);
171 tests &= test<GeometryTypes::pyramid> (order);
172 tests &= test<GeometryTypes::simplex(3)> (order);
173 #endif
174
175 // reduce tested order to 4 in 4d unless explicitly asked for more
176 if (argc < 2)
177 order = 4;
178
179 #ifdef CHECKDIM4
180 tests &= test<GeometryTypes::cube(4)> (order);
181 tests &= test<GeometryTypes::simplex(4)> (order);
182 #endif
183
184 return (tests ? 0 : 1);
185 #endif // TOPOLOGY
186 }