3dsimulation2.0 / simulators /fenics_simulation.py
karthikmn's picture
Create fenics_simulation.py
4774ecf verified
from fenics import *
def run_fenics_simulation(length, width, thickness, force):
"""
Executes a FEniCS-based finite element simulation.
Parameters:
length (float): Length of the plate/beam.
width (float): Width of the plate/beam.
thickness (float): Thickness of the plate/beam.
force (float): Applied force.
Returns:
tuple: (stress, deformation) from the simulation.
"""
# Geometry and mesh
mesh = RectangleMesh(Point(0, 0), Point(length, width), 10, 10)
# Define function space
V = VectorFunctionSpace(mesh, "P", 1)
# Define boundary conditions
def clamped_boundary(x, on_boundary):
return on_boundary and near(x[0], 0)
bc = DirichletBC(V, Constant((0, 0)), clamped_boundary)
# Define material properties
E = 2e11 # Elastic modulus (Pa)
nu = 0.3 # Poisson's ratio
mu = E / (2 * (1 + nu))
lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
# Define strain and stress
def epsilon(u):
return sym(grad(u))
def sigma(u):
return lmbda * div(u) * Identity(2) + 2 * mu * epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = TestFunction(V)
f = Constant((force / (length * width), 0))
T = inner(sigma(u), epsilon(d)) * dx - dot(f, d) * dx
a, L = lhs(T), rhs(T)
# Solve problem
u_sol = Function(V)
solve(a == L, u_sol, bc)
# Post-process results
stress = max(project(sigma(u_sol)[0, 0], FunctionSpace(mesh, "P", 1)).vector())
deformation = max(u_sol.vector())
return stress, deformation