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

replace vector of model constants with map #374

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions include/core/inputFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include <deal.II/base/parameter_handler.h>

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


/**
* \brief Input file reader.
*/
Expand Down Expand Up @@ -44,7 +46,7 @@ class inputFileReader
* \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>
[[nodiscard]] static std::set<std::string>
get_entry_name_ending_list(const std::string &parameters_file_name,
const std::string &keyword,
const std::string &entry_name_begining);
Expand Down Expand Up @@ -82,7 +84,7 @@ class inputFileReader
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
53 changes: 25 additions & 28 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 @@ -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_user_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
15 changes: 10 additions & 5 deletions src/core/inputFileReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ inputFileReader::get_number_of_entries(const std::string &parameters_file_name,

// Method to parse an input file to get a list of variables from related
// subsections
std::vector<std::string>
std::set<std::string>
inputFileReader::get_entry_name_ending_list(const std::string &parameters_file_name,
const std::string &keyword,
const std::string &entry_name_begining)
Expand All @@ -248,7 +248,7 @@ inputFileReader::get_entry_name_ending_list(const std::string &parameters_file_n
std::string entry;
bool found_entry = false;

std::vector<std::string> entry_name_end_list;
std::set<std::string> entry_name_end_list;

// Loop through each line
while (std::getline(input_file, line))
Expand Down Expand Up @@ -282,7 +282,12 @@ inputFileReader::get_entry_name_ending_list(const std::string &parameters_file_n
}

// Add it to the list
entry_name_end_list.push_back(entry);
AssertThrow(
entry_name_end_list.insert(entry).second,
dealii::ExcMessage(
"PRISMS-PF Error: Non-unique constant name in parameters.prm. The "
"constant that you attempted to create was \"" +
entry + "\"."));
}
}
return entry_name_end_list;
Expand Down Expand Up @@ -805,10 +810,10 @@ inputFileReader::declare_parameters(dealii::ParameterHandler &parameter_handler,
"artifact from the loading process.");

// Declare the user-defined constants
for (unsigned int i = 0; i < num_of_constants; i++)
for (const std::string &constant_name : model_constant_names)
{
std::string constants_text = "Model constant ";
constants_text.append(model_constant_names[i]);
constants_text.append(constant_name);
parameter_handler.declare_entry(constants_text,
"0",
dealii::Patterns::Anything(),
Expand Down
41 changes: 16 additions & 25 deletions src/core/userInputParameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,8 @@ userInputParameters<dim>::compute_rank_2_tensor_constant(
}

template <int dim>
void
userInputParameters<dim>::assign_user_constant(
InputVariant<dim>
userInputParameters<dim>::construct_user_constant(
std::vector<std::string> &model_constants_strings)
{
// Ensure that the input includes a value and a type
Expand All @@ -1219,7 +1219,7 @@ userInputParameters<dim>::assign_user_constant(

if (model_constants_strings.size() == 2)
{
assign_primitive_user_constant(model_constants_strings);
return primitive_user_constant(model_constants_strings);
}
else
{
Expand All @@ -1234,14 +1234,12 @@ userInputParameters<dim>::assign_user_constant(
// Rank 1 tensor
if (open_parentheses < 3)
{
model_constants.push_back(
compute_rank_1_tensor_constant(n_elements, model_constants_strings));
return compute_rank_1_tensor_constant(n_elements, model_constants_strings);
}
// Rank 2 tensor
else if (open_parentheses < 5)
{
model_constants.push_back(
compute_rank_2_tensor_constant(n_elements, model_constants_strings));
return compute_rank_2_tensor_constant(n_elements, model_constants_strings);
}
}
else if (boost::iequals(model_constants_type_strings.at(1), "elastic") &&
Expand All @@ -1262,7 +1260,7 @@ userInputParameters<dim>::assign_user_constant(
const std::string elastic_const_symmetry = model_constants_type_strings.at(0);
dealii::Tensor<2, 2 *dim - 1 + dim / 3> temp =
get_Cij_tensor(temp_elastic_constants, elastic_const_symmetry);
model_constants.push_back(temp);
return temp;
}
else
{
Expand All @@ -1271,12 +1269,13 @@ userInputParameters<dim>::assign_user_constant(
"PRISMS-PF ERROR: Only user-defined constant tensors may "
"have multiple elements."));
}
return 0;
}
}

template <int dim>
void
userInputParameters<dim>::assign_primitive_user_constant(
InputVariant<dim>
userInputParameters<dim>::primitive_user_constant(
std::vector<std::string> &model_constants_strings)
{
std::vector<std::string> model_constants_type_strings =
Expand All @@ -1286,25 +1285,24 @@ userInputParameters<dim>::assign_primitive_user_constant(

if (boost::iequals(model_constants_type_strings.at(0), "double"))
{
model_constants.push_back(
dealii::Utilities::string_to_double(model_constants_strings.at(0)));
return dealii::Utilities::string_to_double(model_constants_strings.at(0));
}
else if (boost::iequals(model_constants_type_strings.at(0), "int"))
{
model_constants.push_back(
dealii::Utilities::string_to_int(model_constants_strings.at(0)));
return dealii::Utilities::string_to_int(model_constants_strings.at(0));
}
else if (boost::iequals(model_constants_type_strings.at(0), "bool"))
{
bool temp = boost::iequals(model_constants_strings.at(0), "true");
model_constants.push_back(temp);
return temp;
}
else
{
AssertThrow(false,
dealii::ExcMessage(
"PRISMS-PF Error: The type for user-defined variables must be "
"`double`, `int`, `bool`, `tensor`, or `elastic constants`."));
return 0;
}
}

Expand All @@ -1313,22 +1311,15 @@ void
userInputParameters<dim>::load_user_constants(inputFileReader &input_file_reader,
dealii::ParameterHandler &parameter_handler)
{
const unsigned int number_of_constants = input_file_reader.num_constants;

for (unsigned int i = 0; i < input_file_reader.model_constant_names.size(); i++)
{
model_constant_name_map[input_file_reader.model_constant_names[i]] = i;
}

for (unsigned int i = 0; i < number_of_constants; i++)
for (const std::string &constant_name : input_file_reader.model_constant_names)
{
std::string constants_text = "Model constant ";
constants_text.append(input_file_reader.model_constant_names[i]);
constants_text.append(constant_name);

std::vector<std::string> model_constants_strings =
dealii::Utilities::split_string_list(parameter_handler.get(constants_text));

assign_user_constant(model_constants_strings);
model_constants[constant_name] = construct_user_constant(model_constants_strings);
}
}

Expand Down
Loading