jithenderchoudary commited on
Commit
077a4b7
·
verified ·
1 Parent(s): 28136a4

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +80 -66
visualization.py CHANGED
@@ -1,84 +1,98 @@
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
 
7
- Parameters:
8
- simulator (str): Name of the simulator (e.g., 'Python-Based Solver').
9
- length (float): Length of the object.
10
- width (float): Width of the object.
11
- thickness (float): Thickness of the object.
12
- stress (float): Stress value.
13
- deformation (float): Deformation value.
14
 
15
- Returns:
16
- str: Path to the 2D visualization image.
17
- """
18
- # Generate 2D bar chart
19
- fig, ax = plt.subplots(figsize=(6, 4))
20
- ax.bar(["Stress", "Deformation"], [stress, deformation], color=["red", "blue"])
21
- ax.set_title(f"Simulation Results ({simulator})")
22
- ax.set_ylabel("Magnitude")
23
- ax.grid(True, linestyle="--", alpha=0.6)
24
-
25
- # Save the chart
26
- output_path = "results_2d.png"
 
27
  plt.savefig(output_path)
28
- plt.close(fig)
29
 
30
- return output_path, None
31
 
32
- def visualize_end_product(simulation_type, length, width, thickness, deformation):
 
 
33
  """
34
- Visualize the end product based on the parameters.
 
 
 
 
 
 
 
 
 
 
35
 
36
- Parameters:
37
- simulation_type (str): 'plate' or 'beam'.
38
- length (float): Length of the product (mm).
39
- width (float): Width of the product (mm).
40
- thickness (float): Thickness of the product (mm).
41
- deformation (float): Deformation (mm).
42
 
 
 
 
 
 
 
 
43
  Returns:
44
- str: Path to the saved image of the visualization.
45
  """
46
- fig, ax = plt.subplots(figsize=(6, 4))
47
-
48
- if simulation_type == "plate":
49
- # Draw the plate
50
- rect = plt.Rectangle((0, 0), length, width, color="lightblue", alpha=0.8)
51
- ax.add_patch(rect)
52
- # Add deformation as text
53
- ax.text(
54
- length / 2, width / 2,
55
- f"Deformation: {deformation:.2f} mm",
56
- color="red", fontsize=10, ha="center"
57
- )
58
- elif simulation_type == "beam":
59
- # Draw the beam
60
- rect = plt.Rectangle((0, 0), length, thickness, color="lightgreen", alpha=0.8)
61
- ax.add_patch(rect)
62
- # Add deformation as text
63
- ax.text(
64
- length / 2, thickness / 2,
65
- f"Deflection: {deformation:.2f} mm",
66
- color="red", fontsize=10, ha="center"
67
- )
68
- else:
69
- raise ValueError("Invalid simulation type.")
70
-
71
- # Add dimensions
72
- ax.set_xlim(-10, length + 10)
73
- ax.set_ylim(-10, max(width, thickness) + 10)
74
- ax.set_title(f"Visualization of {simulation_type.capitalize()} End Product")
75
  ax.set_xlabel("Length (mm)")
76
- ax.set_ylabel("Width/Thickness (mm)")
77
- ax.grid(True, linestyle="--", alpha=0.6)
78
 
79
- # Save the visualization
80
- output_path = f"{simulation_type}_end_product.png"
81
  plt.savefig(output_path)
82
- plt.close(fig)
83
 
84
  return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import matplotlib.pyplot as plt
2
+ from mpl_toolkits.mplot3d import Axes3D
3
+ import numpy as np
4
 
5
+ def visualize_results(simulation_type, length, width, thickness, stress, deformation):
6
  """
7
+ Generate 2D visualization of stress and deformation.
8
+ """
9
+ x = np.linspace(0, length, 100)
10
+ stress_distribution = stress * (1 - (x / length))
11
+ deformation_distribution = deformation * (x / length)
12
 
13
+ fig, ax1 = plt.subplots()
 
 
 
 
 
 
14
 
15
+ ax1.set_xlabel("Length (mm)")
16
+ ax1.set_ylabel("Stress (MPa)", color="tab:red")
17
+ ax1.plot(x, stress_distribution, color="tab:red", label="Stress")
18
+ ax1.tick_params(axis="y", labelcolor="tab:red")
19
+
20
+ ax2 = ax1.twinx()
21
+ ax2.set_ylabel("Deformation (mm)", color="tab:blue")
22
+ ax2.plot(x, deformation_distribution, color="tab:blue", label="Deformation")
23
+ ax2.tick_params(axis="y", labelcolor="tab:blue")
24
+
25
+ plt.title(f"{simulation_type} Results")
26
+ plt.legend(loc="upper right")
27
+ output_path = "2d_visualization.png"
28
  plt.savefig(output_path)
29
+ plt.close()
30
 
31
+ return output_path, {"stress": stress_distribution, "deformation": deformation_distribution}
32
 
33
+ def visualize_end_product(use_case, length, width, thickness, deformation):
34
+ """
35
+ Generate an image representation of the end product.
36
  """
37
+ fig, ax = plt.subplots()
38
+ rect = plt.Rectangle((0, 0), length, width, edgecolor="black", facecolor="lightgrey")
39
+ ax.add_patch(rect)
40
+ ax.set_xlim(-10, length + 10)
41
+ ax.set_ylim(-10, width + 10)
42
+ ax.set_title(f"Final Product ({use_case})")
43
+ ax.set_xlabel("Length (mm)")
44
+ ax.set_ylabel("Width (mm)")
45
+ output_path = "end_product_visualization.png"
46
+ plt.savefig(output_path)
47
+ plt.close()
48
 
49
+ return output_path
 
 
 
 
 
50
 
51
+ def create_3d_visualization(length, width, deformation):
52
+ """
53
+ Generate a 3D visualization of deformation based on length and width.
54
+ Parameters:
55
+ length (float): The length of the simulated object.
56
+ width (float): The width of the simulated object.
57
+ deformation (float): The deformation value to visualize.
58
  Returns:
59
+ str: Path to the saved 3D visualization image.
60
  """
61
+ # Create grid data
62
+ x = np.linspace(0, length, 100)
63
+ y = np.linspace(0, width, 100)
64
+ X, Y = np.meshgrid(x, y)
65
+ Z = np.sin((X / length) * np.pi) * np.cos((Y / width) * np.pi) * deformation
66
+
67
+ # Create the 3D plot
68
+ fig = plt.figure()
69
+ ax = fig.add_subplot(111, projection='3d')
70
+ ax.plot_surface(X, Y, Z, cmap='viridis')
71
+ ax.set_title("3D Deformation Visualization")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  ax.set_xlabel("Length (mm)")
73
+ ax.set_ylabel("Width (mm)")
74
+ ax.set_zlabel("Deformation (mm)")
75
 
76
+ # Save the plot to a file
77
+ output_path = "3d_visualization.png"
78
  plt.savefig(output_path)
79
+ plt.close()
80
 
81
  return output_path
82
+
83
+ if __name__ == "__main__":
84
+ # Example usage
85
+ length = 200
86
+ width = 100
87
+ thickness = 10
88
+ stress = 150
89
+ deformation = 2.5
90
+
91
+ # Generate 2D visualization
92
+ visualize_results("Example Simulation", length, width, thickness, stress, deformation)
93
+
94
+ # Generate end-product visualization
95
+ visualize_end_product("Example Use Case", length, width, thickness, deformation)
96
+
97
+ # Generate 3D visualization
98
+ create_3d_visualization(length, width, deformation)