"""Create mesh for University of Queensland dam break flume
"""


from anuga.pmesh.mesh import *
from anuga.coordinate_transforms.geo_reference import Geo_reference


def generate(mesh_filename, gate_position, is_coarse = False):
    """
    Generate mesh for University of Queensland dam break flume.
    The gate position is the distance from the right wall of the wave tank
    to the gate. 
    
    """
    #Basic geometry
    
    xright  = gate_position 
    ybottom = 0
    ytop    = 0.4
    xdam = 0.0
    xleft = gate_position  - 3.0

    #Outline 
    point_sw = [xleft, ybottom]
    point_se = [xright, ybottom]
    point_nw = [xleft, ytop]    
    point_ne = [xright, ytop]

    # Dam seperation (left)
    point_dam_top = [xdam, ytop]
    point_dam_bottom = [xdam, ybottom]    

    m = Mesh()

    #Boundary
    points = [point_sw,   #se
              point_nw,
              point_dam_top,
              point_ne, 
              point_se, 
              point_dam_bottom
              ]
    
    segments = [[0,1], [1,2], [2,3],
                        [3,4 ],[4,5], [5,0],  #The outer border
                        [2,5]]         #dam Separator
    
    segment_tags = {'wall':[0,1,2,4,5],'edge':[3]} # '':[6]
        
    m.add_points_and_segments(points, segments, segment_tags)
    
    dam = m.add_region(-0.0000001,(ytop - ybottom)/2)
    # this is the location of the region.
    dam.setTag("dam")
    
    if is_coarse:
        m.generate_mesh(maximum_triangle_area=0.05)
    else:
        m.generate_mesh(maximum_triangle_area=0.0001)

    m.export_mesh_file(mesh_filename)
    print "mesh created"

#-------------------------------------------------------------
if __name__ == "__main__":
    generate("aa.tsh", 0.75, is_course = True)
