~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to test/unit/graph/python/test.py

  • Committer: Niclas Jansson
  • Date: 2011-06-14 07:30:24 UTC
  • Revision ID: njansson@csc.kth.se-20110614073024-ygy9rpk7jj0dxncv
Cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Unit test for the graph library"""
2
 
 
3
 
__author__ = "Gustav Magnus Vikstrom (gustavv@ifi.uio.no)"
4
 
__date__ = "2007-02-12 -- 2007-03-21"
5
 
__copyright__ = "Copyright (C) 2007 Gustav Magnus Vikstrom"
6
 
__license__  = "GNU LGPL Version 2.1"
7
 
 
8
 
import unittest
9
 
import os
10
 
import tempfile
11
 
import numpy
12
 
from dolfin import *
13
 
 
14
 
class Editor(unittest.TestCase):
15
 
 
16
 
    def testUndirected(self):
17
 
        """Create undirected graph with edges added out of order (should pass)"""
18
 
        graph = Graph()
19
 
        editor = GraphEditor()
20
 
        editor.open(graph, "undirected")
21
 
        editor.initVertices(4)
22
 
        editor.addVertex(0, 2)
23
 
        editor.addVertex(1, 3)
24
 
        editor.addVertex(2, 2)
25
 
        editor.addVertex(3, 3)
26
 
        editor.initEdges(5)
27
 
        editor.addEdge(0, 1)
28
 
        editor.addEdge(1, 2)
29
 
        editor.addEdge(2, 3)
30
 
        editor.addEdge(1, 3)
31
 
        editor.addEdge(0, 3)
32
 
        editor.close()
33
 
 
34
 
        self.assertEqual(graph.numVertices(), 4)
35
 
        self.assertEqual(graph.numEdges(), 5)
36
 
        self.assertEqual(graph.numArches(), 10)
37
 
 
38
 
    def testDirected(self):
39
 
        """Create directed graph with edges added out of order (should pass)"""
40
 
        graph = Graph()
41
 
        editor = GraphEditor()
42
 
        editor.open(graph, "directed")
43
 
        editor.initVertices(4)
44
 
        editor.addVertex(0, 2)
45
 
        editor.addVertex(1, 1)
46
 
        editor.addVertex(2, 1)
47
 
        editor.addVertex(3, 1)
48
 
        editor.initEdges(5)
49
 
        editor.addEdge(0, 1)
50
 
        editor.addEdge(1, 2)
51
 
        editor.addEdge(2, 3)
52
 
        editor.addEdge(3, 1)
53
 
        editor.addEdge(0, 3)
54
 
        editor.close()
55
 
 
56
 
        self.assertEqual(graph.numVertices(), 4)
57
 
        self.assertEqual(graph.numEdges(), 5)
58
 
        self.assertEqual(graph.numArches(), 5)
59
 
 
60
 
class InputOutput(unittest.TestCase):
61
 
 
62
 
    def testUndirectedGraphXML(self):
63
 
        """Write and read undirected graph to/from file"""
64
 
        graph = UndirectedClique(4)
65
 
 
66
 
        # Create temp file from graph, read file and delete tempfile
67
 
        fd, tmp_name = tempfile.mkstemp(suffix=".xml")
68
 
        file = File(tmp_name)
69
 
        file << graph
70
 
        graph2 = Graph()
71
 
        file >> graph2
72
 
        os.remove(tmp_name)
73
 
 
74
 
        self.assertEqual(graph.numVertices(), graph2.numVertices())
75
 
        self.assertEqual(graph.numEdges(), graph2.numEdges())
76
 
 
77
 
    def testDirectedGraphXML(self):
78
 
        """Write and read directed graph to/from file"""
79
 
        graph = DirectedClique(4)
80
 
 
81
 
        # Create temp file from graph, read file and delete tempfile
82
 
        fd, tmp_name = tempfile.mkstemp(suffix=".xml")
83
 
        file = File(tmp_name)
84
 
        file << graph
85
 
        graph2 = Graph()
86
 
        file >> graph2
87
 
        os.remove(tmp_name)
88
 
 
89
 
        graph2.disp()
90
 
        self.assertEqual(graph.numVertices(), graph2.numVertices())
91
 
        self.assertEqual(graph.numEdges(), graph2.numEdges())
92
 
 
93
 
    def testMetisGraphConvertion(self):
94
 
        """Create metis graph file, convert to dolfin xml and read xml file"""
95
 
 
96
 
        fd, tmp_name = tempfile.mkstemp(suffix=".gra")
97
 
        mfile = open(tmp_name, "w")
98
 
        mfile.write("3 3\n")
99
 
        mfile.write(" 1 2\n")
100
 
        mfile.write(" 0 2\n")
101
 
        mfile.write(" 0 1\n")
102
 
        
103
 
        mfile.close()
104
 
 
105
 
        # Create temp dolfin xml file from metis graph
106
 
        fd, tmp_name = tempfile.mkstemp(suffix=".xml")
107
 
        os.system('dolfin-convert %s %s' % (mfile.name, tmp_name))
108
 
 
109
 
        # Read dolfin xml file
110
 
        graph = Graph()
111
 
        file = File(tmp_name)
112
 
        file >> graph
113
 
 
114
 
        # Delete tempfiles
115
 
        os.remove(tmp_name)
116
 
        os.remove(mfile.name)
117
 
        
118
 
        self.assertEqual(graph.numVertices(), 3)
119
 
        self.assertEqual(graph.numEdges(), 3)
120
 
 
121
 
    def testScotchGraphConvertion(self):
122
 
        """Create scotch graph file, convert to dolfin xml and read xml file"""
123
 
        fd, tmp_name = tempfile.mkstemp(suffix=".grf")
124
 
        sfile = open(tmp_name, "w")
125
 
        sfile.write("0\n")
126
 
        sfile.write("3 6\n")
127
 
        sfile.write("1 000\n")
128
 
        sfile.write("2 1 2\n")
129
 
        sfile.write("2 0 2\n")
130
 
        sfile.write("2 0 1\n")
131
 
 
132
 
        sfile.close()
133
 
 
134
 
        # Create temp dolfin xml file from scotch graph
135
 
        fd, tmp_name = tempfile.mkstemp(suffix=".xml")
136
 
        os.system('dolfin-convert %s %s' % (sfile.name, tmp_name))
137
 
 
138
 
        # Read dolfin xml file
139
 
        graph = Graph()
140
 
        file = File(tmp_name)
141
 
        file >> graph
142
 
        
143
 
        # Delete tempfiles
144
 
        os.remove(tmp_name)
145
 
        os.remove(sfile.name)
146
 
 
147
 
        self.assertEqual(graph.numVertices(), 3)
148
 
        self.assertEqual(graph.numEdges(), 3)
149
 
 
150
 
class Partitioning(unittest.TestCase):
151
 
 
152
 
    def testPartition(self):
153
 
        """Create a graph and partition it"""
154
 
  
155
 
        """ Example graph:
