From e2fc8057e05c685637328df6d89b60e581df8719 Mon Sep 17 00:00:00 2001 From: Xander Date: Wed, 25 Dec 2024 00:05:52 -0500 Subject: [PATCH] replace vector of model constants with map --- include/core/inputFileReader.h | 4 +-- include/core/userInputParameters.h | 53 ++++++++++++++---------------- src/core/inputFileReader.cc | 14 +++++--- src/core/userInputParameters.cc | 41 +++++++++-------------- 4 files changed, 52 insertions(+), 60 deletions(-) diff --git a/include/core/inputFileReader.h b/include/core/inputFileReader.h index ba27f943..75d81d90 100644 --- a/include/core/inputFileReader.h +++ b/include/core/inputFileReader.h @@ -44,7 +44,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 + [[nodiscard]] static std::set get_entry_name_ending_list(const std::string ¶meters_file_name, const std::string &keyword, const std::string &entry_name_begining); @@ -82,7 +82,7 @@ class inputFileReader const AttributesList &pp_attributes; dealii::ParameterHandler parameter_handler; unsigned int num_constants; - std::vector model_constant_names; + std::set model_constant_names; unsigned int number_of_dimensions; }; diff --git a/include/core/userInputParameters.h b/include/core/userInputParameters.h index 87225450..073a2285 100644 --- a/include/core/userInputParameters.h +++ b/include/core/userInputParameters.h @@ -25,6 +25,14 @@ #include #include +template +using InputVariant = boost::variant, + dealii::Tensor<2, dim>, + dealii::Tensor<2, 2 * dim - 1 + dim / 3>>; + enum elasticityModel { ISOTROPIC, @@ -59,9 +67,6 @@ class userInputParameters assign_boundary_conditions(std::vector &boundary_condition_list, varBCs &boundary_condition); - // Map linking the model constant name to its index - std::unordered_map 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. @@ -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(model_constants[model_constant_name_map.at(constant_name)]); + return boost::get(model_constants.at(constant_name)); }; /** @@ -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(model_constants[model_constant_name_map.at(constant_name)]); + return boost::get(model_constants.at(constant_name)); }; /** @@ -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(model_constants[model_constant_name_map.at(constant_name)]); + return boost::get(model_constants.at(constant_name)); }; /** @@ -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>( - model_constants[model_constant_name_map.at(constant_name)]); + return boost::get>(model_constants.at(constant_name)); }; /** @@ -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>( - model_constants[model_constant_name_map.at(constant_name)]); + return boost::get>(model_constants.at(constant_name)); }; /** @@ -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>( - model_constants[model_constant_name_map.at(constant_name)]); + model_constants.at(constant_name)); }; // Method to load in the variable attributes @@ -292,13 +295,7 @@ class userInputParameters std::vector> BC_list; // List of user-defined constants - std::vector, - dealii::Tensor<2, dim>, - dealii::Tensor<2, 2 * dim - 1 + dim / 3>>> - model_constants; + std::map> model_constants; // Nucleation parameters bool nucleation_occurs; @@ -434,14 +431,14 @@ class userInputParameters /** * \brief Assign the specified user constant to whatever type. */ - void - assign_user_constant(std::vector &model_constants_strings); + InputVariant + construct_user_constant(std::vector &model_constants_strings); /** * \brief Assign the primitive user constants (e.g., int, double, bool). */ - void - assign_primitive_user_constant(std::vector &model_constants_strings); + InputVariant + primitive_user_constant(std::vector &model_constants_strings); [[nodiscard]] dealii::Tensor<2, 2 * dim - 1 + dim / 3> get_Cij_tensor(std::vector elastic_constants, diff --git a/src/core/inputFileReader.cc b/src/core/inputFileReader.cc index 72de4073..efb9364d 100644 --- a/src/core/inputFileReader.cc +++ b/src/core/inputFileReader.cc @@ -236,7 +236,7 @@ inputFileReader::get_number_of_entries(const std::string ¶meters_file_name, // Method to parse an input file to get a list of variables from related // subsections -std::vector +std::set inputFileReader::get_entry_name_ending_list(const std::string ¶meters_file_name, const std::string &keyword, const std::string &entry_name_begining) @@ -248,7 +248,7 @@ inputFileReader::get_entry_name_ending_list(const std::string ¶meters_file_n std::string entry; bool found_entry = false; - std::vector entry_name_end_list; + std::set entry_name_end_list; // Loop through each line while (std::getline(input_file, line)) @@ -282,7 +282,11 @@ inputFileReader::get_entry_name_ending_list(const std::string ¶meters_file_n } // Add it to the list - entry_name_end_list.push_back(entry); + Assert(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; @@ -805,10 +809,10 @@ inputFileReader::declare_parameters(dealii::ParameterHandler ¶meter_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(), diff --git a/src/core/userInputParameters.cc b/src/core/userInputParameters.cc index 48e7abf1..d69ccd46 100644 --- a/src/core/userInputParameters.cc +++ b/src/core/userInputParameters.cc @@ -1203,8 +1203,8 @@ userInputParameters::compute_rank_2_tensor_constant( } template -void -userInputParameters::assign_user_constant( +InputVariant +userInputParameters::construct_user_constant( std::vector &model_constants_strings) { // Ensure that the input includes a value and a type @@ -1219,7 +1219,7 @@ userInputParameters::assign_user_constant( if (model_constants_strings.size() == 2) { - assign_primitive_user_constant(model_constants_strings); + return primitive_user_constant(model_constants_strings); } else { @@ -1234,14 +1234,12 @@ userInputParameters::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") && @@ -1262,7 +1260,7 @@ userInputParameters::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 { @@ -1271,12 +1269,13 @@ userInputParameters::assign_user_constant( "PRISMS-PF ERROR: Only user-defined constant tensors may " "have multiple elements.")); } + return 0; } } template -void -userInputParameters::assign_primitive_user_constant( +InputVariant +userInputParameters::primitive_user_constant( std::vector &model_constants_strings) { std::vector model_constants_type_strings = @@ -1286,18 +1285,16 @@ userInputParameters::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 { @@ -1305,6 +1302,7 @@ userInputParameters::assign_primitive_user_constant( dealii::ExcMessage( "PRISMS-PF Error: The type for user-defined variables must be " "`double`, `int`, `bool`, `tensor`, or `elastic constants`.")); + return 0; } } @@ -1313,22 +1311,15 @@ void userInputParameters::load_user_constants(inputFileReader &input_file_reader, dealii::ParameterHandler ¶meter_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 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); } }