Example #1
0
def unload_numpy(mesh, kmax=10000, tol=0.01):
    """"""
    key_index = mesh.key_index()
    uv_index = {(u, v): index for index, (u, v) in enumerate(mesh.edges_where({'is_edge': True}))}

    fixed = [key_index[key] for key in mesh.vertices_where({'is_anchor': True})]
    xyz = array(mesh.vertices_attributes('xyz'), dtype=float64)
    edges = [(key_index[u], key_index[v]) for u, v in mesh.edges_where({'is_edge': True})]
    v = len(xyz)
    e = len(edges)
    p = zeros((v, 3), dtype=float64)
    qpre = zeros((e, 1), dtype=float64)
    fpre = zeros((e, 1), dtype=float64)
    lpre = zeros((e, 1), dtype=float64)
    l0 = array([attr['l0'] for key, attr in mesh.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))
    E = array([attr['E'] * 1e+6 for key, attr in mesh.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))
    radius = array([attr['r'] for key, attr in mesh.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))

    xyz, q, f, l, r = dr_numpy(xyz, edges, fixed, p, qpre, fpre, lpre, l0, E, radius, kmax=kmax, tol1=tol)

    for key, attr in mesh.vertices(True):
        index = key_index[key]
        attr['x'] = xyz[index, 0]
        attr['y'] = xyz[index, 1]
        attr['z'] = xyz[index, 2]
        attr['rx'] = r[index, 0]
        attr['ry'] = r[index, 1]
        attr['rz'] = r[index, 2]

    for key, attr in mesh.edges_where({'is_edge': True}, True):
        index = uv_index[key]
        attr['f'] = f[index, 0]
        attr['l'] = l[index, 0]
calculate_sw = SelfweightCalculator(shell,
                                    density=density,
                                    thickness_attr_name='t')
sw = calculate_sw(xyz)
p[:, 2] = -1.0 * sw[:, 0]

# ==============================================================================
# Run DR
# ==============================================================================

xyz, q, f, l, r = dr_numpy(xyz,
                           edges,
                           fixed,
                           p,
                           qpre,
                           fpre,
                           lpre,
                           l0,
                           E,
                           radius,
                           kmax=15000,
                           tol1=0.01)

# ==============================================================================
# Update
# ==============================================================================

for key, attr in shell.vertices(True):
    index = key_index[key]
    attr['x'] = xyz[index, 0]
    attr['y'] = xyz[index, 1]
    attr['z'] = xyz[index, 2]
xyz    = array(shell.get_vertices_attributes('xyz'), dtype=float64)
p      = array(shell.get_vertices_attributes(('px', 'py', 'pz')), dtype=float64)

edges  = [(key_index[u], key_index[v]) for u, v in shell.edges_where({'is_edge': True})]
qpre   = array([0.0] * len(edges), dtype=float64).reshape((-1, 1))
fpre   = array([0.0] * len(edges), dtype=float64).reshape((-1, 1))
lpre   = array([0.0] * len(edges), dtype=float64).reshape((-1, 1))
l0     = array([attr['l0'] for u, v, attr in shell.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))
E      = array([attr['E'] * 1e+6 for u, v, attr in shell.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))
radius = array([attr['r'] for u, v, attr in shell.edges_where({'is_edge': True}, True)], dtype=float64).reshape((-1, 1))

# ==============================================================================
# Run DR
# ==============================================================================

xyz, q, f, l, r = dr_numpy(xyz, edges, fixed, p, qpre, fpre, lpre, l0, E, radius, kmax=1000)

# ==============================================================================
# Update
# ==============================================================================

for key, attr in shell.vertices(True):
    index = key_index[key]

    attr['x'] = xyz[index, 0]
    attr['y'] = xyz[index, 1]
    attr['z'] = xyz[index, 2]
    attr['rx'] = r[index, 0]
    attr['ry'] = r[index, 1]
    attr['rz'] = r[index, 2]
    for key, attr in mesh.vertices(True):
        index = k_i[key]
        attr['x'] = xyz[index, 0]
        attr['y'] = xyz[index, 1]
        attr['z'] = xyz[index, 2]


# ==============================================================================
# run the DR solver

xyz, q, f, l, r = dr_numpy(vertices,
                           edges,
                           fixed,
                           loads,
                           qpre,
                           fpre,
                           lpre,
                           linit,
                           E,
                           radius,
                           kmax=100,
                           callback=callback)

# ==============================================================================
# update

for index, (u, v, attr) in enumerate(mesh.edges(True)):
    attr['f'] = f[index, 0]
    attr['l'] = l[index, 0]

