How to convert element id to internal id by python scripting in LS-PrePost?

Options

I am endeavoring to extract the effective plastic strain from each component in LS-PrePost. My reference is the document titled 'LS-PrePost Scripting Command Language, Python Language and Data Center.pdf,' and I intend to accomplish this task using Python. My strategy includes the following steps:

  1. Record each part information and store it as dictionary format.
  2. The information included part name, part user ID, element ID, and element type.
  3. Using DataCenter module: def get_data() method to extract the element data.
    Question here. How to convert element ID (step2) in internal ID?

I appreciate your time and assistance with this.

get_data() method below.

Best Answers

  • Nikolay Mladenov
    Nikolay Mladenov Member, Employee Posts: 4
    First Answer First Comment
    edited May 29 Answer ✓
    Options

    Please try the snippet below and let me know whether it works:

    import DataCenter as dc
    from DataCenter import Type, Ipt
    import LsPrePost as lspp
    
    num_states = dc.get_data('num_states')
    part_ids = dc.get_data("validpart_ids")
    part_num = part_ids.__len__()
    
    for i in range(num_states):
      lspp.echo("State: "+str(i+1))
      buf = 'state_solidonly{}.pydata'.format(str(i+1))
      f = open(buf,"w")
      f.write("{0:>6s}  {1:>6s}  {2:>6s}  {3:>14s}\n".format("No.","PID","EID","EPS"))
      c = 1
      for j in range(part_num):
        el_ids =  dc.get_data('elemofpart_ids', id=part_ids[j], type=Type.PART, ist=i+1)        
        for k in range(el_ids.__len__()):
          eps = dc.get_data('effective_plastic_strain', id=el_ids[k], type=Type.SOLID, ist=i+1)    
          f.write("{0:6d}  {1:6d}  {2:6d}  {3:+13.7e}\n".format(c,part_ids[j],el_ids[k],eps))
          c += 1
    
      f.close()
    
  • Nikolay Mladenov
    Nikolay Mladenov Member, Employee Posts: 4
    First Answer First Comment
    edited June 4 Answer ✓
    Options

    Please try the script below. It should do what you need and it should be quick.

    1. import time
    2. import DataCenter as dc
    3. from LsPrePost import cmd_result_get_value as crgv, execute_command as exec
    4. 
    5. #advance to the last state
    6. num_states = dc.get_data('num_states')
    7. exec(f'state {num_states}')
    8. 
    9. #send command to fringe EPS (which corrsponds to the digit 7), once it is sent, LSPP would temporarily store element id and max fringe value for active parts, which can be retrieved later via SCL.
    10. exec("fringe 7; pfringe")
    11. 
    12. part_ids = dc.get_data("validpart_ids")
    13. part_num = part_ids.__len__()
    14. for i in range(part_num):
    15.     part_id = part_ids[i]
    16.     start_time = time.time() 
    17.     print ("="*40)
    18.     
    19.     #show part_id only to ensure that both element_id and max_eps LSPP stores are for current active part
    20.     #exec(f"m {part_id}")
    21.     exec(f"selectpart on {part_id}")
    22.     #retrieve element id and max fringe value of current active part from LSPP 
    23.     element_id = crgv(0)
    24.     max_eps = crgv(1)
    25.     print(f"max EPS of the part {part_id} is {max_eps} which happens at element {element_id}")
    26.     
    27.     end_time = time.time()
    28.     execution_time_minutes = (end_time - start_time) / 60
    29.     print(f"Execution time: {execution_time_minutes:.1f} minutes")
    30.     print ("="*40)
    

