Skip to content
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

Improving backward Euler solver #2631

Open
wants to merge 147 commits into
base: next
Choose a base branch
from
Open

Improving backward Euler solver #2631

wants to merge 147 commits into from

Conversation

bendudson
Copy link
Contributor

Attempting to improve the steady state (beuler / snes) solver and address #2624

So far:

  • If scale_rhs = true then the approximate Jacobian calculated by PETSc coloring is used to scale the time derivatives (RHS) by row scaling the Jacobian. This results in pseudo-timestepping, since every equation is effectively using a different timestep. Default is off (false)

If scale_rhs = true then the approximate Jacobian calculated by
PETSc coloring is used to scale the time derivatives (RHS) by row
scaling the Jacobian. This results in pseudo-timestepping, since every
equation is effectively using a different timestep.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

if ((err != 0) or (ctx == nullptr)) {
return err;
}

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 'fctx' is not initialized [cppcoreguidelines-init-variables]

Suggested change
SNESSolver* fctx = nullptr;

}

// Get the the SNESSolver pointer from the function call context
SNESSolver* fctx;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

                                 reinterpret_cast<void**>(&fctx));
                                 ^


// Call the SNESSolver function
return fctx->scaleJacobian(B);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: constructor does not initialize these fields: dt, nlocal, neq, snes_f, snes_x, x0, delta_x, x1, snes, Jmf, fdcoloring, rhs_scaling_factors, jac_row_inv_norms [cppcoreguidelines-pro-type-member-init]

src/solver/impls/snes/snes.hxx:94:

-   BoutReal dt;       ///< Current timestep used in snes_function
+   BoutReal dt{};       ///< Current timestep used in snes_function

src/solver/impls/snes/snes.hxx:109:

-   int nlocal; ///< Number of variables on local processor
-   int neq;    ///< Number of variables in total
+   int nlocal{}; ///< Number of variables on local processor
+   int neq{};    ///< Number of variables in total

src/solver/impls/snes/snes.hxx:116:

-   Vec snes_f;   ///< Used by SNES to store function
-   Vec snes_x;   ///< Result of SNES
-   Vec x0;       ///< Solution at start of current timestep
-   Vec delta_x;  ///< Change in solution
+   Vec snes_f{};   ///< Used by SNES to store function
+   Vec snes_x{};   ///< Result of SNES
+   Vec x0{};       ///< Solution at start of current timestep
+   Vec delta_x{};  ///< Change in solution

src/solver/impls/snes/snes.hxx:122:

-   Vec x1;               ///< Previous solution
+   Vec x1{};               ///< Previous solution

src/solver/impls/snes/snes.hxx:125:

-   SNES snes;                ///< SNES context
-   Mat Jmf;                  ///< Matrix-free Jacobian
-   MatFDColoring fdcoloring; ///< Matrix coloring context, used for finite difference
+   SNES snes{};                ///< SNES context
+   Mat Jmf{};                  ///< Matrix-free Jacobian
+   MatFDColoring fdcoloring{}; ///< Matrix coloring context, used for finite difference

src/solver/impls/snes/snes.hxx:141:

-   Vec rhs_scaling_factors;  ///< Factors to multiply RHS function
-   Vec jac_row_inv_norms;    ///< 1 / Norm of the rows of the Jacobian
+   Vec rhs_scaling_factors{};  ///< Factors to multiply RHS function
+   Vec jac_row_inv_norms{};    ///< 1 / Norm of the rows of the Jacobian

src/solver/impls/snes/snes.cxx Show resolved Hide resolved
if (!scale_rhs) {
return 0; // Not scaling the RHS values
}

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 'ierr' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1025:

- r;
+ r = 0;


// Calculate the norm of each row of the Jacobian
PetscScalar* row_inv_norm_data;
ierr = VecGetArray(jac_row_inv_norms, &row_inv_norm_data);
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 'ncols' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1042:

- cols;
+ cols = 0;

PetscScalar* row_inv_norm_data;
ierr = VecGetArray(jac_row_inv_norms, &row_inv_norm_data);
CHKERRQ(ierr);

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 'vals' is not initialized [cppcoreguidelines-init-variables]

Suggested change
vals = nullptr;

Comment on lines +1073 to +1074

