Spaces:
Build error
Build error
Create fenics_simulation.py
Browse files
simulators/fenics_simulation.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fenics import *
|
| 2 |
+
|
| 3 |
+
def run_fenics_simulation(length, width, thickness, force):
|
| 4 |
+
"""
|
| 5 |
+
Executes a FEniCS-based finite element simulation.
|
| 6 |
+
Parameters:
|
| 7 |
+
length (float): Length of the plate/beam.
|
| 8 |
+
width (float): Width of the plate/beam.
|
| 9 |
+
thickness (float): Thickness of the plate/beam.
|
| 10 |
+
force (float): Applied force.
|
| 11 |
+
Returns:
|
| 12 |
+
tuple: (stress, deformation) from the simulation.
|
| 13 |
+
"""
|
| 14 |
+
# Geometry and mesh
|
| 15 |
+
mesh = RectangleMesh(Point(0, 0), Point(length, width), 10, 10)
|
| 16 |
+
|
| 17 |
+
# Define function space
|
| 18 |
+
V = VectorFunctionSpace(mesh, "P", 1)
|
| 19 |
+
|
| 20 |
+
# Define boundary conditions
|
| 21 |
+
def clamped_boundary(x, on_boundary):
|
| 22 |
+
return on_boundary and near(x[0], 0)
|
| 23 |
+
|
| 24 |
+
bc = DirichletBC(V, Constant((0, 0)), clamped_boundary)
|
| 25 |
+
|
| 26 |
+
# Define material properties
|
| 27 |
+
E = 2e11 # Elastic modulus (Pa)
|
| 28 |
+
nu = 0.3 # Poisson's ratio
|
| 29 |
+
mu = E / (2 * (1 + nu))
|
| 30 |
+
lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
|
| 31 |
+
|
| 32 |
+
# Define strain and stress
|
| 33 |
+
def epsilon(u):
|
| 34 |
+
return sym(grad(u))
|
| 35 |
+
|
| 36 |
+
def sigma(u):
|
| 37 |
+
return lmbda * div(u) * Identity(2) + 2 * mu * epsilon(u)
|
| 38 |
+
|
| 39 |
+
# Define variational problem
|
| 40 |
+
u = TrialFunction(V)
|
| 41 |
+
d = TestFunction(V)
|
| 42 |
+
f = Constant((force / (length * width), 0))
|
| 43 |
+
T = inner(sigma(u), epsilon(d)) * dx - dot(f, d) * dx
|
| 44 |
+
a, L = lhs(T), rhs(T)
|
| 45 |
+
|
| 46 |
+
# Solve problem
|
| 47 |
+
u_sol = Function(V)
|
| 48 |
+
solve(a == L, u_sol, bc)
|
| 49 |
+
|
| 50 |
+
# Post-process results
|
| 51 |
+
stress = max(project(sigma(u_sol)[0, 0], FunctionSpace(mesh, "P", 1)).vector())
|
| 52 |
+
deformation = max(u_sol.vector())
|
| 53 |
+
|
| 54 |
+
return stress, deformation
|