Hi dokken,
I tried to use your code to extract the strains for all points of my mesh. I don’t know if I have done it correctly.If I want to extract the strains matrices at each point, what kind of modification should I do to the code?
I would appreciate anyhelp.
# Linear Elasticty Problem
from petsc4py.PETSc import ScalarType
import ufl
import numpy as np
from dolfinx.fem import *
from petsc4py import PETSc
from mpi4py import MPI
from dolfinx import fem, mesh, plot
from dolfinx.fem import Function, FunctionSpace, Constant
from ufl import TestFunction, dx, inner
L = 10.0
domain = mesh.create_box(MPI.COMM_WORLD,[[0.0,0.0,0.0],[L,1,1]],[10,5,5], mesh.CellType.hexahedron)
V = fem.VectorFunctionSpace(domain, ("CG", 1))
def left(x):
return np.isclose(x[0], 0)
def right(x):
return np.isclose(x[0], L)
fdim = domain.topology.dim -1
left_facets = mesh.locate_entities_boundary(domain, fdim, left)
right_facets = mesh.locate_entities_boundary(domain, fdim, right)
marked_facets = np.hstack([left_facets,right_facets])
marked_values = np.hstack([np.full_like(left_facets,1), np.full_like(right_facets,2)])
sorted_facets = np.argsort(marked_facets)
facet_tag = mesh.meshtags(domain, fdim, marked_facets[sorted_facets],marked_values[sorted_facets])
u_bc = np.array((0,) * domain.geometry.dim, dtype=PETSc.ScalarType)
left_dofs = fem.locate_dofs_topological(V,facet_tag.dim, facet_tag.find(1))
bc_l = fem.dirichletbc(u_bc, left_dofs, V)
right_dofs = fem.locate_dofs_topological(V.sub(0),facet_tag.dim, facet_tag.find(2))
bc_r = fem.dirichletbc(ScalarType(1.0),right_dofs,V.sub(0))
bcs = [bc_l, bc_r]
L = 1
W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma
def eps(u):
return ufl.sym(ufl.grad(u))
def sig(u):
return lambda_* ufl.nabla_div(u) * ufl.Identity(len(u)) + 2*mu*eps(u)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
f = fem.Constant(domain, ScalarType((0,0,0)))
a = ufl.inner(sig(u), eps(v)) * ufl.dx
L = ufl.dot(f, v) * ufl.dx #+ ufl.dot(T,v) * ds
Problem = fem.petsc.LinearProblem(a, L, bcs =bcs, petsc_options={"ksp_type":"preonly", "pc_type":"lu"})
uh = Problem.solve()
# Projection of Strain Tensor
import dolfinx
import dolfinx.io
from mpi4py import MPI
from petsc4py import PETSc
epsilon = eps(uh)
V_eps = fem.TensorFunctionSpace(domain,("DG",0))
strain_expr = fem.Expression(epsilon,V_eps.element.interpolation_points())
strains = fem.Function(V_eps)
strains.interpolate(strain_expr)
# Project DG function to CG space
W= dolfinx.fem.TensorFunctionSpace(domain, ("CG",1))
u, w = ufl.TrialFunction(W), ufl.TestFunction(W)
a= dolfinx.fem.form(ufl.inner(u, w) * ufl.dx)
l = dolfinx.fem.form(ufl.inner(strains, w) * ufl.dx)
eps_h = dolfinx.fem.Function(W)
bcs = []
A = dolfinx.fem.petsc.assemble_matrix(a,bcs)
A.assemble()
b = dolfinx.fem.petsc.assemble_vector(l)
solver = PETSc.KSP().create(MPI.COMM_WORLD)
solver.setType(PETSc.KSP.Type.PREONLY)
solver.getPC().setType(PETSc.PC.Type.LU)
solver.setOperators(A)
solver.solve(b, eps_h.vector)
eps_h.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD)
print(eps_h.vector.array)
#