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 
51 #include "Teuchos_oblackholestream.hpp"
52 #include "Teuchos_RCP.hpp"
53 #include "Teuchos_GlobalMPISession.hpp"
54 
55 using namespace std;
56 using namespace Intrepid;
57 
58 #define INTREPID_TEST_COMMAND( S , throwCounter, nException ) \
59 { \
60  ++nException; \
61  try { \
62  S ; \
63  } \
64  catch (std::logic_error err) { \
65  ++throwCounter; \
66  *outStream << "Expected Error " << nException << " -------------------------------------------------------------\n"; \
67  *outStream << err.what() << '\n'; \
68  *outStream << "-------------------------------------------------------------------------------" << "\n\n"; \
69  }; \
70 }
71 
72 int main(int argc, char *argv[]) {
73 
74  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
75 
76  // This little trick lets us print to std::cout only if
77  // a (dummy) command-line argument is provided.
78  int iprint = argc - 1;
79  Teuchos::RCP<std::ostream> outStream;
80  Teuchos::oblackholestream bhs; // outputs nothing
81  if (iprint > 0)
82  outStream = Teuchos::rcp(&std::cout, false);
83  else
84  outStream = Teuchos::rcp(&bhs, false);
85 
86  // Save the format state of the original std::cout.
87  Teuchos::oblackholestream oldFormatState;
88  oldFormatState.copyfmt(std::cout);
89 
90  *outStream \
91  << "===============================================================================\n" \
92  << "| |\n" \
93  << "| Unit Test (Basis_HGRAD_LINE_C1_FEM) |\n" \
94  << "| |\n" \
95  << "| 1) Conversion of Dof tags into Dof ordinals and back |\n" \
96  << "| 2) Basis values for VALUE, GRAD, CURL, DIV, and Dk operators |\n" \
97  << "| |\n" \
98  << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov), |\n" \
99  << "| Denis Ridzal (dridzal@sandia.gov), |\n" \
100  << "| Kara Peterson (dridzal@sandia.gov). |\n" \
101  << "| |\n" \
102  << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \
103  << "| Trilinos website: http://trilinos.sandia.gov |\n" \
104  << "| |\n" \
105  << "===============================================================================\n"\
106  << "| TEST 1: Basis creation, exception testing |\n"\
107  << "===============================================================================\n";
108 
109 
110  // Define basis and error flag
112  int errorFlag = 0;
113 
114  // Initialize throw counter for exception testing
115  int nException = 0;
116  int throwCounter = 0;
117 
118  // Define array containing the 2 vertices of the reference Line, its center and another point
119  FieldContainer<double> lineNodes(4, 1);
120  lineNodes(0,0) = -1.0;
121  lineNodes(1,0) = 1.0;
122  lineNodes(2,0) = 0.0;
123  lineNodes(3,0) = 0.5;
124 
125  // Generic array for the output values; needs to be properly resized depending on the operator type
127 
128  try{
129  // resize vals to rank-2 container with dimensions (num. points, num. basis functions)
130  vals.resize(lineBasis.getCardinality(), lineNodes.dimension(0) );
131 
132  // Exceptions 1-5: all bf tags/bf Ids below are wrong and should cause getDofOrdinal() and
133  // getDofTag() to access invalid array elements thereby causing bounds check exception
134  // exception #1
135  INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(2,0,0), throwCounter, nException );
136  // exception #2
137  INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(1,1,1), throwCounter, nException );
138  // exception #3
139  INTREPID_TEST_COMMAND( lineBasis.getDofOrdinal(0,4,0), throwCounter, nException );
140  // exception #4
141  INTREPID_TEST_COMMAND( lineBasis.getDofTag(5), throwCounter, nException );
142  // exception #5
143  INTREPID_TEST_COMMAND( lineBasis.getDofTag(-1), throwCounter, nException );
144 
145 #ifdef HAVE_INTREPID_DEBUG
146  // Exceptions 6-17 test exception handling with incorrectly dimensioned input/output arrays
147  // exception #6: input points array must be of rank-2
148  FieldContainer<double> badPoints1(4, 5, 3);
149  INTREPID_TEST_COMMAND( lineBasis.getValues(vals, badPoints1, OPERATOR_VALUE), throwCounter, nException );
150 
151  // exception #7 dimension 1 in the input point array must equal space dimension of the cell
152  FieldContainer<double> badPoints2(4, 2);
153  INTREPID_TEST_COMMAND( lineBasis.getValues(vals, badPoints2, OPERATOR_VALUE), throwCounter, nException );
154 
155  // exception #8 output values must be of rank-2 for OPERATOR_VALUE
156  FieldContainer<double> badVals1(4, 3, 1);
157  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals1, lineNodes, OPERATOR_VALUE), throwCounter, nException );
158 
159  // exception #9 output values must be of rank-3 for OPERATOR_GRAD
160  FieldContainer<double> badVals2(4, 3);
161  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_GRAD), throwCounter, nException );
162 
163  // exception #10 output values must be of rank-3 for OPERATOR_DIV
164  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_DIV), throwCounter, nException );
165 
166  // exception #11 output values must be of rank-3 for OPERATOR_CURL
167  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_CURL), throwCounter, nException );
168 
169  // exception #12 output values must be of rank-3 for OPERATOR_D2
170  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals2, lineNodes, OPERATOR_D2), throwCounter, nException );
171 
172  // exception #13 incorrect 1st dimension of output array (must equal number of basis functions)
173  FieldContainer<double> badVals3(lineBasis.getCardinality() + 1, lineNodes.dimension(0));
174  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals3, lineNodes, OPERATOR_VALUE), throwCounter, nException );
175 
176  // exception #14 incorrect 0th dimension of output array (must equal number of points)
177  FieldContainer<double> badVals4(lineBasis.getCardinality(), lineNodes.dimension(0) + 1);
178  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals4, lineNodes, OPERATOR_VALUE), throwCounter, nException );
179 
180  // exception #15: incorrect 2nd dimension of output array (must equal the space dimension)
181  FieldContainer<double> badVals5(lineBasis.getCardinality(), lineNodes.dimension(0), 2);
182  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals5, lineNodes, OPERATOR_GRAD), throwCounter, nException );
183 
184  // exception #16: incorrect 2nd dimension of output array (must equal D2 cardinality in 1D)
185  FieldContainer<double> badVals6(lineBasis.getCardinality(), lineNodes.dimension(0), 40);
186  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals6, lineNodes, OPERATOR_D2), throwCounter, nException );
187 
188  // exception #17: incorrect 2nd dimension of output array (must equal D3 cardinality in 1D)
189  INTREPID_TEST_COMMAND( lineBasis.getValues(badVals6, lineNodes, OPERATOR_D3), throwCounter, nException );
190 #endif
191 
192  }
193  catch (std::logic_error err) {
194  *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
195  *outStream << err.what() << '\n';
196  *outStream << "-------------------------------------------------------------------------------" << "\n\n";
197  errorFlag = -1000;
198  };
199 
200  // Check if number of thrown exceptions matches the one we expect
201  if (throwCounter != nException) {
202  errorFlag++;
203  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
204  }
205 
206  *outStream \
207  << "\n"
208  << "===============================================================================\n"\
209  << "| TEST 2: correctness of tag to enum and enum to tag lookups |\n"\
210  << "===============================================================================\n";
211 
212  try{
213  std::vector<std::vector<int> > allTags = lineBasis.getAllDofTags();
214 
215  // Loop over all tags, lookup the associated dof enumeration and then lookup the tag again
216  for (unsigned i = 0; i < allTags.size(); i++) {
217  int bfOrd = lineBasis.getDofOrdinal(allTags[i][0], allTags[i][1], allTags[i][2]);
218 
219  std::vector<int> myTag = lineBasis.getDofTag(bfOrd);
220  if( !( (myTag[0] == allTags[i][0]) &&
221  (myTag[1] == allTags[i][1]) &&
222  (myTag[2] == allTags[i][2]) &&
223  (myTag[3] == allTags[i][3]) ) ) {
224  errorFlag++;
225  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
226  *outStream << " getDofOrdinal( {"
227  << allTags[i][0] << ", "
228  << allTags[i][1] << ", "
229  << allTags[i][2] << ", "
230  << allTags[i][3] << "}) = " << bfOrd <<" but \n";
231  *outStream << " getDofTag(" << bfOrd << ") = { "
232  << myTag[0] << ", "
233  << myTag[1] << ", "
234  << myTag[2] << ", "
235  << myTag[3] << "}\n";
236  }
237  }
238 
239  // Now do the same but loop over basis functions
240  for( int bfOrd = 0; bfOrd < lineBasis.getCardinality(); bfOrd++) {
241  std::vector<int> myTag = lineBasis.getDofTag(bfOrd);
242  int myBfOrd = lineBasis.getDofOrdinal(myTag[0], myTag[1], myTag[2]);
243  if( bfOrd != myBfOrd) {
244  errorFlag++;
245  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
246  *outStream << " getDofTag(" << bfOrd << ") = { "
247  << myTag[0] << ", "
248  << myTag[1] << ", "
249  << myTag[2] << ", "
250  << myTag[3] << "} but getDofOrdinal({"
251  << myTag[0] << ", "
252  << myTag[1] << ", "
253  << myTag[2] << ", "
254  << myTag[3] << "} ) = " << myBfOrd << "\n";
255  }
256  }
257  }
258  catch (std::logic_error err){
259  *outStream << err.what() << "\n\n";
260  errorFlag = -1000;
261  };
262 
263  *outStream \
264  << "\n"
265  << "===============================================================================\n"\
266  << "| TEST 3: correctness of basis function values |\n"\
267  << "===============================================================================\n";
268 
269  outStream -> precision(20);
270 
271  // VALUE: Each row gives the 2 correct basis set values at an evaluation point
272  double basisValues[] = {
273  1.0, 0.0,
274  0.0, 1.0,
275  0.5, 0.5,
276  0.25, 0.75
277  };
278 
279  // GRAD, DIV, CURL and D1: each row gives the 2 correct values of the gradients of the 2 basis functions
280  double basisDerivs[] = {
281  -0.5, 0.5,
282  -0.5, 0.5,
283  -0.5, 0.5,
284  -0.5, 0.5
285  };
286 
287  try{
288 
289  // Dimensions for the output arrays:
290  int numFields = lineBasis.getCardinality();
291  int numPoints = lineNodes.dimension(0);
292  int spaceDim = lineBasis.getBaseCellTopology().getDimension();
293 
294  // Generic array for values, grads, curls, etc. that will be properly sized before each call
296 
297  // Check VALUE of basis functions: resize vals to rank-2 container:
298  vals.resize(numFields, numPoints);
299  lineBasis.getValues(vals, lineNodes, OPERATOR_VALUE);
300  for (int i = 0; i < numFields; i++) {
301  for (int j = 0; j < numPoints; j++) {
302  int l = i + j * numFields;
303  if (std::abs(vals(i,j) - basisValues[l]) > INTREPID_TOL) {
304  errorFlag++;
305  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
306 
307  // Output the multi-index of the value where the error is:
308  *outStream << " At multi-index { ";
309  *outStream << i << " ";*outStream << j << " ";
310  *outStream << "} computed value: " << vals(i,j)
311  << " but reference value: " << basisValues[l] << "\n";
312  }
313  }
314  }
315 
316  // Check derivatives of basis function: resize vals to rank-3 container
317  vals.resize(numFields, numPoints, spaceDim);
318  lineBasis.getValues(vals, lineNodes, OPERATOR_GRAD);
319  for (int i = 0; i < numFields; i++) {
320  for (int j = 0; j < numPoints; j++) {
321  for (int k = 0; k < spaceDim; k++) {
322  int l = k + i * spaceDim + j * spaceDim * numFields;
323  if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
324  errorFlag++;
325  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
326 
327  // Output the multi-index of the value where the error is:
328  *outStream << " At multi-index { ";
329  *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
330  *outStream << "} computed grad component: " << vals(i,j,k)
331  << " but reference grad component: " << basisDerivs[l] << "\n";
332  }
333  }
334  }
335  }
336 
337  // Check D1 of basis function (do not resize vals because it has the correct size: D1 = GRAD)
338  lineBasis.getValues(vals, lineNodes, OPERATOR_D1);
339  for (int i = 0; i < numFields; i++) {
340  for (int j = 0; j < numPoints; j++) {
341  for (int k = 0; k < spaceDim; k++) {
342  int l = k + i * spaceDim + j * spaceDim * numFields;
343  if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
344  errorFlag++;
345  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
346 
347  // Output the multi-index of the value where the error is:
348  *outStream << " At multi-index { ";
349  *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
350  *outStream << "} computed D1 component: " << vals(i,j,k)
351  << " but reference D1 component: " << basisDerivs[l] << "\n";
352  }
353  }
354  }
355  }
356 
357 
358  // Check CURL of basis function: resize vals just for illustration!
359  vals.resize(numFields, numPoints, spaceDim);
360  lineBasis.getValues(vals, lineNodes, OPERATOR_CURL);
361  for (int i = 0; i < numFields; i++) {
362  for (int j = 0; j < numPoints; j++) {
363  for (int k = 0; k < spaceDim; k++) {
364  int l = k + i * spaceDim + j * spaceDim * numFields;
365  if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
366  errorFlag++;
367  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
368 
369  // Output the multi-index of the value where the error is:
370  *outStream << " At multi-index { ";
371  *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
372  *outStream << "} computed curl component: " << vals(i,j,k)
373  << " but reference curl component: " << basisDerivs[l] << "\n";
374  }
375  }
376  }
377  }
378 
379 
380  // Check DIV of basis function (do not resize vals because it has the correct size: DIV = CURL)
381  lineBasis.getValues(vals, lineNodes, OPERATOR_DIV);
382  for (int i = 0; i < numFields; i++) {
383  for (int j = 0; j < numPoints; j++) {
384  for (int k = 0; k < spaceDim; k++) {
385  int l = k + i * spaceDim + j * spaceDim * numFields;
386  if (std::abs(vals(i,j,k) - basisDerivs[l]) > INTREPID_TOL) {
387  errorFlag++;
388  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
389 
390  // Output the multi-index of the value where the error is:
391  *outStream << " At multi-index { ";
392  *outStream << i << " ";*outStream << j << " ";*outStream << k << " ";
393  *outStream << "} computed D1 component: " << vals(i,j,k)
394  << " but reference D1 component: " << basisDerivs[l] << "\n";
395  }
396  }
397  }
398  }
399 
400 
401  // Check all higher derivatives - must be zero.
402  for(EOperator op = OPERATOR_D2; op < OPERATOR_MAX; op++) {
403 
404  // The last dimension is the number of kth derivatives and needs to be resized for every Dk
405  int DkCardin = Intrepid::getDkCardinality(op, spaceDim);
406  vals.resize(numFields, numPoints, DkCardin);
407 
408  lineBasis.getValues(vals, lineNodes, op);
409  for (int i = 0; i < vals.size(); i++) {
410  if (std::abs(vals[i]) > INTREPID_TOL) {
411  errorFlag++;
412  *outStream << std::setw(70) << "^^^^----FAILURE!" << "\n";
413 
414  // Get the multi-index of the value where the error is and the operator order
415  std::vector<int> myIndex;
416  vals.getMultiIndex(myIndex,i);
417  int ord = Intrepid::getOperatorOrder(op);
418  *outStream << " At multi-index { ";
419  for(int j = 0; j < vals.rank(); j++) {
420  *outStream << myIndex[j] << " ";
421  }
422  *outStream << "} computed D"<< ord <<" component: " << vals[i]
423  << " but reference D" << ord << " component: 0 \n";
424  }
425  }
426  }
427  }
428 
429  // Catch unexpected errors
430  catch (std::logic_error err) {
431  *outStream << err.what() << "\n\n";
432  errorFlag = -1000;
433  };
434 
435  if (errorFlag != 0)
436  std::cout << "End Result: TEST FAILED\n";
437  else
438  std::cout << "End Result: TEST PASSED\n";
439 
440  // reset format state of std::cout
441  std::cout.copyfmt(oldFormatState);
442 
443  return errorFlag;
444 }
int size() const
Returns size of the FieldContainer defined as the product of its dimensions.
virtual const std::vector< int > & getDofTag(const int dofOrd)
DoF ordinal to DoF tag lookup.
virtual int getCardinality() const
Returns cardinality of the basis.
virtual const std::vector< std::vector< int > > & getAllDofTags()
Retrieves all DoF tags.
Header file for utility class to provide multidimensional containers.
Implementation of the default H(grad)-compatible FEM basis of degree 1 on Line cell.
void getMultiIndex(int &i0, const int valueEnum) const
Returns the multi-index of a value, based on its enumeration, as a list, for rank-1 containers...
int rank() const
Return rank of the FieldContainer = number of indices used to tag the multi-indexed value...
void getValues(ArrayScalar &outputValues, const ArrayScalar &inputPoints, const EOperator operatorType) const
Evaluation of a FEM basis on a reference Line cell.
void resize(const int dim0)
Resizes FieldContainer to a rank-1 container with the specified dimension, initialized by 0...
virtual int getDofOrdinal(const int subcDim, const int subcOrd, const int subcDofOrd)
DoF tag to ordinal lookup.
virtual const shards::CellTopology getBaseCellTopology() const
Returns the base cell topology for which the basis is defined. See Shards documentation http://trilin...
Header file for the Intrepid::HGRAD_LINE_C1_FEM class.