# ==============================================================================
# visualise the result
Example #5
0
def apply_loads_numpy(mesh,
                      safety_loads=1.0,
                      safety_selfweight=1.0,
                      kmax=10000,
                      tol=0.01):
    """Apply loads to a materialised mesh.

    Parameters
    ----------
    mesh : compas_fofin.datastructures.Shell
        The data structure.
    safety_loads : float (1.0)
        Safety factor to be applied to the external loads.
        Default is ``1.0``.
    safety_selfweight : float (1.0)
        Safety factor to be applied to the selfweight.
        Default is ``1.0``.
    kmax : int (10000)
        Maximum number of iterations for the relaxation.
        Default is ``10000``.
    tol : float (0.01)
        Tolerance for residual forces.
        Default is ``0.01``.

    Returns
    -------
    None
        The function updates the input mesh and therefore does not return anything.

    """
    key_index = mesh.key_index()
    uv_index = {
        (u, v): index
        for index, (u, v) in enumerate(mesh.edges_where({'is_edge': True}))
    }

    fixed = [
        key_index[key] for key in mesh.vertices_where({'is_anchor': True})
    ]
    xyz = array(mesh.vertices_attributes('xyz'), dtype=float64)
    edges = [(key_index[u], key_index[v])
             for u, v in mesh.edges_where({'is_edge': True})]
    v = len(xyz)
    e = len(edges)
    p = array(mesh.vertices_attributes(('px', 'py', 'pz')), dtype=float64)
    qpre = zeros((e, 1), dtype=float64)
    fpre = zeros((e, 1), dtype=float64)
    lpre = zeros((e, 1), dtype=float64)
    l0 = array([
        attr['l0'] for key, attr in mesh.edges_where({'is_edge': True}, True)
    ],
               dtype=float64).reshape((-1, 1))
    E = array([
        attr['E'] * 1e+6
        for key, attr in mesh.edges_where({'is_edge': True}, True)
    ],
              dtype=float64).reshape((-1, 1))
    radius = array(
        [attr['r'] for key, attr in mesh.edges_where({'is_edge': True}, True)],
        dtype=float64).reshape((-1, 1))

    p *= safety_loads

    density = mesh.attributes['density']
    if density:
        calculate_sw = SelfweightCalculator(mesh, density=density)
        sw = calculate_sw(xyz)
        p[:, 2] = -safety_selfweight * sw[:, 0]

    xyz, q, f, l, r = dr_numpy(xyz,
                               edges,
                               fixed,
                               p,
                               qpre,
                               fpre,
                               lpre,
                               l0,
                               E,
                               radius,
                               kmax=kmax,
                               tol1=tol)

    for key, attr in mesh.vertices(True):
        index = key_index[key]
        attr['x'] = xyz[index, 0]
        attr['y'] = xyz[index, 1]
        attr['z'] = xyz[index, 2]
        attr['rx'] = r[index, 0]
        attr['ry'] = r[index, 1]
        attr['rz'] = r[index, 2]

    for key, attr in mesh.edges_where({'is_edge': True}, True):
        index = uv_index[key]
        attr['f'] = f[index, 0]
        attr['l'] = l[index, 0]
linit = network.get_edges_attribute('linit')
E = network.get_edges_attribute('E')
radius = network.get_edges_attribute('radius')

lines = []
for u, v in network.edges():
    lines.append({
        'start': network.vertex_coordinates(u, 'xy'),
        'end': network.vertex_coordinates(v, 'xy'),
        'color': '#cccccc',
        'width': 1.0
    })

plotter = NetworkPlotter(network)
plotter.draw_lines(lines)

xyz, q, f, l, r = dr_numpy(vertices, edges, fixed, loads, qpre, fpre, lpre,
                           linit, E, radius)

for key, attr in network.vertices(True):
    index = k_i[key]
    attr['x'] = xyz[index, 0]
    attr['y'] = xyz[index, 1]
    attr['z'] = xyz[index, 2]

plotter.draw_vertices(facecolor={
    key: '#ff0000'
    for key in network.vertices_where({'is_fixed': True})
})
plotter.draw_edges()
plotter.show()
Example #7
0
lpre = mesh.edges_attribute('lpre')
linit = mesh.edges_attribute('l0')
E = mesh.edges_attribute('E')
radius = mesh.edges_attribute('radius')

plotter = MeshPlotter(mesh, figsize=(8, 5))
plotter.draw_vertices()
plotter.draw_edges()
plotter.update(pause=1.000)

xyz, q, f, l, r = dr_numpy(xyz,
                           edges,
                           fixed,
                           loads,
                           qpre,
                           fpre,
                           lpre,
                           linit,
                           E,
                           radius,
                           callback=plot)

for key, attr in mesh.vertices(True):
    index = key_index[key]
    mesh.vertex_attributes(key, 'xyz', xyz[index])
    mesh.vertex_attributes(key, ['rx', 'ry', 'rz'], r[index])

for index, (key, attr) in enumerate(mesh.edges(True)):
    attr['q'] = q[index, 0]
    attr['f'] = f[index, 0]
    attr['l'] = l[index, 0]