feat(libmeanfield): variadic refactor

also added normaliztion operator
This commit is contained in:
2026-09-06 10:15:00 -04:00
parent 71423d543f
commit 76818f2f82
63 changed files with 28794 additions and 1119 deletions

View File

@@ -71,6 +71,10 @@ namespace preconditioning_runtime_test {
++m_snapshot.geometry.revision;
}
void AdvancePreparation() noexcept {
++m_snapshot.preparedOperatorGeneration;
}
void SetPrepared(const bool prepared) noexcept {
m_prepared = prepared;
}
@@ -108,6 +112,136 @@ namespace preconditioning_runtime_test {
};
} // namespace preconditioning_runtime_test
namespace unsupported_physical_preconditioner_test {
class Constraint;
class PreparedConstraint;
class Constraint final {
public:
struct Parameters final {
mean_field::dimensions::SpecificEnthalpyValue target;
};
using TargetValue = mean_field::dimensions::SpecificEnthalpyValue;
using ScalarDescription = mean_field::stellar::ScalarConstraint<
mean_field::dimensions::quantity::SpecificEnthalpy,
mean_field::dimensions::quantity::Dimensionless,
mean_field::dimensions::quantity::SpecificEnthalpy,
"test.unsupported_physical_edge.coordinate",
"q_u",
"test.unsupported_physical_edge.residual",
"R_u">;
using ModelDefinition = mean_field::constraint::ScalarPhaseCondition<
Constraint,
"UnsupportedPhysicalPreconditionerEdge",
mean_field::stellar::Reads<
mean_field::stellar::state::SpecificEnthalpy,
mean_field::stellar::state::OwnGeneratedCoordinate>,
mean_field::stellar::Changes<
mean_field::stellar::equation::PoissonEquation>,
ScalarDescription>;
using EquilibriumPhysics =
mean_field::operators::LocalSpecificationEquilibriumPhysics<
PreparedConstraint>;
explicit constexpr Constraint(const Parameters parameters) noexcept
: m_target(parameters.target) {
}
[[nodiscard]] constexpr TargetValue target() const noexcept {
return m_target;
}
private:
TargetValue m_target;
};
struct PreparationReport final {
bool stateChanged{true};
};
class PreparedConstraint final {
public:
using Report = PreparationReport;
explicit PreparedConstraint(const Constraint &) noexcept {
}
template <typename StateView>
[[nodiscard]] Report PrepareAfterPhysical(const StateView &) noexcept {
m_isPrepared = true;
return {};
}
template <typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddResidual(
mean_field::stellar::equation::OwnConstraint,
Row &
) const noexcept {
return mean_field::stellar::structuralZero;
}
template <typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddResidual(
mean_field::stellar::equation::PoissonEquation,
Row &
) const noexcept {
return mean_field::stellar::structuralZero;
}
template <typename Direction, typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddJacobianAction(
mean_field::stellar::Derivative<
mean_field::stellar::equation::OwnConstraint,
mean_field::stellar::state::SpecificEnthalpy>,
const Direction &,
Row &
) const noexcept {
return mean_field::stellar::zeroDerivative;
}
template <typename Direction, typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddJacobianAction(
mean_field::stellar::Derivative<
mean_field::stellar::equation::OwnConstraint,
mean_field::stellar::state::OwnGeneratedCoordinate>,
const Direction &,
Row &
) const noexcept {
return mean_field::stellar::zeroDerivative;
}
template <typename Direction, typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddJacobianAction(
mean_field::stellar::Derivative<
mean_field::stellar::equation::PoissonEquation,
mean_field::stellar::state::SpecificEnthalpy>,
const Direction &,
Row &
) const noexcept {
return mean_field::stellar::zeroDerivative;
}
template <typename Direction, typename Row>
[[nodiscard]] mean_field::stellar::StructuralZero AddJacobianAction(
mean_field::stellar::Derivative<
mean_field::stellar::equation::PoissonEquation,
mean_field::stellar::state::OwnGeneratedCoordinate>,
const Direction &,
Row &
) const noexcept {
return mean_field::stellar::zeroDerivative;
}
[[nodiscard]] bool IsPrepared() const noexcept {
return m_isPrepared;
}
private:
bool m_isPrepared{false};
};
} // namespace unsupported_physical_preconditioner_test
template <> struct mean_field::preconditioning::StellarEquilibriumProblemTraits<preconditioning_runtime_test::Problem> {
using Problem = preconditioning_runtime_test::Problem;
using Form = preconditioning_runtime_test::Form;
@@ -146,14 +280,40 @@ namespace {
namespace blocks = mean_field::utils::blocks;
namespace preconditioning = mean_field::preconditioning;
using ModelWithoutPhase = mean_field::operators::StellarEquilibriumSpecificationModel;
using CentralDensityModel = mean_field::operators::CentralDensityStellarEquilibriumSpecificationModel;
using ModelWithoutPhase = mean_field::model::StellarModel<mean_field::models::SpecificationSet<
mean_field::eos::Polytrope,
mean_field::surface::Isobaric,
mean_field::models::FixedTotalMass>>;
using CentralDensityModel = mean_field::model::StellarModel<mean_field::models::SpecificationSet<
mean_field::eos::Polytrope,
mean_field::surface::Isobaric,
mean_field::models::FixedTotalMass,
mean_field::models::FixedCentralDensity>>;
using AngularMomentumModel = mean_field::model::StellarModel<mean_field::models::SpecificationSet<
mean_field::eos::Polytrope,
mean_field::surface::Isobaric,
mean_field::models::FixedTotalMass,
mean_field::models::FixedAngularMomentum>>;
using ProvenZeroPhysicalEdgeModel = mean_field::model::StellarModel<
mean_field::models::SpecificationSet<
mean_field::eos::Polytrope,
mean_field::surface::Isobaric,
mean_field::models::FixedTotalMass,
unsupported_physical_preconditioner_test::Constraint>>;
using ProblemWithoutPhase = mean_field::equilibrium::StellarEquilibriumProblem<ModelWithoutPhase>;
using CentralDensityProblem = mean_field::equilibrium::StellarEquilibriumProblem<CentralDensityModel>;
using AngularMomentumProblem = mean_field::equilibrium::StellarEquilibriumProblem<AngularMomentumModel>;
using ProvenZeroPhysicalEdgeProblem =
mean_field::equilibrium::StellarEquilibriumProblem<ProvenZeroPhysicalEdgeModel>;
using PlanWithoutPhase = preconditioning::IdentityPreconditionerPlanFor<ProblemWithoutPhase>;
using CentralDensityPlan = preconditioning::IdentityPreconditionerPlanFor<CentralDensityProblem>;
template <typename Problem>
concept CanMakeDefaultStellarStructureBlock = requires(const Problem &problem) {
preconditioning::stellarStructureBlock(problem);
};
using RefreshingDensityIdentity = preconditioning::ComponentDeclaration<
blocks::type_list<blocks::density::mass::value>,
blocks::type_list<blocks::density::mass::residual>,
@@ -203,6 +363,73 @@ TEST_CASE(
STATIC_CHECK(CentralDensityPlan::ComponentTypes::size == 7);
}
TEST_CASE(
"Default Stellar Structure Availability Distinguishes Proven Zeros From Unhandled Physical Edges",
"[preconditioning][stellar_structure][type_contract][compiler]"
) {
using BaseCompilation =
mean_field::operators::CompiledStellarEquilibriumSystem<ModelWithoutPhase>;
using BaseSupport =
preconditioning::DefaultStellarStructurePhysicalTopologySupport<
ModelWithoutPhase>;
using ProvenZeroSupport =
preconditioning::DefaultStellarStructurePhysicalTopologySupport<
ProvenZeroPhysicalEdgeModel>;
using TrustedFixedMassEdge =
mean_field::operators::StellarEquilibriumJacobianCoupling<
blocks::enthalpy::specific::residual,
blocks::density::mass::value>;
using ProvenZeroPoissonEnthalpyEdge =
mean_field::operators::StellarEquilibriumJacobianCoupling<
blocks::gravity::poisson::residual,
blocks::enthalpy::specific::value>;
// FixedTotalMass contributes h <- rho outside the generic five-field base
// graph. It remains supported because that specification is explicitly
// embedded in the trusted numerical core, not because of a model-pack
// special case.
STATIC_CHECK_FALSE(mean_field::utils::blocks::contains_type_v<
TrustedFixedMassEdge,
typename BaseCompilation::BaseJacobianCouplings>);
STATIC_CHECK(mean_field::utils::blocks::contains_type_v<
TrustedFixedMassEdge,
typename BaseCompilation::ContributionJacobianCouplings>);
STATIC_CHECK(BaseSupport::UnsupportedCouplings::size == 0);
STATIC_CHECK(preconditioning::DefaultStellarStructurePhysicalTopologySupportedFor<
ModelWithoutPhase>);
STATIC_CHECK(preconditioning::DefaultStellarStructurePhysicalTopologySupportedFor<
CentralDensityModel>);
STATIC_CHECK(preconditioning::DefaultStellarStructurePhysicalTopologySupportedFor<
AngularMomentumModel>);
STATIC_CHECK(preconditioning::StellarStructurePreconditionerProblem<
ProblemWithoutPhase>);
STATIC_CHECK(CanMakeDefaultStellarStructureBlock<ProblemWithoutPhase>);
STATIC_CHECK(preconditioning::DefaultStellarPreconditionerAvailableFor<
ProblemWithoutPhase>);
// The mock's novel Poisson <- enthalpy edge is absent from the structure
// backend, but its exact nested provider returns StructuralZero. That is
// a compile-time proof that no preconditioner term is missing; generated-
// coordinate edges are handled independently by the inferred border.
STATIC_CHECK(mean_field::equilibrium::StellarEquilibriumModel<
ProvenZeroPhysicalEdgeModel>);
STATIC_CHECK(mean_field::equilibrium::DiscretizedStellarEquilibriumProblem<
ProvenZeroPhysicalEdgeProblem>);
STATIC_CHECK(ProvenZeroSupport::UnsupportedCouplings::size == 0);
STATIC_CHECK(mean_field::utils::blocks::contains_type_v<
ProvenZeroPoissonEnthalpyEdge,
typename mean_field::operators::CompiledStellarEquilibriumSystem<
ProvenZeroPhysicalEdgeModel>::ContributionJacobianCouplings>);
STATIC_CHECK(preconditioning::DefaultStellarStructurePhysicalTopologySupportedFor<
ProvenZeroPhysicalEdgeModel>);
STATIC_CHECK(preconditioning::StellarStructurePreconditionerProblem<
ProvenZeroPhysicalEdgeProblem>);
STATIC_CHECK(CanMakeDefaultStellarStructureBlock<
ProvenZeroPhysicalEdgeProblem>);
STATIC_CHECK(preconditioning::DefaultStellarPreconditionerAvailableFor<
ProvenZeroPhysicalEdgeProblem>);
}
TEST_CASE(
"Prepared Stellar Identity Preconditioning Is Bitwise Equivalent To The P0 Baseline",
tags::preconditioning_runtime_unit
@@ -286,6 +513,14 @@ TEST_CASE(
CHECK_FALSE(geometryRefresh.changes.linearization);
CHECK(preconditioner.GetStatistics().refreshes == 2);
problem.AdvancePreparation();
CHECK_FALSE(preconditioner.IsCurrent());
const auto preparationRefresh = preconditioner.Refresh();
CHECK(preparationRefresh.changes.linearization);
CHECK_FALSE(preparationRefresh.changes.geometry);
CHECK(preconditioner.IsCurrent());
CHECK(preconditioner.GetStatistics().refreshes == 3);
problem.SetPrepared(false);
CHECK_FALSE(preconditioner.IsCurrent());
CHECK_THROWS_AS(preconditioner.Refresh(), std::logic_error);