Answers

  • Chris Harrold
    Chris Harrold Member, Administrator, Employee Posts: 169
    First Answer First Comment First Anniversary Ansys Employee
    admin
    Options

    @Zephyr - sorry this question is so old, but may be @Ayush Kumar, @Sandeep Medikonda or @Rajesh Meena might have some insight if you still need assistance. Sorry this got stranded for so long.

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 416
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    Options
  • Zephyr
    Zephyr Member Posts: 8
    First Comment Photogenic
    Options

    Hi Chris and Ayush,

    I appreciate your response. Recently, I noticed that there have been some updates in the LS-PrePost Scripting Command Language, Python Language, and Data Center Rev.11. I am still studying how to get the effective plastic strain from each component. I will provide an update if there are any positive results.
    My steps are as follows:

    1 Get the total count of parts using the dc.get_data("validpart_ids") method.
    2. Get the element IDs of each part using the dc.get_data("elemofpart_ids") method.
    3. Get the effective plastic strain from each component using the code below.

    Please feel free to share any suggestions or thoughts you may have. Thank you.


  • Zephyr
    Zephyr Member Posts: 8
    First Comment Photogenic
    Options

    Hi Nikolay,

    Thank you for sharing your code. I have made some modifications to better suit my needs and can now obtain the maximum effective plastic strain from each part. I have attached the script and a screenshot from LSPP.

    However, I am encountering an issue with the time it takes to retrieve the data.
    In my testing case, it takes 15 minutes when extracting 3 parts (137499 elements).
    In other cases, the number of elements can range from 1,000,000 to 4,000,000, which significantly impacts efficiency.

    Do you have any suggestions for improving the data retrieval process?

    Thank you,
    Zephyr Yang

  • Zephyr
    Zephyr Member Posts: 8
    First Comment Photogenic
    Options

    Revised the LSPP_results screenshot.

  • Nikolay Mladenov
    Nikolay Mladenov Member, Employee Posts: 4
    First Answer First Comment
    edited June 4
    Options

    Please check the script below. It should do what you need:

    1. import time
    2. import DataCenter as dc
    3. from LsPrePost import cmd_result_get_value_count as crgvc
    4. from LsPrePost import cmd_result_get_value as crgv, execute_command as exec
    5. 
    6. #advance to the last state
    7. num_states = dc.get_data('num_states')
    8. exec(f'state {num_states}')
    9. 
    10. #send command to fringe EPS (which corrsponds to the digit 7), once it is sent, LSPP would temporarily store element id and max fringe value for active parts, which can be retrieved later via SCL.
    11. exec("fringe 7; pfringe")
    12. 
    13. part_ids = dc.get_data("validpart_ids")
    14. part_num = part_ids.__len__()
    15. for i in range(part_num):
    16.     part_id = part_ids[i]
    17.     start_time = time.time() 
    18.     print ("="*40)
    19.     
    20.     #show part_id only to ensure that both element_id and max_eps LSPP stores are for current active part
    21.     #exec(f"m {part_id}")
    22.     cmdbuf = f"selectpart on {part_id}"
    23.     exec(cmdbuf)
    24.     nr = crgvc()
    25.     for j in range(nr):
    26.       print(str(j)+" "+str(crgv(j)))
    27.     #retrieve element id and max fringe value of current active part from LSPP 
    28.     element_id = crgv(0)
    29.     max_eps = crgv(1)
    30.     print(f"max EPS of the part {part_id} is {max_eps} which happens at element {element_id}")
    31.     
    32.     end_time = time.time()
    33.     execution_time_minutes = (end_time - start_time) / 60
    34.     print(f"Execution time: {execution_time_minutes:.1f} minutes")
    35.     print ("="*40)
    
  • Zephyr
    Zephyr Member Posts: 8
    First Comment Photogenic
    Options

    Hi Nikolay

    It is workable and very helpful.
    I am trying understanding the code.
    Thank you.

    Zephyr Yang

  • Zephyr
    Zephyr Member Posts: 8
    First Comment Photogenic
    edited June 4
    Options

    Hi Nikolay

    It is cool that can get the output from " exec("fringe 7; pfringe")".
    May i know what document i can refer to know more details about the return value of fringe?

    Zephyr: Thnak you. I read the related information in LS-PrePost Scripting Command Language, Python Language and Data Center Rev.11.

    Zephyr Yang

  • Nikolay Mladenov
    Nikolay Mladenov Member, Employee Posts: 4
    First Answer First Comment
    Options

    Hello Zephyr,

    the only official documentation on LSPP Python scripting is the document you have mentioned in your comment above: LS-PrePost Scripting Command Language, Python Language and Data Center

    If we need an additional feature, we check with development whether it is available (and not documented) and, if not, whether it can be implemented.

    Regards,
    Nikolay