libMesh
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
libMesh::MatlabIO Class Reference

This class implements reading meshes in the Matlab PDE toolkit in a proprietary format. More...

#include <matlab_io.h>

Inheritance diagram for libMesh::MatlabIO:
[legend]

Public Member Functions

 MatlabIO (MeshBase &)
 Constructor.
 
virtual void read (const std::string &name) override
 Reads in a matlab data file based on the string you pass it.
 
bool is_parallel_format () const
 Returns true iff this mesh file format and input class are parallelized, so that all processors can read their share of the data at once.
 

Protected Member Functions

MeshBasemesh ()
 
void set_n_partitions (unsigned int n_parts)
 Sets the number of partitions in the mesh.
 
void skip_comment_lines (std::istream &in, const char comment_start)
 Reads input from in, skipping all the lines that start with the character comment_start.
 

Protected Attributes

std::vector< bool > elems_of_dimension
 A vector of bools describing what dimension elements have been encountered when reading a mesh.
 

Private Member Functions

void read_stream (std::istream &in)
 Implementation of the read() function.
 

Private Attributes

MeshBase_obj
 A pointer to a non-const object object.
 
const bool _is_parallel_format
 Flag specifying whether this format is parallel-capable.
 

Detailed Description

This class implements reading meshes in the Matlab PDE toolkit in a proprietary format.

A VALID INPUT FILE for this type of mesh should be generated in Matlab with the following steps: 1.) Draw the domain and triangulate it in the GUI 2.) Export the mesh to matlab using Mesh->Export Mesh 3.) Create a file with this script:

fid = fopen('filename', 'w');
fprintf(fid, '%d %d \n', length(p), length(t));
fprintf(fid, '%f %f \n', p);
fprintf(fid, '%d %d %d %d \n', t);
fclose(fid);

What's going on here? There is no standard for exporting PDE toolkit meshes to files in Matlab. When you choose "export mesh" in the GUI, it returns three matrices that it likes to call p, e, and t. All meshes (as far as I can tell) that come from the PDE toolkit are 2D triangle meshes.

p is the point matrix... Row 1: x coordinate Row 2: y coordinate

e is the edge matrix ... Row 1: starting point number (dummy) Row 2: ending point number (dummy) Row 3: starting parameter value (?) (dummy) Row 4: ending parameter value (?) (dummy) Row 5: boundary segment number (?) (dummy) Row 6: left-hand subdomain number (dummy) Row 7: right-hand subdomain number (dummy)

t is the triangle matrix ... Row 1: Node number 1 Row 2: Node number 2 Row 3: Node number 3 Row 4: subdomain number (dummy)

There are some important things to notice here: o The implied ordering of the p matrix is 1..N o The e matrix is entirely irrelevant in this code o All of the matrices are row based

Author
John W. Peterson
Date
2004

Definition at line 85 of file matlab_io.h.

Constructor & Destructor Documentation

◆ MatlabIO()

libMesh::MatlabIO::MatlabIO ( MeshBase mesh_in)
inlineexplicit

Constructor.

Takes a non-const Mesh reference which it will fill up with elements.

Definition at line 114 of file matlab_io.h.

114 :
115 MeshInput<MeshBase> (mesh_in)
116{}

Member Function Documentation

◆ is_parallel_format()

bool libMesh::MeshInput< MeshBase >::is_parallel_format ( ) const
inlineinherited

Returns true iff this mesh file format and input class are parallelized, so that all processors can read their share of the data at once.

Definition at line 87 of file mesh_input.h.

87{ return this->_is_parallel_format; }
const bool _is_parallel_format
Flag specifying whether this format is parallel-capable.
Definition mesh_input.h:130

◆ mesh()

MeshBase & libMesh::MeshInput< MeshBase >::mesh ( )
inlineprotectedinherited
Returns
The object as a writable reference.

Definition at line 94 of file mesh_input.h.

179{
180 libmesh_error_msg_if(_obj == nullptr, "ERROR: _obj should not be nullptr!");
181 return *_obj;
182}
MeshBase * _obj
A pointer to a non-const object object.
Definition mesh_input.h:123

◆ read()

void libMesh::MatlabIO::read ( const std::string &  name)
overridevirtual

Reads in a matlab data file based on the string you pass it.

Implements libMesh::MeshInput< MeshBase >.

Definition at line 33 of file matlab_io.C.

34{
35 std::ifstream in (name.c_str());
36
37 this->read_stream(in);
38}
void read_stream(std::istream &in)
Implementation of the read() function.
Definition matlab_io.C:41
std::string name(const ElemQuality q)
This function returns a string containing some name for q.

References read_stream().

Referenced by libMesh::NameBasedIO::read().

◆ read_stream()

void libMesh::MatlabIO::read_stream ( std::istream &  in)
private

Implementation of the read() function.

This function is called by the public interface function and implements reading the file.

Definition at line 41 of file matlab_io.C.

42{
43 // This is a serial-only process for now;
44 // the Mesh should be read on processor 0 and
45 // broadcast later
46 libmesh_assert_equal_to (this->mesh().processor_id(), 0);
47
48 // Get a reference to the mesh
49 MeshBase & the_mesh = MeshInput<MeshBase>::mesh();
50
51 // Clear any existing mesh data
52 the_mesh.clear();
53
54 // PDE toolkit only works in 2D
55 the_mesh.set_mesh_dimension(2);
56
57#if LIBMESH_DIM < 2
58 libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support.");
59#endif
60
61 // Check the input buffer
62 libmesh_assert (in.good());
63
64 unsigned int nNodes=0, nElem=0;
65
66 in >> nNodes // Read the number of nodes
67 >> nElem; // Read the number of elements
68
69 // Sort of check that it worked
70 libmesh_assert_greater (nNodes, 0);
71 libmesh_assert_greater (nElem, 0);
72
73 // Read the nodal coordinates
74 {
75 Real x=0., y=0., z=0.;
76
77 for (unsigned int i=0; i<nNodes; i++)
78 {
79 in >> x // x-coordinate value
80 >> y; // y-coordinate value
81
82 the_mesh.add_point ( Point(x,y,z), i);
83 }
84 }
85
86 // Read the elements (elements)
87 {
88 unsigned int node=0, dummy=0;
89
90 for (unsigned int i=0; i<nElem; i++)
91 {
92 // Always build a triangle
93 Elem * elem = the_mesh.add_elem(Elem::build_with_id(TRI3, i));
94
95 for (unsigned int n=0; n<3; n++) // Always read three 3 nodes
96 {
97 in >> node;
98 elem->set_node(n, the_mesh.node_ptr(node-1)); // Assign the node number
99 }
100
101 // There is an additional subdomain number here,
102 // so we read it and get rid of it!
103 in >> dummy;
104 }
105 }
106
107}
static std::unique_ptr< Elem > build_with_id(const ElemType type, dof_id_type id)
Calls the build() method above with a nullptr parent, and additionally sets the newly-created Elem's ...
Definition elem.C:556
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References libMesh::MeshBase::add_elem(), libMesh::MeshBase::add_point(), libMesh::Elem::build_with_id(), libMesh::MeshBase::clear(), libMesh::libmesh_assert(), libMesh::MeshInput< MT >::mesh(), libMesh::MeshInput< MeshBase >::mesh(), libMesh::MeshBase::node_ptr(), libMesh::Real, libMesh::MeshBase::set_mesh_dimension(), libMesh::Elem::set_node(), and libMesh::TRI3.

Referenced by read().

◆ set_n_partitions()

void libMesh::MeshInput< MeshBase >::set_n_partitions ( unsigned int  n_parts)
inlineprotectedinherited

Sets the number of partitions in the mesh.

Typically this gets done by the partitioner, but some parallel file formats begin "pre-partitioned".

Definition at line 101 of file mesh_input.h.

101{ this->mesh().set_n_partitions() = n_parts; }
MeshBase & mesh

◆ skip_comment_lines()

void libMesh::MeshInput< MeshBase >::skip_comment_lines ( std::istream &  in,
const char  comment_start 
)
protectedinherited

Reads input from in, skipping all the lines that start with the character comment_start.

Definition at line 113 of file mesh_input.h.

189{
190 char c, line[256];
191
192 while (in.get(c), c==comment_start)
193 in.getline (line, 255);
194
195 // put back first character of
196 // first non-comment line
197 in.putback (c);
198}

Member Data Documentation

◆ _is_parallel_format

const bool libMesh::MeshInput< MeshBase >::_is_parallel_format
privateinherited

Flag specifying whether this format is parallel-capable.

If this is false (default) I/O is only permitted when the mesh has been serialized.

Definition at line 130 of file mesh_input.h.

◆ _obj

MeshBase * libMesh::MeshInput< MeshBase >::_obj
privateinherited

A pointer to a non-const object object.

This allows us to read the object from file.

Definition at line 123 of file mesh_input.h.

◆ elems_of_dimension

std::vector<bool> libMesh::MeshInput< MeshBase >::elems_of_dimension
protectedinherited

A vector of bools describing what dimension elements have been encountered when reading a mesh.

Definition at line 107 of file mesh_input.h.


The documentation for this class was generated from the following files: