Skip to content

Instantly share code, notes, and snippets.

@w1th0utnam3
Last active June 27, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w1th0utnam3/46b42e16bbfba2a2aa8b17589604d80b to your computer and use it in GitHub Desktop.
Save w1th0utnam3/46b42e16bbfba2a2aa8b17589604d80b to your computer and use it in GitHub Desktop.
Generate Neo-Hookean force & force Jacobian functions using FEniCS
from ufl import *
import ffc.compiler
# Define cell type we solve on
cell = tetrahedron
d = cell.geometric_dimension()
# The finite element function space
element = VectorElement("Lagrange", cell, 1, dim=d)
# Displacement that is used as linearization point
u = Coefficient(element)
# Increment that is solved for in each iteration
du = TrialFunction(element)
# Test functions
v = TestFunction(element)
# Material parameters
rho = Constant(cell)
mu = Constant(cell)
lmbda = Constant(cell)
I = Identity(d)
# Deformation gradient
F = I + grad(u)
F = variable(F)
# Right Cauchy-Green strain tensor
C = F.T * F
# Hyperelastic invariants
Ic = tr(C)
J = det(F)
# Neo-Hookean strain energy density
psi = (mu / 2) * (Ic - 3) - mu * ln(J) + (lmbda / 2) * (ln(J)) ** 2
# First Piola-Kirchhoff stress tensor
P = diff(psi, F)
# Elastic forces
L = inner(P, grad(v)) * dx
# Jacobian of the elastic forces
a = derivative(L, u, du)
# Mass matrix
M = rho*inner(du, v) * dx
# Total kinetic energy
E_kin = (rho/2) * inner(u, u) * dx
# Total strain energy
E_strain = psi*dx
parameters = ffc.default_parameters()
parameters["representation"] = "uflacs"
parameters["optimize"] = True
code_c, code_h = ffc.compiler.compile_form([L], prefix="neohookean", parameters=parameters)
with open("neohookean.c", mode="w") as f:
f.write(code_c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment