Hey everybody,
I am new to Fenics but I think it is the most appropriate way to get the results I need instead of coding complete new FEM routines in python. My goal is to have 3 seperate Fenics routines at the end of the week, for linear elastic, non-linear elastic and linear in-elastic (history dependent) problems in the weak for. All of the routines should calculate strain and stress at the gauss points.
I started with the linear elastic demo ft06_elasticity. Of course feel free to reduce the problem to 2D if it is easier. A normal unitsqaure is also okay.
Since now I modified the code such that I can calculate the strain/stress at each mesh coordinate. But I could not modify it for Gauss Points. Any idea how to do that?
from __future__ import print_function
from fenics import *
import numpy as np
# Scaled variables
L = 1; W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma
# Create mesh and define function space
mesh = BoxMesh(Point(0, 0, 0), Point(L, W, W), 10, 3, 3)
V = VectorFunctionSpace(mesh, 'P', 1)
# Define boundary condition
tol = 1E-14
def clamped_boundary(x, on_boundary):
return on_boundary and x[0] < tol
bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)
# Define strain and stress
def epsilon(u):
return 0.5*(grad(u) + grad(u).T)
#return sym(nabla_grad(u))
def sigma(u):
return lambda_*div(u)*Identity(d) + 2*mu*epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension() # space dimension
v = TestFunction(V)
f = Constant((0, 0, -rho*g))
T = Constant((0, 0, 0))
a = inner(sigma(u), epsilon(v))*dx
L = dot(f, v)*dx + dot(T, v)*ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Plot solution
plot(u, title='Displacement', mode='displacement')
Vsig = TensorFunctionSpace(mesh,'DG',0)
Veps = TensorFunctionSpace(mesh, 'DG', 0)
PKTensor = project(sigma(u), Vsig)
GLFSTensor = project(epsilon(u), Vsig)
Strain = []
Stress = []
temp_vec = np.zeros(shape=[6, 1])
temp_vec2 = np.zeros(shape=[6, 1])
for i in range(mesh.coordinates().shape[0]):
x = mesh.coordinates()[i,0]
y = mesh.coordinates()[i,1]
z = mesh.coordinates()[i,2]
temp_vec[0] = GLFSTensor(x, y, z)[0]
temp_vec[1] = GLFSTensor(x, y, z)[4]
temp_vec[2] = GLFSTensor(x, y, z)[8]
temp_vec[3] = 2*GLFSTensor(x, y, z)[5]
temp_vec[4] = 2*GLFSTensor(x, y, z)[2]
temp_vec[5] = 2*GLFSTensor(x, y, z)[1]
temp_vec2[0] = PKTensor(x, y, z)[0]
temp_vec2[1] = PKTensor(x, y, z)[4]
temp_vec2[2] = PKTensor(x, y, z)[8]
temp_vec2[3] = 2 * PKTensor(x, y, z)[5]
temp_vec2[4] = 2 * PKTensor(x, y, z)[2]
temp_vec2[5] = 2 * PKTensor(x, y, z)[1]
Strain.append(temp_vec)
Stress.append(temp_vec2)
print(Strain)
print(Stress)