I am looking to optimize this code. In its current state it is working, however, I am looking to improve it.
To represent the situation. I have a vtkPixel
(broken_cell_info) whose Bounds I locate and then calculate the center point. After that I look for that point in vtkCell
(output). If the point falls exactly on the cell, the information is printed. If NOT then i check in the four quadrants around the point for any cells in the vicinity, and exits when a point is found.
The current code looks in about 5 Million points and returns 1 Million points; execution time is 8 minutes.
output = data_object.GetOutput()
for t in time_steps:
view.ViewTime = t
Render()
for i in range(0,output_broken.GetNumberOfCells(), 2):
broken_cell_info = output_broken.GetCell(i) ## Get insfomration of SPECIFIC CELL
broken_joint_info = output_broken.GetCellData() ## Get information on the UNSTRCUTURED XML
if broken_joint_info.GetArray(2).GetTuple1(i) > 0 and broken_joint_info.GetArray(2).GetTuple1(i) < 2:
broken_cell_info = output_broken.GetCell(i) ## Get insfomration of SPECIFIC CELL
data_cells_c = broken_cell_info.GetBounds() ## Get bounds of SPECIFIC CELL in the form (xmin,xmax,ymin,ymax,zmin,zmax)
x1 = float("{0:.17g}".format((data_cells_c[0] + data_cells_c[1] ) / 2))
y1 = float("{0:.17g}".format((data_cells_c[2] + data_cells_c[3]) / 2))
cellc = [x1, y1, 0]
cv1 = int(output.FindCell(cellc, None, 0, 1e-3, subId, pcoords, w))
quad_right, quad_bottom, quad_left, quad_top = -1, -1, -1, -1
increment = 1e-3
if cv1 == -1:
while not quad_right > 0 and not quad_top > 0 and not quad_bottom > 0 and not quad_left > 0:
quad_right = output.FindCell([sum(x) for x in zip(cellc, [increment, 0 , 0])], None, 0, 1e-3, subId, pcoords, w) ## Search on the right of the point
quad_top = output.FindCell([sum(x) for x in zip(cellc, [0, increment, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search above the point
quad_bottom = output.FindCell([sum(x) for x in zip(cellc, [0, -increment, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search below the point
quad_left = output.FindCell([sum(x) for x in zip(cellc, [-increment, 0, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search on the left of the point
increment += 1e-2
cv1 = max(quad_right, quad_bottom, quad_left, quad_top)
h = "NEAREST TO " + str(cv1)
else:
h = "Exactly on CELL " + str(cv1)
broken_cell_info_next = output_broken.GetCell(i + 1) ## Get infomration of SPECIFIC CELL
data_cells_c_next = broken_cell_info_next.GetBounds() ## Get bounds of SPECIFIC CELL in the form (xmin,xmax,ymin,ymax,zmin,zmax)
x2 = float("{0:.17g}".format((data_cells_c_next[0] + data_cells_c_next[1]) / 2))
y2 = float("{0:.17g}".format((data_cells_c_next[2] + data_cells_c_next[3]) / 2))
cellc_next = [x2, y2, 0]
cv2 = int(output.FindCell(cellc_next, None, 0, 1e-3, subId, pcoords, w))
quad_right, quad_bottom, quad_left, quad_top = -1, -1, -1, -1
increment = 1e-3
if cv2 == -1:
while not quad_right > 0 and not quad_top > 0 and not quad_bottom > 0 and not quad_left > 0:
quad_right = output.FindCell([sum(x) for x in zip(cellc_next, [increment, 0 , 0])], None, 0, 1e-3, subId, pcoords, w) ## Search on the right of the point
quad_top = output.FindCell([sum(x) for x in zip(cellc_next, [0, increment, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search above the point
quad_bottom = output.FindCell([sum(x) for x in zip(cellc_next, [0, -increment, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search below the point
quad_left = output.FindCell([sum(x) for x in zip(cellc_next, [-increment, 0, 0])], None, 0, 1e-3, subId, pcoords, w) ## Search on the left of the point
increment += 1e-2
cv2 = max(quad_right, quad_bottom, quad_left, quad_top)
h2 = "NEAREST TO " + str(cv2)
else:
h2 = "Exactly on CELL " + str(cv2)
material_id = cells.GetArray(7).GetTuple1(cv1)
material_id_next = cells.GetArray(7).GetTuple1(cv2)
fail_mode = broken_joint_info.GetArray(1).GetTuple1(i) ## Get Array "FALURE MODE" of SPECIFIC CELL
# Write to File
rowData = [t]
rowData.extend([i, h, h2, material_id, material_id_next, fail_mode, broken_joint_info.GetArray(2).GetTuple1(i)])
row.writerow(rowData)