1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import unittest |
---|
5 | #from math import sqrt |
---|
6 | #from Numeric import array, allclose, minimum, maximum, Float |
---|
7 | |
---|
8 | #from pytools.stats import mean |
---|
9 | #from domain import * |
---|
10 | #from boundary import * |
---|
11 | #from python_versions import compute_gradient |
---|
12 | #from config import epsilon |
---|
13 | #from shallow_water import attributes_2_field_values |
---|
14 | |
---|
15 | |
---|
16 | from Numeric import allclose, array |
---|
17 | |
---|
18 | from shallow_water import Domain |
---|
19 | |
---|
20 | from pmesh2domain import * |
---|
21 | |
---|
22 | from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ |
---|
23 | Transmissive_boundary |
---|
24 | |
---|
25 | |
---|
26 | class Test_pmesh2domain(unittest.TestCase): |
---|
27 | |
---|
28 | def setUp(self): |
---|
29 | pass |
---|
30 | |
---|
31 | def tearDown(self): |
---|
32 | pass |
---|
33 | |
---|
34 | def test_pmesh2Domain(self): |
---|
35 | import os |
---|
36 | import tempfile |
---|
37 | |
---|
38 | fileName = tempfile.mktemp(".tsh") |
---|
39 | file = open(fileName,"w") |
---|
40 | file.write("4 3 # <vertex #> <x> <y> [attributes]\n \ |
---|
41 | 0 0.0 0.0 0.0 0.0 0.01 \n \ |
---|
42 | 1 1.0 0.0 10.0 10.0 0.02 \n \ |
---|
43 | 2 0.0 1.0 0.0 10.0 0.03 \n \ |
---|
44 | 3 0.5 0.25 8.0 12.0 0.04 \n \ |
---|
45 | # Vert att title \n \ |
---|
46 | elevation \n \ |
---|
47 | stage \n \ |
---|
48 | friction \n \ |
---|
49 | 2 # <triangle #> [<vertex #>] [<neigbouring triangle #>] \n\ |
---|
50 | 0 0 3 2 -1 -1 1 dsg\n\ |
---|
51 | 1 0 1 3 -1 0 -1 ole nielsen\n\ |
---|
52 | 4 # <segment #> <vertex #> <vertex #> [boundary tag] \n\ |
---|
53 | 0 1 0 2 \n\ |
---|
54 | 1 0 2 3 \n\ |
---|
55 | 2 2 3 \n\ |
---|
56 | 3 3 1 1 \n\ |
---|
57 | 3 0 # <x> <y> [attributes] ...Mesh Vertices... \n \ |
---|
58 | 0 216.0 -86.0 \n \ |
---|
59 | 1 160.0 -167.0 \n \ |
---|
60 | 2 114.0 -91.0 \n \ |
---|
61 | 3 # <vertex #> <vertex #> [boundary tag] ...Mesh Segments... \n \ |
---|
62 | 0 0 1 0 \n \ |
---|
63 | 1 1 2 0 \n \ |
---|
64 | 2 2 0 0 \n \ |
---|
65 | 0 # <x> <y> ...Mesh Holes... \n \ |
---|
66 | 0 # <x> <y> <attribute>...Mesh Regions... \n") |
---|
67 | file.close() |
---|
68 | |
---|
69 | tags = {} |
---|
70 | b1 = Dirichlet_boundary(conserved_quantities = array([0.0])) |
---|
71 | b2 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
72 | b3 = Dirichlet_boundary(conserved_quantities = array([2.0])) |
---|
73 | tags["1"] = b1 |
---|
74 | tags["2"] = b2 |
---|
75 | tags["3"] = b3 |
---|
76 | |
---|
77 | domain = pmesh_to_domain_instance(fileName, Domain) |
---|
78 | os.remove(fileName) |
---|
79 | |
---|
80 | ## check the quantities |
---|
81 | #print domain.quantities['elevation'].vertex_values |
---|
82 | answer = [[0., 8., 0.], |
---|
83 | [0., 10., 8.]] |
---|
84 | assert allclose(domain.quantities['elevation'].vertex_values, |
---|
85 | answer) |
---|
86 | |
---|
87 | #print domain.quantities['stage'].vertex_values |
---|
88 | answer = [[0., 12., 10.], |
---|
89 | [0., 10., 12.]] |
---|
90 | assert allclose(domain.quantities['stage'].vertex_values, |
---|
91 | answer) |
---|
92 | |
---|
93 | #print domain.quantities['friction'].vertex_values |
---|
94 | answer = [[0.01, 0.04, 0.03], |
---|
95 | [0.01, 0.02, 0.04]] |
---|
96 | assert allclose(domain.quantities['friction'].vertex_values, |
---|
97 | answer) |
---|
98 | |
---|
99 | #print domain.quantities['friction'].vertex_values |
---|
100 | assert allclose(domain.tagged_elements['dsg'][0],0) |
---|
101 | assert allclose(domain.tagged_elements['ole nielsen'][0],1) |
---|
102 | |
---|
103 | self.failUnless( domain.boundary[(1, 0)] == '1', |
---|
104 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
105 | self.failUnless( domain.boundary[(1, 2)] == '2', |
---|
106 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
107 | self.failUnless( domain.boundary[(0, 1)] == '3', |
---|
108 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
109 | self.failUnless( domain.boundary[(0, 0)] == 'exterior', |
---|
110 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
111 | #print "domain.boundary",domain.boundary |
---|
112 | self.failUnless( len(domain.boundary) == 4, |
---|
113 | "test_pmesh2Domain Too many boundaries") |
---|
114 | def old_test_tags_to_boundaries (self): |
---|
115 | meshDict = {} |
---|
116 | p0 = [0.0, 0.0] |
---|
117 | p1 = [1.0, 0.0] |
---|
118 | p2 = [0.0, 1.0] |
---|
119 | p3 = [0.646446609407, 0.353553390593] |
---|
120 | meshDict['vertices'] = [p0,p1,p2,p3] |
---|
121 | 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]] |
---|
122 | meshDict['triangles'] = [[0,3,2],[0,1,3]] |
---|
123 | meshDict['triangle_tags'] = [6.6,6.6] |
---|
124 | meshDict['triangle_neighbors'] = [[-1,-1,1],[-1,0,-1]] |
---|
125 | meshDict['segments'] = [[1,0],[0,2],[2,3],[3,1]] |
---|
126 | meshDict['segment_tags'] = [2,3,1,1] |
---|
127 | |
---|
128 | domain = Domain.pmesh_dictionary_to_domain(meshDict) |
---|
129 | |
---|
130 | #domain.set_tag_dict(tag_dict) |
---|
131 | #Boundary tests |
---|
132 | b1 = Dirichlet_boundary(conserved_quantities = array([0.0])) |
---|
133 | b2 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
134 | b3 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
135 | #test adding a boundary |
---|
136 | tags = {} |
---|
137 | tags[1] = b1 |
---|
138 | tags[2] = b2 |
---|
139 | tags[3] = b3 |
---|
140 | domain.set_boundary(tags) |
---|
141 | inverted_id = Volume.instances[0].neighbours[0] |
---|
142 | id = -(inverted_id+1) |
---|
143 | boundary_value = Boundary_value.instances[id] |
---|
144 | boundary_obj = boundary_value.boundary_object |
---|
145 | |
---|
146 | self.failUnless( boundary_obj == b1, |
---|
147 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
148 | |
---|
149 | inverted_id = Volume.instances[0].neighbours[1] |
---|
150 | id = -(inverted_id+1) |
---|
151 | boundary_value = Boundary_value.instances[id] |
---|
152 | boundary_obj = boundary_value.boundary_object |
---|
153 | self.failUnless( boundary_obj == b3, |
---|
154 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
155 | |
---|
156 | #------------------------------------------------------------- |
---|
157 | if __name__ == "__main__": |
---|
158 | suite = unittest.makeSuite(Test_pmesh2domain, 'test') |
---|
159 | runner = unittest.TextTestRunner() |
---|
160 | runner.run(suite) |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|