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

Simplifies ProblemBuilders and Converts Problem to a Struct #10

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3391936
Removes SetCoefficients method.
Jul 8, 2024
db0502f
Removes SetJacobianSolver().
Jul 8, 2024
2197e08
Removes addBoundaryCondition method.
Jul 8, 2024
3850e8a
Removes SetSolverOptions.
Jul 8, 2024
c432547
Removes SetOutputs.
Jul 8, 2024
a429df1
Removes SetJacobianPreconditioner.
Jul 8, 2024
b5c1232
Removes SetBoundaryConditions.
Jul 8, 2024
07d47c4
Removes RegisterFESpaces, RegisterCoefficients.
Jul 8, 2024
27c8930
Removes SetMesh.
Jul 8, 2024
0326979
Removes SetFESpaces, SetGridFunctions.
Jul 8, 2024
429f137
Removes AddFESpace, AddGridFunction.
Jul 8, 2024
84ea395
Fixes compilation after removal of SetMesh.
Jul 8, 2024
03d078b
Moves ConstructOperator method directly into problem builders.
Jul 8, 2024
4ddcdd2
Removes solver options from Problem.
Jul 9, 2024
295e986
Adds base class for the problem operator.
Jul 9, 2024
ca577f6
Adds ReturnOperator() method to ProblemOperatorBase.
Jul 9, 2024
d475473
Moves the problem operator out of the Problem classes.
Jul 9, 2024
d4b87ae
Removes derived problem classes.
Jul 9, 2024
67ad04d
Problem class is now a struct.
Jul 9, 2024
bbb4e3e
Renames "Problem" to "MFEMProblemData".
Jul 9, 2024
9165c60
Simplifies ProblemOperator constructors.
Jul 9, 2024
b56782a
getMFEMMesh(), getMFEMParMesh() return shared pointers.
Jul 11, 2024
02c731b
Removes TestVariables.
Jul 11, 2024
9abcc13
Adds note to the ProblemOperatorBase class with next steps.
Jul 11, 2024
6b8e59e
Simplifies the problem operator constructors.
Jul 11, 2024
f043fb6
Moves ConstructOperator implementation into source file.
Jul 11, 2024
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
4 changes: 3 additions & 1 deletion include/executioners/steady_executioner.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "executioner_base.h"
#include "steady_state_problem_builder.h"
#include "problem_operator.h"

namespace platypus
{
Expand All @@ -16,7 +17,8 @@ class SteadyExecutioner : public Executioner
void Execute() const override;

private:
platypus::SteadyStateProblem * _problem{nullptr};
platypus::MFEMProblemData * _problem{nullptr};
platypus::ProblemOperator * _problem_operator{nullptr};
};

} // namespace platypus
2 changes: 1 addition & 1 deletion include/executioners/transient_executioner.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TransientExecutioner : public Executioner
mutable int _it; // Time index
int _vis_steps; // Number of cyces between each output update
mutable bool _last_step; // Flag to check if current step is final
platypus::TimeDomainProblem * _problem{nullptr};
platypus::MFEMProblemData * _problem{nullptr};
};

} // namespace platypus
13 changes: 8 additions & 5 deletions include/mesh/ExclusiveMFEMMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ class ExclusiveMFEMMesh : public FileMesh
std::unique_ptr<MooseMesh> safeClone() const override;

/**
* Accessors for the _mfem_mesh and _mfem_par_mesh objects. If the objects have
* not been build, the methods will call the appropriate protected methods to
* build them.
* Returns a shared pointer to the MFEMMesh. If the objects have not been built,
* the method will call the appropriate protected methods to build them.
*/
MFEMMesh & getMFEMMesh();
MFEMParMesh & getMFEMParMesh();
[[nodiscard]] std::shared_ptr<MFEMMesh> getMFEMMesh();

/**
* Returns a shared pointer to the MFEMParMesh.
*/
[[nodiscard]] std::shared_ptr<MFEMParMesh> getMFEMParMesh();

