From 164d4bc1be3172ab0834888ea8cfacf8f2cec26e Mon Sep 17 00:00:00 2001 From: landinjm Date: Mon, 6 Jan 2025 12:18:50 -0500 Subject: [PATCH] exceptions --- .../CHiMaD_benchmark1c/customPDE.h | 4 +- .../CHiMaD_benchmark6b/customPDE.h | 2 + applications/allenCahn_conserved/customPDE.h | 10 +-- .../nonUniformDirichletBC.h | 3 +- include/core/exceptions.h | 21 +++++++ .../initial_conditions/initialConditions.h | 12 +--- include/core/matrixFreePDE.h | 1 - include/core/userInputParameters.h | 2 +- include/core/variableContainer.h | 1 - include/core/variableValueContainer.h | 22 ------- .../IntegrationTools/pfield/Mesh.hh | 41 +++++++----- .../pfield/interpolation/Interpolator.hh | 4 +- include/grains/OrderParameterRemapper.h | 6 +- include/utilities/utilities.h | 1 + src/core/checkpoint.cc | 3 +- .../initial_conditions/initialConditions.cc | 32 +--------- src/core/inputFileReader.cc | 1 + src/core/outputResults.cc | 62 ++++++++----------- src/core/solvers/solveIncrement.cc | 16 ++--- src/core/userInputParameters.cc | 14 ++--- src/core/variableAttributeLoader.cc | 2 +- src/core/variableContainer.cc | 1 + src/grains/FloodFiller.cc | 5 +- src/grains/OrderParameterRemapper.cc | 6 +- src/grains/reassignGrains.cc | 3 +- 25 files changed, 116 insertions(+), 159 deletions(-) create mode 100644 include/core/exceptions.h diff --git a/applications/CHiMaD_benchmarks/CHiMaD_benchmark1c/customPDE.h b/applications/CHiMaD_benchmarks/CHiMaD_benchmark1c/customPDE.h index 57392ceb8..15ec0da88 100644 --- a/applications/CHiMaD_benchmarks/CHiMaD_benchmark1c/customPDE.h +++ b/applications/CHiMaD_benchmarks/CHiMaD_benchmark1c/customPDE.h @@ -1,5 +1,3 @@ -#include - #include using namespace dealii; @@ -102,6 +100,8 @@ class customPDE : public MatrixFreePDE #include +#include + template void customPDE::create_triangulation( diff --git a/applications/CHiMaD_benchmarks/CHiMaD_benchmark6b/customPDE.h b/applications/CHiMaD_benchmarks/CHiMaD_benchmark6b/customPDE.h index 176c83294..9e08e5c71 100644 --- a/applications/CHiMaD_benchmarks/CHiMaD_benchmark6b/customPDE.h +++ b/applications/CHiMaD_benchmarks/CHiMaD_benchmark6b/customPDE.h @@ -106,6 +106,8 @@ class customPDE : public MatrixFreePDE #include +#include + template void customPDE::create_triangulation( diff --git a/applications/allenCahn_conserved/customPDE.h b/applications/allenCahn_conserved/customPDE.h index 0f169a694..cab5ba3af 100644 --- a/applications/allenCahn_conserved/customPDE.h +++ b/applications/allenCahn_conserved/customPDE.h @@ -106,6 +106,8 @@ class customPDE : public MatrixFreePDE // solve each time increment #include +#include + template void customPDE::solveIncrement(bool skip_time_dependent) @@ -298,10 +300,10 @@ customPDE::solveIncrement(bool skip_time_dependent) } else { - std::cerr << "PRISMS-PF Error: Nonlinear solver " - "tolerance types other than ABSOLUTE_CHANGE " - "have yet to be implemented." - << std::endl; + AssertThrow( + false, + FeatureNotImplemented( + "Nonlinear solver tolerances besides ABSOLUTE_CHANGE")); } } } diff --git a/include/core/boundary_conditions/nonUniformDirichletBC.h b/include/core/boundary_conditions/nonUniformDirichletBC.h index c043abacc..c3615664d 100644 --- a/include/core/boundary_conditions/nonUniformDirichletBC.h +++ b/include/core/boundary_conditions/nonUniformDirichletBC.h @@ -24,7 +24,8 @@ class NonUniformDirichletBC : public dealii::Function // IC for scalar values [[nodiscard]] double - value(const dealii::Point &p, const unsigned int component = 0) const override + value(const dealii::Point &p, + [[maybe_unused]] const unsigned int component = 0) const override { double scalar_BC = 0.0; dealii::Vector vector_BC(dim); diff --git a/include/core/exceptions.h b/include/core/exceptions.h new file mode 100644 index 000000000..73376bdd9 --- /dev/null +++ b/include/core/exceptions.h @@ -0,0 +1,21 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +#include + +using namespace dealii; + +/** + * Exception for parts of the library that have yet to be implemented yet. The argument is + * used to provide context for the feature that has yet to be implemented. + */ +DeclException1( + FeatureNotImplemented, + std::string, + << "The following feature has yet to be implemented in PRISMS-PF:\n" + << arg1 + << "\nCheck the issues section of PRISMS-PF's github to see if this feature is under " + "development. Additionally, please considering provided a patch to PRISMS-PF if you " + "feel that feature is worthwhile for yourself and others."); + +#endif \ No newline at end of file diff --git a/include/core/initial_conditions/initialConditions.h b/include/core/initial_conditions/initialConditions.h index 8fab9daae..17125e188 100644 --- a/include/core/initial_conditions/initialConditions.h +++ b/include/core/initial_conditions/initialConditions.h @@ -1,10 +1,3 @@ -/* - * initialConditions.h - * - * Created on: Feb 27, 2017 - * Author: stephendewitt - */ - #ifndef INCLUDE_INITIALCONDITIONS_H_ #define INCLUDE_INITIALCONDITIONS_H_ @@ -32,7 +25,8 @@ class InitialCondition : public dealii::Function // IC for scalar values [[nodiscard]] double - value(const dealii::Point &p, const unsigned int component = 0) const override + value(const dealii::Point &p, + [[maybe_unused]] const unsigned int component = 0) const override { double scalar_IC = 0.0; dealii::Vector vector_IC(dim); @@ -78,4 +72,4 @@ class InitialConditionVector : public dealii::Function MatrixFreePDE *matrix_free_pde; }; -#endif /* INCLUDE_INITIALCONDITIONS_H_ */ +#endif diff --git a/include/core/matrixFreePDE.h b/include/core/matrixFreePDE.h index c94a4d237..5f034f00a 100644 --- a/include/core/matrixFreePDE.h +++ b/include/core/matrixFreePDE.h @@ -3,7 +3,6 @@ // dealii headers #include -#include #include #include #include diff --git a/include/core/userInputParameters.h b/include/core/userInputParameters.h index fcc2401ce..5a9886580 100644 --- a/include/core/userInputParameters.h +++ b/include/core/userInputParameters.h @@ -5,7 +5,6 @@ #define INCLUDE_USERINPUTPARAMETERS_H_ #include -#include #include #include #include @@ -16,6 +15,7 @@ #include #include +#include #include #include #include diff --git a/include/core/variableContainer.h b/include/core/variableContainer.h index c6109dd14..f7f311d62 100644 --- a/include/core/variableContainer.h +++ b/include/core/variableContainer.h @@ -3,7 +3,6 @@ #ifndef VARIBLECONTAINER_H #define VARIBLECONTAINER_H -#include #include #include #include diff --git a/include/core/variableValueContainer.h b/include/core/variableValueContainer.h index 57bd1bbf7..f6f6832cd 100644 --- a/include/core/variableValueContainer.h +++ b/include/core/variableValueContainer.h @@ -43,26 +43,4 @@ class variableValueContainer unsigned int num_entries; }; -// void variableValueContainer::set(unsigned int global_variable_index, double -// variable_value){ -// var_index.push_back(global_variable_index); -// value.push_back(variable_value); -// num_entries++; -// } -// -// double variableValueContainer::operator()(unsigned int -// global_variable_index){ -// for (unsigned int i=0; i < num_entries; i++){ -// if (global_variable_index == var_index[i]){ -// return value[i]; -// } -// } -// -// // If this point is reached, the index isn't allowed so an error should -// be thrown std::cerr << "PRISMS-PF Error: Attempted access of a variable -// value that was not marked as needed in 'parameters.in'. Double-check the -// indices in user functions where a variable value is requested." << -// std::endl; abort(); -// } - #endif diff --git a/include/field_input/IntegrationTools/pfield/Mesh.hh b/include/field_input/IntegrationTools/pfield/Mesh.hh index adb994a24..70bbbd7e5 100644 --- a/include/field_input/IntegrationTools/pfield/Mesh.hh +++ b/include/field_input/IntegrationTools/pfield/Mesh.hh @@ -1,10 +1,9 @@ #ifndef Mesh_HH #define Mesh_HH -#include - #include #include +#include #include #include #include @@ -12,6 +11,7 @@ #include #include #include +#include #include namespace PRISMS @@ -329,15 +329,20 @@ namespace PRISMS std::cout << "Read rectilinear file and create mesh" << std::endl; bool mesh_as_points = true; - std::vector x_coord, y_coord, z_coord; + std::vector x_coord; + std::vector y_coord; + std::vector z_coord; std::istringstream ss; - std::string line, str, type; + std::string line; + std::string str; + std::string type; - unsigned int uli_dummy; - double d_dummy; + unsigned int uli_dummy = 0; - unsigned long int Npoints, Ncells, Ncell_numbers, u; + unsigned long int Npoints = 0; + unsigned long int Ncells = 0; + unsigned long int u = 0; std::vector cell_node; PRISMS::Coordinate _coord; @@ -364,7 +369,7 @@ namespace PRISMS std::cout << " reserve OK" << std::endl; for (unsigned int i = 0; i < Npoints; i++) { - float temp_coord; + float temp_coord = NAN; infile >> temp_coord; @@ -389,7 +394,7 @@ namespace PRISMS std::cout << " reserve OK" << std::endl; for (unsigned int i = 0; i < Npoints; i++) { - float temp_coord; + float temp_coord = NAN; infile >> temp_coord; @@ -414,7 +419,7 @@ namespace PRISMS std::cout << " reserve OK" << std::endl; for (unsigned int i = 0; i < Npoints; i++) { - float temp_coord; + float temp_coord = NAN; infile >> temp_coord; @@ -440,7 +445,9 @@ namespace PRISMS } // interpolated coordinates for each node of a cell - std::vector COORD_X(Npoints), COORD_Y(Npoints), COORD_Z(Npoints); + std::vector COORD_X(Npoints); + std::vector COORD_Y(Npoints); + std::vector COORD_Z(Npoints); u = 0; //(defined at the beginning of read_vtk) if (dim > 2) @@ -527,7 +534,9 @@ namespace PRISMS } for (int m = 0; m < dim; m++) - add_once(value[m], hist[m], _coord[m]); + { + add_once(value[m], hist[m], _coord[m]); + } _node.push_back(_coord); } @@ -560,11 +569,15 @@ namespace PRISMS } std::cout << " Min Coordinate: "; for (int j = 0; j < dim; j++) - std::cout << _min[j] << " "; + { + std::cout << _min[j] << " "; + } std::cout << std::endl; std::cout << " Max Coordinate: "; for (int j = 0; j < dim; j++) - std::cout << _max[j] << " "; + { + std::cout << _max[j] << " "; + } std::cout << std::endl; std::cout << " done" << std::endl; diff --git a/include/field_input/IntegrationTools/pfield/interpolation/Interpolator.hh b/include/field_input/IntegrationTools/pfield/interpolation/Interpolator.hh index c760162ab..911f8a2ba 100644 --- a/include/field_input/IntegrationTools/pfield/interpolation/Interpolator.hh +++ b/include/field_input/IntegrationTools/pfield/interpolation/Interpolator.hh @@ -58,14 +58,14 @@ namespace PRISMS } virtual bool - is_in_range(const Coordinate &coord) + is_in_range([[maybe_unused]] const Coordinate &coord) { undefined("bool is_in_range(Coordinate coord) const"); return false; } virtual double - operator()(const Coordinate &coord) + operator()([[maybe_unused]] const Coordinate &coord) { undefined("double operator()(Coordinate coord)"); return double(); diff --git a/include/grains/OrderParameterRemapper.h b/include/grains/OrderParameterRemapper.h index f15cecca6..a935ff6fb 100644 --- a/include/grains/OrderParameterRemapper.h +++ b/include/grains/OrderParameterRemapper.h @@ -25,8 +25,7 @@ class OrderParameterRemapper std::vector> &grain_representations, std::vector *> &solution_fields, dealii::DoFHandler &dof_handler, - unsigned int dofs_per_cell, - double buffer); + unsigned int dofs_per_cell); /** * This method does the core work of the class to reassign grains across @@ -39,8 +38,7 @@ class OrderParameterRemapper const dealii::LinearAlgebra::distributed::Vector *grain_index_field, std::vector *> &solution_fields, dealii::DoFHandler &dof_handler, - unsigned int dofs_per_cell, - double buffer); + unsigned int dofs_per_cell); protected: }; diff --git a/include/utilities/utilities.h b/include/utilities/utilities.h index 23e2e51b8..3f67f2a16 100644 --- a/include/utilities/utilities.h +++ b/include/utilities/utilities.h @@ -4,6 +4,7 @@ #include #include +#include #include /** diff --git a/src/core/checkpoint.cc b/src/core/checkpoint.cc index e6fdc2aae..002039486 100644 --- a/src/core/checkpoint.cc +++ b/src/core/checkpoint.cc @@ -1,8 +1,7 @@ -#include - #include #include +#include #include #include #include diff --git a/src/core/initial_conditions/initialConditions.cc b/src/core/initial_conditions/initialConditions.cc index 2d26eb50a..f85e2139b 100644 --- a/src/core/initial_conditions/initialConditions.cc +++ b/src/core/initial_conditions/initialConditions.cc @@ -1,6 +1,5 @@ -// methods to apply initial conditions - #include +#include #include #include #include @@ -229,8 +228,7 @@ MatrixFreePDE::applyInitialConditions() &grain_index_field, solutionSet, *dofHandlersSet_nonconst.at(scalar_field_index), - FESet.at(scalar_field_index)->dofs_per_cell, - userInputs.buffer_between_grains); + FESet.at(scalar_field_index)->dofs_per_cell); // Smooth the order parameters according to Fick's 2nd Law // In the time cycle below, we evolve the weak form of Eq.: @@ -423,29 +421,3 @@ MatrixFreePDE::applyInitialConditions() } } } - -// ================================================================================= - -// I don't think vector fields are implemented in PFields yet -// template -// class InitialConditionPFieldVec : public Function -//{ -// public: -// unsigned int index; -// Vector values; -// typedef PRISMS::PField ScalarField2D; -// ScalarField2D &inputField; -// -// InitialConditionPFieldVec (const unsigned int _index, ScalarField2D -// &_inputField) : Function(1), index(_index), inputField(_inputField) {} -// -// void vector_value (const Point &p,Vector &vector_IC) const -// { -// double coord[dim]; -// for (unsigned int i = 0; i < dim; i++){ -// coord[i] = p(i); -// } -// -// vector_IC = inputField(coord); -// } -//}; diff --git a/src/core/inputFileReader.cc b/src/core/inputFileReader.cc index 6befb36c6..16129d6f2 100644 --- a/src/core/inputFileReader.cc +++ b/src/core/inputFileReader.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include #include diff --git a/src/core/outputResults.cc b/src/core/outputResults.cc index 2ca7b0bb1..2a52cc47a 100644 --- a/src/core/outputResults.cc +++ b/src/core/outputResults.cc @@ -1,12 +1,10 @@ -// outputResults() method for MatrixFreePDE class - #include #include #include #include +#include -// output results template void MatrixFreePDE::outputResults() @@ -141,20 +139,15 @@ MatrixFreePDE::outputResults() cycleAsString << std::setw(std::floor(std::log10(userInputs.totalIncrements)) + 1) << std::setfill('0') << currentIncrement; - char baseFileName[100]; - char vtuFileName[100]; - - snprintf(baseFileName, - sizeof(baseFileName), - "%s-%s", - userInputs.output_file_name.c_str(), - cycleAsString.str().c_str()); - snprintf(vtuFileName, - sizeof(vtuFileName), - "%s.%u.%s", - baseFileName, - Utilities::MPI::this_mpi_process(MPI_COMM_WORLD), - userInputs.output_file_type.c_str()); + std::ostringstream baseFileNameStream; + baseFileNameStream << userInputs.output_file_name << "-" << cycleAsString.str(); + std::string baseFileName = baseFileNameStream.str(); + + std::ostringstream vtuFileNameStream; + vtuFileNameStream << baseFileName << "." + << Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) << "." + << userInputs.output_file_type; + std::string vtuFileName = vtuFileNameStream.str(); // Write to file in either vtu or vtk format if (userInputs.output_file_type == "vtu") @@ -181,22 +174,19 @@ MatrixFreePDE::outputResults() i < Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD); ++i) { - char vtuProcFileName[100]; - snprintf(vtuProcFileName, - sizeof(vtuProcFileName), - "%s-%s.%u.%s", - userInputs.output_file_name.c_str(), - cycleAsString.str().c_str(), - i, - userInputs.output_file_type.c_str()); + std::ostringstream vtuProcFileNameStream; + vtuProcFileNameStream << userInputs.output_file_name << "-" + << cycleAsString.str() << "." << i << "." + << "userInputs.output_file_type"; + std::string vtuProcFileName = vtuProcFileNameStream.str(); + filenames.emplace_back(vtuProcFileName); } - char pvtuFileName[100]; - snprintf(pvtuFileName, - sizeof(pvtuFileName), - "%s.p%s", - baseFileName, - userInputs.output_file_type.c_str()); + + std::ostringstream pvtuFileNameStream; + pvtuFileNameStream << baseFileName << ".p" << userInputs.output_file_type; + std::string pvtuFileName = pvtuFileNameStream.str(); + std::ofstream master_output(pvtuFileName); data_out.write_pvtu_record(master_output, filenames); @@ -206,12 +196,10 @@ MatrixFreePDE::outputResults() else { // Write the results to a file shared between all processes - char svtuFileName[100]; - snprintf(svtuFileName, - sizeof(svtuFileName), - "%s.%s", - baseFileName, - userInputs.output_file_type.c_str()); + std::ostringstream svtuFileNameStream; + svtuFileNameStream << baseFileName << "." << userInputs.output_file_type; + std::string svtuFileName = svtuFileNameStream.str(); + data_out.write_vtu_in_parallel(svtuFileName, MPI_COMM_WORLD); pcout << "Output written to:" << svtuFileName << "\n\n"; } diff --git a/src/core/solvers/solveIncrement.cc b/src/core/solvers/solveIncrement.cc index 51bf6b41b..9449fcf18 100644 --- a/src/core/solvers/solveIncrement.cc +++ b/src/core/solvers/solveIncrement.cc @@ -1,8 +1,7 @@ -// solveIncrement() method for MatrixFreePDE class - #include #include +#include #include // solve each time increment @@ -183,9 +182,10 @@ MatrixFreePDE::solveIncrement(bool skip_time_dependent) } else { - std::cerr << "PRISMS-PF Error: Nonlinear solver tolerance " - "types other than ABSOLUTE_CHANGE have yet to " - "be implemented.\n"; + AssertThrow( + false, + FeatureNotImplemented( + "Nonlinear solver tolerances besides ABSOLUTE_CHANGE")); } } } @@ -501,9 +501,9 @@ MatrixFreePDE::updateImplicitSolution(unsigned int fieldIndex, } else { - std::cerr << "PRISMS-PF Error: Nonlinear solver tolerance " - "types other than ABSOLUTE_CHANGE have yet to " - "be implemented.\n"; + AssertThrow(false, + FeatureNotImplemented( + "Nonlinear solver tolerances besides ABSOLUTE_CHANGE")); } } else diff --git a/src/core/userInputParameters.cc b/src/core/userInputParameters.cc index cfdd7e22c..10e50357f 100644 --- a/src/core/userInputParameters.cc +++ b/src/core/userInputParameters.cc @@ -1,11 +1,9 @@ -// Methods for the userInputParameters class -#include #include #include +#include #include -// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init, hicpp-member-init) template userInputParameters::userInputParameters(inputFileReader &input_file_reader, dealii::ParameterHandler ¶meter_handler) @@ -45,8 +43,6 @@ userInputParameters::userInputParameters(inputFileReader &input_fi load_model_constants(input_file_reader, parameter_handler); } -// NOLINTEND(cppcoreguidelines-pro-type-member-init, hicpp-member-init) - template void userInputParameters::loadVariableAttributes() @@ -442,11 +438,9 @@ userInputParameters::assign_linear_solve_parameters( else if (boost::iequals(type_string, "ABSOLUTE_SOLUTION_CHANGE")) { temp_type = ABSOLUTE_SOLUTION_CHANGE; - std::cerr << "PRISMS-PF Error: Linear solver tolerance type " - << type_string - << " is not currently implemented, please use either " - "ABSOLUTE_RESIDUAL or RELATIVE_RESIDUAL_CHANGE\n"; - abort(); + AssertThrow(false, + FeatureNotImplemented( + "Linear solver tolerance ABSOLUTE_SOLUTION_CHANGE")); } else { diff --git a/src/core/variableAttributeLoader.cc b/src/core/variableAttributeLoader.cc index f81658865..14f7d9ccf 100644 --- a/src/core/variableAttributeLoader.cc +++ b/src/core/variableAttributeLoader.cc @@ -1,6 +1,6 @@ -#include #include +#include #include void diff --git a/src/core/variableContainer.cc b/src/core/variableContainer.cc index 71ae32b31..7d5bcf450 100644 --- a/src/core/variableContainer.cc +++ b/src/core/variableContainer.cc @@ -1,3 +1,4 @@ +#include #include template diff --git a/src/grains/FloodFiller.cc b/src/grains/FloodFiller.cc index 2f8844ad7..32e79ac94 100644 --- a/src/grains/FloodFiller.cc +++ b/src/grains/FloodFiller.cc @@ -75,7 +75,6 @@ FloodFiller::calcGrainSets( mergeSplitGrains(grain_sets); } -// NOLINTBEGIN(misc-no-recursion) template template void @@ -184,8 +183,6 @@ FloodFiller::recursiveFloodFill( } } -// NOLINTEND(misc-no-recursion) - // ================================================================================= // All-to-all communication of the grain sets // ================================================================================= @@ -245,7 +242,7 @@ FloodFiller::createGlobalGrainSetList( // Communicate the order_parameters std::vector offset(numProcs, 0); - for (int n = 1; n < numProcs; n++) + for (unsigned int n = 1; n < numProcs; n++) { offset[n] = offset[n - 1] + num_grains_per_core[n - 1]; } diff --git a/src/grains/OrderParameterRemapper.cc b/src/grains/OrderParameterRemapper.cc index 892f111e3..5803290eb 100644 --- a/src/grains/OrderParameterRemapper.cc +++ b/src/grains/OrderParameterRemapper.cc @@ -6,8 +6,7 @@ OrderParameterRemapper::remap( std::vector> &grain_representations, std::vector *> &solution_fields, dealii::DoFHandler &dof_handler, - unsigned int dofs_per_cell, - double buffer) + unsigned int dofs_per_cell) { for (unsigned int g = 0; g < grain_representations.size(); g++) { @@ -112,8 +111,7 @@ OrderParameterRemapper::remap_from_index_field( const dealii::LinearAlgebra::distributed::Vector *grain_index_field, std::vector *> &solution_fields, dealii::DoFHandler &dof_handler, - unsigned int dofs_per_cell, - double buffer) + unsigned int dofs_per_cell) { for (unsigned int g = 0; g < grain_representations.size(); g++) { diff --git a/src/grains/reassignGrains.cc b/src/grains/reassignGrains.cc index ec28ec3e6..d08300c2e 100644 --- a/src/grains/reassignGrains.cc +++ b/src/grains/reassignGrains.cc @@ -103,8 +103,7 @@ MatrixFreePDE::reassignGrains() order_parameter_remapper.remap(simplified_grain_representations, solutionSet, *dofHandlersSet_nonconst.at(scalar_field_index), - FESet.at(scalar_field_index)->dofs_per_cell, - userInputs.buffer_between_grains); + FESet.at(scalar_field_index)->dofs_per_cell); pcout << "Reassigning grains completed.\n\n";