~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/test/mesh/cpp/test.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) 2007 Anders Logg.
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// First added:  2007-05-14
5
 
// Last changed: 2007-05-24
6
 
//
7
 
// Unit tests for the mesh library
8
 
 
9
 
#include <dolfin.h>
10
 
#include <dolfin/unittest.h>
11
 
 
12
 
using namespace dolfin;
13
 
 
14
 
class SimpleShapes : public CppUnit::TestFixture
15
 
{
16
 
  CPPUNIT_TEST_SUITE(SimpleShapes);
17
 
  CPPUNIT_TEST(testUnitSquare);
18
 
  CPPUNIT_TEST(testUnitCube);
19
 
  CPPUNIT_TEST_SUITE_END();
20
 
 
21
 
public: 
22
 
 
23
 
  void testUnitSquare()
24
 
  {
25
 
    // Create mesh of unit square
26
 
    UnitSquare mesh(5, 7);
27
 
    CPPUNIT_ASSERT(mesh.numVertices() == 48);
28
 
    CPPUNIT_ASSERT(mesh.numCells() == 70);
29
 
  }
30
 
   
31
 
  void testUnitCube()
32
 
  {
33
 
    // Create mesh of unit cube
34
 
    UnitCube mesh(5, 7, 9);
35
 
    CPPUNIT_ASSERT(mesh.numVertices() == 480);
36
 
    CPPUNIT_ASSERT(mesh.numCells() == 1890);
37
 
  }
38
 
 
39
 
};
40
 
 
41
 
class MeshRefinement : public CppUnit::TestFixture
42
 
{
43
 
  CPPUNIT_TEST_SUITE(MeshRefinement);
44
 
  CPPUNIT_TEST(testRefineUnitSquare);
45
 
  CPPUNIT_TEST(testRefineUnitCube);
46
 
  CPPUNIT_TEST_SUITE_END();
47
 
 
48
 
public:
49
 
 
50
 
  void testRefineUnitSquare()
51
 
  {
52
 
    // Refine mesh of unit square
53
 
    UnitSquare mesh(5, 7);
54
 
    mesh.refine();
55
 
    CPPUNIT_ASSERT(mesh.numVertices() == 165);
56
 
    CPPUNIT_ASSERT(mesh.numCells() == 280);
57
 
  }
58
 
  
59
 
  void testRefineUnitCube()
60
 
  {
61
 
    // Refine mesh of unit cube
62
 
    UnitCube mesh(5, 7, 9);
63
 
    mesh.refine();
64
 
    CPPUNIT_ASSERT(mesh.numVertices() == 3135);
65
 
    CPPUNIT_ASSERT(mesh.numCells() == 15120);
66
 
  }
67
 
 
68
 
};
69
 
 
70
 
class MeshIterators : public CppUnit::TestFixture
71
 
