def G2(grid, Nl): G2 = 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) 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 matrix_mult(grid, ghost_vector_table, vector_table, d_index, q_index): """ Evaluate q = Ad""" # Loop over the full nodes for node in node_iterator(grid): node_id = node.get_node_id() # Do not change the slave nodes if not node.get_slave(): # Find sum A_{ij} d_j sum = 0.0 # Loop over the end points connect to node i for endpt in connect_iterator(grid, node_id): # Find (A)_{ij} stiffness = grid.get_matrix_value(node_id, endpt) # Find d_j, which may be a full or ghost node if grid.is_in(endpt): d = vector_table[str(endpt)][d_index] else: d = ghost_vector_table[str(endpt)][d_index] # Update the sum sum = sum + stiffness * d # Store the result vector_table[str(node_id)][q_index] = sum
def Amatrix(grid, Coord, Nl, h): Amatrix = lil_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 print len(Coord) return Amatrix / float(len(Coord))
def Stencil_L(grid, Nl): 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? idi = int(node.get_value()) for endpt1 in connect_iterator(grid, node.get_node_id()): # Which column corresponds to the current node? idj = int(grid.get_value(endpt1)) # We must not include slave nodes in the matrix columns if not grid.get_slave(endpt1): if idi - idj == 0: Lmatrix[idi, idj] = 4 elif abs(idi - idj) == 1: Lmatrix[idi, idj] = -1 elif abs(idi - idj) == int(np.sqrt(len(Nl))): Lmatrix[idi, idj] = -1 return Lmatrix
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 FD_G1(grid, Nl, h): G1 = lil_matrix((len(Nl), len(Nl))) # for node in Nl: # # node.set_value(Nl.index(node)) for node in node_iterator(grid): if not node.get_slave(): i = int(node.get_value()) for endpt1 in connect_iterator(grid, node.get_node_id()): j = int(grid.get_value(endpt1)) if not grid.get_slave(endpt1): if i - j == 0: G1[i, j] = 1 * -h elif i - j == np.sqrt(len(Nl)): G1[i, j] = 0 * -h elif i - j == -np.sqrt(len(Nl)): G1[i, j] = h # elif i-j == 1: # # # G1[i,j] = -h # # elif i-j == -1: # # # G1[i,j] = -h # # elif i-j == np.sqrt(len(Nl))+1: # # G1[i,j] =-h # # elif i-j == -np.sqrt(len(Nl))+1: # # G1[i,j] = h return G1
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 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 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 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 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 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 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 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 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 Stencil_A(grid, Nl, h): Amatrix = lil_matrix((len(Nl), len(Nl))) for node in node_iterator(grid): if not node.get_slave(): idi = int(node.get_value()) # Loop over the matrix entries in the current row for endpt in connect_iterator(grid, node.get_node_id()): # Which column corresponds to the current node? idj = int(grid.get_value(endpt)) # We must not include slave nodes in the matrix columns if not grid.get_slave(endpt): if idi - idj == 0: Amatrix[idi, idj] = h**2 / 2 elif abs(idi - idj) == 1: Amatrix[idi, idj] = h**2 / 12 elif abs(idi - idj) == int(np.sqrt(len(Nl))): Amatrix[idi, idj] = h**2 / 12 elif abs(idi - idj) == int(np.sqrt(len(Nl))) + 1: Amatrix[idi, idj] = h**2 / 12 return Amatrix
IntNode=[] for node in (not_slave_node(grid)): #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] #print(IntNode_Ordered,IntNode_Ordered[0].get_node_id()._id_no) # # # for j, node in enumerate(IntNode_Ordered): boundary_sum=[] id1=node.get_node_id() coord=node.get_coord() for endpt in (connect_iterator(grid, id1)): if grid.get_slave(endpt): connect_value=grid.get_matrix_value(id1, endpt) #print(node.get_node_id()._id_no,endpt._id_no) boundary_sum.append(true_soln(grid.get_coord(endpt))*connect_value) load_vector[j]=load_vector[j]-sum(boundary_sum) #print(load_vector) # for i in range(len(IntNode_Ordered)): load_value=IntNode_Ordered[i].get_load() #print(load_value) load_vector[i]= load_vector[i]+load_value #
############################################################## # Enumerate interior NodeID in order of ID NodeID_Load=[] for q, node in enumerate(not_slave_node(grid)): #print(q) NodeID_Load.append(node.get_node_id()) NodeID_Load.sort() # For every NodeID in NodeID_load, find its connecting nodes, reorder them and assign them in stiffness matrix for i, nodeid in enumerate(NodeID_Load): Endpt=[] for j, endpt1 in enumerate(connect_iterator(grid, nodeid)): Endpt.append(endpt1) Endpt.sort() #B=[] for k,endpt2 in enumerate(Endpt): #B.append([k,endpt2]) stiffness = grid.get_matrix_value(nodeid,endpt2) fem_matrix[i,k]=stiffness*float(64) #print(i,k,nodeid._id_no,endpt2._id_no,stiffness) #print(nodeid._id_no,endpt2._id_no,stiffness) ############################################################## #print(fem_matrix) #############################################################
def DD_init(grid): count = 0 for node in node_iterator(grid): if not node.get_slave(): #print(node.get_coord()) node.set_value(count) count = count + 1 #print count, 'count' #print(count,'count') # Initialise the A matrix #fem_matrix = csr_matrix((count, count), dtype=float) fem_matrix = eye(count) # Initialise the load vector #load_vector = zeros([count, 1]) # Define the A matrix and load vector 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()) # Add in the entry to the load vector #coord = node.get_coord() #load_vector[i] = load_vector[i]+rhs(coord) # Loop over the matrix entries in the current row for endpt1 in connect_iterator(grid, node.get_node_id()): if grid.is_in(endpt1): j = int(grid.get_value(endpt1)) stiffness = grid.get_matrix_value(node.get_node_id(), endpt1) if not grid.get_slave(endpt1): fem_matrix[i, j] = stiffness # if grid.reference_ghost_table().is_in(endpt1): # # j = int(grid.reference_ghost_table().get_value(endpt1)) # # #stiffness = grid.get_matrix_value(node.get_node_id(), endpt1) # # if not grid.reference_ghost_table().get_slave(endpt1): # # fem_matrix[i, j] = 0 #if not grid.is_in(endpt1): #print "not in grid" # Which column corresponds to the current node? # What is the corresponding matrix value (in the FEM grid) #stiffness = grid.get_matrix_value(node.get_node_id(), endpt1) # We must not include slave nodes in the matrix columns #if not grid.get_slave(endpt1): #fem_matrix[i, j] = stiffness #print(fem_matrix) # Update the load vector to take non-zero boundary conditions # into account #else: #coord = grid.get_coord(endpt1) #load_vector[i] = load_vector[i]-stiffness*true_soln(coord) #print fem_matrix # fem= splu(fem_matrix) # fem = LinearOperator(fem.shape,matvec=fem.solve) return splu(fem_matrix)
# Define the A matrix and load vector 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()) # Add in the entry to the load vector coord = node.get_coord() load_vector[i] = load_vector[i] + rhs(coord) # Loop over the matrix entries in the current row 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): fem_matrix[i, j] = stiffness # Update the load vector to take non-zero boundary conditions # into account else: coord = grid.get_coord(endpt1)