From: David Wells Date: Sat, 29 Oct 2022 17:18:21 +0000 (-0400) Subject: Make the error message for non-integer IDs in UNV files clearer. X-Git-Tag: v9.5.0-rc1~888^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F14381%2Fhead;p=dealii.git Make the error message for non-integer IDs in UNV files clearer. --- diff --git a/source/grid/grid_in.cc b/source/grid/grid_in.cc index abe39ce8d4..cd75e69224 100644 --- a/source/grid/grid_in.cc +++ b/source/grid/grid_in.cc @@ -819,10 +819,10 @@ GridIn::read_unv(std::istream &in) while (tmp != -1) // we do until reach end of 2467 or 2477 { - int n_entities; // number of entities in group - int id; // id is either material or bc - int no; // unv - int dummy; + int n_entities; // number of entities in group + long id; // id is either material or bc + int no; // unv + int dummy; AssertThrow(in.fail() == false, ExcIO()); in >> dummy; @@ -835,7 +835,37 @@ GridIn::read_unv(std::istream &in) n_entities; AssertThrow(in.fail() == false, ExcIO()); - in >> id; + // Occasionally we encounter IDs that are not integers - we don't + // support that case since there is no sane way for us to determine + // integer IDs from, e.g., strings. + std::string line; + // The next character in the input buffer is a newline character so + // we need a call to std::getline() to retrieve it (it is logically + // a line): + std::getline(in, line); + AssertThrow(line.size() == 0, + ExcMessage( + "The line before the line containing an ID has too " + "many entries. This is not a valid UNV file.")); + // now get the line containing the id: + std::getline(in, line); + AssertThrow(in.fail() == false, ExcIO()); + std::istringstream id_stream(line); + id_stream >> id; + AssertThrow( + !id_stream.fail() && id_stream.eof(), + ExcMessage( + "The given UNV file contains a boundary or material id set to '" + + line + + "', which cannot be parsed as a fixed-width integer, whereas " + "deal.II only supports integer boundary and material ids. To fix " + "this, ensure that all such ids are given integer values.")); + AssertThrow( + 0 <= id && + id <= long(std::numeric_limits::max()), + ExcMessage("The provided integer id '" + std::to_string(id) + + "' is not convertible to either types::material_id nor " + "types::boundary_id.")); const unsigned int n_lines = (n_entities % 2 == 0) ? (n_entities / 2) : ((n_entities + 1) / 2); @@ -859,11 +889,11 @@ GridIn::read_unv(std::istream &in) cells[cell_indices[no]].material_id = id; if (line_indices.count(no) > 0) // boundary line - bc - subcelldata.boundary_lines[line_indices[no]].material_id = + subcelldata.boundary_lines[line_indices[no]].boundary_id = id; if (quad_indices.count(no) > 0) // boundary quad - bc - subcelldata.boundary_quads[quad_indices[no]].material_id = + subcelldata.boundary_quads[quad_indices[no]].boundary_id = id; } }