karthikmn commited on
Commit
033ad91
·
verified ·
1 Parent(s): 266f546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -94
app.py CHANGED
@@ -1,99 +1,95 @@
1
- from fenics import *
2
- import matplotlib.pyplot as plt
3
- import numpy as np
4
- import gradio as gr
5
 
6
- def simulate_beam(length, height, E, nu, force):
7
  """
8
- Simulate a beam under uniform load using FEniCS.
9
  Parameters:
10
- length: Length of the beam (m)
11
- height: Height of the beam (m)
12
- E: Young's modulus (Pa)
13
- nu: Poisson's ratio
14
- force: Uniform load (N/m^2)
15
  Returns:
16
- Displacement plot and stress plot as PNG images
17
  """
18
- # Create mesh and define function space
19
- mesh = RectangleMesh(Point(0, 0), Point(length, height), 50, 10)
20
- V = VectorFunctionSpace(mesh, "P", 1)
21
-
22
- # Boundary conditions
23
- def left_boundary(x, on_boundary):
24
- return on_boundary and near(x[0], 0)
25
-
26
- bc = DirichletBC(V, Constant((0, 0)), left_boundary)
27
-
28
- # Define strain and stress
29
- def epsilon(u):
30
- return sym(grad(u))
31
-
32
- def sigma(u):
33
- mu = E / (2 * (1 + nu))
34
- lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
35
- return lmbda * div(u) * Identity(2) + 2 * mu * epsilon(u)
36
-
37
- # Define variational problem
38
- u = TrialFunction(V)
39
- v = TestFunction(V)
40
- f = Constant((0, -force)) # Downward force
41
- a = inner(sigma(u), epsilon(v)) * dx
42
- L = dot(f, v) * dx
43
-
44
- # Solve the problem
45
- u = Function(V)
46
- solve(a == L, u, bc)
47
-
48
- # Plot displacement
49
- displacement_fig, ax1 = plt.subplots()
50
- plot(u, mode="displacement", title="Displacement", axes=ax1)
51
- ax1.set_xlabel("X")
52
- ax1.set_ylabel("Y")
53
- plt.tight_layout()
54
-
55
- # Save displacement plot
56
- displacement_file = "displacement.png"
57
- plt.savefig(displacement_file)
58
- plt.close(displacement_fig)
59
-
60
- # Compute stress
61
- W = TensorFunctionSpace(mesh, "P", 1)
62
- stress = project(sigma(u), W)
63
-
64
- # Plot stress
65
- stress_fig, ax2 = plt.subplots()
66
- plot(stress, title="Stress", axes=ax2)
67
- ax2.set_xlabel("X")
68
- ax2.set_ylabel("Y")
69
- plt.tight_layout()
70
-
71
- # Save stress plot
72
- stress_file = "stress.png"
73
- plt.savefig(stress_file)
74
- plt.close(stress_fig)
75
-
76
- return displacement_file, stress_file
77
-
78
- # Gradio interface
79
- def run_simulation(length, height, E, nu, force):
80
- return simulate_beam(length, height, E, nu, force)
81
-
82
- app = gr.Interface(
83
- fn=run_simulation,
84
- inputs=[
85
- gr.Number(label="Beam Length (m)", value=10),
86
- gr.Number(label="Beam Height (m)", value=1),
87
- gr.Number(label="Young's Modulus (Pa)", value=210e9),
88
- gr.Number(label="Poisson's Ratio", value=0.3),
89
- gr.Number(label="Force (N/m^2)", value=1e4),
90
- ],
91
- outputs=[
92
- gr.Image(label="Displacement Plot"),
93
- gr.Image(label="Stress Plot"),
94
- ],
95
- live=True,
96
- )
97
-
98
- if __name__ == "__main__":
99
- app.launch()
 
1
+ import gmsh
2
+ import os
 
 
3
 
4
+ def generate_apdl_script(cad_file, material_e, material_nu, mesh_size, load_value):
5
  """
6
+ Generate an APDL script from a CAD file using GMSH.
7
  Parameters:
8
+ cad_file: Path to the CAD file (e.g., STEP, IGES).
9
+ material_e: Young's modulus (Pa).
10
+ material_nu: Poisson's ratio.
11
+ mesh_size: Element size for meshing.
12
+ load_value: Load to apply (N).
13
  Returns:
14
+ APDL script as a string.
15
  """
16
+ # Initialize GMSH
17
+ gmsh.initialize()
18
+ gmsh.model.add("CAD_Model")
19
+
20
+ # Import CAD file
21
+ gmsh.model.occ.importShapes(cad_file)
22
+ gmsh.model.occ.synchronize()
23
+
24
+ # Generate 3D mesh
25
+ gmsh.model.mesh.generate(3)
26
+
27
+ # Extract mesh data
28
+ nodes = gmsh.model.mesh.getNodes()
29
+ elements = gmsh.model.mesh.getElements()
30
+
31
+ # Start building APDL script
32
+ apdl_script = """
33
+ /prep7
34
+ ! Import geometry from GMSH
35
+ ! Material Properties
36
+ mp, ex, 1, {material_e}
37
+ mp, nuxy, 1, {material_nu}
38
+
39
+ ! Mesh settings
40
+ esize, {mesh_size}
41
+ et, 1, solid186
42
+
43
+ ! Generate geometry and mesh
44
+ """.format(material_e=material_e, material_nu=material_nu, mesh_size=mesh_size)
45
+
46
+ # Add nodes to APDL script
47
+ for node_id, coords in zip(nodes[0], nodes[1].reshape(-1, 3)):
48
+ apdl_script += "n, {0}, {1}, {2}, {3}\n".format(node_id, *coords)
49
+
50
+ # Add elements to APDL script
51
+ apdl_script += "! Elements\n"
52
+ for elem_id, elem_nodes in zip(elements[1][0], elements[2][0]):
53
+ apdl_script += "e, {0}, {1}, {2}, {3}\n".format(*elem_nodes[:4]) # Example for tetrahedral
54
+
55
+ # Boundary conditions and loads
56
+ apdl_script += """
57
+ ! Apply boundary conditions
58
+ nsel, s, loc, z, 0
59
+ d, all, ux, 0
60
+ d, all, uy, 0
61
+ d, all, uz, 0
62
+
63
+ ! Apply loads
64
+ nsel, s, loc, z, max
65
+ f, all, fz, {load_value}
66
+
67
+ ! Solve
68
+ /solu
69
+ antype, static
70
+ solve
71
+ /post1
72
+ set, last
73
+ prnsol, s, comp
74
+ prnsol, u, comp
75
+ /exit
76
+ """.format(load_value=load_value)
77
+
78
+ gmsh.finalize()
79
+ return apdl_script
80
+
81
+ # Example usage
82
+ cad_file = "example.step" # Replace with the actual CAD file
83
+ material_e = 210e9 # Young's modulus in Pascals
84
+ material_nu = 0.3 # Poisson's ratio
85
+ mesh_size = 10 # Mesh element size
86
+ load_value = 1000 # Load in Newtons
87
+
88
+ apdl_script = generate_apdl_script(cad_file, material_e, material_nu, mesh_size, load_value)
89
+
90
+ # Save APDL script to a file
91
+ output_file = "generated_apdl.txt"
92
+ with open(output_file, "w") as f:
93
+ f.write(apdl_script)
94
+
95
+ print(f"APDL script saved to {output_file}")