Last active
June 27, 2018 13:44
-
-
Save w1th0utnam3/46b42e16bbfba2a2aa8b17589604d80b to your computer and use it in GitHub Desktop.
Generate Neo-Hookean force & force Jacobian functions using FEniCS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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