LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Namespaces | Classes | Enumerations | Functions
lf::io Namespace Reference

Mesh input (from file) and output (in various formats) facilities. More...

Namespaces

namespace  test_utils
 

Classes

struct  GMshFileV2
 A representation of a .msh file (V2) in a c++ data structure. More...
 
struct  GMshFileV4
 A representation of a .msh file (V4) in a c++ data structure. More...
 
class  GmshReader
 Reads a Gmsh *.msh file into a mesh::MeshFactory and provides a link between mesh::Entity objects and the gmsh's physical entities. More...
 
class  VtkFile
 Representation of a VTK file (only relevant) features (advanced usage) More...
 
class  VtkWriter
 Write a mesh along with mesh data into a vtk file. More...
 

Enumerations

enum class  TikzOutputCtrl : unsigned int {
  RenderCells = 1 , CellNumbering = 2 , VerticeNumbering = 4 , NodeNumbering = 8 ,
  EdgeNumbering = 16 , ArrowTips = 32 , WithPreamble = 64
}
 Enum flags: TikzOutputCtrl for output control of mesh drawn in TikZ. More...
 

Functions

std::ostream & operator<< (std::ostream &stream, GMshFileV2::ElementType et)
 Output the element type onto the console: More...
 
std::ostream & operator<< (std::ostream &stream, const GMshFileV2 &mf)
 For debugging purposes: Write the MshFile into a stream. More...
 
size_type NumNodes (GMshFileV2::ElementType et)
 Number of nodes that this element type has. More...
 
base::RefEl RefElOf (GMshFileV2::ElementType et)
 Reference element type of a GmshElementType. More...
 
int DimOf (GMshFileV2::ElementType et)
 Dimension of the GmshElement type. More...
 
GMshFileV2 readGmshFileV2 (std::string::const_iterator begin, std::string::const_iterator end, const std::string &version, bool is_binary, int size_t_size, int one, const std::string &filename)
 Read a *.msh file from disk and copy it's contents into the MshFile Datastructure. More...
 
std::ostream & operator<< (std::ostream &stream, GMshFileV4::ElementType et)
 Output the element type onto a stream: More...
 
int NumNodes (GMshFileV4::ElementType et)
 Number of nodes that this element type has. More...
 
base::RefEl RefElOf (GMshFileV4::ElementType et)
 Get the Reference Element type of a GmshElement. More...
 
int DimOf (GMshFileV4::ElementType et)
 Dimension of the GmshElement type. More...
 
GMshFileV4 ReadGmshFileV4 (std::string::const_iterator begin, std::string::const_iterator end, const std::string &version, bool is_binary, int size_t_size, int one, const std::string &filename)
 Read a GmshFile with format 4 and return it as an in-memory struct. More...
 
std::variant< GMshFileV2, GMshFileV4ReadGmshFile (const std::string &filename)
 
std::ostream & operator<< (std::ostream &stream, const VtkFile::Format &f)
 
void WriteToFile (const VtkFile &vtk_file, const std::string &filename)
 
void writeMatlab (const lf::mesh::Mesh &mesh, std::string filename)
 Writes affine triangulation data to file in MATLAB format. More...
 
void writeMatplotlib (const lf::mesh::Mesh &mesh, std::string filename)
 Write affine triangulation data to file in matplotlib format. More...
 
TikzOutputCtrl operator| (const TikzOutputCtrl &lhs, const TikzOutputCtrl &rhs)
 
TikzOutputCtrl operator& (const TikzOutputCtrl &lhs, const TikzOutputCtrl &rhs)
 
bool writeTikZ (const lf::mesh::Mesh &mesh, const std::string &filename, std::function< bool(const lf::mesh::Entity &)> &&selector, TikzOutputCtrl output_ctrl=TikzOutputCtrl::RenderCells)
 Writes mesh to file in TikZ Graphics format. File as input in LaTeX will draw the mesh. More...
 
