11from __future__ import print_function
22from __future__ import absolute_import
3+ from __future__ import division
34
45import sys
56
4344def drx_numpy (network , factor = 1.0 , tol = 0.1 , steps = 10000 , refresh = 0 , update = False , callback = None , ** kwargs ):
4445 """Run dynamic relaxation analysis.
4546
46- Parameters:
47- network (obj): Network to analyse.
48- factor (float): Convergence factor.
49- tol (float): Tolerance value.
50- steps (int): Maximum number of steps.
51- refresh (int): Update progress every n steps.
52- update (bool): Update the co-ordinates of the Network.
53- callback (obj): Callback function.
54-
55- Returns:
56- array: Vertex co-ordinates.
57- array: Edge forces.
58- array: Edge lengths.
59-
60- Example:
61-
62- .. plot::
63-
64- import compas
65- from compas.datastructures import Network
66- from compas.plotters import NetworkPlotter
67- from compas.numerical import drx_numpy
68- from compas.utilities import i_to_rgb
69-
70- from numpy import linspace
71-
72- L0 = 1
73- L = 1.5
74- n = 40
75- EI = 0.2
76- pins = [0, 5, 20, n - 5]
77-
78- # Network
79-
80- vertices = [[i, i, 0] for i in list(linspace(0, L0, n))]
81- edges = [[i, i + 1] for i in range(n - 1)]
82-
83- network = Network.from_vertices_and_edges(vertices=vertices, edges=edges)
84- network.update_default_vertex_attributes({'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI})
85- network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n})
86- network.set_vertices_attributes(pins, {'B': [0, 0, 0], 'is_fixed': True})
87- network.beams = {'beam': {'nodes': list(range(n))}}
47+ Parameters
48+ ----------
49+ network : Network
50+ Network to analyse.
51+ factor : float
52+ Convergence factor.
53+ tol : float
54+ Tolerance value.
55+ steps : int
56+ Maximum number of steps.
57+ refresh : int
58+ Update progress every n steps.
59+ update : bool
60+ Update the co-ordinates of the Network.
61+ callback : callable
62+ Callback function.
8863
89- # Plotter
64+ Returns
65+ -------
66+ tuple
9067
91- plotter = NetworkPlotter(network)
68+ * array: Vertex co-ordinates.
69+ * array: Edge forces.
70+ * array: Edge lengths.
9271
93- lines = []
94- for u, v in network.edges():
95- lines.append({
96- 'start': network.vertex_coordinates(u, 'xy'),
97- 'end' : network.vertex_coordinates(v, 'xy'),
98- 'color': '#cccccc',
99- 'width': 1.0})
72+ Example
73+ -------
74+ .. plot::
75+ :include-source:
10076
101- plotter.draw_lines(lines)
77+ import compas
78+ from compas.datastructures import Network
79+ from compas.plotters import NetworkPlotter
80+ from compas.numerical import drx_numpy
81+ from compas.utilities import i_to_rgb
10282
103- # Solver
83+ from numpy import linspace
10484
105- drx_numpy(network=network, tol=0.01, refresh=10, factor=30, update=True)
85+ L0 = 1
86+ L = 1.5
87+ n = 40
88+ EI = 0.2
89+ pins = [0, 5, 20, n - 5]
10690
107- # Result
91+ # Network
10892
109- plotter.draw_vertices(radius=0.005, facecolor={key: '#ff0000' for key in pins})
110- plotter.draw_edges()
93+ vertices = [[i, i, 0] for i in list(linspace(0, L0, n))]
94+ edges = [[i, i + 1] for i in range(n - 1)]
11195
112- plotter.show()
96+ network = Network.from_vertices_and_edges(vertices=vertices, edges=edges)
97+ network.update_default_vertex_attributes({'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI})
98+ network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n})
99+ network.set_vertices_attributes(pins, {'B': [0, 0, 0], 'is_fixed': True})
100+ network.beams = {'beam': {'nodes': list(range(n))}}
113101
102+ # Plotter
114103
115- .. code-block:: python
104+ plotter = NetworkPlotter(network)
116105
117- # This code block produces a dynamic visualization
118- # of the figure above using a callback function.
106+ lines = []
107+ for u, v in network.edges():
108+ lines.append({
109+ 'start': network.vertex_coordinates(u, 'xy'),
110+ 'end' : network.vertex_coordinates(v, 'xy'),
111+ 'color': '#cccccc',
112+ 'width': 1.0})
119113
120- import compas
121- from compas.datastructures import Network
122- from compas.plotters import NetworkPlotter
123- from compas.numerical import drx_numpy
124- from compas.utilities import i_to_rgb
114+ plotter.draw_lines(lines)
125115
126- from numpy import linspace
116+ # Solver
127117
128- # Callback
118+ drx_numpy(network=network, tol=0.01, refresh=10, factor=30, update=True)
129119
130- def plot_iterations(X, radius=0.005):
120+ # Result
131121
132- for i in network.vertices():
133- x, y, z = X[i, :]
134- network.set_vertex_attributes(i, {'x': x, 'y': y, 'z': z})
122+ plotter.draw_vertices(radius=0.005, facecolor={key: '#ff0000' for key in pins})
123+ plotter.draw_edges()
135124
136- plotter.update_vertices(radius)
137- plotter.update_edges()
138- plotter.update(pause=0.01)
125+ plotter.show()
139126
140- # Setup
141-
142- L0 = 1
143- L = 1.5
144- n = 40
145- EI = 0.2
146- pins = [0, 5, 20, n - 5]
147-
148- # Network
149-
150- vertices = [[i, i, 0] for i in list(linspace(0, L0, n))]
151- edges = [[i, i + 1] for i in range(n - 1)]
152-
153- network = Network.from_vertices_and_edges(vertices=vertices, edges=edges)
154- network.update_default_vertex_attributes({'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI})
155- network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n})
156- network.set_vertices_attributes(pins, {'B': [0, 0, 0], 'is_fixed': True})
157- network.beams = {'beam': {'nodes': list(range(n))}}
158-
159- # Plotter
160-
161- plotter = NetworkPlotter(network, figsize=(10, 7))
162-
163- # Initial configuration
164-
165- lines = []
166- for u, v in network.edges():
167- lines.append({
168- 'start': network.vertex_coordinates(u, 'xy'),
169- 'end' : network.vertex_coordinates(v, 'xy'),
170- 'color': '#cccccc',
171- 'width': 1.0})
172-
173- plotter.draw_lines(lines)
174- plotter.draw_vertices(radius=0.005, facecolor={key: '#ff0000' for key in pins})
175- plotter.draw_edges()
176-
177- # Solver with dynamic visualization
178-
179- drx_numpy(network=network, tol=0.01, refresh=10, factor=30, update=True, callback=plot_iterations)
180-
181- plotter.show()
127+ See Also
128+ --------
129+ *
182130
183131 """
184-
185132 # Setup
186133
187134 tic1 = time ()
@@ -286,7 +233,7 @@ def drx_solver(tol, steps, factor, C, Ct, X, ks, l0, f0, ind_c, ind_t, P, S, B,
286233 if (ts % refresh == 0 ) or (res < tol ):
287234 print ('Step:{0} Residual:{1:.3g}' .format (ts , res ))
288235 if callback :
289- callback (X , ** kwargs )
236+ callback (ts , X , ** kwargs )
290237 ts += 1
291238 return X , f , l
292239
@@ -490,8 +437,8 @@ def _create_arrays(network):
490437
491438 from numpy import linspace
492439
493-
494- def plot_iterations ( X , radius = 0.005 ):
440+ def plot_iterations ( k , X , radius = 0.005 ):
441+ print ( k )
495442
496443 for i in network .vertices ():
497444 x , y , z = X [i , :]
@@ -501,7 +448,6 @@ def plot_iterations(X, radius=0.005):
501448 plotter .update_edges ()
502449 plotter .update (pause = 0.01 )
503450
504-
505451 # ==========================================================================
506452 # Example 1
507453 # ==========================================================================
0 commit comments