{
72
 
  CPPUNIT_TEST_SUITE(MeshIterators);
73
 
  CPPUNIT_TEST(testVertexIterators);
74
 
  CPPUNIT_TEST(testEdgeIterators);
75
 
  CPPUNIT_TEST(testFaceIterators);
76
 
  CPPUNIT_TEST(testFacetIterators);
77
 
  CPPUNIT_TEST(testCellIterators);
78
 
  CPPUNIT_TEST(testMixedIterators);
79
 
  CPPUNIT_TEST_SUITE_END();
80
 
 
81
 
public:
82
 
 
83
 
  void testVertexIterators()
84
 
  {
85
 
    // Iterate over vertices
86
 
    UnitCube mesh(5, 5, 5);
87
 
    unsigned int n = 0;
88
 
    for (VertexIterator v(mesh); !v.end(); ++v)
89
 
      n++;
90
 
    CPPUNIT_ASSERT(n == mesh.numVertices());
91
 
  }
92
 
 
93
 
  void testEdgeIterators()
94
 
  {
95
 
    // Iterate over edges
96
 
    UnitCube mesh(5, 5, 5);
97
 
    unsigned int n = 0;
98
 
    for (EdgeIterator e(mesh); !e.end(); ++e)
99
 
      n++;
100
 
    CPPUNIT_ASSERT(n == mesh.numEdges());
101
 
  }
102
 
 
103
 
  void testFaceIterators()
104
 
  {
105
 
    // Iterate over faces
106
 
    UnitCube mesh(5, 5, 5);
107
 
    unsigned int n = 0;
108
 
    for (FaceIterator f(mesh); !f.end(); ++f)
109
 
      n++;
110
 
    CPPUNIT_ASSERT(n == mesh.numFaces());
111
 
  }
112
 
 
113
 
  void testFacetIterators()
114
 
  {
115
 
    // Iterate over facets
116
 
    UnitCube mesh(5, 5, 5);
117
 
    unsigned int n = 0;
118
 
    for (FacetIterator f(mesh); !f.end(); ++f)
119
 
      n++;
120
 
    CPPUNIT_ASSERT(n == mesh.numFacets());
121
 
  }
122
 
 
123
 
  void testCellIterators()
124
 
  {
125
 
    // Iterate over cells
126
 
    UnitCube mesh(5, 5, 5);
127
 
    unsigned int n = 0;
128
 
    for (CellIterator c(mesh); !c.end(); ++c)
129
 
      n++;
130
 
    CPPUNIT_ASSERT(n == mesh.numCells());
131
 
  }
132
 
        
133
 
  void testMixedIterators()
134
 
  {
135
 
    // Iterate over vertices of cells
136
 
    UnitCube mesh(5, 5, 5);
137
 
    unsigned int n = 0;
138
 
    for (CellIterator c(mesh); !c.end(); ++c)
139
 
      for (VertexIterator v(*c); !v.end(); ++v)
140
 
        n++;
141
 
    CPPUNIT_ASSERT(n == 4*mesh.numCells());
142
 
  }
143
 
 
144
 
};
145
 
 
146
 
class BoundaryExtraction : public CppUnit::TestFixture
147
 
{
148
 
  CPPUNIT_TEST_SUITE(BoundaryExtraction);
149
 
  CPPUNIT_TEST(testBoundaryComputation);
150
 
  CPPUNIT_TEST(testBoundaryBoundary);
151
 
  CPPUNIT_TEST_SUITE_END();
152
 
 
153
 
public:
154
 
  
155
 
  void testBoundaryComputation()
156
 
  {
157
 
    // Compute boundary of mesh
158
 
    UnitCube mesh(2, 2, 2);
159
 
    BoundaryMesh boundary(mesh);
160
 
    CPPUNIT_ASSERT(boundary.numVertices() == 26);
161
 
    CPPUNIT_ASSERT(boundary.numCells() == 48);
162
 
  }
163
 
 
164
 
  void testBoundaryBoundary()
165
 
  {
166
 
    // Compute boundary of boundary
167
 
    //
168
 
    // Note that we can't do
169
 
    //
170
 
    //   BoundaryMesh b0(mesh);
171
 
    //   BoundaryMesh b1(b0);
172
 
    //
173
 
    // since b1 would then be a copy of b0 (copy
174
 
    // constructor in Mesh will be used).
175
 
 
176
 
    UnitCube mesh(2, 2, 2);
177
 
    BoundaryMesh b0(mesh);
178
 
    BoundaryMesh b1;
179
 
    b1.init(b0);
180
 
    CPPUNIT_ASSERT(b1.numVertices() == 0);
181
 
    CPPUNIT_ASSERT(b1.numCells() == 0);
182
 
  }
183
 
 
184
 
};
185
 
 
186
 
class MeshFunctions : public CppUnit::TestFixture
187
 
{
188
 
  CPPUNIT_TEST_SUITE(MeshFunctions);
189
 
  CPPUNIT_TEST(testAssign);
190
 
  CPPUNIT_TEST_SUITE_END();
191
 
 
192
 
public:
193
 
  
194
 
  void testAssign()
195
 
  {
196
 
    /// Assign value of mesh function
197
 
    UnitSquare mesh(3, 3);
198
 
    MeshFunction<int> f(mesh, 0);
199
 
    f.set(3, 10);
200
 
    Vertex v(mesh, 3);
201
 
    CPPUNIT_ASSERT(f(v) == 10);
202
 
  }
203
 
  
204
 
};
205
 
      
206
 
class InputOutput : public CppUnit::TestFixture
207
 
