|
| 1 | +import compas |
| 2 | +from compas.datastructures import mesh_subdivide |
| 3 | +from compas_fd.datastructures import CableMesh |
| 4 | +from compas_fd.fd import fd_cpp, fd_numpy |
| 5 | +from compas_view2.app import App |
| 6 | +from compas_view2.objects import Object, MeshObject |
| 7 | +from timeit import timeit |
| 8 | + |
| 9 | +Object.register(CableMesh, MeshObject) |
| 10 | + |
| 11 | + |
| 12 | +# ================================================= |
| 13 | +# input mesh |
| 14 | +# ================================================= |
| 15 | + |
| 16 | +mesh = CableMesh.from_obj(compas.get('faces.obj')) |
| 17 | + |
| 18 | +mesh.vertices_attribute('is_anchor', True, mesh.vertices_where({'vertex_degree': 2})) |
| 19 | +dva = {'rx': .0, 'ry': .0, 'rz': .0, |
| 20 | + 'px': .0, 'py': .0, 'pz': -.0, |
| 21 | + 'is_anchor': False} |
| 22 | + |
| 23 | +mesh = mesh_subdivide(mesh, scheme='quad', k=1) |
| 24 | +mesh.update_default_vertex_attributes(dva) |
| 25 | + |
| 26 | + |
| 27 | +# ================================================= |
| 28 | +# pre-process mesh data |
| 29 | +# ================================================= |
| 30 | + |
| 31 | +vertices = [mesh.vertex_coordinates(v) for v in mesh.vertices()] |
| 32 | +fixed = list(mesh.vertices_where({'is_anchor': True})) |
| 33 | +edges = list(mesh.edges()) |
| 34 | +force_densities = mesh.edges_attribute('q') |
| 35 | +loads = mesh.vertices_attributes(['px', 'py', 'pz']) |
| 36 | + |
| 37 | + |
| 38 | +# ================================================= |
| 39 | +# solvers |
| 40 | +# ================================================= |
| 41 | + |
| 42 | +result_eigen = fd_cpp(vertices=vertices, fixed=fixed, edges=edges, |
| 43 | + force_densities=force_densities, loads=loads) |
| 44 | + |
| 45 | +result_np = fd_numpy(vertices=vertices, fixed=fixed, edges=edges, |
| 46 | + forcedensities=force_densities, loads=loads) |
| 47 | + |
| 48 | +forces_comparison = ((abs(l1 - l2) < 1E-14) for l1, l2 |
| 49 | + in zip(result_eigen.forces, result_np.forces)) |
| 50 | +print("Check if forces are equal between solvers: ", all(forces_comparison)) |
| 51 | + |
| 52 | + |
| 53 | +for vertex, coo in zip(mesh.vertices(), result_eigen.vertices): |
| 54 | + mesh.vertex_attributes(vertex, 'xyz', coo) |
| 55 | + |
| 56 | + |
| 57 | +# ================================================= |
| 58 | +# benchmarks |
| 59 | +# ================================================= |
| 60 | + |
| 61 | +def fn_cpp(): |
| 62 | + fd_cpp(vertices=vertices, fixed=fixed, edges=edges, |
| 63 | + force_densities=force_densities, loads=loads) |
| 64 | + |
| 65 | + |
| 66 | +def fn_numpy(): |
| 67 | + fd_numpy(vertices=vertices, fixed=fixed, edges=edges, |
| 68 | + forcedensities=force_densities, loads=loads) |
| 69 | + |
| 70 | + |
| 71 | +nr = 30 |
| 72 | +print(f"Solver time in cpp: {round(1000 * timeit(fn_cpp, number=nr) / nr, 2)} ms") |
| 73 | +print(f"Solver time in numpy: {round(1000 * timeit(fn_numpy, number=nr) / nr, 2)} ms") |
| 74 | + |
| 75 | + |
| 76 | +# ================================================= |
| 77 | +# viz |
| 78 | +# ================================================= |
| 79 | + |
| 80 | +viewer = App() |
| 81 | +viewer.add(mesh) |
| 82 | +viewer.run() |
0 commit comments