BubbleProfiler  0.3.0
by Peter Athron, Csaba Balazs, Michael Bardsley, Andrew Fowlie, Dylan Harries & Graham White
scale.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 
32 #include "action.hpp"
33 #include "error.hpp"
34 #include "logging_manager.hpp"
35 #include "shooting.hpp"
36 
37 #include <iostream>
38 
39 namespace BubbleProfiler {
40 
41 struct Scale_options {
42  double E{1.};
43  double alpha{0.6};
44  double scale{2.};
45  bool verbose{false};
46 };
47 
48 void print_usage()
49 {
50  std::cout <<
51  "Usage: scale [OPTION] <E> <alpha> <dim>\n\n"
52  "Print action under a change of units.\n\n"
53  "Example: scale 1.0 0.6 2.0\n\n"
54  "Options:\n"
55  " -h, --help print this help message\n"
56  " -v, --verbose produce verbose output"
57  << std::endl;
58 }
59 
60 bool starts_with(const std::string& option, const std::string& prefix)
61 {
62  return !option.compare(0, prefix.size(), prefix);
63 }
64 
65 void parse_positional_args(const std::vector<std::string>& args,
66  Scale_options& options)
67 {
68  const auto n_positional_args = args.size();
69 
70  if (n_positional_args == 0) {
71  throw Setup_error(
72  "value of E must be provided");
73  } else if (n_positional_args == 1) {
74  throw Setup_error(
75  "value of alpha must be provided");
76  } else if (n_positional_args == 2) {
77  throw Setup_error(
78  "scale factor must be provided");
79  } else if (n_positional_args > 3) {
80  throw Setup_error(
81  "unrecognized command line argument '"
82  + args[3] + "'");
83  }
84 
85  try {
86  options.E = std::stod(args[0]);
87  } catch (const std::invalid_argument& e) {
88  throw Setup_error(
89  "invalid value for E, '" + args[0] + "'");
90  }
91 
92  try {
93  options.alpha = std::stod(args[1]);
94  } catch (const std::invalid_argument& e) {
95  throw Setup_error(
96  "invalid value for alpha, '" + args[1] + "'");
97  }
98 
99  try {
100  options.scale = std::stod(args[2]);
101  } catch (const std::invalid_argument& e) {
102  throw Setup_error(
103  "invalid value for scale factor '" + args[2] + "'");
104  }
105 }
106 
107 Scale_options parse_cmd_line_args(int argc, const char* argv[])
108 {
109  Scale_options options;
110 
111  std::vector<std::string> positional_args;
112  int i = 1;
113  bool finished_optionals{false};
114  while (i < argc) {
115  const std::string opt(argv[i++]);
116 
117  if (opt == "--") {
118  finished_optionals = true;
119  continue;
120  }
121 
122  if (!starts_with(opt, "-") || finished_optionals) {
123  positional_args.push_back(opt);
124  continue;
125  }
126 
127  if (opt == "-h" || opt == "--help") {
128  print_usage();
129  exit(EXIT_SUCCESS);
130  }
131 
132  if (opt == "-v" || opt == "--verbose") {
133  options.verbose = true;
134  continue;
135  }
136 
137  throw Setup_error(
138  "unrecognized command line argument '" + opt + "'");
139  }
140 
141  parse_positional_args(positional_args, options);
142 
143  return options;
144 }
145 
146 } // namespace BubbleProfiler
147 
148 int main(int argc, const char* argv[])
149 {
150  using namespace BubbleProfiler;
151 
152  if (argc == 1) {
153  std::cerr << "Usage: scale [OPTION] <E> <alpha> <scale>\n"
154  << "Try 'scale --help' for more information."
155  << std::endl;
156  return EXIT_FAILURE;
157  }
158 
159  int exit_code = 0;
160  try {
161  Scale_options options = parse_cmd_line_args(argc, argv);
162 
163  if (options.verbose) {
166  }
167 
168  const double E = options.E;
169  const double alpha = options.alpha;
170  const double scale = options.scale;
171 
172  const double false_min = 0.;
173  const double true_min = scale;
174  const double barrier = (0.75 / alpha - 1.) * scale;
175 
176  const auto potential = [E, alpha, scale](double phi) {
177  phi /= scale;
178  return -pow(scale, 4) * E * (-alpha * phi * phi * phi * phi
179  + phi * phi * phi
180  + 0.5 * (4. * alpha - 3.) * phi * phi);
181  };
182 
183  const auto potential_first = [E, alpha, scale](double phi) {
184  phi /= scale;
185  return -pow(scale, 3) * E * (-4. * alpha * phi * phi * phi
186  + 3. * phi * phi
187  + (4. * alpha - 3.) * phi);
188  };
189 
190  const auto potential_second = [E, alpha, scale](double phi) {
191  phi /= scale;
192  return -pow(scale, 2) * E * (-12. * alpha * phi * phi
193  + 6. * phi + (4. * alpha - 3.));
194  };
195 
196  Shooting one_dim;
197  one_dim.solve(potential, potential_first, potential_second,
198  false_min, true_min, barrier,
199  4, Shooting::Solver_options::Compute_action);
200 
201  std::cout << one_dim.get_euclidean_action() << std::endl;
202  } catch (const Error& e) {
203  std::cerr << "Error: " << e.what() << std::endl;
204  exit_code = EXIT_FAILURE;
205  } catch (...) {
206  std::cerr << "Error: unrecognized error occurred."
207  << std::endl;
208  exit_code = EXIT_FAILURE;
209  }
210 
211  return exit_code;
212 }
void parse_positional_args(const std::vector< std::string > &args, Fubini_options &options)
Functions for calculating action for one-dimensional potential parameterized by and ...
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
static Logging_manager & get_manager()
double get_euclidean_action() const
Definition: shooting.cpp:40
int main(int argc, const char *argv[])
Definition: scale.cpp:148
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)