from dolfin import *
from mshr import *
import matplotlib.pyplot as plt
import numpy as np
# Define mesh and geometry
L = 1.0 # length of the domain
H = 0.1 # height of the domain
Nx = 50 # number of elements in x direction
Ny = 10 # number of elements in y direction
mesh = RectangleMesh(Point(0, 0), Point(L, H), Nx, Ny)
plot(mesh)
# Define function spaces
V = FunctionSpace(mesh, "P", 1) # scalar function space
V_vector = VectorFunctionSpace(mesh, "P", 1) # vector function space
# Define boundary conditions
def left(x, on_boundary):
return on_boundary and near(x[0], 0.0)
def right(x, on_boundary):
return on_boundary and near(x[0], L)
u_left = Constant(0.0)
u_right = Expression("t", t=0.0, degree=1)
bc_left = DirichletBC(V_vector.sub(0), u_left, left)
bc_right = DirichletBC(V_vector.sub(0), u_right, right)
bcs = [bc_left, bc_right]
# Define optimization parameters
E = 1.0 # Young's modulus
nu = 0.3 # Poisson's ratio
# Define optimization problem
u = TrialFunction(V_vector)
v = TestFunction(V_vector)
rho = Function(V)
rho.assign(Constant(1.0)) # initial material distribution
mu = E / (2 * (1 + nu))
lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
def epsilon(u):
return 0.5 * (grad(u) + grad(u).T)
def sigma(u):
return lmbda * div(u) * Identity(2) + 2 * mu * epsilon(u)
F = inner(sigma(u), epsilon(v)) * dx
# Define time-stepping parameters
dt = 0.1 # time step
T = 1.0 # final time
t = 0.0 # current time
# Time-stepping loop
u_sol = Function(V_vector)
while t <= T:
# Update boundary condition
u_right.t = t
# Solve optimization problem
solve(lhs(F) == rhs(F), u_sol, bcs)
# Update material distribution
rho.vector()[:] *= 1.01
for i in range(rho.vector().size()):
if rho.vector()[i] > 1.0:
rho.vector()[i] = 1.0
# Plot solution
plt.figure()
plot(u_sol.sub(0), title="Displacement", mode="displacement")
plt.colorbar()
# Update time
t += dt
# Plot final material distribution
plt.figure()
plot(rho, title="Material distribution")
plt.colorbar()
plt.show()
from dolfin import *
from mshr import *
import matplotlib.pyplot as plt
import numpy as np
# 声明网格和几何体
L = 1.0 # 域的长度
H = 0.1 # 域的高度
Nx = 50 # 在x方向上的元素个数
Ny = 10 # 在y方向上的元素个数
mesh = RectangleMesh(Point(0, 0), Point(L, H), Nx, Ny)
plot(mesh)
# 定义函数空间
V = FunctionSpace(mesh, "P", 1) # 标量函数空间
V_vector = VectorFunctionSpace(mesh, "P", 1) # 矢量函数空间
# 定义边界条件
def left(x, on_boundary):
return on_boundary and near(x[0], 0.0)
def right(x, on_boundary):
return on_boundary and near(x[0], L)
u_left = Constant(0.0)
u_right = Expression("t", t=0.0, degree=1)
bc_left = DirichletBC(V_vector.sub(0), u_left, left)
bc_right = DirichletBC(V_vector.sub(0), u_right, right)
bcs = [bc_left, bc_right]
# 定义优化参数
E = 1.0 # 杨氏模量
nu = 0.3 # 泊松比
# 定义优化问题
u = TrialFunction(V_vector)
v = TestFunction(V_vector)
rho = Function(V)
rho.assign(Constant(1.0)) # 初始材料分布
mu = E / (2 * (1 + nu))
lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
def epsilon(u):
return 0.5 * (grad(u) + grad(u).T)
def sigma(u):
return lmbda * div(u) * Identity(2) + 2 * mu * epsilon(u)
F = inner(sigma(u), epsilon(v)) * dx
# 定义时间步长参数
dt = 0.1 # 步长
T = 1.0 # 最后时间
t = 0.0 # 当前时间
# 步进循环
u_sol = Function(V_vector)
while t <= T:
# 更新边界条件
u_right.t = t
# 求解优化问题
solve(lhs(F) == rhs(F), u_sol, bcs)
# 更新材料分布
rho.vector()[:] *= 1.01
for i in range(rho.vector().size()):
if rho.vector()[i] > 1.0:
rho.vector()[i] = 1.0
# 图示结果
plt.figure()
plot(u_sol.sub(0), title="Displacement", mode="displacement")
plt.colorbar()
# 更新时间
t += dt
# 图示最终材料分布
plt.figure()
plot(rho, title="Material distribution")
plt.colorbar()
plt.show()