bool writeTikZ (const lf::mesh::Mesh &mesh, const std::string &filename, TikzOutputCtrl output_ctrl=TikzOutputCtrl::RenderCells)
 TikZ output of all entities. More...
 

Detailed Description

Mesh input (from file) and output (in various formats) facilities.

Enumeration Type Documentation

◆ TikzOutputCtrl

enum class lf::io::TikzOutputCtrl : unsigned int
strong

Enum flags: TikzOutputCtrl for output control of mesh drawn in TikZ.

See also
writeTikZ()
Enumerator
RenderCells 
CellNumbering 
VerticeNumbering 
NodeNumbering 
EdgeNumbering 
ArrowTips 
WithPreamble 

Definition at line 24 of file write_tikz.h.

Function Documentation

◆ DimOf() [1/2]

int lf::io::DimOf ( GMshFileV2::ElementType  et)

◆ DimOf() [2/2]

int lf::io::DimOf ( GMshFileV4::ElementType  et)

◆ NumNodes() [1/2]

unsigned int lf::io::NumNodes ( GMshFileV2::ElementType  et)

◆ NumNodes() [2/2]

int lf::io::NumNodes ( GMshFileV4::ElementType  et)

◆ operator&()

TikzOutputCtrl lf::io::operator& ( const TikzOutputCtrl lhs,
const TikzOutputCtrl rhs 
)

Definition at line 18 of file write_tikz.cc.

◆ operator<<() [1/4]

std::ostream & lf::io::operator<< ( std::ostream &  stream,
const GMshFileV2 mf 
)

◆ operator<<() [2/4]

std::ostream & lf::io::operator<< ( std::ostream &  stream,
const VtkFile::Format f 
)

Definition at line 105 of file vtk_writer.cc.

References lf::io::VtkFile::ASCII, and lf::io::VtkFile::BINARY.

◆ operator<<() [3/4]

std::ostream & lf::io::operator<< ( std::ostream &  stream,
GMshFileV2::ElementType  et 
)

◆ operator<<() [4/4]

std::ostream & lf::io::operator<< ( std::ostream &  stream,
GMshFileV4::ElementType  et 
)

◆ operator|()

TikzOutputCtrl lf::io::operator| ( const TikzOutputCtrl lhs,
const TikzOutputCtrl rhs 
)

Definition at line 13 of file write_tikz.cc.

◆ ReadGmshFile()

std::variant< GMshFileV2, GMshFileV4 > lf::io::ReadGmshFile ( const std::string &  filename)

Definition at line 642 of file gmsh_reader.cc.

References readGmshFileV2(), and ReadGmshFileV4().

◆ readGmshFileV2()

GMshFileV2 lf::io::readGmshFileV2 ( std::string::const_iterator  begin,
std::string::const_iterator  end,
const std::string &  version,
bool  is_binary,
int  size_t_size,
int  one,
const std::string &  filename 
)

Read a *.msh file from disk and copy it's contents into the MshFile Datastructure.

So far the following sections of the .msh file are read:

  • $MeshFormat
  • PhysicalNames
  • $Nodes
  • $Elements
  • $Periodic

All other sections are ignored.

Note
We support the MshFile format 2.2 in binary or text form.
This routine is mainly used by the GmshReader class.

Name the elements for better error parsing:

Definition at line 609 of file gmsh_file_v2.cc.

References lf::io::GMshFileV2::DoubleSize, lf::io::GMshFileV2::IsBinary, and lf::io::GMshFileV2::VersionNumber.

Referenced by ReadGmshFile().

◆ ReadGmshFileV4()

GMshFileV4 lf::io::ReadGmshFileV4 ( std::string::const_iterator  begin,
std::string::const_iterator  end,
const std::string &  version,
bool  is_binary,
int  size_t_size,
int  one,
const std::string &  filename 
)

Read a GmshFile with format 4 and return it as an in-memory struct.

