def mapCellDataToCellData(ugrid_data, ugrid_mesh, field_name, cell_length_ratio_for_radius=0.5, threshold_min=None, threshold_max=None, verbose=True): if (verbose): print '*** mapCellDataToCellData ***' pdata_data_cell_centers = getCellCenters(ugrid_data) point_locator = vtk.vtkPointLocator() point_locator.SetDataSet(pdata_data_cell_centers) point_locator.Update() pdata_mesh_cell_centers = getCellCenters(ugrid_mesh) nb_components = ugrid_data.GetCellData().GetArray(field_name).GetNumberOfComponents() nb_cells = ugrid_mesh.GetNumberOfCells() farray_field_avg = createFloatArray(field_name+"_avg", nb_components, nb_cells) farray_field_std = createFloatArray(field_name+"_std", nb_components, nb_cells) points_within_radius = vtk.vtkIdList() for num_cell in range(nb_cells): l = (ugrid_mesh.GetCell(num_cell).GetLength2())**(0.5) point_locator.FindPointsWithinRadius(l*cell_length_ratio_for_radius, pdata_mesh_cell_centers.GetPoint(num_cell), points_within_radius) if (points_within_radius.GetNumberOfIds()): values = [numpy.array(ugrid_data.GetCellData().GetArray(field_name).GetTuple(points_within_radius.GetId(num_id))) for num_id in range(points_within_radius.GetNumberOfIds())] if (threshold_min != None): values = [value for value in values if (numpy.linalg.norm(value) > threshold_min)] if (threshold_max != None): values = [value for value in values if (numpy.linalg.norm(value) < threshold_max)] avg = numpy.mean(values, 0) std = numpy.std(values, 0) else: avg = [0]*nb_components std = [0]*nb_components farray_field_avg.InsertTuple(num_cell, avg) farray_field_std.InsertTuple(num_cell, std) ugrid_mesh.GetCellData().AddArray(farray_field_avg) ugrid_mesh.GetCellData().AddArray(farray_field_std)
def readDynaDeformationGradients(mesh, hystory_files_basename, array_name, verbose=True): if (verbose): print '*** readDynaDeformationGradients ***' nb_cells = mesh.GetNumberOfCells() history_files_names = [ hystory_files_basename + '.history#' + str(num) for num in range(11, 20) ] F_list = [[0. for num_component in range(9)] for num_cell in range(nb_cells)] for num_component in range(9): history_file = open(history_files_names[num_component], 'r') for line in history_file: if line.startswith('*') or line.startswith('$'): continue line = line.split() F_list[int(line[0]) - 1][num_component] = float(line[1]) history_file.close() F_array = createFloatArray(array_name, 9, nb_cells) for num_cell in range(nb_cells): F_array.InsertTuple(num_cell, F_list[num_cell]) if (verbose): print "nb_tuples = " + str(F_array.GetNumberOfTuples()) mesh.GetCellData().AddArray(F_array)
def readFiberOrientation(mesh, filename, verbose=True): if (verbose): print '*** readFiberOrientation ***' nb_cells = mesh.GetNumberOfCells() if verbose: print 'Reading fiber orientations...' eF_array = createFloatArray('eF', 3, nb_cells) eS_array = createFloatArray('eS', 3, nb_cells) eN_array = createFloatArray('eN', 3, nb_cells) file = open(filename, 'r') file.readline() num_cell = 0 for line in file: line = line.split(', ') #print line eF = [float(item) for item in line[1:4]] eS = [float(item) for item in line[4:7]] eN = numpy.cross(eF,eS) #print "eF =", eF #print "eS =", eS #print "eN =", eN eF_array.InsertTuple(num_cell, eF) eS_array.InsertTuple(num_cell, eS) eN_array.InsertTuple(num_cell, eN) num_cell += 1 #print "num_cell =", num_cell file.close() mesh.GetCellData().AddArray(eF_array) mesh.GetCellData().AddArray(eS_array) mesh.GetCellData().AddArray(eN_array) return mesh
def addSystolicStrains(mesh, verbose=True): if (verbose): print '*** addSystolicStrains ***' nb_cells = mesh.GetNumberOfCells() farray_F_dia = mesh.GetCellData().GetArray('F_dia') farray_F_sys = mesh.GetCellData().GetArray('F_sys') farray_E_dia = createFloatArray('E_dia', 6, nb_cells) farray_E_sys = createFloatArray('E_sys', 6, nb_cells) farray_F_num = createFloatArray('F_num', 9, nb_cells) farray_E_num = createFloatArray('E_num', 6, nb_cells) for num_cell in range(nb_cells): F_dia = numpy.reshape(farray_F_dia.GetTuple(num_cell), (3, 3), order='C') F_sys = numpy.reshape(farray_F_sys.GetTuple(num_cell), (3, 3), order='C') #print 'F_dia =', F_dia #print 'F_sys =', F_sys C = numpy.dot(numpy.transpose(F_dia), F_dia) E = (C - numpy.eye(3)) / 2 farray_E_dia.InsertTuple(num_cell, mat_sym_to_vec_col(E)) C = numpy.dot(numpy.transpose(F_sys), F_sys) E = (C - numpy.eye(3)) / 2 farray_E_sys.InsertTuple(num_cell, mat_sym_to_vec_col(E)) F = numpy.dot(F_sys, numpy.linalg.inv(F_dia)) farray_F_num.InsertTuple(num_cell, numpy.reshape(F, 9, order='C')) #print 'F =', F C = numpy.dot(numpy.transpose(F), F) E = (C - numpy.eye(3)) / 2 farray_E_num.InsertTuple(num_cell, mat_sym_to_vec_col(E)) mesh.GetCellData().AddArray(farray_E_dia) mesh.GetCellData().AddArray(farray_E_sys) mesh.GetCellData().AddArray(farray_F_num) mesh.GetCellData().AddArray(farray_E_num)
def mapPointDataToCellData(ugrid_data, ugrid_mesh, field_name, cell_length_ratio_for_radius=0.5, verbose=True): if (verbose): print '*** mapPointDataToCellData ***' nb_cells = ugrid_mesh.GetNumberOfCells() filter_cell_centers = vtk.vtkCellCenters() if (vtk.vtkVersion.GetVTKMajorVersion() >= 6): filter_cell_centers.SetInputData(ugrid_mesh) else: filter_cell_centers.SetInput(ugrid_mesh) filter_cell_centers.Update() pdata_mesh_cell_centers = filter_cell_centers.GetOutput() point_locator = vtk.vtkPointLocator() point_locator.SetDataSet(ugrid_data) point_locator.Update() nb_components = ugrid_data.GetPointData().GetArray(field_name).GetNumberOfComponents() farray_tensors = createFloatArray(field_name, nb_components, nb_cells) points_within_radius = vtk.vtkIdList() for num_cell in range(nb_cells): l = (ugrid_mesh.GetCell(num_cell).GetLength2())**(0.5) point_locator.FindPointsWithinRadius(l*cell_length_ratio_for_radius, pdata_mesh_cell_centers.GetPoint(num_cell), points_within_radius) #points_in_cell = findPointsInCell(ugrid_data.GetPoints(), ugrid_mesh.GetCell(num_cell)) if (points_within_radius.GetNumberOfIds()): tensor = sum([numpy.array(ugrid_data.GetPointData().GetArray(field_name).GetTuple(points_within_radius.GetId(num_id))) for num_id in range(points_within_radius.GetNumberOfIds())])/points_within_radius.GetNumberOfIds() else: tensor = [0]*nb_components farray_tensors.InsertTuple(num_cell, tensor) ugrid_mesh.GetCellData().AddArray(farray_tensors)
def rotateSymmetricMatrix(old_array, in_vecs=None, out_vecs=None, verbose=True): if (verbose): print '*** rotateSymmetricMatrix ***' nb_cells = old_array.GetNumberOfTuples() new_array = createFloatArray("", 6, nb_cells) for num_cell in range(nb_cells): old_matrix = vec_col_to_mat_sym(old_array.GetTuple(num_cell)) if (in_vecs == None): in_R = numpy.eye(3) else: in_R = numpy.transpose( numpy.array([ in_vecs[0].GetTuple(num_cell), in_vecs[1].GetTuple(num_cell), in_vecs[2].GetTuple(num_cell) ])) if (out_vecs == None): out_R = numpy.eye(3) else: out_R = numpy.transpose( numpy.array([ out_vecs[0].GetTuple(num_cell), out_vecs[1].GetTuple(num_cell), out_vecs[2].GetTuple(num_cell) ])) R = numpy.dot(numpy.transpose(in_R), out_R) new_matrix = numpy.dot(numpy.dot(numpy.transpose(R), old_matrix), R) new_array.InsertTuple(num_cell, mat_sym_to_vec_col(new_matrix)) return new_array
def addLocalProlateSpheroidalDirections(ugrid_wall, pdata_end, pdata_epi, type_of_support="cell", epiflip=False, endoflip=False, apexflip=False, points_AB=None, eCCname="eCC", eLLname="eLL", eRRname="eRR", verbose=True): if (verbose): print '*** addLocalProlateSpheroidalDirections ***' if (points_AB == None): points_AB = getABPointsFromBoundsAndCenter(pdata_epi, verbose) assert (points_AB.GetNumberOfPoints() == 2), "points_AB must have two points. Aborting." point_A = numpy.array([0.]*3) point_B = numpy.array([0.]*3) points_AB.GetPoint(0, point_A) points_AB.GetPoint(1, point_B) if(apexflip): eL = point_A - point_B else: eL = point_B - point_A eL /= numpy.linalg.norm(eL) if (type_of_support == "cell"): pdata_cell_centers = getCellCenters(ugrid_wall) if (verbose): print "Computing cell normals..." if(epiflip): pdata_epi = getPDataNormals(pdata_epi, flip=1) else: pdata_epi = getPDataNormals(pdata_epi, flip=0) if(endoflip): pdata_end = getPDataNormals(pdata_end, flip=1) else: pdata_end = getPDataNormals(pdata_end, flip=0) if (verbose): print "Computing surface bounds..." bounds_end = pdata_end.GetBounds() bounds_epi = pdata_epi.GetBounds() z_min_end = bounds_end[4] z_min_epi = bounds_epi[4] z_max_end = bounds_end[5] z_max_epi = bounds_epi[5] L_end = z_max_end-z_min_end L_epi = z_max_epi-z_min_epi if (verbose): print "Initializing cell locators..." cell_locator_end = vtk.vtkCellLocator() cell_locator_end.SetDataSet(pdata_end) cell_locator_end.Update() cell_locator_epi = vtk.vtkCellLocator() cell_locator_epi.SetDataSet(pdata_epi) cell_locator_epi.Update() closest_point_end = [0.]*3 closest_point_epi = [0.]*3 generic_cell = vtk.vtkGenericCell() cellId_end = vtk.mutable(0) cellId_epi = vtk.mutable(0) subId = vtk.mutable(0) dist_end = vtk.mutable(0.) dist_epi = vtk.mutable(0.) if (verbose): print "Computing local prolate spheroidal directions..." if (type_of_support == "cell"): nb_cells = ugrid_wall.GetNumberOfCells() elif (type_of_support == "point"): nb_cells = ugrid_wall.GetNumberOfPoints() farray_norm_dist_end = createFloatArray("norm_dist_end", 1, nb_cells) farray_norm_dist_epi = createFloatArray("norm_dist_epi", 1, nb_cells) farray_norm_z_end = createFloatArray("norm_z_end", 1, nb_cells) farray_norm_z_epi = createFloatArray("norm_z_epi", 1, nb_cells) farray_eRR = createFloatArray(eRRname, 3, nb_cells) farray_eCC = createFloatArray(eCCname, 3, nb_cells) farray_eLL = createFloatArray(eLLname, 3, nb_cells) for num_cell in range(nb_cells): if (type_of_support == "cell"): cell_center = numpy.array(pdata_cell_centers.GetPoints().GetPoint(num_cell)) elif (type_of_support == "point"): cell_center = numpy.array(ugrid_wall.GetPoints().GetPoint(num_cell)) cell_locator_end.FindClosestPoint(cell_center, closest_point_end, generic_cell, cellId_end, subId, dist_end) cell_locator_epi.FindClosestPoint(cell_center, closest_point_epi, generic_cell, cellId_epi, subId, dist_epi) norm_dist_end = dist_end/(dist_end+dist_epi) norm_dist_epi = dist_epi/(dist_end+dist_epi) farray_norm_dist_end.InsertTuple(num_cell, [norm_dist_end]) farray_norm_dist_epi.InsertTuple(num_cell, [norm_dist_epi]) norm_z_end = (closest_point_end[2]-z_min_end)/L_end norm_z_epi = (closest_point_epi[2]-z_min_epi)/L_epi farray_norm_z_end.InsertTuple(num_cell, [norm_z_end]) farray_norm_z_epi.InsertTuple(num_cell, [norm_z_epi]) normal_end = numpy.reshape(pdata_end.GetCellData().GetNormals().GetTuple(cellId_end), (3)) normal_epi = numpy.reshape(pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi), (3)) eRR = -1*(1.-norm_dist_end) * normal_end + (1.-norm_dist_epi) * normal_epi eRR /= numpy.linalg.norm(eRR) eCC = numpy.cross(eL, eRR) eCC /= numpy.linalg.norm(eCC) eLL = numpy.cross(eRR, eCC) farray_eRR.InsertTuple(num_cell, eRR) farray_eCC.InsertTuple(num_cell, eCC) farray_eLL.InsertTuple(num_cell, eLL) if (verbose): print "Filling mesh..." if (type_of_support == "cell"): ugrid_wall.GetCellData().AddArray(farray_norm_dist_end) ugrid_wall.GetCellData().AddArray(farray_norm_dist_epi) ugrid_wall.GetCellData().AddArray(farray_norm_z_end) ugrid_wall.GetCellData().AddArray(farray_norm_z_epi) ugrid_wall.GetCellData().AddArray(farray_eRR) ugrid_wall.GetCellData().AddArray(farray_eCC) ugrid_wall.GetCellData().AddArray(farray_eLL) elif (type_of_support == "point"): ugrid_wall.GetPointData().AddArray(farray_norm_dist_end) ugrid_wall.GetPointData().AddArray(farray_norm_dist_epi) ugrid_wall.GetPointData().AddArray(farray_norm_z_end) ugrid_wall.GetPointData().AddArray(farray_norm_z_epi) ugrid_wall.GetPointData().AddArray(farray_eRR) ugrid_wall.GetPointData().AddArray(farray_eCC) ugrid_wall.GetPointData().AddArray(farray_eLL)
def addLocalFiberOrientation2(ugrid_wall, angles_end, angles_epi, points_AB=None, sigma=0., verbose=True): if (verbose): print '*** addLocalFiberOrientation2 ***' assert (len(angles_end) == len(angles_epi)), "angles_end and angle_epi must have same length (nb_long_nodes). Aborting." nb_long_nodes = len(angles_end) dz = 1./(nb_long_nodes-1) nb_circ_nodes = len(angles_end[0]) for angles in angles_end+angles_epi: assert (len(angles) == nb_circ_nodes), "angles lists must have same length (nb_circ_nodes). Aborting." dt = 2*math.pi/nb_circ_nodes if (points_AB == None): points_AB = getABPointsFromBoundsAndCenter(ugrid_wall, verbose) #print points_AB assert (points_AB.GetNumberOfPoints() >= 2), "\"points_AB\" must have at least two points. Aborting." point_A = numpy.array([0.]*3) point_B = numpy.array([0.]*3) points_AB.GetPoint( 0, point_A) points_AB.GetPoint(points_AB.GetNumberOfPoints()-1, point_B) #if (verbose): print "point_A =", point_A #if (verbose): print "point_B =", point_B eL = point_B - point_A eL /= numpy.linalg.norm(eL) #if (verbose): print "eL =", eL if (verbose): print "Computing local fiber orientation..." farray_r = ugrid_wall.GetCellData().GetArray("r") farray_t = ugrid_wall.GetCellData().GetArray("t") farray_z = ugrid_wall.GetCellData().GetArray("z") farray_norm_dist_end = ugrid_wall.GetCellData().GetArray("norm_dist_end") farray_norm_dist_epi = ugrid_wall.GetCellData().GetArray("norm_dist_epi") farray_norm_z_end = ugrid_wall.GetCellData().GetArray("norm_z_end") farray_norm_z_epi = ugrid_wall.GetCellData().GetArray("norm_z_epi") farray_eRR = ugrid_wall.GetCellData().GetArray("eRR") farray_eCC = ugrid_wall.GetCellData().GetArray("eCC") farray_eLL = ugrid_wall.GetCellData().GetArray("eLL") nb_cells = ugrid_wall.GetNumberOfCells() farray_fiber_angle = createFloatArray("fiber_angle", 1, nb_cells) farray_eF = createFloatArray("eF", 3, nb_cells) farray_eS = createFloatArray("eS", 3, nb_cells) farray_eN = createFloatArray("eN", 3, nb_cells) for num_cell in range(nb_cells): #print "num_cell = " + str(num_cell) t = farray_t.GetTuple(num_cell)[0] i_t = int(t/dt/1.000001) #print "i_t = " + str(i_t) zeta = (t - i_t*dt) / dt #print "zeta = " + str(zeta) norm_z_end = farray_norm_z_end.GetTuple(num_cell)[0] norm_z_epi = farray_norm_z_epi.GetTuple(num_cell)[0] i_z_end = int(norm_z_end/dz/1.000001) i_z_epi = int(norm_z_epi/dz/1.000001) #print "i_z_end = " + str(i_z_end) #print "i_z_epi = " + str(i_z_epi) eta_end = (norm_z_end - i_z_end*dz) / dz eta_epi = (norm_z_epi - i_z_epi*dz) / dz #print "eta_end = " + str(eta_end) #print "eta_epi = " + str(eta_epi) t_ii_end = angles_end[i_z_end][i_t%nb_circ_nodes] t_ji_end = angles_end[i_z_end][(i_t+1)%nb_circ_nodes] t_ij_end = angles_end[(i_z_end+1)][i_t%nb_circ_nodes] t_jj_end = angles_end[(i_z_end+1)][(i_t+1)%nb_circ_nodes] t_ii_epi = angles_epi[i_z_epi][i_t%nb_circ_nodes] t_ji_epi = angles_epi[i_z_epi][(i_t+1)%nb_circ_nodes] t_ij_epi = angles_epi[(i_z_epi+1)][i_t%nb_circ_nodes] t_jj_epi = angles_epi[(i_z_epi+1)][(i_t+1)%nb_circ_nodes] #print "t_ii_end = " + str(t_ii_end) #print "t_ji_end = " + str(t_ji_end) #print "t_ij_end = " + str(t_ij_end) #print "t_jj_end = " + str(t_jj_end) #print "t_ii_epi = " + str(t_ii_epi) #print "t_ji_epi = " + str(t_ji_epi) #print "t_ij_epi = " + str(t_ij_epi) #print "t_jj_epi = " + str(t_jj_epi) fiber_angle_end = t_ii_end * (1 - zeta - eta_end + zeta*eta_end) \ + t_ji_end * (zeta - zeta*eta_end) \ + t_ij_end * (eta_end - zeta*eta_end) \ + t_jj_end * (zeta*eta_end) fiber_angle_epi = t_ii_epi * (1 - zeta - eta_epi + zeta*eta_epi) \ + t_ji_epi * (zeta - zeta*eta_epi) \ + t_ij_epi * (eta_epi - zeta*eta_epi) \ + t_jj_epi * (zeta*eta_epi) norm_dist_end = farray_norm_dist_end.GetTuple(num_cell)[0] norm_dist_epi = farray_norm_dist_epi.GetTuple(num_cell)[0] fiber_angle_in_degrees = (1.-norm_dist_end) * fiber_angle_end + (1.-norm_dist_epi) * fiber_angle_epi if (sigma > 0.): fiber_angle_in_degrees *= random.normalvariate(1., sigma) farray_fiber_angle.InsertTuple(num_cell, [fiber_angle_in_degrees]) eRR = numpy.array(farray_eRR.GetTuple(num_cell)) eCC = numpy.array(farray_eCC.GetTuple(num_cell)) eLL = numpy.array(farray_eLL.GetTuple(num_cell)) fiber_angle_in_radians = math.pi*fiber_angle_in_degrees/180 eF = math.cos(fiber_angle_in_radians) * eCC + math.sin(fiber_angle_in_radians) * eLL eS = eRR eN = numpy.cross(eF, eS) farray_eF.InsertTuple(num_cell, eF) farray_eS.InsertTuple(num_cell, eS) farray_eN.InsertTuple(num_cell, eN) if (verbose): print "Filling mesh..." ugrid_wall.GetCellData().AddArray(farray_fiber_angle) ugrid_wall.GetCellData().AddArray(farray_eF) ugrid_wall.GetCellData().AddArray(farray_eS) ugrid_wall.GetCellData().AddArray(farray_eN)
def addFieldPrincipalDirections(ugrid_mesh, field_name, field_type="cell", field_storage="vec", verbose=True): if (verbose): print '*** addFieldPrincipalDirections ***' assert (field_type in ["point", "cell" ]), "\"field_type\" must be \"point\" or \"cell\". Aborting." assert (field_storage in [ "vec", "Cmat", "Fmat" ]), "\"field_storage\" must be \"vec\", \"Cmat\" or \"Fmat\". Aborting." if (field_type == "cell"): nb_cells = ugrid_mesh.GetNumberOfCells() elif (field_type == "point"): nb_cells = ugrid_mesh.GetNumberOfPoints() if (field_type == "cell"): field = ugrid_mesh.GetCellData().GetArray(field_name) elif (field_type == "point"): field = ugrid_mesh.GetPointData().GetArray(field_name) field_Lmin = createFloatArray(field_name + '_Lmin', 1, nb_cells) field_Lmid = createFloatArray(field_name + '_Lmid', 1, nb_cells) field_Lmax = createFloatArray(field_name + '_Lmax', 1, nb_cells) field_Vmin = createFloatArray(field_name + '_Vmin', 3, nb_cells) field_Vmid = createFloatArray(field_name + '_Vmid', 3, nb_cells) field_Vmax = createFloatArray(field_name + '_Vmax', 3, nb_cells) for num_cell in range(nb_cells): if (field_storage == "vec"): matrix = vec_col_to_mat_sym(field.GetTuple(num_cell)) elif (field_storage == "Cmat"): matrix = numpy.reshape(field.GetTuple(num_cell), (3, 3), order='C') elif (field_storage == "Fmat"): matrix = numpy.reshape(field.GetTuple(num_cell), (3, 3), order='F') if (numpy.linalg.norm(matrix) > 1e-6): #if (verbose): print 'num_cell =', num_cell val, vec = numpy.linalg.eig(matrix) #if (verbose): print 'val =', val #if (verbose): print 'vec =', vec idx = val.argsort() val = val[idx] vec = vec[:, idx] #if (verbose): print 'val =', val #if (verbose): print 'vec =', vec matrix_Lmin = [val[0]] matrix_Lmid = [val[1]] matrix_Lmax = [val[2]] matrix_Vmin = vec[:, 0] matrix_Vmid = vec[:, 1] matrix_Vmax = vec[:, 2] else: matrix_Lmin = [0.] matrix_Lmid = [0.] matrix_Lmax = [0.] matrix_Vmin = [0.] * 3 matrix_Vmid = [0.] * 3 matrix_Vmax = [0.] * 3 field_Lmin.InsertTuple(num_cell, matrix_Lmin) field_Lmid.InsertTuple(num_cell, matrix_Lmid) field_Lmax.InsertTuple(num_cell, matrix_Lmax) field_Vmin.InsertTuple(num_cell, matrix_Vmin) field_Vmid.InsertTuple(num_cell, matrix_Vmid) field_Vmax.InsertTuple(num_cell, matrix_Vmax) if (field_type == "cell"): ugrid_mesh.GetCellData().AddArray(field_Lmin) ugrid_mesh.GetCellData().AddArray(field_Lmid) ugrid_mesh.GetCellData().AddArray(field_Lmax) ugrid_mesh.GetCellData().AddArray(field_Vmin) ugrid_mesh.GetCellData().AddArray(field_Vmid) ugrid_mesh.GetCellData().AddArray(field_Vmax) elif (field_type == "point"): ugrid_mesh.GetPointData().AddArray(field_Lmin) ugrid_mesh.GetPointData().AddArray(field_Lmid) ugrid_mesh.GetPointData().AddArray(field_Lmax) ugrid_mesh.GetPointData().AddArray(field_Vmin) ugrid_mesh.GetPointData().AddArray(field_Vmid) ugrid_mesh.GetPointData().AddArray(field_Vmax)
def addLocalFiberOrientation(ugrid_wall, fiber_angle_end, fiber_angle_epi, hsl0_endo, hsl0_epi, points_AB=None, verbose=True): if (verbose): print '*** addLocalFiberOrientation ***' if (points_AB == None): points_AB = getABPointsFromBoundsAndCenter(ugrid_wall, verbose) assert (points_AB.GetNumberOfPoints() >= 2), "\"points_AB\" must have at least two points. Aborting." point_A = numpy.array([0.] * 3) point_B = numpy.array([0.] * 3) points_AB.GetPoint(0, point_A) points_AB.GetPoint(points_AB.GetNumberOfPoints() - 1, point_B) eL = point_B - point_A eL /= numpy.linalg.norm(eL) if (verbose): print "Computing local fiber orientation..." farray_norm_dist_end = ugrid_wall.GetCellData().GetArray("norm_dist_end") farray_norm_dist_epi = ugrid_wall.GetCellData().GetArray("norm_dist_epi") farray_eRR = ugrid_wall.GetCellData().GetArray("eRR") farray_eCC = ugrid_wall.GetCellData().GetArray("eCC") farray_eLL = ugrid_wall.GetCellData().GetArray("eLL") nb_cells = ugrid_wall.GetNumberOfCells() farray_fiber_angle = createFloatArray("fiber_angle", 1, nb_cells) farray_eF = createFloatArray("fiber vectors", 3, nb_cells) farray_eS = createFloatArray("sheet vectors", 3, nb_cells) farray_eN = createFloatArray("sheet normal vectors", 3, nb_cells) farray_hsl0 = createFloatArray("hsl0 values", 1, nb_cells) # LCL hack to have homogeneous fibers near apex #bds = ugrid_wall.GetBounds() #center = vtk.vtkCellCenters() #center.SetInputData(ugrid_wall) #center.Update() ############################################### for num_cell in range(nb_cells): norm_dist_end = farray_norm_dist_end.GetTuple(num_cell)[0] norm_dist_epi = farray_norm_dist_epi.GetTuple(num_cell)[0] fiber_angle_in_degrees = (1. - norm_dist_end) * fiber_angle_end + ( 1. - norm_dist_epi) * fiber_angle_epi # CKM # Calculate what the hsl should be based on normalized distance from # endo and epi? hsl0_in_nm = (hsl0_endo - hsl0_epi) * norm_dist_epi + hsl0_epi # farray_hsl = createFloatArray("hsl0",1,nb_cells) # farray_hsl.InsertTuple(num_cell, [hsl0_in_nm]) # ugrid_wall.GetCellData().AddArray(farray_fiber_angle) (happens after this for loop) # then read it in during fenics script f.read(meshname + '/hsl0') or something # LCL hack to have homogeneous fibers near apex #zloc = center.GetOutput().GetPoints().GetPoint(num_cell)[2] #if(zloc < bds[4]+1): # fiber_angle_in_degrees = 0 ############################################### farray_fiber_angle.InsertTuple(num_cell, [fiber_angle_in_degrees]) eRR = numpy.array(farray_eRR.GetTuple(num_cell)) eCC = numpy.array(farray_eCC.GetTuple(num_cell)) eLL = numpy.array(farray_eLL.GetTuple(num_cell)) fiber_angle_in_radians = math.pi * fiber_angle_in_degrees / 180 eF = math.cos(fiber_angle_in_radians) * eCC + math.sin( fiber_angle_in_radians) * eLL # CKM trying to normalize here #eF_normalized = eF/numpy.linalg.norm(eF) #print numpy.linalg.norm(eF_normalized) #eF = eF_normalized eS = eRR #eS_normalized = eS/numpy.linalg.norm(eS) #eS = eS_normalized eN = numpy.cross(eF, eS) farray_eF.InsertTuple(num_cell, eF) farray_eS.InsertTuple(num_cell, eS) farray_eN.InsertTuple(num_cell, eN) farray_hsl0.InsertTuple(num_cell, [hsl0_in_nm]) if (verbose): print "Filling mesh..." ugrid_wall.GetCellData().AddArray(farray_fiber_angle) ugrid_wall.GetCellData().AddArray(farray_eF) ugrid_wall.GetCellData().AddArray(farray_eS) ugrid_wall.GetCellData().AddArray(farray_eN) ugrid_wall.GetCellData().AddArray(farray_hsl0)
def readDynaDeformationGradients_vtk(mesh, hystory_files_basename, array_name, ES_timept, verbose=True): if (verbose): print '*** readDynaDeformationGradients ***' nb_cells = mesh.GetNumberOfCells() reader = vtk.vtkLSDynaReader() reader.SetDatabaseDirectory('./') reader.SetTimeStep(ES_timept) reader.Update() print nb_cells, reader.GetNumberOfSolidCells() F11 = vtk.vtkDoubleArray() F12 = vtk.vtkDoubleArray() F13 = vtk.vtkDoubleArray() F21 = vtk.vtkDoubleArray() F22 = vtk.vtkDoubleArray() F23 = vtk.vtkDoubleArray() F31 = vtk.vtkDoubleArray() F32 = vtk.vtkDoubleArray() F33 = vtk.vtkDoubleArray() numtuples = reader.GetOutput().GetBlock(0).GetCellData().GetArray( 'IntPtData').GetNumberOfTuples() reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 10, 10, F11) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 11, 11, F12) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 12, 12, F13) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 13, 13, F21) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 14, 14, F22) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 15, 15, F23) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 16, 16, F31) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 17, 17, F32) reader.GetOutput().GetBlock(0).GetCellData().GetArray('IntPtData').GetData( 0, numtuples - 1, 18, 18, F33) F_array = createFloatArray(array_name, 9, nb_cells) for num_cell in range(nb_cells): F_list = [ F11.GetValue(num_cell), F12.GetValue(num_cell), F13.GetValue(num_cell), F21.GetValue(num_cell), F22.GetValue(num_cell), F23.GetValue(num_cell), F31.GetValue(num_cell), F32.GetValue(num_cell), F33.GetValue(num_cell) ] F_array.InsertTuple(num_cell, F_list) if (verbose): print "nb_tuples = " + str(F_array.GetNumberOfTuples()) mesh.GetCellData().AddArray(F_array)
def addLocalCylindricalDirections(ugrid_wall, type_of_support="cell", points_AB=None, verbose=True): if (verbose): print '*** addLocalCylindricalDirections ***' if (points_AB == None): points_AB = getABPointsFromBoundsAndCenter(ugrid_wall, verbose) #print points_AB assert (points_AB.GetNumberOfPoints() >= 2), "points_AB must have at least two points. Aborting." point_A = numpy.array([0.] * 3) point_B = numpy.array([0.] * 3) points_AB.GetPoint(0, point_A) points_AB.GetPoint(points_AB.GetNumberOfPoints() - 1, point_B) #if (verbose): print "point_A =", point_A #if (verbose): print "point_B =", point_B eL = point_B - point_A eL /= numpy.linalg.norm(eL) #if (verbose): print "eL =", eL if (type_of_support == "cell"): pdata_cell_centers = getCellCenters(ugrid_wall) if (type_of_support == "cell"): nb_cells = ugrid_wall.GetNumberOfCells() elif (type_of_support == "point"): nb_cells = ugrid_wall.GetNumberOfPoints() farray_eR = createFloatArray("eRR", 3, nb_cells) farray_eC = createFloatArray("eCC", 3, nb_cells) farray_eL = createFloatArray("eLL", 3, nb_cells) farray_r = createFloatArray("r", 1, nb_cells) farray_t = createFloatArray("t", 1, nb_cells) farray_z = createFloatArray("z", 1, nb_cells) for num_cell in range(nb_cells): #if (verbose): print "num_cell =", num_cell if (type_of_support == "cell"): cell_center = numpy.array( pdata_cell_centers.GetPoints().GetPoint(num_cell)) elif (type_of_support == "point"): cell_center = numpy.array( ugrid_wall.GetPoints().GetPoint(num_cell)) #if (verbose): print "cell_center =", cell_center #eR = cell_center - point_A #eR -= numpy.dot(eR,eL) * eL #eR /= numpy.linalg.norm(eR) #eC = numpy.cross(eL, eR) eR = cell_center - point_A eC = numpy.cross(eL, eR) eC /= numpy.linalg.norm(eC) eR = numpy.cross(eC, eL) if (numpy.dot(eR, eC) > 1e-6) or (numpy.dot(eR, eL) > 1e-6) or (numpy.dot(eC, eL) > 1e-6): print "WTF?!" farray_eR.InsertTuple(num_cell, eR) farray_eC.InsertTuple(num_cell, eC) farray_eL.InsertTuple(num_cell, eL) r = numpy.dot(cell_center - point_A, eR) farray_r.InsertTuple(num_cell, [r]) t = math.atan2(eR[1], eR[0]) t += (t < 0.) * (2 * math.pi) farray_t.InsertTuple(num_cell, [t]) z = numpy.dot(cell_center - point_A, eL) farray_z.InsertTuple(num_cell, [z]) if (type_of_support == "cell"): ugrid_wall.GetCellData().AddArray(farray_eR) ugrid_wall.GetCellData().AddArray(farray_eC) ugrid_wall.GetCellData().AddArray(farray_eL) ugrid_wall.GetCellData().AddArray(farray_r) ugrid_wall.GetCellData().AddArray(farray_t) ugrid_wall.GetCellData().AddArray(farray_z) elif (type_of_support == "point"): ugrid_wall.GetPointData().AddArray(farray_eR) ugrid_wall.GetPointData().AddArray(farray_eC) ugrid_wall.GetPointData().AddArray(farray_eL) ugrid_wall.GetPointData().AddArray(farray_r) ugrid_wall.GetPointData().AddArray(farray_t) ugrid_wall.GetPointData().AddArray(farray_z) return ugrid_wall
def addLocalFiberOrientation_infarct(ugrid_wall, fiber_angle_end, fiber_angle_epi, inf_fiber_angle_end, inf_fiber_angle_epi, matid, points_AB=None, verbose=True): if (verbose): print '*** addLocalFiberOrientation with infarct ***' if (points_AB == None): points_AB = getABPointsFromBoundsAndCenter(ugrid_wall, verbose) assert (points_AB.GetNumberOfPoints() >= 2), "\"points_AB\" must have at least two points. Aborting." point_A = numpy.array([0.] * 3) point_B = numpy.array([0.] * 3) points_AB.GetPoint(0, point_A) points_AB.GetPoint(points_AB.GetNumberOfPoints() - 1, point_B) eL = point_B - point_A eL /= numpy.linalg.norm(eL) if (verbose): print "Computing local fiber orientation..." farray_norm_dist_end = ugrid_wall.GetCellData().GetArray("norm_dist_end") farray_norm_dist_epi = ugrid_wall.GetCellData().GetArray("norm_dist_epi") farray_eRR = ugrid_wall.GetCellData().GetArray("eRR") farray_eCC = ugrid_wall.GetCellData().GetArray("eCC") farray_eLL = ugrid_wall.GetCellData().GetArray("eLL") nb_cells = ugrid_wall.GetNumberOfCells() farray_fiber_angle = createFloatArray("fiber_angle", 1, nb_cells) farray_eF = createFloatArray("fiber vectors", 3, nb_cells) farray_eS = createFloatArray("sheet vectors", 3, nb_cells) farray_eN = createFloatArray("sheet normal vectors", 3, nb_cells) matid_data = createFloatArray("matid", 1, nb_cells) for num_cell in range(nb_cells): norm_dist_end = farray_norm_dist_end.GetTuple(num_cell)[0] norm_dist_epi = farray_norm_dist_epi.GetTuple(num_cell)[0] matid_data.InsertTuple(num_cell, [matid[num_cell]]) if (matid[num_cell] == 1): fiber_angle_in_degrees = (1. - norm_dist_end) * fiber_angle_end + ( 1. - norm_dist_epi) * fiber_angle_epi else: fiber_angle_in_degrees = ( 1. - norm_dist_end) * inf_fiber_angle_end + ( 1. - norm_dist_epi) * inf_fiber_angle_epi farray_fiber_angle.InsertTuple(num_cell, [fiber_angle_in_degrees]) eRR = numpy.array(farray_eRR.GetTuple(num_cell)) eCC = numpy.array(farray_eCC.GetTuple(num_cell)) eLL = numpy.array(farray_eLL.GetTuple(num_cell)) fiber_angle_in_radians = math.pi * fiber_angle_in_degrees / 180 eF = math.cos(fiber_angle_in_radians) * eCC + math.sin( fiber_angle_in_radians) * eLL eS = eRR eN = numpy.cross(eF, eS) farray_eF.InsertTuple(num_cell, eF) farray_eS.InsertTuple(num_cell, eS) farray_eN.InsertTuple(num_cell, eN) if (verbose): print "Filling mesh..." ugrid_wall.GetCellData().AddArray(farray_fiber_angle) ugrid_wall.GetCellData().AddArray(farray_eF) ugrid_wall.GetCellData().AddArray(farray_eS) ugrid_wall.GetCellData().AddArray(farray_eN) ugrid_wall.GetCellData().AddArray(matid_data)