diff --git a/src/language/PugsParser.cpp b/src/language/PugsParser.cpp index f710ebe9ef9235c5e801a4a2c070a5414e163fc0..77bcd91f571ce75b9dcc3d8c672ad2a91f15d664 100644 --- a/src/language/PugsParser.cpp +++ b/src/language/PugsParser.cpp @@ -39,6 +39,15 @@ #include <unordered_map> #include <variant> +void +clear_symbol_tables(std::unique_ptr<ASTNode>& node) +{ + for (auto& child : node->children) { + clear_symbol_tables(child); + } + node->m_symbol_table->clearValues(); +} + void parser(const std::string& filename) { @@ -96,6 +105,7 @@ parser(const std::string& filename) } catch (language::Exit& e) { ExecutionStatManager::getInstance().setExitCode(e.code()); + clear_symbol_tables(root_node); } root_node->m_symbol_table->clearValues(); diff --git a/src/mesh/ItemArrayUtils.hpp b/src/mesh/ItemArrayUtils.hpp index 0d90f77af27add5f08982e2be535bd7b779837a9..cb8ee339b56a3f127b3f446b7ee28970b31aa143 100644 --- a/src/mesh/ItemArrayUtils.hpp +++ b/src/mesh/ItemArrayUtils.hpp @@ -165,7 +165,7 @@ max(const ItemArray<DataType, item_type, ConnectivityPtr>& item_array) void init(data_type& value) const { - value = std::numeric_limits<data_type>::min(); + value = std::numeric_limits<data_type>::lowest(); } PUGS_INLINE diff --git a/src/mesh/ItemValueUtils.hpp b/src/mesh/ItemValueUtils.hpp index e64f2e1bdc0bc1e934aac22258461947c1c0ffd3..031caa640dbc8617ca4514c513035975451f7939 100644 --- a/src/mesh/ItemValueUtils.hpp +++ b/src/mesh/ItemValueUtils.hpp @@ -156,7 +156,7 @@ max(const ItemValue<DataType, item_type, ConnectivityPtr>& item_value) void init(data_type& value) const { - value = std::numeric_limits<data_type>::min(); + value = std::numeric_limits<data_type>::lowest(); } PUGS_INLINE diff --git a/src/mesh/MeshNodeBoundaryUtils.cpp b/src/mesh/MeshNodeBoundaryUtils.cpp index d6a0e31cd9a671530216b9dce15747e07f06c9b4..39715d33d2f80da11dc9b11d1433320305fe2715 100644 --- a/src/mesh/MeshNodeBoundaryUtils.cpp +++ b/src/mesh/MeshNodeBoundaryUtils.cpp @@ -17,7 +17,7 @@ getBounds(const Mesh<2>& mesh, const RefNodeList& ref_node_list) R2& xmax = bounds[1]; xmin = R2{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; - xmax = R2{-std::numeric_limits<double>::max(), -std::numeric_limits<double>::max()}; + xmax = R2{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest()}; auto update_xmin = [](const R2& x, R2& x_min) { if ((x[0] < x_min[0]) or ((x[0] == x_min[0]) and (x[1] < x_min[1]))) { @@ -118,12 +118,12 @@ getBounds(const Mesh<3>& mesh, const RefNodeList& ref_node_list) ymin = R3{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; zmin = R3{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; - xmax = - -R3{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; - ymax = - -R3{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; - zmax = - -R3{std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}; + xmax = R3{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), + std::numeric_limits<double>::lowest()}; + ymax = R3{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), + std::numeric_limits<double>::lowest()}; + zmax = R3{std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), + std::numeric_limits<double>::lowest()}; const NodeValue<const R3>& xr = mesh.xr(); diff --git a/src/mesh/SubItemArrayPerItemUtils.hpp b/src/mesh/SubItemArrayPerItemUtils.hpp index aec08ef845cc80bafd20ec8fa1a3b9bcc94415de..70b835ae3d8ff75c0fad7c9cd1b13c6cfb8ad998 100644 --- a/src/mesh/SubItemArrayPerItemUtils.hpp +++ b/src/mesh/SubItemArrayPerItemUtils.hpp @@ -170,7 +170,7 @@ max(const SubItemArrayPerItem<DataType, ItemOfItem, ConnectivityPtr>& sub_item_a void init(data_type& value) const { - value = std::numeric_limits<data_type>::min(); + value = std::numeric_limits<data_type>::lowest(); } PUGS_INLINE diff --git a/src/mesh/SubItemValuePerItemUtils.hpp b/src/mesh/SubItemValuePerItemUtils.hpp index 575c39c5238877bb064883c54b7d08792547000b..cc0c941f7f23ee5592e861d2a82a23ca07b3d0e1 100644 --- a/src/mesh/SubItemValuePerItemUtils.hpp +++ b/src/mesh/SubItemValuePerItemUtils.hpp @@ -166,7 +166,7 @@ max(const SubItemValuePerItem<DataType, ItemOfItem, ConnectivityPtr>& sub_item_v void init(data_type& value) const { - value = std::numeric_limits<data_type>::min(); + value = std::numeric_limits<data_type>::lowest(); } PUGS_INLINE diff --git a/src/output/VTKWriter.cpp b/src/output/VTKWriter.cpp index cd7834d713d2f9ca94d6dd65aa301ed444c6340f..a7e588527a3a6fc375661e8a2c33cbf332ae3b71 100644 --- a/src/output/VTKWriter.cpp +++ b/src/output/VTKWriter.cpp @@ -222,7 +222,7 @@ struct VTKWriter::VTKType if constexpr (std::is_unsigned_v<DataType>) { return "UInt" + stringify(sizeof(DataType) * 8); } else { - return "UInt" + stringify(sizeof(DataType) * 8); + return "Int" + stringify(sizeof(DataType) * 8); } } else if constexpr (std::is_floating_point_v<DataType>) { return "Float" + stringify(sizeof(DataType) * 8); @@ -431,6 +431,9 @@ VTKWriter::_write(const MeshType& mesh, std::visit([&, name = name](auto&& item_value) { return this->_write_cell_pvtu(fout, name, item_value); }, item_value_variant); } + if (parallel::size() > 1) { + fout << "<PDataArray type=\"UInt8\" Name=\"vtkGhostType\" NumberOfComponents=\"1\"/>\n"; + } fout << "</PCellData>\n"; for (size_t i_rank = 0; i_rank < parallel::size(); ++i_rank) { @@ -470,6 +473,15 @@ VTKWriter::_write(const MeshType& mesh, auto&& item_value) { return this->_write_cell_data(fout, name, item_value, serialize_data_list); }, item_value_variant); } + if (parallel::size() > 1) { + CellValue<uint8_t> vtk_ghost_type{mesh.connectivity()}; + auto cell_is_owned = mesh.connectivity().cellIsOwned(); + parallel_for( + mesh.numberOfCells(), + PUGS_LAMBDA(const CellId cell_id) { vtk_ghost_type[cell_id] = cell_is_owned[cell_id] ? 0 : 1; }); + + _write_array(fout, "vtkGhostType", vtk_ghost_type.arrayView(), serialize_data_list); + } fout << "</CellData>\n"; fout << "<PointData>\n"; for (const auto& [name, item_value_variant] : output_named_item_data_set) { @@ -659,7 +671,7 @@ VTKWriter::_write(const MeshType& mesh, fout << "<?xml version=\"1.0\"?>\n"; fout << _getDateAndVersionComment(); - fout << "<VTKFile type=\"Collection\" version=\"0.1\">\n"; + fout << "<VTKFile type=\"Collection\" version=\"2.0\">\n"; fout << "<Collection>\n"; if (time.has_value()) { diff --git a/src/output/WriterBase.hpp b/src/output/WriterBase.hpp index 1964068a0222bfb35304d30afab2861f3cb97383..c07c20c711e827ae566635ca82c9456bbda853ac 100644 --- a/src/output/WriterBase.hpp +++ b/src/output/WriterBase.hpp @@ -61,7 +61,7 @@ class WriterBase : public IWriter if (m_saved_times.size() > 0) { return m_saved_times[m_saved_times.size() - 1]; } else { - return -std::numeric_limits<double>::max(); + return std::numeric_limits<double>::lowest(); } } @@ -77,7 +77,8 @@ class WriterBase : public IWriter PeriodManager(const PeriodManager&) = default; PeriodManager(PeriodManager&&) = default; - PeriodManager(double time_period) : m_time_period{time_period}, m_next_time{-std::numeric_limits<double>::max()} {} + PeriodManager(double time_period) : m_time_period{time_period}, m_next_time{std::numeric_limits<double>::lowest()} + {} }; protected: diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp index 2f0146e2b9d08c231eaadee7e7f50eeea439ddaf..a08453ba766450909f3bcdf0f0fd678bdb06456c 100644 --- a/src/utils/Array.hpp +++ b/src/utils/Array.hpp @@ -355,7 +355,7 @@ max(const Array<DataType>& array) void init(data_type& value) const { - value = std::numeric_limits<data_type>::min(); + value = std::numeric_limits<data_type>::lowest(); } PUGS_INLINE diff --git a/src/utils/SmallArray.hpp b/src/utils/SmallArray.hpp index 9e0782f1c0adb0b9bfa4bcf25bcaa8977a3800d4..81d70a848187216e82cf80c5208f96cb64e05dfa 100644 --- a/src/utils/SmallArray.hpp +++ b/src/utils/SmallArray.hpp @@ -25,12 +25,14 @@ class [[nodiscard]] SmallArray friend SmallArray<std::add_const_t<DataType>>; public: - PUGS_INLINE size_t size() const noexcept + PUGS_INLINE size_t + size() const noexcept { return m_size; } - friend PUGS_INLINE SmallArray<std::remove_const_t<DataType>> copy(const SmallArray<DataType>& source) + friend PUGS_INLINE SmallArray<std::remove_const_t<DataType>> + copy(const SmallArray<DataType>& source) { SmallArray<std::remove_const_t<DataType>> image(source.m_size); std::copy(source.m_values.get(), source.m_values.get() + source.m_size, image.m_values.get()); @@ -38,28 +40,31 @@ class [[nodiscard]] SmallArray return image; } - friend PUGS_INLINE void copy_to(const SmallArray<DataType>& source, - const SmallArray<std::remove_const_t<DataType>>& destination) + friend PUGS_INLINE void + copy_to(const SmallArray<DataType>& source, const SmallArray<std::remove_const_t<DataType>>& destination) { Assert(source.size() == destination.size()); std::copy(source.m_values.get(), source.m_values.get() + source.m_size, destination.m_values.get()); } - PUGS_INLINE DataType& operator[](index_type i) const noexcept(NO_ASSERT) + PUGS_INLINE DataType& + operator[](index_type i) const noexcept(NO_ASSERT) { Assert(i < m_size); return m_values[i]; } PUGS_INLINE - void fill(const DataType& data) const + void + fill(const DataType& data) const { static_assert(not std::is_const_v<DataType>, "Cannot modify SmallArray of const"); std::fill(m_values.get(), m_values.get() + m_size, data); } template <typename DataType2> - PUGS_INLINE SmallArray& operator=(const SmallArray<DataType2>& array) noexcept + PUGS_INLINE SmallArray& + operator=(const SmallArray<DataType2>& array) noexcept { // ensures that DataType is the same as source DataType2 static_assert(std::is_same<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>(), @@ -99,7 +104,8 @@ class [[nodiscard]] SmallArray #endif // NDEBUG } - friend std::ostream& operator<<(std::ostream& os, const SmallArray& x) + friend std::ostream& + operator<<(std::ostream& os, const SmallArray& x) { if (x.size() > 0) { os << 0 << ':' << NaNHelper(x[0]); @@ -117,13 +123,14 @@ class [[nodiscard]] SmallArray SmallArray(const SmallArray&) = default; template <typename DataType2> - PUGS_INLINE SmallArray(const SmallArray<DataType2>& array) noexcept + PUGS_INLINE + SmallArray(const SmallArray<DataType2>& array) noexcept { this->operator=(array); } PUGS_INLINE - SmallArray(SmallArray &&) = default; + SmallArray(SmallArray&&) = default; PUGS_INLINE ~SmallArray() = default; @@ -172,7 +179,7 @@ max(const SmallArray<DataType>& array) using data_type = std::remove_const_t<DataType>; using index_type = typename SmallArray<DataType>::index_type; - data_type max_value = -std::numeric_limits<data_type>::max(); + data_type max_value = std::numeric_limits<data_type>::lowest(); for (index_type i = 0; i < array.size(); ++i) { if (array[i] > max_value) { max_value = array[i]; diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp index 4a447f70a9e99151027266602763b712d00525cf..28c018cced7ac2c4a2295b1824882a7dc5b492f8 100644 --- a/tests/test_Array.cpp +++ b/tests/test_Array.cpp @@ -324,6 +324,103 @@ TEST_CASE("Array", "[utils]") } } + SECTION("checking for floating Array min/max") + { + SECTION("Min") + { + Array<double> b(10); + b[0] = 13; + b[1] = 1; + b[2] = 8; + b[3] = -3; + b[4] = 23; + b[5] = -1; + b[6] = 13; + b[7] = 0; + b[8] = 12; + b[9] = 9; + + REQUIRE(min(b) == -3); + + b.fill(0); + REQUIRE(min(b) == 0); + + b[0] = -13; + b[1] = -1; + b[2] = -8; + b[3] = -3; + b[4] = -23; + b[5] = -1; + b[6] = -13; + b[7] = 0; + b[8] = -12; + b[9] = -9; + + REQUIRE(min(b) == -23); + + b[0] = 13; + b[1] = 1; + b[2] = 8; + b[3] = 3; + b[4] = 23; + b[5] = 1; + b[6] = 13; + b[7] = 0; + b[8] = 12; + b[9] = 9; + + REQUIRE(min(b) == 0); + + b[7] = 3; + REQUIRE(min(b) == 1); + } + + SECTION("Max") + { + Array<double> b(10); + b[0] = 13; + b[1] = 1; + b[2] = 8; + b[3] = -3; + b[4] = 23; + b[5] = -1; + b[6] = 13; + b[7] = 0; + b[8] = 12; + b[9] = 9; + + REQUIRE(max(b) == 23); + + b[0] = -13; + b[1] = -12; + b[2] = -8; + b[3] = -3; + b[4] = -23; + b[5] = -1; + b[6] = -13; + b[7] = -10; + b[8] = -12; + b[9] = -9; + + REQUIRE(max(b) == -1); + + b.fill(-13); + REQUIRE(max(b) == -13); + + b[0] = 13; + b[1] = 12; + b[2] = 8; + b[3] = 3; + b[4] = 23; + b[5] = 1; + b[6] = 13; + b[7] = 10; + b[8] = 12; + b[9] = 9; + REQUIRE(max(b) == 23); + } + } + SECTION("reproducible floating point sum") { auto direct_sum = [](auto array) { diff --git a/tests/test_ItemArrayUtils.cpp b/tests/test_ItemArrayUtils.cpp index 8d04842849ed469e1cf0c1e6e217d8e1da4f01f8..0237b90da16549c30fd8b581643caec3dc287ddd 100644 --- a/tests/test_ItemArrayUtils.cpp +++ b/tests/test_ItemArrayUtils.cpp @@ -1028,6 +1028,66 @@ TEST_CASE("ItemArrayUtils", "[mesh]") } } } + + SECTION("max for negative double values") + { + std::array mesh_list = MeshDataBaseForTests::get().all3DMeshes(); + + for (const auto& named_mesh : mesh_list) { + SECTION(named_mesh.name()) + { + auto mesh_3d_v = named_mesh.mesh(); + auto mesh_3d = mesh_3d_v->get<Mesh<3>>(); + + const Connectivity<3>& connectivity = mesh_3d->connectivity(); + + CellArray<double> cell_array{connectivity, 2}; + cell_array.fill(std::numeric_limits<double>::max()); + + auto cell_is_owned = connectivity.cellIsOwned(); + parallel_for( + mesh_3d->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { + if (cell_is_owned[cell_id]) { + for (size_t i = 0; i < cell_array.sizeOfArrays(); ++i) { + cell_array[cell_id][i] = -parallel::rank() - 10 + i; + } + } + }); + + REQUIRE(max(cell_array) == -parallel::size() - 10); + } + } + } + + SECTION("max for negative integral values") + { + std::array mesh_list = MeshDataBaseForTests::get().all3DMeshes(); + + for (const auto& named_mesh : mesh_list) { + SECTION(named_mesh.name()) + { + auto mesh_3d_v = named_mesh.mesh(); + auto mesh_3d = mesh_3d_v->get<Mesh<3>>(); + + const Connectivity<3>& connectivity = mesh_3d->connectivity(); + + CellArray<int> cell_array{connectivity, 2}; + cell_array.fill(std::numeric_limits<int>::max()); + + auto cell_is_owned = connectivity.cellIsOwned(); + parallel_for( + mesh_3d->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { + if (cell_is_owned[cell_id]) { + for (size_t i = 0; i < cell_array.sizeOfArrays(); ++i) { + cell_array[cell_id][i] = -2 * parallel::size() + parallel::rank() - 10 + i; + } + } + }); + + REQUIRE(max(cell_array) == -static_cast<int>(parallel::size() + 10)); + } + } + } } SECTION("sum") diff --git a/tests/test_ItemValueUtils.cpp b/tests/test_ItemValueUtils.cpp index f80819f2c619709584c6574dd185d5a0e2f512fb..149dcbfa6c7c6d7976b98f21b559a03016daf857 100644 --- a/tests/test_ItemValueUtils.cpp +++ b/tests/test_ItemValueUtils.cpp @@ -240,6 +240,34 @@ TEST_CASE("ItemValueUtils", "[mesh]") } } } + + SECTION("max of all negative values") + { + std::array mesh_list = MeshDataBaseForTests::get().all3DMeshes(); + + for (const auto& named_mesh : mesh_list) { + SECTION(named_mesh.name()) + { + auto mesh_3d_v = named_mesh.mesh(); + auto mesh_3d = mesh_3d_v->get<Mesh<3>>(); + + const Connectivity<3>& connectivity = mesh_3d->connectivity(); + + CellValue<float> cell_value{connectivity}; + cell_value.fill(std::numeric_limits<float>::max()); + + auto cell_is_owned = connectivity.cellIsOwned(); + parallel_for( + mesh_3d->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { + if (cell_is_owned[cell_id]) { + cell_value[cell_id] = -2 * parallel::size() + parallel::rank() + 1; + } + }); + + REQUIRE(max(cell_value) == -parallel::size()); + } + } + } } SECTION("sum") diff --git a/tests/test_SubItemArrayPerItemUtils.cpp b/tests/test_SubItemArrayPerItemUtils.cpp index 1ff8bb17f7bf170c748cfa15aa3fc82f25f4a879..ce3bb6265fd3e48a13d0505e39ddfa312bf33e2f 100644 --- a/tests/test_SubItemArrayPerItemUtils.cpp +++ b/tests/test_SubItemArrayPerItemUtils.cpp @@ -283,6 +283,39 @@ TEST_CASE("SubItemArrayPerItemUtils", "[mesh]") } } } + + SECTION("max of only negative values") + { + std::array mesh_list = MeshDataBaseForTests::get().all3DMeshes(); + + for (const auto& named_mesh : mesh_list) { + SECTION(named_mesh.name()) + { + auto mesh_3d = named_mesh.mesh()->get<Mesh<3>>(); + + const Connectivity<3>& connectivity = mesh_3d->connectivity(); + + NodeArrayPerEdge<double> node_array_per_edge{connectivity, 3}; + node_array_per_edge.fill(std::numeric_limits<double>::max()); + + auto edge_is_owned = connectivity.edgeIsOwned(); + + parallel_for( + connectivity.numberOfEdges(), PUGS_LAMBDA(EdgeId edge_id) { + if (edge_is_owned[edge_id]) { + auto edge_table = node_array_per_edge.itemTable(edge_id); + for (size_t i = 0; i < edge_table.numberOfRows(); ++i) { + for (size_t j = 0; j < edge_table.numberOfColumns(); ++j) { + edge_table(i, j) = -10. - parallel::size() + parallel::rank() - i - j; + } + } + } + }); + + REQUIRE(max(node_array_per_edge) == -11.); + } + } + } } SECTION("sum") diff --git a/tests/test_SubItemValuePerItemUtils.cpp b/tests/test_SubItemValuePerItemUtils.cpp index 69214c1c933ba6abb0c93e094317901263e70eed..fd84ad62e9dcfb1f5dd379c8f469a5b5d7e04d0d 100644 --- a/tests/test_SubItemValuePerItemUtils.cpp +++ b/tests/test_SubItemValuePerItemUtils.cpp @@ -267,6 +267,36 @@ TEST_CASE("SubItemValuePerItemUtils", "[mesh]") } } } + + SECTION("max for all negative values") + { + std::array mesh_list = MeshDataBaseForTests::get().all2DMeshes(); + + for (const auto& named_mesh : mesh_list) { + SECTION(named_mesh.name()) + { + auto mesh_2d = named_mesh.mesh()->get<Mesh<2>>(); + + const Connectivity<2>& connectivity = mesh_2d->connectivity(); + + EdgeValuePerCell<double> edge_value_per_cell{connectivity}; + edge_value_per_cell.fill(std::numeric_limits<double>::max()); + + auto cell_is_owned = connectivity.cellIsOwned(); + parallel_for( + connectivity.numberOfCells(), PUGS_LAMBDA(CellId cell_id) { + if (cell_is_owned[cell_id]) { + auto cell_array = edge_value_per_cell.itemArray(cell_id); + for (size_t i = 0; i < cell_array.size(); ++i) { + cell_array[i] = -1. * parallel::size() - 10 + parallel::rank() - i; + } + } + }); + + REQUIRE(max(edge_value_per_cell) == -11); + } + } + } } SECTION("sum")