"""Script for running a dam break simulation of UQ's dam break tank,
focusing on a friction comparison.

The simulation will
form part of the ANUGA validation paper.

Ole Nielsen and Duncan Gray, GA - 2006 

"""


#----------------------------------------------------------------------------
# Import necessary modules
#----------------------------------------------------------------------------

# Standard modules
import time
from time import localtime, strftime
import sys
from shutil import copy
from os import path, sep
from os.path import dirname  #, basename

# Related major packages
from anuga.shallow_water import Domain, Reflective_boundary, \
                            Dirichlet_boundary, Time_boundary, File_boundary
from anuga.abstract_2d_finite_volumes.region import Set_region 
from anuga.fit_interpolate.interpolate import interpolate_sww2csv
from anuga.shallow_water.data_manager import start_screen_catcher, \
     copy_code_files


# Scenario specific imports
import project                 # Definition of file names and polygons
import create_mesh


def main(friction, is_trial_run=False):
    if is_trial_run is True:
        add = '_test'
        yieldstep = 1
        finaltime = 31
    else:
        add = ''
        yieldstep = 0.01
        finaltime = 31
    basename = 'zz' + add + str(friction)
    
    mesh_filename = project.mesh_filename + add + '.msh'

    #--------------------------------------------------------------------------
    # Copy scripts to output directory and capture screen
    # output to file
    #--------------------------------------------------------------------------

    
    if is_trial_run is False:
        start_screen_catcher('.', int(friction*100))

    #-------------------------------------------------------------------------
    # Create the triangular mesh
    #-------------------------------------------------------------------------
    
    gate_position = 0.85
    create_mesh.generate(mesh_filename,
                         gate_position,
                         is_coarse=is_trial_run) # this creates the mesh

    #-------------------------------------------------------------------------
    # Setup computational domain
    #-------------------------------------------------------------------------
    domain = Domain(mesh_filename, use_cache = False, verbose = True)
   

    print 'Number of triangles = ', len(domain)
    print 'The extent is ', domain.get_extent()
    print domain.statistics()

    
    domain.set_name(basename)
    domain.set_datadir('.')
    domain.set_quantities_to_be_stored(['stage', 'xmomentum', 'ymomentum'])
    domain.set_minimum_storable_height(0.01)
    #domain.set_store_vertices_uniquely(True)  # for writting to sww

    #-------------------------------------------------------------------------
    # Setup initial conditions
    #-------------------------------------------------------------------------

    slope = 0.05

    def elevation_tilt(x, y):
        return x*slope
        
    domain.set_quantity('stage', elevation_tilt)
    domain.set_quantity('friction', friction) 
    domain.set_quantity('elevation',elevation_tilt)

    print 'Available boundary tags', domain.get_boundary_tags()
    domain.set_region('dam','stage',0.20,
                                 location = 'unique vertices') 

    Br = Reflective_boundary(domain)
    Bd = Dirichlet_boundary([0,0,0])  # to drain the water out.
    domain.set_boundary( {'wall': Br, 'edge': Bd} )

    #-------------------------------------------------------------------------
    # Evolve system through time
    #-------------------------------------------------------------------------
    t0 = time.time()

    for t in domain.evolve(yieldstep, finaltime):
        domain.write_time()
    
        print 'That took %.2f seconds' %(time.time()-t0)
        print 'finished'

    points = [[gate_position - 0.65,0.2],
              [gate_position - 0.55,0.2],
              [gate_position - 0.45,0.2],
              [gate_position - 0.35,0.2],
              [gate_position - 0.25,0.2]
              ]

    #-------------------------------------------------------------------------
    # Calculate gauge info
    #-------------------------------------------------------------------------

    interpolate_sww2csv(basename +".sww",
                        points,
                        basename + "_depth.csv",
                        basename + "_velocity_x.csv",
                        basename + "_velocity_y.csv")
  

#-------------------------------------------------------------
if __name__ == "__main__":
    for friction in [ 0.0, 0.01]:
        main(friction, is_trial_run = False)
    
