Esteemed,
I would like to take advantage of the topic and also the code made @bleyerj in post 2, where I use the code made by him also for an elastic analysis of a cantilever, however I insert 2 holes in the middle of the beam. See the error and the code below:
Error:
File "/Users/.../2D_elasticity/2D_elasticity_withhole.py", line 77, in <module>
solve(a == l, u, BCs)
File "/Users/.../opt/anaconda3/envs/fenics2/lib/python3.8/site-packages/dolfin/fem/solving.py", line 220, in solve
_solve_varproblem(*args, **kwargs)
File "/Users/.../opt/anaconda3/envs/fenics2/lib/python3.8/site-packages/dolfin/fem/solving.py", line 241, in _solve_varproblem
problem = LinearVariationalProblem(eq.lhs, eq.rhs, u, bcs,
File "/Users/.../opt/anaconda3/envs/fenics2/lib/python3.8/site-packages/dolfin/fem/problem.py", line 59, in __init__
cpp.fem.LinearVariationalProblem.__init__(self, a, L, u._cpp_object, bcs)
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 define linear variational problem a(u, v) = L(v) for all v.
*** Reason: Expecting the boundary conditions to to live on (a subspace of) the trial space.
*** Where: This error was encountered inside LinearVariationalProblem.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset:
*** -------------------------------------------------------------------------
Code:
#2D linear elasticity
## Introduction
from fenics import *
from mshr import *
L1 = 2.5
H = 1.
#Define geometry for background
x1 = 1.25
y1 = 0.25
x2 = 1.25
y2 = 0.75
r = 0.1
domain = Rectangle(Point(0, 0), Point(L1, H)) - Circle(Point(x1, y1), r) - Circle(Point(x2, y2), r)
mesh = Mesh(generate_mesh(domain, 200))
## Constitutive relation
def eps(v):
return sym(grad(v))
E = Constant(1e5)
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)
def sigma(v):
return lmbda*tr(eps(v))*Identity(2) + 2.0*mu*eps(v)
## Variational formulation
rho_g = 1e-3
f = Constant((0, -rho_g))
V = VectorFunctionSpace(mesh, 'Lagrange', degree=2)
du = TrialFunction(V)
u_ = TestFunction(V)
a = inner(sigma(du), eps(u_))*dx
l = inner(f, u_)*dx
## Resolution
def left(x, on_boundary):
return near(x[0], 0.)
class Circle1(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and x[0]>1.1 and x[0] < 1.4 and x[1] < 0.1 and x[1]> 0.4
class Circle2(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and x[0]>1.1 and x[0] < 1.4 and x[1] < 0.6 and x[1]> 0.9
Vc = FunctionSpace(mesh, "CG", 1)
uc = Function(Vc)
bc1 = DirichletBC(Vc, Constant(1), Circle1())
bc1.apply(uc.vector())
bc2 = DirichletBC(Vc, Constant(1), Circle2())
bc2.apply(uc.vector())
bc_left = DirichletBC(V, Constant((0.,0.)), left)
BCs = [bc_left, bc1, bc2]
u = Function(V, name="Displacement")
solve(a == l, u, BCs)
plot(1e3*u, mode="displacement")
## Validation and post-processing
print("Maximal deflection:", -u(L,H/2.)[1])
print("Beam theory deflection:", float(3*rho_g*L**4/2/E/H**3))
Vsig = TensorFunctionSpace(mesh, "DG", degree=0)
sig = Function(Vsig, name="Stress")
sig.assign(project(sigma(u), Vsig))
print("Stress at (0,H):", sig(0, H))
file_results = XDMFFile("2D_elasticity_results_with_2holes.xdmf")
file_results.parameters["flush_output"] = True
file_results.parameters["functions_share_mesh"] = True
file_results.write(u, 0.)
file_results.write(sig, 0.)
Can anybody help me?