Skip to content

Commit

Permalink
Apply clang-format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bendudson authored and github-actions[bot] committed Oct 24, 2024
1 parent 1df2f34 commit 393bbe5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
60 changes: 33 additions & 27 deletions src/solver/impls/snes/snes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <bout/output.hxx>

#include "petscsnes.h"
#include "petscmat.h"
#include "petscsnes.h"

/*
* PETSc callback function, which evaluates the nonlinear
Expand Down Expand Up @@ -150,8 +150,8 @@ SNESSolver::SNESSolver(Options* opts)
.doc("Use matrix free Jacobian?")
.withDefault<bool>(false)),
matrix_free_operator((*options)["matrix_free_operator"]
.doc("Use matrix free Jacobian-vector operator?")
.withDefault<bool>(true)),
.doc("Use matrix free Jacobian-vector operator?")
.withDefault<bool>(true)),
lag_jacobian((*options)["lag_jacobian"]
.doc("Re-use the Jacobian this number of SNES iterations")
.withDefault(50)),
Expand All @@ -160,21 +160,20 @@ SNESSolver::SNESSolver(Options* opts)
.withDefault<bool>(true)),
jacobian_recalculated(false),
prune_jacobian((*options)["prune_jacobian"]
.doc("Remove small elements in the Jacobian?")
.withDefault<bool>(false)),
.doc("Remove small elements in the Jacobian?")
.withDefault<bool>(false)),
prune_abstol((*options)["prune_abstol"]
.doc("Prune values with absolute values smaller than this")
.withDefault<BoutReal>(1e-16)),
prune_fraction((*options)["prune_fraction"]
.doc("Prune if fraction of small elements is larger than this")
.withDefault<BoutReal>(0.2)),
.doc("Prune if fraction of small elements is larger than this")
.withDefault<BoutReal>(0.2)),
scale_rhs((*options)["scale_rhs"]
.doc("Scale time derivatives (Jacobian row scaling)?")
.withDefault<bool>(false)),
scale_vars((*options)["scale_vars"]
.doc("Scale variables (Jacobian column scaling)?")
.withDefault<bool>(false)) {}

.doc("Scale variables (Jacobian column scaling)?")
.withDefault<bool>(false)) {}

int SNESSolver::init() {

Expand Down Expand Up @@ -656,7 +655,8 @@ int SNESSolver::init() {
if (prune_jacobian) {
// Will remove small elements from the Jacobian.
// Save a copy to recover from over-pruning
ierr = MatDuplicate(Jfd, MAT_SHARE_NONZERO_PATTERN, &Jfd_original); CHKERRQ(ierr);
ierr = MatDuplicate(Jfd, MAT_SHARE_NONZERO_PATTERN, &Jfd_original);
CHKERRQ(ierr);
}
} else {
// Brute force calculation
Expand Down Expand Up @@ -808,25 +808,32 @@ int SNESSolver::run() {

// Take ownership of snes_x and var_scaling_factors data
PetscScalar* snes_x_data;
ierr = VecGetArray(snes_x, &snes_x_data); CHKERRQ(ierr);
ierr = VecGetArray(snes_x, &snes_x_data);
CHKERRQ(ierr);
PetscScalar* x1_data;
ierr = VecGetArray(x1, &x1_data); CHKERRQ(ierr);
ierr = VecGetArray(x1, &x1_data);
CHKERRQ(ierr);
PetscScalar* var_scaling_factors_data;
ierr = VecGetArray(var_scaling_factors, &var_scaling_factors_data); CHKERRQ(ierr);
ierr = VecGetArray(var_scaling_factors, &var_scaling_factors_data);
CHKERRQ(ierr);

// Normalise each value in the state
// Limit normalisation so scaling factor is never smaller than rtol
for (int i = 0; i < iend - istart; ++i) {
const PetscScalar norm = BOUTMAX(std::abs(snes_x_data[i]), rtol / var_scaling_factors_data[i]);
const PetscScalar norm =
BOUTMAX(std::abs(snes_x_data[i]), rtol / var_scaling_factors_data[i]);
snes_x_data[i] /= norm;
x1_data[i] /= norm; // Update history for predictor
var_scaling_factors_data[i] *= norm;
}

// Restore vector underlying data
ierr = VecRestoreArray(var_scaling_factors, &var_scaling_factors_data); CHKERRQ(ierr);
ierr = VecRestoreArray(x1, &x1_data); CHKERRQ(ierr);
ierr = VecRestoreArray(snes_x, &snes_x_data); CHKERRQ(ierr);
ierr = VecRestoreArray(var_scaling_factors, &var_scaling_factors_data);
CHKERRQ(ierr);
ierr = VecRestoreArray(x1, &x1_data);
CHKERRQ(ierr);
ierr = VecRestoreArray(snes_x, &snes_x_data);
CHKERRQ(ierr);

if (diagnose) {
// Print maximum and minimum scaling factors
Expand All @@ -842,7 +849,7 @@ int SNESSolver::run() {
}
}
++loop_count;

// Copy the state (snes_x) into initial values (x0)
VecCopy(snes_x, x0);

Expand Down Expand Up @@ -928,7 +935,8 @@ int SNESSolver::run() {
if (diagnose) {
output.write("\nRestoring Jacobian\n");
}
ierr = MatCopy(Jfd_original, Jfd, DIFFERENT_NONZERO_PATTERN); CHKERRQ(ierr);
ierr = MatCopy(Jfd_original, Jfd, DIFFERENT_NONZERO_PATTERN);
CHKERRQ(ierr);
// The non-zero pattern has changed, so update coloring
updateColoring();
jacobian_pruned = false; // Reset flag. Will be set after pruning.
Expand Down Expand Up @@ -1048,8 +1056,8 @@ int SNESSolver::run() {

if (small_elements > prune_fraction * total_elements) {
if (diagnose) {
output.write("\nPruning Jacobian elements: {} / {}\n",
small_elements, total_elements);
output.write("\nPruning Jacobian elements: {} / {}\n", small_elements,
total_elements);
}

// Prune Jacobian, keeping diagonal elements
Expand Down Expand Up @@ -1083,8 +1091,7 @@ int SNESSolver::run() {
// Put the result into variables
if (scale_vars) {
// scaled_x <- snes_x * var_scaling_factors
int ierr = VecPointwiseMult(scaled_x, snes_x
, var_scaling_factors);
int ierr = VecPointwiseMult(scaled_x, snes_x, var_scaling_factors);
CHKERRQ(ierr);

const BoutReal* xdata = nullptr;
Expand Down Expand Up @@ -1324,9 +1331,8 @@ void SNESSolver::updateColoring() {
// Replace the old coloring with the new one
MatFDColoringDestroy(&fdcoloring);
MatFDColoringCreate(Jfd, iscoloring, &fdcoloring);
MatFDColoringSetFunction(fdcoloring,
reinterpret_cast<PetscErrorCode (*)()>(FormFunctionForColoring),
this);
MatFDColoringSetFunction(
fdcoloring, reinterpret_cast<PetscErrorCode (*)()>(FormFunctionForColoring), this);
MatFDColoringSetFromOptions(fdcoloring);
MatFDColoringSetUp(Jfd, iscoloring, fdcoloring);
ISColoringDestroy(&iscoloring);
Expand Down
20 changes: 10 additions & 10 deletions src/solver/impls/snes/snes.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ private:
SNES snes; ///< SNES context
Mat Jmf; ///< Matrix Free Jacobian
Mat Jfd; ///< Finite Difference Jacobian
MatFDColoring fdcoloring {NULL}; ///< Matrix coloring context
///< Jacobian evaluation
MatFDColoring fdcoloring{NULL}; ///< Matrix coloring context
///< Jacobian evaluation

bool use_precon; ///< Use preconditioner
std::string ksp_type; ///< Linear solver type
Expand All @@ -134,27 +134,27 @@ private:
std::string pc_type; ///< Preconditioner type
std::string pc_hypre_type; ///< Hypre preconditioner type
std::string line_search_type; ///< Line search type

bool matrix_free; ///< Use matrix free Jacobian
bool matrix_free_operator; ///< Use matrix free Jacobian in the operator?
int lag_jacobian; ///< Re-use Jacobian
bool use_coloring; ///< Use matrix coloring

bool jacobian_recalculated; ///< Flag set when Jacobian is recalculated
bool prune_jacobian; ///< Remove small elements in the Jacobian?
BoutReal prune_abstol; ///< Prune values with absolute values smaller than this
BoutReal prune_fraction; ///< Prune if fraction of small elements is larger than this
bool jacobian_pruned {false}; ///< Has the Jacobian been pruned?
Mat Jfd_original; ///< Used to reset the Jacobian if over-pruned
void updateColoring(); ///< Updates the coloring using Jfd
bool prune_jacobian; ///< Remove small elements in the Jacobian?
BoutReal prune_abstol; ///< Prune values with absolute values smaller than this
BoutReal prune_fraction; ///< Prune if fraction of small elements is larger than this
bool jacobian_pruned{false}; ///< Has the Jacobian been pruned?
Mat Jfd_original; ///< Used to reset the Jacobian if over-pruned
void updateColoring(); ///< Updates the coloring using Jfd

bool scale_rhs; ///< Scale time derivatives?
Vec rhs_scaling_factors; ///< Factors to multiply RHS function
Vec jac_row_inv_norms; ///< 1 / Norm of the rows of the Jacobian

bool scale_vars; ///< Scale individual variables?
Vec var_scaling_factors; ///< Factors to multiply variables when passing to user
Vec scaled_x; ///< The values passed to the user RHS
Vec scaled_x; ///< The values passed to the user RHS
};

#else
Expand Down

0 comments on commit 393bbe5

Please sign in to comment.