How to use the compas.datastructures.Network function in COMPAS

To help you get started, we’ve selected a few COMPAS examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github compas-dev / compas / docs / examples / plotter / network-shortest-path.py View on Github external
"""

from random import choice

import compas

from compas.utilities import pairwise
from compas.datastructures import Network
from compas.topology import dijkstra_path
from compas_plotters import NetworkPlotter


# make a network from a sample file

network = Network.from_obj(compas.get('grid_irregular.obj'))


# start and end

leaves = list(network.vertices_where({'vertex_degree': 1}))

start = end = 0
while start == end:
    start = choice(leaves)
    end = choice(leaves)

# construc an adjacency dict
# add weight to the edges corresponding to their length
# compute the shortest path

adjacency = {key: network.vertex_neighbors(key) for key in network.vertices()}
github compas-dev / compas / tests / compas / datastructures / test_network.py View on Github external
def k5_network():
    network = Network()
    network.add_edge('a', 'b')
    network.add_edge('a', 'c')
    network.add_edge('a', 'd')
    network.add_edge('a', 'e')

    network.add_edge('b', 'c')
    network.add_edge('b', 'd')
    network.add_edge('b', 'e')

    network.add_edge('c', 'd')
    network.add_edge('c', 'e')

    network.add_edge('d', 'e')

    return network
github compas-dev / compas / src / compas / datastructures / network / equilibrium / dr.py View on Github external
# ==============================================================================

if __name__ == '__main__':

    import random

    import compas
    from compas.datastructures import Network
    from compas_plotters import NetworkPlotter
    from compas.datastructures import network_dr
    from compas.utilities import i_to_rgb

    # make a network
    # and set the default vertex and edge attributes

    network = Network.from_obj(compas.get('lines.obj'))

    # identify the fixed vertices
    # and assign random prescribed force densities to the edges

    for key, attr in network.vertices(True):
        attr['is_fixed'] = network.vertex_degree(key) == 1

    for u, v, attr in network.edges(True):
        attr['qpre'] = 1.0 * random.randint(1, 7)

    # make a plotter for (dynamic) visualization
    # and define a callback function
    # for plotting the intermediate configurations

    plotter = NetworkPlotter(network, figsize=(10, 7), fontsize=6)
github compas-dev / compas / docs / reference / generated / compas-topology-vertex_coloring-1.py View on Github external
import compas
from compas.datastructures import Network
from compas.visualization import NetworkPlotter
from compas.topology import vertex_coloring

network = Network.from_obj(compas.get_data('grid_irregular.obj'))

key_color = vertex_coloring(network)
colors = ['#ff0000', '#00ff00', '#0000ff']

plotter = NetworkPlotter(network)

plotter.draw_vertices(facecolor={key: colors[key_color[key]] for key in network.vertices()})
plotter.draw_edges()

plotter.show()
github compas-dev / compas / src / compas_rhinomac / helpers / artists / networkartist.py View on Github external
'text' : textdict[(u, v)],
            })
        return compas_rhinomac.xdraw_labels(labels, layer=self.layer, clear=False, redraw=False)


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

if __name__ == "__main__":

    import compas
    from compas.datastructures import Network
    from compas_rhinomac.helpers.artists.networkartist import NetworkArtist

    network = Network.from_obj(compas.get_data('grid_irregular.obj'))

    artist = NetworkArtist(network, layer='NetworkArtist')

    artist.clear_layer()

    artist.draw_vertices()
    artist.redraw(0.0)

    artist.draw_vertexlabels()
    artist.redraw(1.0)

    artist.draw_edges()
    artist.redraw(1.0)

    artist.draw_edgelabels()
    artist.redraw(1.0)
github compas-dev / compas / src / compas / numerical / algorithms / dr_6dof.py View on Github external
# ==============================================================================
# Main
# ==============================================================================

if __name__ == "__main__":

    from numpy import cos
    from numpy import pi
    from numpy import sin

    m = 8
    R = 100
    at = 0.25 * pi

    network = Network()
    for i in range(m + 1):
        a = i * at / m
        x = R * sin(a)
        y = -R + R * cos(a)
        network.add_vertex(key=i, x=x, y=y)
        if i < m:
            network.add_edge(u=i, v=i+1)
    network.add_vertex(key=(m + 1), x=0, y=-R)

    network.update_default_edge_attributes({
        'E': 10.0 ** 7,
        'nu': 0.0,
        'A': 1.0,
        'Ix': 2.25 * 0.5 ** 4,
        'Iy': 1.0 / 12,
        'Iz': 1.0 / 12,
github compas-dev / compas / src / compas / datastructures / network / explode.py View on Github external
vertices = [
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [2.0, 0.0, 0.0],
        [3.0, 0.0, 0.0],
        [4.0, 0.0, 0.0],
    ]

    edges = [
        (0, 1),
        (2, 3),
        (3, 4),
    ]

    network = Network.from_vertices_and_edges(vertices, edges)

    print(network_disconnected_vertices(network))
    print(network_disconnected_edges(network))
    print(network_explode(network))
github compas-dev / compas / src / compas / numerical / drx / drx_numba.py View on Github external
s = (m + 1)
            p1 = (j + 0) * s + i + 0
            p2 = (j + 0) * s + i + 1
            p3 = (j + 1) * s + i + 0
            p4 = (j + 1) * s + i + 1

            edges.append([p1, p2])
            edges.append([p1, p3])

            if j == m - 1:
                edges.append([p4, p3])

            if i == m - 1:
                edges.append([p2, p4])

    network = Network.from_vertices_and_edges(vertices=vertices, edges=edges)
    sides = [i for i in network.vertices() if network.vertex_degree(i) <= 2]
    network.update_default_vertex_attributes({'P': [0, 0, 1000 / network.number_of_vertices()]})
    network.update_default_edge_attributes({'E': 100, 'A': 1, 'ct': 't'})
    network.set_vertices_attributes(keys=sides, names='B', values=[[0, 0, 0]])

    drx_numba(network=network, tol=0.01, summary=1, update=1)

    data = {
        'vertices': [network.vertex_coordinates(i) for i in network.vertices()],
        'edges': [{'vertices': uv} for uv in network.edges()]
    }

    viewer = VtkViewer(data=data)
    viewer.vertex_size = 0
    viewer.setup()
    viewer.start()
github compas-dev / compas / examples / interface-matlab.py View on Github external
__author__    = ['Tom Van Mele', ]
__copyright__ = 'Copyright 2017, BRG - ETH Zurich',
__license__   = 'MIT'
__email__     = 'van.mele@arch.ethz.ch'


# create a Matlab object
# and connect to existing shared session if possible

matlab = MatlabEngine()
matlab.connect()

# make a network from sample data

network = Network.from_obj(compas.get('grid_irregular.obj'))

# extract vertex coordinates and connectivity matrix
# and convert to Matlab matrices

key_index = network.key_index()

xyz = network.get_vertices_attributes('xyz')
xyz = matlab.double(xyz)

edges = [(key_index[u], key_index[v]) for u, v in network.edges()]

C = connectivity_matrix(edges, rtype='list')
C = matlab.double(C)

# compute coordinate differences in Matlab
github compas-dev / compas / src / compas / numerical / algorithms / dr_6dof_updated.py View on Github external
# ==============================================================================
# Main
# ==============================================================================

if __name__ == "__main__":

    from numpy import cos
    from numpy import pi
    from numpy import sin

    m = 80
    R = 100
    at = 0.25 * pi

    network = Network()
    for i in range(m + 1):
        a = i * at / m
        x = R * sin(a)
        y = -R + R * cos(a)
        network.add_vertex(key=i, x=x, y=y)
        if i < m:
            network.add_edge(key=i, u=i, v=i+1)

    network.update_default_edge_attributes({
        'E': 10.0 ** 7,
        'nu': 0.0,
        'A': 1.0,
        'Ix': 2.25 * 0.5 ** 4,
        'Iy': 1.0 / 12,
        'Iz': 1.0 / 12,
    })