SRPT_APDL_01 / visualization.py
jithenderchoudary's picture
Update visualization.py
856260a verified
import matplotlib.pyplot as plt
def visualize_results(simulator, length, width, thickness, stress, deformation):
"""
Generates 2D visualizations for simulation results.
Parameters:
simulator (str): Name of the simulator (e.g., 'Python-Based Solver').
length (float): Length of the object.
width (float): Width of the object.
thickness (float): Thickness of the object.
stress (float): Stress value.
deformation (float): Deformation value.
Returns:
str: Path to the 2D visualization image.
"""
# Generate 2D bar chart
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(["Stress", "Deformation"], [stress, deformation], color=["red", "blue"])
ax.set_title(f"Simulation Results ({simulator})")
ax.set_ylabel("Magnitude")
ax.grid(True, linestyle="--", alpha=0.6)
# Save the chart
output_path = "results_2d.png"
plt.savefig(output_path)
plt.close(fig)
return output_path, None
def visualize_end_product(simulation_type, length, width, thickness, deformation):
"""
Visualize the end product based on the parameters.
Parameters:
simulation_type (str): 'plate' or 'beam'.
length (float): Length of the product (mm).
width (float): Width of the product (mm).
thickness (float): Thickness of the product (mm).
deformation (float): Deformation (mm).
Returns:
str: Path to the saved image of the visualization.
"""
fig, ax = plt.subplots(figsize=(6, 4))
if simulation_type == "plate":
# Draw the plate
rect = plt.Rectangle((0, 0), length, width, color="lightblue", alpha=0.8)
ax.add_patch(rect)
# Add deformation as text
ax.text(
length / 2, width / 2,
f"Deformation: {deformation:.2f} mm",
color="red", fontsize=10, ha="center"
)
elif simulation_type == "beam":
# Draw the beam
rect = plt.Rectangle((0, 0), length, thickness, color="lightgreen", alpha=0.8)
ax.add_patch(rect)
# Add deformation as text
ax.text(
length / 2, thickness / 2,
f"Deflection: {deformation:.2f} mm",
color="red", fontsize=10, ha="center"
)
else:
raise ValueError("Invalid simulation type.")
# Add dimensions
ax.set_xlim(-10, length + 10)
ax.set_ylim(-10, max(width, thickness) + 10)
ax.set_title(f"Visualization of {simulation_type.capitalize()} End Product")
ax.set_xlabel("Length (mm)")
ax.set_ylabel("Width/Thickness (mm)")
ax.grid(True, linestyle="--", alpha=0.6)
# Save the visualization
output_path = f"{simulation_type}_end_product.png"
plt.savefig(output_path)
plt.close(fig)
return output_path