~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to dolfin/io/XMLMesh.cpp

  • Committer: Anders Logg
  • Date: 2008-05-21 22:42:48 UTC
  • mfrom: (2668.6.1 trunk)
  • mto: (2668.8.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 2670.
  • Revision ID: logg@simula.no-20080521224248-7baydkw3uy323fur
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2003-2006 Anders Logg.
 
1
// Copyright (C) 2003-2008 Anders Logg.
2
2
// Licensed under the GNU LGPL Version 2.1.
3
3
//
4
4
// First added:  2003-10-21
5
 
// Last changed: 2006-06-22
 
5
// Last changed: 2008-05-21
6
6
 
7
7
#include <dolfin/log/dolfin_log.h>
8
8
#include <dolfin/mesh/CellType.h>
13
13
using namespace dolfin;
14
14
 
15
15
//-----------------------------------------------------------------------------
16
 
XMLMesh::XMLMesh(Mesh& mesh) : XMLObject(), _mesh(mesh), state(OUTSIDE), f(0)
 
16
XMLMesh::XMLMesh(Mesh& mesh) : XMLObject(), _mesh(mesh), state(OUTSIDE), f(0), a(0)
17
17
{
18
18
  // Do nothing
19
19
}
31
31
    
32
32
    if ( xmlStrcasecmp(name, (xmlChar *) "mesh") == 0 )
33
33
    {
 
34
      cout << "Reading mesh" << endl;
 
35
 
34
36
      readMesh(name, attrs);
35
37
      state = INSIDE_MESH;
36
38
    }
41
43
    
42
44
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
43
45
    {
 
46
      cout << "Reading vertices" << endl;
 
47
 
44
48
      readVertices(name, attrs);
45
49
      state = INSIDE_VERTICES;
46
50
    }
47
51
    else if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
48
52
    {
 
53
      cout << "Reading cells" << endl;
 
54
 
49
55
      readCells(name, attrs);
50
56
      state = INSIDE_CELLS;
51
57
    }
52
58
    else if ( xmlStrcasecmp(name, (xmlChar *) "data") == 0 )
 
59
    {
 
60
      cout << "Reading data" << endl;
53
61
      state = INSIDE_DATA;
54
 
    
 
62
    }
 
63
 
55
64
    break;
56
65
    
57
66
  case INSIDE_VERTICES:
76
85
    
77
86
    if ( xmlStrcasecmp(name, (xmlChar *) "meshfunction") == 0 )
78
87
    {
 
88
      cout << "Reading mesh function" << endl;
79
89
      readMeshFunction(name, attrs);
80
90
      state = INSIDE_MESH_FUNCTION;
81
91
    }
 
92
    if ( xmlStrcasecmp(name, (xmlChar *) "array") == 0 )
 
93
    {
 
94
      cout << "Reading array" << endl;
 
95
      readArray(name, attrs);
 
96
      state = INSIDE_ARRAY;
 
97
    }
82
98
 
83
99
    break;
84
100
 
85
101
  case INSIDE_MESH_FUNCTION:
86
102
    
87
103
    if ( xmlStrcasecmp(name, (xmlChar *) "entity") == 0 )
88
 
      readEntity(name, attrs);
 
104
      readMeshEntity(name, attrs);
 
105
 
 
106
    break;
 
107
 
 
108
  case INSIDE_ARRAY:
 
109
    
 
110
    if ( xmlStrcasecmp(name, (xmlChar *) "element") == 0 )
 
111
      readArrayElement(name, attrs);
89
112
 
90
113
    break;
91
114
 
104
127
    {
105
128
      closeMesh();
106
129
      state = DONE;
 
130
      cout << "End XML" << endl;
107
131
    }
108
132
    
109
133
    break;
111
135
  case INSIDE_VERTICES:
112
136
    
113
137
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
114
 
      state = INSIDE_MESH;
115
 
    
 
138
    {
 
139
      state = INSIDE_MESH;    
 
140
      cout << "End vertices" << endl;
 
141
    }
 
142
 
116
143
    break;
117
144
 
118
145
  case INSIDE_CELLS:
119
146
         
120
147
    if ( xmlStrcasecmp(name, (xmlChar *) "cells") == 0 )
 
148
    {
121
149
      state = INSIDE_MESH;
122
 
    
 
150
      cout << "End cells" << endl;
 
151
    }
 
152
 
123
153
    break;
124
154
 
125
155
  case INSIDE_DATA:
126
156
 
127
157
    if ( xmlStrcasecmp(name, (xmlChar *) "data") == 0 )
 
158
    {
128
159
      state = INSIDE_MESH;
 
160
      cout << "End data" << endl;
 
161
    }
129
162
 
130
163
    break;
131
164
 
132
165
  case INSIDE_MESH_FUNCTION:
133
166
 
134
167
    if ( xmlStrcasecmp(name, (xmlChar *) "meshfunction") == 0 )
135
 
      state = INSIDE_DATA;
 
168
    {
 
169
      state = INSIDE_DATA;
 
170
      cout << "End mesh function" << endl;
 
171
    }
 
172
 
 
173
    break;
 
174
 
 
175
  case INSIDE_ARRAY:
 
176
 
 
177
    if ( xmlStrcasecmp(name, (xmlChar *) "array") == 0 )
 
178
    {
 
179
      state = INSIDE_DATA;
 
180
      cout << "End array" << endl;
 
181
    }
136
182
 
137
183
    break;
138
184
 
274
320
  // Parse values
275
321
  const std::string id = parseString(name, attrs, "name");
276
322
  const std::string type = parseString(name, attrs, "type");
277
 
  const uint dim = parseUnsignedInt(name, attrs,   "dim");
278
 
  const uint size = parseUnsignedInt(name, attrs,   "size");
 
323
  const uint dim = parseUnsignedInt(name, attrs, "dim");
 
324
  const uint size = parseUnsignedInt(name, attrs, "size");
279
325
 
280
326
  // Only uint supported at this point
281
327
  if (strcmp(type.c_str(), "uint") != 0)
282
328
    error("Only uint-valued mesh data is currently supported.");
283
329
 
 
330
  // Check size
 
331
  _mesh.init(dim);
 
332
  if (_mesh.size(dim) != size)
 
333
    error("Wrong number of values for MeshFunction, expecting %d.", _mesh.size(dim));
 
334
 
284
335
  // Register data
285
 
  f = _mesh.data().create(id, dim);
286
 
  
 
336
  f = _mesh.data().createMeshFunction(id, dim);
 
337
  dolfin_assert(f);
 
338
 
287
339
  // Set all values to zero
288
340
  *f = 0;
289
 
 
290
 
  // Check size
291
 
  if (size >= f->size())
292
 
    error("Wrong size of mesh data \"%s\", at most \"%d\" values may be specified.",
293
 
          id.c_str(), f->size());
 
341
}
 
