def h4(grid, Crhs, g1rhs, g2rhs): # Crhs = plain # # g1rhs = zero # # g2rhs = zero # # build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) h4 = zeros([len(Nl), 1]) for node in not_slave_node(grid): # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) c = Crhs(coord[0], coord[1]) g1 = g1rhs(coord[0], coord[1]) g2 = g2rhs(coord[0], coord[1]) #print c #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] g1entry = grid.get_matrix_value(node.get_node_id(), endpt)[2] g2entry = grid.get_matrix_value(node.get_node_id(), endpt)[3] #print aentry h4[i] += c * lentry - g1entry * g1 - g2entry * g2 #h4[i] += c* lentry + g1entry * g1 + g2entry * g2 #print h4 return h4
def h2(grid, g1rhs, wrhs, alpha): # g1rhs = zero # # wrhs =zero # build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) Nl=[] for node in (not_slave_node(grid)): Nl.append([node,node.get_node_id()]) Nl=sorted(Nl, key = itemgetter(1)) Nl=[node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) h2= zeros([len(Nl), 1]) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) #print coord g1 = g1rhs(coord[0], coord[1]) #print g1 w = wrhs(coord[0], coord[1]) print #w #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] g1entry = grid.get_matrix_value(node.get_node_id(), endpt)[2] #print aentry h2[i] += alpha * g1 * lentry - w * g1entry #G1, G2 #h2[i] += alpha * g1 * lentry +w * g1entry # -G1, -G2 return h2
def h3(grid, g2rhs, wrhs, alpha): # g2rhs = zero # # wrhs = zero # # build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) # Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) h3 = zeros([len(Nl), 1]) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) g2 = g2rhs(coord[0], coord[1]) w = wrhs(coord[0], coord[1]) #print c, w #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] # G2 entries on boundary g2entry = grid.get_matrix_value(node.get_node_id(), endpt)[3] #print aentry h3[i] += alpha * g2 * lentry + w * g2entry #h3[i] += alpha * g2* lentry - w* g2entry return h3
def h1(grid, Crhs, wrhs): Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) h1 = zeros((len(Nl), 1)) h = 1 / float(math.sqrt(len(Nl)) + 1) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) #print node.get_value(), node.get_node_id()._id_no for endpt in connect_iterator(grid, node.get_node_id()): #j = int(grid.get_value(endpt)) #print j if grid.get_slave(endpt): coord = grid.get_coord(endpt) c = Crhs(coord[0], coord[1]) w = wrhs(coord[0], coord[1]) #print aentry lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] aentry = grid.get_matrix_value(node.get_node_id(), endpt)[1] / float(len(Coord)) h1[i] += c * aentry + w * lentry return h1
def dvector(grid, data): #build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) dvector = zeros([len(Nl), 1]) for tri in Interior_triangle_iterator(grid): if tri[1].get_node_id() < tri[2].get_node_id(): basis1 = set_polynomial_linear_2D(tri[0], tri[2], tri[1]) Idi = int(tri[0].get_value()) for i in range(len(Coord)): if NIn_triangle(tri[0], tri[1], tri[2], Coord[i]): dvector[Idi, 0] += basis1.eval(Coord[i][0], Coord[i][1]) * data[i] return dvector / float(len(Coord))
def Amatrix(grid): # node.set_value(Nl.index(node)) # Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) Amatrix = csr_matrix((len(Nl), len(Nl))) for node in node_iterator(grid): if not node.get_slave(): idi = int(node.get_value()) #print idi, node.get_coord() #print node.get_value(), node.get_node_id()._id_no for endpt in connect_iterator(grid, node.get_node_id()): idj = int(grid.get_value(endpt)) #print idj, grid.get_coord(endpt) #print endpt._id_no, grid.get_value(endpt) aentry = grid.get_matrix_value(node.get_node_id(), endpt)[1] #print aentry if not grid.get_slave(endpt): Amatrix[idi, idj] = aentry return Amatrix / float(len(Coord))
def Lets_Make_the_Damn_Big_Matrix(): Nl=[] for node in (not_slave_node(grid)): Nl.append([node,node.get_node_id()]) Nl=sorted(Nl, key = itemgetter(1)) Nl=[node[0] for node in Nl] ZeroMatrix = csr_matrix((len(Nl),len(Nl))) BigMat = bmat([[Amatrix, ZeroMatrix, ZeroMatrix, Lmatrix],\ [ZeroMatrix, alpha*Lmatrix, ZeroMatrix, G1.T],\ [ZeroMatrix, ZeroMatrix, alpha*Lmatrix, G2.T],\ [Lmatrix, G1, G2, ZeroMatrix]]) return BigMat
def Lets_Make_the_Damn_Big_Matrix(): Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] BigMat = bmat([[Amatrix, None, None, math.sqrt(alpha)*Lmatrix],\ [None, Lmatrix, None, -G1.T],\ [None, None, Lmatrix, -G2.T],\ [math.sqrt(alpha)*Lmatrix, -G1, -G2, None]]) return BigMat
def h3_bd(grid, g2rhs, wrhs, alpha, Nl): # Nl = 0 # # for node in node_iterator(grid): # # if not node.get_slave(): # # node.set_value(Nl) # # Nl = Nl+1 h3 = np.zeros((len(Nl), )) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) g2 = g2rhs(coord[0], coord[1]) w = -alpha * wrhs(coord[0], coord[1]) #print c, w #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] # G2 entries on boundary g2entry = grid.get_matrix_value(node.get_node_id(), endpt)[3] #print aentry #h3[i] += alpha * g2* lentry - w* g2entry # G1, G2 h3[i] += alpha * g2 * lentry + w * g2entry # -G1, -G2 return h3
def generate_dmatrix(): """ This rountine gernerates dmatrix after deducted boundary conditions """ Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) dmatrix = zeros([len(Nl), 1]) #print Amatrix for tri in Interior_triangle_iterator(grid): if (tri[1].get_node_id() < tri[2].get_node_id()): #print tri[0] #print tri[0].get_node_id()._id_no, tri[1].get_node_id()._id_no, tri[2].get_node_id()._id_no basis1 = set_polynomial_linear_2D(tri[0], tri[2], tri[1]) Idi = int(tri[0].get_value()) for i in range(len(Coord)): if NIn_triangle(tri[0], tri[1], tri[2], Coord[i]): #print basis1.eval(Coord[i][0], Coord[i][1]), data[i] dmatrix[Idi, 0] += basis1.eval(Coord[i][0], Coord[i][1]) * data[i] #print dmatrix[Idi,0] #print dmatrix/float(len(Coord)) return dmatrix / float(len(Coord))
def G2(grid): #build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) Nl=[] for node in (not_slave_node(grid)): Nl.append([node,node.get_node_id()]) Nl=sorted(Nl, key = itemgetter(1)) Nl=[node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) G2 = csr_matrix((len(Nl), len(Nl))) for node in node_iterator(grid): # Ignore slave (or boundary) nodes if not node.get_slave(): # Which row corresponds to the current node? i = int(node.get_value()) for endpt1 in connect_iterator(grid, node.get_node_id()): # Which column corresponds to the current node? j = int(grid.get_value(endpt1)) # What is the corresponding matrix value (in the FEM grid) g2 = grid.get_matrix_value(node.get_node_id(), endpt1)[3] # We must not include slave nodes in the matrix columns if not grid.get_slave(endpt1): G2[i, j] = g2 return G2
def Lmatrix(grid): Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) # Lmatrix = lil_matrix((len(Nl), len(Nl))) for node in node_iterator(grid): # Ignore slave (or boundary) nodes if not node.get_slave(): # Which row corresponds to the current node? i = int(node.get_value()) for endpt1 in connect_iterator(grid, node.get_node_id()): # Which column corresponds to the current node? j = int(grid.get_value(endpt1)) # What is the corresponding matrix value (in the FEM grid) lentry = grid.get_matrix_value(node.get_node_id(), endpt1)[0] # We must not include slave nodes in the matrix columns if not grid.get_slave(endpt1): Lmatrix[i, j] = lentry return Lmatrix
def generate_XGmatrix(): build_equation_linear_2D(grid, TPS_tri_intergrateX, zero) Nl = [] for node in (not_slave_node(grid)): #print(i) Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) XGmatrix = zeros([len(Nl), len(Nl)]) for node in node_iterator(grid): # Ignore slave (or boundary) nodes if not node.get_slave(): # Which row corresponds to the current node? i = int(node.get_value()) for endpt1 in connect_iterator(grid, node.get_node_id()): # Which column corresponds to the current node? j = int(grid.get_value(endpt1)) # What is the corresponding matrix value (in the FEM grid) stiffness = grid.get_matrix_value(node.get_node_id(), endpt1)[0] # We must not include slave nodes in the matrix columns if not grid.get_slave(endpt1): XGmatrix[i, j] = stiffness return XGmatrix
def h4_bd(grid, Crhs, g1rhs, g2rhs, Nl): h4 = np.zeros((len(Nl), )) for node in not_slave_node(grid): # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) c = Crhs(coord[0], coord[1]) g1 = g1rhs(coord[0], coord[1]) g2 = g2rhs(coord[0], coord[1]) #print c #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] g1entry = grid.get_matrix_value(node.get_node_id(), endpt)[2] g2entry = grid.get_matrix_value(node.get_node_id(), endpt)[3] #print aentry #h4[i] += c* lentry + g1entry * g1 + g2entry * g2 # G1, G2 h4[i] += c * lentry - g1entry * g1 - g2entry * g2 # -G1, -G2 #print h4 return h4
def h1_bd(grid, Crhs, wrhs, alpha, Nl, Coord): h1 = np.zeros((len(Nl), )) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) #print node.get_value(), node.get_node_id()._id_no for endpt in connect_iterator(grid, node.get_node_id()): #j = int(grid.get_value(endpt)) #print j if grid.get_slave(endpt): coord = grid.get_coord(endpt) c = Crhs(coord[0], coord[1]) w = -alpha * wrhs(coord[0], coord[1]) #print aentry lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] aentry = grid.get_matrix_value(node.get_node_id(), endpt)[1] / float(len(Coord)) # print node.get_node_id()._id_no, endpt._id_no, aentry, c h1[i] += c * aentry + w * lentry return h1
def h2_bd(grid, g1rhs, wrhs, alpha, Nl): h2 = np.zeros((len(Nl), )) for node in not_slave_node(grid): # Ignore slave (or boundary) nodes # Which row corresponds to the current node? i = int(node.get_value()) for endpt in connect_iterator(grid, node.get_node_id()): if grid.get_slave(endpt): coord = grid.get_coord(endpt) #print coord g1 = g1rhs(coord[0], coord[1]) #print g1 w = -alpha * wrhs(coord[0], coord[1]) #print endpt._id_no lentry = grid.get_matrix_value(node.get_node_id(), endpt)[0] g1entry = grid.get_matrix_value(node.get_node_id(), endpt)[2] #print aentry #h2[i] += alpha * g1 * lentry - w * g1entry #G1, G2 h2[i] += alpha * g1 * lentry + w * g1entry #-G1, -G2 return h2
def Setup_Grid(i): #from quick_d import dvector global grid, Coord, Nl, h, Crhs, g1rhs, g2rhs, wrhs, data, intx, inty, nodes, quick_d x = np.linspace(0, 1.0, 50) y = np.linspace(0, 1.0, 50) X, Y = np.meshgrid(x, y) Z2 = Exy(X, Y) data = Z2.flatten() coordx = X.flatten() coordy = Y.flatten() Coord = zip(coordx, coordy) grid = Grid() n = 2**i + 1 h = 1 / float(n - 1) intx, inty = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h)) x1, y1 = np.meshgrid(np.arange(0, 1 + h, h), np.arange(0, 1 + h, h)) nodes = np.vstack([x1.ravel(), y1.ravel()]).T true_soln = zero # Boundares Crhs = Exy g1rhs = Xexy g2rhs = Yexy wrhs = XYexy # Build node-eged structure that allows the connection build_square_grid(n, grid, true_soln) # Store the matrices values on grid nodes build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) # Set value for only interior node. Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) #print 'Start sort' #Sort by node id. Nl = sorted(Nl, key=itemgetter(1)) #print 'Finish sort' #print 'START MAKE_LIST' Nl = [node[0] for node in Nl] #print 'END MAKE_LIST' #print 'START SETVALUE' for node in Nl: node.set_value(Nl.index(node))
wrhs = XYexy # # # build_square_grid(n, grid, true_soln) # build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY, X, Y) Nl=[] for node in (not_slave_node(grid)): Nl.append([node,node.get_node_id()]) Nl=sorted(Nl, key = itemgetter(1)) Nl=[node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) #
def generate_Amatrix(): Nl = [] for node in (not_slave_node(grid)): Nl.append([node, node.get_node_id()]) Nl = sorted(Nl, key=itemgetter(1)) Nl = [node[0] for node in Nl] for node in Nl: node.set_value(Nl.index(node)) Amatrix = zeros([len(Nl), len(Nl)]) #plot_fem_grid(grid) for node in node_iterator(grid): if not node.get_slave(): print node.get_node_id()._id_no, node.get_value() for tri in Interior_triangle_iterator(grid): if (tri[1].get_node_id() < tri[2].get_node_id()): #print tri[0].get_node_id()._id_no, tri[1].get_node_id()._id_no, tri[2].get_node_id()._id_no basis1 = set_polynomial_linear_2D(tri[0], tri[2], tri[1]) basis2 = set_polynomial_linear_2D(tri[1], tri[2], tri[0]) basis3 = set_polynomial_linear_2D(tri[2], tri[1], tri[0]) for i in Coord: if NIn_triangle(tri[0], tri[1], tri[2], i): #print In_triangle(tri[0] , tri[1], tri[2], i) #print tri[0].get_node_id()._id_no, tri[1].get_node_id()._id_no, tri[2].get_node_id()._id_no, i,\ #tri[0].get_coord(), tri[1].get_coord(), tri[2].get_coord() Idi = int(tri[0].get_value()) Amatrix[Idi, Idi] += basis1.eval(i[0], i[1]) * basis1.eval( i[0], i[1]) if not tri[1].get_slave(): #print tri[1].get_node_id()._id_no, tri[1].get_value() #print tri[1].get_value() Idj = int(tri[1].get_value()) Amatrix[Idi, Idj] += basis1.eval(i[0], i[1]) * basis2.eval( i[0], i[1]) if not tri[2].get_slave(): #print tri[2].get_node_id()._id_no Idk = int(tri[2].get_value()) Amatrix[Idi, Idk] += basis1.eval(i[0], i[1]) * basis3.eval( i[0], i[1]) # Remove the data once it's been used #Coord.remove(i) # return Amatrix / float(len(Coord))