/**
* Calls buildDummyMesh.
Expand Down
5 changes: 4 additions & 1 deletion include/problem/MFEMProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MFEMBilinearFormKernel.h"
#include "MFEMLinearFormKernel.h"
#include "MFEMFormulation.h"
#include "problem_operator_base.h"
#include "MFEMDataCollection.h"
#include "MFEMFESpace.h"
#include "Function.h"
Expand Down Expand Up @@ -191,6 +192,8 @@ class MFEMProblem : public ExternalProblem

std::shared_ptr<platypus::ProblemBuilder> mfem_problem_builder{nullptr};

std::shared_ptr<platypus::Problem> mfem_problem{nullptr};
std::shared_ptr<platypus::MFEMProblemData> mfem_problem{nullptr};
std::shared_ptr<platypus::ProblemOperatorBase> _mfem_operator{nullptr};

std::unique_ptr<platypus::Executioner> executioner{nullptr};
};
84 changes: 20 additions & 64 deletions include/problem_builders/problem_builder_base.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "equation_system.h"
#include "problem_operator_base.h"
#include "gridfunctions.h"
#include "inputs.h"
#include <fstream>
Expand All @@ -9,18 +10,15 @@
namespace platypus
{

/// Base problem class.
class Problem
/**
* Stores information required for simulation.
*/
struct MFEMProblemData
{
public:
Problem() = default;
virtual ~Problem();

std::shared_ptr<mfem::ParMesh> _pmesh{nullptr};
platypus::BCMap _bc_map;
platypus::Coefficients _coefficients;
platypus::Outputs _outputs;
platypus::InputParameters _solver_options;

std::unique_ptr<mfem::ODESolver> _ode_solver{nullptr};
std::unique_ptr<mfem::BlockVector> _f{nullptr};
Expand All @@ -32,49 +30,16 @@ class Problem
platypus::FECollections _fecs;
platypus::FESpaces _fespaces;
platypus::GridFunctions _gridfunctions;

MPI_Comm _comm;
int _myid;
int _num_procs;

/// Returns a pointer to the operator. See derived classes.
[[nodiscard]] virtual mfem::Operator * GetOperator() const = 0;

/// Virtual method to construct the operator. Call for default problems.
virtual void ConstructOperator() = 0;
};

/// ProblemBuilder base class.
class ProblemBuilder
{
public:
/// NB: delete empty constructor to allow only derived classes to be constructed.
ProblemBuilder() = delete;

// Virtual destructor required to prevent leaks.
virtual ~ProblemBuilder() = default;

void SetMesh(std::shared_ptr<mfem::ParMesh> pmesh);
void SetFESpaces(platypus::FESpaces & fespaces);
void SetGridFunctions(platypus::GridFunctions & gridfunctions);
void SetBoundaryConditions(platypus::BCMap & bc_map);
void SetOutputs(platypus::Outputs & outputs);
void SetSolverOptions(platypus::InputParameters & solver_options);
void SetJacobianPreconditioner(std::shared_ptr<mfem::Solver> preconditioner);
void SetJacobianSolver(std::shared_ptr<mfem::Solver> solver);
void SetCoefficients(platypus::Coefficients & coefficients);

void AddFESpace(std::string fespace_name,
std::string fec_name,
int vdim = 1,
int ordering = mfem::Ordering::byNODES);
void AddGridFunction(std::string gridfunction_name, std::string fespace_name);

void AddBoundaryCondition(std::string bc_name, std::shared_ptr<platypus::BoundaryCondition> bc);

virtual void RegisterFESpaces() = 0;
virtual void RegisterGridFunctions() = 0;
virtual void RegisterCoefficients() = 0;

virtual void SetOperatorGridFunctions() = 0;
virtual void ConstructNonlinearSolver();
Expand All @@ -86,41 +51,32 @@ class ProblemBuilder

void InitializeOutputs();

/// @brief Call @a FinalizeProblem to setup a problem.
/// @param build_operator Skips @a ConstructOperator step if false. Set this to false if the problem
/// operator has already been constructed earlier to avoid rebuilding it.
void FinalizeProblem(bool build_operator = true);
/// @brief Call @a FinalizeProblem to setup a problem. The operator should
/// have already been constructed by calling ConstructOperator().
void FinalizeProblem();

/// Returns a shared pointer to the problem.
std::shared_ptr<platypus::Problem> ReturnProblem() { return _problem; }

protected:
/// Protected constructor. Derived classes must call this constructor.
ProblemBuilder(platypus::Problem * problem)
: _problem(std::shared_ptr<platypus::Problem>(problem))
[[nodiscard]] std::shared_ptr<platypus::MFEMProblemData> ReturnProblem() const
{
return _problem;
}

/// Overridden in derived classes.
[[nodiscard]] virtual platypus::Problem * GetProblem() const = 0;

/// Helper template getter with safety check.
template <class TDerivedProblem>
[[nodiscard]] TDerivedProblem * GetProblem() const
/// Returns a shared pointer to the problem operator.
[[nodiscard]] std::shared_ptr<platypus::ProblemOperatorBase> ReturnOperator() const
{
if (!_problem)
{
MFEM_ABORT("platypus::Problem instance is NULL.");
}

return static_cast<TDerivedProblem *>(_problem.get());
return _problem_operator;
}

protected:
/// Protected constructor. Derived classes must call this constructor.
ProblemBuilder() : _problem(std::make_shared<platypus::MFEMProblemData>()) {}

/// Coefficient used in some derived classes.
mfem::ConstantCoefficient _one_coef{1.0};

private:
std::shared_ptr<platypus::Problem> _problem{nullptr};
protected:
std::shared_ptr<platypus::MFEMProblemData> _problem{nullptr};
std::shared_ptr<platypus::ProblemOperatorBase> _problem_operator{nullptr};
};

