elems = np.array([[0,1],[1,2]]) bcs = [[0, 2], [0.0, 0.0]] load = [[0, 2], [0, 0]] nnodes, dpn_0 = nodes.shape # node count and dofs per node nep = p-1 # number of enrichment per node dpn_er = nep * 2 # enriched dof per node dpn = dpn_0 + dpn_er # total dof per node dofs = dpn * nnodes # total number of dofs C = 1 # material C matrix # K and F initialization K = sp.lil_matrix((dofs,dofs)) F = np.zeros(dofs) # Gauss integration point gauss_k = grule(p+1) gauss_f = grule(p+5) # Assembling stiffness matrix and force vector for e,conn in enumerate(elems): X = nodes[conn] ldofs = dpn * len(conn) k = np.zeros((ldofs, ldofs)) f = np.zeros(ldofs) eft = elementdofs(e, conn, p, dpn_0, nnodes) print(eft) # Stiffness matrix for i, xi in enumerate(gauss_k.xi): N_k, B_k, N_0_k, dN_0_k = shapefns(xi, X, p, h)
p = 1 h = 0.5 nodes = np.array([[0.0], [0.5], [1.0]]) elems = np.array([[0, 1], [1, 2]]) bcs = [[0, 2], [0.0, 0.0]] load = [[0, 2], [0, 0]] nnodes, dpn_0 = nodes.shape # node count and dofs per node dpn_er = 1 # enriched dof per node, so one node one enrichment dpn = dpn_0 + dpn_er # total dof per node dofs = dpn * nnodes # total number of dofs material = tuple([1, 1]) gauss = grule(2) K = sp.lil_matrix((dofs, dofs)) F = np.zeros(dofs) #-- Assembling stiffness matrix and force vector... for e, conn in enumerate(elems): # coordinate array for the element X = nodes[conn] ldofs_0 = dpn_0 * len(conn) ldofs_er = dpn_er * len(conn) k_0 = np.zeros((ldofs_0, ldofs_0)) k_er = np.zeros((ldofs_er, ldofs_er)) k_0_er = np.zeros((ldofs_0, ldofs_er)) k_er_0 = np.zeros((ldofs_er, ldofs_0)) f_0 = np.zeros(ldofs_0)
import scipy.sparse as sp from scipy.sparse.linalg import spsolve from shape_fns_p import legendre, shapes, mapping from bodyforce import BodyF from elastic import constitutive p=6 h = 0.5 nodes = np.array([[0.0], [0.5], [1.0]]) elems = np.array([[0, 1], [1, 2]]) bcs = [[0, 2+(p-1)], [0.0, 0.0]] load = [[0, 2+(p-1)], [0, 0]] nnodes, dpn = nodes.shape # node count and dofs per node dofs = dpn * nnodes + 2*(p-1) # total number of dofs material = tuple([1, 1]) gauss = grule(p+1) K = sp.lil_matrix((dofs, dofs)) F = np.zeros(dofs) #-- Assembling stiffness matrix and force vector... for e, conn in enumerate(elems): # coordinate array for the element X = nodes[conn] ldofs = dpn * len(conn) + (p-1) k = np.zeros((ldofs, ldofs)) f = np.zeros(ldofs) # element degree of freedom if p == 1: eft = np.array([dpn * n + i for n in conn for i in range(dpn)])
def pFEMsol(p, case): h = 0.5 nodes = np.array([[0.0], [0.5], [1.0]]) elems = np.array([[0, 1], [1, 2]]) bcs = [[0, 2 + (p - 1)], [0.0, 0.0]] load = [[0, 2 + (p - 1)], [0, 0]] nnodes, dpn = nodes.shape # node count and dofs per node dofs = dpn * nnodes + 2 * (p - 1) # total number of dofs material = tuple([1, 1]) gauss = grule(p + 1) K = sp.lil_matrix((dofs, dofs)) F = np.zeros(dofs) #-- Assembling stiffness matrix and force vector... for e, conn in enumerate(elems): # coordinate array for the element X = nodes[conn] ldofs = dpn * len(conn) + (p - 1) k = np.zeros((ldofs, ldofs)) f = np.zeros(ldofs) # element degree of freedom if p == 1: eft = np.array([dpn * n + i for n in conn for i in range(dpn)]) if p > 1: if e == 0: eft = np.array([dpn * n for n in range(ldofs)]) elif e > 0: eft_0 = np.array([1 + p * (e - 1), 1 + p * e]) eft_1 = np.array([1 + p * e + (n + 1) for n in range(p - 1)]) eft = np.append(eft_0, eft_1) # derive element k matrix for i, xi in enumerate(gauss.xi): phi, dphi = shapes(xi, p) j = h / 2 Jinv = 1 / j B = np.dot(dphi, Jinv) BB = np.kron(B.T, np.identity(dpn)) matDT = constitutive(material, dpn) k += gauss.wgt[i] * j * np.dot(np.dot(BB.T, matDT), BB) # assemble global K matrix K[eft[:, np.newaxis], eft] += k # derive body force vector f bodyf = grule(p + 3) for i, xi in enumerate(bodyf.xi): phi, dphi = np.array(shapes(xi, p)) Xxi = np.dot(X.T, [phi[0], phi[1]]) #Xxi = mapping(xi,e) j = h / 2 matDT = constitutive(material, dpn) f += bodyf.wgt[i] * j * phi * BodyF(matDT, Xxi, case) # assemble global body force vector F[eft] += f #-- Applying boundary conditions... zero = bcs[0] F -= K[:, zero] * bcs[1] K[:, zero] = 0 K[zero, :] = 0 K[zero, zero] = 1 F[zero] = bcs[1] # apply loads F[load[0]] += load[1] #-- Solving system of equations... u = spsolve(K.tocsr(), F) #-- Calculating strain energy... U = 0.5 * np.dot((u.T * K), u) return dofs, U