Spaces:
Runtime error
Runtime error
| 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 | |