Assign the highest density material property of adjacent elements to an element for surface elements.

I am developing a script that can assign the highest density material property of adjacent elements to an element for surface elements.

However, I am having trouble computationally writing a script that is fast enough. I have over 200,000 elements and I believe my approach is computationally inefficient.

Right now, this is the process of my script:

  1. Import Abaqus script that has material sets, sections, and sets of elements set up already.
    1. The material IDs and set IDs are named in the order highest to lowest density.
  2. Find the assigned material property of a set through sections.
  3. Find elements and adjacent elements and store in a dictionary.
  4. Assign each element in a dictionary its material ID and set ID.
  5. Determine from the surface elements which element has the highest density by checking its dictionary material ID value.
  6. Then use its set ID to have the target element's set ID remove and assigned to that ID, which correlates to the highest density.

I understand this is a very computationally expensive process but I have been unable to figure this out.

As far as I tried, I have been unable to find an elegant way to find the material property associated with each element, so I have to go through a convoluted method.

If anyone has an idea of how to tackle this