Demo091224 / simulators /fenics_simulation.py
SuriRaja's picture
Update simulators/fenics_simulation.py
76c011a verified
from fenics import *
import numpy as np
def run_fenics_simulation(simulation_type, **kwargs):
if simulation_type == "plate":
length, width, thickness = kwargs["length"], kwargs["width"], kwargs["thickness"]
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, thickness), 10, 10, 2)
load = kwargs["force"]
elif simulation_type == "beam":
length, width, thickness = kwargs["length"], kwargs["width"], kwargs["thickness"]
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, thickness), 10, 10, 2)
load = kwargs["load"]
else:
raise ValueError("Invalid simulation type selected.")
V = VectorFunctionSpace(mesh, "P", 1)
u = TrialFunction(V)
v = TestFunction(V)
E, nu = 2e11, 0.3
mu = E / (2.0 * (1.0 + nu))
lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))
def sigma(v):
return lmbda * nabla_div(v) * Identity(3) + 2 * mu * sym(grad(v))
f = Constant((-load, 0, 0))
a = inner(sigma(u), sym(grad(v))) * dx
L = dot(f, v) * dx
def boundary(x, on_boundary):
return on_boundary and near(x[0], 0)
bc = DirichletBC(V, Constant((0, 0, 0)), boundary)
u = Function(V)
solve(a == L, u, bc)
stress = np.max(u.vector().get_local())
deformation = u.vector().norm("l2")
return stress, deformation