-
Notifications
You must be signed in to change notification settings - Fork 111
SNES pseudo-timestepping fixes & improvements #3492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bendudson
wants to merge
4
commits into
next
Choose a base branch
from
snes-ptc
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−41
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c26281
SNESSolver::updatePseudoTimestep fix typo
bendudson fd8dc16
SNESSolver: Improve failure handling in pseudo_transient mode
bendudson ba9bdab
SNESSolver: Add psuedo_squash settings
bendudson 2b5183b
SNESSolver: Update PTC method docs
bendudson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ PetscErrorCode withOptionalSubvectors(Func operation, IS indices, Args... args) | |
| } | ||
|
|
||
| for (std::size_t i = acquired; i > 0; --i) { | ||
| PetscErrorCode restore_ierr = | ||
| const PetscErrorCode restore_ierr = | ||
| VecRestoreSubVector(vectors[i - 1], indices, &subvectors[i - 1]); | ||
| if (ierr == PETSC_SUCCESS) { | ||
| ierr = restore_ierr; | ||
|
|
@@ -332,6 +332,18 @@ SNESSolver::SNESSolver(Options* opts) | |
| pseudo_max_ratio((*options)["pseudo_max_ratio"] | ||
| .doc("PTC maximum timestep ratio between neighbors") | ||
| .withDefault(2.)), | ||
| pseudo_squash_failure_threshold( | ||
| (*options)["pseudo_squash_failure_threshold"] | ||
| .doc("Squash timestep variation when snes failures reaches this threshold") | ||
| .withDefault(5)), | ||
| pseudo_squash_method( | ||
| (*options)["pseudo_squash_method"] | ||
| .doc("Method to apply when squashing pseudo timesteps: affine or log.") | ||
| .withDefault(BoutPseudoSquashMethod::log)), | ||
| pseudo_squash_lambda((*options)["pseudo_squash_lambda"] | ||
| .doc("How much variation to keep? 0 = No variation; 1 = " | ||
| "Full variation (no squashing)") | ||
| .withDefault(0.5)), | ||
| timestep_control((*options)["timestep_control"] | ||
| .doc("Timestep control method") | ||
| .withDefault(BoutSnesTimestep::pid_nonlinear_its)), | ||
|
|
@@ -427,6 +439,8 @@ SNESSolver::SNESSolver(Options* opts) | |
| .doc("Which Jacobian to save: system, scaled, or rhs") | ||
| .withDefault(bout::JacobianExportKind::system)) { | ||
| supports_constraints = true; // This solver can handle constraints | ||
|
|
||
| ASSERT0((pseudo_squash_lambda >= 0.0) and (pseudo_squash_lambda <= 1.0)); | ||
| } | ||
|
|
||
| SNESSolver::~SNESSolver() { | ||
|
|
@@ -1025,15 +1039,60 @@ int SNESSolver::run() { | |
| if (snes_failures == max_snes_failures - 1) { | ||
| // Last chance. Set to uniform smallest timestep | ||
| PetscCall(VecSet(dt_vec, dt_min_reset)); | ||
| pseudo_timestep = dt_min_reset; | ||
|
|
||
| // Scale down pseudo_alpha so that PID controller isn't saturated | ||
| pseudo_alpha = pseudo_alpha_minimum; | ||
|
|
||
| } else if (snes_failures >= pseudo_squash_failure_threshold) { | ||
| // Squash variation in timestep between cells. | ||
| // pseudo_squash_lambda determines how much variation to keep. | ||
|
|
||
| switch (pseudo_squash_method) { | ||
| case BoutPseudoSquashMethod::affine: | ||
| // Modify dt_vec using Affine squash | ||
| // dt_vec <- lambda * dt_vec + (1 - lambda) * timestep | ||
|
|
||
| PetscCall(VecScale(dt_vec, pseudo_squash_lambda)); | ||
| PetscCall(VecShift(dt_vec, (1.0 - pseudo_squash_lambda) * timestep)); | ||
|
|
||
| // Modify pseudo_timestep | ||
| pseudo_timestep = pseudo_squash_lambda * pseudo_timestep | ||
| + (1 - pseudo_squash_lambda) * timestep; | ||
| break; | ||
| case BoutPseudoSquashMethod::log: | ||
| // Log squash | ||
| // dt_vec <- timestep * (dt_vec / timestep)^lambda | ||
| PetscInt size; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'size' is not initialized [cppcoreguidelines-init-variables] src/solver/impls/snes/snes.cxx:1066: - ;
+ = 0; |
||
| PetscCall(VecGetLocalSize(dt_vec, &size)); | ||
| BoutReal* dt_data = nullptr; | ||
| PetscCall(VecGetArray(dt_vec, &dt_data)); | ||
| for (PetscInt i = 0; i != size; ++i) { | ||
| dt_data[i] = | ||
| timestep * std::pow(dt_data[i] / timestep, pseudo_squash_lambda); | ||
| } | ||
| PetscCall(VecRestoreArray(dt_vec, &dt_data)); | ||
|
|
||
| pseudo_timestep = | ||
| timestep * pow(pseudo_timestep / timestep, pseudo_squash_lambda); | ||
| break; | ||
| }; | ||
|
|
||
| // Anti-windup: Calculate the effective alpha parameter | ||
| pseudo_alpha = mean(local_residual * pseudo_timestep, true); | ||
|
|
||
| } else if (snes_failures == 5) { | ||
| // Set uniform timestep | ||
| PetscCall(VecSet(dt_vec, timestep)); | ||
| } else { | ||
| // Global scaling of timesteps | ||
| // Note: A better strategy might be to reduce timesteps | ||
| // in problematic cells. | ||
| PetscCall(VecScale(dt_vec, timestep_factor_on_failure)); | ||
|
|
||
| pseudo_timestep *= timestep_factor_on_failure; | ||
|
|
||
| // Scale alpha down by the same amount | ||
| // If this is not done then PID controller can 'wind up' | ||
| // because the controller keeps increasing alpha between failures. | ||
| pseudo_alpha *= timestep_factor_on_failure; | ||
| } | ||
| } else { | ||
| // Try a smaller timestep | ||
|
|
@@ -1502,7 +1561,7 @@ PetscErrorCode SNESSolver::updatePseudoTimestepping() { | |
| if (i3d.y() != 0) { | ||
| min_neighboring_dt = std::min(min_neighboring_dt, pseudo_timestep[i3d.ym()]); | ||
| } | ||
| if (i3d.x() != mesh->LocalNy - 1) { | ||
| if (i3d.y() != mesh->LocalNy - 1) { | ||
| min_neighboring_dt = std::min(min_neighboring_dt, pseudo_timestep[i3d.yp()]); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.