156
 
                             0 -- 1 -- 2
157
 
                                  |  /
158
 
                                            | /
159
 
                                       4 -- 3 -- 6
160
 
                                                 |    |    |
161
 
                                            |           |    |
162
 
                                            7           5 -- 8
163
 
        """
164
 
 
165
 
        nn = 9
166
 
        num_part = 2
167
 
        graph = Graph()
168
 
        editor = GraphEditor()
169
 
        editor.open(graph, "undirected")
170
 
        editor.initVertices(nn)
171
 
        editor.addVertex(0, 1)
172
 
        editor.addVertex(1, 3)
173
 
        editor.addVertex(2, 2)
174
 
        editor.addVertex(3, 5)
175
 
        editor.addVertex(4, 2)
176
 
        editor.addVertex(5, 2)
177
 
        editor.addVertex(6, 2)
178
 
        editor.addVertex(7, 1)
179
 
        editor.addVertex(8, 2)
180
 
        editor.initEdges(10)
181
 
        editor.addEdge(0, 1)
182
 
        editor.addEdge(1, 2)
183
 
        editor.addEdge(1, 3)
184
 
        editor.addEdge(2, 3)
185
 
        editor.addEdge(3, 4)
186
 
        editor.addEdge(3, 5)
187
 
        editor.addEdge(3, 6)
188
 
        editor.addEdge(4, 7)
189
 
        editor.addEdge(5, 8)
190
 
        editor.addEdge(6, 8)
191
 
        editor.close()
192
 
 
193
 
        parts = numpy.array(0, 'L')
194
 
        parts.resize(nn)
195
 
 
196
 
        GraphPartition.partition(graph, num_part, parts)
197
 
        GraphPartition.eval(graph, num_part, parts)
198
 
        GraphPartition.disp(graph, num_part, parts)
199
 
        edgecut = GraphPartition.edgecut(graph, num_part, parts)
200
 
        GraphPartition.check(graph, num_part, parts)
201
 
 
202
 
if __name__ == "__main__":
203
 
    unittest.main()