SERiF 0.0.1a
3+1D Stellar Structure and Evolution
|
Solves the Lane-Emden equation for a polytropic star using a mixed finite element method. More...
#include <polySolver.h>
Public Member Functions | |
PolySolver (const double n, const double order) | |
~PolySolver () | |
Destructor for PolySolver. | |
void | solve () const |
Solves the polytropic system. | |
double | getN () const |
Gets the polytropic index ![]() | |
double | getOrder () const |
Gets the finite element order used for the ![]() | |
mfem::Mesh & | getMesh () const |
Gets a reference to the computational mesh. | |
mfem::GridFunction & | getTheta () const |
Gets a reference to the solution grid function for ![]() | |
mfem::GridFunction & | getPhi () const |
Gets a reference to the solution grid function for ![]() | |
Private Member Functions | |
PolySolver (mfem::Mesh &mesh, double n, double order) | |
Private constructor that takes an existing mesh. | |
void | assembleBlockSystem () |
Assembles the block system operator m_polytropOperator . | |
mfem::Array< int > | computeBlockOffsets () const |
Compute the block offsets for the operator. These are the offsets that define which dofs belong to which variable. | |
std::unique_ptr< formBundle > | buildIndividualForms (const mfem::Array< int > &blockOffsets) |
Build the individual forms for the block operator (M, Q, D, S, and f). | |
serif::types::MFEMArrayPairSet | getEssentialTrueDof () const |
Computes the essential true degrees of freedom (DOFs) and their values for boundary conditions. | |
std::pair< mfem::Array< int >, mfem::Array< int > > | findCenterElement () const |
Finds the degrees of freedom (DOFs) associated with the geometric center (origin) of the mesh. | |
void | setInitialGuess () const |
Sets the initial guess for the solution variables ![]() ![]() | |
void | saveAndViewSolution (const mfem::BlockVector &state_vector) const |
Saves the 1D radial solution and optionally displays the 2D/3D solution using GLVis. | |
solverBundle | setupNewtonSolver () const |
Sets up the Newton solver and its associated linear solver (GMRES). | |
void | setOperatorEssentialTrueDofs () const |
Sets the essential true DOFs on the PolytropeOperator . | |
void | LoadSolverUserParams (double &newtonRelTol, double &newtonAbsTol, int &newtonMaxIter, int &newtonPrintLevel, double &gmresRelTol, double &gmresAbsTol, int &gmresMaxIter, int &gmresPrintLevel) const |
Loads Newton and GMRES solver parameters from the configuration. | |
Static Private Member Functions | |
static mfem::Mesh & | prepareMesh (double n) |
Prepares the mesh for the simulation. | |
static void | assembleAndFinalizeForm (auto &f) |
Assemble and finalize a given MFEM form. | |
static void | GetDofCoordinates (const mfem::FiniteElementSpace &fes, const std::string &filename) |
Utility function to get and save the coordinates of degrees of freedom for a finite element space. | |
Private Attributes | |
serif::config::Config & | m_config |
Reference to the global configuration manager instance. | |
serif::probe::LogManager & | m_logManager |
Reference to the global log manager instance. | |
quill::Logger * | m_logger |
Pointer to the specific logger instance for this class. | |
double | m_polytropicIndex |
The polytropic index ![]() | |
double | m_feOrder |
The polynomial order for H1 elements ( ![]() ![]() m_feOrder - 1 . | |
mfem::Mesh & | m_mesh |
Reference to the computational mesh (owned by ResourceManager). | |
std::unique_ptr< mfem::H1_FECollection > | m_fecH1 |
Finite Element Collection for ![]() | |
std::unique_ptr< mfem::RT_FECollection > | m_fecRT |
Finite Element Collection for ![]() | |
std::unique_ptr< mfem::FiniteElementSpace > | m_feTheta |
Finite Element Space for ![]() | |
std::unique_ptr< mfem::FiniteElementSpace > | m_fePhi |
Finite Element Space for ![]() | |
std::unique_ptr< mfem::GridFunction > | m_theta |
Grid function for the scalar potential ![]() | |
std::unique_ptr< mfem::GridFunction > | m_phi |
Grid function for the vector flux ![]() | |
std::unique_ptr< PolytropeOperator > | m_polytropOperator |
The main nonlinear operator for the mixed system. | |
std::unique_ptr< mfem::OperatorJacobiSmoother > | m_prec |
Preconditioner (currently seems unused in polySolver.cpp ). | |
Solves the Lane-Emden equation for a polytropic star using a mixed finite element method.
This class sets up and solves the system of equations describing a polytropic stellar model. The Lane-Emden equation, in its second-order form, is:
where is a dimensionless radius,
is a dimensionless temperature/potential related to density (
), and
is the polytropic index.
This solver uses a mixed formulation by introducing :
These equations are discretized using H1 finite elements for and RT (Raviart-Thomas) finite elements for
. The resulting nonlinear system is solved using a Newton method, with the
PolytropeOperator
class defining the system residual and Jacobian.
Boundary conditions typically include at the center (
),
at the surface (
is the first root of
), and
at the surface, which is related to the surface gravity. At the center,
due to symmetry.
Definition at line 203 of file polySolver.h.
serif::polytrope::PolySolver::PolySolver | ( | const double | n, |
const double | order ) |
Definition at line 98 of file polySolver.cpp.
|
default |
Destructor for PolySolver.
Defaulted, handles cleanup of owned resources like std::unique_ptr
members.
|
private |
Private constructor that takes an existing mesh.
Initializes FE collections, spaces, grid functions, and assembles the block system. This is called by the public constructor after prepareMesh
.
mesh | A reference to an initialized mfem::Mesh . |
n | The polytropic index. |
order | The polynomial order for H1 finite elements. |
Definition at line 76 of file polySolver.cpp.
|
staticprivate |
Assemble and finalize a given MFEM form.
This template function calls Assemble()
and Finalize()
on the provided form. Assemble()
computes local element matrices and adds them to the global matrix. Finalize()
builds the sparsity pattern and allows the SparseMatrix
representation to be extracted.
FormType | The type of the MFEM form (e.g., mfem::BilinearForm , mfem::MixedBilinearForm ). |
f | A reference to the form to be assembled and finalized. |
f
must be a valid form object that supports Assemble()
and Finalize()
methods. f
is assembled and finalized, and its sparse matrix representation is available.mfem::NonlinearForm
are typically not "finalized" in this sense, as they don't directly produce a sparse matrix but are evaluated. This function is intended for linear forms. Definition at line 182 of file polySolver.cpp.
|
private |
Assembles the block system operator m_polytropOperator
.
This involves:
buildIndividualForms
.PolytropeOperator
with these forms and offsets. Definition at line 114 of file polySolver.cpp.
|
private |
Build the individual forms for the block operator (M, Q, D, S, and f).
blockOffsets | The offsets for the blocks in the operator, computed by computeBlockOffsets . |
std::unique_ptr<formBundle>
containing the assembled forms.Mform
corresponds to PolytropeOperator
handles any necessary sign changes for the final system assembly.This method initializes and assembles the discrete forms corresponding to the weak formulation of the mixed polytropic system:
The PolytropeOperator
will then use these to form a system like:
(The exact structure depends on the PolytropeOperator
's internal assembly, which might involve stabilization terms using S).
m_feTheta
and m_fePhi
must be valid, populated mfem::FiniteElementSpace
objects. m_polytropicIndex
must be set. formBundle
contains unique pointers to assembled (and finalized where appropriate) MFEM forms. Definition at line 152 of file polySolver.cpp.
|
private |
Compute the block offsets for the operator. These are the offsets that define which dofs belong to which variable.
Create the block offsets. These define the start of each block in the combined vector. Block offsets will be , where
is the number of degrees of freedom for
and
is for
. The interpretation of this is that each block tells the operator where in the flattened (1D) vector the degrees of freedom or coefficients for that free parameter start and end. I.e. we know that in any flattened vector will have a size
. The
dofs will span from
blockOffsets[0]
to blockOffsets[1]
and the dofs will span from
blockOffsets[1]
to blockOffsets[2]
.
This is the same for matrices only in 2D (rows and columns).
The key point here is that this is fundamentally an accounting structure, it is here to keep track of what parts of vectors and matrices belong to which variable.
Also note that we use VSize
rather than Size
. Size
refers to the number of true dofs (after eliminating boundary conditions). VSize
refers to the total number of dofs before BC elimination, which is needed here.
mfem::Array<int>
The offsets for the blocks in the operator.m_feTheta
and m_fePhi
must be valid, populated mfem::FiniteElementSpace
objects. Definition at line 142 of file polySolver.cpp.
|
private |
Finds the degrees of freedom (DOFs) associated with the geometric center (origin) of the mesh.
This method identifies:
These DOFs are used to apply boundary conditions at the center of the polytrope, such as and
.
std::pair
of mfem::Array<int>
:first
: Array of second
: Array of `mfem::ErrorException` | (via MFEM_ABORT ) if no vertex is found at the origin (within tolerance). |
For RT elements, DOFs are typically associated with faces (or edges in 2D). This method collects DOFs from all elements connected to the center vertex. For H1 elements, DOFs can be directly associated with vertices.
m_mesh
, m_feTheta
, m_fePhi
must be initialized. Definition at line 269 of file polySolver.cpp.
|
staticprivate |
Utility function to get and save the coordinates of degrees of freedom for a finite element space.
For each element in the mesh:
fes
.This can be useful for debugging or analyzing the distribution of DOFs.
fes | The mfem::FiniteElementSpace for which to get DOF coordinates. |
filename | The name of the output CSV file. |
fes
must be a valid, initialized mfem::FiniteElementSpace
. The mesh associated with fes
must be valid. filename
is created/truncated and populated with DOF coordinate information.Definition at line 435 of file polySolver.cpp.
|
private |
Computes the essential true degrees of freedom (DOFs) and their values for boundary conditions.
This method determines the DOFs that correspond to:
polySolver.cpp
seems to set components of serif::types::MFEMArrayPairSet
containing two pairs:mfem::Array<int>
of essential TDOF indices, mfem::Array<double>
of corresponding values).mfem::Array<int>
of essential TDOF indices, mfem::Array<double>
of corresponding values)."True DOFs" (tdof) in MFEM refer to the actual degrees of freedom in the global system, as opposed to local DOFs within an element. Essential boundary conditions fix the values of these DOFs. The center condition for is applied to DOFs identified by
findCenterElement()
. Surface conditions are applied to boundary attributes marked as essential (typically attribute 1). The surface flux for is obtained from
polycoeff::thetaSurfaceFlux(m_polytropicIndex)
.
m_mesh
, m_feTheta
, m_fePhi
must be initialized. m_polytropicIndex
must be set. `mfem::ErrorException` | (via MFEM_ABORT ) if findCenterElement() fails to locate the center vertex. |
Definition at line 230 of file polySolver.cpp.
|
inline |
Gets a reference to the computational mesh.
mfem::Mesh
object. Definition at line 267 of file polySolver.h.
|
inline |
Gets the polytropic index .
Definition at line 255 of file polySolver.h.
|
inline |
Gets the finite element order used for the variable.
Definition at line 261 of file polySolver.h.
|
inline |
Gets a reference to the solution grid function for .
mfem::GridFunction
storing the solve()
has been successfully called. Definition at line 281 of file polySolver.h.
|
inline |
Gets a reference to the solution grid function for .
mfem::GridFunction
storing the solve()
has been successfully called. Definition at line 274 of file polySolver.h.
|
private |
Loads Newton and GMRES solver parameters from the configuration.
Retrieves relative tolerance, absolute tolerance, maximum iterations, and print level for both the Newton solver and the GMRES linear solver from the Config
instance. Default values are used if specific configuration keys are not found.
Configuration keys are typically prefixed with Poly:Solver:Newton:
and Poly:Solver:GMRES:
. Example keys: Poly:Solver:Newton:RelTol
, Poly:Solver:GMRES:MaxIter
.
[out] | newtonRelTol | Relative tolerance for Newton solver. |
[out] | newtonAbsTol | Absolute tolerance for Newton solver. |
[out] | newtonMaxIter | Maximum iterations for Newton solver. |
[out] | newtonPrintLevel | Print level for Newton solver. |
[out] | gmresRelTol | Relative tolerance for GMRES solver. |
[out] | gmresAbsTol | Absolute tolerance for GMRES solver. |
[out] | gmresMaxIter | Maximum iterations for GMRES solver. |
[out] | gmresPrintLevel | Print level for GMRES solver. |
m_config
must be a valid reference to a Config
object. m_logger
should be initialized if debug logging is enabled. Definition at line 419 of file polySolver.cpp.
|
staticprivate |
Prepares the mesh for the simulation.
Loads a generic sphere mesh from the ResourceManager
and scales it to the dimensionless radius (the first zero of the Lane-Emden function for the given polytropic index
).
n | The polytropic index. |
mfem::Mesh
(owned by ResourceManager
). std::runtime_error | If ![]() ![]() ![]() |
std::runtime_error | If the mesh resource "mesh:polySphere" cannot be found or loaded. |
The scaling factor is obtained from
polycoeff::x1(n)
. The mesh is expected to be a unit sphere initially.
Definition at line 101 of file polySolver.cpp.
|
private |
Saves the 1D radial solution and optionally displays the 2D/3D solution using GLVis.
Extracts the and
components from the converged
state_vector
.
Poly:Output:1D:Save
), it extracts a 1D solution along a specified ray (defined by co-latitude and longitude) using Probe::getRaySolution
and saves it to a CSV file.Poly:Output:View
), it displays Probe::glVisView
.state_vector | The full solution vector (block vector containing ![]() ![]() PolytropeOperator . |
m_polytropOperator
, m_feTheta
, m_fePhi
must be initialized. Configuration settings for output and viewing must be loaded. Definition at line 388 of file polySolver.cpp.
|
private |
Sets the initial guess for the solution variables and
.
laneEmden::thetaSeriesExpansion
).polycoeff::thetaSurfaceFlux
.findCenterElement
) are set to 0.The method uses mfem::GridFunction::ProjectCoefficient
and ProjectBdrCoefficient
(or ProjectBdrCoefficientNormal
) for these projections.
PolytropeOperator
handles this more robustly.m_theta
, m_phi
, m_mesh
, m_feTheta
, m_fePhi
must be initialized. m_polytropicIndex
must be set. Configuration settings for viewing initial guess should be loaded if desired. m_theta
and m_phi
grid functions are populated with the initial guess. Definition at line 328 of file polySolver.cpp.
|
private |
Sets the essential true DOFs on the PolytropeOperator
.
Calls getEssentialTrueDof()
to compute the boundary condition DOFs and values, and then passes them to m_polytropOperator->set_essential_true_dofs()
. This step is crucial for the PolytropeOperator
to correctly handle boundary conditions when forming reduced systems and applying residuals/Jacobians.
m_polytropOperator
must be initialized. m_mesh
, m_feTheta
, m_fePhi
, m_polytropicIndex
must be set (for getEssentialTrueDof
). m_polytropOperator
. The PolytropeOperator
will likely be marked as not finalized after this call. Definition at line 414 of file polySolver.cpp.
|
private |
Sets up the Newton solver and its associated linear solver (GMRES).
LoadSolverUserParams
).solverBundle
to manage the lifetimes of mfem::GMRESSolver
and mfem::NewtonSolver
.PolytropeOperator
as the nonlinear system operator, and sets the configured GMRES solver as the linear solver for inverting Jacobians.solverBundle
struct containing the configured Newton and GMRES solvers. The ownership of the solvers within the bundle is managed by the bundle itself.m_polytropOperator
must be initialized and finalized. Configuration settings for solver parameters must be available. solverBundle
is returned, ready for newton.Mult()
. Definition at line 472 of file polySolver.cpp.
void serif::polytrope::PolySolver::solve | ( | ) | const |
Solves the polytropic system.
This method orchestrates the solution process:
PolytropeOperator
.PolytropeOperator
with the initial state.std::runtime_error | or mfem::ErrorException if the solver fails to converge or encounters numerical issues. |
Definition at line 190 of file polySolver.cpp.
|
private |
Reference to the global configuration manager instance.
Definition at line 285 of file polySolver.h.
|
private |
Finite Element Collection for (H1 elements).
Definition at line 295 of file polySolver.h.
|
private |
Finite Element Collection for (Raviart-Thomas elements).
Definition at line 296 of file polySolver.h.
|
private |
The polynomial order for H1 elements ( ). RT elements for
are of order
m_feOrder - 1
.
Definition at line 291 of file polySolver.h.
|
private |
Finite Element Space for .
Definition at line 299 of file polySolver.h.
|
private |
Finite Element Space for .
Definition at line 298 of file polySolver.h.
|
private |
Pointer to the specific logger instance for this class.
Definition at line 287 of file polySolver.h.
|
private |
Reference to the global log manager instance.
Definition at line 286 of file polySolver.h.
|
private |
Reference to the computational mesh (owned by ResourceManager).
Definition at line 294 of file polySolver.h.
|
private |
Grid function for the vector flux .
Definition at line 303 of file polySolver.h.
|
private |
The polytropic index .
Definition at line 290 of file polySolver.h.
|
private |
The main nonlinear operator for the mixed system.
Definition at line 306 of file polySolver.h.
|
private |
Preconditioner (currently seems unused in polySolver.cpp
).
Definition at line 307 of file polySolver.h.
|
private |
Grid function for the scalar potential .
Definition at line 302 of file polySolver.h.