def triangle_iterator(grid): """Iterate over the triangles in a grid""" # Get the ghost node table ghost_table = grid.reference_ghost_table() # Loop over the nodes in the grid for node in node_iterator(grid): # Loop over the edges joined to the node for endpt1 in endpt_iterator(grid, node.get_node_id()): # Get the node sitting to the endpoint (which may be a # ghost node) if grid.is_in(endpt1): node1 = grid.get_node(endpt1) else: node1 = ghost_table.get_node(endpt1) #Loop over another set of edges joined to the node for endpt2 in endpt_iterator(grid, node.get_node_id()): # If the endpoints are joined by an additional edge if grid.is_edge(endpt1, endpt2): # Get the node sitting to the endpoint (which may be a # ghost node) if grid.is_in(endpt2): node2 = grid.get_node(endpt2) else: node2 = ghost_table.get_node(endpt2) # Then return the triangle yield [node, node1, node2]
def plot_fem_grid(c, grid): from grid.NodeTable import node_iterator from grid.NghNodes import ngh_iterator from grid.EdgeTable import endpt_iterator from grid.Edge import DomainSet from matplotlib.pyplot import plot, show import matplotlib.pyplot as plt #for ngh in grid.reference_ghost_table(): #print(ngh.get_coord(),'ghost') for node in node_iterator(grid): print(node.get_coord(),'full nodes') #print (node.get_global_id()._id_no, node.get_coord()) #print(node) #plot(node._coord) for endpt in endpt_iterator(grid,node.get_node_id()): #print(endpt, grid.get_coord(endpt)) if grid.is_in(endpt): #print(grid.get_coord(endpt), 'm') x = [node.get_coord()[0], grid.get_coord(endpt)[0]] y = [node.get_coord()[1], grid.get_coord(endpt)[1]] #plot(x,y) if grid.reference_ghost_table().is_in(endpt): print(grid.reference_ghost_table().get_coord(endpt),'ghost') x = [node.get_coord()[0], grid.reference_ghost_table().get_coord(endpt)[0]] #print(ngh[1][i]._id_no) #print(grid.get_coord(ngh[1][i]._id_no)) y = [node.get_coord()[1], grid.reference_ghost_table().get_coord(endpt)[1]] plot(x,y) #print(endpt._id_no,"connected") #plot(node._coord,grid.get_node(endpt._id_no).get_coord()) for ghost in ngh_iterator(grid.reference_ghost_commun()): #print(ghost[1]) for node in ghost[1]: #print(grid.reference_ghost_table().get_coord(node),'coord') for endpt in endpt_iterator(grid, node): #print(endpt) if grid.reference_ghost_table().is_in(endpt): x = [grid.reference_ghost_table().get_coord(node)[0], grid.reference_ghost_table().get_coord(endpt)[0]] y = [grid.reference_ghost_table().get_coord(node)[1], grid.reference_ghost_table().get_coord(endpt)[1]] plot(x,y) #for endpt in endpt_iterator() plt.title('subgrid'+str(c)) show ()
def plot_fem_grid(grid): from grid.NodeTable import node_iterator from grid.EdgeTable import endpt_iterator from grid.Edge import DomainSet from matplotlib.pyplot import plot, show #This prints out the list of triangles for node in node_iterator(grid): #node_i=node.get_node_id() node_i = [node.get_node_id()._id_no] #print(node_i) for endpt_1 in endpt_iterator(grid, node.get_node_id()): node_j = [endpt_1._id_no] #print(node_j) for endpt_2 in endpt_iterator(grid, node.get_node_id()): node_k = [endpt_2._id_no] #print(node_k) #if grid.is_edge(endpt_1,endpt_2) == True: # print(node_i+node_j+node_k) # This plot the grid out from build_packman_grid for node in node_iterator(grid): for endpt in endpt_iterator(grid, node.get_node_id()): x = [ node.get_coord()[0], grid.get_node(endpt._id_no).get_coord()[0] ] y = [ node.get_coord()[1], grid.get_node(endpt._id_no).get_coord()[1] ] plot(x, y) x1 = np.linspace(0 + 1 / float(12), 1.0 - 1 / float(12), 12) y1 = np.linspace(0 + 1 / float(12), 1.0 - 1.0 / float(10), 12) X, Y = np.meshgrid(x1, y1) #fig, ax = plt.subplots() #fig,ax = plt.subplots(1,1,figsize=(10,10)) plt.scatter(X, Y, s=1) show()
def set_slave_value(grid, node): """Assign the slave node a value""" # Loop through the edges joined to the node node_id = node.get_node_id() for endpt1 in endpt_iterator(grid, node_id): # If the edge is a boundary edge if grid.get_location(node_id, endpt1) == DomainSet.boundary: # Evaluate the boundary function at the node coordinates bnd_func = grid.get_boundary_function(node_id, endpt1) node.set_value(bnd_func(node.get_coord()))
def build_equation_linear_2D(grid, tri_integrate, rhs_function): """Define the stiffness matrix and load vector""" # Set the values for the slave nodes and initialise the load value # to 0 for node in node_iterator(grid): node.set_load(0.0) if (node.get_slave()): set_slave_value(grid, node) # Add the matrix connections for linear basis functions and # initialise to zero for node in node_iterator(grid): node_id = node.get_node_id() if not grid.is_connection(node_id, node_id): grid.add_connection(node_id, node_id) grid.set_matrix_value(node_id, node_id, np.array([0.0, 0.0, 0.0, 0.0])) for endpt1 in endpt_iterator(grid, node.get_node_id()): if not grid.is_connection(node_id, endpt1): grid.add_connection(node_id, endpt1) grid.set_matrix_value(node_id, endpt1, np.array([0.0, 0.0, 0.0, 0.0])) # Evalue that A matrix and rhs vector # Loop over the triangles for tri in triangle_iterator(grid): #print(tri[0].get_node_id()._id_no) if tri[1].get_node_id() < tri[2].get_node_id(): # Find the local stiffness entries stiff1, stiff2, stiff3 \ = local_stiffness_linear_2D(tri[0], tri[1], tri[2], tri_integrate) # Find the local load entries local_load = local_load_linear_2D(tri[0], tri[1], tri[2], rhs_function) # Add in the contributions from the current triangle sum_load(tri[0], local_load) sum_stiffness(grid, tri[0], tri[0], stiff1) sum_stiffness(grid, tri[0], tri[1], stiff2) sum_stiffness(grid, tri[0], tri[2], stiff3)
#print(i) IntNode.append([node,node.get_node_id()]) IntNode_ID=sorted(IntNode, key = itemgetter(1)) IntNode_Ordered=[node[0] for node in IntNode_ID] ############################################################ ############################################################ #Add boundary value from corresponding load vector, if it is zero boundary condition, it will add nothing for j, node in enumerate(IntNode_Ordered): boundary_sum=[] id1=node.get_node_id() coord=node.get_coord() for endpt in endpt_iterator(grid, id1): if grid.get_slave(endpt): if abs(node.get_node_id()._id_no-endpt._id_no)==1 or \ abs(node.get_node_id()._id_no-endpt._id_no)==9: #print(node.get_node_id()._id_no,endpt._id_no) boundary_sum.append(true_soln(grid.get_coord(endpt))) load_vector[j]=load_vector[j]+sum(boundary_sum)*64 # Find the load vector unfer zero Dirichlet boundary condition for i in range(len(IntNode_Ordered)): coord=IntNode_Ordered[i].get_coord() load_vector[i]= load_vector[i]+rhs(coord) ###############################################################
def build_matrix_fem_2D(grid, tri_integrate1, tri_integrate2, tri_integrate3, X, Y): coordx = X.flatten() coordy = Y.flatten() Coord = zip(coordx, coordy) """ This routine builds the A matrix G1 and G2 matrix and stiffness, and store it on grid. """ print 'START' # Add the matrix connections for linear basis functions and # initialise to zero for node in node_iterator(grid): node_id = node.get_node_id() if not grid.is_connection(node_id, node_id): grid.add_connection(node_id, node_id) grid.set_matrix_value(node_id, node_id, np.array([0.0, 0.0, 0.0, 0.0])) for endpt1 in endpt_iterator(grid, node.get_node_id()): if not grid.is_connection(node_id, endpt1): grid.add_connection(node_id, endpt1) grid.set_matrix_value(node_id, endpt1, np.array([0.0, 0.0, 0.0, 0.0])) # Evalue that matrix and rhs vector # Loop over the triangles for tri in triangle_iterator(grid): #print(tri[0].get_node_id()._id_no) if tri[1].get_node_id() < tri[2].get_node_id(): # Find the local stiffness entries stiff1, stiff2, stiff3 \ = local_stiffness_linear_2D(tri[0], tri[1], tri[2], tri_integrate1) # Add in the contributions from the current triangle #sum_load(tri[0], local_load) sum_stiffness(grid, tri[0], tri[0], stiff1) sum_stiffness(grid, tri[0], tri[1], stiff2) sum_stiffness(grid, tri[0], tri[2], stiff3) XG1, XG2, XG3 \ = local_stiffness_linear_2D(tri[0], tri[1], tri[2], tri_integrate2) sum_G1(grid, tri[0], tri[0], XG1) sum_G1(grid, tri[0], tri[1], XG2) sum_G1(grid, tri[0], tri[2], XG3) YG1, YG2, YG3 \ = local_stiffness_linear_2D(tri[0], tri[1], tri[2], tri_integrate3) sum_G2(grid, tri[0], tri[0], YG1) sum_G2(grid, tri[0], tri[1], YG2) sum_G2(grid, tri[0], tri[2], YG3) print 'FINISH' ############################################################################### ############################################################################### #Store A matrix for whole grid nodes for tri in triangle_iterator(grid): if (tri[1].get_node_id() < tri[2].get_node_id()): # print type(tri[0].get_node_id()), tri[1].get_node_id(),tri[2].get_node_id(),'dasd' basis1 = set_polynomial_linear_2D(tri[0], tri[1], tri[2]) basis2 = set_polynomial_linear_2D(tri[1], tri[0], tri[2]) basis3 = set_polynomial_linear_2D(tri[2], tri[1], tri[0]) for i in Coord: if In_triangle(tri[0], tri[1], tri[2], i): #print i, 'coord of data' ii = basis1.eval(i[0], i[1]) * basis1.eval(i[0], i[1]) #print ii, 'eva at the data' sum_Aentry(grid, tri[0], tri[0], ii) ij = basis1.eval(i[0], i[1]) * basis2.eval(i[0], i[1]) sum_Aentry(grid, tri[0], tri[1], ij) ik = basis1.eval(i[0], i[1]) * basis3.eval(i[0], i[1]) sum_Aentry(grid, tri[0], tri[2], ik)