def test_rate_operator_negative_rate_full(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) # Flat surface with 1m of water domain.set_quantity("elevation", 0) domain.set_quantity("stage", 10.0) domain.set_quantity("friction", 0) Br = Reflective_boundary(domain) domain.set_boundary({"exterior": Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] # Catchment_Rain_Polygon = read_polygon(join('CatchmentBdy.csv')) # rainfall = file_function(join('1y120m.tms'), quantities=['rainfall']) rate = -1.0 factor = 10.0 default_rate = 0.0 operator = Rate_operator(domain, rate=rate, factor=factor, indices=None, default_rate=default_rate) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [0.0, 0.0, 0.0, 0.0] step_integral = -80.0 # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # print domain.fractional_step_volume_integral assert num.allclose(domain.quantities["stage"].centroid_values, stage_ex) assert num.allclose(domain.quantities["xmomentum"].centroid_values, 0.0) assert num.allclose(domain.quantities["ymomentum"].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, step_integral)
def test_set_w_uh_vh_operator_time(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('xmomentum', 7.0) domain.set_quantity('ymomentum', 8.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['w_uh_vh'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] w_uh_vh = lambda t: [t, t + 1, t + 2] operator = Set_w_uh_vh_operator(domain, w_uh_vh=w_uh_vh, indices=indices) # Apply Operator domain.timestep = 2.0 domain.time = 1.0 operator() t = domain.time stage_ex = [t, t, 1., t] xmom_ex = [t + 1, t + 1, 7., t + 1] ymom_ex = [t + 2, t + 2, 8., t + 2] #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, xmom_ex) assert num.allclose(domain.quantities['ymomentum'].centroid_values, ymom_ex)
def test_set_w_uh_vh_operator_time(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('xmomentum', 7.0) domain.set_quantity('ymomentum', 8.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['w_uh_vh'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] w_uh_vh = lambda t : [t, t+1, t+2] operator = Set_w_uh_vh_operator(domain, w_uh_vh=w_uh_vh, indices=indices) # Apply Operator domain.timestep = 2.0 domain.time = 1.0 operator() t = domain.time stage_ex = [ t, t, 1., t] xmom_ex = [ t+1, t+1, 7., t+1] ymom_ex = [ t+2, t+2, 8., t+2] #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, xmom_ex) assert num.allclose(domain.quantities['ymomentum'].centroid_values, ymom_ex)
def test_slide_tsunami_domain(self): if anuga_installed: pass else: print 'Note: test_slide_tsunami_domain not tested as ANUGA '\ 'is not installed' return length = 600.0 dep = 150.0 th = 9.0 thk = 15.0 wid = 340.0 kappa = 3.0 kappad = 0.8 x0 = 100000. y0 = x0 from anuga.pmesh.mesh_interface import create_mesh_from_regions polygon = [[0,0],[200000,0],[200000,200000],[0,200000]] create_mesh_from_regions(polygon, {'e0': [0], 'e1': [1], 'e2': [2], 'e3': [3]}, maximum_triangle_area=5000000000, filename='test.msh', verbose = False) domain = Domain('test.msh', use_cache = True, verbose = False) slide = slide_tsunami(length, dep, th, x0, y0, \ wid, thk, kappa, kappad, \ domain=domain,verbose=False) domain.set_quantity('stage', slide) stage = domain.get_quantity('stage') w = stage.get_values() ## check = [[-0.0 -0.0 -0.0], ## [-.189709745 -517.877716 -0.0], ## [-0.0 -0.0 -2.7695931e-08], ## [-0.0 -2.7695931e-08 -1.897097e-01] ## [-0.0 -517.877716 -0.0], ## [-0.0 -0.0 -0.0], ## [-0.0 -0.0 -0.0], ## [-0.0 -0.0 -0.0]] assert num.allclose(num.min(w), -517.877771593) assert num.allclose(num.max(w), 0.0) assert num.allclose(slide.a3D, 518.38797486)
def test_slide_tsunami_domain(self): length = 600.0 dep = 150.0 th = 9.0 thk = 15.0 wid = 340.0 kappa = 3.0 kappad = 0.8 x0 = 100000. y0 = x0 from anuga.pmesh.mesh_interface import create_mesh_from_regions polygon = [[0, 0], [200000, 0], [200000, 200000], [0, 200000]] create_mesh_from_regions(polygon, { 'e0': [0], 'e1': [1], 'e2': [2], 'e3': [3] }, maximum_triangle_area=5000000000, filename='test.msh', verbose=False) domain = Domain('test.msh', use_cache=True, verbose=False) slide = slide_tsunami(length, dep, th, x0, y0, \ wid, thk, kappa, kappad, \ domain=domain,verbose=False) domain.set_quantity('stage', slide) stage = domain.get_quantity('stage') w = stage.get_values() ## check = [[-0.0 -0.0 -0.0], ## [-.189709745 -517.877716 -0.0], ## [-0.0 -0.0 -2.7695931e-08], ## [-0.0 -2.7695931e-08 -1.897097e-01] ## [-0.0 -517.877716 -0.0], ## [-0.0 -0.0 -0.0], ## [-0.0 -0.0 -0.0], ## [-0.0 -0.0 -0.0]] assert num.allclose(num.min(w), -517.877771593) assert num.allclose(num.max(w), 0.0) assert num.allclose(slide.a3D, 518.38797486)
def test_rate_operator_simple(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] rate = 1.0 factor = 10.0 default_rate = 0.0 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [21., 21., 1., 21.] # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose( domain.fractional_step_volume_integral, factor * domain.timestep * (rate * domain.areas[indices]).sum())
def test_set_stage_operator_negative(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', lambda x,y : -2*x) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] #Catchment_Rain_Polygon = read_polygon(join('CatchmentBdy.csv')) #rainfall = file_function(join('1y120m.tms'), quantities=['rainfall']) stage = -5.0 operator = Set_stage_operator(domain, stage=stage, indices=indices) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [ -5., -5., 1., -5.] #print domain.quantities['elevation'].centroid_values #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_set_stage_operator_negative(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', lambda x, y: -2 * x) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] #Catchment_Rain_Polygon = read_polygon(join('CatchmentBdy.csv')) #rainfall = file_function(join('1y120m.tms'), quantities=['rainfall']) stage = -5.0 operator = Set_stage_operator(domain, stage=stage, indices=indices) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [-5., -5., 1., -5.] #print domain.quantities['elevation'].centroid_values #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_set_quantity_simple(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] stage = 3.0 update_stage = Set_quantity(domain, "stage", value=stage, indices=indices, test_stage=False) # Apply Operator update_stage() stage_ex = [3., 3., 1., 3.] #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_rate_operator_simple(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] rate = 1.0 factor = 10.0 default_rate= 0.0 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [ 21., 21., 1., 21.] # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, factor*domain.timestep*(rate*domain.areas[indices]).sum())
def test_set_stage_operator_simple(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] stage = 3.0 operator = Set_stage_operator(domain, stage=stage, indices=indices) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [ 3., 3., 1., 3.] #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_rate_operator_functions_rate_default_rate(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] factor = 10.0 def main_rate(t): if t > 20: msg = 'Model time exceeded.' raise Modeltime_too_late, msg else: return 3.0 * t + 7.0 default_rate = lambda t: 3 * t + 7 operator = Rate_operator(domain, rate=main_rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() d = operator.get_timestep() * main_rate(t) * factor + 1 stage_ex = [d, d, 1., d] if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum()) domain.set_starttime(30.0) domain.timestep = 1.0 operator() t = operator.get_time() d = operator.get_timestep() * default_rate(t) * factor + d stage_ex = [d, d, 1., d] if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_rate_operator_negative_rate_full(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 10.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] #Catchment_Rain_Polygon = read_polygon(join('CatchmentBdy.csv')) #rainfall = file_function(join('1y120m.tms'), quantities=['rainfall']) rate = -1.0 factor = 10.0 default_rate = 0.0 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=None, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [0., 0., 0., 0.] step_integral = -80.0 #print domain.quantities['elevation'].centroid_values #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values #print domain.fractional_step_volume_integral assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, step_integral)
def setup_domain(simulation): args = simulation.args verbose = args.verbose alg = args.alg N = args.N S = args.S E = args.E W = args.W from catchment_info import create_catchment_list from catchment_info import create_manning_list CatchmentList = create_catchment_list(simulation) ManningList = create_manning_list(simulation) #--------------------------------------------------------------------------- # CREATING MESH #--------------------------------------------------------------------------- bounding_polygon = [[W, S], [E, S], [E, N], [W, N]] interior_regions = read_polygon_list(CatchmentList) # FIXME: Have these in a shapefile / other file and read them in breaklines = [[[306612.336559443, 6193708.75358065], [306604.441364239, 6193693.17994946]], [[306977.886673843, 6193753.44134088], [306978.027867398, 6193710.94208076]], [[306956.001672788, 6193750.89985688], [306956.707640564, 6193706.14149989]], [[306627.303076293, 6193697.45809624], [306620.525785644, 6193683.62112783]], [[307236.83565407, 6193741.01630802], [307231.682089306, 6193721.03741996]], [[307224.975395434, 6193742.71063068], [307220.880782334, 6193723.36711362]], [[307624.764946969, 6193615.98941489], [307617.98765632, 6193601.44647871]], [[307613.328268998, 6193623.19028621], [307607.751123568, 6193610.97704368]]] # Make the mesh create_mesh_from_regions(bounding_polygon, boundary_tags={ 'south': [0], 'east': [1], 'north': [2], 'west': [3] }, maximum_triangle_area=args.maximum_triangle_area, interior_regions=interior_regions, filename=args.meshname, breaklines=breaklines, use_cache=False, verbose=True) #--------------------------------------------------------------------------- # SETUP COMPUTATIONAL DOMAIN #--------------------------------------------------------------------------- domain = Domain(args.meshname, use_cache=False, verbose=True) domain.set_flow_algorithm(alg) if (not domain.get_using_discontinuous_elevation()): raise Exception, 'This model run relies on a discontinuous elevation solver (because of how topography is set up)' domain.set_datadir(args.model_output_dir) domain.set_name(args.outname) print domain.statistics() #------------------------------------------------------------------------------ # APPLY MANNING'S ROUGHNESSES #------------------------------------------------------------------------------ if verbose: print 'Calculating complicated polygon friction function' friction_list = read_polygon_list(ManningList) domain.set_quantity( 'friction', Polygon_function(friction_list, default=args.base_friction, geo_reference=domain.geo_reference)) # Set a Initial Water Level over the Domain domain.set_quantity('stage', 0) if verbose: print 'Setting up elevation interpolation function' from anuga.utilities.quantity_setting_functions import make_nearestNeighbour_quantity_function if verbose: print 'READING %s' % args.basename + '.csv' elev_xyz = numpy.genfromtxt(fname=args.basename + '.csv', delimiter=',') # Use nearest-neighbour interpolation of elevation if verbose: print 'CREATING nearest neighbour interpolator' elev_fun_wrapper = make_nearestNeighbour_quantity_function( elev_xyz, domain) if verbose: print 'Applying elevation interpolation function' domain.set_quantity('elevation', elev_fun_wrapper, location='centroids') return domain
domain.set_flow_algorithm(alg) domain.set_store_centroids(True) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def topography(x, y): return x*slope # linear bed slope def init_stage(x,y): stg= x*slope+0.01 # Constant depth: 1 cm. return stg domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', mannings) # Constant friction domain.set_quantity('stage', init_stage) else: domain = None #=============================================================================== # Parallel Domain #=============================================================================== domain = distribute(domain) #domain.set_store_centroids(True) #------------------------------------------------------------------------------ # Setup boundary conditions
def test_rate_operator_rate_from_file(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] #--------------------------------- #Typical ASCII file #--------------------------------- finaltime = 1200 filename = 'test_file_function' fid = open(filename + '.txt', 'w') start = time.mktime(time.strptime('2000', '%Y')) dt = 60 #One minute intervals t = 0.0 while t <= finaltime: t_string = time.strftime(time_format, time.gmtime(t + start)) fid.write('%s, %f %f %f\n' % (t_string, 2 * t, t**2, sin(old_div(t * pi, 600)))) t += dt fid.close() #Convert ASCII file to NetCDF (Which is what we really like!) timefile2netcdf(filename + '.txt') #Create file function from time series F = file_function( filename + '.tms', quantities=['Attribute0', 'Attribute1', 'Attribute2']) #Now try interpolation for i in range(20): t = i * 10 q = F(t) #Exact linear intpolation assert num.allclose(q[0], 2 * t) if i % 6 == 0: assert num.allclose(q[1], t**2) assert num.allclose(q[2], sin(old_div(t * pi, 600))) #Check non-exact t = 90 #Halfway between 60 and 120 q = F(t) assert num.allclose(old_div((120**2 + 60**2), 2), q[1]) assert num.allclose( old_div((sin(old_div(120 * pi, 600)) + sin(old_div(60 * pi, 600))), 2), q[2]) t = 100 #Two thirds of the way between between 60 and 120 q = F(t) assert num.allclose(old_div(2 * 120**2, 3) + old_div(60**2, 3), q[1]) assert num.allclose( old_div(2 * sin(old_div(120 * pi, 600)), 3) + old_div(sin(old_div(60 * pi, 600)), 3), q[2]) #os.remove(filename + '.txt') #os.remove(filename + '.tms') domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] rate = file_function(filename + '.tms', quantities=['Attribute1']) # Make starttime of domain consistent with tms file starttime domain.set_starttime(rate.starttime) factor = 1000.0 default_rate = 17.7 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.set_time(360.0) domain.timestep = 1.0 operator() d = domain.get_time()**2 * factor + 1.0 stage_ex0 = [d, d, 1., d] # print d, domain.get_time(), F(360.0) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex0) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum()) domain.set_time(1300.0) domain.timestep = 1.0 operator() d = default_rate * factor + d stage_ex1 = [d, d, 1., d] # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex1) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum()) tmp = numpy.zeros_like(domain.quantities['stage'].centroid_values) tmp[:] = domain.quantities['stage'].centroid_values d0 = domain.fractional_step_volume_integral domain.set_time(-10.0) domain.timestep = 1.0 operator() d = default_rate * factor stage_ex2 = numpy.array([d, d, 0., d]) + numpy.array(stage_ex1) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex2) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, d0 + (d * domain.areas[indices]).sum()) # test timestepping_statistics stats = operator.timestepping_statistics() import re rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", stats) assert num.allclose(float(rr[1]), 17.7) assert num.allclose(float(rr[2]), 106200.0)
def setup_domain(simulation): args = simulation.args verbose = args.verbose alg = args.alg N = args.N S = args.S E = args.E W = args.W from catchment_info import create_catchment_list from catchment_info import create_manning_list CatchmentList = create_catchment_list(simulation) ManningList = create_manning_list(simulation) #------------------------------------------------------------------------------ # CREATING MESH #------------------------------------------------------------------------------ bounding_polygon = [[W, S], [E, S], [E, N], [W, N]] #interior_regions = read_polygon_dir(CatchmentDictionary, join('Model', 'Bdy')) interior_regions = read_polygon_list(CatchmentList) # FIXME: Have these in a shapefile / other file and read them in breaklines=[[[306612.336559443,6193708.75358065], [306604.441364239,6193693.17994946]], [[306977.886673843,6193753.44134088], [306978.027867398,6193710.94208076]], [[306956.001672788,6193750.89985688], [306956.707640564,6193706.14149989]], [[306627.303076293,6193697.45809624], [306620.525785644,6193683.62112783]], [[307236.83565407,6193741.01630802], [307231.682089306,6193721.03741996]], [[307224.975395434,6193742.71063068], [307220.880782334,6193723.36711362]], [[307624.764946969,6193615.98941489], [307617.98765632,6193601.44647871]], [[307613.328268998,6193623.19028621], [307607.751123568,6193610.97704368]]] # Make the mesh create_mesh_from_regions(bounding_polygon, boundary_tags={'south': [0], 'east': [1], 'north': [2], 'west': [3]}, maximum_triangle_area=args.maximum_triangle_area, interior_regions=interior_regions, filename=args.meshname, breaklines=breaklines, use_cache=False, verbose=True) #------------------------------------------------------------------------------ # SETUP COMPUTATIONAL DOMAIN #------------------------------------------------------------------------------ domain = Domain(args.meshname, use_cache=False, verbose=True) domain.set_flow_algorithm(alg) if(not domain.get_using_discontinuous_elevation()): raise Exception, 'This model run relies on a discontinuous elevation solver (because of how topography is set up)' domain.set_datadir(args.model_output_dir) domain.set_name(args.outname) print domain.statistics() #------------------------------------------------------------------------------ # APPLY MANNING'S ROUGHNESSES #------------------------------------------------------------------------------ if verbose: print 'Calculating complicated polygon friction function' friction_list = read_polygon_list(ManningList) domain.set_quantity('friction', Polygon_function(friction_list, default=args.base_friction, geo_reference=domain.geo_reference)) # Set a Initial Water Level over the Domain domain.set_quantity('stage', 0) # Decompress the zip file to make a csv for reading zipfile.ZipFile('DEM_bridges/towradgi_cleaner.zip').extract('towradgi.csv',path='DEM_bridges/') if verbose: print 'Setting up elevation interpolation function' from anuga.utilities.quantity_setting_functions import make_nearestNeighbour_quantity_function elev_xyz=numpy.genfromtxt(fname=args.basename+'.csv',delimiter=',') # Use nearest-neighbour interpolation of elevation elev_fun_wrapper=make_nearestNeighbour_quantity_function(elev_xyz,domain) if verbose: print 'Applying elevation interpolation function' domain.set_quantity('elevation', elev_fun_wrapper, location='centroids') os.remove('DEM_bridges/towradgi.csv') # Clean up csv file return domain
if myid == 0: # structured mesh points, vertices, boundary = anuga.rectangular_cross(int(L/dx), int(W/dy), L, W, (0.0, 0.0)) #domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation',0.0) domain.set_quantity('friction', 0.0) domain.set_quantity('stage', 1.5) domain.set_quantity('xmomentum', 0.0) domain.set_quantity('ymomentum', 0.0) else: domain = None #--------------------------- # Create Parallel Domain #--------------------------- domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions
verbose=True) #------------------------------------------------------------------------------ # SETUP COMPUTATIONAL DOMAIN #------------------------------------------------------------------------------ domain = Domain(meshname, use_cache=False, verbose=True) domain.set_minimum_storable_height(0.0001) domain.set_name(outname) print(domain.statistics()) #------------------------------------------------------------------------------ # APPLY MANNING'S ROUGHNESSES #------------------------------------------------------------------------------ domain.set_quantity('friction', 0.035) domain.set_quantity('elevation', filename=basename + '.csv', use_cache=False, verbose=True, alpha=0.1) #------------------------------------------------------------------------------ # SETUP BOUNDARY CONDITIONS #------------------------------------------------------------------------------ print('Available boundary tags', domain.get_boundary_tags()) Br = anuga.Reflective_boundary(domain) Bd = anuga.Dirichlet_boundary([0, 0, 0])
domain.set_flow_algorithm(alg) domain.set_store_centroids(True) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def topography(x, y): return x * slope # linear bed slope def init_stage(x, y): stg = x * slope + 0.01 # Constant depth: 1 cm. return stg domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', mannings) # Constant friction domain.set_quantity('stage', init_stage) else: domain = None #=============================================================================== # Parallel Domain #=============================================================================== domain = distribute(domain) #domain.set_store_centroids(True) #------------------------------------------------------------------------------ # Setup boundary conditions
# domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) # ------------------------------------------------------------------------------ # Setup Algorithm, either using command line arguments # or override manually yourself # ------------------------------------------------------------------------------ domain.set_flow_algorithm(alg) # ------------------------------------------------------------------------------ # Setup initial conditions # ------------------------------------------------------------------------------ domain.set_quantity("friction", 0.0) domain.set_quantity("stage", 12.0) XX = array( [ 0.0, 50.0, 100.0, 150.0, 250.0, 300.0, 350.0, 400.0, 425.0, 435.0, 450.0, 470.0,
# structured mesh points, vertices, boundary = anuga.rectangular_cross( int(L / dx), int(W / dy), L, W, (0.0, 0.0)) #domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation', 0.0) domain.set_quantity('friction', 0.0) domain.set_quantity('stage', 1.5) domain.set_quantity('xmomentum', 0.0) domain.set_quantity('ymomentum', 0.0) else: domain = None #--------------------------- # Create Parallel Domain #--------------------------- domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions
def start_sim(run_id, Runs, scenario_name, Scenario, session, **kwargs): yieldstep = kwargs['yieldstep'] finaltime = kwargs['finaltime'] logger = logging.getLogger(run_id) max_triangle_area = kwargs['max_triangle_area'] logger.info('Starting hydrata_project') if run_id == 'local_run': base_dir = os.getcwd() else: base_dir = os.getcwd() + '/base_dir/%s/' % run_id outname = run_id meshname = base_dir + 'outputs/' + run_id + '.msh' def get_filename(data_type, file_type): files = os.listdir('%sinputs/%s' % (base_dir, data_type)) filename = '%sinputs/%s/%s' % ( base_dir, data_type, [f for f in files if f[-4:] == file_type][0]) return filename boundary_data_filename = get_filename('boundary_data', '.shp') elevation_data_filename = get_filename('elevation_data', '.tif') try: structures_filename = get_filename('structures', '.shp') except OSError as e: structures_filename = None try: rain_data_filename = get_filename('rain_data', '.shp') except OSError as e: rain_data_filename = None try: inflow_data_filename = get_filename('inflow_data', '.shp') except OSError as e: inflow_data_filename = None try: friction_data_filename = get_filename('friction_data', '.shp') except OSError as e: friction_data_filename = None logger.info('boundary_data_filename: %s' % boundary_data_filename) logger.info('structures_filename: %s' % structures_filename) logger.info('rain_data_filename: %s' % rain_data_filename) logger.info('inflow_data_filename: %s' % inflow_data_filename) logger.info('friction_data_filename: %s' % friction_data_filename) logger.info('elevation_data_filename: %s' % elevation_data_filename) # create a list of project files vector_filenames = [ boundary_data_filename, structures_filename, rain_data_filename, inflow_data_filename, friction_data_filename ] # set the projection system for ANUGA calculations from the geotiff elevation data elevation_data_gdal = gdal.Open(elevation_data_filename) project_spatial_ref = osr.SpatialReference() project_spatial_ref.ImportFromWkt(elevation_data_gdal.GetProjectionRef()) project_spatial_ref_epsg_code = int( project_spatial_ref.GetAttrValue("AUTHORITY", 1)) # check the spatial reference system of the project files matches that of the calculation for filename in vector_filenames: if filename: prj_text = open(filename[:-4] + '.prj').read() srs = osr.SpatialReference() srs.ImportFromESRI([prj_text]) srs.AutoIdentifyEPSG() logger.info('filename is: %s' % filename) logger.info('EPSG is: %s' % srs.GetAuthorityCode(None)) if str(srs.GetAuthorityCode(None)) != str( project_spatial_ref_epsg_code): logger.warning('warning spatial refs are not maching: %s, %s' % (srs.GetAuthorityCode(None), project_spatial_ref_epsg_code)) logger.info('Setting up structures...') if structures_filename: structures = [] logger.info('processing structures from :%s' % structures_filename) ogr_shapefile = ogr.Open(structures_filename) ogr_layer = ogr_shapefile.GetLayer(0) ogr_layer_feature = ogr_layer.GetNextFeature() while ogr_layer_feature: structure = json.loads(ogr_layer_feature.GetGeometryRef(). ExportToJson())['coordinates'][0] structures.append(structure) ogr_layer_feature = None ogr_layer_feature = ogr_layer.GetNextFeature() logger.info('structures: %s' % structures) else: logger.warning('warning: no structures found.') structures = None logger.info('Setting up friction...') frictions = [] if friction_data_filename: logger.info('processing frictions from :%s' % friction_data_filename) ogr_shapefile = ogr.Open(friction_data_filename) ogr_layer = ogr_shapefile.GetLayer(0) ogr_layer_feature = ogr_layer.GetNextFeature() while ogr_layer_feature: friction_poly = json.loads(ogr_layer_feature.GetGeometryRef(). ExportToJson())['coordinates'][0] friction_value = float(ogr_layer_feature.GetField('mannings')) friction_couple = [friction_poly, friction_value] frictions.append(friction_couple) ogr_layer_feature = None ogr_layer_feature = ogr_layer.GetNextFeature() frictions.append(['All', 0.04]) logger.info('frictions: %s' % frictions) else: frictions.append(['All', 0.04]) logger.info('warning: no frictions found.') logger.info('Setting up boundary conditions...') ogr_shapefile = ogr.Open(boundary_data_filename) ogr_layer = ogr_shapefile.GetLayer(0) ogr_layer_definition = ogr_layer.GetLayerDefn() logger.info('ogr_layer_definition.GetGeomType: %s' % ogr_layer_definition.GetGeomType()) boundary_tag_index = 0 bdy_tags = {} bdy = {} ogr_layer_feature = ogr_layer.GetNextFeature() while ogr_layer_feature: boundary_tag_key = ogr_layer_feature.GetField('bdy_tag_k') boundary_tag_value = ogr_layer_feature.GetField('bdy_tag_v') bdy_tags[boundary_tag_key] = [ boundary_tag_index * 2, boundary_tag_index * 2 + 1 ] bdy[boundary_tag_key] = boundary_tag_value geom = ogr_layer_feature.GetGeometryRef().GetPoints() ogr_layer_feature = None ogr_layer_feature = ogr_layer.GetNextFeature() boundary_tag_index = boundary_tag_index + 1 logger.info('bdy_tags: %s' % bdy_tags) logger.info('bdy: %s' % bdy) boundary_data = su.read_polygon(boundary_data_filename) create_mesh_from_regions(boundary_data, boundary_tags=bdy_tags, maximum_triangle_area=max_triangle_area, interior_regions=None, interior_holes=structures, filename=meshname, use_cache=False, verbose=True) domain = Domain(meshname, use_cache=False, verbose=True) domain.set_name(outname) domain.set_datadir(base_dir + '/outputs') logger.info(domain.statistics()) poly_fun_pairs = [['Extent', elevation_data_filename.encode("utf-8")]] topography_function = qs.composite_quantity_setting_function( poly_fun_pairs, domain, nan_treatment='exception', ) friction_function = qs.composite_quantity_setting_function( frictions, domain) domain.set_quantity('friction', friction_function, verbose=True) domain.set_quantity('stage', 0.0) domain.set_quantity('elevation', topography_function, verbose=True, alpha=0.99) domain.set_minimum_storable_height(0.005) logger.info('Applying rainfall...') if rain_data_filename: ogr_shapefile = ogr.Open(rain_data_filename) ogr_layer = ogr_shapefile.GetLayer(0) rainfall = 0 ogr_layer_feature = ogr_layer.GetNextFeature() while ogr_layer_feature: rainfall = float(ogr_layer_feature.GetField('rate_mm_hr')) polygon = su.read_polygon(rain_data_filename) logger.info("applying Polygonal_rate_operator with rate, polygon:") logger.info(rainfall) logger.info(polygon) Polygonal_rate_operator(domain, rate=rainfall, factor=1.0e-6, polygon=polygon, default_rate=0.0) ogr_layer_feature = None ogr_layer_feature = ogr_layer.GetNextFeature() logger.info('Applying surface inflows...') if inflow_data_filename: ogr_shapefile = ogr.Open(inflow_data_filename) ogr_layer = ogr_shapefile.GetLayer(0) ogr_layer_definition = ogr_layer.GetLayerDefn() ogr_layer_feature = ogr_layer.GetNextFeature() while ogr_layer_feature: in_fixed = float(ogr_layer_feature.GetField('in_fixed')) line = ogr_layer_feature.GetGeometryRef().GetPoints() logger.info("applying Inlet_operator with line, in_fixed:") logger.info(line) logger.info(in_fixed) Inlet_operator(domain, line, in_fixed, verbose=False) ogr_layer_feature = None ogr_layer_feature = ogr_layer.GetNextFeature() logger.info('Applying Boundary Conditions...') logger.info('Available boundary tags: %s' % domain.get_boundary_tags()) Br = anuga.Reflective_boundary(domain) Bd = anuga.Dirichlet_boundary([0.0, 0.0, 0.0]) Bt = anuga.Transmissive_boundary(domain) for key, value in bdy.iteritems(): if value == 'Br': bdy[key] = Br elif value == 'Bd': bdy[key] = Bd elif value == 'Bt': bdy[key] = Bt else: logger.info( 'No matching boundary condition exists - please check your shapefile attributes in: %s' % boundary_data_filename) # set a default value for exterior & interior boundary if it is not already set try: bdy['exterior'] except KeyError: bdy['exterior'] = Br try: bdy['interior'] except KeyError: bdy['interior'] = Br logger.info('bdy: %s' % bdy) domain.set_boundary(bdy) domain = distribute(domain) logger.info('Beginning evolve phase...') for t in domain.evolve(yieldstep, finaltime): domain.write_time() print domain.timestepping_statistics() logger.info(domain.timestepping_statistics(track_speeds=True)) percentage_complete = round(domain.time / domain.finaltime, 3) * 100 logger.info('%s percent complete' % percentage_complete) if run_id != 'local_run': write_percentage_complete(run_id, Runs, scenario_name, Scenario, session, percentage_complete) domain.sww_merge(delete_old=True) barrier() finalize() sww_file = base_dir + '/outputs/' + run_id + '.sww' sww_file = sww_file.encode( 'utf-8', 'ignore') # sometimes run_id gets turned to a unicode object by celery util.Make_Geotif(swwFile=sww_file, output_quantities=['depth', 'velocity'], myTimeStep='max', CellSize=max_triangle_area, lower_left=None, upper_right=None, EPSG_CODE=project_spatial_ref_epsg_code, proj4string=None, velocity_extrapolation=True, min_allowed_height=1.0e-05, output_dir=(base_dir + '/outputs/'), bounding_polygon=boundary_data, internal_holes=structures, verbose=False, k_nearest_neighbours=3, creation_options=[]) logger.info("Done. Nice work.")
#------------------------------------------------------------------------------ def topography(x, y): return -x/10. # linear bed slope def init_stage(x,y): stg= -x/10.+0.004 # Constant depth: 10 cm. return stg #line0=[ [0.,0.], [0., 100.] ] #Uin=[uana, 0.0] #Inlet_operator(domain, line0, Q=Qin, velocity=Uin) domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', mann) # Constant friction domain.set_quantity('stage', init_stage) domain.set_quantity('xmomentum', dana*uana) else: domain = None #=========================================================================== # Create Parallel Domain #=========================================================================== domain = distribute(domain) #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------
def test_rate_operator_rate_quantity(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] factor = 10.0 from anuga import Quantity rate_Q = Quantity(domain) rate_Q.set_values(1.0) operator = Rate_operator(domain, rate=rate_Q, factor=factor, \ indices=indices) # Apply Operator domain.timestep = 2.0 operator() rate = rate_Q.centroid_values[indices] t = operator.get_time() Q = operator.get_Q() rate = rate * factor Q_ex = num.sum(domain.areas[indices] * rate) d = operator.get_timestep() * rate + 1 #print "d" #print d #print Q_ex #print Q stage_ex = num.array([1.0, 1.0, 1.0, 1.0]) stage_ex[indices] = d verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex, Q) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum())
# structured mesh points, vertices, boundary = \ anuga.rectangular_cross(int(1.1*L/dx), int(W/dy), 1.1*L, W, (0.0, 0.0)) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('friction', 0.0) domain.set_quantity('elevation', elevation) domain.set_quantity('height', height) domain.set_quantity('stage', stage) else: domain = None #================================================================================== # Parallel Domain #================================================================================== domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions
domain = Domain(filename_root + '.msh', evolved_quantities = evolved_quantities) #------------------------------------------------------------------------------ # Setup parameters of computational domain #------------------------------------------------------------------------------ # Print some stats about mesh and domain print 'Number of triangles = ', len(domain) print 'The extent is ', domain.get_extent() print domain.statistics() domain.set_quantity('elevation', filename = filename_root + '.pts', use_cache = False, verbose = True, alpha = 0.1) domain.set_flow_algorithm('DE0') domain.set_name('run_raster_sed_transport') # Output name domain.set_store_vertices_uniquely(True) domain.set_quantity('stage', expression='elevation') # Dry initial condition """ Store process-specific quantities with same functions """ domain.set_quantities_to_be_stored({'elevation': 2,
domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def elevation(x, y): z_b = zeros(len(x)) for i in range(len(x)): if (8.0 <= x[i] <= 12.0): z_b[i] = 0.2 - 0.05 * (x[i] - 10.0)**2.0 else: z_b[i] = 0.0 return z_b domain.set_quantity('elevation', elevation) domain.set_quantity('friction', 0.0) def stage(x, y): return 2.0 * ones(len(x)) domain.set_quantity('stage', stage) else: domain = None #----------------------------------------------------------------------------- # Create Parallel Domain #------------------------------------------------------------------------------ domain = distribute(domain) #-----------------------------------------------------------------------------
domain = Domain(points, vertices, boundary) # Create Domain domain.set_name("runup") # Output to file runup.sww domain.set_datadir(".") # Use current folder domain.set_quantities_to_be_stored({"stage": 2, "xmomentum": 2, "ymomentum": 2, "elevation": 1}) domain.set_flow_algorithm(alg) # ------------------ # Define topography # ------------------ def topography(x, y): return -x / 2 # Linear bed slope def stagefun(x, y): return -0.45 # Stage domain.set_quantity("elevation", topography) # Use function for elevation domain.get_quantity( "elevation" ).smooth_vertex_values() # Steve's fix -- without this, substantial artificial velcities are generated everywhere in the domain. With this fix, there are artificial velocities near the coast, but not elsewhere. domain.set_quantity("friction", 0.0) # Constant friction domain.set_quantity("stage", stagefun) # Constant negative initial stage else: domain = None # -------------------------- # create Parallel Domain # -------------------------- domain = distribute(domain) # Setup boundary conditions # --------------------------
def test_rate_operator_negative_rate(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] #Catchment_Rain_Polygon = read_polygon(join('CatchmentBdy.csv')) #rainfall = file_function(join('1y120m.tms'), quantities=['rainfall']) rate = -1.0 factor = 10.0 default_rate = 0.0 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() stage_ex = [0., 0., 1., 0.] step_integral = -6.0 #print domain.quantities['elevation'].centroid_values #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values #print domain.fractional_step_volume_integral #print factor*domain.timestep*(rate*domain.areas[indices]).sum() #increment = factor*domain.timestep*rate*domain.areas assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, step_integral) # test timestepping_statistics stats = operator.timestepping_statistics() import re rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", stats) assert num.allclose(float(rr[1]), -1.0) assert num.allclose(float(rr[2]), -60.0)
""" Must include the process-specific quantities when creating the domain """ points, vertices, boundary = rectangular_cross(int(length/dx), int(width/dy), len1=length, len2=width) domain = Domain(points, vertices, boundary) domain.set_flow_algorithm('DE0') domain.set_name('run_simple_veg') # Output name domain.set_store_vertices_uniquely(True) domain.set_quantity('elevation', topography) # elevation is a function domain.set_quantity('stage', expression='elevation') # Dry initial condition """ Store process-specific quantities with same functions """ domain.set_quantities_to_be_stored({'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2}) #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------ max_elev = domain.quantities['elevation'].vertex_values.max()
def test_rate_operator_functions_spatial_with_ghost(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) area = numpy.sum(domain.areas) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print(domain.quantities['elevation'].centroid_values) print(domain.quantities['stage'].centroid_values) print(domain.quantities['xmomentum'].centroid_values) print(domain.quantities['ymomentum'].centroid_values) # Apply operator to these triangles factor = 10.0 def main_spatial_rate(x, y, t): # x and y should be an n by 1 array return x + y default_rate = 0.0 # kludge to make a ghost cell domain.tri_full_flag[1] = 0 operator = Rate_operator(domain, rate=main_spatial_rate, factor=factor, \ default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() Q_all = operator.get_Q(full_only=False) Q_full = operator.get_Q() x = operator.coord_c[:, 0] y = operator.coord_c[:, 1] rate = main_spatial_rate(x, y, t) * factor Q_ex_all = num.sum(domain.areas * rate) Q_ex_full = num.sum( num.where(domain.tri_full_flag == 1, domain.areas * rate, 0.0)) d = operator.get_timestep() * rate + 1 #print "d" #print d #print Q_ex_full, Q_ex_all stage_ex = num.array([1.0, 1.0, 1.0, 1.0]) stage_ex[:] = d if verbose: print(domain.quantities['elevation'].centroid_values) print(domain.quantities['stage'].centroid_values) print(domain.quantities['xmomentum'].centroid_values) print(domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex_all, Q_all) assert num.allclose(Q_ex_full, Q_full) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas * domain.tri_full_flag).sum()) # test timestepping_statistics stats = operator.timestepping_statistics() import re rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", stats) assert num.allclose(float(rr[1]), 1.33333) assert num.allclose(float(rr[2]), 3.33333) assert num.allclose(float(rr[3]), 160.0)
def distibute_three_processors(): """ Do a parallel test of distributing a rectangle onto 3 processors """ # FIXME: Need to update expected values on macos if sys.platform == 'darwin': return # FIXME: Need to update expected values on macos #if sys.platform == 'win32': # return from anuga.utilities import parallel_abstraction as pypar myid = pypar.rank() numprocs = pypar.size() if not numprocs == 3: return try: import pymetis metis_version = 5 except: metis_version = 4 #print numprocs #barrier() if myid == 0: nodes_0, triangles_0, boundary_0 = rectangular_cross(2, 2) domain = Domain(nodes_0, triangles_0, boundary_0) domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', 0.0) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial stage domain.set_quantity('xmomentum', expression='friction + 2.0') domain.set_quantity('ymomentum', ycoord) #---------------------------------------------------------------------------------- # Test pmesh_divide_metis #---------------------------------------------------------------------------------- vertices, triangles, boundary, triangles_per_proc, quantities = pmesh_divide_metis( domain, numprocs) if False: print_seq_values(vertices, triangles, triangles_per_proc) true_seq_values = get_true_seq_values(metis_version=metis_version) if False: print("True Seq Values = \\") pprint(true_seq_values) assert_allclose(vertices, true_seq_values['vertices']) assert_allclose(triangles, true_seq_values['triangles']) assert_allclose(triangles_per_proc, true_seq_values['triangles_per_proc']) #---------------------------------------------------------------------------------- # Test build_submesh #---------------------------------------------------------------------------------- submesh = build_submesh(vertices, triangles, boundary, quantities, triangles_per_proc) if False: print('submesh_values = \\') print_submesh_values(submesh) true_values = get_true_submesh_values(metis_version) assert_allclose(submesh['full_nodes'][0], true_values['full_nodes_0']) assert_allclose(submesh['full_nodes'][1], true_values['full_nodes_1']) assert_allclose(submesh['full_nodes'][2], true_values['full_nodes_2']) assert_allclose(submesh['ghost_nodes'][0], true_values['ghost_nodes_0']) assert_allclose(submesh['ghost_nodes'][1], true_values['ghost_nodes_1']) assert_allclose(submesh['ghost_nodes'][2], true_values['ghost_nodes_2']) assert_allclose(submesh['full_triangles'][0], true_values['full_triangles_0']) assert_allclose(submesh['full_triangles'][1], true_values['full_triangles_1']) assert_allclose(submesh['full_triangles'][2], true_values['full_triangles_2']) assert_allclose(submesh['ghost_triangles'][0], true_values['ghost_triangles_0']) assert_allclose(submesh['ghost_triangles'][1], true_values['ghost_triangles_1']) assert_allclose(submesh['ghost_triangles'][2], true_values['ghost_triangles_2']) assert_allclose(submesh['ghost_commun'][0], true_values['ghost_commun_0']) assert_allclose(submesh['ghost_commun'][1], true_values['ghost_commun_1']) assert_allclose(submesh['ghost_commun'][2], true_values['ghost_commun_2']) assert_(submesh['full_commun'] == true_values['full_commun']) barrier() #-------------------------------- # Now do the comunnication part #-------------------------------- if myid == 0: points, vertices, boundary, quantities, \ ghost_recv_dict, full_send_dict, tri_map, node_map, tri_l2g, node_l2g, \ ghost_layer_width =\ extract_submesh(submesh, triangles_per_proc) #---------------------------------------------------------------------------------- # Test send_submesh #---------------------------------------------------------------------------------- for p in range(1, numprocs): send_submesh(submesh, triangles_per_proc, p, verbose=False) else: #---------------------------------------------------------------------------------- # Test rec_submesh #---------------------------------------------------------------------------------- points, triangles, boundary, quantities, \ ghost_recv_dict, full_send_dict, \ no_full_nodes, no_full_trigs, tri_map, node_map, tri_l2g, node_l2g, \ ghost_layer_width = \ rec_submesh(0, verbose=False) barrier() #-------------------------------- # Now do the test #-------------------------------- if myid == 0: if False: print('extract_values = \\') print_extract_submesh(points, triangles, ghost_recv_dict, \ full_send_dict, tri_map, node_map, ghost_layer_width) true_values = get_true_extract_submesh(metis_version) assert_allclose(points, true_values['points']) assert_allclose(triangles, true_values['triangles']) assert_allclose(ghost_recv_dict[1], true_values['ghost_recv_dict_1']) assert_allclose(ghost_recv_dict[2], true_values['ghost_recv_dict_2']) assert_allclose(full_send_dict[1], true_values['full_send_dict_1']) assert_allclose(full_send_dict[2], true_values['full_send_dict_2']) assert_allclose(tri_map, true_values['tri_map']) assert_allclose(node_map, true_values['node_map']) assert_allclose(ghost_layer_width, true_values['ghost_layer_width']) if myid == 1: if False: print("rec_submesh_1 = \\") print_rec_submesh_1(points, triangles, ghost_recv_dict, full_send_dict, \ tri_map, node_map, ghost_layer_width) true_values = get_true_rec_submesh_1(metis_version) if False: print('true_rec_values_1 = \\') pprint(true_values) assert_allclose(points, true_values['points']) assert_allclose(triangles, true_values['triangles']) assert_allclose(ghost_recv_dict[0], true_values['ghost_recv_dict_0']) assert_allclose(ghost_recv_dict[2], true_values['ghost_recv_dict_2']) assert_allclose(full_send_dict[0], true_values['full_send_dict_0']) assert_allclose(full_send_dict[2], true_values['full_send_dict_2']) assert_allclose(tri_map, true_values['tri_map']) assert_allclose(node_map, true_values['node_map']) assert_allclose(ghost_layer_width, true_values['ghost_layer_width']) if myid == 2: if False: print("rec_submesh_2 = \\") print_rec_submesh_2(points, triangles, ghost_recv_dict, full_send_dict, \ tri_map, node_map, ghost_layer_width) true_values = get_true_rec_submesh_2(metis_version) if False: print('true_rec_values_2 = \\') pprint(true_values) assert_allclose(points, true_values['points']) assert_allclose(triangles, true_values['triangles']) assert_allclose(ghost_recv_dict[0], true_values['ghost_recv_dict_0']) assert_allclose(ghost_recv_dict[1], true_values['ghost_recv_dict_1']) assert_allclose(full_send_dict[0], true_values['full_send_dict_0']) assert_allclose(full_send_dict[1], true_values['full_send_dict_1']) assert_allclose(tri_map, true_values['tri_map']) assert_allclose(node_map, true_values['node_map']) assert_allclose(ghost_layer_width, true_values['ghost_layer_width']) finalize()
if myid == 0: # structured mesh points, vertices, boundary = anuga.rectangular_cross(int(L/dx), int(W/dy), L, W, (0.0, 0.0)) #domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation',0.0) domain.set_quantity('friction', 0.0) domain.set_quantity('stage', 2.) domain.set_quantity('xmomentum', 0.0) domain.set_quantity('ymomentum', 0.0) else: domain = None #--------------------------- # Create Parallel Domain #--------------------------- domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions
def test_rate_operator_rate_from_file(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] #--------------------------------- #Typical ASCII file #--------------------------------- finaltime = 1200 filename = 'test_file_function' fid = open(filename + '.txt', 'w') start = time.mktime(time.strptime('2000', '%Y')) dt = 60 #One minute intervals t = 0.0 while t <= finaltime: t_string = time.strftime(time_format, time.gmtime(t+start)) fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) t += dt fid.close() #Convert ASCII file to NetCDF (Which is what we really like!) timefile2netcdf(filename+'.txt') #Create file function from time series F = file_function(filename + '.tms', quantities = ['Attribute0', 'Attribute1', 'Attribute2']) #Now try interpolation for i in range(20): t = i*10 q = F(t) #Exact linear intpolation assert num.allclose(q[0], 2*t) if i%6 == 0: assert num.allclose(q[1], t**2) assert num.allclose(q[2], sin(t*pi/600)) #Check non-exact t = 90 #Halfway between 60 and 120 q = F(t) assert num.allclose( (120**2 + 60**2)/2, q[1] ) assert num.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) t = 100 #Two thirds of the way between between 60 and 120 q = F(t) assert num.allclose( 2*120**2/3 + 60**2/3, q[1] ) assert num.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) #os.remove(filename + '.txt') #os.remove(filename + '.tms') domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] rate = file_function(filename + '.tms', quantities=['Attribute1']) # Make starttime of domain consistent with tms file starttime domain.set_starttime(rate.starttime) factor = 1000.0 default_rate= 17.7 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.set_time(360.0) domain.timestep = 1.0 operator() d = domain.get_time()**2 * factor + 1.0 stage_ex0 = [ d, d, 1., d] # print d, domain.get_time(), F(360.0) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex0) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum()) domain.set_time(-10.0) domain.timestep = 1.0 try: operator() except: pass else: raise Exception('Should have raised an exception, time too early') domain.set_time(1300.0) domain.timestep = 1.0 operator() d = default_rate*factor + d stage_ex1 = [ d, d, 1., d] # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex1) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())
def test_rate_operator_functions_spatial_with_ghost(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) area = numpy.sum(domain.areas) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles factor = 10.0 def main_spatial_rate(x,y,t): # x and y should be an n by 1 array return x + y default_rate = 0.0 # kludge to make a ghost cell domain.tri_full_flag[1] = 0 operator = Rate_operator(domain, rate=main_spatial_rate, factor=factor, \ default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() Q_all = operator.get_Q(full_only=False) Q_full = operator.get_Q() x = operator.coord_c[:,0] y = operator.coord_c[:,1] rate = main_spatial_rate(x,y,t)*factor Q_ex_all = num.sum(domain.areas*rate) Q_ex_full = num.sum(num.where(domain.tri_full_flag==1,domain.areas*rate,0.0)) d = operator.get_timestep()*rate + 1 #print "d" #print d #print Q_ex_full, Q_ex_all stage_ex = num.array([ 1.0, 1.0, 1.0, 1.0]) stage_ex[:] = d if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex_all, Q_all) assert num.allclose(Q_ex_full, Q_full) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas*domain.tri_full_flag).sum()) # test timestepping_statistics stats = operator.timestepping_statistics() import re rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", stats) assert num.allclose(float(rr[1]), 1.33333) assert num.allclose(float(rr[2]), 3.33333) assert num.allclose(float(rr[3]), 160.0)
def test_rate_operator_functions_rate_default_rate(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] factor = 10.0 def main_rate(t): if t > 20: msg = 'Model time exceeded.' raise Modeltime_too_late, msg else: return 3.0 * t + 7.0 default_rate = lambda t: 3*t + 7 operator = Rate_operator(domain, rate=main_rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() d = operator.get_timestep()*main_rate(t)*factor + 1 stage_ex = [ d, d, 1., d] if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum()) domain.set_starttime(30.0) domain.timestep = 1.0 operator() t = operator.get_time() d = operator.get_timestep()*default_rate(t)*factor + d stage_ex = [ d, d, 1., d] if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
def test_rate_operator_rate_from_file(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] #--------------------------------- #Typical ASCII file #--------------------------------- finaltime = 1200 filename = 'test_file_function' fid = open(filename + '.txt', 'w') start = time.mktime(time.strptime('2000', '%Y')) dt = 60 #One minute intervals t = 0.0 while t <= finaltime: t_string = time.strftime(time_format, time.gmtime(t + start)) fid.write('%s, %f %f %f\n' % (t_string, 2 * t, t**2, sin(t * pi / 600))) t += dt fid.close() #Convert ASCII file to NetCDF (Which is what we really like!) timefile2netcdf(filename + '.txt') #Create file function from time series F = file_function( filename + '.tms', quantities=['Attribute0', 'Attribute1', 'Attribute2']) #Now try interpolation for i in range(20): t = i * 10 q = F(t) #Exact linear intpolation assert num.allclose(q[0], 2 * t) if i % 6 == 0: assert num.allclose(q[1], t**2) assert num.allclose(q[2], sin(t * pi / 600)) #Check non-exact t = 90 #Halfway between 60 and 120 q = F(t) assert num.allclose((120**2 + 60**2) / 2, q[1]) assert num.allclose((sin(120 * pi / 600) + sin(60 * pi / 600)) / 2, q[2]) t = 100 #Two thirds of the way between between 60 and 120 q = F(t) assert num.allclose(2 * 120**2 / 3 + 60**2 / 3, q[1]) assert num.allclose( 2 * sin(120 * pi / 600) / 3 + sin(60 * pi / 600) / 3, q[2]) #os.remove(filename + '.txt') #os.remove(filename + '.tms') domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0, 1, 3] rate = file_function('test_file_function.tms', quantities=['Attribute1']) factor = 1000.0 default_rate = 17.7 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.set_starttime(360.0) domain.timestep = 1.0 operator() d = domain.get_time()**2 * factor + 1.0 stage_ex0 = [d, d, 1., d] # print d, domain.get_time(), F(360.0) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex0) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum()) domain.set_starttime(-10.0) domain.timestep = 1.0 try: operator() except: pass else: raise Exception('Should have raised an exception, time too early') domain.set_starttime(1300.0) domain.timestep = 1.0 operator() d = default_rate * factor + d stage_ex1 = [d, d, 1., d] # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex1) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum())
def distibute_three_processors(): """ Do a parallel test of distributing a rectangle onto 3 processors """ # FIXME: Need to update expected values on macos if sys.platform == 'darwin': return import pypar myid = pypar.rank() numprocs = pypar.size() if not numprocs == 3: return #print numprocs barrier() if myid == 0: points, vertices, boundary = rectangular_cross(2,2) domain = Domain(points, vertices, boundary) domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', 0.0) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial stage domain.set_quantity('xmomentum', expression='friction + 2.0') # domain.set_quantity('ymomentum', ycoord) # #---------------------------------------------------------------------------------- # Test pmesh_divide_metis #---------------------------------------------------------------------------------- nodes, triangles, boundary, triangles_per_proc, quantities = pmesh_divide_metis(domain,numprocs) assert_(num.allclose(nodes,points)) true_vertices = [[0, 9, 1], [3, 9, 0], [4, 9, 3], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8]] true_triangles = [[4, 9, 3], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8], [0, 9, 1], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 9, 0], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7]] assert_(num.allclose(vertices,true_vertices)) assert_(num.allclose(triangles,true_triangles)) assert_(num.allclose(triangles_per_proc,[5,6,5])) #---------------------------------------------------------------------------------- # Test build_submesh #---------------------------------------------------------------------------------- submesh = build_submesh(nodes, triangles, boundary, quantities, triangles_per_proc) assert_(num.allclose(submesh['full_nodes'][0],[[3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [9.0, 0.25, 0.25], [12.0, 0.75, 0.75]])) assert_(num.allclose(submesh['full_nodes'][1],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [9.0, 0.25, 0.25], [10.0, 0.25, 0.75]])) assert_(num.allclose(submesh['full_nodes'][2],[[0.0, 0.0, 0.0], [3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [6.0, 1.0, 0.0], [7.0, 1.0, 0.5], [9.0, 0.25, 0.25], [11.0, 0.75, 0.25]])) assert_(num.allclose(submesh['ghost_nodes'][0],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [6.0, 1.0, 0.0], [10.0, 0.25, 0.75], [11.0, 0.75, 0.25]])) assert_(num.allclose(submesh['ghost_nodes'][1],[[3.0, 0.5, 0.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [11.0, 0.75, 0.25], [12.0, 0.75, 0.75]])) assert_(num.allclose(submesh['ghost_nodes'][2],[[1.0, 0.0, 0.5], [5.0, 0.5, 1.0], [8.0, 1.0, 1.0], [12.0, 0.75, 0.75]])) true_full_triangles = [num.array([[ 4, 9, 3], [ 4, 12, 5], [ 7, 12, 4], [ 8, 12, 7], [ 5, 12, 8]]), num.array([[ 0, 9, 1], [ 1, 9, 4], [ 1, 10, 2], [ 4, 10, 1], [ 5, 10, 4], [ 2, 10, 5]]), num.array([[ 3, 9, 0], [ 3, 11, 4], [ 6, 11, 3], [ 7, 11, 6], [ 4, 11, 7]])] assert_(num.allclose(submesh['full_triangles'][0],true_full_triangles[0])) assert_(num.allclose(submesh['full_triangles'][1],true_full_triangles[1])) assert_(num.allclose(submesh['full_triangles'][2],true_full_triangles[2])) true_ghost_triangles = [num.array([[ 5, 0, 9, 1], [ 6, 1, 9, 4], [ 8, 4, 10, 1], [ 9, 5, 10, 4], [10, 2, 10, 5], [11, 3, 9, 0], [12, 3, 11, 4], [13, 6, 11, 3], [14, 7, 11, 6], [15, 4, 11, 7]]), num.array([[ 0, 4, 9, 3], [ 1, 4, 12, 5], [ 2, 7, 12, 4], [ 4, 5, 12, 8], [11, 3, 9, 0], [12, 3, 11, 4]]), num.array([[ 0, 4, 9, 3], [ 1, 4, 12, 5], [ 2, 7, 12, 4], [ 3, 8, 12, 7], [ 5, 0, 9, 1], [ 6, 1, 9, 4]])] assert_(num.allclose(submesh['ghost_triangles'][0],true_ghost_triangles[0])) assert_(num.allclose(submesh['ghost_triangles'][1],true_ghost_triangles[1])) assert_(num.allclose(submesh['ghost_triangles'][2],true_ghost_triangles[2])) true_full_commun = [{0: [1, 2], 1: [1, 2], 2: [1, 2], 3: [2], 4: [1]}, {5: [0, 2], 6: [0, 2], 7: [], 8: [0], 9: [0], 10: [0]}, {11: [0, 1], 12: [0, 1], 13: [0], 14: [0], 15: [0]}] assert_(true_full_commun == submesh['full_commun']) true_ghost_commun = [num.array([[ 5, 1], [ 6, 1], [ 8, 1], [ 9, 1], [10, 1], [11, 2], [12, 2], [13, 2], [14, 2], [15, 2]]), num.array([[ 0, 0], [ 1, 0], [ 2, 0], [ 4, 0], [11, 2], [12, 2]]), num.array([[0, 0], [1, 0], [2, 0], [3, 0], [5, 1], [6, 1]])] assert_(num.allclose(submesh['ghost_commun'][0],true_ghost_commun[0])) assert_(num.allclose(submesh['ghost_commun'][1],true_ghost_commun[1])) assert_(num.allclose(submesh['ghost_commun'][2],true_ghost_commun[2])) barrier() #-------------------------------- # Now do the comunnication part #-------------------------------- if myid == 0: #---------------------------------------------------------------------------------- # Test send_submesh #---------------------------------------------------------------------------------- for p in range(1, numprocs): send_submesh(submesh, triangles_per_proc, p, verbose=False) #---------------------------------------------------------------------------------- # Test extract_submesh #---------------------------------------------------------------------------------- points, vertices, boundary, quantities, \ ghost_recv_dict, full_send_dict, tri_map, node_map, tri_l2g, node_l2g, \ ghost_layer_width =\ extract_submesh(submesh, triangles_per_proc) true_points = [[0.5, 0.0], [0.5, 0.5], [0.5, 1.0], [1.0, 0.5], [1.0, 1.0], [0.25, 0.25], [0.75, 0.75], [0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [1.0, 0.0], [0.25, 0.75], [0.75, 0.25]] true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [7, 5, 8], [8, 5, 1], [1, 11, 8], [2, 11, 1], [9, 11, 2], [0, 5, 7], [0, 12, 1], [10, 12, 0], [3, 12, 10], [1, 12, 3]] true_ghost_recv = {1: [num.array([5, 6, 7, 8, 9]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([10, 11, 12, 13, 14]), num.array([11, 12, 13, 14, 15])]} true_full_send = {1: [num.array([0, 1, 2, 4]), num.array([0, 1, 2, 4])], 2: [num.array([0, 1, 2, 3]), num.array([0, 1, 2, 3])]} assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) assert_(num.allclose(full_send_dict[1],true_full_send[1])) assert_(num.allclose(full_send_dict[2],true_full_send[2])) #print triangles_per_proc else: #---------------------------------------------------------------------------------- # Test rec_submesh #---------------------------------------------------------------------------------- points, vertices, boundary, quantities, \ ghost_recv_dict, full_send_dict, \ no_full_nodes, no_full_trigs, tri_map, node_map, tri_l2g, node_l2g, \ ghost_layer_width = \ rec_submesh(0, verbose=False) if myid == 1: true_points = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [0.5, 0.5], [0.5, 1.0], [0.25, 0.25], [0.25, 0.75], [0.5, 0.0], [1.0, 0.5], [1.0, 1.0], [0.75, 0.25], [0.75, 0.75]] true_vertices = [[0, 5, 1], [1, 5, 3], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [3, 5, 7], [3, 11, 4], [8, 11, 3], [4, 11, 9], [7, 5, 0], [7, 10, 3]] true_ghost_recv = {0: [num.array([6, 7, 8, 9]), num.array([0, 1, 2, 4])], 2: [num.array([10, 11]), num.array([11, 12])]} true_full_send = {0: [num.array([0, 1, 3, 4, 5]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([0, 1]), num.array([5, 6])]} true_tri_map = num.array([ 6, 7, 8, -1, 9, 0, 1, 2, 3, 4, 5, 10, 11]) true_node_map = num.array([ 0, 1, 2, 7, 3, 4, -1, 8, 9, 5, 6, 10, 11]) assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(tri_map, true_tri_map)) assert_(num.allclose(node_map, true_node_map)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) assert_(num.allclose(full_send_dict[0],true_full_send[0])) assert_(num.allclose(full_send_dict[2],true_full_send[2])) if myid == 2: true_points = [[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [1.0, 0.0], [1.0, 0.5], [0.25, 0.25], [0.75, 0.25], [0.0, 0.5], [0.5, 1.0], [1.0, 1.0], [0.75, 0.75]] true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [2, 5, 1], [2, 10, 8], [4, 10, 2], [9, 10, 4], [0, 5, 7], [7, 5, 2]] true_ghost_recv = {0: [num.array([5, 6, 7, 8]), num.array([0, 1, 2, 3])], 1: [num.array([ 9, 10]), num.array([5, 6])]} true_full_send = {0: [num.array([0, 1, 2, 3, 4]), num.array([11, 12, 13, 14, 15])], 1: [num.array([0, 1]), num.array([11, 12])]} true_tri_map = num.array([5, 6, 7, 8, -1, 9, 10, -1, -1, -1, -1, 0, 1, 2, 3, 4, -1]) true_node_map = num.array([ 0, 7, -1, 1, 2, 8 , 3, 4, 9, 5, -1, 6, 10]) assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(tri_map, true_tri_map)) assert_(num.allclose(node_map, true_node_map)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) assert_(num.allclose(full_send_dict[0],true_full_send[0])) assert_(num.allclose(full_send_dict[1],true_full_send[1]))
#domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) #------------------------------------------------------------------------------ # Setup Algorithm, either using command line arguments # or override manually yourself #------------------------------------------------------------------------------ domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('friction', 0.0) domain.set_quantity('stage', 12.0) XX = array([ 0., 50., 100., 150., 250., 300., 350., 400., 425., 435., 450., 470., 475., 500., 505., 530., 550., 565., 575., 600., 650., 700., 750., 800., 820., 900., 950., 1000., 1500. ]) ZZ = array([ 0., 0., 2.5, 5., 5., 3., 5., 5., 7.5, 8., 9., 9., 9., 9.1, 9., 9., 6., 5.5, 5.5, 5., 4., 3., 3., 2.3, 2., 1.2, 0.4, 0., 0. ]) WW = array([ 40., 40., 30., 30., 30., 30., 25., 25., 30., 35., 35., 40., 40., 40., 45., 45., 50., 45., 40., 40., 30., 40., 40., 5., 40., 35., 25., 40., 40. ]) / 2.
def test_runup_sinusoid(self): """ Run a version of the validation test runup_sinusoid to ensure limiting solution has small velocity """ points, vertices, boundary = anuga.rectangular_cross(20,20, len1=1., len2=1.) domain=Domain(points,vertices,boundary) # Create Domain domain.set_flow_algorithm('DE0') domain.set_name('runup_sinusoid_v2') # Output to file runup.sww domain.set_datadir('.') # Use current folder domain.set_quantities_to_be_stored({'stage': 2, 'xmomentum': 2, 'ymomentum': 2, 'elevation': 1}) #domain.set_store_vertices_uniquely(True) #------------------ # Define topography #------------------ scale_me=1.0 def topography(x,y): return (-x/2.0 +0.05*num.sin((x+y)*50.0))*scale_me def stagefun(x,y): stge=-0.2*scale_me #+0.01*(x>0.9) return stge domain.set_quantity('elevation',topography) # Use function for elevation domain.get_quantity('elevation').smooth_vertex_values() domain.set_quantity('friction',0.03) # Constant friction domain.set_quantity('stage', stagefun) # Constant negative initial stage domain.get_quantity('stage').smooth_vertex_values() #-------------------------- # Setup boundary conditions #-------------------------- Br=anuga.Reflective_boundary(domain) # Solid reflective wall Bd=anuga.Dirichlet_boundary([-0.1*scale_me,0.,0.]) # Constant boundary values -- not used in this example #---------------------------------------------- # Associate boundary tags with boundary objects #---------------------------------------------- domain.set_boundary({'left': Br, 'right': Bd, 'top': Br, 'bottom':Br}) #------------------------------ #Evolve the system through time #------------------------------ for t in domain.evolve(yieldstep=7.0,finaltime=7.0): #print domain.timestepping_statistics() xx = domain.quantities['xmomentum'].centroid_values yy = domain.quantities['ymomentum'].centroid_values dd = domain.quantities['stage'].centroid_values - domain.quantities['elevation'].centroid_values #dd_raw=1.0*dd dd = (dd)*(dd>1.0e-03)+1.0e-03 vv = ( (xx/dd)**2 + (yy/dd)**2)**0.5 vv = vv*(dd>1.0e-03) #print 'Peak velocity is: ', vv.max(), vv.argmax() #print 'Volume is', sum(dd_raw*domain.areas) #print vv.max() assert num.all(vv<1.01e-01)
def test_rate_operator_functions_empty_indices(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [] factor = 10.0 def main_spatial_rate(x, y, t): # x and y should be an n by 1 array return x + y default_rate = 0.0 domain.tri_full_flag[0] = 0 operator = Rate_operator(domain, rate=main_spatial_rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() Q = operator.get_Q() x = operator.coord_c[indices, 0] y = operator.coord_c[indices, 1] rate = main_spatial_rate(x, y, t) * factor Q_ex = num.sum(domain.areas[indices] * rate) d = operator.get_timestep() * rate + 1 #print Q_ex, Q #print indices #print "d" #print d stage_ex = num.array([1.0, 1.0, 1.0, 1.0]) stage_ex[indices] = d if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex, Q) assert num.allclose(domain.fractional_step_volume_integral, ((d - 1.) * domain.areas[indices]).sum())
domain.set_name(output_file) domain.set_datadir(output_dir) #------------------------------------------------------------------------------ # Setup Algorithm, either using command line arguments # or override manually yourself #------------------------------------------------------------------------------ from anuga.utilities.argparsing import parse_standard_args alg = parse_standard_args() domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation',0.0) domain.set_quantity('friction', 0.0) h0 = 5.0 h1 = 1.0 def height(x,y): z = zeros(len(x), float) for i in range(len(x)): if x[i]<=0.0: z[i] = h0 else: z[i] = h1 return z domain.set_quantity('stage', height)
domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) #------------------------------------------------------------------------------ # Setup Algorithm, either using command line arguments # or override manually yourself #------------------------------------------------------------------------------ domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation',-100.0) domain.set_quantity('friction', 0.00) domain.set_quantity('stage', 0.0) else: domain = None #----------------------------------------------------------------------------------- # Parallel Domain #----------------------------------------------------------------------------------- domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions #------------------------------------------------------------------------------
domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def elevation(x,y): z_b = zeros(len(x)) for i in range(len(x)): if (8.0 <= x[i] <= 12.0): z_b[i] = 0.2 - 0.05*(x[i]-10.0)**2 else: z_b[i] = 0.0 return z_b domain.set_quantity('elevation',elevation) domain.set_quantity('friction', 0.0) domain.set_quantity('xmomentum', 0.0) def stage(x,y): return 0.66*ones(len(x)) domain.set_quantity('stage', stage) else: domain = None #----------------------------------------------------------------------------- # create Parallel Domain #------------------------------------------------------------------------------ domain = distribute(domain)
# Setup initial conditions #------------------------------------------------------------------------------ def topography(x, y): return -x / 10. # linear bed slope def init_stage(x, y): stg = -x / 10. + 0.004 # Constant depth: 10 cm. return stg #line0=[ [0.,0.], [0., 100.] ] #Uin=[uana, 0.0] #Inlet_operator(domain, line0, Q=Qin, velocity=Uin) domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', mann) # Constant friction domain.set_quantity('stage', init_stage) domain.set_quantity('xmomentum', dana * uana) else: domain = None #=========================================================================== # Create Parallel Domain #=========================================================================== domain = distribute(domain) #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------
points, vertices, boundary = anuga.rectangular_cross(int(L / dx), int(W / dy), L, W, (-L / 2.0, -W / 2.0)) # domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) # ------------------------------------------------------------------------------ # Setup initial conditions # ------------------------------------------------------------------------------ # No Mannings friction, but we introduce Coulomb friction below. domain.set_quantity("friction", 0.0) domain.set_quantity("stage", stage) domain.set_quantity("elevation", elevation) else: domain = None domain = distribute(domain) # ----------------------------------------------------------------------------- # Special implementation of Coulomb friction # ----------------------------------------------------------------------------- domain.forcing_terms.append(Coulomb_forcing_term)
#domain = anuga.Domain(points, vertices, boundary) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) #------------------------------------------------------------------------------ # Setup Algorithm, either using command line arguments # or override manually yourself #------------------------------------------------------------------------------ domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('elevation', 0.0) domain.set_quantity('friction', 0.0) domain.set_quantity('stage', height) else: domain = None #=================================================================================== # create parallel domain #=================================================================================== domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions #------------------------------------------------------------------------------ Br = anuga.Reflective_boundary(domain) # Solid reflective wall Bt = anuga.Transmissive_boundary(domain) # Continue all values on boundary
def test_erosion_operator_simple_1_5(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) domain.set_flow_algorithm('1_5') #Flat surface with 1m of water domain.set_quantity('elevation', 0.5) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) domain.set_quantity('xmomentum',2.0) domain.set_quantity('ymomentum',3.0) Stage = domain.quantities['stage'].centroid_values Elevation = domain.quantities['elevation'].centroid_values Height = Stage - Elevation sum1 = num.sum(Height) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] operator = Erosion_operator(domain, indices=indices, logging=True) # Apply Operator domain.timestep = 2.0 operator() elev_ex = [ 0.05555556, 0.11111111, 0.27777778, 0.05555556] stage_ex = [ 0.55555556, 0.61111111, 0.77777778, 0.55555556] Stage = domain.quantities['stage'].centroid_values Elevation = domain.quantities['elevation'].centroid_values Height = Stage - Elevation sum2 = num.sum(Height) #print domain.quantities['elevation'].centroid_values #print domain.quantities['stage'].centroid_values #print domain.quantities['xmomentum'].centroid_values #print domain.quantities['ymomentum'].centroid_values assert sum1 == sum2 assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 2.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 3.0)
len2=width) domain = Domain(points, vertices, boundary) domain.set_flow_algorithm('DE0') domain.set_name() # Output name domain.set_store_vertices_uniquely(True) print domain.statistics() domain.set_quantities_to_be_stored({ 'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2 }) domain.set_quantity('elevation', topography) # elevation is a function domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial condition #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------ Bi = Dirichlet_boundary([1.5, 0, 0]) # Inflow Br = Reflective_boundary(domain) # Solid reflective wall Bo = Dirichlet_boundary([-5, 0, 0]) # Outflow domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Setup erosion operator in the middle of dam #------------------------------------------------------------------------------
def test_rate_operator_rate_quantity(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] factor = 10.0 from anuga import Quantity rate_Q = Quantity(domain) rate_Q.set_values(1.0) operator = Rate_operator(domain, rate=rate_Q, factor=factor, \ indices=indices) # Apply Operator domain.timestep = 2.0 operator() rate = rate_Q.centroid_values[indices] t = operator.get_time() Q = operator.get_Q() rate = rate*factor Q_ex = num.sum(domain.areas[indices]*rate) d = operator.get_timestep()*rate + 1 #print "d" #print d #print Q_ex #print Q stage_ex = num.array([ 1.0, 1.0, 1.0, 1.0]) stage_ex[indices] = d verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex, Q) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())
'xmomentum': 2, 'ymomentum': 2, 'elevation': 1 }) domain.set_flow_algorithm(alg) #------------------ # Define topography #------------------ def topography(x, y): return -x / 2 #Linear bed slope def stagefun(x, y): return -0.45 #Stage domain.set_quantity('elevation', topography) # Use function for elevation domain.get_quantity('elevation').smooth_vertex_values( ) # Steve's fix -- without this, substantial artificial velcities are generated everywhere in the domain. With this fix, there are artificial velocities near the coast, but not elsewhere. domain.set_quantity('friction', 0.0) # Constant friction domain.set_quantity('stage', stagefun) # Constant negative initial stage else: domain = None #-------------------------- # create Parallel Domain #-------------------------- domain = distribute(domain) # Setup boundary conditions #-------------------------- Br = anuga.Reflective_boundary(domain) # Solid reflective wall
def test_rate_operator_functions_empty_indices(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0.0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) verbose = False if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [] factor = 10.0 def main_spatial_rate(x,y,t): # x and y should be an n by 1 array return x + y default_rate = 0.0 domain.tri_full_flag[0] = 0 operator = Rate_operator(domain, rate=main_spatial_rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.timestep = 2.0 operator() t = operator.get_time() Q = operator.get_Q() x = operator.coord_c[indices,0] y = operator.coord_c[indices,1] rate = main_spatial_rate(x,y,t)*factor Q_ex = num.sum(domain.areas[indices]*rate) d = operator.get_timestep()*rate + 1 #print Q_ex, Q #print indices #print "d" #print d stage_ex = num.array([ 1.0, 1.0, 1.0, 1.0]) stage_ex[indices] = d if verbose: print domain.quantities['elevation'].centroid_values print domain.quantities['stage'].centroid_values print domain.quantities['xmomentum'].centroid_values print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(Q_ex, Q) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())
points, vertices, boundary = rectangular_cross(int(length/dx), int(width/dy), len1=length, len2=width) domain = Domain(points, vertices, boundary) domain.set_flow_algorithm('DE1') domain.set_name('flat_fill_slice_erosion') # Output name print domain.statistics() domain.set_quantities_to_be_stored({'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2}) domain.set_quantity('elevation', topography) # elevation is a function domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial condition #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------ Bi = Dirichlet_boundary([1.5, 0, 0]) # Inflow Br = Reflective_boundary(domain) # Solid reflective wall Bo = Dirichlet_boundary([-5, 0, 0]) # Outflow domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Setup erosion operator in the middle of dam #------------------------------------------------------------------------------
def distibute_three_processors(): """ Do a parallel test of distributing a rectangle onto 3 processors """ myid = par.rank() par.numprocs = par.size() if not par.numprocs == 3: return #print par.numprocs par.barrier() if myid == 0: points, vertices, boundary = rectangular_cross(2,2) domain = Domain(points, vertices, boundary) domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', 0.0) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial stage domain.set_quantity('xmomentum', expression='friction + 2.0') # domain.set_quantity('ymomentum', ycoord) # #---------------------------------------------------------------------------------- # Test pmesh_divide_metis #---------------------------------------------------------------------------------- nodes, triangles, boundary, triangles_per_proc, quantities = pmesh_divide_metis(domain,par.numprocs) assert_(num.allclose(nodes,points)) true_vertices = [[0, 9, 1], [3, 9, 0], [4, 9, 3], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8]] true_triangles = [[4, 9, 3], [4, 12, 5], [7, 12, 4], [8, 12, 7], [5, 12, 8], [0, 9, 1], [1, 9, 4], [1, 10, 2], [4, 10, 1], [5, 10, 4], [2, 10, 5], [3, 9, 0], [3, 11, 4], [6, 11, 3], [7, 11, 6], [4, 11, 7]] assert_(num.allclose(vertices,true_vertices)) assert_(num.allclose(triangles,true_triangles)) assert_(num.allclose(triangles_per_proc,[5,6,5])) #---------------------------------------------------------------------------------- # Test build_submesh #---------------------------------------------------------------------------------- submesh = build_submesh(nodes, triangles, boundary, quantities, triangles_per_proc) assert_(num.allclose(submesh['full_nodes'][0],[[3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [9.0, 0.25, 0.25], [12.0, 0.75, 0.75]])) assert_(num.allclose(submesh['full_nodes'][1],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [4.0, 0.5, 0.5], [5.0, 0.5, 1.0], [9.0, 0.25, 0.25], [10.0, 0.25, 0.75]])) assert_(num.allclose(submesh['full_nodes'][2],[[0.0, 0.0, 0.0], [3.0, 0.5, 0.0], [4.0, 0.5, 0.5], [6.0, 1.0, 0.0], [7.0, 1.0, 0.5], [9.0, 0.25, 0.25], [11.0, 0.75, 0.25]])) assert_(num.allclose(submesh['ghost_nodes'][0],[[0.0, 0.0, 0.0], [1.0, 0.0, 0.5], [2.0, 0.0, 1.0], [6.0, 1.0, 0.0], [10.0, 0.25, 0.75], [11.0, 0.75, 0.25]])) assert_(num.allclose(submesh['ghost_nodes'][1],[[3.0, 0.5, 0.0], [7.0, 1.0, 0.5], [8.0, 1.0, 1.0], [11.0, 0.75, 0.25], [12.0, 0.75, 0.75]])) assert_(num.allclose(submesh['ghost_nodes'][2],[[1.0, 0.0, 0.5], [5.0, 0.5, 1.0], [8.0, 1.0, 1.0], [12.0, 0.75, 0.75]])) true_full_triangles = [num.array([[ 4, 9, 3], [ 4, 12, 5], [ 7, 12, 4], [ 8, 12, 7], [ 5, 12, 8]]), num.array([[ 0, 9, 1], [ 1, 9, 4], [ 1, 10, 2], [ 4, 10, 1], [ 5, 10, 4], [ 2, 10, 5]]), num.array([[ 3, 9, 0], [ 3, 11, 4], [ 6, 11, 3], [ 7, 11, 6], [ 4, 11, 7]])] assert_(num.allclose(submesh['full_triangles'][0],true_full_triangles[0])) assert_(num.allclose(submesh['full_triangles'][1],true_full_triangles[1])) assert_(num.allclose(submesh['full_triangles'][2],true_full_triangles[2])) true_ghost_triangles = [num.array([[ 5, 0, 9, 1], [ 6, 1, 9, 4], [ 8, 4, 10, 1], [ 9, 5, 10, 4], [10, 2, 10, 5], [11, 3, 9, 0], [12, 3, 11, 4], [13, 6, 11, 3], [14, 7, 11, 6], [15, 4, 11, 7]]), num.array([[ 0, 4, 9, 3], [ 1, 4, 12, 5], [ 2, 7, 12, 4], [ 4, 5, 12, 8], [11, 3, 9, 0], [12, 3, 11, 4]]), num.array([[ 0, 4, 9, 3], [ 1, 4, 12, 5], [ 2, 7, 12, 4], [ 3, 8, 12, 7], [ 5, 0, 9, 1], [ 6, 1, 9, 4]])] assert_(num.allclose(submesh['ghost_triangles'][0],true_ghost_triangles[0])) assert_(num.allclose(submesh['ghost_triangles'][1],true_ghost_triangles[1])) assert_(num.allclose(submesh['ghost_triangles'][2],true_ghost_triangles[2])) true_full_commun = [{0: [1, 2], 1: [1, 2], 2: [1, 2], 3: [2], 4: [1]}, {5: [0, 2], 6: [0, 2], 7: [], 8: [0], 9: [0], 10: [0]}, {11: [0, 1], 12: [0, 1], 13: [0], 14: [0], 15: [0]}] assert_(true_full_commun == submesh['full_commun']) true_ghost_commun = [num.array([[ 5, 1], [ 6, 1], [ 8, 1], [ 9, 1], [10, 1], [11, 2], [12, 2], [13, 2], [14, 2], [15, 2]]), num.array([[ 0, 0], [ 1, 0], [ 2, 0], [ 4, 0], [11, 2], [12, 2]]), num.array([[0, 0], [1, 0], [2, 0], [3, 0], [5, 1], [6, 1]])] assert_(num.allclose(submesh['ghost_commun'][0],true_ghost_commun[0])) assert_(num.allclose(submesh['ghost_commun'][1],true_ghost_commun[1])) assert_(num.allclose(submesh['ghost_commun'][2],true_ghost_commun[2])) par.barrier() #-------------------------------- # Now do the comunnication part #-------------------------------- if myid == 0: #---------------------------------------------------------------------------------- # Test send_submesh #---------------------------------------------------------------------------------- for p in range(1, par.numprocs): send_submesh(submesh, triangles_per_proc, p, verbose=False) #---------------------------------------------------------------------------------- # Test extract_submesh #---------------------------------------------------------------------------------- points, vertices, boundary, quantities, \ ghost_recv_dict, full_send_dict, tri_map, node_map, ghost_layer_width =\ extract_submesh(submesh, triangles_per_proc) true_points = [[0.5, 0.0], [0.5, 0.5], [0.5, 1.0], [1.0, 0.5], [1.0, 1.0], [0.25, 0.25], [0.75, 0.75], [0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [1.0, 0.0], [0.25, 0.75], [0.75, 0.25]] true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [7, 5, 8], [8, 5, 1], [1, 11, 8], [2, 11, 1], [9, 11, 2], [0, 5, 7], [0, 12, 1], [10, 12, 0], [3, 12, 10], [1, 12, 3]] true_ghost_recv = {1: [num.array([5, 6, 7, 8, 9]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([10, 11, 12, 13, 14]), num.array([11, 12, 13, 14, 15])]} true_full_send = {1: [num.array([0, 1, 2, 4]), num.array([0, 1, 2, 4])], 2: [num.array([0, 1, 2, 3]), num.array([0, 1, 2, 3])]} assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) assert_(num.allclose(full_send_dict[1],true_full_send[1])) assert_(num.allclose(full_send_dict[2],true_full_send[2])) #print triangles_per_proc else: #---------------------------------------------------------------------------------- # Test rec_submesh #---------------------------------------------------------------------------------- points, vertices, boundary, quantities, \ ghost_recv_dict, full_send_dict, \ no_full_nodes, no_full_trigs, tri_map, node_map, ghost_layer_width = \ rec_submesh(0, verbose=False) if myid == 1: true_points = [[0.0, 0.0], [0.0, 0.5], [0.0, 1.0], [0.5, 0.5], [0.5, 1.0], [0.25, 0.25], [0.25, 0.75], [0.5, 0.0], [1.0, 0.5], [1.0, 1.0], [0.75, 0.25], [0.75, 0.75]] true_vertices = [[0, 5, 1], [1, 5, 3], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [3, 5, 7], [3, 11, 4], [8, 11, 3], [4, 11, 9], [7, 5, 0], [7, 10, 3]] true_ghost_recv = {0: [num.array([6, 7, 8, 9]), num.array([0, 1, 2, 4])], 2: [num.array([10, 11]), num.array([11, 12])]} true_full_send = {0: [num.array([0, 1, 3, 4, 5]), num.array([ 5, 6, 8, 9, 10])], 2: [num.array([0, 1]), num.array([5, 6])]} true_tri_map = num.array([ 6, 7, 8, -1, 9, 0, 1, 2, 3, 4, 5, 10, 11]) true_node_map = num.array([ 0, 1, 2, 7, 3, 4, -1, 8, 9, 5, 6, 10, 11]) assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(tri_map, true_tri_map)) assert_(num.allclose(node_map, true_node_map)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) assert_(num.allclose(ghost_recv_dict[2],true_ghost_recv[2])) assert_(num.allclose(full_send_dict[0],true_full_send[0])) assert_(num.allclose(full_send_dict[2],true_full_send[2])) if myid == 2: true_points = [[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [1.0, 0.0], [1.0, 0.5], [0.25, 0.25], [0.75, 0.25], [0.0, 0.5], [0.5, 1.0], [1.0, 1.0], [0.75, 0.75]] true_vertices = [[1, 5, 0], [1, 6, 2], [3, 6, 1], [4, 6, 3], [2, 6, 4], [2, 5, 1], [2, 10, 8], [4, 10, 2], [9, 10, 4], [0, 5, 7], [7, 5, 2]] true_ghost_recv = {0: [num.array([5, 6, 7, 8]), num.array([0, 1, 2, 3])], 1: [num.array([ 9, 10]), num.array([5, 6])]} true_full_send = {0: [num.array([0, 1, 2, 3, 4]), num.array([11, 12, 13, 14, 15])], 1: [num.array([0, 1]), num.array([11, 12])]} true_tri_map = num.array([5, 6, 7, 8, -1, 9, 10, -1, -1, -1, -1, 0, 1, 2, 3, 4, -1]) true_node_map = num.array([ 0, 7, -1, 1, 2, 8 , 3, 4, 9, 5, -1, 6, 10]) assert_(num.allclose(ghost_layer_width, 2)) assert_(num.allclose(tri_map, true_tri_map)) assert_(num.allclose(node_map, true_node_map)) assert_(num.allclose(points, true_points)) assert_(num.allclose(vertices, true_vertices)) assert_(num.allclose(ghost_recv_dict[0],true_ghost_recv[0])) assert_(num.allclose(ghost_recv_dict[1],true_ghost_recv[1])) assert_(num.allclose(full_send_dict[0],true_full_send[0])) assert_(num.allclose(full_send_dict[1],true_full_send[1]))
# Setup sequential domain #------------------------------------------------------------------------------ if myid == 0: # structured mesh points, vertices, boundary = anuga.rectangular_cross(int(L/dx), int(W/dy), L, W, (0.0, 0.0)) domain = Domain(points, vertices, boundary) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('friction', 0.0) domain.set_quantity('stage', stage_flat) domain.set_quantity('elevation', bed_elevation) else: domain = None #----------------------------------------------------------------------------- # Parallel Domain #----------------------------------------------------------------------------- domain = distribute(domain) #----------------------------------------------------------------------------- # Setup boundary conditions #------------------------------------------------------------------------------
evolved_quantities = ['stage', 'xmomentum', 'ymomentum', 'elevation'] domain = Domain(points, vertices, boundary, evolved_quantities=evolved_quantities) domain.set_flow_algorithm('DE0') domain.set_name('veg') # Output name # domain.set_store_vertices_uniquely(True) # print domain.statistics() domain.set_quantities_to_be_stored({'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2}) domain.set_quantity('elevation', topography) # elevation is a function domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', topography) # Dry initial condition #------------------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------------------ Bi = Dirichlet_boundary([11.5, 0, 0]) # Inflow Br = Reflective_boundary(domain) # Solid reflective wall # Bo = Dirichlet_boundary([0, 0., 0.]) # Outflow Bo = Transmissive_boundary(domain) domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Setup erosion operator in the middle of dam