source: inundation/ga/storm_surge/pyvolution/pmesh2domain.py @ 1452

Last change on this file since 1452 was 1423, checked in by duncan, 20 years ago

I've removed the function mesh_file_to_mesh_dictionary. Please use the function import_mesh_file instead.

File size: 5.7 KB
RevLine 
[374]1"""Class pmesh2domain - Converting .tsh files to doamains
[371]2
3
4   Copyright 2004
5   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
6   Geoscience Australia
7"""
8
9
[415]10def pmesh_to_domain_instance(fileName, DomainClass, setting_function = None):
[371]11    """
12    """
13    import sys
[389]14    from domain import Domain
[371]15
[1178]16    vertex_coordinates, volumes, tag_dict, vertex_quantity_dict \
17                        ,tagged_elements_dict, geo_reference = \
18           pmesh_to_domain(fileName,
19                           setting_function=setting_function)
[371]20       
[1178]21    assert issubclass(DomainClass, Domain),"DomainClass is not a subclass of Domain."
[636]22
23
24   
[969]25    domain = DomainClass(vertex_coordinates, volumes, tag_dict,
[1178]26                         tagged_elements = tagged_elements_dict,
27                         geo_reference = geo_reference )
[371]28
[636]29
30   
31    #FIXME (Ole): Is this really the right place to apply the a default
32    #value specific to the shallow water wave equation?
33    #The 'assert' above indicates that any subclass of Domain is acceptable.
34    #Suggestion - module shallow_water.py will eventually take care of this
35    #(when I get around to it) so it should be removed from here. 
36   
[905]37    # This doesn't work on the domain instance.
38    # This is still needed so -ve elevations don't cuase 'lakes'
[1178]39    # The fixme we discussed was to only create a quantity when its values
40    #are set.
[905]41    # I think that's the way to go still
42   
[773]43    # set the water stage to be the elevation
44    if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('stage'):
45        vertex_quantity_dict['stage'] = vertex_quantity_dict['elevation']
[905]46       
[389]47    domain.set_quantity_vertices_dict(vertex_quantity_dict)
48    #print "vertex_quantity_dict",vertex_quantity_dict
49    return domain
[371]50
51
[389]52
53def pmesh_to_domain(fileName, setting_function = None):
[371]54    """
55    convert a pmesh dictionary to a list of Volumes.
56    Also, return a list of triangles which have boundary tags
[389]57    mesh_dict structure;
[371]58    generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) 
59    generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...]
60    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
[969]61    generated segment tag list: [S1Tag, S2Tag, ...] (list of ints)
[371]62    triangle list:  [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers)
63    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
[415]64    triangle attribute list: [T1att, T2att, ...] (list of strings)
[371]65    """
[389]66   
67    from Numeric import transpose
[1423]68    from load_mesh.loadASCII import import_mesh_file
[389]69   
[1423]70    mesh_dict = import_mesh_file(fileName)
[389]71    #print "mesh_dict",mesh_dict
[968]72    vertex_coordinates = mesh_dict['vertices']
73    volumes = mesh_dict['triangles']
[389]74    vertex_quantity_dict = {}
[968]75    point_atts = transpose(mesh_dict['vertex_attributes'])
76    point_titles  = mesh_dict['vertex_attribute_titles']
[1178]77    geo_reference  = mesh_dict['geo_reference']
[389]78    for quantity, value_vector in map (None, point_titles, point_atts):
79        vertex_quantity_dict[quantity] = value_vector
[969]80    tag_dict = pmesh_dict_to_tag_dict(mesh_dict)
[415]81    tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict)
[1178]82    return vertex_coordinates, volumes, tag_dict, vertex_quantity_dict,tagged_elements_dict, geo_reference
[371]83
84
85
[415]86def build_tagged_elements_dictionary(mesh_dict):
87    """Build the dictionary of element tags.
88    tagged_elements is a dictionary of element arrays,
89    keyed by tag:
90    { (tag): [e1, e2, e3..] }
91    """
[969]92    tri_atts = mesh_dict['triangle_tags']
[415]93    #print "tri_atts", tri_atts
94    tagged_elements = {}
95    for tri_att_index in range(len(tri_atts)):
[1183]96        tagged_elements.setdefault(tri_atts[tri_att_index],[]).append(tri_att_index)
[415]97    #print "DSG pm2do tagged_elements", tagged_elements
98    return tagged_elements
[371]99
100#FIXME: The issue is whether this format should be stored in the tsh file
101#instead of having to be created here?
102
[905]103#This information is pyvolution focused, not mesh generation focused.
104#This is an appropriate place for the info to be created. -DSG
105
[371]106#FIXME: Another issue is that the tsh file stores consecutive
107#indices explicitly. This is really redundant.
108#Suggest looking at obj and our own sww format and also consider
109#using netCDF.
110
[905]111# It is redundant.  It was the format originally decided on.
112# I'm happy for it to change. -DSG
113
[969]114def pmesh_dict_to_tag_dict(mesh_dict):
[389]115    """ Convert the pmesh dictionary (mesh_dict) description of boundary tags
[969]116    to a dictionary of tags, indexed with volume id and face number.
[371]117    """
[968]118    triangles = mesh_dict['triangles']
[371]119    sides = calc_sides(triangles)
[969]120    tag_dict = {}
121    for seg, tag in map(None,mesh_dict['segments'],
122                           mesh_dict['segment_tags']):
[371]123        v1 = seg[0]
124        v2 = seg[1]
125        for key in [(v1,v2),(v2,v1)]:
[969]126            if sides.has_key(key) and tag <> "":
[686]127                #"" represents null.  Don't put these into the dictionary
[969]128                #this creates a dict of lists of faces, indexed by tag
129                #tagged_edges.setdefault(tag,[]).append(sides[key])
130                tag_dict[sides[key]] = tag
[686]131
[969]132    return tag_dict
[371]133
134   
135def calc_sides(triangles):
136    #Build dictionary mapping from sides (2-tuple of points)
137    #to left hand side neighbouring triangle       
138    sides = {}
139    for id, triangle in enumerate(triangles):
140        a = triangle[0]
141        b = triangle[1]
142        c = triangle[2]       
143        sides[a,b] = (id, 2) #(id, face)
144        sides[b,c] = (id, 0) #(id, face)
145        sides[c,a] = (id, 1) #(id, face)
146    return sides       
147
Note: See TracBrowser for help on using the repository browser.