source: anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_pmesh2domain.py @ 5899

Last change on this file since 5899 was 5899, checked in by rwilson, 15 years ago

Initial NumPy? changes (again!).

File size: 9.7 KB
Line 
1## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py
2
3#!/usr/bin/env python
4#
5
6import unittest
7
8#from numpy.oldnumeric import allclose, array
9from numpy import allclose, array
10
11
12#from anuga.pyvolution.pmesh2domain import *
13from pmesh2domain import *
14
15from anuga.shallow_water import Domain,\
16     Reflective_boundary, Dirichlet_boundary,\
17     Transmissive_boundary
18
19from anuga.coordinate_transforms.geo_reference import Geo_reference
20from anuga.pmesh.mesh import importMeshFromFile
21
22class Test_pmesh2domain(unittest.TestCase):
23
24    def setUp(self):
25        pass
26
27    def tearDown(self):
28        pass
29
30    def test_pmesh2Domain(self):
31         import os
32         import tempfile
33
34         fileName = tempfile.mktemp(".tsh")
35         file = open(fileName,"w")
36         file.write("4 3 # <vertex #> <x> <y> [attributes]\n \
370 0.0 0.0 0.0 0.0 0.01 \n \
381 1.0 0.0 10.0 10.0 0.02  \n \
392 0.0 1.0 0.0 10.0 0.03  \n \
403 0.5 0.25 8.0 12.0 0.04  \n \
41# Vert att title  \n \
42elevation  \n \
43stage  \n \
44friction  \n \
452 # <triangle #> [<vertex #>] [<neigbouring triangle #>]  \n\
460 0 3 2 -1  -1  1 dsg\n\
471 0 1 3 -1  0 -1   ole nielsen\n\
484 # <segment #> <vertex #>  <vertex #> [boundary tag] \n\
490 1 0 2 \n\
501 0 2 3 \n\
512 2 3 \n\
523 3 1 1 \n\
533 0 # <x> <y> [attributes] ...Mesh Vertices... \n \
540 216.0 -86.0 \n \
551 160.0 -167.0 \n \
562 114.0 -91.0 \n \
573 # <vertex #>  <vertex #> [boundary tag] ...Mesh Segments... \n \
580 0 1 0 \n \
591 1 2 0 \n \
602 2 0 0 \n \
610 # <x> <y> ...Mesh Holes... \n \
620 # <x> <y> <attribute>...Mesh Regions... \n \
630 # <x> <y> <attribute>...Mesh Regions, area... \n\
64#Geo reference \n \
6556 \n \
66140 \n \
67120 \n")
68         file.close()
69
70         tags = {}
71         b1 =  Dirichlet_boundary(conserved_quantities = array([0.0]))
72         b2 =  Dirichlet_boundary(conserved_quantities = array([1.0]))
73         b3 =  Dirichlet_boundary(conserved_quantities = array([2.0]))
74         tags["1"] = b1
75         tags["2"] = b2
76         tags["3"] = b3
77
78         domain = pmesh_to_domain_instance(fileName, Domain)
79         os.remove(fileName)
80         #print "domain.tagged_elements", domain.tagged_elements
81         ## check the quantities
82         #print domain.quantities['elevation'].vertex_values
83         answer = [[0., 8., 0.],
84                   [0., 10., 8.]]
85         assert allclose(domain.quantities['elevation'].vertex_values,
86                        answer)
87
88         #print domain.quantities['stage'].vertex_values
89         answer = [[0., 12., 10.],
90                   [0., 10., 12.]]
91         assert allclose(domain.quantities['stage'].vertex_values,
92                        answer)
93
94         #print domain.quantities['friction'].vertex_values
95         answer = [[0.01, 0.04, 0.03],
96                   [0.01, 0.02, 0.04]]
97         assert allclose(domain.quantities['friction'].vertex_values,
98                        answer)
99
100         #print domain.quantities['friction'].vertex_values
101         assert allclose(domain.tagged_elements['dsg'][0],0)
102         assert allclose(domain.tagged_elements['ole nielsen'][0],1)
103
104         self.failUnless( domain.boundary[(1, 0)]  == '1',
105                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
106         self.failUnless( domain.boundary[(1, 2)]  == '2',
107                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
108         self.failUnless( domain.boundary[(0, 1)]  == '3',
109                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
110         self.failUnless( domain.boundary[(0, 0)]  == 'exterior',
111                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
112         #print "domain.boundary",domain.boundary
113         self.failUnless( len(domain.boundary)  == 4,
114                          "test_pmesh2Domain Too many boundaries")
115         #FIXME change to use get_xllcorner
116         #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner
117         self.failUnless(domain.geo_reference.xllcorner  == 140.0,
118                          "bad geo_referece")
119    #************
120   
121    def test_pmesh2Domain_instance(self):
122         import os
123         import tempfile
124
125         fileName = tempfile.mktemp(".tsh")
126         file = open(fileName,"w")
127         file.write("4 3 # <vertex #> <x> <y> [attributes]\n \
1280 0.0 0.0 0.0 0.0 0.01 \n \
1291 1.0 0.0 10.0 10.0 0.02  \n \
1302 0.0 1.0 0.0 10.0 0.03  \n \
1313 0.5 0.25 8.0 12.0 0.04  \n \
132# Vert att title  \n \
133elevation  \n \
134stage  \n \
135friction  \n \
1362 # <triangle #> [<vertex #>] [<neigbouring triangle #>]  \n\
1370 0 3 2 -1  -1  1 dsg\n\
1381 0 1 3 -1  0 -1   ole nielsen\n\
1394 # <segment #> <vertex #>  <vertex #> [boundary tag] \n\
1400 1 0 2 \n\
1411 0 2 3 \n\
1422 2 3 \n\
1433 3 1 1 \n\
1443 0 # <x> <y> [attributes] ...Mesh Vertices... \n \
1450 216.0 -86.0 \n \
1461 160.0 -167.0 \n \
1472 114.0 -91.0 \n \
1483 # <vertex #>  <vertex #> [boundary tag] ...Mesh Segments... \n \
1490 0 1 0 \n \
1501 1 2 0 \n \
1512 2 0 0 \n \
1520 # <x> <y> ...Mesh Holes... \n \
1530 # <x> <y> <attribute>...Mesh Regions... \n \
1540 # <x> <y> <attribute>...Mesh Regions, area... \n\
155#Geo reference \n \
15656 \n \
157140 \n \
158120 \n")
159         file.close()
160
161         mesh_instance = importMeshFromFile(fileName)
162       
163         tags = {}
164         b1 =  Dirichlet_boundary(conserved_quantities = array([0.0]))
165         b2 =  Dirichlet_boundary(conserved_quantities = array([1.0]))
166         b3 =  Dirichlet_boundary(conserved_quantities = array([2.0]))
167         tags["1"] = b1
168         tags["2"] = b2
169         tags["3"] = b3
170
171         domain = pmesh_instance_to_domain_instance(mesh_instance, Domain)
172
173         os.remove(fileName)
174         #print "domain.tagged_elements", domain.tagged_elements
175         ## check the quantities
176         #print domain.quantities['elevation'].vertex_values
177         answer = [[0., 8., 0.],
178                   [0., 10., 8.]]
179         assert allclose(domain.quantities['elevation'].vertex_values,
180                        answer)
181
182         #print domain.quantities['stage'].vertex_values
183         answer = [[0., 12., 10.],
184                   [0., 10., 12.]]
185         assert allclose(domain.quantities['stage'].vertex_values,
186                        answer)
187
188         #print domain.quantities['friction'].vertex_values
189         answer = [[0.01, 0.04, 0.03],
190                   [0.01, 0.02, 0.04]]
191         assert allclose(domain.quantities['friction'].vertex_values,
192                        answer)
193
194         #print domain.quantities['friction'].vertex_values
195         assert allclose(domain.tagged_elements['dsg'][0],0)
196         assert allclose(domain.tagged_elements['ole nielsen'][0],1)
197
198         self.failUnless( domain.boundary[(1, 0)]  == '1',
199                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
200         self.failUnless( domain.boundary[(1, 2)]  == '2',
201                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
202         self.failUnless( domain.boundary[(0, 1)]  == '3',
203                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
204         self.failUnless( domain.boundary[(0, 0)]  == 'exterior',
205                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
206         #print "domain.boundary",domain.boundary
207         self.failUnless( len(domain.boundary)  == 4,
208                          "test_pmesh2Domain Too many boundaries")
209         #FIXME change to use get_xllcorner
210         #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner
211         self.failUnless(domain.geo_reference.xllcorner  == 140.0,
212                          "bad geo_referece")
213         
214    #***********
215    def old_test_tags_to_boundaries (self):
216         meshDict = {}
217         p0 = [0.0, 0.0]
218         p1 = [1.0, 0.0]
219         p2 = [0.0, 1.0]
220         p3 = [0.646446609407, 0.353553390593]
221         meshDict['vertices'] = [p0,p1,p2,p3]
222         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]]
223         meshDict['triangles'] = [[0,3,2],[0,1,3]]
224         meshDict['triangle_tags'] = [6.6,6.6]
225         meshDict['triangle_neighbors'] = [[-1,-1,1],[-1,0,-1]]
226         meshDict['segments'] = [[1,0],[0,2],[2,3],[3,1]]
227         meshDict['segment_tags'] = [2,3,1,1]
228
229         domain = Domain.pmesh_dictionary_to_domain(meshDict)
230
231         #domain.set_tag_dict(tag_dict)
232         #Boundary tests
233         b1 =  Dirichlet_boundary(conserved_quantities = array([0.0]))
234         b2 =  Dirichlet_boundary(conserved_quantities = array([1.0]))
235         b3 =  Dirichlet_boundary(conserved_quantities = array([1.0]))
236         #test adding a boundary
237         tags = {}
238         tags[1] = b1
239         tags[2] = b2
240         tags[3] = b3
241         domain.set_boundary(tags)
242         inverted_id = Volume.instances[0].neighbours[0]
243         id = -(inverted_id+1)
244         boundary_value = Boundary_value.instances[id]
245         boundary_obj = boundary_value.boundary_object
246
247         self.failUnless( boundary_obj  == b1,
248                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
249
250         inverted_id = Volume.instances[0].neighbours[1]
251         id = -(inverted_id+1)
252         boundary_value = Boundary_value.instances[id]
253         boundary_obj = boundary_value.boundary_object
254         self.failUnless( boundary_obj  == b3,
255                          "test_tags_to_boundaries  failed. Single boundary wasn't added.")
256
257#-------------------------------------------------------------
258if __name__ == "__main__":
259    suite = unittest.makeSuite(Test_pmesh2domain, 'test')
260    runner = unittest.TextTestRunner()
261    runner.run(suite)
262
263
264
265
Note: See TracBrowser for help on using the repository browser.