Hi all!
I’m struggling to modify this example: Implementation — FEniCSx tutorial
so that I can create a FE model of 1D beam with a cylindrical section (I’m trying to model a wire) clamped on one side and free in the other side.
I guess it’s a fairly simple example when used to fenicsx and dolfinx, but I’m new to it so I would be grateful if someone could give me a basis to work with. Thanks!
Here’s my code right now (not working):
from dolfinx import mesh, fem, plot, io, default_scalar_type
from dolfinx.fem.petsc import LinearProblem
from mpi4py import MPI
import ufl
import numpy as np
L = 1
W = 0.2
mu = 1
rho = 1
delta = W / L
gamma = 0.4 * delta**2
beta = 1.25
lambda_ = beta
g = gamma
# nb_cells = 20
gdim, shape, degree = 1, "interval", 1
cell = ufl.Cell(shape, geometric_dimension=gdim)
domain = ufl.Mesh(ufl.VectorElement("Lagrange", cell, degree))
x = np.array([[0], [0.2], [0.6], [1], [1.3]])
cells = np.array([[0, 1], [1, 2], [2, 3], [3, 4]], dtype=np.int64)
domain = mesh.create_mesh(MPI.COMM_WORLD, cells, x, domain)
V = fem.FunctionSpace(domain, ("Lagrange", 1))
def clamped_boundary(x):
return np.isclose(x[1], 0)
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, clamped_boundary)
u_D = np.array((0.,) * 1, dtype=default_scalar_type)
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)
# u_zero = np.array((0,) * 1, dtype=default_scalar_type)
# print(fem.locate_dofs_geometrical(V, clamped_boundary))
# bc = fem.dirichletbc(u_zero, fem.locate_dofs_geometrical(V, clamped_boundary), V)
T = fem.Constant(domain, default_scalar_type((0, 0, 0)))
ds = ufl.Measure("ds", domain=domain)
def epsilon(u):
return ufl.sym(ufl.grad(u)) # Equivalent to 0.5*(ufl.nabla_grad(u) + ufl.nabla_grad(u).T)
def sigma(u):
return lambda_ * ufl.nabla_div(u) * ufl.Identity(len(u)) + 2 * mu * epsilon(u)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
f = fem.Constant(domain, default_scalar_type((0, 0, -rho * g)))
a = ufl.inner(sigma(u), epsilon(v)) * ufl.dx
L = ufl.dot(f, v) * ufl.dx + ufl.dot(T, v) * ds
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
# print(type(uh))
# print(uh.x.array)
with io.XDMFFile(domain.comm, "deformation.xdmf", "w",encoding=io.XDMFFile.Encoding.ASCII) as xdmf:
xdmf.write_mesh(domain)
uh.name = "Deformation"
xdmf.write_function(uh)