def initialize_pop(self): p=POP(redirection="file", number_of_workers=8,redirect_stdout_file="pop.out") cwd=os.getcwd() p.change_directory(cwd) popdatadir="/home/inti/code/amuse/trunk/sandbox/pelupes/pop/" #set the grid we want to use p.set_horiz_grid_file(popdatadir+'data/input/grid/horiz_grid_20010402.ieeer8') p.set_vert_grid_file(popdatadir+'data/input/grid/in_depths.dat') p.set_topography_file(popdatadir+'data/input/grid/topography_20010702.ieeei4') #set the restart file p.set_ts_file(popdatadir+'data/input/restart/r.x1_SAMOC_control.00750101') #setup the forcing p.set_shf_monthly_file(popdatadir+'data/input/shf_monthly/shf.normal_year+flux.mon') p.set_sfwf_monthly_file(popdatadir+'data/input/sfwf/sfwf_phc0-50_ncarp_r46_flux.mon') p.set_ws_monthly_file(popdatadir+'data/input/ws_monthly/ws.1958-2000.mon') self.pop_grid=p.get_grid() self.pop_forcings_grid=StaggeredGrid(p.elements, p.forcings, p._compute_cell_corners) self.pop=p self.timestep=self.timestep or self.pop.timestep/2
def test2(self): #generate corners for a simple structured grid shape = [5, 5] ind = numpy.indices((shape[0] + 1, shape[1] + 1)) lats = numpy.array(ind[0], dtype=numpy.float) lats = (0.5 - lats / shape[1]) * numpy.pi lats[0] = lats[0] - 1e-14 lats = lats[::-1, :] lons = numpy.array(ind[1], dtype=numpy.float) lons = lons / ind.shape[1] * 2.0 * numpy.pi corners = numpy.array([lons, lats]) elements = new_structured_grid(shape, corners, axes_names=['lon', 'lat']) #let the north east corners of each cell be the position of the nodes positions = numpy.array([lons[1:, 1:], lats[1:, 1:]]) nodes = StructuredGrid(*shape) nodes.lat = (lats[1:, 1:] | units.rad) nodes.lon = (lons[1:, 1:] | units.rad) print(elements, nodes) grid = StaggeredGrid(elements, nodes, get_corners=lambda: numpy.array([lons, lats])) values = numpy.random.random(shape) print(values) elements.values = values nodes.values = grid.map_elements_to_nodes(values) print(nodes.values) remapped_values = grid.map_nodes_to_elements(nodes.values) print(remapped_values) before_sum = values.sum() after_sum = remapped_values.sum() print('before', before_sum, 'after', after_sum) self.assertEqual( after_sum, before_sum, msg="Sum of values before and after remapping should be the same")
def test2(self): #generate corners for a simple structured grid shape = [5,5] ind = numpy.indices( (shape[0]+1,shape[1]+1)) lats = numpy.array( ind[0] , dtype=numpy.float) lats = (0.5-lats/shape[1]) * numpy.pi lats[0] = lats[0] - 1e-14 lats = lats[::-1,:] lons = numpy.array( ind[1] , dtype=numpy.float) lons = lons/ind.shape[1] * 2.0*numpy.pi corners = numpy.array([lons,lats]) elements = new_structured_grid(shape, corners, axes_names=['lon', 'lat']) #let the north east corners of each cell be the position of the nodes positions = numpy.array([lons[1:,1:], lats[1:,1:]]) nodes = StructuredGrid(*shape) nodes.lat = (lats[1:,1:] | units.rad) nodes.lon = (lons[1:,1:] | units.rad) print elements, nodes grid = StaggeredGrid(elements, nodes, get_corners=lambda: numpy.array([lons, lats])) values = numpy.random.random(shape) print values elements.values = values nodes.values = grid.map_elements_to_nodes(values) print nodes.values remapped_values = grid.map_nodes_to_elements(nodes.values) print remapped_values before_sum = values.sum() after_sum = remapped_values.sum() print 'before', before_sum, 'after', after_sum self.assertEquals(after_sum, before_sum, msg="Sum of values before and after remapping should be the same")
def setUp(self): try: from omuse.community.cdo.interface import CDORemapper except: self.skip( "conservative spherical remapper requires omuse.community.cdo.interface" ) #this test creates a structured staggered grid and an unstructured staggered grid #and then uses the conservative_spherical_remapper to remap values between the grids #define nodal points and triangles of a small test grid #got this grid from http://matplotlib.org/examples/pylab_examples/triplot_demo.html xy = numpy.asarray([[-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890], [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898], [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919], [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949], [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959], [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965], [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980], [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996], [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021], [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005], [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987], [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968], [-0.020, 0.954], [-0.006, 0.947], [0.003, 0.935], [0.006, 0.926], [0.005, 0.921], [0.022, 0.923], [0.033, 0.912], [0.029, 0.905], [0.017, 0.900], [0.012, 0.895], [0.027, 0.893], [0.019, 0.886], [0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879], [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872], [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933], [-0.077, 0.990], [-0.059, 0.993]]) triangles = numpy.asarray([[67, 66, 1], [65, 2, 66], [1, 66, 2], [64, 2, 65], [63, 3, 64], [60, 59, 57], [2, 64, 3], [3, 63, 4], [0, 67, 1], [62, 4, 63], [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [4, 62, 68], [6, 5, 9], [61, 68, 62], [69, 68, 61], [9, 5, 70], [6, 8, 7], [4, 70, 5], [8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9], [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12], [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71], [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71], [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19], [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24], [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28], [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45], [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40], [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40], [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38], [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]]) num_elems = len(triangles) elements = UnstructuredGrid(num_elems) elements.n1 = triangles[:, 0] - 1 elements.n2 = triangles[:, 1] - 1 elements.n3 = triangles[:, 2] - 1 lons = numpy.zeros(num_elems, dtype=numpy.double) lats = numpy.zeros(num_elems, dtype=numpy.double) for i in range(num_elems): for n in triangles[i]: lons[i] += xy[n, 0] / 3.0 lats[i] += xy[n, 1] / 3.0 elements.lon = (lons | units.rad) elements.lat = (lats | units.rad) nodes = UnstructuredGrid(len(xy)) nodes.lon = (xy[:, 0] | units.rad) nodes.lat = (xy[:, 1] | units.rad) self.unstructured = StaggeredGrid(elements, nodes) #generate corners for a simple structured grid as source grid shape = [5, 5] lon_range = xy[:, 0].max() - xy[:, 0].min() + 0.025 lon_min = xy[:, 0].min() - 0.0125 lat_range = xy[:, 1].max() - xy[:, 1].min() + 0.025 lat_min = xy[:, 1].min() - 0.0125 ind = numpy.indices((shape[0] + 1, shape[1] + 1)) lats = numpy.array(ind[1], dtype=numpy.float) lats = lat_min + lats / shape[1] * lat_range lons = numpy.array(ind[0], dtype=numpy.float) lons = lon_min + lons / shape[0] * lon_range corners = numpy.array([lons, lats]) elements = new_structured_grid(shape, corners, axes_names=['lon', 'lat']) nodes = StructuredGrid(*ind[0].shape) nodes.lat = (lats | units.rad) nodes.lon = (lons | units.rad) self.structured = StaggeredGrid(elements, nodes) corners += 0.01 #shift the grid nodes = StructuredGrid(*ind[0].shape) nodes.lat = (corners[1] | units.rad) nodes.lon = (corners[0] | units.rad) self.structured2 = StaggeredGrid(elements, nodes)
def test3(self): #define nodal points and triangles of a small test grid #got this grid from http://matplotlib.org/examples/pylab_examples/triplot_demo.html xy = numpy.asarray([ [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890], [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898], [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919], [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949], [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959], [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965], [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980], [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996], [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021], [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005], [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987], [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968], [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926], [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905], [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886], [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879], [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872], [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933], [-0.077, 0.990], [-0.059, 0.993]]) triangles = numpy.asarray([ [67, 66, 1], [65, 2, 66], [ 1, 66, 2], [64, 2, 65], [63, 3, 64], [60, 59, 57], [ 2, 64, 3], [ 3, 63, 4], [ 0, 67, 1], [62, 4, 63], [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68], [ 6, 5, 9], [61, 68, 62], [69, 68, 61], [ 9, 5, 70], [ 6, 8, 7], [ 4, 70, 5], [ 8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9], [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12], [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71], [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71], [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19], [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24], [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28], [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45], [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40], [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40], [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38], [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]]) num_elems = len(triangles) elements = UnstructuredGrid(num_elems) elements.n1 = triangles[:,0] - 1 elements.n2 = triangles[:,1] - 1 elements.n3 = triangles[:,2] - 1 nodes = UnstructuredGrid(len(xy)) nodes.lon = (xy[:,0] | units.rad) nodes.lat = (xy[:,1] | units.rad) grid = StaggeredGrid(elements, nodes) values = numpy.random.random(num_elems) print values elements.values = values nodes.values = grid.map_elements_to_nodes(values) print nodes.values remapped_values = grid.map_nodes_to_elements(nodes.values) print remapped_values before_sum = values.sum() after_sum = remapped_values.sum() print 'before', before_sum, 'after', after_sum self.assertAlmostEquals(after_sum, before_sum, msg="Sum of values before and after remapping should be the same")
class POP_Adcirc(object): def __init__(self, timestep=None, initialize_adcirc_state=False, adcirc_ramp=2. | units.day, elev_boundary_sponge=False,layer_depth=500. | units.m, distributed=False, boundary_forcing="elevation"): self.timestep=timestep self.adcirc_ramp=adcirc_ramp self.elev_boundary_sponge=elev_boundary_sponge self.layer_depth=layer_depth self.boundary_forcing=boundary_forcing if distributed: self.initialize_distributed_amuse() self.initialize_pop() self.initialize_adcirc() if initialize_adcirc_state: self.initialize_adcirc_state() self.initialize_channels() self.model_time=0. | units.s self.pop_time_offset=self.pop.model_time self.adcirc_time_offset=self.adcirc.model_time def initialize_distributed_amuse(self): da = init_local_only() da.use_for_all_workers() self.distributed_amuse=da def initialize_pop(self): p=POP(redirection="file", number_of_workers=8,redirect_stdout_file="pop.out") cwd=os.getcwd() p.change_directory(cwd) popdatadir="/home/inti/code/amuse/trunk/sandbox/pelupes/pop/" #set the grid we want to use p.set_horiz_grid_file(popdatadir+'data/input/grid/horiz_grid_20010402.ieeer8') p.set_vert_grid_file(popdatadir+'data/input/grid/in_depths.dat') p.set_topography_file(popdatadir+'data/input/grid/topography_20010702.ieeei4') #set the restart file p.set_ts_file(popdatadir+'data/input/restart/r.x1_SAMOC_control.00750101') #setup the forcing p.set_shf_monthly_file(popdatadir+'data/input/shf_monthly/shf.normal_year+flux.mon') p.set_sfwf_monthly_file(popdatadir+'data/input/sfwf/sfwf_phc0-50_ncarp_r46_flux.mon') p.set_ws_monthly_file(popdatadir+'data/input/ws_monthly/ws.1958-2000.mon') self.pop_grid=p.get_grid() self.pop_forcings_grid=StaggeredGrid(p.elements, p.forcings, p._compute_cell_corners) self.pop=p self.timestep=self.timestep or self.pop.timestep/2 def read_adcirc_grid(self, grid_file): gr=adcirc_grid_reader(grid_file,coordinates="spherical") gr.read_grid() nodes,elements,elev_boundaries,flow_boundaries=gr.get_sets() if self.boundary_forcing=="flow": # change elevation to flow boundary for b in elev_boundaries: b.type=22 flow_boundaries=elev_boundaries+flow_boundaries elev_boundaries=[] elements.n1=elements.nodes[:,0] elements.n2=elements.nodes[:,1] elements.n3=elements.nodes[:,2] i=0 for e in elev_boundaries+flow_boundaries: i+=1 nodes[e.nodes].vmark=i # put maximum on depth to simulate top layer nodes.depth=numpy.minimum(nodes.depth ,self.layer_depth ) # fix south east corner of bathymetry to prevent step right on edge # this is actually counterproductive... #~ lon=nodes.lon #~ lat=nodes.lat #~ a=numpy.where( (lat<11.5 |units.deg)*(lon>-61. | units.deg))[0] #~ nodes[a].depth=numpy.minimum(nodes[a].depth, 100. | units.m) # maybe increasing depth?? self.nodes=nodes self.elements=elements self._elev_boundaries=elev_boundaries self._flow_boundaries=flow_boundaries self._neta=gr.parameters["NETA"] self._nflux=gr.parameters["NFLUX"] def initialize_adcirc(self): self.read_adcirc_grid("grid.input") param=adcirc_parameter_reader("param.input") param.read_parameters(NETA=self._neta, NFLUX=self._nflux) #~ param.parameters['NBFR']=-1 param.parameters['NWS']=0 adcirc=Adcirc(coordinates="spherical", redirection="file",redirect_stdout_file="adcirc.out") adcirc.set_rootdir(os.getcwd()) #~ adcirc._parameters=param.parameters adcirc._parameters=get_default_parameter_set() adcirc._parameters["NCOR"]=1 # set coriolis f internally adcirc.assign_grid_and_boundary(self.nodes, self.elements, self._elev_boundaries, self._flow_boundaries) adcirc.parameters.use_interface_parameters=True adcirc.parameters.use_interface_grid=True if self.boundary_forcing=="elevation": adcirc.parameters.use_interface_elevation_boundary=True elif self.boundary_forcing=="flow": adcirc.parameters.use_interface_flow_boundary=True adcirc.parameters.use_interface_met_forcing=True adcirc.parameters.A_H=1000. | units.m**2/units.s #~ adcirc.parameters.A_H=param.parameters["ESLM"] | units.m**2/units.s timestep=self.timestep n=1 #~ while timestep>abs(param.parameters["DTDP"]) | units.s: while timestep>300. | units.s: n+=1 timestep=timestep/n adcirc.parameters.timestep=timestep #~ adcirc.parameters.bottom_friction_law=["linear","quadratic","hybrid"][param.parameters["NOLIBF"]] #~ adcirc.parameters.linear_bottom_friction_coeff=param.parameters["TAU"]| units.s**-1 adcirc.parameters.bottom_friction_law="hybrid" adcirc.parameters.hybrid_bottom_friction_hbreak=50. | units.m #~ adcirc.parameters.bottom_friction_law="linear" #~ adcirc.parameters.linear_bottom_friction_coeff=0.00001| units.s**-1 adcirc.parameters.quadratic_bottom_friction_coeff=0.003#param.parameters["CF"] adcirc.parameters.use_predictor_corrector=True#param.parameters["DTDP"]<0 adcirc.parameters.central_longitude=param.parameters["SLAM0"] | units.deg adcirc.parameters.central_latitude=param.parameters["SFEA0"] | units.deg adcirc.parameters.GWCE_weighting_factor=-1.#param.parameters["TAU0"] #~ adcirc.parameters.GWCE_weighting_factor=0.005 #~ adcirc.parameters.spatial_derivative_advective_term_parameter=0 #~ adcirc.parameters.time_derivative_advective_term_parameter=0 #~ adcirc.parameters.finite_amplitude_term_parameter=0 adcirc.parameters.minimum_depth=5.| units.m if self.adcirc_ramp: adcirc.parameters.use_ramping=True adcirc.parameters.ramping_time=self.adcirc_ramp print(adcirc.parameters) self.adcirc=adcirc self.adcirc_grid=adcirc.get_grid() self.adcirc_forcings_grid=StaggeredGrid(self.adcirc_grid.elements, nodes=adcirc.forcings) elements=adcirc.elements.copy(filter_attributes=lambda x,y: y in ["lat","lon","n1","n2","n3"] ) nodes=adcirc.nodes.copy(filter_attributes=lambda x,y: y in ["lat","lon"] ) self.adcirc_memory_grid = StaggeredGrid( elements, nodes=nodes ) def initialize_adcirc_boundary_channel(self): # two things remain to be fixed: # - subgrid (as generated by pop nodes slicing) is not a structuredgrid yet # so remapping doesn't work directly (proper fix is to fix subgridding) # - the definitions of lat and lon are inconsistent between codes, leading # to problems with remapping. Various possible fixes: make def consistent, or # define something in the remapper (coordinate trasnform keyword, or is the channel # transform (not implemented yet for remapping channel) something tobe used here?? elev_boundaries=list(self.adcirc.elevation_boundaries()) flow_boundaries=list(self.adcirc.flow_boundaries()) remapper=functools.partial(interpolating_2D_remapper, axes_names=["lon","lat"]) if self.boundary_forcing=="elevation": boundary_=elev_boundaries[0] elif self.boundary_forcing=="flow": boundary_=flow_boundaries[0] boundary=boundary_.empty_copy() boundary.node=boundary_.node boundary.lat=self.adcirc.nodes[boundary.node].lat boundary.lon=self.adcirc.nodes[boundary.node].lon + (2*numpy.pi| units.rad) #~ from amuse.io import write_set_to_file #~ write_set_to_file(elev_boundaries[0],"elev_boundary","amuse") #~ raise # correction for grid mismatch (??) : boundary.lat+= (0.25 | units.deg) boundary.lon+= (0.25 | units.deg) # hardcoded subregion pop_region=self.pop.nodes[280:310,218:317] pop_region_copy=pop_region.copy() self._adcirc_boundary=boundary self.boundary_channel=pop_region_copy.new_remapping_channel_to(boundary, remapper) self._pop_boundary_region=pop_region self._pop_boundary_region_copy=pop_region_copy self._aux_boundary_channel1=pop_region.new_channel_to(self._pop_boundary_region_copy) self._aux_boundary_channel2=self._adcirc_boundary.new_channel_to(boundary_) def update_adcirc_boundary(self): if self.boundary_forcing=="elevation": self.update_elevation_boundary() elif self.boundary_forcing=="flow": self.update_flow_boundary() def update_elevation_boundary(self): self._aux_boundary_channel1.copy_attributes(["ssh"]) self.boundary_channel.copy_attributes(["ssh"]) if self.elev_boundary_sponge: n=self.elev_nsponge sponge=numpy.arange(n)/(1.*n) self._adcirc_boundary[:n].ssh*=sponge self._adcirc_boundary[-n:].ssh*=sponge[::-1] self._aux_boundary_channel2.transform(["eta"],None,["ssh"]) def update_flow_boundary(self): self._aux_boundary_channel1.copy_attributes(["vx_barotropic","vy_barotropic"]) self.boundary_channel.copy_attributes(["depth","vx_barotropic","vy_barotropic"]) self._aux_boundary_channel2.transform(["flux_x","flux_y"],lambda x,y,z : (x*y, x*z), ["depth","vx_barotropic","vy_barotropic"]) def initialize_channels(self): self.initialize_adcirc_boundary_channel() self.forcings_channel = self.pop_forcings_grid.new_remapping_channel_to(self.adcirc_forcings_grid, conservative_spherical_remapper) #~ self.memory_channel = self.pop_grid.new_remapping_channel_to(self.adcirc_memory_grid, conservative_spherical_remapper) def update_adcirc_forcings(self): self.forcings_channel.copy_attributes(["tau_x","tau_y"]) #~ tau_x=self.adcirc_forcings_grid.nodes.tau_x #~ tau_y=self.adcirc_forcings_grid.nodes.tau_y #~ coriolis_f=self.adcirc_forcings_grid.nodes.coriolis_f #~ print "tau_x (min,max,mean):", tau_x.min(),tau_x.max(),tau_x.mean() #~ print "tau_y (min,max,mean):", tau_y.min(),tau_y.max(),tau_y.mean() #~ print "coriolis_f (min,max,mean):", coriolis_f.min(),coriolis_f.max(),coriolis_f.mean() def initialize_adcirc_state(self): channel1 = self.pop_grid.new_remapping_channel_to(self.adcirc_memory_grid, conservative_spherical_remapper) channel2 = self.adcirc_memory_grid.nodes.new_channel_to(self.adcirc_grid.nodes) channel1.copy_attributes(["vx_barotropic", "vy_barotropic","ssh"]) def f(vx,vy,eta): return vx,vy,eta, 0. | units.m/units.s channel2.transform(["vx","vy","eta","deta_dt"], f, ["vx_barotropic", "vy_barotropic","ssh"]) def evolve_model(self, tend, timestep=None): timestep=timestep or self.timestep or tend-self.model_time while self.model_time<tend-timestep/2: next_timestep=self.pop.timestep_next print("update boundary..") self.update_adcirc_boundary() print("update forcings..") self.update_adcirc_forcings() print("evolve pop...") self.pop.evolve_model(self.model_time+next_timestep + self.pop_time_offset) print("evolve adcirc..") self.adcirc.evolve_model(self.model_time+next_timestep + self.adcirc_time_offset) print("done") self.model_time+=next_timestep
def initialize_adcirc(self): self.read_adcirc_grid("grid.input") param=adcirc_parameter_reader("param.input") param.read_parameters(NETA=self._neta, NFLUX=self._nflux) #~ param.parameters['NBFR']=-1 param.parameters['NWS']=0 adcirc=Adcirc(coordinates="spherical", redirection="file",redirect_stdout_file="adcirc.out") adcirc.set_rootdir(os.getcwd()) #~ adcirc._parameters=param.parameters adcirc._parameters=get_default_parameter_set() adcirc._parameters["NCOR"]=1 # set coriolis f internally adcirc.assign_grid_and_boundary(self.nodes, self.elements, self._elev_boundaries, self._flow_boundaries) adcirc.parameters.use_interface_parameters=True adcirc.parameters.use_interface_grid=True if self.boundary_forcing=="elevation": adcirc.parameters.use_interface_elevation_boundary=True elif self.boundary_forcing=="flow": adcirc.parameters.use_interface_flow_boundary=True adcirc.parameters.use_interface_met_forcing=True adcirc.parameters.A_H=1000. | units.m**2/units.s #~ adcirc.parameters.A_H=param.parameters["ESLM"] | units.m**2/units.s timestep=self.timestep n=1 #~ while timestep>abs(param.parameters["DTDP"]) | units.s: while timestep>300. | units.s: n+=1 timestep=timestep/n adcirc.parameters.timestep=timestep #~ adcirc.parameters.bottom_friction_law=["linear","quadratic","hybrid"][param.parameters["NOLIBF"]] #~ adcirc.parameters.linear_bottom_friction_coeff=param.parameters["TAU"]| units.s**-1 adcirc.parameters.bottom_friction_law="hybrid" adcirc.parameters.hybrid_bottom_friction_hbreak=50. | units.m #~ adcirc.parameters.bottom_friction_law="linear" #~ adcirc.parameters.linear_bottom_friction_coeff=0.00001| units.s**-1 adcirc.parameters.quadratic_bottom_friction_coeff=0.003#param.parameters["CF"] adcirc.parameters.use_predictor_corrector=True#param.parameters["DTDP"]<0 adcirc.parameters.central_longitude=param.parameters["SLAM0"] | units.deg adcirc.parameters.central_latitude=param.parameters["SFEA0"] | units.deg adcirc.parameters.GWCE_weighting_factor=-1.#param.parameters["TAU0"] #~ adcirc.parameters.GWCE_weighting_factor=0.005 #~ adcirc.parameters.spatial_derivative_advective_term_parameter=0 #~ adcirc.parameters.time_derivative_advective_term_parameter=0 #~ adcirc.parameters.finite_amplitude_term_parameter=0 adcirc.parameters.minimum_depth=5.| units.m if self.adcirc_ramp: adcirc.parameters.use_ramping=True adcirc.parameters.ramping_time=self.adcirc_ramp print(adcirc.parameters) self.adcirc=adcirc self.adcirc_grid=adcirc.get_grid() self.adcirc_forcings_grid=StaggeredGrid(self.adcirc_grid.elements, nodes=adcirc.forcings) elements=adcirc.elements.copy(filter_attributes=lambda x,y: y in ["lat","lon","n1","n2","n3"] ) nodes=adcirc.nodes.copy(filter_attributes=lambda x,y: y in ["lat","lon"] ) self.adcirc_memory_grid = StaggeredGrid( elements, nodes=nodes )
def test3(self): #define nodal points and triangles of a small test grid #got this grid from http://matplotlib.org/examples/pylab_examples/triplot_demo.html xy = numpy.asarray([[-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890], [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898], [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919], [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949], [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959], [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965], [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980], [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996], [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021], [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005], [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987], [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968], [-0.020, 0.954], [-0.006, 0.947], [0.003, 0.935], [0.006, 0.926], [0.005, 0.921], [0.022, 0.923], [0.033, 0.912], [0.029, 0.905], [0.017, 0.900], [0.012, 0.895], [0.027, 0.893], [0.019, 0.886], [0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879], [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872], [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933], [-0.077, 0.990], [-0.059, 0.993]]) triangles = numpy.asarray([[67, 66, 1], [65, 2, 66], [1, 66, 2], [64, 2, 65], [63, 3, 64], [60, 59, 57], [2, 64, 3], [3, 63, 4], [0, 67, 1], [62, 4, 63], [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [4, 62, 68], [6, 5, 9], [61, 68, 62], [69, 68, 61], [9, 5, 70], [6, 8, 7], [4, 70, 5], [8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9], [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12], [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71], [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71], [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19], [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24], [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28], [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45], [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40], [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40], [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38], [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]]) num_elems = len(triangles) elements = UnstructuredGrid(num_elems) elements.n1 = triangles[:, 0] - 1 elements.n2 = triangles[:, 1] - 1 elements.n3 = triangles[:, 2] - 1 nodes = UnstructuredGrid(len(xy)) nodes.lon = (xy[:, 0] | units.rad) nodes.lat = (xy[:, 1] | units.rad) grid = StaggeredGrid(elements, nodes) values = numpy.random.random(num_elems) print(values) elements.values = values nodes.values = grid.map_elements_to_nodes(values) print(nodes.values) remapped_values = grid.map_nodes_to_elements(nodes.values) print(remapped_values) before_sum = values.sum() after_sum = remapped_values.sum() print('before', before_sum, 'after', after_sum) self.assertAlmostEqual( after_sum, before_sum, msg="Sum of values before and after remapping should be the same")
def get_grid(self): return StaggeredGrid(self.elements, self.nodes, self._compute_cell_corners)
def get_grid(self): return StaggeredGrid(self.elements, self.nodes)