File size: 1,993 Bytes
bed8d89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from fenics import *
import numpy as np

def run_fenics_simulation(simulation_type, **kwargs):
    """
    Run FEniCS simulation for the selected use case.

    Parameters:
        simulation_type (str): 'plate' or 'beam'.
        kwargs: Input parameters such as length, width, thickness, force/load.

    Returns:
        stress (float): Calculated maximum stress (approx).
        deformation (float): Total deformation (approx).
    """
    # Mesh setup
    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.")

    # Function space
    V = VectorFunctionSpace(mesh, "P", 1)

    # Trial and test functions
    u = TrialFunction(V)
    v = TestFunction(V)

    # Material properties
    E, nu = 2e11, 0.3  # Elastic modulus and Poisson's ratio
    mu = E / (2.0 * (1.0 + nu))
    lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu))

    # Stress-strain relationship
    def sigma(v):
        return lmbda * nabla_div(v) * Identity(3) + 2 * mu * sym(grad(v))

    # Load
    f = Constant((-load, 0, 0))

    # Variational form
    a = inner(sigma(u), sym(grad(v))) * dx
    L = dot(f, v) * dx

    # Boundary conditions
    def boundary(x, on_boundary):
        return on_boundary and near(x[0], 0)

    bc = DirichletBC(V, Constant((0, 0, 0)), boundary)

    # Solve
    u = Function(V)
    solve(a == L, u, bc)

    # Post-processing
    stress = np.max(u.vector().get_local())  # Approximate stress
    deformation = u.vector().norm("l2")  # Approximate deformation
    return stress, deformation