Skip to content

Commit a585f41

Browse files
Revathyvenugopal162pre-commit-ci[bot]pyansys-ci-bot
authored
fix: optimization plotting (#887)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 2cf2304 commit a585f41

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

doc/changelog/887.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimization plotting

examples/Optimization/Plate_Thickness_Optimization.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949
from ansys.dyna.core import Deck
5050
from ansys.dyna.core import keywords as kwd
5151
from ansys.dyna.core.pre.examples.download_utilities import EXAMPLES_PATH, DownloadManager
52-
from ansys.dyna.core.run.linux_runner import LinuxRunner
52+
53+
# from ansys.dyna.core.run.linux_runner import LinuxRunner
54+
from ansys.dyna.core.run.local_solver import run_dyna
5355
from ansys.dyna.core.run.options import MemoryUnit, MpiOption, Precision
54-
from ansys.dyna.core.run.windows_runner import WindowsRunner
56+
57+
# from ansys.dyna.core.run.windows_runner import WindowsRunner
5558

5659
# sphinx_gallery_thumbnail_path = '_static/pre/opt/plate_thickness.png'
5760

@@ -276,16 +279,15 @@ def write_input_deck(**kwargs):
276279

277280

278281
def run_job(directory):
279-
if os.name == "nt":
280-
runner = WindowsRunner(
281-
ncpu=2, memory=2, precision=Precision.SINGLE, mpi_option=MpiOption.MPP_INTEL_MPI, memory_unit=MemoryUnit.MB
282-
)
283-
elif os.name == "posix":
284-
runner = LinuxRunner(
285-
ncpu=2, memory=2, precision=Precision.DOUBLE, mpi_option=MpiOption.MPP_INTEL_MPI, memory_unit=MemoryUnit.MB
286-
)
287-
runner.set_input("input.k", directory)
288-
runner.run() # Run LS-DYNA simulation
282+
run_dyna(
283+
"input.k",
284+
working_directory=directory,
285+
ncpu=2,
286+
memory=2,
287+
precision=Precision.SINGLE,
288+
mpi_option=MpiOption.MPP_INTEL_MPI,
289+
memory_unit=MemoryUnit.MB,
290+
)
289291
assert os.path.isfile(os.path.join(directory, "d3plot")), "No result file found"
290292

291293

@@ -340,38 +342,47 @@ def get_plate_displacement(directory):
340342
# ~~~~~~~~~~~~~~~~~~~~~~
341343
#
342344

343-
for iteration in range(0, max_iterations):
344-
# Define thickness based on iteration
345+
all_results = []
346+
347+
for iteration in range(max_iterations):
348+
# Define thickness for this iteration
345349
thickness = initial_thickness + thickness_increment * iteration
346-
wd = os.path.join(workdir.name, "thickness_%.4s" % thickness)
347-
print(wd)
350+
wd = os.path.join(workdir.name, f"thickness_{thickness:.4f}")
348351
pathlib.Path(wd).mkdir(exist_ok=True)
349352
# Create LS-Dyna input deck with new thickness
350353
write_input_deck(thickness=thickness, wd=wd)
351-
# Run Solver
352354
try:
355+
# Run solver
353356
run_job(wd)
354-
# Run PyDPF Post
357+
# Post-process displacement
355358
time_data, max_disp_data, min_disp_data = get_plate_displacement(wd)
356-
reduced_time_data = [t * 1000 for t in time_data]
357-
# Determine if target displacement is reached
359+
reduced_time_data = [t * 1000 for t in time_data] # Convert to ms
360+
# Store result
361+
all_results.append({"thickness": thickness, "time": reduced_time_data, "max_disp": max_disp_data})
362+
# Check if target displacement is reached
358363
if max(max_disp_data) <= target_displacement:
359-
print("Final Thickness: %.4s, Max Plate Displacement: %.5s" % (thickness, max(max_disp_data)))
360-
plt.plot(
361-
reduced_time_data, max_disp_data, "r", label="Max Plate Displacement with %.4s thickness" % thickness
362-
)
364+
print(f"Target displacement reached at thickness {thickness:.4f}")
363365
break
364-
# Add series to the plot
365-
plt.plot(reduced_time_data, max_disp_data, "b")
366366

367367
except Exception as e:
368-
print(e)
368+
print(f"Iteration {iteration} failed:", e)
369+
369370
###############################################################################
370371
# Generate graphical output
371372
# ~~~~~~~~~~~~~~~~~~~~~~~~~
372373
#
373374

374-
plt.xlabel("Time (e^-3 s)")
375+
# Now plot all results
376+
plt.figure(figsize=(8, 5))
377+
for res in all_results:
378+
thickness = res["thickness"]
379+
time_data = res["time"]
380+
max_disp_data = res["max_disp"]
381+
color = "r" if max(max_disp_data) <= target_displacement else "b"
382+
label = f"Thickness {thickness:.4f}"
383+
plt.plot(time_data, max_disp_data, color=color, label=label)
384+
plt.xlabel("Time (ms)")
375385
plt.ylabel("Displacement (mm)")
376-
plt.legend()
386+
plt.title("Plate Displacement vs Time for Different Thicknesses")
387+
plt.grid(True)
377388
plt.show()

0 commit comments

Comments
 (0)