def __init__(self, filename, coordinates): r = self.r = adcirc_grid_reader(filename, coordinates) r.read_grid() # r.p contains the lines of node definitions, strip last column self.x = r.p[:, 0] self.y = r.p[:, 1] # r.t contains the connectivity of nodes to form triangles, strip fist column self.triangles = r.t[:, 1:] - 1
def read_grid(self, grid_file): gr = adcirc_grid_reader(grid_file, coordinates="spherical") gr.read_grid() nodes, elements, elev_boundaries, flow_boundaries = gr.get_sets() 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 self.nodes = nodes self.elements = elements self._elev_boundaries = elev_boundaries self._flow_boundaries = flow_boundaries self._neta = gr.parameters["NETA"]
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 __init__(self, filename, coordinates): from omuse.community.adcirc.read_grid import adcirc_grid_reader r = adcirc_grid_reader(filename, coordinates) r.read_grid() self.num_elems = num_elems = r.parameters['NE'] self.num_nodes = num_nodes = r.parameters['NP'] self.size = num_elems self.corners = num_corners = 3 #assuming only triangles are used for now # r.p contains the lines of node definitions, strip last column self.lon = lon = r.p[:, 0] self.lat = lat = r.p[:, 1] # r.t contains the connectivity of nodes to form triangles, strip fist column self.triangles = triangles = r.t[:, 1:] - 1 #minus one to offset Fortran indexes starting at 1 self.grid_center_lat = grid_center_lat = numpy.zeros( num_elems, dtype=numpy.double) self.grid_center_lon = grid_center_lon = numpy.zeros( num_elems, dtype=numpy.double) self.grid_corner_lat = grid_corner_lat = numpy.zeros( num_elems * num_corners, dtype=numpy.double) self.grid_corner_lon = grid_corner_lon = numpy.zeros( num_elems * num_corners, dtype=numpy.double) self.inverse_mapping = inverse_mapping = [[] for i in range(num_nodes)] self.elem_area = elem_area = numpy.zeros(num_elems, dtype=numpy.double) self.elem_area_spherical = elem_area_spherical = numpy.zeros( num_elems, dtype=numpy.double) i = 0 for triangle in triangles: n1, n2, n3 = triangle lat1 = lat[n1] lat2 = lat[n2] lat3 = lat[n3] lon1 = lon[n1] lon2 = lon[n2] lon3 = lon[n3] #add element i as neighbors of nodes n1, n2, and n3 inverse_mapping[n1].append(i) inverse_mapping[n2].append(i) inverse_mapping[n3].append(i) grid_center_lat[i] = (lat1 + lat2 + lat3) / 3.0 grid_center_lon[i] = (lon1 + lon2 + lon3) / 3.0 grid_corner_lat[i * 3 + 0] = lat1 grid_corner_lat[i * 3 + 1] = lat2 grid_corner_lat[i * 3 + 2] = lat3 grid_corner_lon[i * 3 + 0] = lon1 grid_corner_lon[i * 3 + 1] = lon2 grid_corner_lon[i * 3 + 2] = lon3 #triangle area, uses counter-clockwise sorting of nodes in trangle #assumes 2D euclidian geometry, not entirely accurate for lat-lon elem_area[i] = (((lon2 - lon1) * (lat3 - lat1)) - ((lon3 - lon1) * (lat2 - lat1))) / 2.0 #compute triangle area using spherical geometry a = distance(lat1, lon1, lat2, lon2) b = distance(lat2, lon2, lat3, lon3) c = distance(lat3, lon3, lat1, lon1) elem_area_spherical[i] = triangle_area(a, b, c) i += 1 self.adjacency_list = adjacency_list = [] self.boundary_node = boundary_node = [False for i in range(num_nodes)] for node in range(num_nodes): #create a list for each cell center connected to this corner cell_neighbors = [[] for i in inverse_mapping[node]] #use celli as index into cell_neighbors array celli = 0 #for each neighboring cell/triangle for cell in inverse_mapping[node]: corners = triangles[cell][:] #copy corner list corner_list = list(corners) corner_list.remove( node) #remove the shared corner, look for another one for i in inverse_mapping[node]: matches = 0 for c in triangles[i]: if c in corner_list: matches += 1 if matches == 1: #matches should be one, if it's 2 then it is the cell itself cell_neighbors[celli].append(i) #if there is a cell with less than two neighboring cells for this corner, then #this corner must be on a boundary if len(cell_neighbors[celli]) < 2: boundary_node[node] = True #print "node=", node, "cell=", cell, "neighbors=", cell_neighbors[celli] celli += 1 adjacency_list.append(cell_neighbors)
def run(tend=5. | units.day, state=None): param=adcirc_parameter_reader("data/test/2d/fort.15") param.read_parameters(NETA=9) param.parameters['NBFR']=-1 gr=adcirc_grid_reader("data/test/2d/fort.14") gr.read_grid() nodes,elements,elev_boundary,flow_boundary=gr.get_sets() code=Adcirc() code._parameters=param.parameters code.assign_grid_and_boundary(nodes,elements,elev_boundary, flow_boundary) code.parameters.use_interface_elevation_boundary=True code.parameters.use_interface_parameters=True code.parameters.use_interface_grid=True code.parameters.A_H=param.parameters["ESLM"] | units.m**2/units.s code.parameters.timestep=abs(param.parameters["DTDP"]) | units.s code.parameters.bottom_friction_law=["linear","quadratic","hybrid"][param.parameters["NOLIBF"]] try: code.parameters.linear_bottom_friction_coeff=param.parameters["TAU"]| units.s**-1 except: pass try: code.parameters.quadratic_bottom_friction_coeff=param.parameters["CF"] except: pass code.parameters.use_predictor_corrector=param.parameters["DTDP"]<0 code.parameters.use_interface_met_forcing=False if state: nodes,elements=state if state: code.parameters.begin_time=nodes.collection_attributes.time if state: channel=nodes.new_channel_to(code.nodes) channel.copy_attributes(["eta","deta_dt","status","vx","vy"]) channel=elements.new_channel_to(code.elements) channel.copy_attributes(["status"]) tnow=code.model_time dt=code.parameters.timestep elev_boundaries= list(code.elevation_boundaries()) eta61=[] time=[] forcing=[] while tnow<tend-dt/2: elev_boundaries[0].eta=tidal_force_function(tnow+dt/2) code.evolve_model(tnow+dt) tnow=code.get_model_time() eta=code.nodes[60].eta.number time.append(tnow.number) eta61.append(eta) forcing.append(elev_boundaries[0].eta[0].number) state=code.nodes.copy(),code.elements.copy() state[0].collection_attributes.time=code.model_time print("done at:", code.model_time.in_(units.day)) code.stop() return state,time,eta61,forcing
def test2(self): tend=5*86400. | units.s param=adcirc_parameter_reader("data/test/2d/fort.15") param.read_parameters(NETA=9) gr=adcirc_grid_reader("data/test/2d/fort.14") gr.read_grid() nodes,elements,elev_boundary,flow_boundary=gr.get_sets() code=Adcirc() code._parameters=param.parameters code.assign_grid_and_boundary(nodes,elements,elev_boundary, flow_boundary) code.parameters.use_interface_elevation_boundary=False code.parameters.use_interface_parameters=True code.parameters.use_interface_grid=True code.parameters.A_H=param.parameters["ESLM"] | units.m**2/units.s code.parameters.timestep=abs(param.parameters["DTDP"]) | units.s code.parameters.bottom_friction_law=["linear","quadratic","hybrid"][param.parameters["NOLIBF"]] try: code.parameters.linear_bottom_friction_coeff=param.parameters["TAU"]| units.s**-1 except: pass try: code.parameters.quadratic_bottom_friction_coeff=param.parameters["CF"] except: pass code.parameters.use_predictor_corrector=param.parameters["DTDP"]<0 code.parameters.use_interface_met_forcing=False print(code.parameters) tnow=code.model_time dt=code.parameters.timestep elev_boundaries= list(code.elevation_boundaries()) eta61=[] time=[] forcing=[] while tnow<tend-dt/2: code.evolve_model(tnow+dt) tnow=code.get_model_time() eta=code.nodes[60].eta.number time.append(tnow.number) eta61.append(eta) forcing.append(elev_boundaries[0].eta[0].number) code.stop() from matplotlib import pyplot pyplot.ion() f=pyplot.figure(figsize=(8,6)) pyplot.show() pyplot.clf() pyplot.plot(time,eta61,'r+') pyplot.plot(time,forcing,'g+') pyplot.plot(time,tidal_force_function((time| units.s)).number) pyplot.draw() sleep(3)