Spaces:
Build error
Build error
Update visualization.py
Browse files- visualization.py +66 -38
visualization.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def visualize_results(simulator, length, width, thickness, stress, deformation):
|
| 4 |
"""
|
| 5 |
-
Generates 2D visualizations for simulation results.
|
| 6 |
Parameters:
|
| 7 |
simulator (str): Name of the simulator (e.g., 'Python-Based Solver').
|
| 8 |
length (float): Length of the object.
|
|
@@ -11,7 +13,7 @@ def visualize_results(simulator, length, width, thickness, stress, deformation):
|
|
| 11 |
stress (float): Stress value.
|
| 12 |
deformation (float): Deformation value.
|
| 13 |
Returns:
|
| 14 |
-
str: Path to the 2D visualization
|
| 15 |
"""
|
| 16 |
# Generate 2D bar chart
|
| 17 |
fig, ax = plt.subplots(figsize=(6, 4))
|
|
@@ -20,16 +22,40 @@ def visualize_results(simulator, length, width, thickness, stress, deformation):
|
|
| 20 |
ax.set_ylabel("Magnitude")
|
| 21 |
ax.grid(True, linestyle="--", alpha=0.6)
|
| 22 |
|
| 23 |
-
# Save
|
| 24 |
-
|
| 25 |
-
plt.savefig(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
plt.close(fig)
|
| 27 |
|
| 28 |
-
return
|
| 29 |
|
| 30 |
def visualize_end_product(simulation_type, length, width, thickness, deformation):
|
| 31 |
"""
|
| 32 |
-
Visualize the end product based on the parameters.
|
| 33 |
Parameters:
|
| 34 |
simulation_type (str): 'plate' or 'beam'.
|
| 35 |
length (float): Length of the product (mm).
|
|
@@ -37,44 +63,46 @@ def visualize_end_product(simulation_type, length, width, thickness, deformation
|
|
| 37 |
thickness (float): Thickness of the product (mm).
|
| 38 |
deformation (float): Deformation (mm).
|
| 39 |
Returns:
|
| 40 |
-
str: Path to the saved image of the visualization.
|
| 41 |
"""
|
| 42 |
-
fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if simulation_type == "plate":
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
ax.add_patch(rect)
|
| 48 |
-
# Add deformation as text
|
| 49 |
-
ax.text(
|
| 50 |
-
length / 2, width / 2,
|
| 51 |
-
f"Deformation: {deformation:.2f} mm",
|
| 52 |
-
color="red", fontsize=10, ha="center"
|
| 53 |
-
)
|
| 54 |
elif simulation_type == "beam":
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
ax.add_patch(rect)
|
| 58 |
-
# Add deformation as text
|
| 59 |
-
ax.text(
|
| 60 |
-
length / 2, thickness / 2,
|
| 61 |
-
f"Deflection: {deformation:.2f} mm",
|
| 62 |
-
color="red", fontsize=10, ha="center"
|
| 63 |
-
)
|
| 64 |
else:
|
| 65 |
raise ValueError("Invalid simulation type.")
|
| 66 |
-
|
| 67 |
# Add dimensions
|
| 68 |
-
ax.set_xlim(-10, length + 10)
|
| 69 |
-
ax.set_ylim(-10, max(width, thickness) + 10)
|
| 70 |
-
ax.set_title(f"Visualization of {simulation_type.capitalize()} End Product")
|
| 71 |
ax.set_xlabel("Length (mm)")
|
| 72 |
-
ax.set_ylabel("Width
|
| 73 |
-
ax.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
plt.close(fig)
|
| 79 |
|
| 80 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
def visualize_results(simulator, length, width, thickness, stress, deformation):
|
| 6 |
"""
|
| 7 |
+
Generates 3D and 2D visualizations for simulation results.
|
| 8 |
Parameters:
|
| 9 |
simulator (str): Name of the simulator (e.g., 'Python-Based Solver').
|
| 10 |
length (float): Length of the object.
|
|
|
|
| 13 |
stress (float): Stress value.
|
| 14 |
deformation (float): Deformation value.
|
| 15 |
Returns:
|
| 16 |
+
str: Path to the 3D and 2D visualization images.
|
| 17 |
"""
|
| 18 |
# Generate 2D bar chart
|
| 19 |
fig, ax = plt.subplots(figsize=(6, 4))
|
|
|
|
| 22 |
ax.set_ylabel("Magnitude")
|
| 23 |
ax.grid(True, linestyle="--", alpha=0.6)
|
| 24 |
|
| 25 |
+
# Save 2D chart
|
| 26 |
+
output_2d_path = "results_2d.png"
|
| 27 |
+
plt.savefig(output_2d_path)
|
| 28 |
+
plt.close(fig)
|
| 29 |
+
|
| 30 |
+
# Generate 3D bar chart
|
| 31 |
+
fig = plt.figure(figsize=(8, 6))
|
| 32 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 33 |
+
|
| 34 |
+
x = np.array([1, 2])
|
| 35 |
+
y = np.array([0, 0])
|
| 36 |
+
z = np.array([0, 0])
|
| 37 |
+
dx = np.array([1, 1])
|
| 38 |
+
dy = np.array([1, 1])
|
| 39 |
+
dz = np.array([stress, deformation])
|
| 40 |
+
|
| 41 |
+
ax.bar3d(x, y, z, dx, dy, dz, color=["red", "blue"], alpha=0.6)
|
| 42 |
+
ax.set_xlabel('Parameters')
|
| 43 |
+
ax.set_ylabel('Category')
|
| 44 |
+
ax.set_zlabel('Magnitude')
|
| 45 |
+
ax.set_xticks([1, 2])
|
| 46 |
+
ax.set_xticklabels(['Stress', 'Deformation'])
|
| 47 |
+
ax.set_title(f"3D Simulation Results ({simulator})")
|
| 48 |
+
|
| 49 |
+
# Save 3D chart
|
| 50 |
+
output_3d_path = "results_3d.png"
|
| 51 |
+
plt.savefig(output_3d_path)
|
| 52 |
plt.close(fig)
|
| 53 |
|
| 54 |
+
return output_2d_path, output_3d_path
|
| 55 |
|
| 56 |
def visualize_end_product(simulation_type, length, width, thickness, deformation):
|
| 57 |
"""
|
| 58 |
+
Visualize the end product based on the parameters with 3D visualization.
|
| 59 |
Parameters:
|
| 60 |
simulation_type (str): 'plate' or 'beam'.
|
| 61 |
length (float): Length of the product (mm).
|
|
|
|
| 63 |
thickness (float): Thickness of the product (mm).
|
| 64 |
deformation (float): Deformation (mm).
|
| 65 |
Returns:
|
| 66 |
+
str: Path to the saved image of the 3D visualization.
|
| 67 |
"""
|
| 68 |
+
fig = plt.figure(figsize=(8, 6))
|
| 69 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 70 |
+
|
| 71 |
+
# Create a grid of coordinates for the surface
|
| 72 |
+
x = np.linspace(0, length, 10)
|
| 73 |
+
y = np.linspace(0, width, 10)
|
| 74 |
+
x, y = np.meshgrid(x, y)
|
| 75 |
+
z = np.zeros_like(x) # Base plane
|
| 76 |
+
|
| 77 |
+
# Add deformation to the surface
|
| 78 |
+
z_deformed = z + np.random.normal(0, deformation / 10, size=z.shape) # Adding deformation visually
|
| 79 |
|
| 80 |
if simulation_type == "plate":
|
| 81 |
+
ax.plot_surface(x, y, z_deformed, color='lightblue', alpha=0.8)
|
| 82 |
+
ax.text(length / 2, width / 2, np.max(z_deformed) + 2, f"Deformation: {deformation:.2f} mm", color="red", fontsize=10, ha="center")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
elif simulation_type == "beam":
|
| 84 |
+
ax.plot_surface(x, y, z_deformed, color='lightgreen', alpha=0.8)
|
| 85 |
+
ax.text(length / 2, thickness / 2, np.max(z_deformed) + 2, f"Deflection: {deformation:.2f} mm", color="red", fontsize=10, ha="center")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
raise ValueError("Invalid simulation type.")
|
| 88 |
+
|
| 89 |
# Add dimensions
|
|
|
|
|
|
|
|
|
|
| 90 |
ax.set_xlabel("Length (mm)")
|
| 91 |
+
ax.set_ylabel("Width (mm)")
|
| 92 |
+
ax.set_zlabel("Deformation (mm)")
|
| 93 |
+
ax.set_title(f"3D Visualization of {simulation_type.capitalize()} End Product")
|
| 94 |
+
|
| 95 |
+
# Save the 3D visualization
|
| 96 |
+
output_3d_path = f"{simulation_type}_end_product_3d.png"
|
| 97 |
+
plt.savefig(output_3d_path)
|
| 98 |
plt.close(fig)
|
| 99 |
|
| 100 |
+
return output_3d_path
|
| 101 |
+
|
| 102 |
+
# Example usage:
|
| 103 |
+
simulate_results_2d_path, simulate_results_3d_path = visualize_results('Python-Based Solver', 200, 100, 20, 350, 0.02)
|
| 104 |
+
end_product_3d_path = visualize_end_product('plate', 200, 100, 20, 0.02)
|
| 105 |
+
|
| 106 |
+
print(f"2D Simulation Visualization saved at: {simulate_results_2d_path}")
|
| 107 |
+
print(f"3D Simulation Visualization saved at: {simulate_results_3d_path}")
|
| 108 |
+
print(f"3D End Product Visualization saved at: {end_product_3d_path}")
|