342
void XMLMesh::readArray(const xmlChar* name, const xmlChar** attrs)
 
343
{
 
344
  // Parse values
 
345
  const std::string id = parseString(name, attrs, "name");
 
346
  const std::string type = parseString(name, attrs, "type");
 
347
  const uint size = parseUnsignedInt(name, attrs, "size");
 
348
 
 
349
  // Only uint supported at this point
 
350
  if (strcmp(type.c_str(), "uint") != 0)
 
351
    error("Only uint-valued mesh data is currently supported.");
 
352
 
 
353
  // Register data
 
354
  a = _mesh.data().createArray(id, size);
 
355
  dolfin_assert(a);
294
356
}
295
357
//-----------------------------------------------------------------------------
296
 
void XMLMesh::readEntity(const xmlChar* name, const xmlChar** attrs)
 
358
void XMLMesh::readMeshEntity(const xmlChar* name, const xmlChar** attrs)
297
359
{
298
360
  // Read index
299
361
  const uint index = parseUnsignedInt(name, attrs, "index");
305
367
  f->set(index, value);
306
368
}
307
369
//-----------------------------------------------------------------------------
 
370
void XMLMesh::readArrayElement(const xmlChar* name, const xmlChar** attrs)
 
371
{
 
372
  return;
 
373
 
 
374
  // Read index
 
375
  const uint index = parseUnsignedInt(name, attrs, "index");
 
376
 
 
377
  // Read and set value
 
378
  dolfin_assert(a);
 
379
  dolfin_assert(index < a->size());
 
380
  const uint value = parseUnsignedInt(name, attrs, "value");
 
381
  (*a)[index] = value;
 
382
}
 
383
//-----------------------------------------------------------------------------
308
384
void XMLMesh::closeMesh()
309
385
{
310
386
  editor.close();