Пример #1
0
def numba_dr_run(network,
                 factor=1.0,
                 tol=0.1,
                 steps=10000,
                 summary=0,
                 bmesh=False,
                 scale=0,
                 update=False):
    """ Run Numba accelerated dynamic relaxation analysis.

    Parameters:
        network (obj): Network to analyse.
        factor (float): Convergence factor.
        tol (float): Tolerance value.
        steps (int): Maximum number of iteration steps.
        summary (bool): Print summary at end.
        bmesh (bool): Draw Blender mesh on or off.
        scale (float): Scale on plotting axial forces.
        update (bool): Update the co-ordinates of the Network.

    Returns:
        array: Vertex co-ordinates.
        array: Edge forces.
        array: Edge lengths.
    """

    # Setup

    tic1 = time.time()
    X, B, P, Pn, S, V, E, A, C, Ct, f0, l0, ind_c, ind_t, u, v, M, ks = create_arrays(
        network)
    f0_, ks_, l0_, M_ = f0.ravel(), ks.ravel(), l0.ravel(), M.ravel()
    try:
        beams = 1
        inds, indi, indf, EIx, EIy = beam_data(network.beams, network)
        inds = array(inds)
        indi = array(indi)
        indf = array(indf)
        EIx = EIx.ravel()
        EIy = EIy.ravel()
    except AttributeError:
        beams = 0
        z0 = array([0])
        z1 = array([0.])
        inds, indi, indf, EIx, EIy = z0, z0, z0, z1, z1
    if not ind_c:
        ind_c = [-1]
    if not ind_t:
        ind_t = [-1]
    ind_c = array(ind_c)
    ind_t = array(ind_t)
    rows, cols, vals = find(Ct)
    toc1 = time.time() - tic1

    # Solver

    if summary:
        print('\n\nNumba DR -------------------------')
    tic2 = time.time()
    X = numba_dr_solver(tol, steps, factor, u, v, X, ks_, l0_, f0_, ind_c,
                        ind_t, rows, cols, vals, P, S, B, M_, summary, inds,
                        indi, indf, EIx, EIy, beams)
    uvw, l = uvw_lengths(C, X)
    f = f0 + ks * (l - l0)
    toc2 = time.time() - tic2

    # Plot results

    if bmesh:
        edges = list(zip(u, v))
        bmesh = draw_bmesh('network', vertices=X, edges=edges, layer=19)

    if scale:
        fsc = abs(f) / max(abs(f))
        log = (f > 0) * 1
        colours = ['blue' if i else 'red' for i in log]
        pipes = []
        sp = X[u, :]
        ep = X[v, :]
        for c in range(len(f)):
            r = scale * fsc[c]
            pipes.append({
                'radius': r,
                'start': sp[c, :],
                'end': ep[c, :],
                'colour': colours[c],
                'name': str(f[c]),
                'layer': 19
            })
        xdraw_pipes(pipes, div=4)

    # Summary

    if summary:
        print('Setup time: {0:.3g}s'.format(toc1))
        print('Solver time: {0:.3g}s'.format(toc2))
        print('----------------------------------')

    # Update

    if update:
        k_i = network.key_index()
        for key in list(network.vertices()):
            i = k_i[key]
            x, y, z = X[i, :]
            network.set_vertex_attributes(i, {'x': x, 'y': y, 'z': z})

    return X, f, l
Пример #2
0
def dr_run(network,
           factor=1.0,
           tol=0.1,
           steps=10000,
           refresh=0,
           bmesh=False,
           scale=0,
           update=False):
    """ Run dynamic relaxation analysis.

    Parameters:
        network (obj): Network to analyse.
        factor (float): Convergence factor.
        tol (float): Tolerance value.
        steps (int): Maximum number of iteration steps.
        refresh (int): Update progress every n steps.
        bmesh (bool): Draw Blender mesh updates on or off.
        scale (float): Scale on plotting axial forces.
        update (bool): Update the co-ordinates of the Network.

    Returns:
        array: Vertex co-ordinates.
        array: Edge forces.
        array: Edge lengths.
    """

    # Setup

    tic1 = time()
    X, B, P, Pn, S, V, E, A, C, Ct, f0, l0, ind_c, ind_t, u, v, M, ks = create_arrays(
        network)
    try:
        beams = network.beams
        inds, indi, indf, EIx, EIy = beam_data(beams, network)
    except AttributeError:
        beams = inds = indi = indf = EIx = EIy = None
    toc1 = time() - tic1
    if bmesh:
        k_i = network.key_index()
        edges = [[u[i], v[i]] for i in range(len(u))]
        faces = []
        for fkey in list(network.faces()):
            faces.append([k_i[key] for key in network.face[fkey]])
        bmesh = draw_bmesh('network',
                           vertices=X,
                           edges=edges,
                           faces=faces,
                           layer=19)

    # Solver

    tic2 = time()
    X, f, l = dr_solver(tol, steps, factor, C, Ct, X, ks, l0, f0, ind_c, ind_t,
                        P, S, B, M, V, refresh, bmesh, beams, inds, indi, indf,
                        EIx, EIy)
    toc2 = time() - tic2

    # Plot results

    if scale:
        fsc = abs(f) / max(abs(f))
        log = (f > 0) * 1
        colours = ['blue' if i else 'red' for i in log]
        pipes = []
        sp = X[u, :]
        ep = X[v, :]
        for c in range(len(f)):
            r = scale * fsc[c]
            pipes.append({
                'radius': r,
                'start': sp[c, :],
                'end': ep[c, :],
                'colour': colours[c],
                'name': str(f[c]),
                'layer': 19
            })
        xdraw_pipes(pipes, div=4)

    # Summary

    if refresh:
        print('\n\nNumPy-SciPy DR -------------------')
        print('Setup time: {0:.3g}s'.format(toc1))
        print('Solver time: {0:.3g}s'.format(toc2))
        print('----------------------------------')

    # Update

    if update:
        k_i = network.key_index()
        for key in list(network.vertices()):
            i = k_i[key]
            x, y, z = X[i, :]
            network.set_vertex_attributes(i, {'x': x, 'y': y, 'z': z})

    return X, f, l
