Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
base_predictor.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.0.
2//
3//heun_euler.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//heun_euler.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with heun_euler.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16
17// predictor.hpp
18//
19// copyright 2015
20// James B. Collins
21// West Texas A&M University
22// Department of Mathematics
23// Spring 2016
24
25
31
32#ifndef BERTINI_BASE_PREDICTORS_HPP
33#define BERTINI_BASE_PREDICTORS_HPP
34
37
38#include "system.hpp"
39#include "mpfr_extensions.hpp"
40#include <Eigen/LU>
41
42#include <boost/type_index.hpp>
43
44
45namespace bertini{
46 namespace tracking{
47 namespace predict{
48
49
50 using Predictor = Predictor;
51
57 inline
58 Predictor DefaultPredictor()
59 {
60 return Predictor::Euler;
61 }
62
63
67 inline
68 unsigned Order(Predictor predictor_choice)
69 {
70 switch (predictor_choice)
71 {
72 case (Predictor::Euler):
73 return 1;
74 case (Predictor::HeunEuler):
75 return 1;
76 case (Predictor::RK4):
77 return 4;
78 case (Predictor::RKF45):
79 return 4;
80 case (Predictor::RKCashKarp45):
81 return 4;
82 case (Predictor::RKDormandPrince56):
83 return 5;
84 case (Predictor::RKVerner67):
85 return 6;
86 default:
87 {
88 throw std::runtime_error("incompatible predictor choice in Order");
89 }
90 }
91 }
92
93
94 inline bool HasErrorEstimate(Predictor predictor_choice)
95 {
96 switch (predictor_choice)
97 {
98 case (Predictor::Euler):
99 return false;
100 case (Predictor::HeunEuler):
101 return true;
102 case (Predictor::RK4):
103 return false;
104 case (Predictor::RKF45):
105 return true;
106 case (Predictor::RKCashKarp45):
107 return true;
108 case (Predictor::RKDormandPrince56):
109 return true;
110 case (Predictor::RKVerner67):
111 return true;
112 default:
113 {
114 throw std::runtime_error("incompatible predictor choice in HasErrorEstimate");
115 }
116 }
117 }
118
119
120
121
122
123
124
125
126
150
151 class BasePredictor
152 {
153 public:
154
155
156 BasePredictor(Predictor method){};
157
173
174 template<typename ComplexType, typename RealType>
175 SuccessCode Predict(Vec<ComplexType> & next_space,
176 System const& S,
177 Vec<ComplexType> const& current_space, ComplexType current_time,
178 ComplexType const& delta_t,
179 RealType & condition_number_estimate,
180 unsigned & num_steps_since_last_condition_number_computation,
181 unsigned frequency_of_CN_estimation,
182 RealType const& tracking_tolerance)
183 {
184
185 return FullStep<ComplexType, RealType>(next_space, S, current_space, current_time, delta_t);
186
187
188 }
189
190
191
211
212 template<typename ComplexType, typename RealType>
213 SuccessCode Predict(Vec<ComplexType> & next_space,
214 RealType & size_proportion,
215 RealType & norm_J,
216 RealType & norm_J_inverse,
217 System const& S,
218 Vec<ComplexType> const& current_space, ComplexType current_time,
219 ComplexType const& delta_t,
220 RealType & condition_number_estimate,
221 unsigned & num_steps_since_last_condition_number_computation,
222 unsigned frequency_of_CN_estimation,
223 RealType const& tracking_tolerance,
224 AdaptiveMultiplePrecisionConfig const& AMP_config)
225 {
226
227
228 auto success_code = Predict<ComplexType, RealType>(next_space, S, current_space, current_time, delta_t,
229 condition_number_estimate, num_steps_since_last_condition_number_computation,
230 frequency_of_CN_estimation, tracking_tolerance);
231
232 if(success_code != SuccessCode::Success)
233 return success_code;
234
235 // Calculate condition number and updated if needed
236 Eigen::PartialPivLU<Mat<ComplexType>>& dhdxref = std::get< Eigen::PartialPivLU<Mat<ComplexType>> >(dh_dx_);
237 Mat<ComplexType>& LUref = std::get< Mat<ComplexType> >(LU_);
238
239 Vec<ComplexType> randy = RandomOfUnits<ComplexType>(S.NumVariables());
240 Vec<ComplexType> temp_soln = LUref.solve(randy);
241
242 norm_J = dhdxref.norm();
243 norm_J_inverse = temp_soln.norm();
244
245 if (num_steps_since_last_condition_number_computation >= frequency_of_CN_estimation)
246 {
247 condition_number_estimate = norm_J * norm_J_inverse;
248 num_steps_since_last_condition_number_computation = 1; // reset the counter to 1
249 }
250 else // no need to compute the condition number
251 num_steps_since_last_condition_number_computation++;
252
253
254 // Set size_proportion
255 SetSizeProportion<ComplexType,RealType>(size_proportion, delta_t);
256
257
258
259 //AMP Criteria
260 if (!amp::CriterionA(norm_J, norm_J_inverse, AMP_config)) // AMP_criterion_A != ok
261 return SuccessCode::HigherPrecisionNecessary;
262 else if (!amp::CriterionC(norm_J_inverse, current_space, tracking_tolerance, AMP_config)) // AMP_criterion_C != ok
263 return SuccessCode::HigherPrecisionNecessary;
264
265
266 return success_code;
267 }
268
269
290
291 template<typename ComplexType, typename RealType>
292 SuccessCode Predict(Vec<ComplexType> & next_space,
293 RealType & error_estimate,
294 RealType & size_proportion,
295 RealType & norm_J,
296 RealType & norm_J_inverse,
297 System const& S,
298 Vec<ComplexType> const& current_space, ComplexType current_time,
299 ComplexType const& delta_t,
300 RealType & condition_number_estimate,
301 unsigned & num_steps_since_last_condition_number_computation,
302 unsigned frequency_of_CN_estimation,
303 RealType const& tracking_tolerance,
304 AdaptiveMultiplePrecisionConfig const& AMP_config)
305 {
306 // If this is a method without an error estimator, then can't calculate size proportion and should throw an error
307
308 if(!predict::HasErrorEstimate(predictor_))
309 {
310 throw std::runtime_error("incompatible predictor choice in ExplicitPredict, no error estimator");
311 }
312
313
314
315
316 auto success_code = Predict<ComplexType,RealType>(next_space, size_proportion, norm_J, norm_J_inverse,
317 S, current_space, current_time, delta_t,
318 condition_number_estimate, num_steps_since_last_condition_number_computation,
319 frequency_of_CN_estimation, tracking_tolerance, AMP_config);
320
321 if(success_code != SuccessCode::Success)
322 return success_code;
323
324 SetErrorEstimate<ComplexType,RealType>(error_estimate, delta_t);
325
326
327 return success_code;
328 }
329
330
331
332
339
340 virtual void PredictorMethod(Predictor method) = 0;
341
342 Predictor PredictorMethod()
343 {
344 return predictor_;
345 }
346
347
348
349
353 inline
354 unsigned Order()
355 {
356 return p_;
357 }
358
359
360 inline bool HasErrorEstimate()
361 {
362 return predict::HasErrorEstimate(predictor_);
363 }
364
365
366
367
368
369 protected:
371 //
372 // Protected Members Methods
373 //
375
387
388 template<typename ComplexType, typename RealType>
389 virtual SuccessCode FullStep(Vec<ComplexType> & next_space,
390 System const& S,
391 Vec<ComplexType> const& current_space, ComplexType current_time,
392 ComplexType const& delta_t) = 0;
393
394
404
405 template<typename ComplexType, typename RealType>
406 virtual SuccessCode SetErrorEstimate(RealType & error_estimate, ComplexType const& delta_t) = 0;
407
408
409
419
420 template<typename ComplexType, typename RealType>
421 virtual SuccessCode SetSizeProportion(RealType & size_proportion, ComplexType const& delta_t) = 0;
422
423
424
436
437 template<typename ComplexType>
438 virtual SuccessCode EvalRHS(System const& S,
439 Vec<ComplexType> const& space, ComplexType time, Mat<ComplexType> & K, int stage) = 0;
440
441
442
443
444
445
446
447
448
449
451 //
452 // Protected Data Members
453 //
455
456 Predictor predictor_;
457 unsigned p_;
458 std::tuple< Mat<dbl>, Mat<mpfr> > dh_dx_;
459 std::tuple< Eigen::PartialPivLU<Mat<dbl>>, Eigen::PartialPivLU<Mat<mpfr>> > LU_;
460
461
462
463 }; // re: class BasePredictor
464
465 } // re: namespace predict
466 }// re: namespace tracking
467}// re: namespace bertini
468
469
470
471
472
473
474
475#endif
476
Provides the Adaptive Multiple Precision criteria functions.
bool CriterionC(double const &norm_J_inverse, const Eigen::MatrixBase< Derived > &z, double tracking_tolerance, AdaptiveMultiplePrecisionConfig const &AMP_config)
Check AMP Criterion C.
Definition amp_criteria.hpp:206
bool CriterionA(double const &norm_J, double const &norm_J_inverse, AdaptiveMultiplePrecisionConfig const &AMP_config)
Check AMP Criterion A.
Definition amp_criteria.hpp:71
bool HasErrorEstimate(Predictor predictor_choice)
Ask whether a predictor method provides an error estimate.
Definition base_predictor.hpp:94
Predictor DefaultPredictor()
Get the Bertini2 default predictor.
Definition base_predictor.hpp:58
unsigned Order(Predictor predictor_choice)
The order of the predictor.
Definition base_predictor.hpp:68
The fundamental polynomial system class for Bertini2.
Definition system/system.hpp:99
size_t NumVariables() const
virtual SuccessCode SetSizeProportion(RealType &size_proportion, ComplexType const &delta_t)=0
Compute the size proportion variable for AMP computation.
virtual SuccessCode SetErrorEstimate(RealType &error_estimate, ComplexType const &delta_t)=0
Computes the error estimate of this prediction step.
virtual SuccessCode EvalRHS(System const &S, Vec< ComplexType > const &space, ComplexType time, Mat< ComplexType > &K, int stage)=0
Evaluates the RHS of the Davidenko differential equation at a particular time and space.
virtual SuccessCode FullStep(Vec< ComplexType > &next_space, System const &S, Vec< ComplexType > const &current_space, ComplexType current_time, ComplexType const &delta_t)=0
Performs a full prediction step from current_time to current_time + delta_t.
SuccessCode Predict(Vec< ComplexType > &next_space, System const &S, Vec< ComplexType > const &current_space, ComplexType current_time, ComplexType const &delta_t, RealType &condition_number_estimate, unsigned &num_steps_since_last_condition_number_computation, unsigned frequency_of_CN_estimation, RealType const &tracking_tolerance)
Perform a generic predictor step.
Definition base_predictor.hpp:175
virtual void PredictorMethod(Predictor method)=0
SuccessCode Predict(Vec< ComplexType > &next_space, RealType &size_proportion, RealType &norm_J, RealType &norm_J_inverse, System const &S, Vec< ComplexType > const &current_space, ComplexType current_time, ComplexType const &delta_t, RealType &condition_number_estimate, unsigned &num_steps_since_last_condition_number_computation, unsigned frequency_of_CN_estimation, RealType const &tracking_tolerance, AdaptiveMultiplePrecisionConfig const &AMP_config)
Perform a generic predictor step and return size_proportion and condition number information.
Definition base_predictor.hpp:213
unsigned Order()
Definition base_predictor.hpp:354
SuccessCode Predict(Vec< ComplexType > &next_space, RealType &error_estimate, RealType &size_proportion, RealType &norm_J, RealType &norm_J_inverse, System const &S, Vec< ComplexType > const &current_space, ComplexType current_time, ComplexType const &delta_t, RealType &condition_number_estimate, unsigned &num_steps_since_last_condition_number_computation, unsigned frequency_of_CN_estimation, RealType const &tracking_tolerance, AdaptiveMultiplePrecisionConfig const &AMP_config)
Perform a generic predictor step and return error estimate, size_proportion and condition number info...
Definition base_predictor.hpp:292
Mat< NumberType > RandomOfUnits(uint rows, uint cols)
Make a random matrix of units (numbers with norm 1).
Definition eigen_extensions.hpp:502
Extensions to the Boost.Multiprecision library.
Definition trackers/config.hpp:154
Configs and settings for tracking.