Hello everyone, I have utilized the null space while solving an elasticity problem in a 2D rectangular domain. I applied horizontal traction at the boundary where x=0. Below is the code:
from dolfin import *
import ufl
import numpy as np
# Create mesh and define function space
mesh = UnitSquareMesh(10, 10)
V = VectorFunctionSpace(mesh, "CG", 2)
u = TrialFunction(V)
v = TestFunction(V)
# Parameters
E = Constant(50e3)
nu = Constant(0.2)
mu = E/2/(1+nu)
lmbda = E*nu/(1+nu)/(1-2*nu)
def eps(v):
return 0.5*(grad(v) + grad(v).T)
def sigma(v):
return lmbda*tr(eps(v))*Identity(2) + 2.0*mu*eps(v)
class Leftboundary(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 0) and on_boundary
left = Leftboundary()
# Discrete traction
T_data = [0.01] * 11
T_coordinates = [[0, 0], [0, 0.1], [0, 0.2], [0, 0.3], [0, 0.4], [0, 0.5], [0, 0.6], [0, 0.7], [0, 0.8], [0, 0.9], [0, 1.0]]
T_space = FunctionSpace(mesh, "CG", 1)
T_load = Function(T_space)
vertex_mark = MeshFunction("size_t", mesh, 0, 0)
left.mark(vertex_mark, 1)
vertices = vertex_mark.where_equal(1)
vertex_to_dof_map = vertex_to_dof_map(T_space)
# Get all boundary coordinates
dof_coords = T_space.tabulate_dof_coordinates()
boundary_dofs = [vertex_to_dof_map[vertex] for vertex in vertices]
boundary_coords = np.array([dof_coords[boundary_dof] for boundary_dof in boundary_dofs])
points_A = np.expand_dims(boundary_coords, 1)
points_B = np.expand_dims(T_coordinates, 0)
distances = np.sum(np.square(points_A - points_B), axis=2)
is_close = distances < 1e2 * DOLFIN_EPS
positions = np.nonzero(is_close)
for row, col in zip(*positions):
T_load.vector()[boundary_dofs[row]] = T_data[col]
boundary_mark = MeshFunction("size_t", mesh,1)
left.mark(boundary_mark, 1)
ds = Measure('ds', domain=mesh, subdomain_data = boundary_mark)
a = inner(sigma(u), eps(v)) * dx
Traction = ufl.as_vector((T_load, 0))
L = dot(Traction, v) * ds(1)
# Assemble system
A = assemble(a)
b = assemble(L)
# Solution Function
u = Function(V)
# Create Krylov solver
solver = PETScKrylovSolver("cg")
solver.set_operator(A)
# Create vector that spans the null space and normalize
null_vec = Vector(u.vector())
V.dofmap().set(null_vec, 1.0)
null_vec *= 1.0/null_vec.norm("l2")
# Create null space basis object and attach to PETSc matrix
null_space = VectorSpaceBasis([null_vec])
as_backend_type(A).set_nullspace(null_space)
null_space.orthogonalize(b)
solver.solve(u.vector(), b)
However, the runtime output is unclear, and I am unsure of where the issue lies:
Traceback (most recent call last):
File "/home/dyfluid/Desktop/contact2DV/test.py", line 74, in <module>
solver.solve(u.vector(), b)
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
I hope to receive some assistance, thank you.