www.mooseframework.org
SinglePhaseFluidPropertiesTestUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
10 #pragma once
11 
13 
14 // Relative perturbation for derivative tests
15 #define REL_PERTURBATION 1e-6
16 
17 // Macro for performing a derivative test with a custom perturbation
18 #define DERIV_TEST_CUSTOM_PERTURBATION(f, a, b, tol, rel_pert) \
19  { \
20  const Real da = rel_pert * a; \
21  const Real db = rel_pert * b; \
22  const Real df_da_fd = (f(a + da, b) - f(a - da, b)) / (2 * da); \
23  const Real df_db_fd = (f(a, b + db) - f(a, b - db)) / (2 * db); \
24  Real f_value, df_da, df_db; \
25  f(a, b, f_value, df_da, df_db); \
26  REL_TEST(f(a, b), f_value, REL_TOL_CONSISTENCY); \
27  REL_TEST(df_da, df_da_fd, tol); \
28  REL_TEST(df_db, df_db_fd, tol); \
29  }
30 
31 // Macro for performing a derivative test
32 #define DERIV_TEST(f, a, b, tol) \
33  { \
34  DERIV_TEST_CUSTOM_PERTURBATION(f, a, b, tol, REL_PERTURBATION); \
35  }
36 
37 // Macro for performing a derivative test (1d function)
38 #define DERIV_TEST_1D(f, dfda, a, tol) \
39  { \
40  const Real da = REL_PERTURBATION * a; \
41  const Real df_da_fd = (f(a + da) - f(a - da)) / (2 * da); \
42  Real df_da = dfda(a); \
43  REL_TEST(df_da, df_da_fd, tol); \
44  }
45 
46 // Macro for testing that a "not implemented" error message is thrown for f(a,b)
47 #define NOT_IMPLEMENTED_TEST_VALUE(f) \
48  { \
49  try \
50  { \
51  f(0, 0); \
52  } \
53  catch (const std::exception & x) \
54  { \
55  std::string msg(x.what()); \
56  EXPECT_TRUE(msg.find("not implemented") != std::string::npos); \
57  } \
58  }
59 
60 // Macro for testing that a "not implemented" error message is thrown for f(a,b,c,d,e)
61 #define NOT_IMPLEMENTED_TEST_DERIV(f) \
62  { \
63  try \
64  { \
65  Real f_val, df_da, df_db; \
66  f(0, 0, f_val, df_da, df_db); \
67  } \
68  catch (const std::exception & x) \
69  { \
70  std::string msg(x.what()); \
71  EXPECT_TRUE(msg.find("not implemented") != std::string::npos); \
72  } \
73  }
FluidPropertiesTestUtils.h