# Input guid = rs.ObjectsByLayer('Plane')[0] rhinomesh = RhinoMesh(guid) vertices, faces = rhinomesh.get_vertices_and_faces() points = [[x, y, sin(x) * cos(y)] for x, y, z in vertices] # Set-up XFunc basedir = 'D:/compas-dev/examples/' tmpdir = 'C:/Temp/' xfunc = XFunc(basedir=basedir, tmpdir=tmpdir) # Python xfunc.funcname = 'hpc_normals_func.python_normals' normals, toc1 = xfunc(points, offset=0.5)['data'] print('Python : {0:.6f} ms'.format(toc1 * 1000)) # Numba xfunc.funcname = 'hpc_normals_func.numba_normals' normals, toc2 = xfunc(points, offset=0.5)['data'] print('Numba : {0:.6f} ms'.format((toc2 * 1000))) # Numpy xfunc.funcname = 'hpc_normals_func.numpy_normals' normals, toc3 = xfunc(points, offset=0.5)['data'] print('Numpy : {0:.6f} ms'.format(toc3 * 1000))
def plot_principal_stresses(structure, step, ptype, scale, rotate=0, layer=None): """ Plots the principal stresses of the elements. Parameters ---------- structure : obj Structure object. step : str Name of the Step. ptype : str 'max' 'min' for maximum or minimum principal stresses. scale : float Scale on the length of the line markers. rotate : int Rotate lines by 90 deg, 0 or 1. layer : str Layer name for plotting. Returns ------- None Notes ----- - Currently an alpha script and only for triangular shell elements in Abaqus. - Centroids are taken on the undeformed geometry. """ data = structure.results[step]['element'] basedir = utilities.__file__.split('__init__.py')[0] xfunc = XFunc('principal_stresses', basedir=basedir, tmpdir=structure.path) xfunc.funcname = 'functions.principal_stresses' result = xfunc(data, ptype, scale, rotate) try: vec1, vec5, pr1, pr5 = result if not layer: layer = '{0}_principal_{1}'.format(step, ptype) rs.CurrentLayer(rs.AddLayer(layer)) rs.DeleteObjects(rs.ObjectsByLayer(layer)) centroids = [ structure.element_centroid(i) for i in sorted(structure.elements, key=int) ] rs.EnableRedraw(False) for c, centroid in enumerate(centroids): v1 = vec1[c] v5 = vec5[c] id1 = rs.AddLine(add_vectors(centroid, scale_vector(v1, -1)), add_vectors(centroid, v1)) id5 = rs.AddLine(add_vectors(centroid, scale_vector(v5, -1)), add_vectors(centroid, v5)) col1 = [255, 0, 0] if pr1[c] > 0 else [0, 0, 255] col5 = [255, 0, 0] if pr5[c] > 0 else [0, 0, 255] rs.ObjectColor(id1, col1) rs.ObjectColor(id5, col5) rs.EnableRedraw(True) except: print('\n***** Error calculating/plotting principal stresses *****')
def plot_data(structure, step, field='um', layer=None, scale=1.0, radius=0.05, cbar=[None, None], iptype='mean', nodal='mean', mode='', colorbar_size=1): """ Plots analysis results on the deformed shape of the Structure. Parameters ---------- structure : obj Structure object. step : str Name of the Step. field : str Field to plot, e.g. 'um', 'sxx', 'sm1'. layer : str Layer name for plotting. scale : float Scale on displacements for the deformed plot. radius : float Radius of the pipe visualisation meshes. cbar : list Minimum and maximum limits on the colorbar. iptype : str 'mean', 'max' or 'min' of an element's integration point data. nodal : str 'mean', 'max' or 'min' for nodal values. mode : int Mode or frequency number to plot, for modal, harmonic or buckling analysis. colorbar_size : float Scale on the size of the colorbar. Returns ------- None Notes ----- - Pipe visualisation of line elements is not based on the element section. """ # Create and clear Rhino layer if not layer: layer = '{0}-{1}'.format(step, field) rs.CurrentLayer(rs.AddLayer(layer)) rs.DeleteObjects(rs.ObjectsByLayer(layer)) rs.EnableRedraw(False) # Node and element data nodes = structure.nodes_xyz() elements = [ structure.elements[i].nodes for i in sorted(structure.elements, key=int) ] nodal_data = structure.results[step]['nodal'] nkeys = sorted(structure.nodes, key=int) ux = [nodal_data['ux{0}'.format(mode)][i] for i in nkeys] uy = [nodal_data['uy{0}'.format(mode)][i] for i in nkeys] uz = [nodal_data['uz{0}'.format(mode)][i] for i in nkeys] try: data = [nodal_data['{0}{1}'.format(field, mode)][i] for i in nkeys] dtype = 'nodal' except (Exception): data = structure.results[step]['element'][field] dtype = 'element' # Postprocess basedir = utilities.__file__.split('__init__.py')[0] xfunc = XFunc('postprocess', basedir=basedir, tmpdir=structure.path) xfunc.funcname = 'functions.postprocess' result = xfunc(nodes, elements, ux, uy, uz, data, dtype, scale, cbar, 255, iptype, nodal) try: toc, U, cnodes, fabs, fscaled, celements, eabs = result print('\n***** Data processed : {0} s *****'.format(toc)) # Plot meshes mesh_faces = [] line_faces = [[0, 4, 5, 1], [1, 5, 6, 2], [2, 6, 7, 3], [3, 7, 4, 0]] block_faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]] tet_faces = [[0, 2, 1, 1], [1, 2, 3, 3], [1, 3, 0, 0], [0, 3, 2, 2]] for element, nodes in enumerate(elements): n = len(nodes) if n == 2: u, v = nodes sp, ep = U[u], U[v] plane = rs.PlaneFromNormal(sp, subtract_vectors(ep, sp)) xa = plane.XAxis ya = plane.YAxis r = radius xa_pr = scale_vector(xa, +r) xa_mr = scale_vector(xa, -r) ya_pr = scale_vector(ya, +r) ya_mr = scale_vector(ya, -r) pts = [ add_vectors(sp, xa_pr), add_vectors(sp, ya_pr), add_vectors(sp, xa_mr), add_vectors(sp, ya_mr), add_vectors(ep, xa_pr), add_vectors(ep, ya_pr), add_vectors(ep, xa_mr), add_vectors(ep, ya_mr) ] guid = rs.AddMesh(pts, line_faces) if dtype == 'element': col1 = col2 = celements[element] elif dtype == 'nodal': col1 = cnodes[u] col2 = cnodes[v] rs.MeshVertexColors(guid, [col1] * 4 + [col2] * 4) elif n == 3: mesh_faces.append(nodes + [nodes[-1]]) elif n == 4: if structure.elements[element].__name__ in [ 'ShellElement', 'MembraneElement' ]: mesh_faces.append(nodes) else: for face in tet_faces: mesh_faces.append([nodes[i] for i in face]) elif n == 8: for block in block_faces: mesh_faces.append([nodes[i] for i in block]) if mesh_faces: guid = rs.AddMesh(U, mesh_faces) rs.MeshVertexColors(guid, cnodes) # Plot colorbar xr, yr, _ = structure.node_bounds() yran = yr[1] - yr[0] if yr[1] - yr[0] else 1 s = yran * 0.1 * colorbar_size xmin = xr[1] + 3 * s ymin = yr[0] xl = [xmin, xmin + s] yl = [ymin + i * s for i in range(11)] verts = [[xi, yi, 0] for xi in xl for yi in yl] faces = [[i, i + 1, i + 12, i + 11] for i in range(10)] id = rs.AddMesh(verts, faces) y = [i[1] for i in verts] yn = yran * colorbar_size colors = [ colorbar(2 * (yi - ymin - 0.5 * yn) / yn, input='float', type=255) for yi in y ] rs.MeshVertexColors(id, colors) h = 0.6 * s for i in range(5): x0 = xmin + 1.2 * s yu = ymin + (5.8 + i) * s yl = ymin + (3.8 - i) * s vu = float(+max(eabs, fabs) * (i + 1) / 5.) vl = float(-max(eabs, fabs) * (i + 1) / 5.) rs.AddText('{0:.5g}'.format(vu), [x0, yu, 0], height=h) rs.AddText('{0:.5g}'.format(vl), [x0, yl, 0], height=h) rs.AddText('0', [x0, ymin + 4.8 * s, 0], height=h) rs.AddText('Step:{0} Field:{1}'.format(step, field), [xmin, ymin + 12 * s, 0], height=h) if mode != '': freq = str(round(structure.results[step]['frequencies'][mode], 3)) rs.AddText('Mode:{0} Freq:{1}Hz'.format(mode, freq), [xmin, ymin - 1.5 * s, 0], height=h) # Return to Default layer rs.CurrentLayer(rs.AddLayer('Default')) rs.LayerVisible(layer, False) rs.EnableRedraw(True) except: print( '\n***** Error encountered during data processing or plotting *****' )
def plot_voxels(structure, step, field='smises', cbar=[None, None], iptype='mean', nodal='mean', vdx=None, mode='', plot='vtk'): """ Voxel 4D visualisation. Parameters ---------- structure : obj Structure object. step : str Name of the Step. field : str Field to plot, e.g. 'smises'. cbar : list Minimum and maximum limits on the colorbar. iptype : str 'mean', 'max' or 'min' of an element's integration point data. nodal : str 'mean', 'max' or 'min' for nodal values. vdx : float Voxel spacing. mode : int mode or frequency number to plot, in case of modal, harmonic or buckling analysis. plot : str Plot voxels with 'vtk'. Returns ------- None """ # Node and element data nodes = structure.nodes_xyz() elements = [ structure.elements[i].nodes for i in sorted(structure.elements, key=int) ] nodal_data = structure.results[step]['nodal'] nkeys = sorted(structure.nodes, key=int) ux = [nodal_data['ux{0}'.format(mode)][i] for i in nkeys] uy = [nodal_data['uy{0}'.format(mode)][i] for i in nkeys] uz = [nodal_data['uz{0}'.format(mode)][i] for i in nkeys] try: data = [nodal_data[field + str(mode)][key] for key in nkeys] dtype = 'nodal' except (Exception): data = structure.results[step]['element'][field] dtype = 'element' # Postprocess basedir = utilities.__file__.split('__init__.py')[0] xfunc = XFunc('postprocess', basedir=basedir, tmpdir=structure.path) xfunc.funcname = 'functions.postprocess' result = xfunc(nodes, elements, ux, uy, uz, data, dtype, 1, cbar, 255, iptype, nodal) try: toc, U, cnodes, fabs, fscaled, celements, eabs = result print('\n***** Data processed : {0} s *****'.format(toc)) except: print('\n***** Error post-processing *****') try: xfunc = XFunc('voxels', basedir=basedir, tmpdir=structure.path) xfunc.funcname = 'functions.plotvoxels' xfunc(values=fscaled, U=U, vdx=vdx, plot=plot) print('\n***** Voxels finished *****') except: print('\n***** Error plotting voxels *****')
def add_tets_from_mesh(structure, name, mesh, draw_tets=False, volume=None, layer='Default', acoustic=False, thermal=False): """ Adds tetrahedron elements from a Rhino mesh to the Structure object. Parameters ---------- structure : obj Structure object to update. name : str Name for the element set of tetrahedrons. mesh : obj The Rhino mesh representing the outer surface. draw_tets : bool Draw the generated tetrahedrons. volume : float Maximum volume for each tet. layer : str Layer to draw tetrahedrons if draw_tets=True. acoustic : bool Acoustic properties on or off. thermal : bool Thermal properties on or off. Returns ------- None Nodes and elements are updated in the Structure object. """ rhinomesh = RhinoMesh(mesh) vertices = rhinomesh.get_vertex_coordinates() faces = [face[:3] for face in rhinomesh.get_face_vertices()] basedir = utilities.__file__.split('__init__.py')[0] xfunc = XFunc('tets', basedir=basedir, tmpdir=structure.path) xfunc.funcname = 'functions.tets_from_vertices_faces' try: tets_points, tets_elements = xfunc(vertices=vertices, faces=faces, volume=volume) for point in tets_points: structure.add_node(point) ekeys = [] for element in tets_elements: nodes = [ structure.check_node_exists(tets_points[i]) for i in element ] ekey = structure.add_element(nodes=nodes, type='TetrahedronElement', acoustic=acoustic, thermal=thermal) ekeys.append(ekey) structure.add_set(name=name, type='element', selection=ekeys) if draw_tets: rs.EnableRedraw(False) rs.DeleteObjects(rs.ObjectsByLayer(layer)) rs.CurrentLayer(layer) tet_faces = [[0, 2, 1, 1], [1, 2, 3, 3], [1, 3, 0, 0], [0, 3, 2, 2]] for i, points in enumerate(tets_elements): xyz = [tets_points[j] for j in points] rs.AddMesh(vertices=xyz, face_vertices=tet_faces) rs.EnableRedraw(True) except: print('***** Error using MeshPy or drawing Tets *****')