Hi @dokken, thanks for the suggestion, I tried to use the code from Bitbucket after some modifications but ran into error while executing this cell -
# Assemble system, applying boundary conditions and preserving
# symmetry)
A, b = assemble_system(a, L, bcs)
Error -
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_739/3299545883.py in <module>
1 # Assemble system, applying boundary conditions and preserving
2 # symmetry)
----> 3 A, b = assemble_system(a, L, bcs)
/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/assembling.py in assemble_system(A_form, b_form, bcs, x0, form_compiler_parameters, add_values, finalize_tensor, keep_diagonal, A_tensor, b_tensor, backend)
428 assembler.assemble(A_tensor, b_tensor, x0)
429 else:
--> 430 assembler.assemble(A_tensor, b_tensor)
431
432 return A_tensor, b_tensor
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
***
*** fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to match boundary condition to function space.
*** Reason: Function space on axis 0 does not contain BC space.
*** Where: This error was encountered inside SystemAssembler.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: ubuntu
*** -------------------------------------------------------------------------
Minimum working example which I’m using -
from dolfin import *
import matplotlib.pyplot as plt
# Test for PETSc
if not has_linear_algebra_backend("PETSc"):
print("DOLFIN has not been configured with PETSc. Exiting.")
exit()
# Set backend to PETSC
parameters["linear_algebra_backend"] = "PETSc"
def build_nullspace(V, x):
"""Function to build null space for 3D elasticity"""
# Create list of vectors for null space
nullspace_basis = [x.copy() for i in range(6)]
# Build translational null space basis
V.sub(0).dofmap().set(nullspace_basis[0], 1.0);
V.sub(1).dofmap().set(nullspace_basis[1], 1.0);
V.sub(2).dofmap().set(nullspace_basis[2], 1.0);
for x in nullspace_basis:
x.apply("insert")
# Create vector space basis and orthogonalize
basis = VectorSpaceBasis(nullspace_basis)
basis.orthonormalize()
return basis
from dolfin import *
from mshr import *
import matplotlib.pyplot as plt
#Making a cylindrical geometry
cylinder = Cylinder(Point(0.5, 0.5, 0), Point(0.5, 0.5, 2), 0.25, 0.25)
cube = Box(Point(0, 0, 0),Point(1, 1, 1))
domain = cube - cylinder
# Making Mesh (40 corresponds to the mesh density)
mesh = generate_mesh(domain, 40)
# Stress and strain computation
E = Constant(200e3) #E = 200 GPa
nu = Constant(0.3)
model = "plane_stress"
mu = E/2/(1+nu)
lmbda = E*nu/(1+nu)/(1-2*nu)
if model == "plane_stress":
lmbda = 2*mu*lmbda/(lmbda+2*mu)
# Define strain and stress
def epsilon(u):
return sym(grad(u))
def sigma(u):
return lmbda*tr(epsilon(u))*Identity(3) + 2*mu*epsilon(u)
# Create function space
V = VectorFunctionSpace(mesh, "Lagrange", 1)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
a = inner(sigma(u), epsilon(v))*dx
L = inner(f, v)*dx
# Set up boundary condition
class left(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and near(x[0], 0., 0.1)
class right(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and near(x[0], 1., 0.1)
boundaries = MeshFunction("size_t", mesh, mesh.geometry().dim() - 1)
left().mark(boundaries, 1)
right().mark(boundaries, 2)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
bc_left = DirichletBC(V, Constant((0., 0., 0.)), boundaries, 1)
bc_right = DirichletBC(V.sub(0), Constant(.1) , boundaries, 2)
bcs = [bc_left, bc_right]
# Assemble system, applying boundary conditions and preserving
# symmetry)
A, b = assemble_system(a, L, bcs)