Spaces:
Runtime error
Runtime error
Update simulators/fenics_simulation.py
Browse files
simulators/fenics_simulation.py
CHANGED
|
@@ -1 +1,64 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fenics import *
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def run_fenics_simulation(simulation_type, **kwargs):
|
| 5 |
+
"""
|
| 6 |
+
Run FEniCS simulation for the selected use case.
|
| 7 |
+
|
| 8 |
+
Parameters:
|
| 9 |
+
simulation_type (str): 'plate' or 'beam'.
|
| 10 |
+
kwargs: Input parameters such as length, width, thickness, force/load.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
stress (float): Calculated maximum stress (approx).
|
| 14 |
+
deformation (float): Total deformation (approx).
|
| 15 |
+
"""
|
| 16 |
+
# Mesh setup
|
| 17 |
+
if simulation_type == "plate":
|
| 18 |
+
length, width, thickness = kwargs["length"], kwargs["width"], kwargs["thickness"]
|
| 19 |
+
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, thickness), 10, 10, 2)
|
| 20 |
+
load = kwargs["force"]
|
| 21 |
+
elif simulation_type == "beam":
|
| 22 |
+
length, width, thickness = kwargs["length"], kwargs["width"], kwargs["thickness"]
|
| 23 |
+
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, thickness), 10, 10, 2)
|
| 24 |
+
load = kwargs["load"]
|
| 25 |
+
else:
|
| 26 |
+
raise ValueError("Invalid simulation type selected.")
|
| 27 |
+
|
| 28 |
+
# Function space
|
| 29 |
+
V = VectorFunctionSpace(mesh, "P", 1)
|
| 30 |
+
|
| 31 |
+
# Trial and test functions
|
| 32 |
+
u = TrialFunction(V)
|
| 33 |
+
v = TestFunction(V)
|
| 34 |
+
|
| 35 |
+
# Material properties
|
| 36 |
+
E, nu = 2e11, 0.3 # Elastic modulus and Poisson's ratio
|
| 37 |
+
mu = E / (2.0 * (1.0 + nu))
|
| 38 |
+
lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))
|
| 39 |
+
|
| 40 |
+
# Stress-strain relationship
|
| 41 |
+
def sigma(v):
|
| 42 |
+
return lmbda * nabla_div(v) * Identity(3) + 2 * mu * sym(grad(v))
|
| 43 |
+
|
| 44 |
+
# Load
|
| 45 |
+
f = Constant((-load, 0, 0))
|
| 46 |
+
|
| 47 |
+
# Variational form
|
| 48 |
+
a = inner(sigma(u), sym(grad(v))) * dx
|
| 49 |
+
L = dot(f, v) * dx
|
| 50 |
+
|
| 51 |
+
# Boundary conditions
|
| 52 |
+
def boundary(x, on_boundary):
|
| 53 |
+
return on_boundary and near(x[0], 0)
|
| 54 |
+
|
| 55 |
+
bc = DirichletBC(V, Constant((0, 0, 0)), boundary)
|
| 56 |
+
|
| 57 |
+
# Solve
|
| 58 |
+
u = Function(V)
|
| 59 |
+
solve(a == L, u, bc)
|
| 60 |
+
|
| 61 |
+
# Post-processing
|
| 62 |
+
stress = np.max(u.vector().get_local()) # Approximate stress
|
| 63 |
+
deformation = u.vector().norm("l2") # Approximate deformation
|
| 64 |
+
return stress, deformation
|