~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/io/XMLMesh.cpp

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2003-2006 Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// First added:  2003-10-21
5
 
// Last changed: 2006-06-22
6
 
 
7
 
#include <dolfin/dolfin_log.h>
8
 
#include <dolfin/CellType.h>
9
 
#include <dolfin/Mesh.h>
10
 
#include <dolfin/XMLMesh.h>
11
 
 
12
 
using namespace dolfin;
13
 
 
14
 
//-----------------------------------------------------------------------------
15
 
XMLMesh::XMLMesh(Mesh& mesh) : XMLObject(), _mesh(mesh), state(OUTSIDE)
16
 
{
17
 
  // Do nothing
18
 
}
19
 
//-----------------------------------------------------------------------------
20
 
XMLMesh::~XMLMesh()
21
 
{
22
 
  // Do nothing
23
 
}
24
 
//-----------------------------------------------------------------------------
25
 
void XMLMesh::startElement(const xmlChar *name, const xmlChar **attrs)
26
 
{
27
 
  switch ( state )
28
 
  {
29
 
  case OUTSIDE:
30
 
    
31
 
    if ( xmlStrcasecmp(name, (xmlChar *) "mesh") == 0 )
32
 
    {
33
 
      readMesh(name, attrs);
34
 
      state = INSIDE_MESH;
35
 
    }
36
 
    
37
 
    break;
38
 
 
39
 
  case INSIDE_MESH:
40
 
    
41
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
42
 
    {
43
 
      readVertices(name, attrs);
44
 
      state = INSIDE_VERTICES;
45
 
    }
46
 
    else if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
47
 
    {
48
 
      readCells(name, attrs);
49
 
      state = INSIDE_CELLS;
50
 
    }
51
 
    
52
 
    break;
53
 
    
54
 
  case INSIDE_VERTICES:
55
 
    
56
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertex") == 0 )
57
 
      readVertex(name, attrs);
58
 
 
59
 
    break;
60
 
    
61
 
  case INSIDE_CELLS:
62
 
    
63
 
    if ( xmlStrcasecmp(name, (xmlChar *) "interval") == 0 )
64
 
      readInterval(name, attrs);
65
 
    else if ( xmlStrcasecmp(name, (xmlChar *) "triangle") == 0 )
66
 
      readTriangle(name, attrs);
67
 
    else if ( xmlStrcasecmp(name, (xmlChar *) "tetrahedron") == 0 )
68
 
      readTetrahedron(name, attrs);
69
 
    
70
 
    break;
71
 
    
72
 
  default:
73
 
    ;
74
 
  }
75
 
}
76
 
//-----------------------------------------------------------------------------
77
 
void XMLMesh::endElement(const xmlChar *name)
78
 
{
79
 
  switch ( state )
80
 
  {
81
 
  case INSIDE_MESH:
82
 
    
83
 
    if ( xmlStrcasecmp(name, (xmlChar *) "mesh") == 0 )
84
 
    {
85
 
      closeMesh();
86
 
      state = DONE;
87
 
    }
88
 
    
89
 
    break;
90
 
    
91
 
  case INSIDE_VERTICES:
92
 
    
93
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
94
 
      state = INSIDE_MESH;
95
 
    
96
 
    break;
97
 
 
98
 
  case INSIDE_CELLS:
99
 
         
100
 
    if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
101
 
      state = INSIDE_MESH;
102
 
    
103
 
    break;
104
 
    
105
 
  default:
106
 
    ;
107
 
  }
108
 
}
109
 
//-----------------------------------------------------------------------------
110
 
void XMLMesh::open(std::string filename)
111
 
{
112
 
  // Do nothing
113
 
}
114
 
//-----------------------------------------------------------------------------
115
 
bool XMLMesh::close()
116
 
{
117
 
  return state == DONE;
118
 
}
119
 
//-----------------------------------------------------------------------------
120
 
void XMLMesh::readMesh(const xmlChar *name, const xmlChar **attrs)
121
 
{
122
 
  // Parse values
123
 
  std::string type = parseString(name, attrs, "celltype");
124
 
  uint gdim = parseUnsignedInt(name, attrs, "dim");
125
 
  
126
 
  // Create cell type to get topological dimension
127
 
  CellType* cell_type = CellType::create(type);
128
 
  uint tdim = cell_type->dim();
129
 
  delete cell_type;
130
 
 
131
 
  // Open mesh for editing
132
 
  editor.open(_mesh, CellType::string2type(type), tdim, gdim);
133
 
}
134
 
//-----------------------------------------------------------------------------
135
 
void XMLMesh::readVertices(const xmlChar *name, const xmlChar **attrs)
136
 
