Skip to content

Commit

Permalink
chaning function name and adding a type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed May 16, 2024
1 parent ae78706 commit eb2c6d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 51 deletions.
13 changes: 7 additions & 6 deletions include/micm/solver/solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@

namespace micm
{
class SolverImplBase {
public:
using SolverParameters = std::variant<std::monostate, micm::RosenbrockSolverParameters, micm::BackwardEulerSolverParameters>;

class SolverImplBase
{
public:
virtual ~SolverImplBase() = default;
};

template<class LinearSolverPolicy, class ProcessSetPolicy>
struct SolverImpl : public SolverImplBase {
struct SolverImpl : public SolverImplBase
{
ProcessSetPolicy process_set_;
LinearSolverPolicy linear_solver_;
};

class Solver
{
private:
using SolverParameters =
std::variant<std::monostate, micm::RosenbrockSolverParameters, micm::BackwardEulerSolverParameters>;

SolverParameters parameters_;
std::size_t number_of_grid_cells_;
std::size_t number_of_species_;
Expand Down
52 changes: 25 additions & 27 deletions include/micm/solver/solver_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,68 @@ namespace micm
micm::System system_;
std::size_t number_of_grid_cells_;
std::vector<micm::Process> reactions_;
std::variant<std::monostate, micm::RosenbrockSolverParameters, micm::BackwardEulerSolverParameters> options_;
SolverParameters options_;
bool ignore_unused_species_ = true;
bool reorder_state_ = true;

public:
/// @brief Set the chemical system
/// @param system
/// @return
/// @param system
/// @return
SolverBuilder& SetSystem(micm::System system);

/// @brief Set the reactions
/// @param reactions
/// @return
/// @param reactions
/// @return
SolverBuilder& SetReactions(std::vector<micm::Process> reactions);

/// @brief Set the number of grid cells
/// @param number_of_grid_cells
/// @return
/// @param number_of_grid_cells
/// @return
SolverBuilder& SetNumberOfGridCells(int number_of_grid_cells);

/// @brief Choose a rosenbrock solver
/// @param options
/// @return
SolverBuilder& SolverParameters(const RosenbrockSolverParameters& options);
/// @param options
/// @return
SolverBuilder& SetSolverParameters(const RosenbrockSolverParameters& options);

/// @brief Choose a backward euler solver
/// @param options
/// @return
SolverBuilder& SolverParameters(const BackwardEulerSolverParameters& options);
/// @param options
/// @return
SolverBuilder& SetSolverParameters(const BackwardEulerSolverParameters& options);

/// @brief
/// @return
/// @brief
/// @return
Solver Build();

protected:

/// @brief
/// @return
/// @brief
/// @return
virtual Solver BuildBackwardEulerSolver() = 0;
virtual ~SolverBuilder() = default;

/// @brief
/// @tparam ProcessSetPolicy
/// @return
/// @brief
/// @tparam ProcessSetPolicy
/// @return
template<class ProcessSetPolicy>
void UnusedSpeciesCheck();

/// @brief Get a species map properly ordered
/// @return
/// @return
template<class MatrixPolicy, class ProcessSetPolicy>
std::map<std::string, std::size_t> GetSpeciesMap() const;

/// @brief Set the absolute tolerances per species
/// @param parameters
/// @param species_map
/// @return
/// @param parameters
/// @param species_map
/// @return
void SetAbsoluteTolerances(std::vector<double>& tolerances, const std::map<std::string, std::size_t>& species_map) const;

/// @brief Return the labels of the custom parameters
/// @return
/// @return
std::vector<std::string> GetCustomParameterLabels() const;

std::vector<std::size_t> GetJacobianDiagonalElements(auto jacobian) const;

};

template<class MatrixPolicy, class SparseMatrixPolicy>
Expand Down
25 changes: 15 additions & 10 deletions include/micm/solver/solver_builder.inl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace micm
return *this;
}

inline SolverBuilder& SolverBuilder::SolverParameters(const RosenbrockSolverParameters& options)
inline SolverBuilder& SolverBuilder::SetSolverParameters(const RosenbrockSolverParameters& options)
{
if (!std::holds_alternative<std::monostate>(options_))
{
Expand All @@ -75,7 +75,7 @@ namespace micm
return *this;
}

inline SolverBuilder& SolverBuilder::SolverParameters(const BackwardEulerSolverParameters& options)
inline SolverBuilder& SolverBuilder::SetSolverParameters(const BackwardEulerSolverParameters& options)
{
if (!std::holds_alternative<std::monostate>(options_))
{
Expand Down Expand Up @@ -137,7 +137,6 @@ namespace micm
for (auto& name : system_.UniqueNames())
species_map[name] = index++;


if (reorder_state_)
{
// get unsorted Jacobian non-zero elements
Expand Down Expand Up @@ -189,7 +188,8 @@ namespace micm
}
}

inline std::vector<std::string> SolverBuilder::GetCustomParameterLabels() const {
inline std::vector<std::string> SolverBuilder::GetCustomParameterLabels() const
{
std::vector<std::string> param_labels{};
for (const auto& reaction : reactions_)
if (reaction.rate_constant_)
Expand All @@ -198,7 +198,8 @@ namespace micm
return param_labels;
}

inline std::vector<std::size_t> SolverBuilder::GetJacobianDiagonalElements(auto jacobian) const {
inline std::vector<std::size_t> SolverBuilder::GetJacobianDiagonalElements(auto jacobian) const
{
std::vector<std::size_t> jacobian_diagonal_elements;

jacobian_diagonal_elements.reserve(jacobian.NumRows());
Expand All @@ -221,19 +222,23 @@ namespace micm
auto labels = GetCustomParameterLabels();
std::size_t number_of_species = system_.StateSize();


UnusedSpeciesCheck<ProcessSetPolicy>();
SetAbsoluteTolerances(parameters.absolute_tolerance_, species_map);

ProcessSetPolicy process_set(reactions_, species_map);
auto jacobian = BuildJacobian<SparseMatrixPolicy>(process_set.NonZeroJacobianElements(), number_of_grid_cells_, number_of_species);
auto jacobian =
BuildJacobian<SparseMatrixPolicy>(process_set.NonZeroJacobianElements(), number_of_grid_cells_, number_of_species);
auto diagonal_elements = GetJacobianDiagonalElements(jacobian);

process_set.SetJacobianFlatIds(jacobian);
micm::LinearSolver<double, SparseMatrixPolicy> linear_solver(jacobian, 1e-30);
micm::LinearSolver<typename MatrixPolicy::value_type, SparseMatrixPolicy> linear_solver(jacobian, 1e-30);

return Solver(
new SolverImpl<decltype(linear_solver), decltype(process_set)>(),
parameters, number_of_grid_cells_, number_of_species, reactions_.size());
new SolverImpl<decltype(linear_solver), decltype(process_set)>(),
parameters,
number_of_grid_cells_,
number_of_species,
reactions_.size());
}

} // namespace micm
17 changes: 9 additions & 8 deletions test/unit/solver/test_solver_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <algorithm>
#include <random>

namespace {
namespace
{
auto a = micm::Species("A");
auto b = micm::Species("B");
auto c = micm::Species("C");
Expand All @@ -30,24 +31,24 @@ namespace {
.SetPhase(gas_phase);
micm::System the_system = micm::System(micm::SystemParameters{ .gas_phase_ = gas_phase });
std::vector<micm::Process> reactions = { r1, r2 };
}
} // namespace

TEST(SolverBuilder, CanBuildBackwardEuler)
{
auto backward_euler = micm::CpuSolverBuilder<micm::Matrix<double>, micm::SparseMatrix<double>>()
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.SolverParameters(micm::BackwardEulerSolverParameters{})
.SetSolverParameters(micm::BackwardEulerSolverParameters{})
.Build();

constexpr std::size_t L = 4;
auto backward_euler_vector = micm::CpuSolverBuilder<micm::VectorMatrix<double, L>, micm::SparseMatrix<double>>()
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.SolverParameters(micm::BackwardEulerSolverParameters{})
.Build();
.SetSystem(the_system)
.SetReactions(reactions)
.SetNumberOfGridCells(1)
.SetSolverParameters(micm::BackwardEulerSolverParameters{})
.Build();
}

TEST(SolverBuilder, CanBuildRosenbrock)
Expand Down

0 comments on commit eb2c6d8

Please sign in to comment.