File: test_04.hpp

package info (click to toggle)
trilinos 12.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 825,604 kB
  • sloc: cpp: 3,352,065; ansic: 432,926; fortran: 169,495; xml: 61,903; python: 40,836; sh: 32,697; makefile: 26,612; javascript: 8,535; perl: 7,136; f90: 6,372; csh: 4,183; objc: 2,620; lex: 1,469; lisp: 810; yacc: 497; awk: 364; ml: 281; php: 145
file content (284 lines) | stat: -rw-r--r-- 14,586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// @HEADER
// ************************************************************************
//
//                           Intrepid2 Package
//                 Copyright (2007) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Kyungjoo Kim  (kyukim@sandia.gov), or
//                    Mauro Perego  (mperego@sandia.gov)
//
// ************************************************************************
// @HEADER


/** \file
    \brief  Test of the CellTools class.
    \author Created by P. Bochev, D. Ridzal, K. Peterson and Kyungjoo Kim
*/

#include "Intrepid2_config.h"

#ifdef HAVE_INTREPID2_DEBUG
#define INTREPID2_TEST_FOR_DEBUG_ABORT_OVERRIDE_TO_CONTINUE
#endif

#include "Intrepid2_DefaultCubatureFactory.hpp"

#include "Intrepid2_CellTools.hpp"

#include "Teuchos_oblackholestream.hpp"
#include "Teuchos_RCP.hpp"
#include "Teuchos_ScalarTraits.hpp"


namespace Intrepid2 {

  namespace Test {
#define INTREPID2_TEST_ERROR_EXPECTED( S )                              \
    try {                                                               \
      S ;                                                               \
    }                                                                   \
    catch (std::logic_error err) {                                      \
      *outStream << "Expected Error ----------------------------------------------------------------\n"; \
      *outStream << err.what() << '\n';                                 \
      *outStream << "-------------------------------------------------------------------------------" << "\n\n"; \
    };
#define ConstructWithLabel(obj, ...) obj(#obj, __VA_ARGS__)
    
    template<typename ValueType, typename DeviceSpaceType>
    int CellTools_Test04(const bool verbose) {

      Teuchos::RCP<std::ostream> outStream;
      Teuchos::oblackholestream bhs; // outputs nothing

      if (verbose)
        outStream = Teuchos::rcp(&std::cout, false);
      else
        outStream = Teuchos::rcp(&bhs,       false);

      Teuchos::oblackholestream oldFormatState;
      oldFormatState.copyfmt(std::cout);

      typedef typename
        Kokkos::Impl::is_space<DeviceSpaceType>::host_mirror_space::execution_space HostSpaceType ;

      *outStream << "DeviceSpace::  "; DeviceSpaceType::print_configuration(*outStream, false);
      *outStream << "HostSpace::    ";   HostSpaceType::print_configuration(*outStream, false);
  
      *outStream                                                       
        << "===============================================================================\n"
        << "|                                                                             |\n"
        << "|                              Unit Test CellTools                            |\n"
        << "|                                                                             |\n"
        << "|     1) Mapping to and from reference cells with base and extended topologies|\n"
        << "|        using default initial guesses when computing the inverse F^{-1}      |\n"
        << "|     2) Repeat all tests from 1) using user-defined initial guess for F^{-1} |\n"
        << "|     3) Exception testing                                                    |\n"
        << "|                                                                             |\n"
        << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov)                      |\n"
        << "|                      Denis Ridzal (dridzal@sandia.gov), or                  |\n"
        << "|                      Kara Peterson(kjpeter@sandia.gov), or                  |\n"
        << "|                      Kyungjoo Kim (kyukim@sandia.gov)                       |\n"
        << "|                                                                             |\n"
        << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n"
        << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n"
        << "|                                                                             |\n"
        << "===============================================================================\n";

      typedef CellTools<DeviceSpaceType> ct;
      typedef RealSpaceTools<DeviceSpaceType> rst;
      typedef Kokkos::DynRankView<ValueType,DeviceSpaceType> DynRankView;

      const ValueType tol = tolerence()*100.0;

      int errorFlag  = 0;

      // Collect all supported cell topologies
      std::vector<shards::CellTopology> standardBaseTopologies;
      shards::getTopologies(standardBaseTopologies, 4, shards::STANDARD_CELL, shards::BASE_TOPOLOGY);
      const auto topoSize = standardBaseTopologies.size();

      for (auto testCase=0;testCase<2;++testCase) {
        try {
          switch (testCase) {
          case 0: {
            *outStream
              << "\n"
              << "===============================================================================\n" 
              << "| Test 1: computing F(x) and F^{-1}(x) using default initial guesses.         |\n" 
              << "===============================================================================\n\n";
            break;
          }
          case 1: {
            *outStream
              << "\n"
              << "===============================================================================\n" 
              << "| Test 2: computing F(x) and F^{-1}(x) using user-defined initial guess.      |\n" 
              << "===============================================================================\n\n";
            break;
          }
          } 

          /*
           *  Test summary:
           *
           *    A reference point set is mapped to physical frame and then back to reference frame.
           *    Test passes if the final set of points matches the first set of points. The cell workset
           *    is generated by perturbing randomly the worksetCell of a reference cell with the specified 
           *    cell topology. 
           *
           */
          DefaultCubatureFactory cubFactory;   
          
          // Initialize testing env.
          const auto numCells = 10;
          const auto testAccuracy = 4;

          DynRankView ConstructWithLabel(cubPointsMax,  Parameters::MaxIntegrationPoints, Parameters::MaxDimension);
          DynRankView ConstructWithLabel(cubWeightsMax, Parameters::MaxIntegrationPoints);
          // for arbitrary high order elements, this should be Parameters::MaxIntegrationPoints 
          const auto maxNumNodes = 64; 
          DynRankView ConstructWithLabel(refCellNodesMax,          maxNumNodes, Parameters::MaxDimension);
          DynRankView ConstructWithLabel(worksetCellMax, numCells, maxNumNodes, Parameters::MaxDimension);

          DynRankView ConstructWithLabel(physPointsMax,    numCells, Parameters::MaxIntegrationPoints, Parameters::MaxDimension);
          DynRankView ConstructWithLabel(controlPointsMax, numCells, Parameters::MaxIntegrationPoints, Parameters::MaxDimension);

          DynRankView ConstructWithLabel(initGuessMax, numCells, Parameters::MaxIntegrationPoints, Parameters::MaxDimension);

          // Loop over cell topologies, make cell workset for each one by perturbing the worksetCell & test methods
          for (size_type topoOrd=0;topoOrd<topoSize;++topoOrd) {
            const auto cell = standardBaseTopologies[topoOrd];
            
            if (!ct::hasReferenceCell(cell))
              continue;
      
            // 1.   Define a single reference point set using cubature factory with order 4 cubature
            const auto cellCubature = cubFactory.create<DeviceSpaceType>(cell, testAccuracy); 
            const auto cubDim = cellCubature->getDimension();
            const auto cubNumPoints = cellCubature->getNumPoints();
            
            typedef Kokkos::pair<ordinal_type,ordinal_type> range_type;
            range_type cubPointRange(0, cubNumPoints), cubDimRange(0, cubDim);            
            auto cubPoints  = Kokkos::subdynrankview( cubPointsMax,  cubPointRange, cubDimRange );
            auto cubWeights = Kokkos::subdynrankview( cubWeightsMax, cubPointRange );

            cellCubature->getCubature(cubPoints, cubWeights);

            // 2.   Define a cell workset by perturbing the worksetCell of the reference cell with the specified topology
            // 2.1  Resize dimensions of the rank-3 (C,N,D) cell workset array for the current topology
            const auto numNodes = cell.getNodeCount();
            const auto cellDim  = cell.getDimension();
            
            range_type nodeRange(0, numNodes), cellDimRange(0, cellDim);            
            auto worksetCell = Kokkos::subdynrankview(worksetCellMax, Kokkos::ALL(), nodeRange, cellDimRange);
            
            // 2.2  Copy worksetCell of the reference cell with the same topology to temp rank-2 (N,D) array
            auto refCellNodes = Kokkos::subdynrankview(refCellNodesMax, nodeRange, cellDimRange);
            ct::getReferenceSubcellNodes(refCellNodes, cellDim, 0, cell);
            
            // 2.3  Create randomly perturbed version of the reference cell and save in the cell workset array
            for (auto cellOrd=0;cellOrd<numCells;++cellOrd) {
              // Move vertices +/-0.125 along their axes. Gives nondegenerate cells for base and extended topologies 
              for (size_type nodeOrd=0;nodeOrd<numNodes;++nodeOrd) 
                for(size_type d=0;d<cellDim;++d) {
                  const auto delta = Teuchos::ScalarTraits<double>::random()/16.0;
                  worksetCell(cellOrd, nodeOrd, d) = refCellNodes(nodeOrd, d) + delta;
                } 
            }
            
            /* 
             * 3.1 Test 1: single point set to single physical cell: map ref. point set in rank-2 (P,D) array
             *      to a physical point set in rank-2 (P,D) array for a specified cell ordinal. Use the cub.
             *      points array for this test. Resize physPoints and controlPoints to rank-2 (P,D) arrays.
             */
            auto physPoints    = Kokkos::subdynrankview( physPointsMax,    Kokkos::ALL(), cubPointRange, cubDimRange );
            auto controlPoints = Kokkos::subdynrankview( controlPointsMax, Kokkos::ALL(), cubPointRange, cubDimRange );
            
            *outStream 
              << " Mapping a set of " << cubNumPoints << " points to one cell in a workset of " << numCells << " " 
              << cell.getName() << " cells. \n";

            // Forward map:: requires cell ordinal; input (P,D) -> output (C,P,D)
            ct::mapToPhysicalFrame(physPoints, cubPoints, worksetCell, cell);

            // Inverse map: requires cell ordinal; input (C,P,D) -> output (C,P,D)
            switch (testCase) {
            case 0: {
              ct::mapToReferenceFrame(controlPoints, physPoints, worksetCell, cell);
              break;
            }
            case 1: {
              auto initGuess = Kokkos::subdynrankview( initGuessMax, Kokkos::ALL(), cubPointRange, cubDimRange );
              // cubPoints become the initial guess
              rst::clone(initGuess, cubPoints);
              ct::mapToReferenceFrameInitGuess(controlPoints, initGuess, physPoints, worksetCell, cell);
              break;
            }
            }
            
            // Points in controlPoints should match the originals in cubPoints up to a tolerance
            for (auto cellOrd=0;cellOrd<numCells;++cellOrd) 
              for (auto pt=0;pt<cubNumPoints;++pt) 
                for (size_type d=0;d<cellDim;++d) 
                  if( std::abs( controlPoints(cellOrd, pt, d) - cubPoints(pt, d) ) > tol ) {
                    errorFlag++;
                    *outStream
                      << std::setw(70) << "^^^^----FAILURE!" << "\n"
                      << " Mapping a single point set to a single physical cell in a workset failed for: \n"
                      << "                    Cell Topology = " << cell.getName() << "\n"
                      << " Physical cell ordinal in workset = " << cellOrd << "\n"
                      << "          Reference point ordinal = " << std::setprecision(12) << pt << "\n"
                      << "    At reference point coordinate = " << std::setprecision(12) << d << "\n"
                      << "                   Original value = " << cubPoints(pt, d) << "\n"
                      << "                     F^{-1}F(P_d) = " << controlPoints(cellOrd, pt, d) <<"\n";
                  }
          }
        } catch (std::logic_error err) {
          *outStream << err.what() << "\n";
          errorFlag = -1000;
        }
      }

      if (errorFlag != 0)
        std::cout << "End Result: TEST FAILED\n";
      else
        std::cout << "End Result: TEST PASSED\n";

      // reset format state of std::cout
      std::cout.copyfmt(oldFormatState);

      return errorFlag;
    }
  } // end test
} // end intrepid2