[716] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | |
---|
| 4 | import unittest |
---|
| 5 | |
---|
| 6 | from Numeric import allclose, array |
---|
| 7 | |
---|
| 8 | from shallow_water import Domain |
---|
| 9 | |
---|
| 10 | from pmesh2domain import * |
---|
| 11 | |
---|
| 12 | from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ |
---|
| 13 | Transmissive_boundary |
---|
| 14 | |
---|
[1178] | 15 | from coordinate_transforms.geo_reference import Geo_reference |
---|
[716] | 16 | |
---|
[2056] | 17 | #This is making pyvolution dependent on pmesh. |
---|
| 18 | # not good. this should be in a seperate package.(directory) |
---|
| 19 | from pmesh.mesh import importMeshFromFile |
---|
[1178] | 20 | |
---|
[1018] | 21 | class Test_pmesh2domain(unittest.TestCase): |
---|
| 22 | |
---|
[716] | 23 | def setUp(self): |
---|
| 24 | pass |
---|
[1018] | 25 | |
---|
[716] | 26 | def tearDown(self): |
---|
| 27 | pass |
---|
[1018] | 28 | |
---|
[716] | 29 | def test_pmesh2Domain(self): |
---|
| 30 | import os |
---|
| 31 | import tempfile |
---|
[1018] | 32 | |
---|
[716] | 33 | fileName = tempfile.mktemp(".tsh") |
---|
| 34 | file = open(fileName,"w") |
---|
| 35 | file.write("4 3 # <vertex #> <x> <y> [attributes]\n \ |
---|
| 36 | 0 0.0 0.0 0.0 0.0 0.01 \n \ |
---|
| 37 | 1 1.0 0.0 10.0 10.0 0.02 \n \ |
---|
| 38 | 2 0.0 1.0 0.0 10.0 0.03 \n \ |
---|
| 39 | 3 0.5 0.25 8.0 12.0 0.04 \n \ |
---|
| 40 | # Vert att title \n \ |
---|
| 41 | elevation \n \ |
---|
[773] | 42 | stage \n \ |
---|
[716] | 43 | friction \n \ |
---|
| 44 | 2 # <triangle #> [<vertex #>] [<neigbouring triangle #>] \n\ |
---|
| 45 | 0 0 3 2 -1 -1 1 dsg\n\ |
---|
| 46 | 1 0 1 3 -1 0 -1 ole nielsen\n\ |
---|
[969] | 47 | 4 # <segment #> <vertex #> <vertex #> [boundary tag] \n\ |
---|
[716] | 48 | 0 1 0 2 \n\ |
---|
| 49 | 1 0 2 3 \n\ |
---|
| 50 | 2 2 3 \n\ |
---|
| 51 | 3 3 1 1 \n\ |
---|
| 52 | 3 0 # <x> <y> [attributes] ...Mesh Vertices... \n \ |
---|
| 53 | 0 216.0 -86.0 \n \ |
---|
| 54 | 1 160.0 -167.0 \n \ |
---|
| 55 | 2 114.0 -91.0 \n \ |
---|
[969] | 56 | 3 # <vertex #> <vertex #> [boundary tag] ...Mesh Segments... \n \ |
---|
[716] | 57 | 0 0 1 0 \n \ |
---|
| 58 | 1 1 2 0 \n \ |
---|
| 59 | 2 2 0 0 \n \ |
---|
| 60 | 0 # <x> <y> ...Mesh Holes... \n \ |
---|
[1178] | 61 | 0 # <x> <y> <attribute>...Mesh Regions... \n \ |
---|
[1422] | 62 | 0 # <x> <y> <attribute>...Mesh Regions, area... \n\ |
---|
| 63 | #Geo reference \n \ |
---|
[1178] | 64 | 56 \n \ |
---|
| 65 | 140 \n \ |
---|
| 66 | 120 \n") |
---|
[716] | 67 | file.close() |
---|
[1018] | 68 | |
---|
[716] | 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 |
---|
[1018] | 76 | |
---|
[716] | 77 | domain = pmesh_to_domain_instance(fileName, Domain) |
---|
| 78 | os.remove(fileName) |
---|
[1183] | 79 | #print "domain.tagged_elements", domain.tagged_elements |
---|
[1018] | 80 | ## check the quantities |
---|
[716] | 81 | #print domain.quantities['elevation'].vertex_values |
---|
| 82 | answer = [[0., 8., 0.], |
---|
| 83 | [0., 10., 8.]] |
---|
| 84 | assert allclose(domain.quantities['elevation'].vertex_values, |
---|
[1018] | 85 | answer) |
---|
| 86 | |
---|
[773] | 87 | #print domain.quantities['stage'].vertex_values |
---|
[716] | 88 | answer = [[0., 12., 10.], |
---|
| 89 | [0., 10., 12.]] |
---|
[773] | 90 | assert allclose(domain.quantities['stage'].vertex_values, |
---|
[716] | 91 | answer) |
---|
[1018] | 92 | |
---|
[716] | 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) |
---|
[1018] | 102 | |
---|
[716] | 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") |
---|
[1178] | 114 | #FIXME change to use get_xllcorner |
---|
[1422] | 115 | #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner |
---|
[1178] | 116 | self.failUnless(domain.geo_reference.xllcorner == 140.0, |
---|
| 117 | "bad geo_referece") |
---|
[2056] | 118 | #************ |
---|
| 119 | |
---|
| 120 | def test_pmesh2Domain_instance(self): |
---|
| 121 | import os |
---|
| 122 | import tempfile |
---|
| 123 | |
---|
| 124 | fileName = tempfile.mktemp(".tsh") |
---|
| 125 | file = open(fileName,"w") |
---|
| 126 | file.write("4 3 # <vertex #> <x> <y> [attributes]\n \ |
---|
| 127 | 0 0.0 0.0 0.0 0.0 0.01 \n \ |
---|
| 128 | 1 1.0 0.0 10.0 10.0 0.02 \n \ |
---|
| 129 | 2 0.0 1.0 0.0 10.0 0.03 \n \ |
---|
| 130 | 3 0.5 0.25 8.0 12.0 0.04 \n \ |
---|
| 131 | # Vert att title \n \ |
---|
| 132 | elevation \n \ |
---|
| 133 | stage \n \ |
---|
| 134 | friction \n \ |
---|
| 135 | 2 # <triangle #> [<vertex #>] [<neigbouring triangle #>] \n\ |
---|
| 136 | 0 0 3 2 -1 -1 1 dsg\n\ |
---|
| 137 | 1 0 1 3 -1 0 -1 ole nielsen\n\ |
---|
| 138 | 4 # <segment #> <vertex #> <vertex #> [boundary tag] \n\ |
---|
| 139 | 0 1 0 2 \n\ |
---|
| 140 | 1 0 2 3 \n\ |
---|
| 141 | 2 2 3 \n\ |
---|
| 142 | 3 3 1 1 \n\ |
---|
| 143 | 3 0 # <x> <y> [attributes] ...Mesh Vertices... \n \ |
---|
| 144 | 0 216.0 -86.0 \n \ |
---|
| 145 | 1 160.0 -167.0 \n \ |
---|
| 146 | 2 114.0 -91.0 \n \ |
---|
| 147 | 3 # <vertex #> <vertex #> [boundary tag] ...Mesh Segments... \n \ |
---|
| 148 | 0 0 1 0 \n \ |
---|
| 149 | 1 1 2 0 \n \ |
---|
| 150 | 2 2 0 0 \n \ |
---|
| 151 | 0 # <x> <y> ...Mesh Holes... \n \ |
---|
| 152 | 0 # <x> <y> <attribute>...Mesh Regions... \n \ |
---|
| 153 | 0 # <x> <y> <attribute>...Mesh Regions, area... \n\ |
---|
| 154 | #Geo reference \n \ |
---|
| 155 | 56 \n \ |
---|
| 156 | 140 \n \ |
---|
| 157 | 120 \n") |
---|
| 158 | file.close() |
---|
| 159 | |
---|
| 160 | mesh_instance = importMeshFromFile(fileName) |
---|
| 161 | |
---|
| 162 | tags = {} |
---|
| 163 | b1 = Dirichlet_boundary(conserved_quantities = array([0.0])) |
---|
| 164 | b2 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
| 165 | b3 = Dirichlet_boundary(conserved_quantities = array([2.0])) |
---|
| 166 | tags["1"] = b1 |
---|
| 167 | tags["2"] = b2 |
---|
| 168 | tags["3"] = b3 |
---|
| 169 | |
---|
| 170 | domain = pmesh_instance_to_domain_instance(mesh_instance, Domain) |
---|
[2533] | 171 | |
---|
[2056] | 172 | os.remove(fileName) |
---|
| 173 | #print "domain.tagged_elements", domain.tagged_elements |
---|
| 174 | ## check the quantities |
---|
| 175 | #print domain.quantities['elevation'].vertex_values |
---|
| 176 | answer = [[0., 8., 0.], |
---|
| 177 | [0., 10., 8.]] |
---|
| 178 | assert allclose(domain.quantities['elevation'].vertex_values, |
---|
| 179 | answer) |
---|
| 180 | |
---|
| 181 | #print domain.quantities['stage'].vertex_values |
---|
| 182 | answer = [[0., 12., 10.], |
---|
| 183 | [0., 10., 12.]] |
---|
| 184 | assert allclose(domain.quantities['stage'].vertex_values, |
---|
| 185 | answer) |
---|
| 186 | |
---|
| 187 | #print domain.quantities['friction'].vertex_values |
---|
| 188 | answer = [[0.01, 0.04, 0.03], |
---|
| 189 | [0.01, 0.02, 0.04]] |
---|
| 190 | assert allclose(domain.quantities['friction'].vertex_values, |
---|
| 191 | answer) |
---|
| 192 | |
---|
| 193 | #print domain.quantities['friction'].vertex_values |
---|
| 194 | assert allclose(domain.tagged_elements['dsg'][0],0) |
---|
| 195 | assert allclose(domain.tagged_elements['ole nielsen'][0],1) |
---|
| 196 | |
---|
| 197 | self.failUnless( domain.boundary[(1, 0)] == '1', |
---|
| 198 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
| 199 | self.failUnless( domain.boundary[(1, 2)] == '2', |
---|
| 200 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
| 201 | self.failUnless( domain.boundary[(0, 1)] == '3', |
---|
| 202 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
| 203 | self.failUnless( domain.boundary[(0, 0)] == 'exterior', |
---|
| 204 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
| 205 | #print "domain.boundary",domain.boundary |
---|
| 206 | self.failUnless( len(domain.boundary) == 4, |
---|
| 207 | "test_pmesh2Domain Too many boundaries") |
---|
| 208 | #FIXME change to use get_xllcorner |
---|
| 209 | #print "d.geo_reference.xllcorner",domain.geo_reference.xllcorner |
---|
| 210 | self.failUnless(domain.geo_reference.xllcorner == 140.0, |
---|
| 211 | "bad geo_referece") |
---|
[1178] | 212 | |
---|
[2056] | 213 | #*********** |
---|
[716] | 214 | def old_test_tags_to_boundaries (self): |
---|
| 215 | meshDict = {} |
---|
| 216 | p0 = [0.0, 0.0] |
---|
| 217 | p1 = [1.0, 0.0] |
---|
| 218 | p2 = [0.0, 1.0] |
---|
| 219 | p3 = [0.646446609407, 0.353553390593] |
---|
[968] | 220 | meshDict['vertices'] = [p0,p1,p2,p3] |
---|
| 221 | 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]] |
---|
| 222 | meshDict['triangles'] = [[0,3,2],[0,1,3]] |
---|
[969] | 223 | meshDict['triangle_tags'] = [6.6,6.6] |
---|
[968] | 224 | meshDict['triangle_neighbors'] = [[-1,-1,1],[-1,0,-1]] |
---|
[1018] | 225 | meshDict['segments'] = [[1,0],[0,2],[2,3],[3,1]] |
---|
[969] | 226 | meshDict['segment_tags'] = [2,3,1,1] |
---|
[1018] | 227 | |
---|
[716] | 228 | domain = Domain.pmesh_dictionary_to_domain(meshDict) |
---|
| 229 | |
---|
[969] | 230 | #domain.set_tag_dict(tag_dict) |
---|
[716] | 231 | #Boundary tests |
---|
| 232 | b1 = Dirichlet_boundary(conserved_quantities = array([0.0])) |
---|
| 233 | b2 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
| 234 | b3 = Dirichlet_boundary(conserved_quantities = array([1.0])) |
---|
| 235 | #test adding a boundary |
---|
| 236 | tags = {} |
---|
| 237 | tags[1] = b1 |
---|
| 238 | tags[2] = b2 |
---|
| 239 | tags[3] = b3 |
---|
| 240 | domain.set_boundary(tags) |
---|
| 241 | inverted_id = Volume.instances[0].neighbours[0] |
---|
| 242 | id = -(inverted_id+1) |
---|
| 243 | boundary_value = Boundary_value.instances[id] |
---|
| 244 | boundary_obj = boundary_value.boundary_object |
---|
[1018] | 245 | |
---|
[716] | 246 | self.failUnless( boundary_obj == b1, |
---|
| 247 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
[1018] | 248 | |
---|
[716] | 249 | inverted_id = Volume.instances[0].neighbours[1] |
---|
| 250 | id = -(inverted_id+1) |
---|
| 251 | boundary_value = Boundary_value.instances[id] |
---|
[1018] | 252 | boundary_obj = boundary_value.boundary_object |
---|
[716] | 253 | self.failUnless( boundary_obj == b3, |
---|
| 254 | "test_tags_to_boundaries failed. Single boundary wasn't added.") |
---|
| 255 | |
---|
| 256 | #------------------------------------------------------------- |
---|
| 257 | if __name__ == "__main__": |
---|
[1018] | 258 | suite = unittest.makeSuite(Test_pmesh2domain, 'test') |
---|
[716] | 259 | runner = unittest.TextTestRunner() |
---|
| 260 | runner.run(suite) |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | |
---|