Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,95 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import numpy as np
|
| 4 |
-
import gradio as gr
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
Parameters:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Returns:
|
| 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 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 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}")
|
|
|
|
|
|