Пример #3
0
def draw_network(network, type='mesh', layer=0, show_vertices=0.01, show_edges=0.005, show_faces=True,
                 vertex_name_attr=[], edge_name_attr=[], face_name_attr=[]):
    """ Draw a representation of a Network datastructure.

    Parameters:
        network (obj): Network datastructure.
        type (str): Draw as 'mesh' or 'lines'.
        layer (int): Layer to draw Network on.
        show_vertices (float): Size of vertex cubes when type is 'lines'.
        show_edges (float): Size of lines when type is 'lines'.
        show_faces (bool): Draw Network faces.
        vertex_name_attr (list): Attributes to show in vertex names.
        edge_name_attr (list): Attributes to show in edge names.
        face_name_attr (list): Attributes to show in face names.

    Returns:
        obj: Created Blender mesh or None.
    """
    keys = list(network.vertices())
    vertices = [network.vertex_coordinates(key) for key in keys]

    k_i = network.key_index()
    uv_i = network.uv_index()
    u_v = list(network.edges())
    edges = [(k_i[u], k_i[v]) for u, v in u_v]

    fkeys = list(network.faces())
    faces = [network.face[fkey] for fkey in fkeys]

    if type == 'mesh':

        return draw_bmesh('network', vertices=vertices, edges=edges, faces=faces)

    elif type == 'lines':

        if show_vertices:
            cubes_vertices = []
            for key in keys:
                vertex = network.vertex[key]
                xyz = network.vertex_coordinates(key)
                color = 'black' if vertex.get('is_fixed', None) else 'white'
                name_dic = {}
                for attr in vertex_name_attr:
                    name_dic[attr] = vertex.get(attr, 'None')
                name = json.dumps(name_dic)
                cubes_vertices.append({'radius': show_vertices, 'pos': xyz, 'color': color, 'name': name})
            xdraw_cubes(cubes_vertices)

        if show_edges:
            lines_edges = []
            for u, v in u_v:
                sp = network.vertex_coordinates(u)
                ep = network.vertex_coordinates(v)
                i = uv_i[(u, v)]
                color = network.edge[u][v].get('color', 'grey')
                name_dic = {}
                for attr in edge_name_attr:
                    name_dic[attr] = network.edge[u][v].get(attr, 'None')
                name = json.dumps(name_dic)
                lines_edges.append({'name': name, 'start': sp, 'end': ep, 'layer': layer, 'color': color, 'width': show_edges})
            xdraw_lines(lines_edges)

        if show_faces:
            for fkey in fkeys:
                facedata = network.facedata[fkey]
                color = facedata.get('color', 'grey')
                pts = [vertices[i] for i in network.face[fkey]]
                name_dic = {}
                for attr in face_name_attr:
                    name_dic[attr] = facedata.get(attr, 'None')
                name = json.dumps(name_dic)
                draw_bmesh(name=name, vertices=pts, faces=[list(range(len(pts)))], layer=layer, color=color)

    deselect_all_objects()
Пример #4
0
    """
    bmesh.mode_set(mode='EDIT')
    bmesh.normals_make_consistent(inside=False)
    bmesh.mode_set(mode='OBJECT')


# ==============================================================================
# Debugging
# ==============================================================================

if __name__ == "__main__":

    from compas.cad.blender.utilities import draw_bmesh
    from compas.cad.blender.utilities import clear_layers

    clear_layers([0])

    vertices = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
    faces = [[0, 1, 2], [2, 3, 0]]
    bmesh = draw_bmesh('bmesh', vertices=vertices, faces=faces, wire=True)

    colours = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]]
    colour_bmesh_vertices(bmesh, [0, 1, 2, 3], colours)

    update_bmesh_vertices(bmesh,
                          [[0, 0, 0.1], [1, 0, 0.2], [1, 1, 0.3], [0, 1, 0]])

    print(bmesh_edge_lengths(bmesh))
    print(bmesh_face_areas(bmesh))
    print(bmesh_face_normals(bmesh))