Skip to content

Commit

Permalink
lint and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Feb 29, 2024
1 parent f73ea58 commit f72b8a3
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 157 deletions.
6 changes: 2 additions & 4 deletions src/compas_tna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get(filename):
>>> import compas_tna
>>> from compas_tna.diagrams import FormDiagram
>>> form = FormDiagram.from_obj(compas.get('faces.obj'))
>>> form = FormDiagram.from_obj(compas.get("faces.obj"))
"""
filename = filename.strip("/")
Expand All @@ -58,9 +58,7 @@ def get(filename):
if os.path.exists(localpath):
return localpath
else:
return "https://raw.githubusercontent.com/BlockResearchGroup/compas_tna/master/data/{}".format(
filename
)
return "https://raw.githubusercontent.com/BlockResearchGroup/compas_tna/master/data/{}".format(filename)


__all_plugins__ = [
Expand Down
2 changes: 1 addition & 1 deletion src/compas_tna/diagrams/diagram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from compas.datastructures import Mesh

Expand Down
8 changes: 5 additions & 3 deletions src/compas_tna/diagrams/forcediagram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from compas_tna.diagrams import Diagram

Expand Down Expand Up @@ -103,7 +103,8 @@ def uv_index(self, form=None):
Parameters
----------
form : :class:`compas_tna.diagrams.FormDiagram`, optional
If provided, this maps edge uv's to the index in a list matching the ordering of corresponding edges in the form diagram.
If provided, this maps edge uv's to the index in a list
matching the ordering of corresponding edges in the form diagram.
Returns
-------
Expand All @@ -121,7 +122,8 @@ def uv_index(self, form=None):
return uv_index

def ordered_edges(self, form):
"""Construct an edge list in which the edges are ordered according to the ordering of edges in a corresponding list of form diagram edges.
"""Construct an edge list in which the edges are ordered
according to the ordering of edges in a corresponding list of form diagram edges.
Parameters
----------
Expand Down
37 changes: 14 additions & 23 deletions src/compas_tna/diagrams/formdiagram.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from compas.geometry import Vector
from compas.datastructures import Graph
from compas.geometry import Vector
from compas.utilities import pairwise

from compas_tna.diagrams import Diagram


Expand All @@ -16,19 +15,22 @@ class FormDiagram(Diagram):
-----
A FormDiagram has the following constructor functions
* ``from_obj`` : Construct a diagram from the geometry described in an OBJ file. Only points, lines, and faces are taken into account.
* ``from_json`` : Construct a diagram from a JSON file containing a serialised "data" dictionary.
* ``from_lines`` : Construct a diagram from pairs of line start and end points.
* ``from_obj`` : Construct a diagram from the geometry described in an OBJ file.
Only points, lines, and faces are taken into account.
* ``from_json`` : Construct a diagram from a JSON file containing a serialised "data" dictionary.
* ``from_lines`` : Construct a diagram from pairs of line start and end points.
Default vertex/edge/face attributes can be "public" or "protected".
Protected attributes are usually only for internal use and should only be modified by the algorithms that rely on them.
Protected attributes are usually only for internal use
and should only be modified by the algorithms that rely on them.
If you do change them, do so with care...
Protected vertex attributes are: ``_sw, _rx, _ry, _rz``.
Protected edge attributes are: ``_h, _f, _a, _is_edge, _is_tension``.
Protected face attributes are: ``_is_loaded``.
The FormDiagram is a mesh.
Since edges are implicit objects in COMPAS meshes, those edges that are not relevant from a TNA perspective have to be marked as ``_is_edge=False``.
Since edges are implicit objects in COMPAS meshes,
those edges that are not relevant from a TNA perspective have to be marked as ``_is_edge=False``.
Usually, the user should not have to worry about this.
Furthermore, changing an edge to ``_is_edge=False`` requires an equivalent change in the force diagram.
Therefore, the attribute ``_is_edge`` is marked as "protected".
Expand Down Expand Up @@ -130,7 +132,7 @@ def from_lines(cls, lines, delete_boundary_face=True, precision=None, **kwargs):
>>> import compas
>>> from compas.files import OBJ
>>> from compas_tna.diagrams import FormDiagram
>>> obj = OBJ(compas.get('lines.obj'))
>>> obj = OBJ(compas.get("lines.obj"))
>>> vertices = obj.parser.vertices
>>> edges = obj.parser.lines
>>> lines = [(vertices[u], vertices[v]) for u, v in edges]
Expand All @@ -156,10 +158,7 @@ def uv_index(self):
dict
A dictionary of uv-index pairs.
"""
return {
(u, v): index
for index, (u, v) in enumerate(self.edges_where(_is_edge=True))
}
return {(u, v): index for index, (u, v) in enumerate(self.edges_where(_is_edge=True))}

def index_uv(self):
"""Returns a dictionary that maps edges in a list to the corresponding
Expand Down Expand Up @@ -197,11 +196,7 @@ def dual_diagram(self, cls):
"""
dual = cls()
fkey_centroid = {fkey: self.face_centroid(fkey) for fkey in self.faces()}
inner = list(
set(self.vertices())
- set(self.vertices_on_boundary())
- set(self.supports())
)
inner = list(set(self.vertices()) - set(self.vertices_on_boundary()) - set(self.supports()))
vertices = {}
faces = {}
for key in inner:
Expand Down Expand Up @@ -360,11 +355,7 @@ def update_boundaries(self):
self.delete_vertex(vertex)
# boundaries
for boundary in self.vertices_on_boundaries():
supports = [
vertex
for vertex in boundary
if self.vertex_attribute(vertex, "is_support")
]
supports = [vertex for vertex in boundary if self.vertex_attribute(vertex, "is_support")]
if len(supports) == 0:
# if the boundary contains no supports
# only an additional face has to be added
Expand Down
9 changes: 4 additions & 5 deletions src/compas_tna/equilibrium/diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
from numpy import empty_like
from numpy.linalg import cond
from scipy.linalg import lstsq
from scipy.linalg import solve
from scipy.linalg import norm
from scipy.linalg import solve
from scipy.sparse.linalg import factorized

