Wire typed experimental preconditioners into Newton diagnostics

This commit is contained in:
2026-09-17 17:05:44 -04:00
parent dc8d4b0501
commit fcb7833844
2 changed files with 123 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ import mean_field;
#include "plateau_physical_diagnostics.hpp"
#include "plateau_preconditioner_diagnostics.hpp"
#include "plateau_preconditioner_benchmark.hpp"
#include "plateau_newton_preconditioner.hpp"
#include "plateau_pressure_obstruction_diagnostics.hpp"
#include "plateau_shape_curvature_diagnostics.hpp"
#include "plateau_numerical_strategy_diagnostics.hpp"
@@ -151,6 +152,23 @@ Options Parse(int argc, char **argv) {
if (!std::isfinite(o.linearTolerance) || o.linearTolerance <= 0.0)
throw std::invalid_argument("A preconditioner benchmark requires a finite positive --linear-tol");
}
if (!o.preconditionerProbe) {
if (o.preconditionerVariant != "independent" && o.preconditionerVariant != "gravity-schur" &&
o.preconditionerVariant != "stellar-ldu")
throw std::invalid_argument("Newton preconditioner variants are independent, gravity-schur, or stellar-ldu");
if (o.preconditionerVariant != "independent") {
if (o.probes || o.pressureProbe || o.shapeCurvature || o.surfaceTrace || o.strategyProbe ||
o.positivityProbe || o.positivityCertificate || !o.probeState.empty() || !o.probeDirection.empty() ||
!o.replayCandidate.empty())
throw std::invalid_argument("A custom Newton preconditioner cannot be combined with frozen or certificate probes");
if (o.strategy != "baseline" && o.strategy != "positivity" && o.strategy != "curvature" &&
o.strategy != "positivity-curvature")
throw std::invalid_argument("Custom Newton preconditioners support baseline, positivity, curvature, or positivity-curvature");
if (!std::isfinite(o.linearTolerance) || o.linearTolerance <= 0.0 || o.schurCorrectionSweeps < 0 ||
!std::isfinite(o.schurRelaxation) || o.schurRelaxation <= 0.0)
throw std::invalid_argument("Invalid custom Newton preconditioner controls");
}
}
if ((!o.probeDirection.empty() || !o.replayCandidate.empty()) && o.probeState.empty())
throw std::invalid_argument("A supplied direction or candidate requires --probe-state");
if (o.strategy != "baseline" && o.strategy != "positivity" && o.strategy != "curvature" &&
@@ -410,7 +428,8 @@ void FrozenProbes(State &s, const fem::FEM &f, const Options &o, const mfem::Vec
}
}
template <class Context> int RunContext(Context &context, const Options &o, const Scales &scales) {
template <bool FullDefaultStack, class Context>
int RunContext(Context &context, const Options &o, const Scales &scales) {
std::vector<operators::RootBlockDescriptor> rows, values;
mfem::Vector residualFactors, stateFactors;
std::vector<int> centralRows;
@@ -428,10 +447,14 @@ template <class Context> int RunContext(Context &context, const Options &o, cons
<< "\ngravity_iterations=" << o.gravityIterations << "\ngravity_restart=" << o.gravityRestart
<< "\npreconditioner_probe=" << o.preconditionerProbe
<< "\npreconditioner_variant=" << o.preconditionerVariant
<< "\ncontext_preconditioner_variant=" << (o.preconditionerProbe ? "independent" : o.preconditionerVariant)
<< "\ncontext_gravity_schur_sweeps="
<< (!o.preconditionerProbe && o.preconditionerVariant == "gravity-schur" ? o.schurCorrectionSweeps : 0)
<< "\nschur_correction_sweeps=" << o.schurCorrectionSweeps << "\nschur_relaxation=" << o.schurRelaxation
<< "\ncompiler=" << __VERSION__ << "\ncompiled=" << __DATE__ << ' ' << __TIME__ << '\n';
metadata.flush();
if (o.preconditionerProbe) {
// Custom Newton recipes do not instantiate the nine-variant frozen suite.
if constexpr (FullDefaultStack) if (o.preconditionerProbe) {
metadata << "benchmark_input=" << (o.probeState.empty() ? "lane_emden_seed" : "physical_snapshot") << '\n';
if (!o.probeState.empty()) metadata << "probe_state=" << o.probeState << '\n';
solver::detail::StellarEquilibriumContextDiagnostics::WithState(context, [&](auto &s, const fem::FEM &) {
@@ -456,7 +479,7 @@ template <class Context> int RunContext(Context &context, const Options &o, cons
// the experiment. The CSV retains both recurrence and true gate results.
return 0;
}
if (!o.probeState.empty()) {
if constexpr (FullDefaultStack) if (!o.probeState.empty()) {
if (o.maxNewton != 0)
throw std::invalid_argument("--probe-state requires --max-newton 0");
metadata << "probe_state=" << o.probeState << '\n';
@@ -677,9 +700,11 @@ template <class Context> int RunContext(Context &context, const Options &o, cons
auto report = [&] {
if (o.strategy == "positivity") return run(nl::NewtonNumerics{nl::PolynomialEnthalpy{}, nl::MonolithicGravity{}});
if (o.strategy == "curvature") return run(nl::NewtonNumerics{nl::SampledEnthalpy{}, nl::GravityCurvatureCorrection{gravity}});
if (o.strategy == "reduced") return run(nl::NewtonNumerics{nl::SampledEnthalpy{}, nl::EliminatedGravity{gravity}});
if constexpr (FullDefaultStack)
if (o.strategy == "reduced") return run(nl::NewtonNumerics{nl::SampledEnthalpy{}, nl::EliminatedGravity{gravity}});
if (o.strategy == "positivity-curvature") return run(nl::NewtonNumerics{nl::PolynomialEnthalpy{}, nl::GravityCurvatureCorrection{gravity}});
if (o.strategy == "positivity-reduced") return run(nl::NewtonNumerics{nl::PolynomialEnthalpy{}, nl::EliminatedGravity{gravity}});
if constexpr (FullDefaultStack)
if (o.strategy == "positivity-reduced") return run(nl::NewtonNumerics{nl::PolynomialEnthalpy{}, nl::EliminatedGravity{gravity}});
return run(nl::NewtonNumerics{});
}();
const auto &strategyDiagnostics = report.diagnostics().numericalStrategies;
@@ -723,7 +748,7 @@ template <class Context> int RunContext(Context &context, const Options &o, cons
sh << (j ? "," : "") << schur(i, j);
sh << '\n';
}
if (o.probes) {
if constexpr (FullDefaultStack) if (o.probes) {
experiment::plateau_diagnostics::InspectPreconditioner(s, o.output);
FrozenProbes(s, f, o);
}
@@ -733,10 +758,29 @@ template <class Context> int RunContext(Context &context, const Options &o, cons
template <class Model> int Setup(Model model, fem::FEM f, const Options &o, const Scales &scales) {
auto d = equilibrium::makeStellarDiscretization(
std::move(f), normalization::PhysicalRieszDiagonal{dimensions::LengthValue{1.}, utils::G});
auto context = solver::makeContext(std::move(model), std::move(d), preconditioning::makePreconditioner(),
solver::linear::FGMRES({.restartLength = o.restart, .printLevel = -1}),
seed::LaneEmden({.centralDensity = dimensions::DensityValue{scales.rho}}));
return RunContext(context, o, scales);
const auto withRecipe = [&](auto recipe) {
using Recipe = decltype(recipe);
using Problem = equilibrium::StellarEquilibriumProblem<Model, decltype(d)>;
static_assert(preconditioning::StellarPreconditionerRuntimeAvailableFor<Recipe, Problem>);
using Inverse = preconditioning::PreparedStellarInverseType<Recipe, Problem>;
static_assert(solver::LinearPreconditionerApplicationContractAvailable<Inverse>);
static_assert(solver::linearPreconditionerApplicationContract<Inverse> ==
preconditioning::ApplicationContract::stationary_linear);
auto context = solver::makeContext(std::move(model), std::move(d), std::move(recipe),
solver::linear::FGMRES({.restartLength = o.restart, .printLevel = -1}),
seed::LaneEmden({.centralDensity = dimensions::DensityValue{scales.rho}}));
constexpr bool fullDefaultStack = std::same_as<Recipe, preconditioning::DefaultStellarPreconditioner>;
return RunContext<fullDefaultStack>(context, o, scales);
};
if (o.preconditionerProbe || o.preconditionerVariant == "independent")
return withRecipe(preconditioning::makePreconditioner());
if (o.preconditionerVariant == "gravity-schur")
return withRecipe(experiment::plateau_diagnostics::GravitySchurNewtonPreconditioner{
{.correctionSweeps = o.schurCorrectionSweeps, .relaxation = o.schurRelaxation}});
if (o.preconditionerVariant == "stellar-ldu")
return withRecipe(experiment::plateau_diagnostics::StellarLDUNewtonPreconditioner{
{.correctionSweeps = 0, .relaxation = 1.0}});
throw std::logic_error("An unsupported Newton preconditioner bypassed option validation.");
}
int Run(const Options &o) {
const auto scales = Derive(o.n);