From 22d08bfba3bc180befe4a6595b4dd5ca33f57cbe Mon Sep 17 00:00:00 2001 From: Jan Nikl Date: Wed, 11 Mar 2026 16:57:23 -0700 Subject: [PATCH 1/3] Added settings commands to StreamReader. --- lib/stream_reader.cpp | 98 +++++++++++++++++++++++++++++++++++++++---- lib/stream_reader.hpp | 2 + 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index c88766b1..3416416c 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -19,6 +19,11 @@ using namespace mfem; enum class Command { + // Settings + FixOrientation, + SaveColoring, + KeepAttributes, + // Solution Mesh, Solution, Quadrature, @@ -66,6 +71,11 @@ static const StreamCommands commands; StreamCommands::StreamCommands() { + // Settings + (*this)[Command::FixOrientation] = {"fix_orientations", false, "<0/off/1/on>", "Turn off/on fix of the orientations for inverted elements."}; + (*this)[Command::SaveColoring] = {"save_coloring", false, "<0/off/1/on>", "Turn off/on saving of the mesh coloring generated when opening only a mesh."}; + (*this)[Command::KeepAttributes] = {"keep_attributes", false, "", "Toggles between processor rank and real attributes when loading a parallel solution."}; + // Solution (*this)[Command::Mesh] = {"mesh", false, "", "Visualize the mesh."}; (*this)[Command::Solution] = {"solution", false, " ", "Visualize the solution."}; (*this)[Command::Quadrature] = {"quadrature", false, " ", "Visualize the quadrature."}; @@ -120,6 +130,25 @@ int StreamReader::ReadStream( data.SetMesh(NULL); data.keys.clear(); + string str_cmd = data_type; + + while (true) + { + int err = ReadStreamOne(is, str_cmd); + if (err) { return err; } + + if (data.mesh) { break; } + + // load next command + is >> ws >> str_cmd; + } + + data.ExtrudeMeshAndSolution(); + return 0; +} + +int StreamReader::ReadStreamOne(istream &is, const string &data_type) +{ auto it = find(commands.begin(), commands.end(), data_type); if (it == commands.end()) { @@ -131,6 +160,27 @@ int StreamReader::ReadStream( Command cmd = (Command)(it - commands.begin()); switch (cmd) { + case Command::FixOrientation: + { + string mode; + is >> ws >> mode; + data.fix_elem_orient = (mode != "off" && mode != "0"); + } + break; + case Command::SaveColoring: + { + string mode; + is >> ws >> mode; + data.save_coloring = (mode != "off" && mode != "0"); + } + break; + case Command::KeepAttributes: + { + string mode; + is >> ws >> mode; + data.keep_attr = (mode != "proc"); + } + break; case Command::Fem2D: { Vector sol; @@ -313,7 +363,7 @@ int StreamReader::ReadStream( } break; case Command::Max: //dummy - break; + return 1; } if (commands[cmd].keys) @@ -321,7 +371,6 @@ int StreamReader::ReadStream( is >> data.keys; } - data.ExtrudeMeshAndSolution(); return 0; } @@ -333,8 +382,6 @@ int StreamReader::ReadStreams(const StreamCollection &input_streams) std::vector cgf_array(nproc); std::vector qf_array(nproc); - std::string data_type; - int gf_count = 0; int cgf_count = 0; int qf_count = 0; @@ -346,10 +393,45 @@ int StreamReader::ReadStreams(const StreamCollection &input_streams) #endif istream &isock = *input_streams[p]; // assuming the "parallel nproc p" part of the stream has been read - isock >> ws >> data_type >> ws; // "*_data" / "mesh" / "solution" + bool load_data = false; + Command cmd; + while (!load_data) + { + std::string data_type; + isock >> ws >> data_type; + + auto it = find(commands.begin(), commands.end(), data_type); + if (it == commands.end()) + { + cerr << "Unknown data format " << data_type << endl; + PrintCommands(); + return 1; + } + + cmd = (Command)(it - commands.begin()); + switch (cmd) + { + case Command::FixOrientation: + case Command::SaveColoring: + case Command::KeepAttributes: + // process on root + ReadStreamOne(isock, data_type); + break; + case Command::Mesh: + case Command::Solution: + case Command::Quadrature: #ifdef GLVIS_DEBUG - cout << " type " << data_type << " ... " << flush; + cout << " type " << data_type << " ... " << flush; #endif + load_data = true; + break; + default: + cerr << "Command not available in parallel"; + return 1; + } + } + + isock >> ws; mesh_array[p] = new Mesh(isock, 1, 0, data.fix_elem_orient); if (!data.keep_attr) { @@ -364,12 +446,12 @@ int StreamReader::ReadStreams(const StreamCollection &input_streams) } } gf_array[p] = NULL; - if (data_type == "quadrature") + if (cmd == Command::Quadrature) { qf_array[p] = new QuadratureFunction(mesh_array[p], isock); qf_count++; } - else if (data_type != "mesh") + else if (cmd != Command::Mesh) { if (CheckStreamIsComplex(isock, true)) { diff --git a/lib/stream_reader.hpp b/lib/stream_reader.hpp index e5ed9672..aaae3c20 100644 --- a/lib/stream_reader.hpp +++ b/lib/stream_reader.hpp @@ -26,6 +26,8 @@ class StreamReader static bool CheckStreamIsComplex(std::istream &sol, bool parallel = false); + int ReadStreamOne(std::istream &is, const std::string &data_type); + public: StreamReader(DataState &data_): data(data_) { } From 174a9b4a2c5907879c79c53c7e3e57627d803870 Mon Sep 17 00:00:00 2001 From: Jan Nikl Date: Wed, 11 Mar 2026 19:30:52 -0700 Subject: [PATCH 2/3] Fixed shadowing. --- lib/stream_reader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index 3416416c..25264242 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -134,8 +134,8 @@ int StreamReader::ReadStream( while (true) { - int err = ReadStreamOne(is, str_cmd); - if (err) { return err; } + int ierr = ReadStreamOne(is, str_cmd); + if (ierr) { return ierr; } if (data.mesh) { break; } From 873359df7c8eee30b75ca1e7dc8a99706288e3d2 Mon Sep 17 00:00:00 2001 From: Jan Nikl Date: Wed, 11 Mar 2026 19:38:53 -0700 Subject: [PATCH 3/3] Minor changes based on review. --- lib/stream_reader.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/stream_reader.cpp b/lib/stream_reader.cpp index 25264242..4621140f 100644 --- a/lib/stream_reader.cpp +++ b/lib/stream_reader.cpp @@ -20,7 +20,7 @@ using namespace mfem; enum class Command { // Settings - FixOrientation, + FixOrientations, SaveColoring, KeepAttributes, // Solution @@ -72,9 +72,9 @@ static const StreamCommands commands; StreamCommands::StreamCommands() { // Settings - (*this)[Command::FixOrientation] = {"fix_orientations", false, "<0/off/1/on>", "Turn off/on fix of the orientations for inverted elements."}; + (*this)[Command::FixOrientations] = {"fix_orientations", false, "<0/off/1/on>", "Turn off/on fix of the orientations for inverted elements."}; (*this)[Command::SaveColoring] = {"save_coloring", false, "<0/off/1/on>", "Turn off/on saving of the mesh coloring generated when opening only a mesh."}; - (*this)[Command::KeepAttributes] = {"keep_attributes", false, "", "Toggles between processor rank and real attributes when loading a parallel solution."}; + (*this)[Command::KeepAttributes] = {"keep_attributes", false, "", "Toggles between processor rank and real attributes when loading a parallel solution."}; // Solution (*this)[Command::Mesh] = {"mesh", false, "", "Visualize the mesh."}; (*this)[Command::Solution] = {"solution", false, " ", "Visualize the solution."}; @@ -160,7 +160,7 @@ int StreamReader::ReadStreamOne(istream &is, const string &data_type) Command cmd = (Command)(it - commands.begin()); switch (cmd) { - case Command::FixOrientation: + case Command::FixOrientations: { string mode; is >> ws >> mode; @@ -178,7 +178,7 @@ int StreamReader::ReadStreamOne(istream &is, const string &data_type) { string mode; is >> ws >> mode; - data.keep_attr = (mode != "proc"); + data.keep_attr = (mode != "proc" && mode != "off" && mode != "0"); } break; case Command::Fem2D: @@ -411,10 +411,10 @@ int StreamReader::ReadStreams(const StreamCollection &input_streams) cmd = (Command)(it - commands.begin()); switch (cmd) { - case Command::FixOrientation: + case Command::FixOrientations: case Command::SaveColoring: case Command::KeepAttributes: - // process on root + // assume the options are the same on all ranks ReadStreamOne(isock, data_type); break; case Command::Mesh: