Yes, sure, here is a whole file:
from dolfin import *
import numpy as np
# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True, \
"eliminate_zeros": True, \
"precompute_basis_const": True, \
"precompute_ip_const": True}
mesh=BoxMesh(Point(0.0, 0.0, 0.0),Point(1.0, 1.0, 1.0),20,20,20)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
N = FacetNormal(mesh)
# Mark boundary subdomians
boundxmin=mesh.coordinates()[:,0].min()
boundxmax=mesh.coordinates()[:,0].max()
boundymin=mesh.coordinates()[:,1].min()
boundymax=mesh.coordinates()[:,1].max()
boundzmin=mesh.coordinates()[:,2].min()
boundzmax=mesh.coordinates()[:,2].max()
left = CompiledSubDomain("near(x[0], side) && abs(x[2])< diam&& on_boundary", side = boundxmax, diam=0.1*(boundzmax-boundzmin)/2)
c = Expression(("0.0", "0.0", "0.0"),degree=3)
bcl = DirichletBC(V, c, left)
bcs = [bcl]
class myAirChamber(SubDomain):
def inside(self,x,on_boundary):
return on_boundary and \
not(near(x[0],boundxmin)) and not(near(x[0],boundxmax)) and \
not(near(x[1],boundymin)) and not(near(x[1],boundymax)) and \
not(near(x[2],boundzmin)) and not(near(x[2],boundzmax))
class OmegaSheet(SubDomain):
def inside(self,x,on_boundary):
return x[1]<=0.5
class OmegaNet(SubDomain):
def inside(self,x,on_boundary):
return x[1]>=0.5
subdomains = MeshFunction('size_t', mesh, 3)
# Mark subdomains with numbers 0 and 1
subdomainSheet = OmegaSheet()
subdomainSheet.mark(subdomains, 0)
subdomainNet = OmegaNet()
subdomainNet.mark(subdomains, 1)
# Define functions
du = TrialFunction(V) # Incremental displacement
v = TestFunction(V) # Test function
u = Function(V) # Displacement from previous iteration
B = Constant((0.0, 0.0, 0.0)) # Body force per unit volume
T = Constant((0.0, 0.0, 0.0)) # Traction force on the boundary
# Kinematics
d = u.geometric_dimension()
I = Identity(d) # Identity tensor
# Elasticity parameters
E, nu = 10.0, 0.27
mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu)))
p=0.01
print()
print('pressure: ',p)
#T = Constant((0.0, p, 0.0)) # Body force per unit volume
F = I + grad(u) # Deformation gradient
C = F.T*F # Right Cauchy-Green tensor
Finv=inv(F)
# Invariants of deformation tensors
Ic = tr(C)
J = det(F)
n = J*Finv.T*N
# Stored strain energy density (compressible neo-Hookean model)
psi = (mu/2)*(Ic - 3) - mu*ln(J) + (lmbda/2)*(ln(J))**2
pr=Constant((p))
boundaries = MeshFunction("size_t", mesh,mesh.topology().dim()-1, 0)
boundaries.set_all(0)
bottom_bound=myAirChamber()
bottom_bound.mark(boundaries, 1)
ds_region = Measure('ds')[boundaries]
dx_ = Measure('dx', subdomain_data=subdomains)
##Let's try this
Pi = psi*dx_(0) +psi*dx_(1) - dot(B, u)*dx - dot(u,-pr*n)*ds_region(1)#use inverse normal
# Compute first variation of Pi (directional derivative about u in the direction of v)
F = derivative(Pi, u, v)
# Compute Jacobian of F
J = derivative(F, u, du)
# Solve variational problem
solve(F == 0, u, bcs, J=J,
form_compiler_parameters=ffc_options,
solver_parameters={
"newton_solver":{\
"relaxation_parameter":0.5,
"relative_tolerance":1e-6 ,
"maximum_iterations":100,
} })