--- /dev/null
+New: GridOut::write_vtu() and GridIn::read_vtu() allow to save and restore a locally refined triangulation,
+using a xml section in the vtu file that is ignored by vtu readers.
+<br>
+(Luca Heltai, Nicola Giuliani, 2020/01/17)
tecplot,
/// Use read_vtk()
vtk,
+ /// Use read_vtu()
+ vtu,
/// Use read_assimp()
assimp,
};
void
read_vtk(std::istream &in);
+ /**
+ * Read grid data from a unstructured vtu file, saved by deal.II using
+ * GridOut::write_vtu(), with the flag
+ * GridOutFlags::Vtu::serialize_triangulation set to true.
+ *
+ * Notice that this function does not support reading in arbitrary vtu files,
+ * but only files that were written by deal.II itself, using the function
+ * GridOut::write_vtu and setting GridOutFlags::Vtu::serialize_triangulation
+ * to true.
+ *
+ * When this flag is set to true, the generated vtu file contains the
+ * triangulation in a xml section which is ignored by vtu general vtu readers.
+ * If this section is absent, an exception is thrown.
+ *
+ * @author Luca Heltai, Nicola Giuliani, 2020
+ */
+ void
+ read_vtu(std::istream &in);
+
+
/**
* Read grid data from an unv file as generated by the Salome mesh
* generator. Numerical data is ignored.
/**
* Flags for grid output in Vtu format. These flags are the same as those
- * declared in DataOutBase::VtkFlags.
+ * declared in DataOutBase::VtkFlags, with the addition of a flag that
+ * determines if you want to add a entry in the vtu file (which is really
+ * a xml file) containing the entire serialization of the triangulation.
*
* @ingroup output
*/
struct Vtu : public DataOutBase::VtkFlags
- {};
+ {
+ Vtu(const bool serialize_triangulation = false)
+ : serialize_triangulation(serialize_triangulation)
+ {}
+
+ /**
+ * Add to the vtu file also the serialized triangulation.
+ */
+ bool serialize_triangulation;
+ };
} // namespace GridOutFlags
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria.h>
+#include <boost/archive/binary_iarchive.hpp>
#include <boost/io/ios_state.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/xml_parser.hpp>
+#include <boost/serialization/serialization.hpp>
#include <algorithm>
#include <cctype>
}
+
+template <int dim, int spacedim>
+void
+GridIn<dim, spacedim>::read_vtu(std::istream &in)
+{
+ namespace pt = boost::property_tree;
+ pt::ptree tree;
+ pt::read_xml(in, tree);
+ auto section = tree.get_optional<std::string>("VTKFile.dealiiData");
+
+ AssertThrow(section,
+ ExcMessage(
+ "While reading a VTU file, failed to find dealiiData section. "
+ "Notice that we can only read grid files in .vtu format that "
+ "were created by the deal.II library, using a call to "
+ "GridOut::write_vtu(), where the flag "
+ "GridOutFlags::Vtu::serialize_triangulation is set to true."));
+
+ const auto decoded =
+ Utilities::decode_base64({section->begin(), section->end() - 1});
+ const auto string_archive =
+ Utilities::decompress({decoded.begin(), decoded.end()});
+ std::istringstream in_stream(string_archive);
+ boost::archive::binary_iarchive ia(in_stream);
+ tria->load(ia, 0);
+}
+
+
template <int dim, int spacedim>
void
GridIn<dim, spacedim>::read_unv(std::istream &in)
read_vtk(in);
return;
+ case vtu:
+ read_vtu(in);
+ return;
+
case unv:
read_unv(in);
return;
return ".msh";
case vtk:
return ".vtk";
+ case vtu:
+ return ".vtu";
case unv:
return ".unv";
case ucd:
if (format_name == "vtk")
return vtk;
+ if (format_name == "vtu")
+ return vtu;
+
// This is also the typical extension of Abaqus input files.
if (format_name == "inp")
return ucd;
std::string
GridIn<dim, spacedim>::get_format_names()
{
- return "dbmesh|msh|unv|vtk|ucd|abaqus|xda|netcdf|tecplot|assimp";
+ return "dbmesh|msh|unv|vtk|vtu|ucd|abaqus|xda|netcdf|tecplot|assimp";
}
#include <deal.II/numerics/data_out.h>
+#include <boost/algorithm/string.hpp>
+#include <boost/archive/binary_oarchive.hpp>
+
#include <algorithm>
#include <cmath>
#include <cstring>
#include <list>
#include <set>
-
DEAL_II_NAMESPACE_OPEN
std::vector<DataOutBase::Patch<dim, spacedim>> patches;
patches.reserve(tria.n_active_cells());
generate_triangulation_patches(patches, tria.begin_active(), tria.end());
- DataOutBase::write_vtu(
+
+ DataOutBase::write_vtu_header(out, vtu_flags);
+ DataOutBase::write_vtu_main(
patches,
triangulation_patch_data_names(),
std::vector<
DataComponentInterpretation::DataComponentInterpretation>>(),
vtu_flags,
out);
+ if (vtu_flags.serialize_triangulation)
+ {
+ out << " </UnstructuredGrid>\n";
+ out << "<dealiiData encoding=\"base64\">";
+ std::stringstream outstring;
+ boost::archive::binary_oarchive ia(outstring);
+ tria.save(ia, 0);
+ const auto compressed = Utilities::compress(outstring.str());
+ out << Utilities::encode_base64({compressed.begin(), compressed.end()});
+ out << "\n</dealiiData>\n";
+ out << "</VTKFile>\n";
+ }
+ else
+ DataOutBase::write_vtu_footer(out);
+ out << std::flush;
AssertThrow(out, ExcIO());
}
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2020 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.
+//
+// ---------------------------------------------------------------------
+
+
+// read and write a 2d file in the VTU format
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/grid_in.h>
+#include <deal.II/grid/grid_out.h>
+#include <deal.II/grid/tria.h>
+
+#include <boost/archive/text_oarchive.hpp>
+
+#include <string>
+
+#include "../tests.h"
+
+int
+main()
+{
+ initlog();
+
+ Triangulation<2> tria;
+ Triangulation<2> tria2;
+ GridGenerator::hyper_ball(tria);
+
+ for (const auto cell : tria.active_cell_iterators())
+ {
+ if (cell->center()[0] < 0)
+ cell->set_refine_flag();
+ }
+ tria.execute_coarsening_and_refinement();
+
+ for (const auto cell : tria.active_cell_iterators())
+ {
+ if (cell->center()[1] < 0)
+ cell->set_refine_flag();
+ }
+ tria.execute_coarsening_and_refinement();
+
+ {
+ std::ofstream out("grid.vtu");
+ GridOut go;
+ GridOutFlags::Vtu vtu_flags(true);
+ go.set_flags(vtu_flags);
+ go.write_vtu(tria, out);
+ }
+ {
+ std::ifstream in("grid.vtu");
+ GridIn<2> gi;
+ gi.attach_triangulation(tria2);
+ gi.read_vtu(in);
+ }
+ std::stringstream stream1;
+ std::stringstream stream2;
+ {
+ boost::archive::text_oarchive oa(stream1);
+ tria.save(oa, 0);
+ }
+ {
+ boost::archive::text_oarchive oa(stream2);
+ tria2.save(oa, 0);
+ }
+ if (stream1.str() == stream2.str())
+ deallog << "OK" << std::endl;
+ else
+ deallog << "Not OK" << std::endl;
+ std::remove("grid.vtu");
+}
\ No newline at end of file
--- /dev/null
+
+DEAL::OK