Repository navigation
SNES predictor refactoring - #3506
Conversation
New enum `BoutSnesPredictor` can be `none` or `linear`. An error message is printed if the previous boolean `predictor` setting is used. This is to enable different types of predictor to be implemented.
Separates the logic of extrapolation from previous steps from the rest of the solver logic. Should make experimenting with other predictor methods easier.
Small updates to documentation and error message.
ZedThree
left a comment
There was a problem hiding this comment.
LGTM, some minor suggested polish though
Use `BOUT_ENUM_CLASS_NS` to put the enums into the `bout` namespace.
@ZedThree review suggestion, using a struct rather than std::pair.
| /// `x` must already be allocated and will be overwritten. | ||
| void predict(SnesPredictor predictor_type, BoutReal time, Vec& x) { | ||
| // [t0, x0] is the most recent state | ||
| const auto& [t0, x0] = this->history[0]; |
There was a problem hiding this comment.
warning: 'x0' declared with a const-qualified typedef; results in the type being '_p_Vec *const' instead of 'const _p_Vec *' [misc-misplaced-const]
const auto& [t0, x0] = this->history[0];
^Additional context
/usr/lib/petscdir/petsc3.22/x86_64-linux-gnu-real/include/petscvec.h:25: typedef declared here
typedef struct _p_Vec *Vec;
^| BOUT_DO_PETSC(VecCopy(x0, x)); | ||
| return; | ||
| case SnesPredictor::linear: { | ||
| const auto& [t1, x1] = this->history[1]; |
There was a problem hiding this comment.
warning: 'x1' declared with a const-qualified typedef; results in the type being '_p_Vec *const' instead of 'const _p_Vec *' [misc-misplaced-const]
const auto& [t1, x1] = this->history[1];
^Additional context
/usr/lib/petscdir/petsc3.22/x86_64-linux-gnu-real/include/petscvec.h:25: typedef declared here
typedef struct _p_Vec *Vec;
^Symbols moved into `bout` namespace
- be_rational uses three states to work out a backward Euler damping factor and extrapolate. - Quality measure that evaluates how good predictors are.
Scales the predicted step size by a factor that is adapted. After each step the optimal factor is calculated and used to update the factor for the next step.
Moves predictor options into subsection of solver options. e.g. Rather than `solver:predictor_blend` it's `solver:predictor:blend`
Fixed call to VecAXPY with incorrect argument order. Additional unit testing. Documentation and code docstrings consistent with implementation.
|
|
||
| // Predicted state | ||
| // `x` always lies between `x0` (constant prediction) and the linear prediction. | ||
| x_data[i] = x0_data[i] + dt * s0 * damp; |
There was a problem hiding this comment.
warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
| x_data[i] = x0_data[i] + dt * s0 * damp; | |
| rediction.( |
src/solver/impls/snes/snes.cxx:293:
- mp;
+ mp);| } | ||
|
|
||
| BoutReal Predictor::quality(Vec x0, Vec x_new, Vec x_pred) { | ||
| PetscReal norm_num, norm_den; |
There was a problem hiding this comment.
warning: variable 'norm_num' is not initialized [cppcoreguidelines-init-variables]
, Vec x_pred) {
^this fix will not be applied because it overlaps with another fix
| } | ||
|
|
||
| BoutReal Predictor::optimal_weight(Vec x0, Vec x_new, Vec x_pred) { | ||
| PetscReal norm_delta, norm_align; |
There was a problem hiding this comment.
warning: variable 'norm_delta' is not initialized [cppcoreguidelines-init-variables]
, Vec x_pred) {
^this fix will not be applied because it overlaps with another fix
Makes printing of the `quality` metric optional. Renamed `quality` to `prediction_error_ratio` because it's a metric where smaller is better; minimising quality doesn't seem right.
Only prints blending diagnostics when `diagnose=true`
ZedThree
left a comment
There was a problem hiding this comment.
LGTM! Just wondering if the use of this-> is a deliberate choice, or just an LLMism?
|
Thsnks @ZedThree ! The |
Separate the predictor into a separate
Predictorclass.Set predictor type using
solver:predictor:type. Options areconstant,linearandbe_rationalthat uses a damped relaxation to steady state.An option
solver:predictor:blendcalculates the optimum scaling of the previous predictor step, and uses that to update the scaling of the next step.Adds automatic detection of cases where predictors can cause numerical issues e.g. when the step size is below tolerance.
Includes documentation and unit tests.
Hermes-3 1D-threshold example
linear(default) predictor:be_rationalpredictor: