Wildmeshing Toolkit
output.cpp
Go to the documentation of this file.
1 #include "output.hpp"
2 
3 #include <wmtk/Mesh.hpp>
4 #include <fmt/std.h>
5 #include <type_traits>
7 #include <wmtk/io/HDF5Writer.hpp>
9 #include <wmtk/utils/Logger.hpp>
10 #include "OutputOptions.hpp"
12 
13 
15 
16 void output(
17  const Mesh& mesh,
18  const std::filesystem::path& file,
19  const std::string& position_attr_name)
20 {
21  wmtk::logger().info("Saving on {}", file.string());
22 
23  if (file.extension().empty()) {
24  assert(!position_attr_name.empty());
25  std::array<bool, 4> out = {{false, false, false, false}};
26  for (int64_t d = 0; d <= mesh.top_cell_dimension(); ++d) {
27  out[d] = true;
28  }
29  ParaviewWriter writer(file, position_attr_name, mesh, out[0], out[1], out[2], out[3]);
30  mesh.serialize(writer);
31  } else if (file.extension() == ".hdf5") {
32  output_hdf5(mesh, file);
33  } else
34  throw std::runtime_error(std::string("Unknown file type: ") + file.string());
35 }
36 
37 void output(
38  const Mesh& mesh,
39  const std::filesystem::path& file,
40  const attribute::MeshAttributeHandle& position_attr)
41 {
42  const std::string attr_name = mesh.get_attribute_name(position_attr.as<double>());
43  output(mesh, file, attr_name);
44 }
45 
46 void output_hdf5(const Mesh& mesh, const std::filesystem::path& file)
47 {
48  HDF5Writer writer(file);
49  mesh.serialize(writer);
50 }
51 
52 
53 
54 void output(
55  const Mesh& mesh,
56  const OutputOptions& opts)
57 {
58 
59  if (opts.type == ".vtu") {
60  assert(
61  opts.position_attribute.index() != std::variant_npos);
62  std::string name = std::visit([](const auto& v) -> std::string{
63  using T = std::decay_t<decltype(v)>;
64  if constexpr(std::is_same_v<T,std::string>) {
65  return v;
66  } else if constexpr(std::is_same_v<T,wmtk::attribute::MeshAttributeHandle>) {
67  return v.name();
68  }
69  }, opts.position_attribute);
70  std::array<bool, 4> out = {{false, false, false, false}};
71  for (int64_t d = 0; d <= mesh.top_cell_dimension(); ++d) {
72  out[d] = true;
73  }
74  ParaviewWriter writer(opts.file,name, mesh, out[0], out[1], out[2], out[3]);
75  mesh.serialize(writer);
76  } else if (opts.type == ".hdf5") {
77  output_hdf5(mesh, opts.file);
78  } else
79  throw std::runtime_error(
80  fmt::format("Unable to write file [{}] of extension [{}]",
81  opts.file, opts.type));
82 }
83 
84 void output(
85  const multimesh::NamedMultiMesh& mesh,
86  const OutputOptions& opts)
87 {
88  output(mesh.root(), opts);
89 
90  if(opts.mesh_name_path.has_value()) {
91  const auto& path = opts.mesh_name_path.value();
92  std::ofstream ofs(path);
93  ofs<< *mesh.get_names_json();
94  }
95 
96 }
97 
98 } // namespace wmtk::components
void serialize(MeshWriter &writer, const Mesh *local_root=nullptr) const
Definition: Mesh.cpp:92
int64_t top_cell_dimension() const
Definition: Mesh.hpp:992
std::string get_attribute_name(const TypedAttributeHandle< T > &handle) const
Definition: Mesh.hpp:949
auto as() const -> const held_handle_type< held_type_from_primitive< T >()> &
std::unique_ptr< nlohmann::json > get_names_json() const
void output_hdf5(const Mesh &mesh, const std::filesystem::path &file)
Write the mesh to file.
Definition: output.cpp:46
void output(const Mesh &mesh, const std::filesystem::path &file, const std::string &position_attr_name)
Write the mesh to file.
Definition: output.cpp:16
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58
std::optional< std::filesystem::path > mesh_name_path
std::variant< wmtk::attribute::MeshAttributeHandle, std::string > position_attribute