Intrepid2
Intrepid2_Utils_ExtDataDef.hpp
Go to the documentation of this file.
1// @HEADER
2// ************************************************************************
3//
4// Intrepid2 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 Kyungjoo Kim (kyukim@sandia.gov), or
38// Mauro Perego (mperego@sandia.gov)
39//
40// ************************************************************************
41// @HEADER
42
48#ifndef __INTREPID2_UTILS_EXTDATA_DEF_HPP__
49#define __INTREPID2_UTILS_EXTDATA_DEF_HPP__
50
51namespace Intrepid2 {
52
53 /***************************************************************************************************
54 * *
55 * Utility functions for handling external data in tests *
56 * *
57 ***************************************************************************************************/
58
59 template<typename ValueType,
60 class ...testMatProperties>
61 ordinal_type compareToAnalytic( std::ifstream &inputFile,
62 const Kokkos::DynRankView<ValueType,testMatProperties...> testMat,
63 const ValueType reltol,
64 const ordinal_type iprint,
65 const TypeOfExactData analyticDataType ) {
66 INTREPID2_TEST_FOR_EXCEPTION( testMat.rank() != 2, std::invalid_argument,
67 ">>> ERROR (compareToAnalytic): testMat must have rank 2");
68
69 Teuchos::RCP<std::ostream> outStream;
70 Teuchos::oblackholestream outNothing;
71 if (iprint > 0)
72 outStream = Teuchos::rcp(&std::cout, false);
73 else
74 outStream = Teuchos::rcp(&outNothing, false);
75
76 // Save the format state of the original std::cout.
77 Teuchos::oblackholestream oldFormatState;
78 oldFormatState.copyfmt(std::cout);
79
80 std::string line;
81 ValueType testentry;
82 ValueType abstol;
83 ValueType absdiff;
84 ordinal_type i = 0, j = 0;
85 ordinal_type err = 0;
86
87 while (! inputFile.eof() && i < static_cast<ordinal_type>(testMat.extent(0)) ) {
88 std::getline(inputFile,line);
89 std::istringstream linestream(line);
90 std::string chunk;
91 j = 0;
92 while( linestream >> chunk ) {
93 ordinal_type num1;
94 ordinal_type num2;
95 std::string::size_type loc = chunk.find( "/", 0);
96 if( loc != std::string::npos ) {
97 chunk.replace( loc, 1, " ");
98 std::istringstream chunkstream(chunk);
99 chunkstream >> num1;
100 chunkstream >> num2;
101 testentry = (ValueType)(num1)/(ValueType)(num2);
102 abstol = ( std::fabs(testentry) < reltol ? reltol : std::fabs(reltol*testentry) );
103 absdiff = std::fabs(testentry - testMat(i, j));
104 if (absdiff > abstol) {
105 ++err;
106 *outStream << "FAILURE --> ";
107 }
108 *outStream << "entry[" << i << "," << j << "]:" << " "
109 << testMat(i, j) << " " << num1 << "/" << num2 << " "
110 << absdiff << " " << "<?" << " " << abstol << "\n";
111 }
112 else {
113 std::istringstream chunkstream(chunk);
114 if (analyticDataType == INTREPID2_UTILS_FRACTION) {
115 chunkstream >> num1;
116 testentry = (ValueType)(num1);
117 }
118 else if (analyticDataType == INTREPID2_UTILS_SCALAR)
119 chunkstream >> testentry;
120 abstol = ( std::fabs(testentry) < reltol ?reltol : std::fabs(reltol*testentry) );
121 absdiff = std::fabs(testentry - testMat(i, j));
122 if (absdiff > abstol) {
123 ++err;
124 *outStream << "FAILURE --> ";
125 }
126 *outStream << "entry[" << i << "," << j << "]:" << " "
127 << testMat(i, j) << " " << testentry << " "
128 << absdiff << " " << "<?" << " " << abstol << "\n";
129 }
130 ++j;
131 }
132 ++i;
133 }
134
135 // reset format state of std::cout
136 std::cout.copyfmt(oldFormatState);
137
138 return err;
139 } // end compareToAnalytic
140
141 template<typename ValueType,
142 class ...testMatProperties>
143 void getAnalytic( Kokkos::DynRankView<ValueType,testMatProperties...> testMat,
144 std::ifstream &inputFile,
145 const TypeOfExactData analyticDataType ) {
146 INTREPID2_TEST_FOR_EXCEPTION( testMat.rank() != 2, std::invalid_argument,
147 ">>> ERROR (getToAnalytic): testMat must have rank 2");
148
149 Teuchos::oblackholestream oldFormatState;
150 oldFormatState.copyfmt(std::cout);
151
152 std::string line;
153 ValueType testentry;
154 ordinal_type i = 0, j = 0;
155
156 while (! inputFile.eof() && i < static_cast<ordinal_type>(testMat.extent(0)) ) {
157 std::getline (inputFile,line);
158 std::istringstream linestream(line);
159 std::string chunk;
160 j = 0;
161 while( linestream >> chunk ) {
162 ordinal_type num1;
163 ordinal_type num2;
164 std::string::size_type loc = chunk.find( "/", 0);
165 if( loc != std::string::npos ) {
166 chunk.replace( loc, 1, " ");
167 std::istringstream chunkstream(chunk);
168 chunkstream >> num1;
169 chunkstream >> num2;
170 testentry = (ValueType)(num1)/(ValueType)(num2);
171 testMat(i, j) = testentry;
172 }
173 else {
174 std::istringstream chunkstream(chunk);
175 if (analyticDataType == INTREPID2_UTILS_FRACTION) {
176 chunkstream >> num1;
177 testentry = (ValueType)(num1);
178 }
179 else if (analyticDataType == INTREPID2_UTILS_SCALAR)
180 chunkstream >> testentry;
181 testMat(i, j) = testentry;
182 }
183 ++j;
184 }
185 ++i;
186 }
187
188 // reset format state of std::cout
189 std::cout.copyfmt(oldFormatState);
190 } // end getAnalytic
191
192} // end namespace Intrepid2
193
194#endif
ordinal_type compareToAnalytic(std::ifstream &inputFile, const Kokkos::DynRankView< ValueType, testMatProperties... > testMat, const ValueType reltol, const ordinal_type iprint, const TypeOfExactData analyticDataType=INTREPID2_UTILS_FRACTION)
Compares the values in the test matrix testMat to precomputed analytic values stored in a file,...
void getAnalytic(Kokkos::DynRankView< ValueType, testMatProperties... > testMat, std::ifstream &inputFile, const TypeOfExactData analyticDataType=INTREPID2_UTILS_FRACTION)
Loads analytic values stored in a file into the matrix testMat, where the output matrix is an array o...
TypeOfExactData
Declarations of templated utility functions.