"""
This experiment was based heavily on the cairns demo given in the user manual and found in
the anuga repository.

However, it has been simplified so that it doesnt require all the files the cairns demo does
"""

#------------------------------------------------------------------------------
# Import necessary modules
#------------------------------------------------------------------------------
import os
import time
import sys
import anuga
from anuga_parallel import distribute, myid, numprocs
from anuga.abstract_2d_finite_volumes.util import add_directories
from anuga.utilities import log

#set up variables for the correct I/O directories for data storage
home = os.getenv('INUNDATIONHOME')
scenariodirV = add_directories(home, ["data","mem_time_test", "parallel",
                                       "template", "template-" + str(numprocs) +"-"+ str(myid)])
h = 'CAIRNS.msh'
file_pathh = os.path.join(scenariodirV, h)
log.log_filename = os.path.join(scenariodirV, "anuga.log")
log._setup = False

log.timingInfo(msg=('numberofcpus,'+str(numprocs))) #write the variable to be measured to file
log.timingInfo(msg=('myid,'+str(myid))) #write the variable to be measured to file

log.timingInfo(msg=('beforetime,'+str(log.TimeStamp()))) #get the time at the beginning of the simulation

log.resource_usage_timing(prefix = 'beforesimulation')#get memory statistics here
#------------------------------------------------------------------------------
#Create the domain and mesh for the resource experiment on only one processor
#------------------------------------------------------------------------------

if myid == 0:
    domain = anuga.create_domain_from_regions([(0.0,0.0),(10000.0,10000.0),(0.0,10000.0),(10000.0,0.0)],
                                    boundary_tags={'top': [0],
                                                   'right': [1],
                                                   'bottom': [2],
                                                   'left': [3]},
                                    maximum_triangle_area=100.0,
                                    mesh_filename=file_pathh)
else:
    domain = None

#parallel   
domain = distribute(domain)
domain.set_name('CAIRNS') # Name of sww file
domain.set_datadir(scenariodirV)# Store sww output here

log.resource_usage_timing(prefix = 'aftermesh')#get memory statistics here
log.timingInfo(msg=('aftermeshtime,'+str(log.TimeStamp()))) #get the time at the beginning of the simulation

#------------------------------------------------------------------------------
# Setup initial conditions
#------------------------------------------------------------------------------

def topography(x,y):
    return 0.0

tide = 100.0
friction = 0.0
domain.set_quantity('stage', tide)
domain.set_quantity('friction', friction) 
domain.set_quantity('elevation',topography,alpha=0.1)

log.resource_usage_timing(prefix='afterinitialconditions') #get memory statistics here

#------------------------------------------------------------------------------
# Setup boundary conditions
#------------------------------------------------------------------------------

Bi = anuga.Dirichlet_boundary([tide, 223.52, 0]) # inflow
Bo = anuga.Dirichlet_boundary([-tide, 223.52, 0]) # inflow
Br = anuga.Reflective_boundary(domain)
domain.set_boundary({'right': Bo,'bottom': Br,'left': Bi,'top': Br})

log.resource_usage_timing(prefix='afterboundary') #get memory statistics here
#------------------------------------------------------------------------------
# Evolve system through time
#------------------------------------------------------------------------------
for t in domain.evolve(yieldstep=2000, finaltime=2000): 
    print domain.timestepping_statistics()

log.resource_usage_timing(prefix='aftersimulation')#get memory statistics here
log.timingInfo(msg=('aftertime,'+str(log.TimeStamp()))) #get the time at the end of the simulation

