Spaces:
Sleeping
Sleeping
Update simulators/fenics_simulation.py
Browse files
simulators/fenics_simulation.py
CHANGED
@@ -2,18 +2,6 @@ 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)
|
@@ -25,40 +13,29 @@ def run_fenics_simulation(simulation_type, **kwargs):
|
|
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 |
-
|
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 |
-
|
62 |
-
|
63 |
-
deformation = u.vector().norm("l2") # Approximate deformation
|
64 |
return stress, deformation
|
|
|
2 |
import numpy as np
|
3 |
|
4 |
def run_fenics_simulation(simulation_type, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
if simulation_type == "plate":
|
6 |
length, width, thickness = kwargs["length"], kwargs["width"], kwargs["thickness"]
|
7 |
mesh = BoxMesh(Point(0, 0, 0), Point(length, width, thickness), 10, 10, 2)
|
|
|
13 |
else:
|
14 |
raise ValueError("Invalid simulation type selected.")
|
15 |
|
|
|
16 |
V = VectorFunctionSpace(mesh, "P", 1)
|
|
|
|
|
17 |
u = TrialFunction(V)
|
18 |
v = TestFunction(V)
|
19 |
|
20 |
+
E, nu = 2e11, 0.3
|
|
|
21 |
mu = E / (2.0 * (1.0 + nu))
|
22 |
lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))
|
23 |
|
|
|
24 |
def sigma(v):
|
25 |
return lmbda * nabla_div(v) * Identity(3) + 2 * mu * sym(grad(v))
|
26 |
|
|
|
27 |
f = Constant((-load, 0, 0))
|
|
|
|
|
28 |
a = inner(sigma(u), sym(grad(v))) * dx
|
29 |
L = dot(f, v) * dx
|
30 |
|
|
|
31 |
def boundary(x, on_boundary):
|
32 |
return on_boundary and near(x[0], 0)
|
33 |
|
34 |
bc = DirichletBC(V, Constant((0, 0, 0)), boundary)
|
35 |
|
|
|
36 |
u = Function(V)
|
37 |
solve(a == L, u, bc)
|
38 |
|
39 |
+
stress = np.max(u.vector().get_local())
|
40 |
+
deformation = u.vector().norm("l2")
|
|
|
41 |
return stress, deformation
|