/// Interface for problem builders that are constructing problems with an equation system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,30 @@
namespace platypus
{

/// Steady-state problems with an equation system.
class SteadyStateEquationSystemProblem : public SteadyStateProblem, public EquationSystemInterface
{
public:
SteadyStateEquationSystemProblem() = default;
~SteadyStateEquationSystemProblem() override = default;

[[nodiscard]] EquationSystemProblemOperator * GetOperator() const override
{
return static_cast<EquationSystemProblemOperator *>(SteadyStateProblem::GetOperator());
}

void SetOperator(std::unique_ptr<EquationSystemProblemOperator> problem_operator)
{
SteadyStateProblem::SetOperator(std::move(problem_operator));
}

void ConstructOperator() override
{
auto equation_system = std::make_unique<platypus::EquationSystem>();
auto problem_operator = std::make_unique<platypus::EquationSystemProblemOperator>(
*this, std::move(equation_system));

SetOperator(std::move(problem_operator));
}

[[nodiscard]] platypus::EquationSystem * GetEquationSystem() const override
{
return GetOperator()->GetEquationSystem();
}
};

/// Problem-builder for SteadyStateEquationSystemProblem.
class SteadyStateEquationSystemProblemBuilder : public SteadyStateProblemBuilder,
public EquationSystemProblemBuilderInterface
{
public:
/// NB: set "_problem" member variable in parent class.
SteadyStateEquationSystemProblemBuilder()
: SteadyStateProblemBuilder(new SteadyStateEquationSystemProblem)
{
}
SteadyStateEquationSystemProblemBuilder() = default;

~SteadyStateEquationSystemProblemBuilder() override = default;

/// NB: use of final! This calls ProblemBuilder::InitializeKernels and also ensures that the
/// equation system is initialized.
void InitializeKernels() final;

void ConstructOperator() override;

protected:
[[nodiscard]] platypus::SteadyStateEquationSystemProblem * GetProblem() const override
[[nodiscard]] platypus::EquationSystemProblemOperator & GetOperator() const
{
return ProblemBuilder::GetProblem<platypus::SteadyStateEquationSystemProblem>();
return static_cast<platypus::EquationSystemProblemOperator &>(*_problem_operator);
}

[[nodiscard]] platypus::EquationSystem * GetEquationSystem() const override
{
return GetProblem()->GetEquationSystem();
return GetOperator().GetEquationSystem();
}
};

Expand Down
46 changes: 3 additions & 43 deletions include/problem_builders/steady_state_problem_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,15 @@
namespace platypus
{

/// Class for steady-state problems with no equation system.
class SteadyStateProblem : public Problem
{
public:
SteadyStateProblem() = default;
~SteadyStateProblem() override = default;

[[nodiscard]] platypus::ProblemOperator * GetOperator() const override
{
if (!_problem_operator)
{
MFEM_ABORT("No operator has been added.");
}

return _problem_operator.get();
}

void SetOperator(std::unique_ptr<platypus::ProblemOperator> problem_operator)
{
_problem_operator.reset();
_problem_operator = std::move(problem_operator);
}

void ConstructOperator() override
{
_problem_operator.reset();
_problem_operator = std::make_unique<platypus::ProblemOperator>(*this);
}

private:
std::unique_ptr<platypus::ProblemOperator> _problem_operator{nullptr};
};

class SteadyStateProblemBuilder : public ProblemBuilder
{
public:
SteadyStateProblemBuilder() : ProblemBuilder(new platypus::SteadyStateProblem) {}
SteadyStateProblemBuilder() = default;

~SteadyStateProblemBuilder() override = default;

void RegisterFESpaces() override {}

void RegisterGridFunctions() override {}

void RegisterCoefficients() override {}

void SetOperatorGridFunctions() override;

void ConstructOperator() override;
Expand All @@ -59,12 +22,9 @@ class SteadyStateProblemBuilder : public ProblemBuilder
void ConstructTimestepper() override {}

protected:
// NB: constructor for derived classes.
SteadyStateProblemBuilder(platypus::SteadyStateProblem * problem) : ProblemBuilder(problem) {}

[[nodiscard]] platypus::SteadyStateProblem * GetProblem() const override
[[nodiscard]] platypus::ProblemOperator & GetOperator() const
{
return ProblemBuilder::GetProblem<platypus::SteadyStateProblem>();
return static_cast<platypus::ProblemOperator &>(*_problem_operator);
}
};

Expand Down
Loading