{
137
 
  // Parse values
138
 
  uint num_vertices = parseUnsignedInt(name, attrs, "size");
139
 
 
140
 
  // Set number of vertices
141
 
  editor.initVertices(num_vertices);
142
 
}
143
 
//-----------------------------------------------------------------------------
144
 
void XMLMesh::readCells(const xmlChar *name, const xmlChar **attrs)
145
 
{
146
 
  // Parse values
147
 
  uint num_cells = parseUnsignedInt(name, attrs, "size");
148
 
 
149
 
  // Set number of vertices
150
 
  editor.initCells(num_cells);
151
 
}
152
 
//-----------------------------------------------------------------------------
153
 
void XMLMesh::readVertex(const xmlChar *name, const xmlChar **attrs)
154
 
{
155
 
  // Read index
156
 
  uint v = parseUnsignedInt(name, attrs, "index");
157
 
  
158
 
  // Handle differently depending on geometric dimension
159
 
  switch ( _mesh.geometry().dim() )
160
 
  {
161
 
  case 1:
162
 
    {
163
 
      real x = parseReal(name, attrs, "x");
164
 
      editor.addVertex(v, x);
165
 
    }
166
 
    break;
167
 
  case 2:
168
 
    {
169
 
      real x = parseReal(name, attrs, "x");
170
 
      real y = parseReal(name, attrs, "y");
171
 
      editor.addVertex(v, x, y);
172
 
    }
173
 
    break;
174
 
  case 3:
175
 
    {
176
 
      real x = parseReal(name, attrs, "x");
177
 
      real y = parseReal(name, attrs, "y");
178
 
      real z = parseReal(name, attrs, "z");
179
 
      editor.addVertex(v, x, y, z);
180
 
    }
181
 
    break;
182
 
  default:
183
 
    error("Dimension of mesh must be 1, 2 or 3.");
184
 
  }
185
 
}
186
 
//-----------------------------------------------------------------------------
187
 
void XMLMesh::readInterval(const xmlChar *name, const xmlChar **attrs)
188
 
{
189
 
  // Check dimension
190
 
  if ( _mesh.topology().dim() != 1 )
191
 
    error("Mesh entity (interval) does not match dimension of mesh (%d).",
192
 
                 _mesh.topology().dim());
193
 
 
194
 
  // Parse values
195
 
  uint c  = parseUnsignedInt(name, attrs, "index");
196
 
  uint v0 = parseUnsignedInt(name, attrs, "v0");
197
 
  uint v1 = parseUnsignedInt(name, attrs, "v1");
198
 
  
199
 
  // Add cell
200
 
  editor.addCell(c, v0, v1);
201
 
}
202
 
//-----------------------------------------------------------------------------
203
 
void XMLMesh::readTriangle(const xmlChar *name, const xmlChar **attrs)
204
 
{
205
 
  // Check dimension
206
 
  if ( _mesh.topology().dim() != 2 )
207
 
    error("Mesh entity (triangle) does not match dimension of mesh (%d).",
208
 
                 _mesh.topology().dim());
209
 
 
210
 
  // Parse values
211
 
  uint c  = parseUnsignedInt(name, attrs, "index");
212
 
  uint v0 = parseUnsignedInt(name, attrs, "v0");
213
 
  uint v1 = parseUnsignedInt(name, attrs, "v1");
214
 
  uint v2 = parseUnsignedInt(name, attrs, "v2");
215
 
  
216
 
  // Add cell
217
 
  editor.addCell(c, v0, v1, v2);
218
 
}
219
 
//-----------------------------------------------------------------------------
220
 
void XMLMesh::readTetrahedron(const xmlChar *name, const xmlChar **attrs)
221
 
{
222
 
  // Check dimension
223
 
  if ( _mesh.topology().dim() != 3 )
224
 
    error("Mesh entity (tetrahedron) does not match dimension of mesh (%d).",
225
 
                 _mesh.topology().dim());
226
 
 
227
 
  // Parse values
228
 
  uint c  = parseUnsignedInt(name, attrs, "index");
229
 
  uint v0 = parseUnsignedInt(name, attrs, "v0");
230
 
  uint v1 = parseUnsignedInt(name, attrs, "v1");
231
 
  uint v2 = parseUnsignedInt(name, attrs, "v2");
232
 
  uint v3 = parseUnsignedInt(name, attrs, "v3");
233
 
  
234
 
  // Add cell
235
 
  editor.addCell(c, v0, v1, v2, v3);
236
 
}
237
 
//-----------------------------------------------------------------------------
238
 
void XMLMesh::closeMesh()
239
 
{
240
 
  editor.close();
241
 
}
242
 
//-----------------------------------------------------------------------------