https://mooseframework.inl.gov
SinglePhaseFluidPropertiesTestUtils.h
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 
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 #define AD_DERIV_TEST_CUSTOM_PERTURBATION(f, a, b, tol, rel_pert) \
32  { \
33  const ADReal da = rel_pert * a; \
34  const ADReal db = rel_pert * b; \
35  const ADReal df_da_fd = (f(a + da, b) - f(a - da, b)) / (2 * da); \
36  const ADReal df_db_fd = (f(a, b + db) - f(a, b - db)) / (2 * db); \
37  ADReal f_value, df_da, df_db; \
38  f(a, b, f_value, df_da, df_db); \
39  REL_TEST(f(a, b), f_value, REL_TOL_CONSISTENCY); \
40  REL_TEST(df_da, df_da_fd, tol); \
41  REL_TEST(df_db, df_db_fd, tol); \
42  }
43 
44 // Macro for performing a derivative test
45 #define DERIV_TEST(f, a, b, tol) \
46  { \
47  DERIV_TEST_CUSTOM_PERTURBATION(f, a, b, tol, REL_PERTURBATION); \
48  }
49 
50 // Macro for performing a derivative test
51 #define AD_DERIV_TEST(f, a, b, tol) \
52  { \
53  AD_DERIV_TEST_CUSTOM_PERTURBATION(f, a, b, tol, REL_PERTURBATION); \
54  }
55 
56 // Macro for performing a derivative test (1d function)
57 #define DERIV_TEST_1D(f, dfda, a, tol) \
58  { \
59  const Real da = REL_PERTURBATION * a; \
60  const Real df_da_fd = (f(a + da) - f(a - da)) / (2 * da); \
61  Real df_da = dfda(a); \
62  REL_TEST(df_da, df_da_fd, tol); \
63  }
64 
65 // Macro for testing that a "not implemented" error message is thrown for f(a,b)
66 #define NOT_IMPLEMENTED_TEST_VALUE(f) \
67  { \
68  try \
69  { \
70  f(0, 0); \
71  } \
72  catch (const std::exception & x) \
73  { \
74  std::string msg(x.what()); \
75  EXPECT_TRUE(msg.find("not implemented") != std::string::npos); \
76  } \
77  }
78 
79 // Macro for testing that a "not implemented" error message is thrown for f(a,b,c,d,e)
80 #define NOT_IMPLEMENTED_TEST_DERIV(f) \
81  { \
82  try \
83  { \
84  Real f_val, df_da, df_db; \
85  f(0, 0, f_val, df_da, df_db); \
86  } \
87  catch (const std::exception & x) \
88  { \
89  std::string msg(x.what()); \
90  EXPECT_TRUE(msg.find("not implemented") != std::string::npos); \
91  } \
92  }