Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/solver/impls/petsc/petsc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,39 @@ PetscErrorCode PetscMonitor(TS ts, PetscInt UNUSED(step), PetscReal t, Vec X, vo
PetscFunctionBegin;

auto* s = static_cast<PetscSolver*>(ctx);

if (s->diagnose) {
// Print diagnostic information.
// Using the same format at the SNES solver

SNESConvergedReason reason;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'reason' is not initialized [cppcoreguidelines-init-variables]

    SNESConvergedReason reason;
                        ^

SNESGetConvergedReason(s->snes, &reason);

PetscReal timestep;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'timestep' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/petsc/petsc.cxx:48:

- #include <petsc.h>
+ #include <math.h>
+ #include <petsc.h>
Suggested change
PetscReal timestep;
PetscReal timestep = NAN;

TSGetTimeStep(s->ts, &timestep);

// SNES failure counter is only reset when TSSolve is called
static PetscInt prev_snes_failures = 0;
PetscInt snes_failures;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'snes_failures' is not initialized [cppcoreguidelines-init-variables]

Suggested change
PetscInt snes_failures;
PetscInt snes_failures = 0;

TSGetSNESFailures(s->ts, &snes_failures);
snes_failures -= prev_snes_failures;
prev_snes_failures += snes_failures;

// Get number of iterations
int nl_its;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'nl_its' is not initialized [cppcoreguidelines-init-variables]

Suggested change
int nl_its;
int nl_its = 0;

SNESGetIterationNumber(s->snes, &nl_its);
int lin_its;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'lin_its' is not initialized [cppcoreguidelines-init-variables]

Suggested change
int lin_its;
int lin_its = 0;

SNESGetLinearSolveIterations(s->snes, &lin_its);

output.print("\r"); // Carriage return for printing to screen
output.write("Time: {}, timestep: {}, nl iter: {}, lin iter: {}, reason: {}", t,
timestep, nl_its, lin_its, static_cast<int>(reason));
if (snes_failures > 0) {
output.write(", SNES failures: {}", snes_failures);
}
output.write("\n");
}

if (t < s->next_output) {
// Not reached output time yet => return
PetscFunctionReturn(0);
Expand Down Expand Up @@ -903,8 +936,19 @@ PetscErrorCode PetscSolver::rhs(BoutReal t, Vec udata, Vec dudata, bool linear)
load_vars(const_cast<BoutReal*>(udata_array));
VecRestoreArrayRead(udata, &udata_array);

// Call RHS function
run_rhs(t, linear);
try {
// Call RHS function
run_rhs(t, linear);
} catch (BoutException& e) {
// Simulation might fail, e.g. negative densities
// if timestep too large
output_warn.write("WARNING: BoutException thrown: {}\n", e.what());

// Tell SNES that the input was out of domain
SNESSetFunctionDomainError(snes);
// Note: Returning non-zero error here leaves vectors in locked state
return 0;
}

// Save derivatives to PETSc
BoutReal* dudata_array;
Expand Down
15 changes: 13 additions & 2 deletions src/solver/impls/snes/snes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,13 @@ int SNESSolver::run() {
ierr = VecRestoreArrayRead(snes_x, &xdata);
CHKERRQ(ierr);
}
run_rhs(simtime);

try {
run_rhs(simtime);
} catch (BoutException& e) {
output_error.write("ERROR: BoutException thrown: {}\n", e.what());
return 1;
}

// Copy derivatives back
{
Expand Down Expand Up @@ -1168,7 +1174,12 @@ int SNESSolver::run() {
ierr = VecRestoreArrayRead(output_x, &xdata);
CHKERRQ(ierr);
}
run_rhs(target); // Run RHS to calculate auxilliary variables

try {
run_rhs(target); // Run RHS to calculate auxilliary variables
} catch (BoutException& e) {
output_error.write("ERROR: BoutException thrown: {}\n", e.what());
}

if (call_monitors(target, s, getNumberOutputSteps()) != 0) {
break; // User signalled to quit
Expand Down
Loading