Parameters
beginbeginning of the file to parse (without header)
endend of the file to parse
versionthe exact version of the file, should be >=4
is_binaryIs the file in binary format? (from header)
size_t_sizesizeof(std::size_t) (from header)
oneRepresentation of the number one if is_binary==true (from header)
filenameThe name of the file that is being parsed (for better diagnostics)

Definition at line 304 of file gmsh_file_v4.cc.

References lf::io::GMshFileV4::is_binary, lf::io::GMshFileV4::periodic_links, lf::io::GMshFileV4::size_t_size, and lf::io::GMshFileV4::version_number.

Referenced by ReadGmshFile().

◆ RefElOf() [1/2]

base::RefEl lf::io::RefElOf ( GMshFileV2::ElementType  et)

◆ RefElOf() [2/2]

base::RefEl lf::io::RefElOf ( GMshFileV4::ElementType  et)

◆ writeMatlab()

void lf::io::writeMatlab ( const lf::mesh::Mesh mesh,
std::string  filename 
)

Writes affine triangulation data to file in MATLAB format.

Parameters
meshthe mesh to be stored to file
filenamename of output file: .m will be appended unless present

This function creates a .m-file containing a MATLAB function of the same name

function [x,y,TRI,EDS] = filename()

that initializes four variables

  • x the x-coordinates of the nodes if the affine triangular mesh
  • y the y-coordinates of the nodes if the affine triangular mesh
  • TRI an N x 4 -matrix whose rows contain the indices of the nodes of every triangle of the mesh in positions 1-3, the triangle index in position 4.
  • QUAD an N x 5 -matrix; each row contains the node indices of a quad as first four entries, and the cell index as last entry.
  • EDS an Mx2 - matrix containing the indices of the endpoints of the edges of the mesh.

The data returned by this function can be visualized by the MATLAB function plot_lf_mesh(), which is also provided with LehrFEM++.

Definition at line 9 of file write_matlab.cc.

References lf::mesh::Mesh::DimMesh(), lf::mesh::Mesh::Entities(), lf::geometry::Geometry::Geometry(), lf::geometry::Geometry::Global(), lf::mesh::Mesh::Index(), lf::base::RefEl::kQuad(), lf::base::RefEl::kSegment(), lf::base::RefEl::kTria(), lf::mesh::Mesh::NumEntities(), and lf::base::RefEl::RefEl().

Referenced by lf::refinement::WriteMatlab().

◆ writeMatplotlib()

void lf::io::writeMatplotlib ( const lf::mesh::Mesh mesh,
std::string  filename 
)

Write affine triangulation data to file in matplotlib format.

Parameters
meshthe mesh to be stored to file
filenamename of output file: .csv appended unless present

This function creates a .csv file containing all relevant information about the given mesh in the following format:

Points: codim, index, x_coord, y_coord

Segments: codim, index, point1_idx, point2_idx

Triangles/Quadrilaterals: codim, index, segment1_idx, segment2_idx, ...

The .csv file can be read by plot_mesh.py to visualize the mesh

Definition at line 16 of file write_matplotlib.cc.

References lf::mesh::Mesh::DimMesh(), lf::mesh::Mesh::Entities(), lf::geometry::Geometry::Geometry(), lf::geometry::Geometry::Global(), lf::mesh::Mesh::Index(), lf::base::RefEl::kPoint(), lf::base::RefEl::kQuad(), lf::base::RefEl::kSegment(), lf::base::RefEl::kTria(), lf::base::RefEl::NodeCoords(), lf::base::RefEl::RefEl(), and lf::base::RefEl::ToString().

◆ writeTikZ() [1/2]

bool lf::io::writeTikZ ( const lf::mesh::Mesh mesh,
const std::string &  filename,
std::function< bool(const lf::mesh::Entity &)> &&  selector,
TikzOutputCtrl  output_ctrl = TikzOutputCtrl::RenderCells 
)

Writes mesh to file in TikZ Graphics format. File as input in LaTeX will draw the mesh.

Parameters
meshthe mesh to be stored to file
filenamename of output file.
selectorfunction which chooses what entities to print
output_ctrlenum flags controlling amount of output
Returns
false, if there has been a problem writing to file

