TAGGED: ls_dyna
-
-
December 11, 2024 at 8:40 pmrji229Subscriber
Path problem of simulation result files when using python or cmd to call ls-run to calculate k files
Hello,
I aim to write a Python script to sequentially run
.k
files located in multiple subfolders within the current directory. To achieve this, I plan to use Python to invoke theLS-Run
command for executing the.k
files. However, I am facing an issue: although the.k
files are located in different subfolders, the result files generated by the program are always saved in the current directory. Unfortunately, I could not find any option in theLS-Run
keywords to specify the output file directory.
Here is the code for running the .k files: -
December 12, 2024 at 10:28 pmReno GenestAnsys Employee
Hello,
By default, the results files (d3plot) and other output files (messag, d3hsp, etc.) are written in the working directory where the .k file is located. I think there is something wrong with your Python script.
I would do the process manually without the Python script first. Does it work?
Also, I would simplify your Python script to only run LS-DYNA. Are you able to get the results file in the working directory? Then, add complexities later.
At last, why do you use LS-RUN? Why not use LS-DYNA command lines directly in your Python script? What LS-RUN does is it takes inputs (number of cores, input file location, solver location, etc.) and creates the LS-DYNA command line and sends it to the computer. The command line created by LS-RUN is in the "Preview" field of LS-RUN:
Â
You can run this command line directly in command prompt.
Â
Let me know if this helps or not.
Â
Reno.
-
December 13, 2024 at 2:46 pmrji229Subscriber
Thanks for your advice. If I manually use LS-Run, the result d3plot files are in the current .k file directory, there is no problem.
I use Ls-run because I found an online tutorial that said putting the command in "cmd" could run .k files. I don't know how to use LS-DYNA command lines directly in the Python script. Are there any references, instructions, or examples that I can learn from?
-
-
December 13, 2024 at 8:30 pmReno GenestAnsys Employee
Hello,
I created a short Python program to run LS-DYNA. By default, LS-DYNA will run where the Python script is located and the output files (d3plot, messag, d3hsp, etc.) will be written where the Python script is located.
The solution is to change the directory using os.chdir in your Python script before you call the LS-DYNA solver. Here is an example:
import osimport subprocessÂos.chdir("D:\KFILES\Cube\Script")Âsubprocess.run(["C:\LS-DYNA_Solvers\SMP_DEV_dp\ls-dyna_smp_d_DEV_116799-gea62947e59_winx64_ifort190.exe", "i=D:\KFILES\Cube\Script\input.k", "ncpu=1", "memory=20m"])Â#os.system('"C:\LS-DYNA_Solvers\SMP_DEV_dp\ls-dyna_smp_d_DEV_116799-gea62947e59_winx64_ifort190.exe" i=D:\KFILES\Cube\Script\input.k ncpu=1 memory=20m')Â#result = subprocess.run(["C:\LS-DYNA_Solvers\SMP_DEV_dp\ls-dyna_smp_d_DEV_116799-gea62947e59_winx64_ifort190.exe", "i=D:\KFILES\Cube\Script\input.k", "ncpu=1", "memory=20m"], capture_output=True, text=True)Â#print(result.stdout)ÂÂYou can run LS-DYNA using the subprocess.run() method or the os.system() method. But, subprocess.run() has more options and is recommended here How to execute a Program or System Command from Python - Python Engineer.ÂÂÂLet me know how it goes.ÂÂReno.-
December 17, 2024 at 9:19 pmrji229Subscriber
Many thanks for your assistance. I have a further question. Is it possible to set the maximum running time for the simulation through the subprocess.run command? If the running time exceeds the maximum time, this job will be neglected and then switched to run the next .k file.
-
-
December 18, 2024 at 12:09 amReno GenestAnsys Employee
Hello,
You can use the timeout= option with the subprocess.run() method. I tested it and it seems to work.
I Googled "How to set a maximum running time with subprocess.run() Python?" to find the solution.
"
You can set a maximum running time (timeout) forÂsubprocess.run()
 using theÂtimeout
 parameter:PythonÂimport subprocess
try:
result = subprocess.run(
["command", "arg1", "arg2"],
timeout=5, # Timeout in seconds
capture_output=True,
text=True
)
print("Output:", result.stdout)
except subprocess.TimeoutExpired:
print("Command timed out")ÂExplanation:timeout
:Â TheÂtimeout
 parameter takes a float value representing the maximum number of seconds you want the process to run.capture_output=True
:Â This captures the standard output and standard error of the subprocess, making them available inÂresult.stdout
 andÂresult.stderr
.text=True
:Â This decodes the output as text (if possible).
If the subprocess exceeds the timeout:A subprocess.TimeoutExpired exception will be raised and The subprocess will be terminated."Âhttps://docs.python.org/3/library/subprocess.html#subprocess.TimeoutExpiredÂLet me know how it goes.ÂÂReno. -
December 18, 2024 at 12:13 amReno GenestAnsys Employee
Hello,
The timeout may throw an error message. You may want to catch the error with try: and except: as in the code block in my previous post:
try:
   subprocess.run([...], timeout=3000)
except:
    print("Run time exceeded. Go to the next run.")
Â
Let me know how it goes.
Â
Reno.
-
December 18, 2024 at 7:02 pmrji229Subscriber
Many thanks for your reply. Another problem occurs when I try to use the command to run k files. For example, in the screenshot, I generate a series of k files through code and run these k files through a complete restart (while inputting the corresponding d3dump files). The problem now is that in some steps, the information in the message file has not been updated for a long time, but the Python code is still displayed as being executed. This situation does not happen every time, and may only occur in certain cases. I want to ask if this is the case, how can I determine whether ls-dyna is still iterating the time step or has stopped running?
-
-
December 18, 2024 at 7:53 pmReno GenestAnsys Employee
Hello,
It does happen for a specific model and a specific LS-DYNA solver that the run is "hanging" and nothing is happening. We can verify by looking at the LS-DYNA process in task manager (CPU usage %, etc.). If the CPU % is almost zero, then the run is hanging. I am not sure how to check this with Python code. Maybe you can find something with a Google search.
Or, you may be able to check the messag file and kill the run if there is nothing written to the file for a certain period of time.
The situation when the LS-DYNA solver is hanging is called a deadlock. You will find more information about deadlocks here:
Deadlocks guide - SQL Server | Microsoft Learn
Introduction of Deadlock in Operating System - GeeksforGeeks
Â
In the above 2 links, you will find ways to identify deadlocks. You can try to implement these methods with Python.
Â
Reno.
Â
Reno.
-
- You must be logged in to reply to this topic.
- LS-DYNA Installation Issues with Student Workbench 2024 R2
- Mathematical model generation stuck at 10%
- Cross-coupled stiffness elements in LS-DYNA
- LS-Dyna CESE SMP d vs MPP d solver
- CESE solver – Ignition mechanism
- Initial Stress Shell Application and HistVarCosine in LS-DYNA
- shape memory alloy material in LS-DYNA
- *** Error 40058 (SOL+58) retractor 1000002:convergence failure at time1.01E+02
- MAT072R3- concrete damage rel3 validation
- Initial Velocity Generation
-
1502
-
599
-
596
-
591
-
366
© 2025 Copyright ANSYS, Inc. All rights reserved.