source: trunk/anuga_core/source/anuga/abstract_2d_finite_volumes/test_pmesh2domain.py @ 8050

Last change on this file since 8050 was 7737, checked in by hudson, 14 years ago

Various refactorings, all unit tests pass.
Domain renamed to generic domain.

File size: 9.5 KB
Line 
1#!/usr/bin/env python
2#
3
4import unittest
5
6from pmesh2domain import *
7
8from anuga.shallow_water.shallow_water_domain import Domain
9from anuga.abstract_2d_finite_volumes.generic_boundary_conditions \
10                        import Dirichlet_boundary
11
12from anuga.coordinate_transforms.geo_reference import Geo_reference
13from anuga.pmesh.mesh import importMeshFromFile
14
15import numpy as num
16
17
18class Test_pmesh2domain(unittest.TestCase):
19
20    def setUp(self):
21        pass
22
23    def tearDown(self):
24        pass
25
26    def test_pmesh2Domain(self):
27         import os
28         import tempfile
29
30         fileName = tempfile.mktemp(".tsh")
31         file = open(fileName,"w")
32         file.write("4 3 # <vertex #> <x> <y> [attributes]\n \
330 0.0 0.0 0.0 0.0 0.01 \n \
341 1.0 0.0 10.0 10.0 0.02  \n \
352 0.0 1.0 0.0 10.0 0.03  \n \
363 0.5 0.25 8.0 12.0 0.04  \n \
37# Vert att title  \n \
38elevation  \n \
39stage  \n \
40friction  \n \
412 # <triangle #> [<vertex #>] [<neigbouring triangle #>]  \n\
420 0 3 2 -1  -1  1 dsg\n\
431 0 1 3 -1  0 -1   ole nielsen\n\
444 # <segment #> <vertex #>  <vertex #> [boundary tag] \n\
450 1 0 2 \n\
461 0 2 3 \n\
472 2 3 \n\
483 3 1 1 \n\
493 0 # <x> <y> [attributes] ...Mesh Vertices... \n \
500 216.0 -86.0 \n \
511 160.0 -167.0 \n \
522 114.0 -91.0 \n \
533 # <vertex #>  <vertex #> [boundary tag] ...Mesh Segments... \n \
540 0 1 0 \n \
551 1 2 0 \n \
562 2 0 0 \n \
570 # <x> <y> ...Mesh Holes... \n \
580 # <x> <y> <attribute>...Mesh Regions... \n \
590 # <x> <y> <attribute>...Mesh Regions, area... \n\
60#Geo reference \n \
6156 \n \
62140 \n \
63120 \n")
64         file.close()
65
66         tags = {}
67         b1 =  Dirichlet_boundary(conserved_quantities = num.array([0.0]))
68         b2 =  Dirichlet_boundary(conserved_quantities = num.array([1.0]))
69         b3 =  Dirichlet_boundary(conserved_quantities = num.array([2.0]))
70         tags["1"] = b1
71         tags["2"] = b2
72         tags["3"] = b3
73
74         domain = pmesh_to_domain_instance(fileName, Domain)
75         os.remove(fileName)
76         #print "domain.tagged_elements", domain.tagged_elements
77         ## check the quantities
78         #print domain.quantities['elevation'].vertex_values
79         answer = [[0., 8., 0.],
80                   [0., 10., 8.]]
81         assert num.allclose(domain.quantities['elevation'].vertex_values,
82                             answer)
83
84         #print domain.quantities['stage'].vertex_values
85         answer = [[0., 12., 10.],
86                   [0., 10., 12.]]
87         assert num.allclose(domain.quantities['stage'].vertex_values,
88                             answer)
89
90         #print domain.quantities['friction'].vertex_values
91         answer = [[0.01, 0.04, 0.03],
92                   [0.01, 0.02, 0.04]]
93         assert num.allclose(domain.quantities['friction'].vertex_values,
94                             answer)
95
96         #print domain.quantities['friction'].vertex_values
97         tagged_elements = domain.get_tagged_elements()
98         assert num.allclose(tagged_elements['dsg'][0],0)
99         assert num.allclose(tagged_elements['ole nielsen'][0],1)
100
101         self.failUnless( domain.boundary[(1, 0)]  == '1',
102                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
103         self.failUnless( domain.boundary[(1, 2)]  == '2',
104                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
105         self.failUnless( domain.boundary[(0, 1)]  == '3',
106                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
107         self.failUnless( domain.boundary[(0, 0)]  == 'exterior',
108                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
109         #print "domain.boundary",domain.boundary
110         self.failUnless( len(domain.boundary)  == 4,
111                          "test_pmesh2Domain Too many boundaries")
112         #FIXME change to use get_xllcorner
113         #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner
114         self.failUnless(domain.geo_reference.xllcorner  == 140.0,
115                          "bad geo_referece")
116    #************
117   
118    def test_pmesh2Domain_instance(self):
119         import os
120         import tempfile
121
122         fileName = tempfile.mktemp(".tsh")
123         file = open(fileName,"w")
124         file.write("4 3 # <vertex #> <x> <y> [attributes]\n \
1250 0.0 0.0 0.0 0.0 0.01 \n \
1261 1.0 0.0 10.0 10.0 0.02  \n \
1272 0.0 1.0 0.0 10.0 0.03  \n \
1283 0.5 0.25 8.0 12.0 0.04  \n \
129# Vert att title  \n \
130elevation  \n \
131stage  \n \
132friction  \n \
1332 # <triangle #> [<vertex #>] [<neigbouring triangle #>]  \n\
1340 0 3 2 -1  -1  1 dsg\n\
1351 0 1 3 -1  0 -1   ole nielsen\n\
1364 # <segment #> <vertex #>  <vertex #> [boundary tag] \n\
1370 1 0 2 \n\
1381 0 2 3 \n\
1392 2 3 \n\
1403 3 1 1 \n\
1413 0 # <x> <y> [attributes] ...Mesh Vertices... \n \
1420 216.0 -86.0 \n \
1431 160.0 -167.0 \n \
1442 114.0 -91.0 \n \
1453 # <vertex #>  <vertex #> [boundary tag] ...Mesh Segments... \n \
1460 0 1 0 \n \
1471 1 2 0 \n \
1482 2 0 0 \n \
1490 # <x> <y> ...Mesh Holes... \n \
1500 # <x> <y> <attribute>...Mesh Regions... \n \
1510 # <x> <y> <attribute>...Mesh Regions, area... \n\
152#Geo reference \n \
15356 \n \
154140 \n \
155120 \n")
156         file.close()
157
158         mesh_instance = importMeshFromFile(fileName)
159       
160         tags = {}
161         b1 =  Dirichlet_boundary(conserved_quantities = num.array([0.0]))
162         b2 =  Dirichlet_boundary(conserved_quantities = num.array([1.0]))
163         b3 =  Dirichlet_boundary(conserved_quantities = num.array([2.0]))
164         tags["1"] = b1
165         tags["2"] = b2
166         tags["3"] = b3
167
168         domain = pmesh_to_domain_instance(mesh_instance, Domain)
169
170         os.remove(fileName)
171         #print "domain.tagged_elements", domain.tagged_elements
172         ## check the quantities
173         #print domain.quantities['elevation'].vertex_values
174         answer = [[0., 8., 0.],
175                   [0., 10., 8.]]
176         assert num.allclose(domain.quantities['elevation'].vertex_values,
177                             answer)
178
179         #print domain.quantities['stage'].vertex_values
180         answer = [[0., 12., 10.],
181                   [0., 10., 12.]]
182         assert num.allclose(domain.quantities['stage'].vertex_values,
183                             answer)
184
185         #print domain.quantities['friction'].vertex_values
186         answer = [[0.01, 0.04, 0.03],
187                   [0.01, 0.02, 0.04]]
188         assert num.allclose(domain.quantities['friction'].vertex_values,
189                             answer)
190
191         #print domain.quantities['friction'].vertex_values
192         tagged_elements = domain.get_tagged_elements()         
193         assert num.allclose(tagged_elements['dsg'][0],0)
194         assert num.allclose(tagged_elements['ole nielsen'][0],1)
195
196         self.failUnless( domain.boundary[(1, 0)]  == '1',
197                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
198         self.failUnless( domain.boundary[(1, 2)]  == '2',
199                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
200         self.failUnless( domain.boundary[(0, 1)]  == '3',
201                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
202         self.failUnless( domain.boundary[(0, 0)]  == 'exterior',
203                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
204         #print "domain.boundary",domain.boundary
205         self.failUnless( len(domain.boundary)  == 4,
206                          "test_pmesh2Domain Too many boundaries")
207         #FIXME change to use get_xllcorner
208         #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner
209         self.failUnless(domain.geo_reference.xllcorner  == 140.0,
210                          "bad geo_referece")
211         
212    #***********
213    def old_test_tags_to_boundaries (self):
214         meshDict = {}
215         p0 = [0.0, 0.0]
216         p1 = [1.0, 0.0]
217         p2 = [0.0, 1.0]
218         p3 = [0.646446609407, 0.353553390593]
219         meshDict['vertices'] = [p0,p1,p2,p3]
220         meshDict['vertex_attributes'] = [[0.0, 0.0,7.0],[10.0, 0.0,7.0],[0.0, 10.0,7.0],[6.46446609407, 3.53553390593,7.0]]
221         meshDict['triangles'] = [[0,3,2],[0,1,3]]
222         meshDict['triangle_tags'] = [6.6,6.6]
223         meshDict['triangle_neighbors'] = [[-1,-1,1],[-1,0,-1]]
224         meshDict['segments'] = [[1,0],[0,2],[2,3],[3,1]]
225         meshDict['segment_tags'] = [2,3,1,1]
226
227         domain = Domain.pmesh_dictionary_to_domain(meshDict)
228
229         #domain.set_tag_dict(tag_dict)
230         #Boundary tests
231         b1 =  Dirichlet_boundary(conserved_quantities = num.array([0.0]))
232         b2 =  Dirichlet_boundary(conserved_quantities = num.array([1.0]))
233         b3 =  Dirichlet_boundary(conserved_quantities = num.array([1.0]))
234         #test adding a boundary
235         tags = {}
236         tags[1] = b1
237         tags[2] = b2
238         tags[3] = b3
239         domain.set_boundary(tags)
240         inverted_id = Volume.instances[0].neighbours[0]
241         id = -(inverted_id+1)
242         boundary_value = Boundary_value.instances[id]
243         boundary_obj = boundary_value.boundary_object
244
245         self.failUnless( boundary_obj  == b1,
246                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
247
248         inverted_id = Volume.instances[0].neighbours[1]
249         id = -(inverted_id+1)
250         boundary_value = Boundary_value.instances[id]
251         boundary_obj = boundary_value.boundary_object
252         self.failUnless( boundary_obj  == b3,
253                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
254
255#-------------------------------------------------------------
256
257if __name__ == "__main__":
258    suite = unittest.makeSuite(Test_pmesh2domain, 'test')
259    runner = unittest.TextTestRunner()
260    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.