if (diagnose) {
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
if (diagnose) {
ctors
calePetscReal max_scale;
PetscReal min_scale;

// Modify the RHS scaling: factor = factor / norm
ierr = VecPointwiseMult(rhs_scaling_factors, rhs_scaling_factors, jac_row_inv_norms);
CHKERRQ(ierr);

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 'max_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                    ^

this fix will not be applied because it overlaps with another fix

// Modify the RHS scaling: factor = factor / norm
ierr = VecPointwiseMult(rhs_scaling_factors, rhs_scaling_factors, jac_row_inv_norms);
CHKERRQ(ierr);

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 'min_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                               ^

this fix will not be applied because it overlaps with another fix

bendudson and others added 3 commits January 13, 2023 14:36
Removes function PCGAMGSetSymGraph, so symmetry should be set though
matrix options.
If nproc > 1 then the local index offset should only be added to
points inside the domain.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

}

// Get the the SNESSolver pointer from the function call context
SNESSolver* fctx;
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 'fctx' is not initialized [cppcoreguidelines-init-variables]

Suggested change
SNESSolver* fctx;
SNESSolver* fctx = nullptr;

src/solver/impls/snes/snes.cxx Show resolved Hide resolved
src/solver/impls/snes/snes.cxx Show resolved Hide resolved
int ierr;

// Get index of rows owned by this processor
int rstart, rend;
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 'ierr' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1030:

- err;
+ err = 0;

Comment on lines +1032 to +1033

// Check that the vector has the same ownership range
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
// Check that the vector has the same ownership range
essor
reint rstart;
int rend;


PetscInt ncols;
const PetscScalar* vals;
for (int row = rstart; row < rend; row++) {
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 'ncols' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1048:

- cols;
+ cols = 0;

const PetscScalar* vals;
for (int row = rstart; row < rend; row++) {
MatGetRow(B, row, &ncols, nullptr, &vals);

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 'vals' is not initialized [cppcoreguidelines-init-variables]

Suggested change
vals = nullptr;

Comment on lines +1079 to +1080
VecMin(rhs_scaling_factors, nullptr, &min_scale);
output.write("RHS scaling: {} -> {}\n", min_scale, max_scale);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
VecMin(rhs_scaling_factors, nullptr, &min_scale);
output.write("RHS scaling: {} -> {}\n", min_scale, max_scale);
ctors
calePetscReal max_scale;
PetscReal min_scale;

// Print maximum and minimum scaling factors
PetscReal max_scale, min_scale;
VecMax(rhs_scaling_factors, nullptr, &max_scale);
VecMin(rhs_scaling_factors, nullptr, &min_scale);
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 'max_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                    ^

this fix will not be applied because it overlaps with another fix

// Print maximum and minimum scaling factors
PetscReal max_scale, min_scale;
VecMax(rhs_scaling_factors, nullptr, &max_scale);
VecMin(rhs_scaling_factors, nullptr, &min_scale);
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 'min_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                               ^

this fix will not be applied because it overlaps with another fix

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

src/solver/impls/snes/snes.cxx Show resolved Hide resolved
}

int ierr;

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 'ierr' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1028:

- err;
+ err = 0;

Comment on lines +1030 to +1031
int rstart, rend;
MatGetOwnershipRange(B, &rstart, &rend);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
int rstart, rend;
MatGetOwnershipRange(B, &rstart, &rend);
essor
reint rstart;
int rend;

int ierr;

// Get index of rows owned by this processor
int rstart, rend;
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 'rstart' is not initialized [cppcoreguidelines-init-variables]

essor
            ^

this fix will not be applied because it overlaps with another fix


// Get index of rows owned by this processor
int rstart, rend;
MatGetOwnershipRange(B, &rstart, &rend);
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 'rend' is not initialized [cppcoreguidelines-init-variables]

rend;
^

this fix will not be applied because it overlaps with another fix

ierr = VecGetArray(jac_row_inv_norms, &row_inv_norm_data);
CHKERRQ(ierr);

PetscInt ncols;
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 'ncols' is not initialized [cppcoreguidelines-init-variables]

src/solver/impls/snes/snes.cxx:1046:

- cols;
+ cols = 0;


PetscInt ncols;
const PetscScalar* vals;
for (int row = rstart; row < rend; row++) {
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 'vals' is not initialized [cppcoreguidelines-init-variables]

Suggested change
for (int row = rstart; row < rend; row++) {
vals = nullptr;

Comment on lines +1077 to +1078
PetscReal max_scale, min_scale;
VecMax(rhs_scaling_factors, nullptr, &max_scale);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: multiple declarations in a single statement reduces readability [readability-isolate-declaration]

Suggested change
PetscReal max_scale, min_scale;
VecMax(rhs_scaling_factors, nullptr, &max_scale);
ctors
calePetscReal max_scale;
PetscReal min_scale;


if (diagnose) {
// Print maximum and minimum scaling factors
PetscReal max_scale, min_scale;
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 'max_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                    ^

this fix will not be applied because it overlaps with another fix


if (diagnose) {
// Print maximum and minimum scaling factors
PetscReal max_scale, min_scale;
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 'min_scale' is not initialized [cppcoreguidelines-init-variables]

ctors
                               ^

this fix will not be applied because it overlaps with another fix

dschwoerer and others added 29 commits May 3, 2023 09:43
The parallel transform already checks whether it is needed.

For the identity transform this is always needed, so the second check is
harmful in that case, and otherwise not needed.
Remove unnecessary check for aligned fields when applying twist-shift
Physics models often modify metric tensor components during
initialisation (e.g. normalisation), so get their values
for saving to output only after init has been called.
Get mesh outputs after physics init
No mallocs now needed when setting matrix elements in serial or
parallel for 1D-recycling example.

Hypre preconditioner type can now be set with pc_hypre_type option.
Defaults to pilut, as this seems to give the best performance in
parallel.
cvode_nonlinear_convergence_coef
    Sets parameter through call to CVodeSetNonlinConvCoef
    https://sundials.readthedocs.io/en/latest/cvodes/Usage/SIM.html#c.CVodeSetNonlinConvCoef

cvode_linear_convergence_coef
    Sets parameter through call to CVodeSetEpsLin
    https://sundials.readthedocs.io/en/latest/cvodes/Usage/SIM.html#c.CVodeSetEpsLin
Add some documentation of new cvode and beuler solver options.
If PETSc is not configured with Hypre then PCHYPRESetType is
not defined.
If a step fails, re-calculate the Jacobian and preconditioner.
This can be an expensive operation, so doing this on failure
allows lag_jacobian to be increased.
Enables `petsc:log_view=true` option to set the PETSc `-log_view`
switch correctly.
They are not used anyway
Reverts some of the deling commits and changes _runs to _compiles
Co-authored-by: David Bold <dschwoerer@users.noreply.github.com>
Co-authored-by: David Bold <dschwoerer@users.noreply.github.com>
Co-authored-by: David Bold <dschwoerer@users.noreply.github.com>
Do not try to run SLEPc or PETSc in configure
Update `make dist` invocation

[skip ci]
mesh:paralleltransform is a section

[skip ci]
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2023

clang-tidy review says "All clean, LGTM! 👍"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants