libMesh
Loading...
Searching...
No Matches
matlab_io.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19// C++ includes
20#include <fstream>
21
22// Local includes
23#include "libmesh/matlab_io.h"
24#include "libmesh/mesh_base.h"
25#include "libmesh/face_tri3.h"
26
27namespace libMesh
28{
29
30// ------------------------------------------------------------
31// MatlabIO class members
32
33void MatlabIO::read(const std::string & name)
34{
35 std::ifstream in (name.c_str());
36
37 this->read_stream(in);
38}
39
40
41void MatlabIO::read_stream(std::istream & in)
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
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}
108
109} // namespace libMesh
This is the base class from which all geometric element types are derived.
Definition elem.h:96
virtual Node *& set_node(const unsigned int i)
Definition elem.h:2567
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
void read_stream(std::istream &in)
Implementation of the read() function.
Definition matlab_io.C:41
virtual void read(const std::string &name) override
Reads in a matlab data file based on the string you pass it.
Definition matlab_io.C:33
This is the MeshBase class.
Definition mesh_base.h:81
virtual const Node * node_ptr(const dof_id_type i) const =0
void set_mesh_dimension(unsigned char d)
Resets the logical dimension of the mesh.
Definition mesh_base.h:423
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
virtual void clear()
Deletes all the element and node data that is currently stored.
Definition mesh_base.C:1036
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
The libMesh namespace provides an interface to certain functionality in the library.
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real