Skip to content

Commit

Permalink
Reorganize inputFileReader and userInputParameters (#375)
Browse files Browse the repository at this point in the history
* replace vector of model constants with map

note: use AssertThrow rather than Assert for release exceptions

* minor refactor of inputFileReader and userInputs

Mostly reorganization. Split the parameter_handler declarations into several functions rather than one 500-line function.
Object-oriented some functionality. Removed get_number_of_entries.
Renamed a few functions.
  • Loading branch information
fractalsbyx authored Jan 1, 2025
1 parent 992c771 commit 389726b
Show file tree
Hide file tree
Showing 4 changed files with 432 additions and 357 deletions.
102 changes: 82 additions & 20 deletions include/core/inputFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include <deal.II/base/parameter_handler.h>

#include <core/variableAttributeLoader.h>
#include <set>
#include <string>
#include <vector>

/**
* \brief Input file reader.
* \brief Parameters file reader. Declares parameter names in a dealii parameter_handler
* and parses the file for the values. Variable assignment occurs in userInputParameters.
*/
class inputFileReader
{
Expand All @@ -26,35 +28,23 @@ class inputFileReader
* \brief Method to get a list of entry values from multiple subsections in an input
* file.
*/
[[nodiscard]] static std::vector<std::string>
get_subsection_entry_list(const std::string &parameters_file_name,
const std::string &subsec_name,
[[nodiscard]] std::vector<std::string>
get_subsection_entry_list(const std::string &subsec_name,
const std::string &entry_name,
const std::string &default_entry);

/**
* \brief Method to count the number of related entries in an input file.
*/
[[nodiscard]] static unsigned int
get_number_of_entries(const std::string &parameters_file_name,
const std::string &keyword,
const std::string &entry_name);

/**
* \brief Get the trailing part of the entry name after a specified string (used to
* extract the model constant names).
*/
[[nodiscard]] static std::vector<std::string>
get_entry_name_ending_list(const std::string &parameters_file_name,
const std::string &keyword,
const std::string &entry_name_begining);
[[nodiscard]] std::set<std::string>
get_model_constant_names();

/**
* \brief Method to declare the parameters to be read from an input file.
*/
void
declare_parameters(dealii::ParameterHandler &parameter_handler,
const unsigned int num_of_constants) const;
declare_parameters();

/**
* \brief Method to check if a line has the desired contents and if so, extract it.
Expand All @@ -78,11 +68,83 @@ class inputFileReader
static bool
check_keyword_match(std::string &line, const std::string &keyword);

/**
* \brief Declare parameters for the mesh.
*/
void
declare_mesh();

/**
* \brief Declare parameters for timestepping.
*/
void
declare_time_discretization();

/**
* \brief Declare parameters for linear and nonlinear solvers.
*/
void
declare_solver_parameters();

/**
* \brief Declare parameters for outputs.
*/
void
declare_output_parameters();

/**
* \brief Declare parameters for loading ICs from files.
*/
void
declare_load_IC_parameters();

/**
* \brief Declare parameters for checkpoints.
*/
void
declare_checkpoint_parameters();

/**
* \brief Declare parameters for boundary conditions.
*/
void
declare_BC_parameters();

/**
* \brief Declare parameters for pinned points.
*/
void
declare_pinning_parameters();

/**
* \brief Declare parameters for nucleation
*/
void
declare_nucleation_parameters();

/**
* \brief Declare parameters for grain remapping.
*/
void
declare_grain_remapping_parameters();

/**
* \brief Declare parameters for grain structure loading.
*/
void
declare_grain_loading_parameters();

/**
* \brief Declare parameters for user-defined model constants.
*/
void
declare_model_constants();

const std::string parameters_file_name;
const AttributesList &var_attributes;
const AttributesList &pp_attributes;
dealii::ParameterHandler parameter_handler;
unsigned int num_constants;
std::vector<std::string> model_constant_names;
std::set<std::string> model_constant_names;
unsigned int number_of_dimensions;
};

Expand Down
57 changes: 27 additions & 30 deletions include/core/userInputParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#include <unordered_map>
#include <vector>

template <int dim>
using InputVariant = boost::variant<double,
int,
bool,
dealii::Tensor<1, dim>,
dealii::Tensor<2, dim>,
dealii::Tensor<2, 2 * dim - 1 + dim / 3>>;

enum elasticityModel
{
ISOTROPIC,
Expand Down Expand Up @@ -59,9 +67,6 @@ class userInputParameters
assign_boundary_conditions(std::vector<std::string> &boundary_condition_list,
varBCs<dim> &boundary_condition);

// Map linking the model constant name to its index
std::unordered_map<std::string, unsigned int> model_constant_name_map;

/**
* \brief Retrieve the double from the `model_constants` that are defined from the
* parameters.prm parser. This is essentially just a wrapper for boost::get.
Expand All @@ -71,13 +76,13 @@ class userInputParameters
[[nodiscard]] double
get_model_constant_double(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<double>(model_constants[model_constant_name_map.at(constant_name)]);
return boost::get<double>(model_constants.at(constant_name));
};

/**
Expand All @@ -89,13 +94,13 @@ class userInputParameters
[[nodiscard]] int
get_model_constant_int(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<int>(model_constants[model_constant_name_map.at(constant_name)]);
return boost::get<int>(model_constants.at(constant_name));
};

/**
Expand All @@ -107,13 +112,13 @@ class userInputParameters
[[nodiscard]] bool
get_model_constant_bool(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<bool>(model_constants[model_constant_name_map.at(constant_name)]);
return boost::get<bool>(model_constants.at(constant_name));
};

/**
Expand All @@ -125,14 +130,13 @@ class userInputParameters
[[nodiscard]] dealii::Tensor<1, dim>
get_model_constant_rank_1_tensor(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<dealii::Tensor<1, dim>>(
model_constants[model_constant_name_map.at(constant_name)]);
return boost::get<dealii::Tensor<1, dim>>(model_constants.at(constant_name));
};

/**
Expand All @@ -144,14 +148,13 @@ class userInputParameters
[[nodiscard]] dealii::Tensor<2, dim>
get_model_constant_rank_2_tensor(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<dealii::Tensor<2, dim>>(
model_constants[model_constant_name_map.at(constant_name)]);
return boost::get<dealii::Tensor<2, dim>>(model_constants.at(constant_name));
};

/**
Expand All @@ -163,14 +166,14 @@ class userInputParameters
[[nodiscard]] dealii::Tensor<2, 2 * dim - 1 + dim / 3>
get_model_constant_elasticity_tensor(const std::string &constant_name) const
{
Assert(model_constant_name_map.find(constant_name) != model_constant_name_map.end(),
Assert(model_constants.find(constant_name) != model_constants.end(),
dealii::ExcMessage(
"PRISMS-PF Error: Mismatch between constants in parameters.prm and "
"customPDE.h. The constant that you attempted to access was " +
constant_name + "."));

return boost::get<dealii::Tensor<2, 2 * dim - 1 + dim / 3>>(
model_constants[model_constant_name_map.at(constant_name)]);
model_constants.at(constant_name));
};

// Method to load in the variable attributes
Expand Down Expand Up @@ -292,13 +295,7 @@ class userInputParameters
std::vector<varBCs<dim>> BC_list;

// List of user-defined constants
std::vector<boost::variant<double,
int,
bool,
dealii::Tensor<1, dim>,
dealii::Tensor<2, dim>,
dealii::Tensor<2, 2 * dim - 1 + dim / 3>>>
model_constants;
std::map<std::string, InputVariant<dim>> model_constants;

// Nucleation parameters
bool nucleation_occurs;
Expand Down Expand Up @@ -401,8 +398,8 @@ class userInputParameters
const std::vector<unsigned int> &userGivenTimeStepList);

void
load_user_constants(inputFileReader &input_file_reader,
dealii::ParameterHandler &parameter_handler);
load_model_constants(inputFileReader &input_file_reader,
dealii::ParameterHandler &parameter_handler);

/**
* \brief Compute the number of tensor rows.
Expand Down Expand Up @@ -434,14 +431,14 @@ class userInputParameters
/**
* \brief Assign the specified user constant to whatever type.
*/
void
assign_user_constant(std::vector<std::string> &model_constants_strings);
InputVariant<dim>
construct_user_constant(std::vector<std::string> &model_constants_strings);

/**
* \brief Assign the primitive user constants (e.g., int, double, bool).
*/
void
assign_primitive_user_constant(std::vector<std::string> &model_constants_strings);
InputVariant<dim>
primitive_model_constant(std::vector<std::string> &model_constants_strings);

[[nodiscard]] dealii::Tensor<2, 2 * dim - 1 + dim / 3>
get_Cij_tensor(std::vector<double> elastic_constants,
Expand Down
Loading

0 comments on commit 389726b

Please sign in to comment.