import numpy as np # 定义三角形节点坐标和单元节点关系 nodes = np.array([[0, 0], [0, 1], [1, 0]]) elems = np.array([[0, 1, 2]]) # 定义材料的弹性模量和泊松比 E = 210e9 nu = 0.3 # 计算材料的弹性矩阵 D = E / (1 - nu ** 2) * np.array([[1, nu, 0], [nu, 1, 0], [0, 0, (1 - nu) / 2]]) # 构造三角形常应变单元的刚度矩阵 def get_element_stiffness_matrix(elem): x1, y1 = nodes[elem[0]] x2, y2 = nodes[elem[1]] x3, y3 = nodes[elem[2]] A = 0.5 * abs(x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) B = np.array([[y2 - y3, 0, y3 - y1, 0, y1 - y2, 0], [0, x3 - x2, 0, x1 - x3, 0, x2 - x1], [x3 - x2, y2 - y3, x1 - x3, y3 - y1, x2 - x1, y1 - y2]]) return A * np.linalg.inv(B.T @ D @ B) # 构造整体刚度矩阵 num_nodes = nodes.shape[0] num_elems = elems.shape[0] K = np.zeros((2 * num_nodes, 2 * num_nodes)) for i in range(num_elems): elem = elems[i] ke = get_element_stiffness_matrix(elem) for r in range(3): for c in range(3): K[2 * elem[r], 2 * elem[c]] += ke[2 * r, 2 * c] K[2 * elem[r], 2 * elem[c] + 1] += ke[2 * r, 2 * c + 1] K[2 * elem[r] + 1, 2 * elem[c]] += ke[2 * r + 1, 2 * c] K[2 * elem[r] + 1, 2 * elem[c] + 1] += ke[2 * r + 1, 2 * c + 1] # 定义边界条件 fixed_nodes = [0] fixed_dofs = [2 * i for i in fixed_nodes] free_dofs = [i for i in range(2 * num_nodes) if i not in fixed_dofs] # 定义外力 F = np.zeros(2 * num_nodes) F[2] = -5000 # 求解位移场 K_ff = K[np.ix_(free_dofs, free_dofs)] F_f = F[free_dofs] u_f = np.linalg.solve(K_ff, F_f) u = np.zeros(2 * num_nodes) u[free_dofs] = u_f # 输出结果 print("位移场:") print(u.reshape(-1, 2)) print("应力场:") for i in range(num_elems): x1, y1 = nodes[elem[0]] x2, y2 = nodes[elem[1]] x3, y3 = nodes[elem[2]] elem = elems[i] u_e = u[2 * elem] B_e = np.array([[y2 - y3, 0, y3 - y1, 0, y1 - y2, 0], [0, x3 - x2, 0, x1 - x3, 0, x2 - x1], [x3 - x2, y2 - y3, x1 - x3, y3 - y1, x2 - x1, y1 - y2]]) epsilon_e = B_e @ u_e sigma_e = D @ epsilon_e print(sigma_e)这段代码中的定义边界条件和定义外力
时间: 2024-04-05 12:31:46 浏览: 137
定义边界条件的代码如下:
fixed_nodes = [0]
fixed_dofs = [2 * i for i in fixed_nodes]
其中,fixed_nodes表示被固定的节点的编号,fixed_dofs表示这些节点在整体位移场中的自由度编号,其中乘以2是因为每个节点有两个自由度(x和y方向)。这里的代码表示将节点0固定。
定义外力的代码如下:
F = np.zeros(2 * num_nodes)
F[2] = -5000
其中,F表示整体外力向量,num_nodes表示节点总数,这里的代码表示将节点2处施加一个向下的力5000N。同样乘以2是因为每个节点有两个自由度(x和y方向)。
阅读全文