https://mooseframework.inl.gov
TestStochasticToolsCalculators.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 #include "gtest/gtest.h"
10 #include <vector>
11 #include "Calculators.h"
12 #include "VectorCalculators.h"
13 #include "libmesh/communicator.h"
14 #include "libmesh/parallel_object.h"
15 
16 using namespace libMesh;
17 using namespace StochasticTools;
18 
19 template <typename InType, typename OutType>
20 std::pair<std::vector<OutType>, std::vector<OutType>>
21 calculate(const InType & x, const std::vector<std::string> & compute)
22 {
24  ParallelObject po(comm);
25  std::vector<std::unique_ptr<StochasticTools::Calculator<InType, OutType>>> calcs;
26  for (const auto & stat : compute)
27  calcs.push_back(StochasticTools::makeCalculator<InType, OutType>(stat, po));
28 
29  std::vector<OutType> result1;
30  for (unsigned int i = 0; i < calcs.size(); ++i)
31  result1.push_back(calcs[i]->compute(x, false));
32 
33  std::vector<OutType> result2;
34  for (unsigned int i = 0; i < calcs.size(); ++i)
35  {
36  calcs[i]->initializeCalculator();
37  for (const auto & val : x)
38  calcs[i]->updateCalculator(val);
39  calcs[i]->finalizeCalculator(false);
40  result2.push_back(calcs[i]->getValue());
41  }
42 
43  return {result1, result2};
44 }
45 
46 TEST(StochasticTools, Calculators)
47 {
48  const std::vector<std::string> compute = {
49  "mean", "min", "max", "sum", "stddev", "stderr", "ratio", "norm2", "median"};
50 
51  {
52  const std::vector<Real> x = {6, 1, 7, 3, 4, 5, 2};
53  const std::vector<Real> expect = {
54  4, 1, 7, 28, 2.1602468994692869408, 0.81649658092772603446, 7, 11.832159566199232259, 4};
55 
56  auto result = calculate<std::vector<Real>, Real>(x, compute);
57  for (unsigned int i = 0; i < expect.size(); ++i)
58  {
59  EXPECT_EQ(result.first[i], expect[i]);
60  EXPECT_EQ(result.second[i], expect[i]);
61  }
62  }
63 
64  {
65  const std::vector<int> x = {6, 1, 7, 3, 4, 5, 2};
66  const std::vector<Real> expect = {
67  4, 1, 7, 28, 2.1602468994692869408, 0.81649658092772603446, 7, 11.832159566199232259, 4};
68 
69  auto result = calculate<std::vector<int>, Real>(x, compute);
70  for (unsigned int i = 0; i < expect.size(); ++i)
71  {
72  EXPECT_EQ(result.first[i], expect[i]);
73  EXPECT_EQ(result.second[i], expect[i]);
74  }
75  }
76 
77  {
78  const std::vector<std::vector<Real>> x = {{6, 1, 7, 3, 4, 5, 2},
79  {1, 7, 3, 4, 5, 2, 6},
80  {7, 3, 4, 5, 2, 6, 1},
81  {3, 4, 5, 2, 6, 1, 7},
82  {4, 5, 2, 6, 1, 7, 3},
83  {5, 2, 6, 1, 7, 3, 4},
84  {2, 6, 1, 7, 3, 4, 5}};
85  const std::vector<Real> expect = {
86  4, 1, 7, 28, 2.1602468994692869408, 0.81649658092772603446, 7, 11.832159566199232259, 4};
87 
88  auto result = calculate<std::vector<std::vector<Real>>, std::vector<Real>>(x, compute);
89  for (unsigned int i = 0; i < expect.size(); ++i)
90  for (unsigned int j = 0; j < x[0].size(); ++j)
91  {
92  EXPECT_EQ(result.first[i][j], expect[i]);
93  EXPECT_EQ(result.second[i][j], expect[i]);
94  }
95  }
96 }
TEST(StochasticTools, Calculators)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
Enum for batch type in stochastic tools MultiApp.
const std::vector< double > x
std::pair< std::vector< OutType >, std::vector< OutType > > calculate(const InType &x, const std::vector< std::string > &compute)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")