from sympy import *
h,x = symbols('h x')
M = Matrix([[1,0,0,0],[0,1,0,0],[1,h,h**2,h**3],[0,1,2*h,3*h**2]])
Minv = M.inv()
M
Minv
C = Matrix([1,x,x**2,x**3]) #entries for order three poly
u = Minv.T * C
du = diff(Minv.T * C,x)
u
du
Aii = integrate(u[2]*u[2],(x,0,h)) + integrate(u[0]*u[0],(x,0,h)) #diagonal
Aii
Aii_1 = integrate(u[0]*u[2],(x,0,h)) #u_i-1, u_i and the corresponding
Aii_1
Bii = integrate(du[2]*du[2],(x,0,h)) + integrate(du[0]*du[0],(x,0,h)) #diagonal
Bii
Bii_1 = integrate(du[0]*du[2],(x,0,h)) #du_i-1, du_i and the corresponding
Bii_1
Aii = integrate(u[3]*u[3],(x,0,h)) + integrate(u[1]*u[1],(x,0,h)) #diagonal
Aii
Aii_1 = integrate(u[1]*u[3],(x,0,h)) #u_i-1, u_i and the corresponding
Aii_1
Bii = integrate(du[3]*du[3],(x,0,h)) + integrate(du[1]*du[1],(x,0,h)) #diagonal
Bii
Bii_1 = integrate(du[1]*du[3],(x,0,h)) #du_i-1, du_i and the corresponding
Bii_1
Aii = integrate(u[2]*u[3],(x,0,h)) + integrate(u[0]*u[1],(x,0,h)) #diagonal
Aii
Aii_1 = integrate(u[2]*u[1],(x,0,h)) #off diagonal
Aii_1
Bii = integrate(du[2]*du[3],(x,0,h)) + integrate(du[0]*du[1],(x,0,h)) #diagonal
Bii
Bii_1 = integrate(du[1]*du[2],(x,0,h)) #du_i-1, du_i and the corresponding
Bii_1
beam bending
import numpy as np
import matplotlib.pyplot as plt
#we reuse M, Minv, u, du from the the prior problem: replicated below:
h,x = symbols('h x')
M = Matrix([[1,0,0,0],[0,1,0,0],[1,h,h**2,h**3],[0,1,2*h,3*h**2]])
Minv = M.inv()
C = Matrix([1,x,x**2,x**3]) #entries for order three poly
u = Minv.T * C
du = diff(u, x)
ddu = diff(du, x)
#element stiffness matrix
k = zeros(4,4)
for i in range(k.shape[0]):
for j in range(k.shape[1]):
k[i,j] = integrate(ddu[i]*ddu[j],(x,0,h)).subs(h,1)
# node amount
N = 20
# below logic updated from Neil's example @ https://gitlab.cba.mit.edu/classes/864.23/site/-/blob/main/code/fem/beam.py
# bc i couldn't get it to work alas!
K = zeros(N,N)
K[0,0] = k[0,0]
K[0,1] = k[0,1]
K[0,2] = k[0,2]
K[0,3] = k[0,3]
K[1,0] = k[1,0]
K[1,1] = k[1,1]
K[1,2] = k[1,2]
K[1,3] = k[1,3]
for i in range(2,N-3,2):
K[i,i-2] = k[2,0]
K[i,i-1] = k[2,1]
K[i,i] = k[2,2]+k[0,0]
K[i,i+1] = k[2,3]+k[0,1]
K[i,i+2] = k[0,2]
K[i,i+3] = k[0,3]
K[i+1,i-2] = k[3,0]
K[i+1,i-1] = k[3,1]
K[i+1,i] = k[3,2]+k[1,0]
K[i+1,i+1] = k[3,3]+k[1,1]
K[i+1,i+2] = k[1,2]
K[i+1,i+3] = k[1,3]
K[N-2,N-4] = k[2,0]
K[N-2,N-3] = k[2,1]
K[N-2,N-2] = k[2,2]
K[N-2,N-1] = k[2,3]
K[N-1,N-4] = k[3,0]
K[N-1,N-3] = k[3,1]
K[N-1,N-2] = k[3,2]
K[N-1,N-1] = k[3,3]
K = np.array(K).astype(np.float64)
# boundary conditions
K[:,0] = 0
K[0,:] = 0
K[:,1] = 0
K[1,:] = 0
K[0,0] = 1
K[1,1] = 1
Kinv = np.linalg.inv(K)
from matplotlib.widgets import Slider
%matplotlib notebook
f_init = -1 # initial value
F = np.zeros(N)
F[N-1] = f_init
def f(force):
F = np.zeros(N)
F[N-1] = force
return np.dot(Kinv,F)[::2]
fig, ax = plt.subplots()
line, = ax.plot(f(f_init))
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)
axForce = fig.add_axes([0.1, 0.25, 0.0225, 0.63])
force_Slider = Slider(
ax=axForce,
label="applied force",
valmin=-10,
valmax=0,
valinit=f_init,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
line.set_ydata(f(force_Slider.val))
fig.canvas.draw_idle()
force_Slider.on_changed(update)
plt.show()
# import taichi as ti
# ti.init(arch=ti.cpu)
# #construct global stiffness matrix
# K = ti.field(float, shape=(N,N))
# K.fill(0)
# @ti.kernel
# def fillK():
# for i,j in K:
# if i <=1 and j <= 3:
# K[i,j] = k[i,j]
# elif i >= (N-2) and j <=3:
# K[i,(N-4+j)] = k[-(i-N),j]
# elif j == i:
# K[i,j] = k[0,0] + k[2,2]
# else:
# #ahugh
# fillK()
import taichi as ti
ti.init(arch=ti.cpu)
# material properties
E = 2e9 # Pa
I = 100 # m^4
nodes = 10 # node amount
l = 0.1 # element length
p = ti.Vector.field(2, dtype=float, shape=(nodes,)) # x,y position of each node
theta = ti.Vector.field(2, dtype=float, shape=(nodes,)) # slope at each node
Fs = ti.Vector.field(1, dtype=float, shape=(nodes,)) # reaction shear at each node
M = ti.Vector.field(1, dtype=float, shape=(nodes,)) # moment at each node
u = ti.Vector.field(1, dtype=float, shape=(nodes,)) # applied force
# element stiffness matrix
kn = (E*I/l**2)*np.array([[12, 6*l, -12, 6*l],
[6*l, 4*l**2, -6*l, 2*l**2],
[-12, -6*l, 12, -6*l],
[6*l, 2*l**2, -6*l, 4*l**4]])
k = ti.field(float, shape=(4,4))
k.from_numpy(kn)
# global stiffness matrix
K = ti.field(float, shape=(4*nodes,4*nodes))
@ti.kernel
def initBeam():
for i in p:
p[i] = [i*l, 0]
theta = [i*l, 0]
@ti.kernel
def initU():
for i in u:
u[i] = [0]
@ti.kernel
def getGlobalStiffness():
for i,j in K:
K[i,j] #will finish later.
#print(k)
initBeam()
initU()