from compas.matrices import connectivity_matrix
from compas.matrices import equilibrium_matrix
from compas.linalg import dof
from compas.linalg import rref
from compas.linalg import nonpivots

from compas.linalg import rref
from compas.matrices import connectivity_matrix
from compas.matrices import equilibrium_matrix

EPS = 1 / sys.float_info.epsilon

Expand Down
35 changes: 11 additions & 24 deletions src/compas_tna/equilibrium/horizontal.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from compas.geometry import angle_vectors_xy

from .parallelisation import parallelise_edges


Expand Down Expand Up @@ -51,10 +52,7 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
# form diagram
# --------------------------------------------------------------------------
k_i = form.vertex_index()
i_nbrs = {
k_i[key]: [k_i[nbr] for nbr in form.vertex_neighbors(key)]
for key in form.vertices()
}
i_nbrs = {k_i[key]: [k_i[nbr] for nbr in form.vertex_neighbors(key)] for key in form.vertices()}
fixed = set(list(form.supports()) + list(form.fixed()))
fixed = [k_i[key] for key in fixed]
xy = form.vertices_attributes("xy")
Expand All @@ -65,9 +63,7 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
hmin = form.edges_attribute("hmin", keys=edges)
hmax = form.edges_attribute("hmax", keys=edges)

flipmask = [
-1.0 if form.edge_attribute(edge, "_is_tension") else 1.0 for edge in edges
]
flipmask = [-1.0 if form.edge_attribute(edge, "_is_tension") else 1.0 for edge in edges]

uv_i = form.uv_index()
ij_e = {(k_i[u], k_i[v]): index for (u, v), index in iter(uv_i.items())}
Expand All @@ -77,10 +73,7 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
# force diagram
# --------------------------------------------------------------------------
_k_i = force.vertex_index()
_i_nbrs = {
_k_i[key]: [_k_i[nbr] for nbr in force.vertex_neighbors(key)]
for key in force.vertices()
}
_i_nbrs = {_k_i[key]: [_k_i[nbr] for nbr in force.vertex_neighbors(key)] for key in force.vertices()}
_fixed = list(force.fixed())
_fixed = [_k_i[key] for key in _fixed]
_xy = force.vertices_attributes("xy")
Expand All @@ -103,23 +96,19 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
# that is the (alpha) weighted average of the directions of corresponding
# edges of the two diagrams
# --------------------------------------------------------------------------
uv = [
[factor * (xy[j][0] - xy[i][0]), factor * (xy[j][1] - xy[i][1])]
for (i, j), factor in zip(edges, flipmask)
]
uv = [[factor * (xy[j][0] - xy[i][0]), factor * (xy[j][1] - xy[i][1])] for (i, j), factor in zip(edges, flipmask)]
_uv = [[_xy[j][0] - _xy[i][0], _xy[j][1] - _xy[i][1]] for i, j in _edges]
lengths = [(dx**2 + dy**2) ** 0.5 for dx, dy in uv]
forces = [(dx**2 + dy**2) ** 0.5 for dx, dy in _uv]
# --------------------------------------------------------------------------
# the target vectors
# --------------------------------------------------------------------------
form_targets = [
[alpha * v[0] / l, alpha * v[1] / l] if l else [0, 0]
for v, l in zip(uv, lengths)
[alpha * v[0] / length, alpha * v[1] / length] if length else [0, 0] for v, length in zip(uv, lengths)
]
force_targets = [
[(1 - alpha) * v[0] / l, (1 - alpha) * v[1] / l] if l else [0, 0]
for v, l in zip(_uv, forces)
[(1 - alpha) * v[0] / length, (1 - alpha) * v[1] / length] if length else [0, 0]
for v, length in zip(_uv, forces)
]
targets = [[a[0] + b[0], a[1] + b[1]] for a, b in zip(form_targets, force_targets)]
# --------------------------------------------------------------------------
Expand Down Expand Up @@ -168,7 +157,7 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
# compute the force densities
# --------------------------------------------------------------------------
forces[:] = [f * factor for f, factor in zip(forces, flipmask)]
q = [f / l for f, l in zip(forces, lengths)]
q = [f / length for f, length in zip(forces, lengths)]
# --------------------------------------------------------------------------
# rotate the force diagram 90 degrees in CW direction
# this way the relation between the two diagrams is easier to read
Expand All @@ -188,9 +177,7 @@ def horizontal_nodal(form, force, alpha=100, kmax=100, callback=None):
form.vertex_attributes(key, "xy", xy[i])
for uv in form.edges_where({"_is_edge": True}):
i = uv_i[uv]
form.edge_attributes(
uv, ["q", "_f", "_l", "_a"], [q[i], forces[i], lengths[i], angles[i]]
)
form.edge_attributes(uv, ["q", "_f", "_l", "_a"], [q[i], forces[i], lengths[i], angles[i]])
# --------------------------------------------------------------------------
# update force
# --------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit f72b8a3

Please sign in to comment.