QMCPACK
test_min_oned.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2018 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include <iostream>
16 
18 
19 
20 namespace qmcplusplus
21 {
22 using RealType = double;
23 
24 class MinTest
25 {
26 public:
27  MinTest(double value = 0.0) : min_value(value) {}
29  RealType one_cycle(RealType x) { return (x - min_value) * (x - min_value); }
30 
32  {
33  auto bracket = bracket_minimum([this](RealType x) -> RealType { return one_cycle(x); }, x0);
34  REQUIRE(bracket.success == true);
35  RealType xa = bracket.a;
36  RealType xb = bracket.b;
37  RealType xc = bracket.c;
38  //std::cout << " xa = " << xa;
39  //std::cout << " xb = " << xb;
40  //std::cout << " xc = " << xc;
41  //std::cout << std::endl;
42 
43  REQUIRE(xa < xb);
44  REQUIRE(xb < xc);
45 
46  // For a starting point of 1.3
47  //CHECK(xa == Approx(-0.0041));
48  //CHECK(xb == Approx( 0.03615));
49  //CHECK(xc == Approx(-0.04435));
50 
51 
52  RealType fa = one_cycle(xa);
53  RealType fb = one_cycle(xb);
54  RealType fc = one_cycle(xc);
55 
56  REQUIRE(fa > fb);
57  REQUIRE(fc > fb);
58  }
59 
60  // ensure the bracket search will find a minimum at the edge of the bound
62  {
63  auto bracket = bracket_minimum([this](RealType x) -> RealType { return one_cycle(x); }, x0, bound);
64  REQUIRE(bracket.success == false);
65  }
66 
67  void find_min(RealType x0)
68  {
69  auto bracket = bracket_minimum([this](RealType x) -> RealType { return one_cycle(x); }, x0);
70  auto m = find_minimum([this](RealType x) -> RealType { return one_cycle(x); }, bracket);
71 
72  CHECK(m.first == Approx(min_value));
73  CHECK(m.second == Approx(0.0));
74  }
75 };
76 
77 TEST_CASE("bracket minimum", "[numerics]")
78 {
79  MinTest min_test;
80  min_test.find_bracket(1.3);
81  min_test.find_bracket(-1.3);
82  min_test.find_bracket(10.0);
83 
84 
85  MinTest min_test2(1.5);
86  min_test2.find_bracket(1.3);
87  min_test2.find_bracket(-1.3);
88  min_test2.find_bracket(10.0);
89  min_test2.find_bracket_bound(1.2, 1.4);
90 
91  MinTest min_test3(-0.5);
92  min_test3.find_bracket(1.3);
93  min_test3.find_bracket(-1.3);
94  min_test3.find_bracket(10.0);
95  min_test3.find_bracket_bound(1.0, 2.0);
96 }
97 
98 TEST_CASE("find minimum", "[numerics]")
99 {
100  MinTest min_test;
101  min_test.find_min(1.3);
102  min_test.find_min(-1.3);
103  min_test.find_min(10.0);
104 
105  MinTest min_test2(1.5);
106  min_test2.find_min(1.3);
107  min_test2.find_min(-1.3);
108  min_test2.find_min(10.0);
109 
110  MinTest min_test3(-0.5);
111  min_test3.find_min(1.3);
112  min_test3.find_min(-1.3);
113  min_test3.find_min(10.0);
114 }
115 
116 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Bracket_min_t< T > bracket_minimum(const F &f, T initial_value, T bound_max=-1.0)
TEST_CASE("complex_helper", "[type_traits]")
MinTest(double value=0.0)
RealType one_cycle(RealType x)
REQUIRE(std::filesystem::exists(filename))
std::pair< T, T > find_minimum(const F &f, Bracket_min_t< T > &bracket)
void find_min(RealType x0)
void find_bracket(RealType x0)
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void find_bracket_bound(RealType x0, RealType bound)