"""Simple water flow example using ANUGA

Water driven up a linear slope and time varying boundary,
similar to a beach environment
"""

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

import sys
from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
from anuga.shallow_water import Domain
from anuga.shallow_water import Reflective_boundary
from anuga.shallow_water import Dirichlet_boundary
from anuga.shallow_water import Time_boundary
from anuga.shallow_water import Transmissive_boundary
from anuga.shallow_water import Transmissive_Momentum_Set_Stage_boundary
from anuga.geospatial_data.geospatial_data import *
from math import cos

#------------------------------------------------------------------------------
# Setup computational domain
#------------------------------------------------------------------------------
dx = 10.
dy = dx
L = 100000.
W = 3000.

# structured mesh
#points, vertices, boundary = rectangular_cross(int(L/dx), int(W/dy),
#                                               L, W, (0.0, -W/2)) # Basic mesh
#points, vertices, boundary = rectangular_cross(666, 3, 100000, 3000, (0.0, -0.0)) # Basic mesh
#points, vertices, boundary = rectangular_cross(530, 10, 5300, 100, (-5000.0, -50.0)) # Basic mesh
#points, vertices, boundary = rectangular_cross(1000, 100, 20, 3) # Basic mesh
#domain = Domain(points, vertices, boundary) 

# unstructured mesh
poly_domain = [[0,-W],[0,W],[L,W],[L,-W]]
meshname = 'test.msh'
from anuga.pmesh.mesh_interface import create_mesh_from_regions
# Create mesh
create_mesh_from_regions(poly_domain,
                         boundary_tags={'left': [0], 'top': [1],
                                        'right': [2], 'bottom': [3]},
                         maximum_triangle_area = 100000,
                         filename=meshname)

# Create domain
domain = Domain(meshname, use_cache=True, verbose = True)
domain.set_name('myexample2')                
domain.set_default_order(2) # Use second order spatial scheme
domain.set_datadir('.')                     # Use current directory for output

#------------------------------------------------------------------------------
# Setup initial conditions
#------------------------------------------------------------------------------
#domain.set_quantity('elevation', topography) # Use function for elevation
domain.set_quantity('elevation', -100)
domain.set_quantity('friction', 0.00)
domain.set_quantity('stage', 0.0)            

#-----------------------------------------------------------------------------
# Setup boundary conditions
#------------------------------------------------------------------------------
from math import sin, pi, exp
Br = Reflective_boundary(domain)      # Solid reflective wall
Bt = Transmissive_boundary(domain)    # Continue all values on boundary 
Bd = Dirichlet_boundary([1,0.,0.]) # Constant boundary values
amplitude = 1
Bw = Time_boundary(domain=domain,     # Time dependent boundary  
## Sine wave
                   f=lambda t: [(-amplitude*sin((1./300.)*t*2*pi)), 0.0, 0.0])
## Sawtooth?
#                   f=lambda t: [(-8.0*(sin((1./180.)*t*2*pi))+(1./2.)*sin((2./180.)*t*2*pi)+(1./3.)*sin((3./180.)*t*2*pi)), 0.0, 0.0])
## Sharp rise, linear fall
#                   f=lambda t: [(5.0*(-((t-0.)/300.)*(t<300.)-cos((t-300.)*2.*pi*(1./240.))*(t>=300. and t<420.)+(1.-(t-420.)/300.)*(t>=420. and t <720.))), 0.0, 0.0])
#                   f=lambda t: [amplitude*(1.-2.*(pi*(1./720.)*(t-720.))**2)/exp((pi*(1./720.)*(t-720.))**2) , 0.0, 0.0])
#                   f=lambda t: [(-8.0*sin((1./720.)*t*2*pi))*((t<720.)-0.5*(t<360.)), 0.0, 0.0])

# Associate boundary tags with boundary objects
domain.set_boundary({'left': Bw, 'right': Bt, 'top': Br, 'bottom': Br})

#------------------------------------------------------------------------------
# Evolve system through time
#------------------------------------------------------------------------------

for t in domain.evolve(yieldstep = 20.0, finaltime = 40*60.):
    domain.write_time()
    
