From: Pasquale Africa Date: Tue, 4 Jul 2023 19:41:32 +0000 (+0000) Subject: Add tests for serializing serial triangulation X-Git-Tag: relicensing~634^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea9f882618d1207fc8c8fd56270d25e659c972c0;p=dealii.git Add tests for serializing serial triangulation --- diff --git a/tests/grid/save_load_01.cc b/tests/grid/save_load_01.cc new file mode 100644 index 0000000000..26f83aff01 --- /dev/null +++ b/tests/grid/save_load_01.cc @@ -0,0 +1,85 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2008 - 2023 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// Test Triangulation::load()/save(). +// Create a triangulation, save it and load it. +// The initial and the loaded triangulations must be the same. + +#include +#include + +#include + +#include +#include +#include + +#include + +#include "./tests.h" + +using namespace dealii; + +template +void +test(TriangulationType &triangulation) +{ + const std::string filename = "save_load_" + std::to_string(dim) + "d_out"; + + triangulation.save(filename); + deallog.push("save"); + print_statistics(triangulation, false); + deallog.pop(); + + triangulation.clear(); + + triangulation.load(filename); + deallog.push("load"); + print_statistics(triangulation, false); + deallog.pop(); +} + + +int +main(int argc, char **argv) +{ + initlog(); + + deallog.push("2d"); + { + constexpr int dim = 2; + + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(3); + + test(triangulation); + } + deallog.pop(); + + deallog.push("3d"); + { + constexpr int dim = 3; + + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(3); + + test(triangulation); + } + deallog.pop(); +} diff --git a/tests/grid/save_load_01.output b/tests/grid/save_load_01.output new file mode 100644 index 0000000000..b3c4579611 --- /dev/null +++ b/tests/grid/save_load_01.output @@ -0,0 +1,21 @@ + +DEAL:2d:save::n_levels: 4 +DEAL:2d:save::n_cells: 85 +DEAL:2d:save::n_active_cells: 64 +DEAL:2d:save::has_hanging_nodes: false +DEAL:2d:save:: +DEAL:2d:load::n_levels: 4 +DEAL:2d:load::n_cells: 85 +DEAL:2d:load::n_active_cells: 64 +DEAL:2d:load::has_hanging_nodes: false +DEAL:2d:load:: +DEAL:3d:save::n_levels: 4 +DEAL:3d:save::n_cells: 585 +DEAL:3d:save::n_active_cells: 512 +DEAL:3d:save::has_hanging_nodes: false +DEAL:3d:save:: +DEAL:3d:load::n_levels: 4 +DEAL:3d:load::n_cells: 585 +DEAL:3d:load::n_active_cells: 512 +DEAL:3d:load::has_hanging_nodes: false +DEAL:3d:load:: diff --git a/tests/grid/serialization_01.cc b/tests/grid/serialization_01.cc new file mode 100644 index 0000000000..e1700eff1e --- /dev/null +++ b/tests/grid/serialization_01.cc @@ -0,0 +1,122 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2008 - 2023 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// Test serialization with triangulations. + +#include + +#include + +#include +#include + +#include + +#include + +#include +#include + +#include "./tests.h" + +using namespace dealii; + +template +class InterpolationFunction : public Function +{ +public: + InterpolationFunction() + : Function(1) + {} + + virtual double + value(const Point &p, const unsigned int component = 0) const + { + return p.norm(); + } +}; + +template +void +test(TriangulationType &triangulation) +{ + DoFHandler dof_handler(triangulation); + dof_handler.distribute_dofs(FE_Q(2)); + + using VectorType = Vector; + + VectorType vector(dof_handler.n_dofs()); + + VectorTools::interpolate(dof_handler, InterpolationFunction(), vector); + + VectorType vector_loaded(dof_handler.n_dofs()); + + print_statistics(triangulation, false); + + std::stringstream stream; + { + boost::archive::text_oarchive oa(stream); + oa << triangulation; + oa << vector; + } + + triangulation.clear(); + + { + boost::archive::text_iarchive ia(stream); + ia >> triangulation; + ia >> vector_loaded; + } + print_statistics(triangulation, false); + + // Verify that error is 0. + VectorType error(vector); + error.add(-1, vector_loaded); + + deallog << (error.linfty_norm() < 1e-16 ? "PASSED" : "FAILED") << std::endl; +} + + +int +main(int argc, char **argv) +{ + initlog(); + + deallog.push("2d"); + { + constexpr int dim = 2; + + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(3); + + test(triangulation); + } + deallog.pop(); + + deallog.push("3d"); + { + constexpr int dim = 3; + + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(3); + + test(triangulation); + } + deallog.pop(); +} diff --git a/tests/grid/serialization_01.output b/tests/grid/serialization_01.output new file mode 100644 index 0000000000..117a575fb0 --- /dev/null +++ b/tests/grid/serialization_01.output @@ -0,0 +1,23 @@ + +DEAL:2d::n_levels: 4 +DEAL:2d::n_cells: 85 +DEAL:2d::n_active_cells: 64 +DEAL:2d::has_hanging_nodes: false +DEAL:2d:: +DEAL:2d::n_levels: 4 +DEAL:2d::n_cells: 85 +DEAL:2d::n_active_cells: 64 +DEAL:2d::has_hanging_nodes: false +DEAL:2d:: +DEAL:2d::PASSED +DEAL:3d::n_levels: 4 +DEAL:3d::n_cells: 585 +DEAL:3d::n_active_cells: 512 +DEAL:3d::has_hanging_nodes: false +DEAL:3d:: +DEAL:3d::n_levels: 4 +DEAL:3d::n_cells: 585 +DEAL:3d::n_active_cells: 512 +DEAL:3d::has_hanging_nodes: false +DEAL:3d:: +DEAL:3d::PASSED