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

Small: Refactor setting linear solver parameters #1105

Merged
merged 3 commits into from
Jun 20, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [[PR 1019]](https://github.com/parthenon-hpc-lab/parthenon/pull/1019) Enable output for non-cell-centered variables

### Changed (changing behavior/API/variables/...)
- [[PR 1105]](https://github.com/parthenon-hpc-lab/parthenon/pull/1105) Refactor parameter input for linear solvers
- [[PR 1078]](https://github.com/parthenon-hpc-lab/parthenon/pull/1078) Add reduction fallback in 1D. Add IndexRange overload for 1D par loops
- [[PR 1024]](https://github.com/parthenon-hpc-lab/parthenon/pull/1024) Add .outN. to history output filenames
- [[PR 1004]](https://github.com/parthenon-hpc-lab/parthenon/pull/1004) Allow parameter modification from an input file for restarts
Expand Down
19 changes: 9 additions & 10 deletions example/poisson_gmg/parthinput.poisson
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ x2max = -0.75
level = 3

<poisson>
solver = BiCGSTAB
precondition = true
precondition_vcycles = 1
restart_threshold = 0.0
solver = BiCGSTAB # or MG
flux_correct = true
smoother = SRJ2
do_FAS = true
diagonal_alpha = 0.0
max_iterations = 15
pre_smooth_iterations = 2
post_smooth_iterations = 2
jacobi_damping = 0.0

x0 = 0.0
y0 = 0.0
z0 = 0.0
radius = 0.5
interior_D = 100.0
exterior_D = 1.0

<poisson/solver_params>
precondition = true
max_iterations = 15
residual_tolerance = 1.e-8
print_per_step = true
smoother = SRJ2
do_FAS = true
49 changes: 2 additions & 47 deletions example/poisson_gmg/poisson_package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,75 +79,30 @@ std::shared_ptr<StateDescriptor> Initialize(ParameterInput *pin) {
int max_poisson_iterations = pin->GetOrAddInteger("poisson", "max_iterations", 10000);
pkg->AddParam<>("max_iterations", max_poisson_iterations);

int pre_smooth_iterations = pin->GetOrAddInteger("poisson", "pre_smooth_iterations", 2);
pkg->AddParam<>("pre_smooth_iterations", pre_smooth_iterations);

int post_smooth_iterations =
pin->GetOrAddInteger("poisson", "post_smooth_iterations", 2);
pkg->AddParam<>("post_smooth_iterations", post_smooth_iterations);

Real diagonal_alpha = pin->GetOrAddReal("poisson", "diagonal_alpha", 0.0);
pkg->AddParam<>("diagonal_alpha", diagonal_alpha);

Real jacobi_damping = pin->GetOrAddReal("poisson", "jacobi_damping", 0.5);
pkg->AddParam<>("jacobi_damping", jacobi_damping);

std::string smoother_method = pin->GetOrAddString("poisson", "smoother", "SRJ2");
pkg->AddParam<>("smoother", smoother_method);

bool do_FAS = pin->GetOrAddBoolean("poisson", "do_FAS", false);
pkg->AddParam<>("do_FAS", do_FAS);

std::string solver = pin->GetOrAddString("poisson", "solver", "MG");
pkg->AddParam<>("solver", solver);

bool precondition = pin->GetOrAddBoolean("poisson", "precondition", true);
pkg->AddParam<>("precondition", precondition);

int precondition_vcycles = pin->GetOrAddInteger("poisson", "precondition_vcycles", 1);
pkg->AddParam<>("precondition_vcycles", precondition_vcycles);

bool flux_correct = pin->GetOrAddBoolean("poisson", "flux_correct", false);
pkg->AddParam<>("flux_correct", flux_correct);

Real restart_threshold = pin->GetOrAddReal("poisson", "restart_threshold", 0.0);
pkg->AddParam<>("restart_threshold", restart_threshold);

int check_interval = pin->GetOrAddInteger("poisson", "check_interval", 100);
pkg->AddParam<>("check_interval", check_interval);

Real err_tol = pin->GetOrAddReal("poisson", "error_tolerance", 1.e-8);
pkg->AddParam<>("error_tolerance", err_tol);

Real res_tol = pin->GetOrAddReal("poisson", "residual_tolerance", 1.e-8);
pkg->AddParam<>("residual_tolerance", res_tol);

bool fail_flag = pin->GetOrAddBoolean("poisson", "fail_without_convergence", false);
pkg->AddParam<>("fail_without_convergence", fail_flag);

bool warn_flag = pin->GetOrAddBoolean("poisson", "warn_without_convergence", true);
pkg->AddParam<>("warn_without_convergence", warn_flag);

bool use_exact_rhs = pin->GetOrAddBoolean("poisson", "use_exact_rhs", false);
pkg->AddParam<>("use_exact_rhs", use_exact_rhs);

PoissonEquation eq;
eq.do_flux_cor = flux_correct;

parthenon::solvers::MGParams mg_params;
mg_params.max_iters = max_poisson_iterations;
mg_params.residual_tolerance = res_tol;
mg_params.do_FAS = do_FAS;
mg_params.smoother = smoother_method;
parthenon::solvers::MGParams mg_params(pin, "poisson/solver_params");
parthenon::solvers::MGSolver<u, rhs, PoissonEquation> mg_solver(pkg.get(), mg_params,
eq);
pkg->AddParam<>("MGsolver", mg_solver, parthenon::Params::Mutability::Mutable);

parthenon::solvers::BiCGSTABParams bicgstab_params;
bicgstab_params.max_iters = max_poisson_iterations;
bicgstab_params.residual_tolerance = res_tol;
bicgstab_params.precondition = precondition;
bicgstab_params.print_per_step = true;
parthenon::solvers::BiCGSTABParams bicgstab_params(pin, "poisson/solver_params");
parthenon::solvers::BiCGSTABSolver<u, rhs, PoissonEquation> bicg_solver(
pkg.get(), bicgstab_params, eq);
pkg->AddParam<>("MGBiCGSTABsolver", bicg_solver,
Expand Down
13 changes: 11 additions & 2 deletions src/solvers/bicgstab_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ namespace solvers {

struct BiCGSTABParams {
MGParams mg_params;
int max_iters = 10;
int max_iters = 1000;
Real residual_tolerance = 1.e-12;
Real restart_threshold = -1.0;
bool precondition = true;
bool print_per_step = false;

BiCGSTABParams() = default;
BiCGSTABParams(ParameterInput *pin, const std::string &input_block) {
max_iters = pin->GetOrAddInteger(input_block, "max_iterations", max_iters);
residual_tolerance =
pin->GetOrAddReal(input_block, "residual_tolerance", residual_tolerance);
precondition = pin->GetOrAddBoolean(input_block, "precondition", precondition);
print_per_step = pin->GetOrAddBoolean(input_block, "print_per_step", print_per_step);
mg_params = MGParams(pin, input_block);
}
};

// The equations class must include a template method
Expand Down
15 changes: 14 additions & 1 deletion src/solvers/mg_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,25 @@ namespace parthenon {
namespace solvers {

struct MGParams {
int max_iters = 10;
int max_iters = 1000;
Real residual_tolerance = 1.e-12;
bool do_FAS = true;
std::string smoother = "SRJ2";
bool two_by_two_diagonal = false;
int max_coarsenings = std::numeric_limits<int>::max();

MGParams() = default;
MGParams(ParameterInput *pin, const std::string &input_block) {
max_iters = pin->GetOrAddInteger(input_block, "max_iterations", max_iters);
residual_tolerance =
pin->GetOrAddReal(input_block, "residual_tolerance", residual_tolerance);
do_FAS = pin->GetOrAddBoolean(input_block, "do_FAS", do_FAS);
smoother = pin->GetOrAddString(input_block, "smoother", smoother);
two_by_two_diagonal =
pin->GetOrAddBoolean(input_block, "two_by_two_diagonal", two_by_two_diagonal);
max_coarsenings =
pin->GetOrAddInteger(input_block, "max_coarsenings", max_coarsenings);
}
};

// The equations class must include a template method
Expand Down
12 changes: 8 additions & 4 deletions tst/regression/test_suites/poisson_gmg/parthinput.poisson
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ level = 3

<poisson>
solver = MG
smoother = SRJ2
do_FAS = true
residual_tolerance = 1.e-12
max_iterations = 15
flux_correct = false
use_exact_rhs = true

Expand All @@ -70,3 +66,11 @@ radius = 0.5
diagonal_alpha = 0.0
interior_D = 1.0
exterior_D = 1.0

<poisson/solver_params>
precondition = true
max_iterations = 15
residual_tolerance = 1.e-12
print_per_step = true
smoother = SRJ2
do_FAS = true
Loading