File: test_06.hpp

package info (click to toggle)
trilinos 13.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 837,948 kB
  • sloc: cpp: 3,466,824; ansic: 433,701; fortran: 168,838; python: 102,712; xml: 66,353; sh: 38,380; makefile: 25,246; perl: 7,516; f90: 6,358; csh: 4,161; objc: 2,620; lex: 1,484; lisp: 810; javascript: 552; yacc: 515; awk: 364; ml: 281; php: 145
file content (458 lines) | stat: -rw-r--r-- 20,524 bytes parent folder | download | duplicates (2)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// @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 Kyungjoo Kim
*/

#include "Intrepid2_config.h"

#include "Intrepid2_CellTools_Serial.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"; \
    };                                                                  

    template<typename OutputViewType,
             typename inputViewType,
             typename worksetViewType>
    struct F_mapToPhysicalFrame {
      OutputViewType _output;
      inputViewType _input;
      worksetViewType _workset;

      KOKKOS_INLINE_FUNCTION
      F_mapToPhysicalFrame(OutputViewType output_,
                           inputViewType input_,
                           worksetViewType workset_) 
        : _output(output_), 
          _input(input_), 
          _workset(workset_) {}

      KOKKOS_INLINE_FUNCTION
      void
      operator()(const ordinal_type cl) const {
        const ordinal_type P = _output.extent(1);
        const ordinal_type N = _workset.extent(1);

        double buf[10]; Kokkos::View<double*,Kokkos::Impl::ActiveExecutionMemorySpace> val(&buf[0], N); // N
        auto nodes = Kokkos::subview(_workset, cl, Kokkos::ALL(), Kokkos::ALL()); // N,D

        for (ordinal_type i=0;i<P;++i) {
          auto in  = Kokkos::subview(_input,      i, Kokkos::ALL()); // D
          auto out = Kokkos::subview(_output, cl, i, Kokkos::ALL()); // D
          Impl::Basis_HGRAD_QUAD_C1_FEM::Serial<OPERATOR_VALUE>::getValues(val, in);
          Impl::CellTools::Serial::mapToPhysicalFrame(out, val, nodes);
        }
      }
    };

    template<typename OutputViewType,
             typename inputViewType,
             typename worksetViewType>      
    struct F_mapToReferenceFrame {
      OutputViewType _output;
      inputViewType _input;
      worksetViewType _workset;


      KOKKOS_INLINE_FUNCTION
      F_mapToReferenceFrame(OutputViewType output_,
                            inputViewType input_,
                            worksetViewType workset_) 
        : _output(output_), 
          _input(input_), 
          _workset(workset_) {}
      
      KOKKOS_INLINE_FUNCTION
      void
      operator()(const ordinal_type cl) const {
        const ordinal_type P = _output.extent(1);

        auto nodes = Kokkos::subview(_workset, cl, Kokkos::ALL(), Kokkos::ALL()); // N,D
        for (ordinal_type i=0;i<P;++i) {
          auto in  = Kokkos::subview(_input,  cl, i, Kokkos::ALL()); // D
          auto out = Kokkos::subview(_output, cl, i, Kokkos::ALL()); // D
          
          Impl::CellTools::Serial
            ::mapToReferenceFrame<Impl::Basis_HGRAD_QUAD_C1_FEM>(out, in, nodes);
        }
        
      }
    };
        
    template<typename ValueType, typename DeviceType>
    int CellTools_Test06(const bool verbose) {

      using ExecSpaceType = typename DeviceType::execution_space;

      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<DeviceType>::host_mirror_space::execution_space HostSpaceType ;

      typedef typename ExecSpaceType::array_layout DeviceArrayLayout;
        

      *outStream << "DeviceSpace::  ";   ExecSpaceType::print_configuration(*outStream, false);
      *outStream << "HostSpace::    ";   HostSpaceType::print_configuration(*outStream, false);
      
      *outStream
        << "===============================================================================\n"
        << "|                                                                             |\n"
        << "|                              Unit Test CellTools                            |\n"
        << "|                                                                             |\n"
        << "|     1) Serial interface tests                                               |\n"
        << "|                                                                             |\n"
        << "===============================================================================\n";
  
      const ValueType tol = tolerence()*100.0;

      int errorFlag = 0;
      
      try {
        
        {
          *outStream
            << "\n"
            << "===============================================================================\n"
            << "| Test 1: reference quad c1 element mapped into 2D physical space:            |\n"
            << "===============================================================================\n\n";

          const ordinal_type C = 3, P = 5, N = 4, D = 2;
          
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,HostSpaceType> pts_on_ref_host("pts_on_ref_host", P, D);
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,HostSpaceType> workset_host("workset_host", C, N, D);
           
          pts_on_ref_host(0, 0) =  0.0;           pts_on_ref_host(0, 1) =  0.0; 
          pts_on_ref_host(1, 0) =  0.3;           pts_on_ref_host(1, 1) =  0.2; 
          pts_on_ref_host(2, 0) =  0.2;           pts_on_ref_host(2, 1) =  0.4; 
          pts_on_ref_host(3, 0) = -0.1;           pts_on_ref_host(3, 1) =  0.1; 
          pts_on_ref_host(4, 0) = -0.2;           pts_on_ref_host(4, 1) =  0.3; 
          /*
          workset_host(0, 0, 0) = -1.0;           workset_host(0, 0, 1) = -1.0; 
          workset_host(0, 1, 0) =  1.0;           workset_host(0, 1, 1) = -1.0; 
          workset_host(0, 2, 0) =  1.0;           workset_host(0, 2, 1) =  1.0; 
          workset_host(0, 3, 0) = -1.0;           workset_host(0, 3, 1) =  1.0; 

          workset_host(1, 0, 0) = -1.0;           workset_host(1, 0, 1) = -1.0; 
          workset_host(1, 1, 0) =  1.0;           workset_host(1, 1, 1) = -1.0; 
          workset_host(1, 2, 0) =  1.0;           workset_host(1, 2, 1) =  1.0; 
          workset_host(1, 3, 0) = -1.0;           workset_host(1, 3, 1) =  1.0; 

          workset_host(2, 0, 0) = -1.0;           workset_host(2, 0, 1) = -1.0; 
          workset_host(2, 1, 0) =  1.0;           workset_host(2, 1, 1) = -1.0; 
          workset_host(2, 2, 0) =  1.0;           workset_host(2, 2, 1) =  1.0; 
          workset_host(2, 3, 0) = -1.0;           workset_host(2, 3, 1) =  1.0; 
          */
          workset_host(0, 0, 0) =  0.0;           workset_host(0, 0, 1) =  0.0; 
          workset_host(0, 1, 0) =  2.0;           workset_host(0, 1, 1) =  0.0; 
          workset_host(0, 2, 0) =  2.0;           workset_host(0, 2, 1) =  2.0; 
          workset_host(0, 3, 0) =  0.0;           workset_host(0, 3, 1) =  2.0; 

          workset_host(1, 0, 0) = -3.0;           workset_host(1, 0, 1) = -4.0; 
          workset_host(1, 1, 0) =  2.0;           workset_host(1, 1, 1) =  0.0; 
          workset_host(1, 2, 0) =  1.0;           workset_host(1, 2, 1) =  2.0; 
          workset_host(1, 3, 0) = -1.0;           workset_host(1, 3, 1) =  1.0; 

          workset_host(2, 0, 0) = -0.5;           workset_host(2, 0, 1) = -0.5; 
          workset_host(2, 1, 0) =  2.0;           workset_host(2, 1, 1) = -0.5; 
          workset_host(2, 2, 0) =  3.0;           workset_host(2, 2, 1) =  2.0; 
          workset_host(2, 3, 0) =  1.0;           workset_host(2, 3, 1) =  2.0; 

          auto pts_on_ref = Kokkos::create_mirror_view(typename DeviceType::memory_space(), pts_on_ref_host);
          Kokkos::deep_copy(pts_on_ref, pts_on_ref_host);

          auto workset = Kokkos::create_mirror_view(typename DeviceType::memory_space(), workset_host);
          Kokkos::deep_copy(workset, workset_host);

          Kokkos::RangePolicy<ExecSpaceType> policy(0, C);
          
          ///
          /// mapToPhysicalFrame 
          ///

          // ** compute via front interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> a_pts_on_phy("a_pts_on_phy", C, P, D);
          {
            CellTools<DeviceType>
              ::mapToPhysicalFrame(a_pts_on_phy, pts_on_ref, workset, 
                                   shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >()));
          }
          auto a_pts_on_phy_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a_pts_on_phy);
          Kokkos::deep_copy(a_pts_on_phy_host, a_pts_on_phy);
          
          // ** compute via impl interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> b_pts_on_phy("b_pts_on_phy", C, P, D);


          {
            typedef F_mapToPhysicalFrame<decltype(b_pts_on_phy),
                                         decltype(pts_on_ref),
                                         decltype(workset)> FunctorType;
            Kokkos::parallel_for(policy, FunctorType(b_pts_on_phy, pts_on_ref, workset));
          }
          // I love lambda...
          // Kokkos::parallel_for(policy, KOKKOS_LAMBDA(const ordinal_type cl) {
          //     double buf[N]; Kokkos::View<double*,Kokkos::Impl::ActiveExecutionMemorySpace> val(&buf[0], N); // N
          //     auto nodes = Kokkos::subview(workset, cl, Kokkos::ALL(), Kokkos::ALL()); // N,D
          //     for (ordinal_type i=0;i<P;++i) {
          //       auto pt_on_ref = Kokkos::subview(  pts_on_ref,     i, Kokkos::ALL()); // D
          //       auto pt_on_phy = Kokkos::subview(b_pts_on_phy, cl, i, Kokkos::ALL()); // D
          //       Impl::Basis_HGRAD_QUAD_C1_FEM::Serial<OPERATOR_VALUE>::getValues(val, pt_on_ref);
          //       Impl::CellTools::Serial::mapToPhysicalFrame(pt_on_phy, val, nodes);
          //     }
          //   });
          auto b_pts_on_phy_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b_pts_on_phy);
          Kokkos::deep_copy(b_pts_on_phy_host, b_pts_on_phy);

          // ** compare
          {
            for (ordinal_type cl=0;cl<C;++cl)
              for (ordinal_type i=0;i<P;++i) 
                for (ordinal_type j=0;j<D;++j) {
                  const double diff = std::abs(a_pts_on_phy_host(cl, i, j) - b_pts_on_phy_host(cl, i, j));
                  if (diff > tol) {
                    *outStream << "Error : mapToPhysicalFrame at (" 
                               << cl << "," << i << "," << j 
                               << ") with diff = " << diff << "\n"; 
                    errorFlag++;
                  }
                }
          }

          ///
          /// mapToReferenceFrame 
          ///
          auto pts_on_phy = a_pts_on_phy;

          // ** compute via front interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> a_pts_on_ref("a_pts_on_ref", C, P, D);
          {
            CellTools<DeviceType>
              ::mapToReferenceFrame(a_pts_on_ref, pts_on_phy, workset, 
                                   shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >()));
          }
          auto a_pts_on_ref_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), a_pts_on_ref);
          Kokkos::deep_copy(a_pts_on_ref_host, a_pts_on_ref);

          // ** compare
          {
            for (ordinal_type cl=0;cl<C;++cl)
              for (ordinal_type i=0;i<P;++i) 
                for (ordinal_type j=0;j<D;++j) {
                  const double diff = std::abs(pts_on_ref_host(i, j) - a_pts_on_ref_host(cl, i, j));
                  if (diff > tol) {
                    *outStream << "Error : mapToReferenceFrame (front version) at (" 
                               << cl << "," << i << "," << j 
                               << ") with diff = " << diff << "\n"; 
                    errorFlag++;
                  }
                }
          }
                    
          // ** compute via impl interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> b_pts_on_ref("b_pts_on_ref", C, P, D);

          {
            typedef F_mapToReferenceFrame<decltype(b_pts_on_ref),
                                          decltype(pts_on_phy),
                                          decltype(workset)> FunctorType;
            Kokkos::parallel_for(policy, FunctorType(b_pts_on_ref, pts_on_phy, workset));
          }
          // Kokkos::parallel_for(policy, KOKKOS_LAMBDA(const ordinal_type cl) {
          //     auto nodes = Kokkos::subview(workset, cl, Kokkos::ALL(), Kokkos::ALL()); // N,D
          //     for (ordinal_type i=0;i<P;++i) {
          //       auto pt_on_phy = Kokkos::subview(  pts_on_phy, cl, i, Kokkos::ALL()); // D
          //       auto pt_on_ref = Kokkos::subview(b_pts_on_ref, cl, i, Kokkos::ALL()); // D

          //       Impl::CellTools::Serial
          //         ::mapToReferenceFrame<Impl::Basis_HGRAD_QUAD_C1_FEM>(pt_on_ref, pt_on_phy, nodes);
          //     }
          //   });
          auto b_pts_on_ref_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b_pts_on_ref);
          Kokkos::deep_copy(b_pts_on_ref_host, b_pts_on_ref);

          // ** compare          
          {
            for (ordinal_type cl=0;cl<C;++cl)
              for (ordinal_type i=0;i<P;++i) 
                for (ordinal_type j=0;j<D;++j) {
                  const double diff = std::abs(pts_on_ref_host(i, j) - b_pts_on_ref_host(cl, i, j));
                  if (diff > tol) {
                    *outStream << "Error : mapToReferenceFrame (impl version) at (" 
                               << cl << "," << i << "," << j 
                               << ") with diff = " << diff << "\n"; 
                    errorFlag++;
                  }
                }
          }


          *outStream
            << "\n"
            << "===============================================================================\n"
            << "| Test 2: reference quad c1 element mapped into 3D physical space:            |\n"
            << "===============================================================================\n\n";


          Kokkos::DynRankView<ValueType,DeviceArrayLayout,HostSpaceType> workset3d_host("workset3d_host", C, N, D+1);

          //set x,y components
          for(size_t i=0; i<workset_host.extent(0);++i)
            for(size_t j=0; j<workset_host.extent(1);++j)
            for(int d=0; d<D;++d)
              workset3d_host(i,j,d) = workset_host(i,j,d);

          //set z component
          workset3d_host(0,0,2) = -1.0;
          workset3d_host(0,1,2) =  1.0;
          workset3d_host(0,2,2) =  4.0;
          workset3d_host(0,3,2) =  2.0;

          workset3d_host(1,0,2) =  1.0;
          workset3d_host(1,1,2) =  0.0;
          workset3d_host(1,2,2) =  0.0;
          workset3d_host(1,3,2) =  0.0;

          workset3d_host(2,0,2) =  0.0;
          workset3d_host(2,1,2) =  0.0;
          workset3d_host(2,2,2) =  5.0;
          workset3d_host(2,3,2) =  0.0;

          auto workset3d = Kokkos::create_mirror_view(typename DeviceType::memory_space(), workset3d_host);
          Kokkos::deep_copy(workset3d, workset3d_host);

          ///
          /// mapToPhysicalFrame
          ///

          // ** compute via impl interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> b_pts3d_on_phy("b_pts_on_phy", C, P, D+1);


          {
            typedef F_mapToPhysicalFrame<decltype(b_pts3d_on_phy),
                                         decltype(pts_on_ref),
                                         decltype(workset3d)> FunctorType;
            Kokkos::parallel_for(policy, FunctorType(b_pts3d_on_phy, pts_on_ref, workset3d));
          }

          ///
          /// mapToReferenceFrame
          ///

          // ** compute via impl interface
          Kokkos::DynRankView<ValueType,DeviceArrayLayout,DeviceType> b_pts3d_on_ref("b_pts3d_on_ref", C, P, D);

          {
            typedef F_mapToReferenceFrame<decltype(b_pts3d_on_ref),
                                          decltype(b_pts3d_on_phy),
                                          decltype(workset3d)> FunctorType;
            Kokkos::parallel_for(policy, FunctorType(b_pts3d_on_ref, b_pts3d_on_phy, workset3d));
          }

          auto b_pts3d_on_ref_host = Kokkos::create_mirror_view(typename HostSpaceType::memory_space(), b_pts3d_on_ref);
          Kokkos::deep_copy(b_pts3d_on_ref_host, b_pts3d_on_ref);

          // ** compare
          {
            for (ordinal_type cl=0;cl<C;++cl)
              for (ordinal_type i=0;i<P;++i)
                for (ordinal_type j=0;j<D;++j) {
                  const double diff = std::abs(pts_on_ref_host(i, j) - b_pts3d_on_ref_host(cl, i, j));
                  if (diff > tol) {
                    *outStream << "Error (3d physical space) : mapToReferenceFrame (impl version) at ("
                               << cl << "," << i << "," << j
                               << ") with diff = " << diff << "\n";
                    errorFlag++;
                  }
                }
          }
        }
      } catch (std::logic_error &err) {
        //============================================================================================//
        // Wrap up test: check if the test broke down unexpectedly due to an exception                //
        //============================================================================================//
        *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;
    }
  }
}