BubbleProfiler  0.3.0
by Peter Athron, Csaba Balazs, Michael Bardsley, Andrew Fowlie, Dylan Harries & Graham White
action.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of BubbleProfiler.
3  *
4  * BubbleProfiler is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * BubbleProfiler is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with BubbleProfiler. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
34 #include "error.hpp"
35 #include "logging_manager.hpp"
37 #include "shooting.hpp"
38 
39 #include <iostream>
40 #include <string>
41 #include <vector>
42 
43 namespace BubbleProfiler {
44 
46  double E{1.};
47  double alpha{0.6};
48  int dim{4};
49  bool verbose{false};
50 };
51 
52 void print_usage()
53 {
54  std::cout <<
55  "Usage: quartic [OPTION] <E> <alpha> <dim>\n\n"
56  "Calculate action for one-dimensional quartic potential\n"
57  "parameterized by E and alpha.\n\n"
58  "Example: quartic 1.0 0.6 3\n\n"
59  "Options:\n"
60  " -h, --help print this help message\n"
61  " -v, --verbose produce verbose output"
62  << std::endl;
63 }
64 
65 bool starts_with(const std::string& option, const std::string& prefix)
66 {
67  return !option.compare(0, prefix.size(), prefix);
68 }
69 
70 void parse_positional_args(const std::vector<std::string>& args,
71  Action_options& options)
72 {
73  const auto n_positional_args = args.size();
74 
75  if (n_positional_args == 0) {
76  throw Setup_error(
77  "value of E must be provided");
78  } else if (n_positional_args == 1) {
79  throw Setup_error(
80  "value of alpha must be provided");
81  } else if (n_positional_args == 2) {
82  throw Setup_error(
83  "number of dimensions must be provided");
84  } else if (n_positional_args > 3) {
85  throw Setup_error(
86  "unrecognized command line argument '"
87  + args[3] + "'");
88  }
89 
90  try {
91  options.E = std::stod(args[0]);
92  } catch (const std::invalid_argument& e) {
93  throw Setup_error(
94  "invalid value for E, '" + args[0] + "'");
95  }
96 
97  try {
98  options.alpha = std::stod(args[1]);
99  } catch (const std::invalid_argument& e) {
100  throw Setup_error(
101  "invalid value for alpha, '" + args[1] + "'");
102  }
103 
104  try {
105  options.dim = std::stoi(args[2]);
106  } catch (const std::invalid_argument& e) {
107  throw Setup_error(
108  "invalid number of dimensions '" + args[2] + "'");
109  }
110 }
111 
112 Action_options parse_cmd_line_args(int argc, const char* argv[])
113 {
114  Action_options options;
115 
116  std::vector<std::string> positional_args;
117  int i = 1;
118  while (i < argc) {
119  const std::string opt(argv[i++]);
120 
121  if (!starts_with(opt, "-")) {
122  positional_args.push_back(opt);
123  continue;
124  }
125 
126  if (opt == "-h" || opt == "--help") {
127  print_usage();
128  exit(EXIT_SUCCESS);
129  }
130 
131  if (opt == "-v" || opt == "--verbose") {
132  options.verbose = true;
133  continue;
134  }
135 
136  throw Setup_error(
137  "unrecognized command line argument '" + opt + "'");
138  }
139 
140  parse_positional_args(positional_args, options);
141 
142  return options;
143 }
144 
145 } // namespace BubbleProfiler
146 
147 int main(int argc, const char* argv[])
148 {
149  using namespace BubbleProfiler;
150 
151  if (argc == 1) {
152  std::cerr << "Usage: quartic [OPTION] <E> <alpha> <dim>\n"
153  << "Try 'quartic --help' for more information."
154  << std::endl;
155  return EXIT_FAILURE;
156  }
157 
158  int exit_code = 0;
159  try {
160  Action_options options = parse_cmd_line_args(argc, argv);
161 
162  if (options.verbose) {
165  }
166 
167  Restricted_quartic_potential potential(options.alpha, options.E);
168  const double false_min = potential.get_local_minimum_location();
169  const double barrier = potential.get_local_maximum_location();
170  const double true_min = potential.get_global_minimum_location();
171 
172  Shooting profiler;
173  profiler.set_bisection_precision_bits(5);
174  profiler.set_action_arrived_rel(1.e-3);
175  profiler.set_shooting_abs_tol(1.e-4);
176  profiler.set_shooting_rel_tol(1.e-4);
177  profiler.set_action_abs_tol(1.e-6);
178  profiler.set_action_rel_tol(1.e-6);
179  profiler.set_drho_frac(1.e-3);
180  profiler.set_bisect_lambda_max(5);
181  profiler.set_max_iterations(100000);
182  profiler.set_max_periods(1.e2);
183  profiler.set_f_y_max(1.e6);
184  profiler.set_f_y_min(1.e-3);
185  profiler.set_y_max(1.e1);
186 
187  profiler.solve(potential, false_min, true_min, barrier,
188  options.dim, Shooting::Solver_options::Compute_action);
189 
190  std::cout << "# E = " << options.E
191  << ", alpha = " << options.alpha
192  << ", dim = " << options.dim
193  << ", action = " << profiler.get_euclidean_action()
194  << std::endl;
195  } catch (const Error& e) {
196  std::cerr << "Error: " << e.what() << std::endl;
197  exit_code = EXIT_FAILURE;
198  } catch (...) {
199  std::cerr << "Error: unrecognized error occurred."
200  << std::endl;
201  exit_code = EXIT_FAILURE;
202  }
203 
204  return exit_code;
205 }
void set_shooting_rel_tol(double tol)
Definition: shooting.hpp:58
void parse_positional_args(const std::vector< std::string > &args, Fubini_options &options)
void set_max_iterations(int i)
Definition: shooting.hpp:76
Bubble_profiler_inputs parse_cmd_line_args(int argc, const char *argv[])
void solve(const std::function< double(double)> &potential_, const std::function< double(double)> &potential_first_, const std::function< double(double)> &potential_second_, double false_min_, double true_min_, double barrier_, int dim_=3, unsigned int options=(Solver_options::Compute_action|Solver_options::Compute_profile))
Definition: shooting.cpp:81
void set_bisect_lambda_max(double l)
Definition: shooting.hpp:74
static Logging_manager & get_manager()
double get_euclidean_action() const
Definition: shooting.cpp:40
void set_f_y_max(double f)
Definition: shooting.hpp:80
int main(int argc, const char *argv[])
Definition: action.cpp:147
void set_action_rel_tol(double tol)
Definition: shooting.hpp:62
void set_f_y_min(double f)
Definition: shooting.hpp:82
void set_action_arrived_rel(double tol)
Definition: shooting.hpp:54
void set_action_abs_tol(double tol)
Definition: shooting.hpp:60
void set_max_periods(double p)
Definition: shooting.hpp:78
void set_drho_frac(double frac)
Definition: shooting.hpp:64
void set_y_max(double y)
Definition: shooting.hpp:84
Solve the one-dimensional problem using the shooting method.
Definition: shooting.hpp:44
Exception indicating general setup error.
Definition: error.hpp:32
One-dimensional shooting method.
bool starts_with(const std::string &option, const std::string &prefix)
void set_shooting_abs_tol(double tol)
Definition: shooting.hpp:56
void set_bisection_precision_bits(int b)
Definition: shooting.hpp:52