domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br}) #------------------------------------------------------------------------------ # Evolve system through time #------------------------------------------------------------------------------ polygon1 = [ [10.0, 0.0], [11.0, 0.0], [11.0, 5.0], [10.0, 5.0] ] polygon2 = [ [12.0, 2.0], [13.0, 2.0], [13.0, 3.0], [12.0, 3.0] ] from anuga.operators.rate_operators import Rate_operator op1 = Rate_operator(domain, rate=lambda t: 10.0 if (t>=0.0) else 0.0, polygon=polygon2) op2 = Rate_operator(domain, rate=lambda t: 10.0 if (t>=0.0) else 0.0, radius=0.5, center=(10.0, 3.0)) domain.set_starttime(-0.1) for t in domain.evolve(yieldstep=0.01, finaltime=0.0): domain.print_timestepping_statistics() domain.print_operator_timestepping_statistics() stage = domain.get_quantity('stage') elev = domain.get_quantity('elevation') height = stage - elev print 'integral = ', height.get_integral() for t in domain.evolve(yieldstep=0.1, duration=5.0): domain.print_timestepping_statistics() domain.print_operator_timestepping_statistics()
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_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_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(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_rate_operator_rate_from_file(self): from anuga.config import rho_a, rho_w, eta_w from math import pi, cos, sin a = [0.0, 0.0] b = [0.0, 2.0] c = [2.0, 0.0] d = [0.0, 4.0] e = [2.0, 2.0] f = [4.0, 0.0] points = [a, b, c, d, e, f] # bac, bce, ecf, dbe vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] #--------------------------------- #Typical ASCII file #--------------------------------- finaltime = 1200 filename = 'test_file_function' fid = open(filename + '.txt', 'w') start = time.mktime(time.strptime('2000', '%Y')) dt = 60 #One minute intervals t = 0.0 while t <= finaltime: t_string = time.strftime(time_format, time.gmtime(t+start)) fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600))) t += dt fid.close() #Convert ASCII file to NetCDF (Which is what we really like!) timefile2netcdf(filename+'.txt') #Create file function from time series F = file_function(filename + '.tms', quantities = ['Attribute0', 'Attribute1', 'Attribute2']) #Now try interpolation for i in range(20): t = i*10 q = F(t) #Exact linear intpolation assert num.allclose(q[0], 2*t) if i%6 == 0: assert num.allclose(q[1], t**2) assert num.allclose(q[2], sin(t*pi/600)) #Check non-exact t = 90 #Halfway between 60 and 120 q = F(t) assert num.allclose( (120**2 + 60**2)/2, q[1] ) assert num.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) t = 100 #Two thirds of the way between between 60 and 120 q = F(t) assert num.allclose( 2*120**2/3 + 60**2/3, q[1] ) assert num.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) #os.remove(filename + '.txt') #os.remove(filename + '.tms') domain = Domain(points, vertices) #Flat surface with 1m of water domain.set_quantity('elevation', 0) domain.set_quantity('stage', 1.0) domain.set_quantity('friction', 0) Br = Reflective_boundary(domain) domain.set_boundary({'exterior': Br}) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values # Apply operator to these triangles indices = [0,1,3] rate = file_function(filename + '.tms', quantities=['Attribute1']) # Make starttime of domain consistent with tms file starttime domain.set_starttime(rate.starttime) factor = 1000.0 default_rate= 17.7 operator = Rate_operator(domain, rate=rate, factor=factor, \ indices=indices, default_rate = default_rate) # Apply Operator domain.set_time(360.0) domain.timestep = 1.0 operator() d = domain.get_time()**2 * factor + 1.0 stage_ex0 = [ d, d, 1., d] # print d, domain.get_time(), F(360.0) # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex0) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum()) domain.set_time(-10.0) domain.timestep = 1.0 try: operator() except: pass else: raise Exception('Should have raised an exception, time too early') domain.set_time(1300.0) domain.timestep = 1.0 operator() d = default_rate*factor + d stage_ex1 = [ d, d, 1., d] # print domain.quantities['elevation'].centroid_values # print domain.quantities['stage'].centroid_values # print domain.quantities['xmomentum'].centroid_values # print domain.quantities['ymomentum'].centroid_values assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex1) assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0) assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0) assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())
def 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 parallel_time_varying_file_boundary_sts(self): """ parallel_test_time_varying_file_boundary_sts_sequential(self): Read correct points from ordering file and apply sts to boundary. The boundary is time varying. Compares sequential result with distributed result found using anuga_parallel """ #------------------------------------------------------------ # Define test variables #------------------------------------------------------------ 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, time_step_count * time_step, time_step) for i in range(n): #ha[i]+=num.sin(times) ha[i] += times / finaltime #------------------------------------------------------------ # Write mux data to file then convert to sts format #------------------------------------------------------------ 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() #------------------------------------------------------------ # Define boundary_polygon on each processor. This polygon defines the # urs boundary and lies on a portion of the bounding_polygon #------------------------------------------------------------ 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 = 10000 meshname = 'urs_test_mesh' + '.tsh' interior_regions = None boundary_tags = {'ocean': [0, 1], 'otherocean': [2, 3, 4]} #------------------------------------------------------------ # Create mesh on the master processor and store in file. This file # is read in by each slave processor when needed #------------------------------------------------------------ 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) # print domain_fbound.mesh.get_boundary_polygon() else: domain_fbound = None barrier() if (verbose and myid == 0): print 'DISTRIBUTING PARALLEL DOMAIN' domain_fbound = distribute(domain_fbound) #-------------------------------------------------------------------- # 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 = [[279000, 664000], [280250, 664130], [279280, 665400], [280500, 665000]] interpolation_points = num.array(interpolation_points) #if myid==0: # import pylab as P # boundary_polygon=num.array(boundary_polygon) # P.plot(boundary_polygon[:,0],boundary_polygon[:,1]) # P.plot(interpolation_points[:,0],interpolation_points[:,1],'ko') # P.show() fbound_gauge_values = [] fbound_proc_tri_ids = [] for i, point in enumerate(interpolation_points): fbound_gauge_values.append([]) # Empty list for timeseries try: k = domain_fbound.get_triangle_containing_point(point) if domain_fbound.tri_full_flag[k] == 1: fbound_proc_tri_ids.append(k) else: fbound_proc_tri_ids.append(-1) except: fbound_proc_tri_ids.append(-2) if verbose: print 'P%d has points = %s' % (myid, fbound_proc_tri_ids) #------------------------------------------------------------ # Set boundary conditions #------------------------------------------------------------ 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}) #------------------------------------------------------------ # Evolve the domain on each processor #------------------------------------------------------------ for i, t in enumerate( domain_fbound.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step=False)): stage = domain_fbound.get_quantity('stage') for i in range(4): if fbound_proc_tri_ids[i] > -1: fbound_gauge_values[i].append( stage.centroid_values[fbound_proc_tri_ids[i]]) #------------------------------------------------------------ # Create domain to be run sequntially on each processor #------------------------------------------------------------ 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, function=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,function=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}) drchlt_gauge_values = [] drchlt_proc_tri_ids = [] for i, point in enumerate(interpolation_points): drchlt_gauge_values.append([]) # Empty list for timeseries try: k = domain_drchlt.get_triangle_containing_point(point) if domain_drchlt.tri_full_flag[k] == 1: drchlt_proc_tri_ids.append(k) else: drchlt_proc_tri_ids.append(-1) except: drchlt_proc_tri_ids.append(-2) if verbose: print 'P%d has points = %s' % (myid, drchlt_proc_tri_ids) #------------------------------------------------------------ # Evolve entire domain on each processor #------------------------------------------------------------ for i, t in enumerate( domain_drchlt.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step=False)): stage = domain_drchlt.get_quantity('stage') for i in range(4): drchlt_gauge_values[i].append( stage.centroid_values[drchlt_proc_tri_ids[i]]) #------------------------------------------------------------ # Compare sequential values with parallel values #------------------------------------------------------------ barrier() success = True for i in range(4): if fbound_proc_tri_ids[i] > -1: fbound_gauge_values[i] = num.array(fbound_gauge_values[i]) drchlt_gauge_values[i] = num.array(drchlt_gauge_values[i]) #print i,fbound_gauge_values[i][4] #print i,drchlt_gauge_values[i][4] success = success and num.allclose(fbound_gauge_values[i], drchlt_gauge_values[i]) assert success #, (fbound_gauge_values[i]-drchlt_gauge_values[i]) #assert_(success) if not sys.platform == 'win32': if myid == 0: os.remove(sts_file + '.sts') if myid == 0: os.remove(meshname)
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 parallel_time_varying_file_boundary_sts(self): """ parallel_test_time_varying_file_boundary_sts_sequential(self): Read correct points from ordering file and apply sts to boundary. The boundary is time varying. Compares sequential result with distributed result found using anuga_parallel """ #------------------------------------------------------------ # Define test variables #------------------------------------------------------------ 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, time_step_count*time_step, time_step) for i in range(n): #ha[i]+=num.sin(times) ha[i]+=times/finaltime #------------------------------------------------------------ # Write mux data to file then convert to sts format #------------------------------------------------------------ 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() #------------------------------------------------------------ # Define boundary_polygon on each processor. This polygon defines the # urs boundary and lies on a portion of the bounding_polygon #------------------------------------------------------------ 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=10000 meshname = 'urs_test_mesh' + '.tsh' interior_regions=None boundary_tags={'ocean': [0,1], 'otherocean': [2,3,4]} #------------------------------------------------------------ # Create mesh on the master processor and store in file. This file # is read in by each slave processor when needed #------------------------------------------------------------ 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) # print domain_fbound.mesh.get_boundary_polygon() else: domain_fbound=None barrier() if ( verbose and myid == 0 ): print 'DISTRIBUTING PARALLEL DOMAIN' domain_fbound = distribute(domain_fbound) #-------------------------------------------------------------------- # 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 = [[279000,664000], [280250,664130], [279280,665400], [280500,665000]] interpolation_points=num.array(interpolation_points) #if myid==0: # import pylab as P # boundary_polygon=num.array(boundary_polygon) # P.plot(boundary_polygon[:,0],boundary_polygon[:,1]) # P.plot(interpolation_points[:,0],interpolation_points[:,1],'ko') # P.show() fbound_gauge_values = [] fbound_proc_tri_ids = [] for i, point in enumerate(interpolation_points): fbound_gauge_values.append([]) # Empty list for timeseries try: k = domain_fbound.get_triangle_containing_point(point) if domain_fbound.tri_full_flag[k] == 1: fbound_proc_tri_ids.append(k) else: fbound_proc_tri_ids.append(-1) except: fbound_proc_tri_ids.append(-2) if verbose: print 'P%d has points = %s' %(myid, fbound_proc_tri_ids) #------------------------------------------------------------ # Set boundary conditions #------------------------------------------------------------ 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}) #------------------------------------------------------------ # Evolve the domain on each processor #------------------------------------------------------------ for i, t in enumerate(domain_fbound.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step = False)): stage = domain_fbound.get_quantity('stage') for i in range(4): if fbound_proc_tri_ids[i] > -1: fbound_gauge_values[i].append(stage.centroid_values[fbound_proc_tri_ids[i]]) #------------------------------------------------------------ # Create domain to be run sequntially on each processor #------------------------------------------------------------ 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, function=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,function=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}) drchlt_gauge_values = [] drchlt_proc_tri_ids = [] for i, point in enumerate(interpolation_points): drchlt_gauge_values.append([]) # Empty list for timeseries try: k = domain_drchlt.get_triangle_containing_point(point) if domain_drchlt.tri_full_flag[k] == 1: drchlt_proc_tri_ids.append(k) else: drchlt_proc_tri_ids.append(-1) except: drchlt_proc_tri_ids.append(-2) if verbose: print 'P%d has points = %s' %(myid, drchlt_proc_tri_ids) #------------------------------------------------------------ # Evolve entire domain on each processor #------------------------------------------------------------ for i, t in enumerate(domain_drchlt.evolve(yieldstep=yieldstep, finaltime=finaltime, skip_initial_step = False)): stage = domain_drchlt.get_quantity('stage') for i in range(4): drchlt_gauge_values[i].append(stage.centroid_values[drchlt_proc_tri_ids[i]]) #------------------------------------------------------------ # Compare sequential values with parallel values #------------------------------------------------------------ barrier() success = True for i in range(4): if fbound_proc_tri_ids[i] > -1: fbound_gauge_values[i]=num.array(fbound_gauge_values[i]) drchlt_gauge_values[i]=num.array(drchlt_gauge_values[i]) #print i,fbound_gauge_values[i][4] #print i,drchlt_gauge_values[i][4] success = success and num.allclose(fbound_gauge_values[i], drchlt_gauge_values[i]) assert success#, (fbound_gauge_values[i]-drchlt_gauge_values[i]) #assert_(success) if not sys.platform == 'win32': if myid==0: os.remove(sts_file+'.sts') if myid==0: os.remove(meshname)