This function writes a file of code, which included in LaTeX draws a mesh using TikZ Graphics. In particular, edges, cells and nodes in the mesh can be written to file and visualized. Numbering of the aforementioned and local vertice numbering of cells can be enabled by using enum flags. Combine the flags by using the binary or (|) operator to get a more detailed visualization of the mesh.
Another option is to pass the corresponding integer value directly as an argument. See the enum definition for correct value.
The selector function goes through all entities and returns either true or false. An entity is printed iff selector returns true. For instance, selector can check and return true if an entity is a point. Then only nodes in the mesh are printed. See example below.

Output control flags:

Note
If no details about nodes, cells or edges are wanted, simply pass 0 as the 'int output_ctrl' parameter in writeTikZ(). This will draw only the mesh grid and nodes.
Omitting the output_ctrl argument completely when calling the function: Cells, numbering of cells and numbering of vertices will be printed. (output_ctrl = 7)
TikzOutputCtrl::RenderCells must be enabled in order to use the flags for numbering of cells and of local vertices of cells.
If the selector argument is omitted, all entities in the mesh are printed.

In the LaTeX document, remember to include "\usepackage{tikz}". Use "\input{}" to include the code file and visualize the mesh.

Examples of use

Function call
    // Enum flag for node numbering
    writeTikZ(*mesh, "filename.txt", TikzOutputCtrl::NodeNumbering);
    // Combining enum flags, enabling more detailed output
    // The two examples are equivalent:
    writeTikZ(*mesh, "filename.txt",
TikzOutputCtrl::RenderCells|TikzOutputCtrl::EdgeNumbering|TikzOutputCtrl::VerticeNumbering);
    writeTikZ(*mesh, "filename.txt", 21);
    // Note that ::VerticeNumbering only works because ::RenderCells is
activated.
    // Without flags
    writeTikZ(*mesh, "filename.txt",0);
    // Without specifying last argument
    writeTikZ(*mesh, "filename.txt"); is equivalent to writeTikZ(*mesh,
"filename.txt", 7);
 * 
How to include into LaTeX source
     \documentclass{article}
     \usepackage{tikz}
     \begin{document}
     \input{"filename.txt"}
     \end{document}
 * 
selector function
  // Example of selector function
    auto desiredEntities = [&](const lf::mesh::Entity& entity) {
      // Nodes only
      return (entity.RefEl() == lf::base::RefEl::kPoint());
    };

    * 

Definition at line 23 of file write_tikz.cc.

References ArrowTips, CellNumbering, lf::mesh::Mesh::DimMesh(), EdgeNumbering, lf::mesh::Mesh::Entities(), lf::geometry::Geometry::Geometry(), lf::geometry::Geometry::Global(), lf::mesh::Mesh::Index(), lf::base::RefEl::kPoint(), lf::base::RefEl::kQuad(), lf::base::RefEl::kSegment(), lf::base::RefEl::kTria(), lf::base::RefEl::NodeCoords(), NodeNumbering, lf::mesh::Mesh::NumEntities(), lf::base::RefEl::NumNodes(), lf::base::RefEl::RefEl(), RenderCells, VerticeNumbering, and WithPreamble.

Referenced by writeTikZ().

◆ writeTikZ() [2/2]

bool lf::io::writeTikZ ( const lf::mesh::Mesh mesh,
const std::string &  filename,
TikzOutputCtrl  output_ctrl = TikzOutputCtrl::RenderCells 
)

TikZ output of all entities.

See also
writeTikZ

Calls the general implementation with a selector that returns true throughout.

Definition at line 253 of file write_tikz.cc.

References writeTikZ().

◆ WriteToFile()

void lf::io::WriteToFile ( const VtkFile vtk_file,
const std::string &  filename 
)

Definition at line 550 of file vtk_writer.cc.

References lf::io::VtkFile::BINARY, and lf::io::VtkFile::format.

Referenced by lf::io::VtkWriter::~VtkWriter().