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