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 setup_and_evolve(domain, verbose=False): #-------------------------------------------------------------------------- # Setup domain parameters #-------------------------------------------------------------------------- domain.set_flow_algorithm('DE0') #domain.set_store_vertices_uniquely() #------------------------------------------------------------------------------ # Setup boundary conditions # This must currently happen *AFTER* domain has been distributed #------------------------------------------------------------------------------ Br = Reflective_boundary(domain) # Solid reflective wall Bd = Dirichlet_boundary([-0.2,0.,0.]) # Constant boundary values # Associate boundary tags with boundary objects domain.set_boundary({'left': Br, 'right': Bd, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Evolve #------------------------------------------------------------------------------ for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): if myid == 0 and verbose : domain.write_time() #if myid == 0 and verbose : print domain.quantities['stage'].get_maximum_value() domain.sww_merge(delete_old=True)
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_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 concept_ungenerateIII(self): from anuga import Domain, Reflective_boundary, \ Dirichlet_boundary from anuga.pmesh.mesh_interface import create_mesh_from_regions # These are the absolute values polygon = [[0, 0], [100, 0], [100, 100], [0, 100]] boundary_tags = {'wall': [0, 1, 3], 'wave': [2]} inner1_polygon = [[10, 10], [20, 10], [20, 20], [10, 20]] inner2_polygon = [[30, 30], [40, 30], [40, 40], [30, 40]] max_area = 1 interior_regions = [(inner1_polygon, 5), (inner2_polygon, 10)] m = create_mesh_from_regions(polygon, boundary_tags, max_area, interior_regions=interior_regions) fileName = tempfile.mktemp('.txt') file = open(fileName, 'w') file.write(' 1 ?? ??\n\ 90.0 90.0\n\ 81.0 90.0\n\ 81.0 81.0\n\ 90.0 81.0\n\ 90.0 90.0\n\ END\n\ 2 ?? ??\n\ 10.0 80.0\n\ 10.0 90.0\n\ 20.0 90.0\n\ 10.0 80.0\n\ END\n\ END\n') file.close() m.import_ungenerate_file(fileName) os.remove(fileName) m.generate_mesh(maximum_triangle_area=max_area, verbose=False) mesh_filename = 'mesh.tsh' m.export_mesh_file(mesh_filename) domain = Domain(mesh_filename, use_cache=False) Br = Reflective_boundary(domain) Bd = Dirichlet_boundary([3, 0, 0]) domain.set_boundary({'wall': Br, 'wave': Bd}) yieldstep = 0.1 finaltime = 10 for t in domain.evolve(yieldstep, finaltime): domain.write_time()
def run_simulation(parallel=False): domain = create_domain_from_file(mesh_filename) domain.set_quantity('stage', Set_Stage(756000.0, 756500.0, 2.0)) #-------------------------------------------------------------------------- # Create parallel domain if requested #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose: print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain) #------------------------------------------------------------------------------ # Setup boundary conditions # This must currently happen *after* domain has been distributed #------------------------------------------------------------------------------ domain.store = False Br = Reflective_boundary(domain) # Solid reflective wall domain.set_boundary({ 'outflow': Br, 'inflow': Br, 'inner': Br, 'exterior': Br, 'open': Br }) #------------------------------------------------------------------------------ # Evolution #------------------------------------------------------------------------------ if parallel: if myid == 0 and verbose: print('PARALLEL EVOLVE') else: if verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep=yieldstep, finaltime=finaltime): pass
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 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 test_set_elevation_operator_small_function_de0(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) 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] def elev(t): if t < 10.0: return 5.0 else: return 7.0 operator = Set_elevation_operator(domain, elevation=elev, indices=indices) # Apply Operator at time t=1.0 domain.set_time(1.0) operator() elev_ex = [5., 5., 0., 5.] stage_ex = [6., 6., 1., 6.] #pprint( domain.quantities['elevation'].centroid_values) #pprint( domain.quantities['stage'].centroid_values) #pprint( domain.quantities['xmomentum'].centroid_values) #pprint( domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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) # Apply Operator at time t=15.0 domain.set_time(15.0) operator() elev_ex = [7., 7., 0., 7.] stage_ex = [8., 8., 1., 8.] #pprint( domain.quantities['elevation'].centroid_values ) #pprint( domain.quantities['stage'].centroid_values ) # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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 concept_ungenerateII(self): from anuga import Domain, Reflective_boundary, Dirichlet_boundary x = 0 y = 0 mesh_geo = geo_reference = Geo_reference(56, x, y) # These are the absolute values polygon_absolute = [[0, 0], [100, 0], [100, 100], [0, 100]] x_p = -10 y_p = -40 geo_ref_poly = Geo_reference(56, x_p, y_p) polygon = geo_ref_poly.change_points_geo_ref(polygon_absolute) boundary_tags = {'wall': [0, 1, 3], 'wave': [2]} inner1_polygon_absolute = [[10, 10], [20, 10], [20, 20], [10, 20]] inner1_polygon = geo_ref_poly.\ change_points_geo_ref(inner1_polygon_absolute) inner2_polygon_absolute = [[30, 30], [40, 30], [40, 40], [30, 40]] inner2_polygon = geo_ref_poly.\ change_points_geo_ref(inner2_polygon_absolute) max_area = 1 interior_regions = [(inner1_polygon, 5), (inner2_polygon, 10)] m = create_mesh_from_regions(polygon, boundary_tags, max_area, interior_regions=interior_regions, poly_geo_reference=geo_ref_poly, mesh_geo_reference=mesh_geo) m.export_mesh_file('a_test_mesh_iknterface.tsh') fileName = tempfile.mktemp('.txt') file = open(fileName, 'w') file.write(' 1 ?? ??\n\ 90.0 90.0\n\ 81.0 90.0\n\ 81.0 81.0\n\ 90.0 81.0\n\ 90.0 90.0\n\ END\n\ 2 ?? ??\n\ 10.0 80.0\n\ 10.0 90.0\n\ 20.0 90.0\n\ 10.0 80.0\n\ END\n\ END\n') file.close() m.import_ungenerate_file(fileName) #, tag='wall') os.remove(fileName) m.generate_mesh(maximum_triangle_area=max_area, verbose=False) mesh_filename = 'bento_b.tsh' m.export_mesh_file(mesh_filename) domain = Domain(mesh_filename, use_cache=False) Br = Reflective_boundary(domain) Bd = Dirichlet_boundary([3, 0, 0]) domain.set_boundary({'wall': Br, 'wave': Bd}) yieldstep = 0.1 finaltime = 10 for t in domain.evolve(yieldstep, finaltime): domain.write_time()
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())
def test_set_elevation_operator_negative_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', lambda x, y: -2 * x) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) stage_c = domain.quantities['stage'].centroid_values elev_c = domain.quantities['elevation'].centroid_values height_c = stage_c - elev_c integral0 = num.sum(height_c) 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']) elev = -5.0 operator = Set_elevation_operator(domain, elevation=elev, indices=indices) # Apply Operator domain.timestep = 2.0 operator() height_c = stage_c - elev_c integral1 = num.sum(height_c) assert integral0 == integral1 elev_ex = [-4.88888889, -4.77777778, -5.77777778, -4.88888889] stage_ex = [-2.55555556, -1.11111111, 0.55555556, -2.55555556] # 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['elevation'].centroid_values, elev_ex) 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(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)
def generate_merimbula_domain(gpu=True): #----------------------------------------------------------------------- # Import necessary modules #----------------------------------------------------------------------- import os import sys import time import numpy as num #------------------------ # ANUGA Modules #------------------------ from anuga import Domain from anuga import Reflective_boundary from anuga import Dirichlet_boundary from anuga import Time_boundary from anuga import Transmissive_boundary from anuga import rectangular_cross from anuga import create_domain_from_file from anuga_cuda import GPU_domain, merimbula_dir #----------------------------------------------------------------------- # Setup parameters #----------------------------------------------------------------------- #mesh_filename = "merimbula_10785.tsh" ; x0 = 756000.0 ; x1 = 756500.0 mesh_filename = "merimbula_43200.tsh" x0 = 756000.0 x1 = 756500.0 #mesh_filename = "test-100.tsh" ; x0 = 0.25 ; x1 = 0.5 #mesh_filename = "test-20.tsh" ; x0 = 250.0 ; x1 = 350.0 mesh_filename = merimbula_dir + mesh_filename yieldstep = 50 finaltime = 500 verbose = True #----------------------------------------------------------------------- # Setup procedures #----------------------------------------------------------------------- class Set_Stage: """Set an initial condition with constant water height, for x0<x<x1 """ def __init__(self, x0=0.25, x1=0.5, h=1.0): self.x0 = x0 self.x1 = x1 self.h = h def __call__(self, x, y): return self.h * ((x > self.x0) & (x < self.x1)) + 1.0 class Set_Elevation: """Set an elevation """ def __init__(self, h=1.0): self.x0 = x0 self.x1 = x1 self.h = h def __call__(self, x, y): return x / self.h #-------------------------------------------------------------------------- # Setup Domain only on processor 0 #-------------------------------------------------------------------------- if not gpu: domain = create_domain_from_file(mesh_filename) else: domain = create_domain_from_file(mesh_filename, GPU_domain) domain.using_gpu = False domain.set_quantity('stage', Set_Stage(x0, x1, 2.0)) domain.set_datadir('Data') domain.set_name('merimbula_new') domain.set_store(True) #domain.set_quantity('elevation', Set_Elevation(500.0)) #print domain.statistics() #print domain.get_extent() #print domain.get_extent(absolute=True) #print domain.geo_reference #-------------------------------------------------------------------------- # Distribute sequential domain on processor 0 to other processors #-------------------------------------------------------------------------- #if myid == 0 and verbose: print 'DISTRIBUTING DOMAIN' #domain = distribute(domain) #-------------------------------------------------------------------------- # On all processors, setup evolve parameters for domains on all processors # (all called "domain" #-------------------------------------------------------------------------- #domain.set_flow_algorithm('2_0') #domain.smooth = False #domain.set_default_order(2) #domain.set_timestepping_method('rk2') #domain.set_CFL(0.7) #domain.set_beta(1.5) #for p in range(numprocs): # if myid == p: # print 'P%d'%p # print domain.get_extent() # print domain.get_extent(absolute=True) # print domain.geo_reference # print domain.s2p_map # print domain.p2s_map # print domain.tri_l2g # print domain.node_l2g # else: # pass # # barrier() #------------------------------------------------------------------------------ # Setup boundary conditions # This must currently happen *after* domain has been distributed #------------------------------------------------------------------------------ Br = Reflective_boundary(domain) # Solid reflective wall domain.set_boundary({ 'outflow': Br, 'inflow': Br, 'inner': Br, 'exterior': Br, 'open': Br }) return domain
def evolution_test(parallel=False): domain = create_domain_from_file(mesh_filename) domain.set_quantity('stage', Set_Stage(756000.0, 756500.0, 2.0)) #-------------------------------------------------------------------------- # Create parallel domain if requested #-------------------------------------------------------------------------- if parallel: if par.myid == 0 and verbose: print 'DISTRIBUTING PARALLEL DOMAIN' domain = distribute(domain) #------------------------------------------------------------------------------ # Setup boundary conditions # This must currently happen *after* domain has been distributed #------------------------------------------------------------------------------ domain.store = False Br = Reflective_boundary(domain) # Solid reflective wall domain.set_boundary({ 'outflow': Br, 'inflow': Br, 'inner': Br, 'exterior': Br, 'open': Br }) #------------------------------------------------------------------------------ # Setup diagnostic arrays #------------------------------------------------------------------------------ l1list = [] l2list = [] linflist = [] l1norm = num.zeros(3, num.float) l2norm = num.zeros(3, num.float) linfnorm = num.zeros(3, num.float) recv_norm = num.zeros(3, num.float) #------------------------------------------------------------------------------ # Evolution #------------------------------------------------------------------------------ if parallel: if par.myid == 0 and verbose: print 'PARALLEL EVOLVE' else: if verbose: print 'SEQUENTIAL EVOLVE' for t in domain.evolve(yieldstep=yieldstep, finaltime=finaltime): edges = domain.quantities[quantity].edge_values.take(num.flatnonzero( domain.tri_full_flag), axis=0) l1norm[0] = l1_norm(edges[:, 0]) l1norm[1] = l1_norm(edges[:, 1]) l1norm[2] = l1_norm(edges[:, 2]) l2norm[0] = l2_norm(edges[:, 0]) l2norm[1] = l2_norm(edges[:, 1]) l2norm[2] = l2_norm(edges[:, 2]) linfnorm[0] = linf_norm(edges[:, 0]) linfnorm[1] = linf_norm(edges[:, 1]) linfnorm[2] = linf_norm(edges[:, 2]) if parallel: l2norm[0] = pow(l2norm[0], 2) l2norm[1] = pow(l2norm[1], 2) l2norm[2] = pow(l2norm[2], 2) if par.myid == 0: #domain.write_time() #print edges[:,1] for p in range(1, par.numprocs): recv_norm = par.receive(p) l1norm += recv_norm recv_norm = par.receive(p) l2norm += recv_norm recv_norm = par.receive(p) linfnorm[0] = max(linfnorm[0], recv_norm[0]) linfnorm[1] = max(linfnorm[1], recv_norm[1]) linfnorm[2] = max(linfnorm[2], recv_norm[2]) l2norm[0] = pow(l2norm[0], 0.5) l2norm[1] = pow(l2norm[1], 0.5) l2norm[2] = pow(l2norm[2], 0.5) l1list.append(l1norm) l2list.append(l2norm) linflist.append(linfnorm) else: par.send(l1norm, 0) par.send(l2norm, 0) par.send(linfnorm, 0) else: #domain.write_time() l1list.append(l1norm) l2list.append(l2norm) linflist.append(linfnorm) return (l1list, l2list, linflist)
t2 = time.time() if myid == 0 : print 'Distribute domain: Time ',t2-t1 if myid == 0 : print 'after parallel domain' domain.set_name('sw_rectangle') #Boundaries T = Transmissive_boundary(domain) R = Reflective_boundary(domain) domain.set_boundary( {'left': R, 'right': R, 'bottom': R, 'top': R, 'ghost': None} ) if myid == 0 : print 'after set_boundary' #domain.check_integrity() if myid == 0 : print 'after check_integrity' class Set_Stage: """Set an initial condition with constant water height, for x<x0
def test_set_elevation_operator_center_radius_de1(self): from math import pi, cos, sin length = 2.0 width = 2.0 dx = dy = 0.5 domain = rectangular_cross_domain(int(old_div(length, dx)), int(old_div(width, dy)), len1=length, len2=width) #Flat surface with 1m of water domain.set_flow_algorithm('DE1') domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) R = Reflective_boundary(domain) domain.set_boundary({'left': R, 'right': R, 'bottom': R, 'top': R}) from pprint import pprint #pprint(domain.quantities['stage'].centroid_values) # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles def elev(t): if t < 10.0: return 5.0 else: return 7.0 operator = Set_elevation_operator(domain, elevation=elev, center=[1.0, 1.0], radius=1.0) # Apply Operator at time t=1.0 domain.set_time(1.0) operator() #pprint(domain.quantities['elevation'].centroid_values) elev_ex = [ 0., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 5., 5., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 0. ] #pprint(domain.quantities['stage'].centroid_values) stage_ex = [ 1., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 6., 6., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 1. ] # from pprint import pprint # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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) # Apply Operator at time t=15.0 domain.set_time(15.0) operator() #pprint(domain.quantities['elevation'].centroid_values) elev_ex = [ 0., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 7., 7., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 0. ] #pprint(domain.quantities['stage'].centroid_values) stage_ex = [ 1., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 8., 8., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 1. ] # from pprint import pprint # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # pprint (domain.quantities['xmomentum'].centroid_values) # pprint (domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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) operator = Set_elevation(domain, elevation=0.0) #print operator.value_type operator() #from pprint import pprint # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # pprint (domain.quantities['xmomentum'].centroid_values) # pprint (domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['elevation'].centroid_values, 0.0) assert num.allclose(domain.quantities['stage'].centroid_values, 1.0) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) operator = Set_elevation(domain, elevation=lambda t: t, indices=[0, 1, 3]) operator() #pprint (domain.quantities['elevation'].centroid_values) #pprint (domain.quantities['stage'].centroid_values) elev_ex = [ 15., 15., 0., 15., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. ] stage_ex = [ 16., 16., 1., 16., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1. ] # from pprint import pprint # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # pprint (domain.quantities['xmomentum'].centroid_values) # pprint (domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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_circular_elevation_operator_large_function(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin length = 2.0 width = 2.0 dx = dy = 0.5 domain = rectangular_cross_domain(int(old_div(length, dx)), int(old_div(width, dy)), len1=length, len2=width) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) R = Reflective_boundary(domain) domain.set_boundary({'left': R, 'right': R, 'bottom': R, 'top': R}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles def elev(t): if t < 10.0: return 5.0 else: return 7.0 operator = Circular_set_elevation_operator(domain, elevation=elev, center=[1.0, 1.0], radius=1.0) # Apply Operator at time t=1.0 domain.set_time(1.0) operator() elev_ex = [ 0., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 5., 5., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 0., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 0., 0. ] stage_ex = [ 1., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 6., 6., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 1., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 1., 1. ] # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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) # Apply Operator at time t=15.0 domain.set_time(15.0) operator() elev_ex = [ 0., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 7., 7., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 0., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 0., 0. ] stage_ex = [ 1., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 8., 8., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 1., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 1., 1. ] # pprint (domain.quantities['elevation'].centroid_values) # pprint (domain.quantities['stage'].centroid_values) # pprint (domain.quantities['xmomentum'].centroid_values) # pprint (domain.quantities['ymomentum'].centroid_values) assert num.allclose(domain.quantities['elevation'].centroid_values, elev_ex) 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_large_mesh_function(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin length = 2.0 width = 2.0 dx = dy = 0.5 #dx = dy = 0.1 domain = rectangular_cross_domain(int(old_div(length, dx)), int(old_div(width, dy)), len1=length, len2=width) #Flat surface with 1m of water domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) R = Reflective_boundary(domain) domain.set_boundary({'left': R, 'right': R, 'bottom': R, 'top': R}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values def stage(t): if t < 10.0: return 5.0 else: return 7.0 polygon = [(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)] #operator = Polygonal_set_stage_operator(domain, stage=stage, polygon=polygon) operator = operator = Set_quantity(domain, 'stage', value=stage, polygon=polygon, test_stage=False) #operator.plot_region() # Apply Operator at time t=1.0 domain.set_time(1.0) operator() stage_ex_expanded = \ [ 1., 1., 5., 5., 1., 5., 5., 5., 1., 5., 5., 5., 1., 5., 5., 1., 5., 1., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 1., 5., 1., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 1., 5., 1., 1., 5., 5., 5., 1., 5., 5., 5., 1., 5., 5., 5., 1., 1.] stage_ex = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] # print domain.quantities['elevation'].centroid_values # pprint(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) # Apply Operator at time t=15.0 domain.set_time(15.0) operator() stage_ex_expanded = \ [ 1., 1., 7., 7., 1., 7., 7., 7., 1., 7., 7., 7., 1., 7., 7., 1., 7., 1., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 1., 7., 1., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 1., 7., 1., 1., 7., 7., 7., 1., 7., 7., 7., 1., 7., 7., 7., 1., 1.] stage_ex = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] # pprint(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 sequential_time_varying_file_boundary_sts(self): """sequential_ltest_time_varying_file_boundary_sts_sequential(self): Read correct points from ordering file and apply sts to boundary. The boundary is time varying. FIXME add to test_urs2sts. """ lat_long_points = [[6.01, 97.0], [6.02, 97.0], [6.05, 96.9], [6.0, 97.0]] bounding_polygon = [[6.0, 97.0], [6.01, 97.0], [6.02, 97.0], [6.02, 97.02], [6.00, 97.02]] tide = 3.0 time_step_count = 65 time_step = 2. n = len(lat_long_points) first_tstep = num.ones(n, num.int) last_tstep = (time_step_count) * num.ones(n, num.int) finaltime = num.float(time_step * (time_step_count - 1)) yieldstep = num.float(time_step) gauge_depth = 20 * num.ones(n, num.float) ha = 2 * num.ones((n, time_step_count), num.float) ua = 10 * num.ones((n, time_step_count), num.float) va = -10 * num.ones((n, time_step_count), num.float) times = num.arange(0., num.float(time_step_count * time_step), time_step) for i in range(n): #ha[i]+=num.sin(times) ha[i] += times / finaltime sts_file = "test" if myid == 0: base_name, files = self.write_mux2(lat_long_points, time_step_count, time_step, first_tstep, last_tstep, depth=gauge_depth, ha=ha, ua=ua, va=va) # base name will not exist, but 3 other files are created # Write order file file_handle, order_base_name = tempfile.mkstemp("") os.close(file_handle) os.remove(order_base_name) d = "," order_file = order_base_name + 'order.txt' fid = open(order_file, 'w') # Write Header header = 'index, longitude, latitude\n' fid.write(header) indices = [3, 0, 1] for i in indices: line=str(i)+d+str(lat_long_points[i][1])+d+\ str(lat_long_points[i][0])+"\n" fid.write(line) fid.close() urs2sts(base_name, basename_out=sts_file, ordering_filename=order_file, mean_stage=tide, verbose=verbose) self.delete_mux(files) assert (os.access(sts_file + '.sts', os.F_OK)) os.remove(order_file) barrier() boundary_polygon = create_sts_boundary(sts_file) # Append the remaining part of the boundary polygon to be defined by # the user bounding_polygon_utm = [] for point in bounding_polygon: zone, easting, northing = redfearn(point[0], point[1]) bounding_polygon_utm.append([easting, northing]) boundary_polygon.append(bounding_polygon_utm[3]) boundary_polygon.append(bounding_polygon_utm[4]) assert num.allclose(bounding_polygon_utm, boundary_polygon) extent_res = 1000000 meshname = 'urs_test_mesh' + '.tsh' interior_regions = None boundary_tags = {'ocean': [0, 1], 'otherocean': [2, 3, 4]} # have to change boundary tags from last example because now bounding # polygon starts in different place. if myid == 0: create_mesh_from_regions(boundary_polygon, boundary_tags=boundary_tags, maximum_triangle_area=extent_res, filename=meshname, interior_regions=interior_regions, verbose=verbose) barrier() domain_fbound = Domain(meshname) domain_fbound.set_quantities_to_be_stored(None) domain_fbound.set_quantity('stage', tide) if verbose: print "Creating file boundary condition" Bf = File_boundary(sts_file + '.sts', domain_fbound, boundary_polygon=boundary_polygon) Br = Reflective_boundary(domain_fbound) domain_fbound.set_boundary({'ocean': Bf, 'otherocean': Br}) temp_fbound = num.zeros(int(finaltime / yieldstep) + 1, num.float) if verbose: print "Evolving domain with file boundary condition" for i, t in enumerate( domain_fbound.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step=False)): temp_fbound[i] = domain_fbound.quantities['stage'].centroid_values[ 2] if verbose: domain_fbound.write_time() domain_drchlt = Domain(meshname) domain_drchlt.set_quantities_to_be_stored(None) domain_drchlt.set_starttime(time_step) domain_drchlt.set_quantity('stage', tide) Br = Reflective_boundary(domain_drchlt) #Bd = Dirichlet_boundary([2.0+tide,220+10*tide,-220-10*tide]) Bd = Time_boundary( domain=domain_drchlt, f=lambda t: [ 2.0 + t / finaltime + tide, 220. + 10. * tide + 10. * t / finaltime, -220. - 10. * tide - 10. * t / finaltime ]) #Bd = Time_boundary(domain=domain_drchlt,f=lambda t: [2.0+num.sin(t)+tide,10.*(2+20.+num.sin(t)+tide),-10.*(2+20.+num.sin(t)+tide)]) domain_drchlt.set_boundary({'ocean': Bd, 'otherocean': Br}) temp_drchlt = num.zeros(int(finaltime / yieldstep) + 1, num.float) for i, t in enumerate( domain_drchlt.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step=False)): temp_drchlt[i] = domain_drchlt.quantities['stage'].centroid_values[ 2] #domain_drchlt.write_time() #print domain_fbound.quantities['stage'].vertex_values #print domain_drchlt.quantities['stage'].vertex_values assert num.allclose(temp_fbound, temp_drchlt), temp_fbound - temp_drchlt assert num.allclose(domain_fbound.quantities['stage'].vertex_values, domain_drchlt.quantities['stage'].vertex_values) assert num.allclose( domain_fbound.quantities['xmomentum'].vertex_values, domain_drchlt.quantities['xmomentum'].vertex_values) assert num.allclose( domain_fbound.quantities['ymomentum'].vertex_values, domain_drchlt.quantities['ymomentum'].vertex_values) if not sys.platform == 'win32': if myid == 0: os.remove(sts_file + '.sts') if myid == 0: os.remove(meshname)
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())
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 run_simulation(parallel=False, G = None, seq_interpolation_points=None, verbose=False): #-------------------------------------------------------------------------- # Setup computational domain and quantities #-------------------------------------------------------------------------- domain = rectangular_cross_domain(M, N) 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_low_froude(1) domain.set_name('runup') # Set sww filename domain.set_datadir('.') # Set output dir #-------------------------------------------------------------------------- # Create the parallel domain #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose : print('DISTRIBUTING PARALLEL DOMAIN') domain = distribute(domain, verbose=False) #-------------------------------------------------------------------------- # Setup domain parameters #-------------------------------------------------------------------------- domain.set_quantities_to_be_stored(None) #------------------------------------------------------------------------------ # Setup boundary conditions # This must currently happen *AFTER* domain has been distributed #------------------------------------------------------------------------------ Br = Reflective_boundary(domain) # Solid reflective wall Bd = Dirichlet_boundary([-0.2,0.,0.]) # Constant boundary values # Associate boundary tags with boundary objects domain.set_boundary({'left': Br, 'right': Bd, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Find which sub_domain in which the interpolation points are located # # Sometimes the interpolation points sit exactly # between two centroids, so in the parallel run we # reset the interpolation points to the centroids # found in the sequential run #------------------------------------------------------------------------------ interpolation_points = [[0.4,0.5], [0.6,0.5], [0.8,0.5], [0.9,0.5]] gauge_values = [] tri_ids = [] for i, point in enumerate(interpolation_points): gauge_values.append([]) # Empty list for timeseries #if is_inside_polygon(point, domain.get_boundary_polygon()): #print "Point ", myid, i, point try: k = domain.get_triangle_containing_point(point) if domain.tri_full_flag[k] == 1: tri_ids.append(k) else: tri_ids.append(-1) except: tri_ids.append(-2) #print " tri_ids ",myid, i, tri_ids[-1] if verbose: print('P%d has points = %s' %(myid, tri_ids)) c_coord = domain.get_centroid_coordinates() interpolation_points = [] for id in tri_ids: if id<1: if verbose: print('WARNING: Interpolation point not within the domain!') interpolation_points.append(c_coord[id,:]) #------------------------------------------------------------------------------ # Evolve system through time #------------------------------------------------------------------------------ time = [] if parallel: if myid == 0 and verbose: print('PARALLEL EVOLVE') else: if myid == 0 and verbose: print('SEQUENTIAL EVOLVE') for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): if myid == 0 and verbose : domain.write_time() # Record time series at known points time.append(domain.get_time()) stage = domain.get_quantity('stage') for i in range(4): if tri_ids[i] > -1: gauge_values[i].append(stage.centroid_values[tri_ids[i]]) #---------------------------------------- # Setup test arrays during sequential run #---------------------------------------- if not parallel: G = [] for i in range(4): G.append(gauge_values[i]) success = True for i in range(4): if tri_ids[i] > -1: #print num.max(num.array(gauge_values[i])- num.array(G[i])) success = success and num.allclose(gauge_values[i], G[i]) assert_(success) return G, interpolation_points
def test_read_sww(self): """ Save to an sww file and then read back the info. Here we store the info "uniquely" """ #--------------------------------------------------------------------- # Import necessary modules #--------------------------------------------------------------------- from anuga.abstract_2d_finite_volumes.mesh_factory import \ rectangular_cross from anuga.shallow_water.shallow_water_domain import Domain from anuga import Reflective_boundary from anuga.abstract_2d_finite_volumes.generic_boundary_conditions\ import Dirichlet_boundary, Time_boundary #--------------------------------------------------------------------- # Setup computational domain #--------------------------------------------------------------------- length = 8. width = 4. dx = dy = 2 # Resolution: Length of subdivisions on both axes inc = 0.05 # Elevation increment points, vertices, boundary = rectangular_cross(int(length/dx), int(width/dy), len1=length, len2=width) domain = Domain(points, vertices, boundary) domain.set_name('read_sww_test'+str(domain.processor)) # Output name domain.set_quantities_to_be_stored({'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2, 'friction': 1}) domain.set_store_vertices_uniquely(True) #--------------------------------------------------------------------- # Setup initial conditions #--------------------------------------------------------------------- domain.set_quantity('elevation', 0.0) # Flat bed initially domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', 0.0) # Dry initial condition #------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------ Bi = Dirichlet_boundary([0.4, 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}) #------------------------------------------------------------------- # Evolve system through time #------------------------------------------------------------------- for t in domain.evolve(yieldstep=1, finaltime=4.0): pass # Check that quantities have been stored correctly source = domain.get_name() + '.sww' #x = fid.variables['x'][:] #y = fid.variables['y'][:] #stage = fid.variables['stage'][:] #elevation = fid.variables['elevation'][:] #fid.close() #assert len(stage.shape) == 2 #assert len(elevation.shape) == 2 #M, N = stage.shape sww_file = sww.Read_sww(source) #print 'last frame number',sww_file.get_last_frame_number() assert num.allclose(sww_file.x, domain.get_vertex_coordinates()[:,0]) assert num.allclose(sww_file.y, domain.get_vertex_coordinates()[:,1]) assert num.allclose(sww_file.time, [0.0, 1.0, 2.0, 3.0, 4.0]) M = domain.get_number_of_triangles() assert num.allclose(num.reshape(num.arange(3*M), (M,3)), sww_file.vertices) last_frame_number = sww_file.get_last_frame_number() assert last_frame_number == 4 assert num.allclose(sww_file.get_bounds(), [0.0, length, 0.0, width]) assert 'stage' in sww_file.quantities.keys() assert 'friction' in sww_file.quantities.keys() assert 'elevation' in sww_file.quantities.keys() assert 'xmomentum' in sww_file.quantities.keys() assert 'ymomentum' in sww_file.quantities.keys() for qname, q in sww_file.read_quantities(last_frame_number).items(): #print qname #print num.linalg.norm(num.abs((domain.get_quantity(qname).get_values()-q).flatten()), ord=1) assert num.allclose(domain.get_quantity(qname).get_values(), q) #----------------------------------------- # Start the evolution off again at frame 3 #----------------------------------------- sww_file.read_quantities(last_frame_number-1) points, vertices, boundary = rectangular_cross(int(length/dx), int(width/dy), len1=length, len2=width) new_domain = Domain(points, vertices, boundary) new_domain.set_quantities_to_be_stored(None) new_domain.set_store_vertices_uniquely(True) for qname, q in sww_file.read_quantities(last_frame_number-1).items(): new_domain.set_quantity(qname, q) #------------------------------------------------------------------ # Setup boundary conditions #------------------------------------------------------------------ Bi = Dirichlet_boundary([0.4, 0, 0]) # Inflow Br = Reflective_boundary(new_domain) # Solid reflective wall Bo = Dirichlet_boundary([-5, 0, 0]) # Outflow new_domain.set_boundary({'left': Bi, 'right': Bo, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------- # Evolve system through time #------------------------------------------------------------------- for t in new_domain.evolve(yieldstep=1.0, finaltime=1.0): pass # Compare new_domain and domain quantities for quantity in domain.get_quantity_names(): dv = domain.get_quantity(quantity).get_values() ndv = new_domain.get_quantity(quantity).get_values() #print dv-ndv assert num.allclose( dv, ndv, rtol=5.e-2, atol=5.e-2)
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 #------------------------------------------------------------------------------ print 'Set up Erosion Area to test...' from anuga import Bed_shear_erosion_operator polygon1 = [[10.6, 1.0], [13.7, 1.0], [13.7, 4.0], [10.6, 4.0]] # create operator op1 = Bed_shear_erosion_operator(domain, threshold=2.0,
def test_set_stage_operator_line(self): from math import pi, cos, sin length = 2.0 width = 2.0 dx = dy = 0.5 #dx = dy = 0.1 domain = rectangular_cross_domain(int(old_div(length, dx)), int(old_div(width, dy)), len1=length, len2=width) #Flat surface with 1m of water domain.set_quantity('elevation', -10.0) domain.set_quantity('stage', -1.0) domain.set_quantity('friction', 0) R = Reflective_boundary(domain) domain.set_boundary({'left': R, 'right': R, 'bottom': R, 'top': R}) # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values def stage(x, y, t): #print x,y if t < 10.0: return x else: return y line = [(0.5, 0.5), (1.5, 0.75)] operator = Set_quantity(domain, 'stage', value=stage, line=line, test_stage=False) #print 'stage at 1',stage(3.0,4.0,1.0) # return x value #print 'stage at 15', stage(3.0,4.0,15.0) # return y value #print operator.indices #print operator.value_type # Apply Operator at time t=1.0 domain.set_time(1.0) operator() stage_ex = [ -1., -1., 0.41666667, 0.25, -1., 0.25, 0.41666667, -1., -1., -1., -1., -1., -1., -1., -1., -1., 0.58333333, -1., -1., 0.75, 0.58333333, 0.75, 0.91666667, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1.08333333, 1.25, 1.41666667, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1.58333333, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1. ] #from pprint import pprint # pprint(domain.quantities['stage'].centroid_values) # pprint(domain.quantities['stage'].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) # Apply Operator at time t=15.0 domain.set_time(15.0) operator() stage_ex = [ -1., -1., 0.25, 0.41666667, -1., 0.58333333, 0.75, -1., -1., -1., -1., -1., -1., -1., -1., -1., 0.25, -1., -1., 0.41666667, 0.75, 0.58333333, 0.75, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 0.75, 0.58333333, 0.75, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 0.75, -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1. ] Plot = False if Plot: operator.plot_region() cellsize = 0.01 domain.quantities[ 'stage'].extrapolate_second_order_and_limit_by_vertex() from pprint import pprint pprint(domain.quantities['stage'].centroid_values) x, y, z = domain.quantities['stage'].save_to_array( cellsize=cellsize, smooth=False) #pprint(z) import pylab import numpy #a = numpy.where(a == -9999, numpy.nan, a) #a = numpy.where(a > 10.0, numpy.nan, a) #z = z[::-1,:] """ print z print z.shape print x print y """ nrows = z.shape[0] ncols = z.shape[1] ratio = float(nrows) / float(ncols) print(ratio) #y = numpy.arange(nrows)*cellsize #x = numpy.arange(ncols)*cellsize #Setup fig size to correpond to array size fig = pylab.figure(figsize=(10, 10 * ratio)) levels = numpy.arange(-2.0, 2, 0.01) CF = pylab.contourf(x, y, z, levels=levels) CB = pylab.colorbar(CF, shrink=0.8, extend='both') #CC = pylab.contour(x,y,a, levels=levels) pylab.show() from pprint import pprint #pprint(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(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 test_set_elevation_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) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) stage_c = domain.quantities['stage'].centroid_values elev_c = domain.quantities['elevation'].centroid_values height_c = stage_c - elev_c integral0 = num.sum(height_c) 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] elev = 3.0 operator = Set_elevation_operator(domain, elevation=elev, indices=indices) # Apply Operator domain.timestep = 2.0 operator() height_c = stage_c - elev_c integral1 = num.sum(height_c) assert integral0 == integral1 stage_ex = [3.66666667, 3.33333333, 2.33333333, 3.66666667] elev_ex = [2.66666667, 2.33333333, 1.33333333, 2.66666667] # 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['elevation'].centroid_values, elev_ex) 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)