Skip to content

Commit 075397c

Browse files
author
andrewtliew
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/compas/hpc/geometry/basic_numba.py
2 parents a388391 + aa7535e commit 075397c

File tree

21 files changed

+271
-55
lines changed

21 files changed

+271
-55
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ ENV/
109109
libs/**
110110
!libs/PLACEHOLDER
111111

112+
temp/**
113+
!temp/PLACEHOLDER
114+
112115
src/.build/**
113116
*.rhl
114117

src/compas/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
DATA = os.path.abspath(os.path.join(HOME, 'samples'))
4747
DOCS = os.path.abspath(os.path.join(HOME, 'docs'))
4848
LIBS = os.path.abspath(os.path.join(HOME, 'libs'))
49-
TEMP = os.path.abspath(os.path.join(HOME, '__temp'))
49+
TEMP = os.path.abspath(os.path.join(HOME, 'temp'))
5050

5151

5252
def _find_resource(filename):

src/compas/geometry/algorithms/smoothing.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import absolute_import
33
from __future__ import division
44

5+
from copy import deepcopy
56

67
from compas.geometry import centroid_points
78
from compas.geometry import center_of_mass_polygon
@@ -118,7 +119,8 @@ def smooth_centroid(vertices,
118119
fixed = set(fixed)
119120

120121
for k in range(kmax):
121-
vertices_0 = {key: xyz for key, xyz in iter(vertices.items())}
122+
vertices_0 = {key: xyz[:] for key, xyz in iter(vertices.items())}
123+
# vertices_0 = deepcopy(vertices)
122124

123125
for key, point in iter(vertices_0.items()):
124126
if key in fixed:
@@ -131,6 +133,10 @@ def smooth_centroid(vertices,
131133
vertices[key][1] += damping * (centroid[1] - point[1])
132134
vertices[key][2] += damping * (centroid[2] - point[2])
133135

136+
# vertices[key][0] = centroid[0]
137+
# vertices[key][1] = centroid[1]
138+
# vertices[key][2] = centroid[2]
139+
134140
if callback:
135141
callback(vertices, k, callback_args)
136142

@@ -236,7 +242,7 @@ def smooth_centerofmass(vertices,
236242
raise Exception('The callback is not callable.')
237243

238244
for k in range(kmax):
239-
vertices_0 = {key: xyz for key, xyz in iter(vertices.items())}
245+
vertices_0 = {key: xyz[:] for key, xyz in iter(vertices.items())}
240246

241247
for key, point in iter(vertices_0.items()):
242248
if key in fixed:
@@ -341,7 +347,7 @@ def smooth_resultant(vertices,
341347
fixed = set(fixed)
342348

343349
for k in range(kmax):
344-
vertices_0 = {key: xyz for key, xyz in iter(vertices.items())}
350+
vertices_0 = {key: xyz[:] for key, xyz in iter(vertices.items())}
345351

346352
for key, point in iter(vertices_0.items()):
347353
if key in fixed:
@@ -453,7 +459,7 @@ def smooth_area(vertices,
453459
fixed = set(fixed)
454460

455461
for k in range(kmax):
456-
vertices_0 = {key: point for key, point in iter(vertices.items())}
462+
vertices_0 = {key: point[:] for key, point in iter(vertices.items())}
457463

458464
face_centroid = {fkey: centroid_points([vertices[key] for key in keys]) for fkey, keys in iter(faces.items())}
459465
face_area = {fkey: area_polygon([vertices[key] for key in keys]) for fkey, keys in iter(faces.items())}
@@ -742,17 +748,24 @@ def network_smooth_resultant(network, fixed=None, kmax=100, damping=0.05, callba
742748

743749
if __name__ == "__main__":
744750

751+
import os
752+
import sys
753+
745754
import compas
746755

747756
from compas.datastructures import Mesh
748757
from compas.plotters import MeshPlotter
749758

750759
mesh = Mesh.from_obj(compas.get('faces.obj'))
751760

761+
edges = list(mesh.edges())
762+
752763
fixed = [key for key in mesh.vertices() if mesh.vertex_degree(key) == 2]
753764

754765
plotter = MeshPlotter(mesh, figsize=(10, 7))
755766

767+
images = []
768+
756769
lines = []
757770
for u, v in mesh.edges():
758771
lines.append({
@@ -763,20 +776,32 @@ def network_smooth_resultant(network, fixed=None, kmax=100, damping=0.05, callba
763776
})
764777
plotter.draw_lines(lines)
765778

766-
plotter.draw_vertices(facecolor={key: '#ff0000' for key in fixed})
779+
plotter.draw_vertices(facecolor={key: '#ff0000' for key in fixed}, text={key: key for key in fixed})
767780
plotter.draw_faces()
768781
plotter.draw_edges()
769782

770783
plotter.update(pause=1.0)
771784

785+
# filepath = os.path.join(compas.TEMP, 'image_{}.jpg'.format(0))
786+
# plotter.save(filepath)
787+
# images.append(filepath)
788+
772789
def callback(mesh, k, args):
773790
print(k)
774791
plotter.update_vertices()
775792
plotter.update_faces()
776793
plotter.update_edges()
777794
plotter.update(pause=0.001)
778795

796+
# filepath = os.path.join(compas.TEMP, 'image_{}.jpg'.format(k + 1))
797+
# plotter.save(filepath)
798+
# images.append(filepath)
799+
779800
mesh_smooth_centroid(mesh, kmax=50, fixed=fixed, callback=callback)
780801

781-
plotter.update(pause=1.0)
802+
# plotter.saveas_gif(os.path.join(compas.TEMP, 'smoothing.gif'), images)
803+
804+
for filepath in images:
805+
os.remove(filepath)
806+
782807
plotter.show()

src/compas/hpc/__init__.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,45 +70,61 @@
7070
circle_from_points_xy_numba
7171
7272
73-
solvers
73+
core
7474
====
7575
76+
cuda
77+
----
78+
7679
.. autosummary::
7780
:toctree: generated/
7881
79-
devo_numba
8082
83+
euler
84+
-----
8185
82-
core
83-
=====
86+
.. autosummary::
87+
:toctree: generated/
88+
89+
euler.connect_to_euler
90+
euler.load_euler_module
91+
euler.recieve_file_from_euler
92+
euler.send_file_to_euler
93+
euler.send_folder_to_euler
94+
euler.show_euler_jobs
95+
euler.show_euler_quotas
96+
euler.show_euler_modules
97+
euler.show_euler_module_info
98+
euler.show_euler_resources
99+
euler.submit_job
100+
euler.kill_job
101+
euler.sync_folder_to_euler
102+
103+
numba
104+
-----
84105
85106
.. autosummary::
86107
:toctree: generated/
87108
88-
connect_to_euler
89-
load_euler_module
90-
recieve_file_from_euler
91-
send_file_to_euler
92-
send_folder_to_euler
93-
show_euler_jobs
94-
show_euler_quotas
95-
show_euler_modules
96-
show_euler_module_info
97-
show_euler_resources
98-
submit_job
99-
kill_job
100-
sync_folder_to_euler
109+
numba.numba_cross
110+
numba.numba_vdot
111+
numba.numba_dot
112+
numba.numba_length
113+
numba.numba_subtract
101114
102-
"""
103115
116+
opencl
117+
------
118+
119+
.. autosummary::
120+
:toctree: generated/
121+
122+
123+
"""
104124
from .geometry import *
105-
from .core import *
106125
from .algorithms import *
107-
from .solvers import *
108126

109127
from .geometry import __all__ as a
110-
from .core import __all__ as b
111-
from .algorithms import __all__ as c
112-
from .solvers import __all__ as d
128+
from .algorithms import __all__ as b
113129

114-
__all__ = a + b + c + d
130+
__all__ = a + b
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
5+
from numba import jit
6+
7+
from numpy import array
8+
from numpy import argmin
9+
from numpy import min
10+
from numpy import random
11+
from numpy import zeros
12+
13+
14+
__author__ = ['Andrew Liew <[email protected]>']
15+
__copyright__ = 'Copyright 2017, BLOCK Research Group - ETH Zurich'
16+
__license__ = 'MIT License'
17+
__email__ = '[email protected]'
18+
19+
20+
__all__ = [
21+
'devo_numba'
22+
]
23+
24+
25+
args = 0
26+
27+
28+
@jit(nogil=True, nopython=True)
29+
def devo_numba(bounds, population, iterations):
30+
""" Call the Numba accelerated differential evolution solver.
31+
32+
Parameters:
33+
bounds (array): Lower and upper bounds for each DoF.
34+
population (int): Number of agents in the population.
35+
iterations (int): Number of cross-over cycles or steps to perform.
36+
37+
Returns:
38+
array: Values that give the optimum (minimised) function.
39+
"""
40+
41+
F = 0.8
42+
CR = 0.9
43+
44+
# Heading
45+
46+
print('\n---------------------------------')
47+
print('Differential Evolution started...')
48+
print('---------------------------------')
49+
50+
# Setup population
51+
52+
k = bounds.shape[0]
53+
agents = random.rand(k, population)
54+
for i in range(k):
55+
for j in range(population):
56+
agents[i, j] *= bounds[i, 1] - bounds[i, 0]
57+
agents[i, j] += bounds[i, 0]
58+
candidates = zeros((population, population - 1))
59+
for i in range(population):
60+
c = 0
61+
for j in range(population - 1):
62+
if j == i:
63+
c += 1
64+
candidates[i, j] = c
65+
c += 1
66+
ts = 0
67+
fun = zeros(population)
68+
for i in range(population):
69+
fun[i] = _fn(agents[:, i], args)
70+
fopt = min(fun)
71+
72+
# print(' ')
73+
# print('Generation: ', ts, ' fopt: ', fopt)
74+
75+
# Start evolution
76+
77+
agents_ = zeros((k, population))
78+
while ts < iterations:
79+
ind = random.rand(k, population) < CR
80+
for i in range(population):
81+
rands = candidates[i, :]
82+
random.shuffle(rands)
83+
ind0 = int(candidates[i, int(rands[0])])
84+
ind1 = int(candidates[i, int(rands[1])])
85+
ind2 = int(candidates[i, int(rands[2])])
86+
ac = agents[:, ind0]
87+
bc = agents[:, ind1]
88+
cc = agents[:, ind2]
89+
for j in range(k):
90+
if ind[j, i]:
91+
agents_[j, i] = ac[j] + F * (bc[j] - cc[j])
92+
fun_ = _fn(agents_[:, i], args)
93+
if fun_ < fun[i]:
94+
agents[:, i] = agents_[:, i]
95+
fun[i] = fun_
96+
fopt = min(fun)
97+
xopt = agents[:, argmin(fun)]
98+
ts += 1
99+
100+
# print('Generation: ', ts, ' fopt: ', fopt)
101+
102+
# Summary
103+
104+
# print('\n-------------------------------')
105+
# print('Differential Evolution finished')
106+
# print('fopt: ', fopt)
107+
# print('-------------------------------')
108+
109+
return xopt
110+
111+
112+
@jit(nogil=True, nopython=True)
113+
def _fn(u, args):
114+
# Booth's function, fopt=0, uopt=(1, 3)
115+
x = u[0]
116+
y = u[1]
117+
z = (x + 2 * y - 7)**2 + (2 * x + y - 5)**2
118+
return z
119+
120+
121+
# ==============================================================================
122+
# Testing
123+
# ==============================================================================
124+
125+
if __name__ == "__main__":
126+
127+
bounds = array([[-10., 10.], [-10., 10.]])
128+
xopt = devo_numba(bounds, population=20, iterations=100)
129+
print(xopt)

src/compas/hpc/core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .euler_ import *
1+
from .euler import *
22

3-
from .euler_ import __all__ as a
3+
from .euler import __all__ as a
44

55
__all__ = a
File renamed without changes.

src/compas/hpc/core/cuda_/_array.py renamed to src/compas/hpc/core/cuda/array.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
15
from numpy import array
26
from numpy import float32
37
from numpy import float64

src/compas/hpc/core/cuda_/_linalg.py renamed to src/compas/hpc/core/cuda/linalg.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
15
from numpy import float64
26
from numpy import ceil
37

4-
from compas.hpc.cuda._math import cuda_sqrt
5-
from compas.hpc.cuda._math import cuda_sum
8+
from compas.hpc.cuda.math import cuda_sqrt
9+
from compas.hpc.cuda.math import cuda_sum
610

711
try:
812
import pycuda

src/compas/hpc/core/cuda_/_math.py renamed to src/compas/hpc/core/cuda/math.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
15
try:
26
import pycuda
37
import pycuda.autoinit

0 commit comments

Comments
 (0)