Intrepid
test_02.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 "Intrepid_CellTools.hpp"
54 #include "Intrepid_Utils.hpp"
55 #include "Teuchos_oblackholestream.hpp"
56 #include "Teuchos_RCP.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 
59 using namespace std;
60 using namespace Intrepid;
61 
62 int main(int argc, char *argv[]) {
63 
64  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
65 
66  // This little trick lets us print to std::cout only if
67  // a (dummy) command-line argument is provided.
68  int iprint = argc - 1;
69  Teuchos::RCP<std::ostream> outStream;
70  Teuchos::oblackholestream bhs; // outputs nothing
71  if (iprint > 0)
72  outStream = Teuchos::rcp(&std::cout, false);
73  else
74  outStream = Teuchos::rcp(&bhs, false);
75 
76  // Save the format state of the original std::cout.
77  Teuchos::oblackholestream oldFormatState;
78  oldFormatState.copyfmt(std::cout);
79 
80  *outStream \
81  << "===============================================================================\n" \
82  << "| |\n" \
83  << "| Unit Test (FunctionSpaceTools) |\n" \
84  << "| |\n" \
85  << "| 1) basic operator transformations and integration in HCURL |\n" \
86  << "| |\n" \
87  << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov) or |\n" \
88  << "| Denis Ridzal (dridzal@sandia.gov). |\n" \
89  << "| |\n" \
90  << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \
91  << "| Trilinos website: http://trilinos.sandia.gov |\n" \
92  << "| |\n" \
93  << "===============================================================================\n";
94 
95 
96  int errorFlag = 0;
97 
98  typedef FunctionSpaceTools fst;
99 
100  *outStream \
101  << "\n"
102  << "===============================================================================\n"\
103  << "| TEST 1: correctness of math operations |\n"\
104  << "===============================================================================\n";
105 
106  outStream->precision(20);
107 
108  try {
109  shards::CellTopology cellType = shards::getCellTopologyData< shards::Hexahedron<> >(); // cell type: hex
110 
111  /* Related to cubature. */
112  DefaultCubatureFactory<double> cubFactory; // create cubature factory
113  int cubDegree = 20; // cubature degree
114  Teuchos::RCP<Cubature<double> > myCub = cubFactory.create(cellType, cubDegree); // create default cubature
115  int spaceDim = myCub->getDimension(); // get spatial dimension
116  int numCubPoints = myCub->getNumPoints(); // get number of cubature points
117 
118  /* Related to basis. */
119  Basis_HCURL_HEX_I1_FEM<double, FieldContainer<double> > hexBasis; // create H-curl basis on a hex
120  int numFields = hexBasis.getCardinality(); // get basis cardinality
121 
122  /* Cell geometries and orientations. */
123  int numCells = 4;
124  int numNodes = 8;
125  int numCellData = numCells*numNodes*spaceDim;
126  int numSignData = numCells*numFields;
127 
128  double hexnodes[] = {
129  // hex 0 -- affine
130  -1.0, -1.0, -1.0,
131  1.0, -1.0, -1.0,
132  1.0, 1.0, -1.0,
133  -1.0, 1.0, -1.0,
134  -1.0, -1.0, 1.0,
135  1.0, -1.0, 1.0,
136  1.0, 1.0, 1.0,
137  -1.0, 1.0, 1.0,
138  // hex 1 -- affine
139  -3.0, -3.0, 1.0,
140  6.0, 3.0, 1.0,
141  7.0, 8.0, 0.0,
142  -2.0, 2.0, 0.0,
143  -3.0, -3.0, 4.0,
144  6.0, 3.0, 4.0,
145  7.0, 8.0, 3.0,
146  -2.0, 2.0, 3.0,
147  // hex 2 -- affine
148  -3.0, -3.0, 0.0,
149  9.0, 3.0, 0.0,
150  15.0, 6.1, 0.0,
151  3.0, 0.1, 0.0,
152  9.0, 3.0, 0.1,
153  21.0, 9.0, 0.1,
154  27.0, 12.1, 0.1,
155  15.0, 6.1, 0.1,
156  // hex 3 -- nonaffine
157  -2.0, -2.0, 0.0,
158  2.0, -1.0, 0.0,
159  1.0, 6.0, 0.0,
160  -1.0, 1.0, 0.0,
161  0.0, 0.0, 1.0,
162  1.0, 0.0, 1.0,
163  1.0, 1.0, 1.0,
164  0.0, 1.0, 1.0
165  };
166 
167  short edgesigns[] = {
168  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
169  1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1,
170  -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1,
171  1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1
172  };
173 
174  /* Computational arrays. */
175  FieldContainer<double> cub_points(numCubPoints, spaceDim);
176  FieldContainer<double> cub_weights(numCubPoints);
177  FieldContainer<double> cell_nodes(numCells, numNodes, spaceDim);
178  FieldContainer<short> field_signs(numCells, numFields);
179  FieldContainer<double> jacobian(numCells, numCubPoints, spaceDim, spaceDim);
180  FieldContainer<double> jacobian_inv(numCells, numCubPoints, spaceDim, spaceDim);
181  FieldContainer<double> jacobian_det(numCells, numCubPoints);
182  FieldContainer<double> weighted_measure(numCells, numCubPoints);
183 
184  FieldContainer<double> curl_of_basis_at_cub_points(numFields, numCubPoints, spaceDim);
185  FieldContainer<double> transformed_curl_of_basis_at_cub_points(numCells, numFields, numCubPoints, spaceDim);
186  FieldContainer<double> weighted_transformed_curl_of_basis_at_cub_points(numCells, numFields, numCubPoints, spaceDim);
187  FieldContainer<double> stiffness_matrices(numCells, numFields, numFields);
188 
189  FieldContainer<double> value_of_basis_at_cub_points(numFields, numCubPoints, spaceDim);
190  FieldContainer<double> transformed_value_of_basis_at_cub_points(numCells, numFields, numCubPoints, spaceDim);
191  FieldContainer<double> weighted_transformed_value_of_basis_at_cub_points(numCells, numFields, numCubPoints, spaceDim);
192  FieldContainer<double> mass_matrices(numCells, numFields, numFields);
193 
194  /******************* START COMPUTATION ***********************/
195 
196  // get cubature points and weights
197  myCub->getCubature(cub_points, cub_weights);
198 
199  // fill cell vertex array
200  cell_nodes.setValues(hexnodes, numCellData);
201 
202  // set basis function signs, for each cell
203  field_signs.setValues(edgesigns, numSignData);
204 
205  // compute geometric cell information
206  CellTools<double>::setJacobian(jacobian, cub_points, cell_nodes, cellType);
207  CellTools<double>::setJacobianInv(jacobian_inv, jacobian);
208  CellTools<double>::setJacobianDet(jacobian_det, jacobian);
209 
210  // compute weighted measure
211  fst::computeCellMeasure<double>(weighted_measure, jacobian_det, cub_weights);
212 
213  // Computing stiffness matrices:
214  // tabulate curls of basis functions at (reference) cubature points
215  hexBasis.getValues(curl_of_basis_at_cub_points, cub_points, OPERATOR_CURL);
216 
217  // transform curls of basis functions
218  fst::HCURLtransformCURL<double>(transformed_curl_of_basis_at_cub_points,
219  jacobian,
220  jacobian_det,
221  curl_of_basis_at_cub_points);
222 
223  // multiply with weighted measure
224  fst::multiplyMeasure<double>(weighted_transformed_curl_of_basis_at_cub_points,
225  weighted_measure,
226  transformed_curl_of_basis_at_cub_points);
227 
228  // we can apply the field signs to the basis function arrays, or after the fact, see below
229  fst::applyFieldSigns<double>(transformed_curl_of_basis_at_cub_points, field_signs);
230  fst::applyFieldSigns<double>(weighted_transformed_curl_of_basis_at_cub_points, field_signs);
231 
232  // compute stiffness matrices
233  fst::integrate<double>(stiffness_matrices,
234  transformed_curl_of_basis_at_cub_points,
235  weighted_transformed_curl_of_basis_at_cub_points,
236  COMP_CPP);
237 
238  // Computing mass matrices:
239  // tabulate values of basis functions at (reference) cubature points
240  hexBasis.getValues(value_of_basis_at_cub_points, cub_points, OPERATOR_VALUE);
241 
242  // transform values of basis functions
243  fst::HCURLtransformVALUE<double>(transformed_value_of_basis_at_cub_points,
244  jacobian_inv,
245  value_of_basis_at_cub_points);
246 
247  // multiply with weighted measure
248  fst::multiplyMeasure<double>(weighted_transformed_value_of_basis_at_cub_points,
249  weighted_measure,
250  transformed_value_of_basis_at_cub_points);
251 
252  // compute mass matrices
253  fst::integrate<double>(mass_matrices,
254  transformed_value_of_basis_at_cub_points,
255  weighted_transformed_value_of_basis_at_cub_points,
256  COMP_CPP);
257 
258  // apply field signs (after the fact, as a post-processing step)
259  fst::applyLeftFieldSigns<double>(mass_matrices, field_signs);
260  fst::applyRightFieldSigns<double>(mass_matrices, field_signs);
261 
262  /******************* STOP COMPUTATION ***********************/
263 
264 
265  /******************* START COMPARISON ***********************/
266  string basedir = "./testdata";
267  for (int cell_id = 0; cell_id < numCells-1; cell_id++) {
268 
269  stringstream namestream;
270  string filename;
271  namestream << basedir << "/mass_HCURL_HEX_I1_FEM" << "_" << "0" << cell_id+1 << ".dat";
272  namestream >> filename;
273 
274  ifstream massfile(&filename[0]);
275  if (massfile.is_open()) {
276  if (compareToAnalytic<double>(&mass_matrices(cell_id, 0, 0), massfile, 1e-10, iprint) > 0)
277  errorFlag++;
278  massfile.close();
279  }
280  else {
281  errorFlag = -1;
282  std::cout << "End Result: TEST FAILED\n";
283  return errorFlag;
284  }
285 
286  namestream.clear();
287  namestream << basedir << "/stiff_HCURL_HEX_I1_FEM" << "_" << "0" << cell_id+1 << ".dat";
288  namestream >> filename;
289 
290  ifstream stifffile(&filename[0]);
291  if (stifffile.is_open())
292  {
293  if (compareToAnalytic<double>(&stiffness_matrices(cell_id, 0, 0), stifffile, 1e-10, iprint) > 0)
294  errorFlag++;
295  stifffile.close();
296  }
297  else {
298  errorFlag = -1;
299  std::cout << "End Result: TEST FAILED\n";
300  return errorFlag;
301  }
302 
303  }
304  for (int cell_id = 3; cell_id < numCells; cell_id++) {
305 
306  stringstream namestream;
307  string filename;
308  namestream << basedir << "/mass_fp_HCURL_HEX_I1_FEM" << "_" << "0" << cell_id+1 << ".dat";
309  namestream >> filename;
310 
311  ifstream massfile(&filename[0]);
312  if (massfile.is_open()) {
313  if (compareToAnalytic<double>(&mass_matrices(cell_id, 0, 0), massfile, 1e-4, iprint, INTREPID_UTILS_SCALAR) > 0)
314  errorFlag++;
315  massfile.close();
316  }
317  else {
318  errorFlag = -1;
319  std::cout << "End Result: TEST FAILED\n";
320  return errorFlag;
321  }
322 
323  namestream.clear();
324  namestream << basedir << "/stiff_fp_HCURL_HEX_I1_FEM" << "_" << "0" << cell_id+1 << ".dat";
325  namestream >> filename;
326 
327  ifstream stifffile(&filename[0]);
328  if (stifffile.is_open())
329  {
330  if (compareToAnalytic<double>(&stiffness_matrices(cell_id, 0, 0), stifffile, 1e-4, iprint, INTREPID_UTILS_SCALAR) > 0)
331  errorFlag++;
332  stifffile.close();
333  }
334  else {
335  errorFlag = -1;
336  std::cout << "End Result: TEST FAILED\n";
337  return errorFlag;
338  }
339 
340  }
341  /******************* STOP COMPARISON ***********************/
342 
343  *outStream << "\n";
344  }
345  catch (std::logic_error err) {
346  *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
347  *outStream << err.what() << '\n';
348  *outStream << "-------------------------------------------------------------------------------" << "\n\n";
349  errorFlag = -1000;
350  };
351 
352 
353  if (errorFlag != 0)
354  std::cout << "End Result: TEST FAILED\n";
355  else
356  std::cout << "End Result: TEST PASSED\n";
357 
358  // reset format state of std::cout
359  std::cout.copyfmt(oldFormatState);
360 
361  return errorFlag;
362 }
int main(int argc, char *argv[])
outdated tests for orthogonal bases
Definition: test_02.cpp:63
virtual int getCardinality() const
Returns cardinality of the basis.
Header file for the Intrepid::CellTools class.
Header file for utility class to provide multidimensional containers.
Defines expert-level interfaces for the evaluation of functions and operators in physical space (supp...
void getValues(ArrayScalar &outputValues, const ArrayScalar &inputPoints, const EOperator operatorType) const
Evaluation of a FEM basis on a reference Hexahedron cell.
Intrepid utilities.
Header file for the abstract base class Intrepid::DefaultCubatureFactory.
Implementation of the default H(curl)-compatible FEM basis of degree 1 on Hexahedron cell...
Header file for the Intrepid::FunctionSpaceTools class.
A factory class that generates specific instances of cubatures.
Teuchos::RCP< Cubature< Scalar, ArrayPoint, ArrayWeight > > create(const shards::CellTopology &cellTopology, const std::vector< int > &degree)
Factory method.
A stateless class for operations on cell data. Provides methods for:
Header file for the Intrepid::HCURL_HEX_I1_FEM class.