16#include <kernel/geometry/io_common.hpp>
17#include <kernel/geometry/mesh_atlas.hpp>
18#include <kernel/geometry/mesh_node.hpp>
19#include <kernel/shape.hpp>
21#include <kernel/util/string.hpp>
22#include <kernel/util/tiny_algebra.hpp>
65 end(bytes.data() + bytes.size()),
96 const char* eol =
static_cast<const char*
>(std::memchr(
current,
'\n', std::size_t(
end -
current)));
105 const auto line_len = std::size_t(eol -
current);
107 const std::string_view line{
current, line_len};
126 template<
typename... Args>
129 const std::string_view line =
next_line();
130 std::istringstream stream((std::string(line)));
132 ((stream >> args), ...);
142 std::memcpy(t,
current,
sizeof(T));
164 bool _have_root_markup =
false;
213 XASSERTM(std::filesystem::exists(path),
"GmshFileReader: File " +
stringify(path) +
" does not exist");
214 std::ifstream file(path, std::ios::binary);
215 XASSERTM(file.good(),
"GmshFileReader: Bad stream");
307 XASSERT(view.next_line() ==
"$MeshFormat");
320 XASSERTM(one == 1,
".msh files with non-native endianess are not supported!");
323 XASSERT(view.next_line() ==
"$EndMeshFormat");
327 std::string_view line = view.next_line();
333 else if(line ==
"$Elements")
343 _have_root_markup =
true;
359 template<
typename MeshType_>
362 using MeshShapeType =
typename MeshType_::ShapeType;
363 using DataType =
typename MeshType_::CoordType;
364 using VertexType =
typename MeshType_::VertexType;
367 static constexpr int shape_dim = MeshType_::shape_dim;
369 if(!_have_root_markup)
379 XABORTM(
"CoordType of MeshType_ does not match data type of .msh file");
405 std::array<Index, shape_dim + 1> size;
410 auto mesh = std::make_unique<MeshType_>(size.data());
413 auto& vertex_set = mesh->get_vertex_set();
420 for(std::size_t node(0); node <
_num_nodes; node++)
424 node_view.
memcpy(&vertex_index);
434 node_view.
memcpy(&vertex_set[
Index(vertex_index - 1)][j]);
455 vertex_set[vertex_index - 1] = VertexType{x, y, z};
460 auto& element_index_set = mesh->template get_index_set<shape_dim, 0>();
470 for(std::size_t e(0); e < std::size_t(header.num_elements); e++)
473 int element_index(0);
474 chunk_view.
memcpy(&element_index);
481 chunk_view.
advance(std::size_t(header.tags_per_element) *
sizeof(
int));
483 for(
int n(0); n < (int)num_verts; n++)
486 chunk_view.
memcpy(&node_index);
500 std::string_view line = view.next_line();
501 std::istringstream stream((std::string(line)));
503 int element_index(0);
507 stream >> element_index >> type >> num_tags;
514 for(
int tag(0); tag < num_tags; tag++)
520 for(
int n(0); n < (int)num_verts; n++)
523 stream >> node_number;
534 mesh->deduct_topology_from_top();
535 return std::make_unique<RootMeshNode<MeshType_>>(std::move(mesh), &mesh_atlas);
556 default:
XABORTM(
"Unsupported .msh element type!");
569 default:
XABORTM(
"Unsupported .msh element type!");
582 default:
XABORTM(
"Unsupported .msh element type!");
597 static constexpr std::array<int, 4> quadrangle_ordering{0, 1, 3, 2};
600 static constexpr std::array<int, 8> hexahedron_ordering{0, 1, 3, 2, 4, 5, 7, 6};
606 case 3:
return quadrangle_ordering.at(std::size_t(node));
608 case 5:
return hexahedron_ordering.at(std::size_t(node));
609 default:
XABORTM(
"Unsupported .msh element type!");
634 XASSERT(view.next_line() ==
"$EndNodes");
650 std::size_t elements_handled(0);
656 view.memcpy(&header.type);
657 view.memcpy(&header.num_elements);
658 view.memcpy(&header.tags_per_element);
661 header.elements_start = view.position();
675 elements_handled += std::size_t(header.num_elements);
686 view.parse_line(number, type);
699 XASSERT(view.next_line() ==
"$EndElements");
708 std::stringstream buffer;
709 buffer << stream.rdbuf();
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
ParsedMeshType get_mesh_type() const
Returns the mesh-type of the parsed mesh.
std::vector< BinaryElementsHeader > _element_headers
Element chunks, for binary files.
int _shape_dim
Shape dimension.
int get_shape_dim() const
Returns the shape-dimension of the parsed mesh.
const char * _nodes_start
Start of nodes in the file.
void read_data(const std::istream &stream)
Read file into string.
double _version
.msh file version
GmshFileReader(const std::istream &stream)
Input stream constructor.
String _file_contents
Contents of the .msh file.
void scan_elements(FileView &view)
Gather start position(s) of element data and ensure the mesh contains only a single element type.
static int nodes_per_element(int type)
.msh element type to number of nodes per element
String _mesh_type_string
Mesh type string.
static ParsedShapeType element_type_to_shape_type(int type)
.msh element type to ShapeType mapping
int _element_type
.msh element type
static int msh_to_feat_order(int type, int node)
Re-ordering for cell vertices.
static int element_type_to_shape_dim(int type)
.msh element type to shape dimension mapping
ParsedShapeType _shape_type
Shape type.
static std::size_t binary_element_chunk_size(const BinaryElementsHeader &header)
Size (in bytes) of one chunk of elements in a binary .msh file.
std::size_t _num_nodes
Number of nodes in the file.
GmshFileReader(const std::filesystem::path &path)
File path constructor.
void scan_nodes(FileView &view)
Gather number of nodes and start position of node data.
ParsedShapeType get_shape_type() const
Returns the shape-type of the parsed mesh.
int get_world_dim() const
Returns the world-dimension of the parsed mesh.
std::size_t _num_elements
Number of elements in the file.
bool _is_binary
.msh ascii/binary indicator
const char * _elements_start
Start of elements in the file, for ascii files.
const String & get_meshtype_string() const
Returns the mesh-type string of the parsed mesh.
void read_root_markup()
Reads the root markup from the input stream and analyses it.
std::unique_ptr< RootMeshNode< MeshType_ > > parse(MeshAtlas< MeshType_ > &mesh_atlas, PartitionSet *part_set=nullptr)
Parses the mesh file into a mesh node and a mesh atlas.
std::size_t _data_size
.msh size for reals
Mesh Atlas class template.
String class implementation.
ParsedMeshType
mesh type enumeration
@ conformal
conformal mesh type
ParsedShapeType
shape type enumeration
@ simplex
simplex shape type
@ hypercube
hypercube shape type
@ unknown
unknown shape type
String stringify(const T_ &item)
Converts an item into a String.
std::uint64_t Index
Index data type.
Utility class for walking through potentially mixed ascii and binary .msh files.
const char * end
End of buffer (for bounds checks)
void advance(std::size_t len)
Advance the view len chars.
FileView(const String &bytes, const char *pos)
Constructor.
std::string_view next_line()
void memcpy(T *t)
Copy a value from the buffer and then advance that many bytes.
const char * current
Current position in buffer.
const char * position() const
Current position in buffer.
FileView(const String &bytes)
Constructor.
void parse_line(Args &... args)
Parses a line (split by whitespace) into the given parameters.
Face traits tag struct template.
Hypercube shape tag struct template.
Simplex shape tag struct template.