def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- # Setup computational domain and quantities #-------------------------------------------------------------------------- domain = rectangular_cross_domain(M, N) domain.set_name('odomain') # Set sww filename domain.set_datadir('.') 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_quantities_to_be_stored({'elevation': 2,'stage': 2,'xmomentum': 2,'ymomentum': 2}) domain.set_store_vertices_uniquely(False) domain.set_flow_algorithm('DE1') georef = Geo_reference(zone=56,xllcorner=100000.0,yllcorner=200000.0) domain.set_georeference(georef) #-------------------------------------------------------------------------- # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: if verbose: print 'DUMPING PARTITION DATA' sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- # Create the parallel domains #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose : print 'DISTRIBUTING TO PARALLEL DOMAIN' pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') if myid == 0 and verbose : print 'LOADING IN PARALLEL DOMAIN' sdomain = sequential_distribute_load(filename='odomain', verbose = verbose) sdomain.set_name('sdomain') assert domain.get_datadir() == pdomain.get_datadir() assert domain.get_store() == pdomain.get_store() assert domain.get_store_centroids() == pdomain.get_store_centroids() assert domain.smooth == pdomain.smooth assert domain.reduction == pdomain.reduction assert domain.minimum_storable_height == pdomain.minimum_storable_height assert domain.get_flow_algorithm() == pdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height() == pdomain.get_minimum_allowed_height() assert domain.geo_reference == pdomain.geo_reference assert domain.get_datadir() == sdomain.get_datadir() assert domain.get_store() == sdomain.get_store() assert domain.get_store_centroids() == sdomain.get_store_centroids() assert domain.smooth == sdomain.smooth assert domain.reduction == sdomain.reduction assert domain.minimum_storable_height == sdomain.minimum_storable_height assert domain.get_flow_algorithm() == sdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height() == sdomain.get_minimum_allowed_height() assert domain.geo_reference == sdomain.geo_reference
t0 = time.time() verbose = True #-------------------------------------------------------------------------- # Setup Domain only on processor 0 #-------------------------------------------------------------------------- if myid == 0: length = 2.0 width = 2.0 #dx = dy = 0.005 #dx = dy = 0.00125 dx = dy = 0.5 domain = rectangular_cross_domain(int(length/dx), int(width/dy), len1=length, len2=width, origin=(-length/2, -width/2), verbose=verbose) domain.set_store(True) domain.set_quantity('elevation', lambda x,y : -1.0-x ) domain.set_quantity('stage', 1.0) domain.set_flow_algorithm('DE0') domain.print_statistics() else: domain = None t1 = time.time() if myid == 0 : print 'Create sequential domain: Time',t1-t0
def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- # Setup computational domain and quantities #-------------------------------------------------------------------------- domain = rectangular_cross_domain(M, N) domain.set_name('odomain') # Set sww filename domain.set_datadir('.') 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_quantities_to_be_stored({'elevation': 2,'stage': 2,'xmomentum': 2,'ymomentum': 2}) domain.set_store_vertices_uniquely(False) domain.set_flow_algorithm('DE1') georef = Geo_reference(zone=56,xllcorner=100000.0,yllcorner=200000.0) domain.set_georeference(georef) #-------------------------------------------------------------------------- # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: if verbose: print('DUMPING PARTITION DATA') sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- # Create the parallel domains #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose : print('DISTRIBUTING TO PARALLEL DOMAIN') pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') if myid == 0 and verbose : print('LOADING IN PARALLEL DOMAIN') sdomain = sequential_distribute_load(filename='odomain', verbose = verbose) sdomain.set_name('sdomain') if myid == 0 and verbose : print('TESTING AGAINST SEQUENTIAL DOMAIN') assert domain.get_datadir() == pdomain.get_datadir() assert domain.get_store() == pdomain.get_store() assert domain.get_store_centroids() == pdomain.get_store_centroids() assert domain.smooth == pdomain.smooth assert domain.reduction == pdomain.reduction assert domain.minimum_storable_height == pdomain.minimum_storable_height assert domain.get_flow_algorithm() == pdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height() == pdomain.get_minimum_allowed_height() assert domain.geo_reference == pdomain.geo_reference assert domain.get_datadir() == sdomain.get_datadir() assert domain.get_store() == sdomain.get_store() assert domain.get_store_centroids() == sdomain.get_store_centroids() assert domain.smooth == sdomain.smooth assert domain.reduction == sdomain.reduction assert domain.minimum_storable_height == sdomain.minimum_storable_height assert domain.get_flow_algorithm() == sdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height() == sdomain.get_minimum_allowed_height() assert domain.geo_reference == sdomain.geo_reference if myid == 0 and verbose : print('REMOVING DATA FILES') if myid == 0: import os #os.remove('odomain.sww') #os.remove('pdomain.sww') #os.remove('sdomain.sww') try: os.remove('odomain_P4_0.pickle') os.remove('odomain_P4_1.pickle') os.remove('odomain_P4_2.pickle') os.remove('odomain_P4_3.pickle') import glob [ os.remove(fl) for fl in glob.glob('*.npy') ] except: if verbose: print('remove files failed') if myid == 0 and verbose : print('FINISHED')
from anuga import myid, finalize, distribute args = anuga.get_args() alg = args.alg verbose = args.verbose scale_me = 1.0 #------------------------- # create Sequential domain #------------------------- if myid == 0: #--------- #Setup computational domain #--------- domain = anuga.rectangular_cross_domain(40, 40, len1=1., len2=1.) domain.set_flow_algorithm(alg) domain.set_name('runup_sinusoid') # Output to file runup.sww domain.set_datadir('.') # Use current folder #------------------ # Define topography #------------------ def topography(x, y): return (-x / 2.0 + 0.05 * numpy.sin((x + y) * 50.0)) * scale_me def stagefun(x, y): stge = -0.2 * scale_me return stge
#-------- import anuga import numpy from math import sin, pi, exp, sqrt from anuga import Domain from anuga import myid, finalize, distribute args = anuga.get_args() alg = args.alg verbose = args.verbose if myid == 0: #--------- #Setup computational domain #--------- domain = anuga.rectangular_cross_domain(15,15, len1=1., len2=1.) domain.set_flow_algorithm(alg) domain.set_name('dimensional_lid_driven') # Output to file runup.sww domain.set_datadir('.') # Use current folder #------------------ # Define topography #------------------ domain.set_quantity('elevation',0.0) # Use function for elevation domain.set_quantity('friction',0.0) # Constant friction domain.set_quantity('stage',1.) # Constant negative initial stage else: domain = None
def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- # Setup computational domain and quantities #-------------------------------------------------------------------------- if myid == 0: domain = rectangular_cross_domain(M, N) domain.set_name('odomain') # Set sww filename domain.set_datadir('.') 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 else: domain = None #-------------------------------------------------------------------------- # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: if verbose: print 'DUMPING PARTITION DATA' sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- # Create the parallel domains #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose : print 'DISTRIBUTING TO PARALLEL DOMAIN' pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') if myid == 0 and verbose : print 'LOADING IN PARALLEL DOMAIN' sdomain = sequential_distribute_load(filename='odomain', verbose = verbose) sdomain.set_name('sdomain') if myid == 0 and verbose: print 'EVOLVING pdomain' setup_and_evolve(pdomain, verbose=verbose) if myid == 0 and verbose: print 'EVOLVING sdomain' setup_and_evolve(sdomain, verbose=verbose) if myid == 0: if verbose: print 'EVOLVING odomain' setup_and_evolve(domain, verbose=verbose) if myid == 0 and verbose: parameter_file=open('odomain.txt', 'w') from pprint import pprint pprint(domain.get_algorithm_parameters(),parameter_file,indent=4) parameter_file.close() parameter_file=open('sdomain.txt', 'w') from pprint import pprint pprint(sdomain.get_algorithm_parameters(),parameter_file,indent=4) parameter_file.close() parameter_file=open('pdomain.txt', 'w') from pprint import pprint pprint(pdomain.get_algorithm_parameters(),parameter_file,indent=4) parameter_file.close() assert num.allclose(pdomain.quantities['stage'].centroid_values, sdomain.quantities['stage'].centroid_values) assert num.allclose(pdomain.quantities['stage'].vertex_values, sdomain.quantities['stage'].vertex_values) assert num.allclose(pdomain.vertex_coordinates, sdomain.vertex_coordinates) assert num.allclose(pdomain.centroid_coordinates, sdomain.centroid_coordinates) #--------------------------------- # Now compare the merged sww files #--------------------------------- if myid == 0: if verbose: print 'COMPARING SWW FILES' odomain_v = util.get_output('odomain.sww') odomain_c = util.get_centroids(odomain_v) pdomain_v = util.get_output('pdomain.sww') pdomain_c = util.get_centroids(pdomain_v) sdomain_v = util.get_output('sdomain.sww') sdomain_c = util.get_centroids(sdomain_v) # Test some values against the original ordering if verbose: order = 2 print 'PDOMAIN CENTROID VALUES' print num.linalg.norm(odomain_c.x-pdomain_c.x,ord=order) print num.linalg.norm(odomain_c.y-pdomain_c.y,ord=order) print num.linalg.norm(odomain_c.stage[-1]-pdomain_c.stage[-1],ord=order) print num.linalg.norm(odomain_c.xmom[-1]-pdomain_c.xmom[-1],ord=order) print num.linalg.norm(odomain_c.ymom[-1]-pdomain_c.ymom[-1],ord=order) print num.linalg.norm(odomain_c.xvel[-1]-pdomain_c.xvel[-1],ord=order) print num.linalg.norm(odomain_c.yvel[-1]-pdomain_c.yvel[-1],ord=order) print 'SDOMAIN CENTROID VALUES' print num.linalg.norm(odomain_c.x-sdomain_c.x,ord=order) print num.linalg.norm(odomain_c.y-sdomain_c.y,ord=order) print num.linalg.norm(odomain_c.stage[-1]-sdomain_c.stage[-1],ord=order) print num.linalg.norm(odomain_c.xmom[-1]-sdomain_c.xmom[-1],ord=order) print num.linalg.norm(odomain_c.ymom[-1]-sdomain_c.ymom[-1],ord=order) print num.linalg.norm(odomain_c.xvel[-1]-sdomain_c.xvel[-1],ord=order) print num.linalg.norm(odomain_c.yvel[-1]-sdomain_c.yvel[-1],ord=order) print 'PDOMAIN VERTEX VALUES' print num.linalg.norm(odomain_v.stage[-1]-pdomain_v.stage[-1],ord=order) print num.linalg.norm(odomain_v.xmom[-1]-pdomain_v.xmom[-1],ord=order) print num.linalg.norm(odomain_v.ymom[-1]-pdomain_v.ymom[-1],ord=order) print num.linalg.norm(odomain_v.xvel[-1]-pdomain_v.xvel[-1],ord=order) print num.linalg.norm(odomain_v.yvel[-1]-pdomain_v.yvel[-1],ord=order) print 'SDOMAIN VERTEX VALUES' print num.linalg.norm(odomain_v.stage[-1]-sdomain_v.stage[-1],ord=order) print num.linalg.norm(odomain_v.xmom[-1]-sdomain_v.xmom[-1],ord=order) print num.linalg.norm(odomain_v.ymom[-1]-sdomain_v.ymom[-1],ord=order) print num.linalg.norm(odomain_v.xvel[-1]-sdomain_v.xvel[-1],ord=order) print num.linalg.norm(odomain_v.yvel[-1]-sdomain_v.yvel[-1],ord=order) assert num.allclose(odomain_c.stage,pdomain_c.stage) assert num.allclose(odomain_c.xmom,pdomain_c.xmom) assert num.allclose(odomain_c.ymom,pdomain_c.ymom) assert num.allclose(odomain_c.xvel,pdomain_c.xvel) assert num.allclose(odomain_c.yvel,pdomain_c.yvel) assert num.allclose(odomain_v.x,pdomain_v.x) assert num.allclose(odomain_v.y,pdomain_v.y) assert num.linalg.norm(odomain_v.x-pdomain_v.x,ord=0) == 0 assert num.linalg.norm(odomain_v.y-pdomain_v.y,ord=0) == 0 assert num.linalg.norm(odomain_v.stage[-1]-pdomain_v.stage[-1],ord=0) < 100 assert num.linalg.norm(odomain_v.xmom[-1]-pdomain_v.xmom[-1],ord=0) < 100 assert num.linalg.norm(odomain_v.ymom[-1]-pdomain_v.ymom[-1],ord=0) < 100 assert num.linalg.norm(odomain_v.xvel[-1]-pdomain_v.xvel[-1],ord=0) < 100 assert num.linalg.norm(odomain_v.yvel[-1]-pdomain_v.yvel[-1],ord=0) < 100 assert num.allclose(odomain_c.x,sdomain_c.x) assert num.allclose(odomain_c.y,sdomain_c.y) assert num.allclose(odomain_c.stage,sdomain_c.stage) assert num.allclose(odomain_c.xmom,sdomain_c.xmom) assert num.allclose(odomain_c.ymom,sdomain_c.ymom) assert num.allclose(odomain_c.xvel,sdomain_c.xvel) assert num.allclose(odomain_c.yvel,sdomain_c.yvel) assert num.allclose(odomain_v.x,sdomain_v.x) assert num.allclose(odomain_v.y,sdomain_v.y) order = 0 assert num.linalg.norm(odomain_v.x-sdomain_v.x,ord=order) == 0 assert num.linalg.norm(odomain_v.y-sdomain_v.y,ord=order) == 0 assert num.linalg.norm(odomain_v.stage[-1]-sdomain_v.stage[-1],ord=order) < 100 assert num.linalg.norm(odomain_v.xmom[-1]-sdomain_v.xmom[-1],ord=order) < 100 assert num.linalg.norm(odomain_v.ymom[-1]-sdomain_v.ymom[-1],ord=order) < 100 assert num.linalg.norm(odomain_v.xvel[-1]-sdomain_v.xvel[-1],ord=order) < 100 assert num.linalg.norm(odomain_v.yvel[-1]-sdomain_v.yvel[-1],ord=order) < 100 # COMPARE CENTROID PDOMAIN SDOMAIN assert num.allclose(pdomain_c.x,sdomain_c.x) assert num.allclose(pdomain_c.y,sdomain_c.y) assert num.allclose(pdomain_c.stage[-1],sdomain_c.stage[-1]) assert num.allclose(pdomain_c.xmom[-1],sdomain_c.xmom[-1]) assert num.allclose(pdomain_c.ymom[-1],sdomain_c.ymom[-1]) assert num.allclose(pdomain_c.xvel[-1],sdomain_c.xvel[-1]) assert num.allclose(pdomain_c.yvel[-1],sdomain_c.yvel[-1]) # COMPARE VERTEX PDOMAIN SDOMAIN assert num.allclose(pdomain_v.x,sdomain_v.x) assert num.allclose(pdomain_v.y,sdomain_v.y) assert num.allclose(pdomain_v.stage[-1],sdomain_v.stage[-1]) assert num.allclose(pdomain_v.xmom[-1],sdomain_v.xmom[-1]) assert num.allclose(pdomain_v.ymom[-1],sdomain_v.ymom[-1]) assert num.allclose(pdomain_v.xvel[-1],sdomain_v.xvel[-1]) assert num.allclose(pdomain_v.yvel[-1],sdomain_v.yvel[-1]) import os os.remove('odomain.sww') os.remove('pdomain.sww') os.remove('sdomain.sww') os.remove('odomain_P4_0.pickle') os.remove('odomain_P4_1.pickle') os.remove('odomain_P4_2.pickle') os.remove('odomain_P4_3.pickle')
def run_simulation(parallel=False, verbose=False): #-------------------------------------------------------------------------- # Setup computational domain and quantities #-------------------------------------------------------------------------- domain = rectangular_cross_domain(M, N) domain.set_name('odomain') # Set sww filename domain.set_datadir('.') 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_quantities_to_be_stored({ 'elevation': 2, 'stage': 2, 'xmomentum': 2, 'ymomentum': 2 }) domain.set_store_vertices_uniquely(False) domain.set_flow_algorithm('DE1') georef = Geo_reference(zone=56, xllcorner=100000.0, yllcorner=200000.0) domain.set_georeference(georef) #-------------------------------------------------------------------------- # Create pickled partition #-------------------------------------------------------------------------- if myid == 0: if verbose: print 'DUMPING PARTITION DATA' sequential_distribute_dump(domain, numprocs, verbose=verbose, parameters=new_parameters) #-------------------------------------------------------------------------- # Create the parallel domains #-------------------------------------------------------------------------- if parallel: if myid == 0 and verbose: print 'DISTRIBUTING TO PARALLEL DOMAIN' pdomain = distribute(domain, verbose=verbose, parameters=new_parameters) pdomain.set_name('pdomain') if myid == 0 and verbose: print 'LOADING IN PARALLEL DOMAIN' sdomain = sequential_distribute_load(filename='odomain', verbose=verbose) sdomain.set_name('sdomain') assert domain.get_datadir() == pdomain.get_datadir() assert domain.get_store() == pdomain.get_store() assert domain.get_store_centroids() == pdomain.get_store_centroids() assert domain.smooth == pdomain.smooth assert domain.reduction == pdomain.reduction assert domain.minimum_storable_height == pdomain.minimum_storable_height assert domain.get_flow_algorithm() == pdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height( ) == pdomain.get_minimum_allowed_height() assert domain.geo_reference == pdomain.geo_reference assert domain.get_datadir() == sdomain.get_datadir() assert domain.get_store() == sdomain.get_store() assert domain.get_store_centroids() == sdomain.get_store_centroids() assert domain.smooth == sdomain.smooth assert domain.reduction == sdomain.reduction assert domain.minimum_storable_height == sdomain.minimum_storable_height assert domain.get_flow_algorithm() == sdomain.get_flow_algorithm() assert domain.get_minimum_allowed_height( ) == sdomain.get_minimum_allowed_height() assert domain.geo_reference == sdomain.geo_reference
"""Simple water flow example using ANUGA Water flowing down a channel """ #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ import anuga #------------------------------------------------------------------------------ # Setup computational domain #------------------------------------------------------------------------------ # Create a domain with named boundaries "left", "right", "top" and "bottom" domain = anuga.rectangular_cross_domain(10, 5, len1=10.0, len2=5.0) domain.set_name('channel1') # Output name #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def topography(x, y): return -x/10 # linear bed slope domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', # Dry bed expression='elevation') #------------------------------------------------------------------------------
nu=0.25) # Create domain dx = dy = 10000 L = 800000 W = 800000 # Create topography def topography(x, y): el = -1000 return el domain = anuga.rectangular_cross_domain(int(L / dx), int(W / dy), len1=L, len2=W) domain.set_name('test') domain.set_quantity('elevation', function=topography, location='centroids') def tsunami_function(x, y): import okada params['x'] = x params['y'] = y #import pprint #pprint.pprint(params)
def test_set_elevation_operator_center_radius_1_5(self): from math import pi, cos, sin length = 2.0 width = 2.0 dx = dy = 0.5 domain = rectangular_cross_domain(int(length / dx), int(width / dy), len1=length, len2=width) domain.set_flow_algorithm('1_5') #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}) 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 = [ 2.08333333, 2.08333333, 3.75, 3.75, 4.58333333, 4.58333333, 5., 5., 4.58333333, 5., 5., 4.58333333, 2.08333333, 3.75, 3.75, 2.08333333, 4.58333333, 4.58333333, 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 4.58333333, 5., 5., 4.58333333, 5., 4.58333333, 4.58333333, 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 5., 4.58333333, 4.58333333, 3.75, 2.08333333, 2.08333333, 3.75, 5., 4.58333333, 4.58333333, 5., 5., 5., 4.58333333, 4.58333333, 3.75, 3.75, 2.08333333, 2.08333333 ] #pprint(domain.quantities['stage'].centroid_values) stage_ex = [ 3.08333333, 3.08333333, 4.75, 4.75, 5.58333333, 5.58333333, 6., 6., 5.58333333, 6., 6., 5.58333333, 3.08333333, 4.75, 4.75, 3.08333333, 5.58333333, 5.58333333, 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 5.58333333, 6., 6., 5.58333333, 6., 5.58333333, 5.58333333, 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 6., 5.58333333, 5.58333333, 4.75, 3.08333333, 3.08333333, 4.75, 6., 5.58333333, 5.58333333, 6., 6., 6., 5.58333333, 5.58333333, 4.75, 4.75, 3.08333333, 3.08333333 ] # 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() elev_ex = [ 3.64583333, 3.64583333, 5.97916667, 5.97916667, 6.72916667, 6.72916667, 7., 7., 6.72916667, 7., 7., 6.72916667, 3.64583333, 5.97916667, 5.97916667, 3.64583333, 6.72916667, 6.72916667, 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 6.72916667, 7., 7., 6.72916667, 7., 6.72916667, 6.72916667, 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 7., 6.72916667, 6.72916667, 5.97916667, 3.64583333, 3.64583333, 5.97916667, 7., 6.72916667, 6.72916667, 7., 7., 7., 6.72916667, 6.72916667, 5.97916667, 5.97916667, 3.64583333, 3.64583333 ] stage_ex = [ 4.64583333, 4.64583333, 6.97916667, 6.97916667, 7.72916667, 7.72916667, 8., 8., 7.72916667, 8., 8., 7.72916667, 4.64583333, 6.97916667, 6.97916667, 4.64583333, 7.72916667, 7.72916667, 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 7.72916667, 8., 8., 7.72916667, 8., 7.72916667, 7.72916667, 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 8., 7.72916667, 7.72916667, 6.97916667, 4.64583333, 4.64583333, 6.97916667, 8., 7.72916667, 7.72916667, 8., 8., 8., 7.72916667, 7.72916667, 6.97916667, 6.97916667, 4.64583333, 4.64583333 ] # 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() elev_ex = [ 11.25, 10., 5.625, 6.875, 2.5, 3.125, 0.625, 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.875, 1.25, 0., 0.625, 0.625, 0.625, 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 = [ 12.25, 11., 6.625, 7.875, 3.5, 4.125, 1.625, 1., 1., 1., 1., 1., 1., 1., 1., 1., 2.875, 2.25, 1., 1.625, 1.625, 1.625, 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_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(length/dx), int(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_stage_operator(domain, stage=stage, line=line) #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_set_stage_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 #dx = dy = 0.1 domain = rectangular_cross_domain(int(length/dx), int(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 = Polygonal_set_stage_operator(domain, stage=stage, polygon=polygon) #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)
return stge #*(stge>topo) + (topo)*(stge<=topo) #-------------------------------------------------------------------------- # Setup Domain only on processor 0 #-------------------------------------------------------------------------- myid = 0 numprocs = 3 length = 2.0 width = 2.0 dx = dy = 0.005 # 640,000 dx = dy = 0.05 domain = rectangular_cross_domain(int(length / dx), int(width / dy), len1=length, len2=width, verbose=verbose) print domain.number_of_global_triangles domain.set_store(True) domain.set_flow_algorithm('DE0') domain.set_minimum_allowed_height(0.01) 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() domain.set_name('sw_rectangle')
if x[i] <= x_s: Z[i] = quad(func1, x[i], x_s) #on the left of the shock Z[i] += quad(func2, x_s, L) #on right of the shock else: Z[i] = quad(func2, x[i], L) #on right of the shock #print('Integral = ', -Z[:,0], ' with error = ', Z[:,1]) elevation = -1. * Z[:, 0] return elevation #------------------------------------------------------------------------------ # Setup sequential domain #------------------------------------------------------------------------------ if myid == 0: # structured mesh domain = anuga.rectangular_cross_domain(int(L / dx), int(W / dy), L, W, (0.0, 0.0)) domain.set_name(output_file) domain.set_datadir(output_dir) domain.set_flow_algorithm(alg) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ domain.set_quantity('stage', 2.87870797) domain.set_quantity('elevation', bed) domain.set_quantity('friction', n) else: domain = None
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)
Water flowing down a channel with more complex topography """ #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ import anuga #------------------------------------------------------------------------------ # Setup computational domain #------------------------------------------------------------------------------ length = 40. width = 5. dx = dy = 0.1 # Resolution: Length of subdivisions on both axes domain = anuga.rectangular_cross_domain(int(length/dx), int(width/dy), len1=length, len2=width) domain.set_name('channel3') # Output name domain.print_statistics() #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def topography(x,y): """Complex topography defined by a function of vectors x and y.""" z = -x/10 N = len(x) for i in range(N):
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)
return stge#*(stge>topo) + (topo)*(stge<=topo) #-------------------------------------------------------------------------- # Setup Domain only on processor 0 #-------------------------------------------------------------------------- myid = 0 numprocs = 3 length = 2.0 width = 2.0 dx = dy = 0.005 # 640,000 dx = dy = 0.05 domain = rectangular_cross_domain(int(length/dx), int(width/dy), len1=length, len2=width, verbose=verbose) print domain.number_of_global_triangles domain.set_store(True) domain.set_flow_algorithm('DE0') domain.set_minimum_allowed_height(0.01) 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() domain.set_name('sw_rectangle')
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 #-------------------------------------------------------------------------- # 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_name('runup') # Set sww filename domain.set_datadir('.') # Set output dir domain.set_flow_algorithm('2_0') 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
Water flowing down a channel with changing boundary conditions """ #------------------------------------------------------------------------------ # Import necessary modules #------------------------------------------------------------------------------ import anuga #------------------------------------------------------------------------------ # Setup computational domain #------------------------------------------------------------------------------ length = 10. width = 5. dx = dy = 1. # Resolution: Length of subdivisions on both axes domain = anuga.rectangular_cross_domain(int(length/dx), int(width/dy), len1=length, len2=width) domain.set_name('channel2') # Output name #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ def topography(x,y): return -x/10 # linear bed slope domain.set_quantity('elevation', topography) # Use function for elevation domain.set_quantity('friction', 0.01) # Constant friction domain.set_quantity('stage', expression='elevation') # Dry initial condition
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 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_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)
from math import cos from numpy import zeros, float, where import numpy from time import localtime, strftime, gmtime #------------------------------------------------------------------------------ # Setup domain #------------------------------------------------------------------------------ dx = 100. dy = dx L = 10000. W = L #W = dx # structured mesh domain = anuga.rectangular_cross_domain(int(L / dx), int(W / dy), L, W, (-L / 2.0, -W / 2.0)) domain.set_name() #------------------------------------------------------------------------------ # Setup Algorithm #------------------------------------------------------------------------------ domain.set_flow_algorithm(2.0) #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ h0 = 1000.0 domain.set_quantity('elevation', 0.0) domain.set_quantity('friction', 0.0)
from numpy import zeros, float, where import numpy from time import localtime, strftime, gmtime #------------------------------------------------------------------------------ # Setup domain #------------------------------------------------------------------------------ dx = 1000. dy = dx L = 100000. W = 10*dx #W = dx # structured mesh domain = anuga.rectangular_cross_domain(int(L/dx), int(W/dy), L, W, (0.0, -W/2)) domain.set_name() # based on script name print domain.mesh.statistics(nbins=50) #------------------------------------------------------------------------------ # Setup Algorithm #------------------------------------------------------------------------------ domain.set_flow_algorithm('2_0') #------------------------------------------------------------------------------ # Setup initial conditions #------------------------------------------------------------------------------ h0 = 10.0 h1 = 0.0
elif (averaging == 'min'): out[j] = allTopo[out_indices].min() elif (averaging == 'max'): out[j] = allTopo[out_indices].max() else: raise Exception('Unknown value of averaging') return (out) return elevation_setter # Quick test if __name__ == '__main__': import anuga domain = anuga.rectangular_cross_domain(10, 5, len1=10.0, len2=5.0) # Define a topography function where the spatial scale of variation matches # the scale of a mesh triangle def topography(x, y): return x % 0.5 # Do 'averaging' where 2 <= y <= 3 polygon_for_averaging = [[[0.0, 2.0], [0.0, 3.0], [10.0, 3.0], [10.0, 2.0]]] topography_smooth = make_spatially_averaged_function( topography, domain, approx_grid_spacing=[0.1, 0.1], averaging='min',
from anuga import myid, finalize, distribute args = anuga.get_args() alg = args.alg verbose = args.verbose scale_me=1.0 #------------------------- # create Sequential domain #------------------------- if myid == 0: #--------- #Setup computational domain #--------- domain = anuga.rectangular_cross_domain(40,40, len1=1., len2=1.) domain.set_flow_algorithm(alg) domain.set_name('runup_sinusoid') # Output to file runup.sww domain.set_datadir('.') # Use current folder #------------------ # Define topography #------------------ def topography(x,y): return (-x/2.0 +0.05*numpy.sin((x+y)*50.0))*scale_me def stagefun(x,y): stge=-0.2*scale_me return stge