""" File used for looping create_buildings script to create many
    building scenarios with variations in building width """


#Convention for strings representing files
# #_file has the extention
#           #name does not have the extension

import time
from anuga.pyvolution.pmesh2domain import pmesh_to_domain_instance 
from caching import cache
from anuga.pyvolution.shallow_water import Domain, Reflective_boundary,\
File_boundary, Dirichlet_boundary, Time_boundary, Transmissive_boundary
from anuga.pyvolution.least_squares import fit_to_mesh_file, DEFAULT_ALPHA 
import project
from create_buildings import create_mesh
from anuga.pmesh.mesh import importMeshFromFile
import Numeric



#from building_generator import create_mesh


# creates buildign scenarios from widths A to B in steps of n >>
# >> list(Numeric.arange(A,B,n))
DR = list(Numeric.arange(3,7,1)) 
#DR = [4,5] #[3,5,7,9,11,13,15,17,19,21,23]
for depth in DR:
    
    meshname = project.meshname
    outputname = project.outputname
    t0 = time.time()
    meshname, triagle_count = cache(create_mesh,(1000,depth),
                                    {'mesh_file':meshname,
                                     'triangles_in_name':True}
                                    ,dependencies = ['create_buildings.py']
                                    ,evaluate = True      
                                    )
    
    print 'Initialising the mesh took %.2f seconds' %(time.time()-t0)  

    #Setup domain
    domain = cache(pmesh_to_domain_instance, (meshname, Domain),
                   dependencies = [meshname]                    
                   ,verbose = False
                   )               

    # Building scenario name with width and traingle count added.
    domain.set_name(project.basename + '_Rot(45)_5_D_%s_%d' %(str(depth), triagle_count))
    domain.set_datadir(project.outputdir)
    domain.store = True
    domain.quantities_to_be_stored = ['stage', 'xmomentum', 'ymomentum']

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

    #Setup Initial Conditions
    domain.set_quantity('friction', 0.01)
    domain.set_quantity('stage', 0)

    #Setup Boundary Conditions
    print domain.get_boundary_tags()

    domain.starttime = 0  #Obtained from MOST

    Br = Reflective_boundary(domain)
    Bt = Transmissive_boundary(domain)
    # <<<<<<<<< CHANGE WAVE DEPTH HERE >>>>>>>>>>>>	
    Bdw = Dirichlet_boundary([5,0,0])	# inundating wave height	
    Bdb = Dirichlet_boundary([0,0,0])   # rear boundary, keep at zero.
    Bw = Time_boundary(domain=domain,
                       f=lambda t: [(60<t<660)*4, 0, 0])

    domain.set_boundary( {'wall': Br,'wave': Bdw, 'back': Bdb, 'exterior':Bdw} ) 

    #Evolve
    t0 = time.time()

    for t in domain.evolve(yieldstep = 10, finaltime = 1000):
        domain.write_time()      

    print 'That took %.2f seconds' %(time.time()-t0)
