How to speed up my script: the assignment of highest density material to an element from the property of nearby elements?
William Kuo_21386
New Altair Community Member
I am creating a script to change the material property of an element to match the highest density of nearby elements. The goal is to generate a list of elements and a list of material properties to assign my density to. The material properties are already named in the order by how dense it is. Therefore, it's the reason why it's compared. However, this script is slow in the context of having tens of thousands of elements. Is there any way to speed this script up? I was thinking multithreading or a faster function that I haven't been able to find.
*createmark sets 1 "Eset_Surface" *isolateonlyentitybymark 1 *createmark elems 1 "displayed" set surfaceElements [hm_getmark elems 1] set densityList [] foreach e $surfaceElements { *clearmarkall 1 *clearmarkall 2 *createmark elems 1 "by id" $e hm_getnearbyentities inputentitytype=elems inputentitymark=1 outputentitytypes={elems} outputentitymark=2 radius=0.9 nearby_search_method=sphere set nearbyElements [hm_getmark elems 2] set densityElement 1000 foreach n $nearbyElements { set materialID [hm_getvalue elems id=$n dataname=material] if {$materialID < $densityElement} { set densityElement $materialID } } lappend densityList $densityElement } llength $densityElement
0
Answers
-
I updated the code. It's now faster but still takes hours to finish.
*createmark sets 1 "Eset_Surface" *isolateonlyentitybymark 1 *createmark elems 1 "displayed" set surfaceElements [hm_getmark elems 1] set densityList [] foreach e $surfaceElements { *clearmarkall 1 *createmark elements 1 $e *findmark elements 1 1 1 elements 0 2 set nearbyElements [hm_getmark elems 2] set densityElement 1000 foreach n $nearbyElements { set materialID [hm_getvalue elems id=$n dataname=material] if {$materialID < $densityElement} { set densityElement $materialID } } lappend densityList $densityElement } llength $densityList
0 -
I updated my code further and deleted irrelevant elements and put everything in a dictionary. It's faster but still takes an hour. I think the issue is *findmark being slow.
*createmark sets 1 "Eset_Surface" *isolateonlyentitybymark 1 *createmark elems 1 "displayed" set surfaceElements [hm_getmark elems 1] set densityList [] foreach e $surfaceElements { lappend densityList [hm_getvalue elems id=$e dataname=material] } foreach a $surfaceElements b $densityList { dict set elementDict $a $b } dict for {id density} $elementDict { *clearmarkall 1 *createmark elements 1 $id *findmark elements 1 1 1 elements 0 2 foreach n [hm_getmark elems 2] { set density2 [dict get $elementDict $n] if {$density2 < $density} { set densityElement $density2 } } lappend densityList $densityElement } puts [dict size $elementDict] llength $densityList
0