{
208
 
  CPPUNIT_TEST_SUITE(InputOutput);
209
 
  CPPUNIT_TEST(testMeshXML2D);
210
 
  CPPUNIT_TEST(testMeshXML3D);
211
 
  CPPUNIT_TEST(testMeshMatlab2D);
212
 
  CPPUNIT_TEST(testMeshFunction);
213
 
  CPPUNIT_TEST_SUITE_END();
214
 
 
215
 
public:
216
 
 
217
 
  void testMeshXML2D()
218
 
  {
219
 
    // Write and read 2D mesh to/from file
220
 
    UnitSquare mesh_out(3, 3);
221
 
    Mesh mesh_in;
222
 
    File file("unitsquare.xml");
223
 
    file << mesh_out;
224
 
    file >> mesh_in;
225
 
    CPPUNIT_ASSERT(mesh_in.numVertices() == 16);
226
 
  }
227
 
  
228
 
  void testMeshXML3D()
229
 
  {
230
 
    // Write and read 3D mesh to/from file
231
 
    UnitCube mesh_out(3, 3, 3);
232
 
    Mesh mesh_in;
233
 
    File file("unitcube.xml");
234
 
    file << mesh_out;
235
 
    file >> mesh_in;
236
 
    CPPUNIT_ASSERT(mesh_in.numVertices() == 64);
237
 
  }
238
 
 
239
 
  void testMeshMatlab2D()
240
 
  {
241
 
    // Write matlab format (no real test)
242
 
    UnitSquare mesh(5, 5);
243
 
    File file("unitsquare.m");
244
 
    file << mesh;
245
 
    CPPUNIT_ASSERT(0 == 0);
246
 
  }
247
 
  
248
 
  void testMeshFunction()
249
 
  {
250
 
    // Write and read mesh function to/from file
251
 
    UnitSquare mesh(1, 1);
252
 
    MeshFunction<int> f(mesh, 0);
253
 
    f.set(0, 2);
254
 
    f.set(1, 4);
255
 
    f.set(2, 6);
256
 
    f.set(3, 8);
257
 
    File file("meshfunction.xml");
258
 
    file << f;
259
 
    MeshFunction<int> g(mesh, 0);
260
 
    file >> g;
261
 
    for (VertexIterator v(mesh); !v.end(); ++v)
262
 
      CPPUNIT_ASSERT(f(*v) == g(*v));
263
 
  }
264
 
 
265
 
};
266
 
 
267
 
class PyCCInterface : public CppUnit::TestFixture
268
 
{
269
 
  CPPUNIT_TEST_SUITE(PyCCInterface);
270
 
  CPPUNIT_TEST(testGetGeometricalDimension);
271
 
  CPPUNIT_TEST(testGetCoordinates);
272
 
  CPPUNIT_TEST(testGetCells);
273
 
  CPPUNIT_TEST_SUITE_END();
274
 
 
275
 
public:
276
 
 
277
 
  void testGetGeometricalDimension()
278
 
  {
279
 
    // Get geometrical dimension of mesh
280
 
    UnitSquare mesh(5, 5);
281
 
    CPPUNIT_ASSERT(mesh.geometry().dim() == 2);
282
 
  }
283
 
 
284
 
  void testGetCoordinates()
285
 
  {
286
 
    // Get coordinates of vertices
287
 
    UnitSquare mesh(5, 5);
288
 
    CPPUNIT_ASSERT(mesh.geometry().size() == 36);
289
 
  }
290
 
 
291
 
  void testGetCells()
292
 
  {
293
 
    // Get cells of mesh
294
 
    UnitSquare mesh(5, 5);
295
 
    CPPUNIT_ASSERT(mesh.topology().size(2) == 50);
296
 
  }
297
 
 
298
 
};
299
 
 
300
 
CPPUNIT_TEST_SUITE_REGISTRATION(SimpleShapes);
301
 
CPPUNIT_TEST_SUITE_REGISTRATION(MeshRefinement);
302
 
CPPUNIT_TEST_SUITE_REGISTRATION(MeshIterators);
303
 
CPPUNIT_TEST_SUITE_REGISTRATION(BoundaryExtraction);
304
 
CPPUNIT_TEST_SUITE_REGISTRATION(MeshFunctions);
305
 
CPPUNIT_TEST_SUITE_REGISTRATION(InputOutput);
306
 
CPPUNIT_TEST_SUITE_REGISTRATION(PyCCInterface);
307
 
 
308
 
int main()
309
 
{
310
 
  DOLFIN_TEST;
311
 
}