Intrepid
test_01.cpp
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Pavel Bochev (pbboche@sandia.gov)
38 // Denis Ridzal (dridzal@sandia.gov), or
39 // Kara Peterson (kjpeter@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
50 #include "Teuchos_oblackholestream.hpp"
51 #include "Teuchos_RCP.hpp"
52 #include "Teuchos_GlobalMPISession.hpp"
53 
54 using namespace std;
55 using namespace Intrepid;
56 
57 #define INTREPID_TEST_COMMAND( S , throwCounter, nException ) \
58 { \
59  ++nException; \
60  try { \
61  S ; \
62  } \
63  catch (std::logic_error err) { \
64  ++throwCounter; \
65  *outStream << "Expected Error " << nException << " -------------------------------------------------------------\n"; \
66  *outStream << err.what() << '\n'; \
67  *outStream << "-------------------------------------------------------------------------------" << "\n\n"; \
68  }; \
69 }
70 
71 int main(int argc, char *argv[]) {
72 
73  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
74 
75  // This little trick lets us print to std::cout only if
76  // a (dummy) command-line argument is provided.
77  int iprint = argc - 1;
78  Teuchos::RCP<std::ostream> outStream;
79  Teuchos::oblackholestream bhs; // outputs nothing
80  if (iprint > 0)
81  outStream = Teuchos::rcp(&std::cout, false);
82  else
83  outStream = Teuchos::rcp(&bhs, false);
84 
85  // Save the format state of the original std::cout.
86  Teuchos::oblackholestream oldFormatState;
87  oldFormatState.copyfmt(std::cout);
88 
89  *outStream \
90  << "===============================================================================\n" \
91  << "| |\n" \
92  << "| Unit Test (Basis_HGRAD_TET_COMP12_FEM) |\n" \
93  << "| |\n" \
94  << "| 1) Evaluation of Basis Function Values |\n" \
95  << "| |\n" \
96  << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov), |\n" \
97  << "| Denis Ridzal (dridzal@sandia.gov), |\n" \
98  << "| Kara Peterson (kjpeter@sandia.gov), |\n" \
99  << "| Jake Ostien (jtostie@sandia.gov). |\n" \
100  << "| |\n" \
101  << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \
102  << "| Trilinos website: http://trilinos.sandia.gov |\n" \
103  << "| |\n" \
104  << "===============================================================================\n"\
105  << "| TEST 1: Basis creation, values |\n"\
106  << "===============================================================================\n";
107 
108  // Define basis and error flag
110  int errorFlag = 0;
111 
112  // Initialize throw counter for exception testing
113  //int nException = 0;
114  //int throwCounter = 0;
115 
116  // Define array containing the 10 vertices of the reference TET
117  FieldContainer<double> tetNodes(10, 3);
118  tetNodes(0,0) = 0.0; tetNodes(0,1) = 0.0; tetNodes(0,2) = 0.0;
119  tetNodes(1,0) = 1.0; tetNodes(1,1) = 0.0; tetNodes(1,2) = 0.0;
120  tetNodes(2,0) = 0.0; tetNodes(2,1) = 1.0; tetNodes(2,2) = 0.0;
121  tetNodes(3,0) = 0.0; tetNodes(3,1) = 0.0; tetNodes(3,2) = 1.0;
122  tetNodes(4,0) = 0.5; tetNodes(4,1) = 0.0; tetNodes(4,2) = 0.0;
123  tetNodes(5,0) = 0.5; tetNodes(5,1) = 0.5; tetNodes(5,2) = 0.0;
124  tetNodes(6,0) = 0.0; tetNodes(6,1) = 0.5; tetNodes(6,2) = 0.0;
125  tetNodes(7,0) = 0.0; tetNodes(7,1) = 0.0; tetNodes(7,2) = 0.5;
126  tetNodes(8,0) = 0.5; tetNodes(8,1) = 0.0; tetNodes(8,2) = 0.5;
127  tetNodes(9,0) = 0.0; tetNodes(9,1) = 0.5; tetNodes(9,2) = 0.5;
128  // Define array containing 5 integration points
129  FieldContainer<double> tetPoints(5, 3);
130  tetPoints(0,0) = 0.25; tetPoints(0,1) = 0.25; tetPoints(0,2) = 0.25;
131  tetPoints(1,0) = 0.5; tetPoints(1,1) = (1./6.); tetPoints(1,2) = (1./6.);
132  tetPoints(2,0) = (1./6.); tetPoints(2,1) = 0.5; tetPoints(2,2) = (1./6.);
133  tetPoints(3,0) = (1./6.); tetPoints(3,1) = (1./6.); tetPoints(3,2) = 0.5;
134  tetPoints(4,0) = (1./6.); tetPoints(4,1) = (1./6.); tetPoints(4,2) = (1./6.);
135 
136  // output precision
137  outStream -> precision(20);
138 
139  // VALUE: Each row gives the 10 correct basis set values at an evaluation point
140  double nodalBasisValues[] = {
141  // first 4 vertices
142  1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
143  0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
144  0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
145  0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
146  // second 6 vertices
147  0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
148  0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
149  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
150  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
151  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
152  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0
153  };
154  double pointBasisValues[] = {
155  // pt 0 {0.25, 0.25, 0.25}
156  0.0, 0.0, 0.0, 0.0, 1./6., 1./6., 1./6., 1./6., 1./6., 1./6.,
157  // pt 1 {0.5, 1/6, 1/6}
158  0.0, 0.0, 0.0, 0.0, 1./3., 1./3., 0.0, 0.0, 1./3., 0.0,
159  // pt 2 {1/6, 0.5, 0.1/6}
160  0.0, 0.0, 0.0, 0.0, 0.0, 1./3., 1./3., 0.0, 0.0, 1./3.,
161  // pt 3 {1/6, 1/6, 0.5}
162  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1./3., 1./3., 1./3.,
163  // pt 4 {1/6, 1/6, 1/6}
164  0.0, 0.0, 0.0, 0.0, 1./3., 0.0, 1./3., 1./3., 0.0, 0.0
165  };
166 
167  // GRAD and D1: each row gives the 3x10 correct values of the gradients of the 10 basis functions
168  double pointBasisGrads[] = {
169  // point 0
170  -0.25, -0.25, -0.25, 0.25, 0.00, 0.00, 0.00, 0.25, 0.0, 0.00, 0.0, 0.25, 0.0, -0.75, -0.75, \
171  0.75, 0.75, 0.00, -0.75, 0.00, -0.75, -0.75, -0.75, 0.0, 0.75, 0.0, 0.75, 0.0, 0.75, 0.75,
172 
173  // point 1
174  0.15, 0.15, 0.15, 1.45, 0.00, 0.00, 0.00, -0.15, 0.0, 0.00, 0.0, -0.15, -104./60., -2.15, -2.15, \
175  5./12., 2.15, 0.00, -17./60., 0.00, -0.15, -17./60., -0.15, 0.0, 5./12., 0.0, 2.15, -2./15., 0.15, 0.15,
176 
177  // point 2
178  0.15, 0.15, 0.15, -0.15, 0.0, 0.0, 0.0, 1.45, 0.0, 0.0, 0.0, -0.15, 0.0, -17./60., -0.15, \
179  2.15, 5./12., 0.0, -2.15, -104./60., -2.15, -0.15, -17./60., 0.0, 0.15, -2./15., 0.15, 0.0, 5./12., 2.15,
180 
181  // point 3
182  0.15, 0.15, 0.15, -0.15, 0.0, 0.0, 0.0, -0.15, 0.0, 0.0, 0.0, 1.45, 0.0, -0.15, -17./60., \
183  0.15, 0.15, -2./15., -0.15, 0.0, -17./60., -2.15, -2.15, -104./60., 2.15, 0.0, 5./12., 0.0, 2.15, 5./12.,
184 
185  // point 4
186  -1.45, -1.45, -1.45, -0.15, 0.0, 0.0, 0.0, -0.15, 0.0, 0.0, 0.0, -0.15, 104./60., -5./12., -5./12., \
187  17./60., 17./60., 2./15., -5./12., 104./60., -5./12., -5./12., -5./12., 104./60., 17./60., 2./15., 17./60., 2./15., 17./60., 17./60.
188  };
189 
190  try{
191 
192  // Dimensions for the output arrays:
193  int numFields = tetBasis.getCardinality();
194  int numNodes = tetNodes.dimension(0);
195  int spaceDim = tetBasis.getBaseCellTopology().getDimension();
196 
197  // Generic array for values, grads, curls, etc. that will be properly sized before each call
199 
200  // Check VALUE of basis functions at nodes: resize vals to rank-2 container:\n";
201  vals.resize(numFields, numNodes);
202  tetBasis.getValues(vals, tetNodes, OPERATOR_VALUE);
203 
204  for (int i = 0; i < numFields; i++) {
205  for (int j = 0; j < numNodes; j++) {
206  int l = i + j * numFields;
207  if (std::abs(vals(i,j) - nodalBasisValues[l]) > INTREPID_TOL) {
208  errorFlag++;
209  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
210 
211  // Output the multi-index of the value where the error is:
212  *outStream << " At multi-index { ";
213  *outStream << i << " ";*outStream << j << " ";
214  *outStream << "} computed value: " << vals(i,j)
215  << " but reference value: " << nodalBasisValues[l] << "\n";
216  }
217  }
218  }
219 
220  // Check VALUE of basis functions at points: resize vals to rank-2 container:\n";
221  int numPoints = tetPoints.dimension(0);
222  vals.resize(numFields, numPoints);
223  vals.initialize(0.0);
224  tetBasis.getValues(vals, tetPoints, OPERATOR_VALUE);
225 
226  for (int i = 0; i < numFields; i++) {
227  for (int j = 0; j < numPoints; j++) {
228  int l = i + j * numFields;
229  if (std::abs(vals(i,j) - pointBasisValues[l]) > INTREPID_TOL) {
230  errorFlag++;
231  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
232 
233  // Output the multi-index of the value where the error is:
234  *outStream << " At multi-index { ";
235  *outStream << i << " ";*outStream << j << " ";
236  *outStream << "} computed value: " << vals(i,j)
237  << " but reference value: " << pointBasisValues[l] << "\n";
238  }
239  }
240  }
241 
242  // Check GRAD of basis functions at points: resize vals to rank-3 container:\n";
243  numPoints = tetPoints.dimension(0);
244  vals.resize(numFields, numPoints, spaceDim);
245  vals.initialize(0.0);
246  tetBasis.getValues(vals, tetPoints, OPERATOR_GRAD);
247 
248  for (int i = 0; i < numFields; i++) {
249  for (int j = 0; j < numPoints; j++) {
250  for (int k = 0; k < spaceDim; k++) {
251  int l = k + i * spaceDim + j * spaceDim * numFields;
252  if (std::abs(vals(i,j,k) - pointBasisGrads[l]) > INTREPID_TOL) {
253  errorFlag++;
254  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
255 
256  // Output the multi-index of the value where the error is:
257  *outStream << " At multi-index { ";
258  *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
259  *outStream << "} computed grad component: " << vals(i,j,k)
260  << " but reference grad component: " << pointBasisGrads[l] << "\n";
261  }
262  }
263  }
264  }
265  }
266  // Catch unexpected errors
267  catch (std::logic_error err) {
268  *outStream << err.what() << "\n\n";
269  errorFlag = -1000;
270  };
271 
272 
273  if (errorFlag != 0)
274  std::cout << "End Result: TEST FAILED\n";
275  else
276  std::cout << "End Result: TEST PASSED\n";
277 
278  // reset format state of std::cout
279  std::cout.copyfmt(oldFormatState);
280 
281  return errorFlag;
282 }
int main(int argc, char *argv[])
Performs a code-code comparison to FIAT for Nedelec bases on tets (values and curls) ...
Definition: test_01.cpp:65
virtual int getCardinality() const
Returns cardinality of the basis.
int dimension(const int whichDim) const
Returns the specified dimension.
Header file for utility class to provide multidimensional containers.
void resize(const int dim0)
Resizes FieldContainer to a rank-1 container with the specified dimension, initialized by 0...
void getValues(ArrayScalar &outputValues, const ArrayScalar &inputPoints, const EOperator operatorType) const
FEM basis evaluation on a reference Tetrahedron cell.
void initialize(const Scalar value=0)
Initializes a field container by assigning value to all its elements.
Header file for the Intrepid::HGRAD_TET_COMP12_FEM class.
virtual const shards::CellTopology getBaseCellTopology() const
Returns the base cell topology for which the basis is defined. See Shards documentation http://trilin...