Line | Branch | Exec | Source |
---|---|---|---|
1 | // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
2 | // vi: set et ts=4 sw=4 sts=4: | ||
3 | // | ||
4 | // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder | ||
5 | // SPDX-License-Identifier: GPL-3.0-or-later | ||
6 | // | ||
7 | /*! | ||
8 | * \file | ||
9 | * \ingroup MaterialTests | ||
10 | * \brief Test for the 2p van Genuchten material law | ||
11 | */ | ||
12 | |||
13 | #ifndef DUMUX_TEST_MATERIALLAW_FUNCTIONS_HH | ||
14 | #define DUMUX_TEST_MATERIALLAW_FUNCTIONS_HH | ||
15 | |||
16 | #include <iomanip> | ||
17 | |||
18 | #include <dune/common/float_cmp.hh> | ||
19 | #include <dumux/common/math.hh> | ||
20 | #include <dumux/common/numericdifferentiation.hh> | ||
21 | |||
22 | namespace Dumux::Test { | ||
23 | |||
24 | template<class F, class D> | ||
25 | 16 | void testDerivatives(std::string_view derivName, | |
26 | const std::vector<double>& values, | ||
27 | const F& f, const D& deriv) | ||
28 | { | ||
29 | static constexpr double eps = 1e-1; | ||
30 | static constexpr double numEps = 1e-8; | ||
31 |
4/4✓ Branch 0 taken 800 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 800 times.
✓ Branch 3 taken 8 times.
|
1648 | for (auto val : values) |
32 | { | ||
33 | 3200 | double analyticDeriv = deriv(val); | |
34 | |||
35 | 1600 | double numericDeriv = 0.0; | |
36 | 3200 | Dumux::NumericDifferentiation::partialDerivative(f, val, | |
37 |
1/2✓ Branch 1 taken 800 times.
✗ Branch 2 not taken.
|
1600 | numericDeriv, f(val), numEps*val, 0 /*central differences*/); |
38 | |||
39 |
3/4✓ Branch 0 taken 408 times.
✓ Branch 1 taken 392 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 800 times.
|
2416 | if (Dune::FloatCmp::ne(analyticDeriv, numericDeriv, eps)) |
40 | ✗ | DUNE_THROW(Dune::Exception, "Analytic derivative for " << derivName | |
41 | << " doesn't match numerical derivative: " | ||
42 | << std::setprecision(10) << analyticDeriv << " != " << numericDeriv | ||
43 | << " evaluated at " << val); | ||
44 | } | ||
45 | 16 | } | |
46 | |||
47 | template<class F, class G> | ||
48 | 38 | void testValueEqualRange(std::string_view testName, | |
49 | const std::vector<double>& values, | ||
50 | const F& f, const G& g) | ||
51 | { | ||
52 | static constexpr double eps = 1e-5; | ||
53 |
4/4✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1900 times.
✓ Branch 3 taken 19 times.
|
3914 | for (auto val : values) |
54 | { | ||
55 | 3800 | const auto a = f(val); | |
56 |
1/2✓ Branch 1 taken 1100 times.
✗ Branch 2 not taken.
|
5400 | const auto b = g(val); |
57 | |||
58 | 3800 | const auto throwError = [&] | |
59 | { | ||
60 | ✗ | DUNE_THROW(Dune::Exception, "Test: " << testName << ": Function values do not match: " | |
61 | << a << " != " << b << " evaluated at " << val << "\n"); | ||
62 | }; | ||
63 | |||
64 |
3/4✓ Branch 0 taken 1892 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1892 times.
|
3800 | if (a == 0.0 || b == 0.0) |
65 | { | ||
66 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
32 | if (Dune::FloatCmp::ne<decltype(a), Dune::FloatCmp::absolute>(a, b, 1e-15)) |
67 | ✗ | throwError(); | |
68 | } | ||
69 |
3/4✓ Branch 0 taken 687 times.
✓ Branch 1 taken 1205 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1892 times.
|
5158 | else if (Dune::FloatCmp::ne(a, b, eps)) |
70 | ✗ | throwError(); | |
71 | } | ||
72 | 38 | } | |
73 | |||
74 | template<class RegLaw> | ||
75 | 16 | void runMaterialLawTest(const std::string& name, | |
76 | const RegLaw& regLaw, | ||
77 | const std::vector<typename RegLaw::Scalar>& sw, | ||
78 | const std::vector<typename RegLaw::Scalar>& swForDerivatives) | ||
79 | { | ||
80 | 24 | const auto pc = [&](){ auto pc = sw; | |
81 | 1616 | for (int i = 0; i < sw.size(); ++i) | |
82 | 1600 | pc[i] = regLaw.pc(sw[i]); | |
83 | 24 | return pc; | |
84 | }(); | ||
85 | |||
86 |
5/10✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 500 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 500 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 300 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 300 times.
✗ Branch 15 not taken.
|
2416 | testDerivatives("dpc_dsw", swForDerivatives, [&](auto sw){ return regLaw.pc(sw); }, [&](auto sw){ return regLaw.dpc_dsw(sw); }); |
87 | // testDerivatives("dsw_dpc", pc, [&](auto pc){ return regLaw.sw(pc); }, [&](auto pc){ return regLaw.dsw_dpc(pc); }); | ||
88 |
5/9✓ Branch 1 taken 500 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 500 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 300 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 300 times.
✗ Branch 11 not taken.
|
816 | testValueEqualRange("Checking sw == sw(pc(sw))", sw, [](auto sw){ return sw; }, [&](auto sw) { return regLaw.sw(regLaw.pc(sw)); }); |
89 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
816 | testValueEqualRange("Checking 1.0 == dsw_dpc*dpc_dsw^-1", sw, [](auto sw){ return 1.0; }, [&](auto sw) { return regLaw.dpc_dsw(sw)*regLaw.dsw_dpc(regLaw.pc(sw)); }); |
90 | |||
91 | // test pc-sw curve against some precomputed values | ||
92 |
6/18✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 8 times.
✓ Branch 13 taken 8 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
|
32 | writeContainerToFile(pc, "test_pnm_pcsw_" + name + ".dat", 100); |
93 | 16 | } | |
94 | |||
95 | } // end namespace Dumux | ||
96 | |||
97 | #endif | ||
98 |