pytopo3d 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. pytopo3d-0.1.0/LICENSE +21 -0
  2. pytopo3d-0.1.0/MANIFEST.in +20 -0
  3. pytopo3d-0.1.0/PKG-INFO +369 -0
  4. pytopo3d-0.1.0/README.md +327 -0
  5. pytopo3d-0.1.0/environment.yml +22 -0
  6. pytopo3d-0.1.0/examples/obstacles_config_cylinder.json +15 -0
  7. pytopo3d-0.1.0/pyproject.toml +50 -0
  8. pytopo3d-0.1.0/pytopo3d/__init__.py +0 -0
  9. pytopo3d-0.1.0/pytopo3d/cli/__init__.py +3 -0
  10. pytopo3d-0.1.0/pytopo3d/cli/command.py +207 -0
  11. pytopo3d-0.1.0/pytopo3d/cli/parser.py +235 -0
  12. pytopo3d-0.1.0/pytopo3d/core/__init__.py +0 -0
  13. pytopo3d-0.1.0/pytopo3d/core/compliance.py +32 -0
  14. pytopo3d-0.1.0/pytopo3d/core/optimizer.py +256 -0
  15. pytopo3d-0.1.0/pytopo3d/preprocessing/__init__.py +3 -0
  16. pytopo3d-0.1.0/pytopo3d/preprocessing/geometry.py +213 -0
  17. pytopo3d-0.1.0/pytopo3d/runners/__init__.py +3 -0
  18. pytopo3d-0.1.0/pytopo3d/runners/experiment.py +227 -0
  19. pytopo3d-0.1.0/pytopo3d/runners/pipeline.py +140 -0
  20. pytopo3d-0.1.0/pytopo3d/utils/__init__.py +3 -0
  21. pytopo3d-0.1.0/pytopo3d/utils/assembly.py +108 -0
  22. pytopo3d-0.1.0/pytopo3d/utils/boundary.py +63 -0
  23. pytopo3d-0.1.0/pytopo3d/utils/export.py +133 -0
  24. pytopo3d-0.1.0/pytopo3d/utils/filter.py +145 -0
  25. pytopo3d-0.1.0/pytopo3d/utils/import_design_space.py +109 -0
  26. pytopo3d-0.1.0/pytopo3d/utils/logger.py +161 -0
  27. pytopo3d-0.1.0/pytopo3d/utils/metrics.py +169 -0
  28. pytopo3d-0.1.0/pytopo3d/utils/obstacles.py +269 -0
  29. pytopo3d-0.1.0/pytopo3d/utils/oc_update.py +88 -0
  30. pytopo3d-0.1.0/pytopo3d/utils/results_manager.py +334 -0
  31. pytopo3d-0.1.0/pytopo3d/utils/solver.py +28 -0
  32. pytopo3d-0.1.0/pytopo3d/utils/stiffness.py +104 -0
  33. pytopo3d-0.1.0/pytopo3d/visualization/__init__.py +3 -0
  34. pytopo3d-0.1.0/pytopo3d/visualization/animation.py +278 -0
  35. pytopo3d-0.1.0/pytopo3d/visualization/display.py +325 -0
  36. pytopo3d-0.1.0/pytopo3d/visualization/runner.py +133 -0
  37. pytopo3d-0.1.0/pytopo3d/visualization/visualizer.py +255 -0
  38. pytopo3d-0.1.0/pytopo3d.egg-info/SOURCES.txt +37 -0
  39. pytopo3d-0.1.0/setup.cfg +4 -0
  40. pytopo3d-0.1.0/setup.py +47 -0
pytopo3d-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Jihoon Kim
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ include LICENSE
2
+ include README.md
3
+ include MANIFEST.in
4
+ include environment.yml
5
+ include pyproject.toml
6
+ include setup.py
7
+
8
+ recursive-include examples *
9
+
10
+ recursive-exclude assets *
11
+ recursive-exclude __pycache__ *
12
+ recursive-exclude .git *
13
+ recursive-exclude .venv *
14
+ recursive-exclude results *
15
+ recursive-exclude .cursor *
16
+ recursive-exclude .pytest_cache *
17
+ recursive-exclude *.egg-info *
18
+ recursive-exclude *.eggs *
19
+ recursive-exclude build *
20
+ recursive-exclude dist *
@@ -0,0 +1,369 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytopo3d
3
+ Version: 0.1.0
4
+ Summary: 3D SIMP Topology Optimization Framework for Python
5
+ Home-page: https://github.com/jihoonkim888/PyTopo3D
6
+ Author: Jihoon Kim, Namwoo Kang
7
+ Author-email: Jihoon Kim <jihoonkim888@example.com>, Namwoo Kang <example@example.com>
8
+ License: MIT
9
+ Project-URL: Homepage, https://github.com/jihoonkim888/PyTopo3D
10
+ Project-URL: Bug Tracker, https://github.com/jihoonkim888/PyTopo3D/issues
11
+ Project-URL: Documentation, https://github.com/jihoonkim888/PyTopo3D
12
+ Project-URL: Source Code, https://github.com/jihoonkim888/PyTopo3D
13
+ Keywords: topology optimization,SIMP,structural optimization,3D,finite element
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: numpy
24
+ Requires-Dist: pandas
25
+ Requires-Dist: matplotlib
26
+ Requires-Dist: scipy
27
+ Requires-Dist: scikit-image
28
+ Requires-Dist: networkx
29
+ Requires-Dist: pillow
30
+ Requires-Dist: plotly
31
+ Requires-Dist: tqdm
32
+ Requires-Dist: trimesh
33
+ Requires-Dist: psutil
34
+ Requires-Dist: pypardiso
35
+ Provides-Extra: dev
36
+ Requires-Dist: jupyter; extra == "dev"
37
+ Requires-Dist: jupyterlab; extra == "dev"
38
+ Dynamic: author
39
+ Dynamic: home-page
40
+ Dynamic: license-file
41
+ Dynamic: requires-python
42
+
43
+ # PyTopo3D: 3D SIMP Topology Optimization Framework for Python
44
+
45
+ ![Design optimization with boundary conditions](assets/optimization_animation.gif)
46
+
47
+ A comprehensive Python implementation of 3D Topology Optimization based on SIMP (Solid Isotropic Material with Penalization) method. Unlike traditional MATLAB implementations, PyTopo3D brings the power of 3D SIMP-based optimization to the Python ecosystem with support for obstacle regions.
48
+
49
+ ## Table of Contents
50
+ - [PyTopo3D: 3D SIMP Topology Optimization Framework for Python](#pytopo3d-3d-simp-topology-optimization-framework-for-python)
51
+ - [Table of Contents](#table-of-contents)
52
+ - [Overview](#overview)
53
+ - [Installation](#installation)
54
+ - [Basic Usage](#basic-usage)
55
+ - [Configuration Parameters](#configuration-parameters)
56
+ - [Command-line Interface](#command-line-interface)
57
+ - [Python Package API](#python-package-api)
58
+ - [Advanced Features](#advanced-features)
59
+ - [Obstacle Configuration](#obstacle-configuration)
60
+ - [Command Line Usage](#command-line-usage)
61
+ - [Python API Usage](#python-api-usage)
62
+ - [Obstacle Configuration Format](#obstacle-configuration-format)
63
+ - [Design Space Customization](#design-space-customization)
64
+ - [Command Line Usage](#command-line-usage-1)
65
+ - [Python API Usage](#python-api-usage-1)
66
+ - [Understanding Pitch and Resolution](#understanding-pitch-and-resolution)
67
+ - [Exporting Results](#exporting-results)
68
+ - [Command Line Usage](#command-line-usage-2)
69
+ - [Animation Generation](#animation-generation)
70
+ - [Experiment Management](#experiment-management)
71
+ - [Command Line Options](#command-line-options)
72
+ - [Acknowledgements](#acknowledgements)
73
+ - [Citation](#citation)
74
+
75
+ ## Overview
76
+
77
+ This code performs 3D structural topology optimization using the SIMP (Solid Isotropic Material with Penalization) method. It is designed to be efficient by utilizing:
78
+
79
+ - Parallel solver (PyPardiso if available, otherwise SciPy's spsolve)
80
+ - Precomputed assembly mapping for fast matrix assembly
81
+ - Support for obstacle regions where no material can be placed
82
+ - Flexible obstacle configuration via JSON files
83
+ - Advanced visualization capabilities including animation generation
84
+
85
+ ## Installation
86
+
87
+ 1. Clone this repository:
88
+ ```bash
89
+ git clone https://github.com/jihoonkim888/PyTopo3D.git
90
+ cd PyTopo3D
91
+ ```
92
+
93
+ 2. Create and activate the conda environment:
94
+ ```bash
95
+ # Create the environment from the environment.yml file
96
+ conda env create -f environment.yml
97
+
98
+ # Activate the environment
99
+ conda activate pytopo3d
100
+ ```
101
+
102
+ ## Basic Usage
103
+
104
+ ### Configuration Parameters
105
+
106
+ The main optimization parameters are:
107
+
108
+ - `nelx`, `nely`, `nelz`: Number of elements in x, y, z directions (default: 60, 30, 20)
109
+ - `volfrac`: Volume fraction constraint (0.0-1.0) (default: 0.3)
110
+ - `penal`: Penalization power for SIMP method (default: 3.0)
111
+ - `rmin`: Filter radius for sensitivity filtering (default: 3.0)
112
+ - `disp_thres`: Display threshold for 3D visualization (elements with density > disp_thres are shown) (default: 0.5)
113
+ - `tolx`: Convergence tolerance on design change (default: 0.01)
114
+ - `maxloop`: Maximum number of iterations (default: 2000)
115
+
116
+ ### Command-line Interface
117
+
118
+ To run a basic optimization:
119
+
120
+ ```bash
121
+ python main.py --nelx 32 --nely 16 --nelz 16 --volfrac 0.3 --penal 3.0 --rmin 3.0
122
+ ```
123
+
124
+ For full options:
125
+
126
+ ```bash
127
+ python main.py --help
128
+ ```
129
+
130
+ ### Python Package API
131
+
132
+ ```python
133
+ import numpy as np
134
+ from pytopo3d.core.optimizer import top3d
135
+
136
+ # Define parameters
137
+ nelx, nely, nelz = 32, 16, 16
138
+ volfrac = 0.3
139
+ penal = 3.0
140
+ rmin = 3.0
141
+ disp_thres = 0.5
142
+
143
+ # Run optimization
144
+ result = top3d(nelx, nely, nelz, volfrac, penal, rmin, disp_thres)
145
+
146
+ # Save result
147
+ np.save("optimized_design.npy", result)
148
+ ```
149
+
150
+ ## Advanced Features
151
+
152
+ ### Obstacle Configuration
153
+
154
+ PyTopo3D supports defining regions where material should not be placed during optimization.
155
+
156
+ #### Command Line Usage
157
+
158
+ ```bash
159
+ python main.py --obstacle-config examples/obstacles_config_cylinder.json
160
+ ```
161
+
162
+ #### Python API Usage
163
+
164
+ ```python
165
+ # Create a custom obstacle mask
166
+ obstacle_mask = np.zeros((nely, nelx, nelz), dtype=bool)
167
+ obstacle_mask[5:15, 3:7, 3:7] = True # Example obstacle
168
+
169
+ # Or load obstacles from config file
170
+ from pytopo3d.preprocessing.geometry import load_geometry_data
171
+
172
+ design_space_mask, obstacle_mask, combined_obstacle_mask = load_geometry_data(
173
+ nelx=nelx,
174
+ nely=nely,
175
+ nelz=nelz,
176
+ obstacle_config="path/to/config.json"
177
+ )
178
+
179
+ # Use the mask in optimization
180
+ result = top3d(nelx, nely, nelz, volfrac, penal, rmin, disp_thres,
181
+ obstacle_mask=combined_obstacle_mask)
182
+ ```
183
+
184
+ #### Obstacle Configuration Format
185
+
186
+ The obstacle configuration file is a JSON file with the following structure:
187
+
188
+ ```json
189
+ {
190
+ "obstacles": [
191
+ {
192
+ "type": "cube",
193
+ "center": [0.5, 0.5, 0.2], // x, y, z as fractions [0-1]
194
+ "size": 0.15 // single value for a cube
195
+ },
196
+ {
197
+ "type": "sphere",
198
+ "center": [0.25, 0.25, 0.6],
199
+ "radius": 0.1
200
+ },
201
+ {
202
+ "type": "cylinder",
203
+ "center": [0.75, 0.5, 0.5],
204
+ "radius": 0.08,
205
+ "height": 0.7,
206
+ "axis": 2 // 0=x, 1=y, 2=z
207
+ },
208
+ {
209
+ "type": "cube",
210
+ "center": [0.25, 0.75, 0.5],
211
+ "size": [0.15, 0.05, 0.3] // [x, y, z] for a cuboid
212
+ }
213
+ ]
214
+ }
215
+ ```
216
+
217
+ Supported obstacle types:
218
+ - `cube`: A cube or cuboid. Use `size` as a single value for a cube, or as `[x, y, z]` for a cuboid.
219
+ - `sphere`: A sphere. Use `radius` to set the size.
220
+ - `cylinder`: A cylinder. Use `radius`, `height`, and `axis` (0=x, 1=y, 2=z) to configure.
221
+
222
+ All positions are specified as fractions [0-1] of the domain size, making it easy to reuse configurations across different mesh resolutions.
223
+
224
+ ### Design Space Customization
225
+
226
+ PyTopo3D allows using STL files to define the design space geometry, enabling complex shapes beyond the standard rectangular domain.
227
+
228
+ #### Command Line Usage
229
+
230
+ ```bash
231
+ python main.py --design-space-stl path/to/design_space.stl --pitch 0.5
232
+ ```
233
+
234
+ Command line options:
235
+ - `--design-space-stl`: Path to an STL file defining the design space geometry
236
+ - `--pitch`: Distance between voxel centers when voxelizing STL (default: 1.0, smaller values create finer detail)
237
+ - `--invert-design-space`: Flag to invert the design space (treat STL as void space rather than design space)
238
+
239
+ #### Python API Usage
240
+
241
+ ```python
242
+ from pytopo3d.preprocessing.geometry import load_geometry_data
243
+ import numpy as np
244
+
245
+ # Load design space from STL
246
+ design_space_mask, obstacle_mask, combined_obstacle_mask = load_geometry_data(
247
+ nelx=60,
248
+ nely=30,
249
+ nelz=20,
250
+ design_space_stl="path/to/design_space.stl",
251
+ pitch=0.5,
252
+ invert_design_space=False
253
+ )
254
+
255
+ # The shape of the mask is determined by the STL geometry and pitch
256
+ nely, nelx, nelz = design_space_mask.shape
257
+ print(f"Resolution from voxelization: {nely}x{nelx}x{nelz}")
258
+
259
+ # Use the mask in optimization
260
+ from pytopo3d.core.optimizer import top3d
261
+
262
+ result = top3d(
263
+ nelx=nelx,
264
+ nely=nely,
265
+ nelz=nelz,
266
+ volfrac=0.3,
267
+ penal=3.0,
268
+ rmin=3.0,
269
+ disp_thres=0.5,
270
+ obstacle_mask=combined_obstacle_mask
271
+ )
272
+ ```
273
+
274
+ #### Understanding Pitch and Resolution
275
+
276
+ The `pitch` parameter directly controls the resolution of the voxelized model:
277
+
278
+ - Smaller pitch values create higher resolution voxelizations
279
+ - The number of voxels along any dimension = physical length ÷ pitch
280
+ - Choose pitch value based on the level of detail needed and computational resources available
281
+
282
+ ### Exporting Results
283
+
284
+ You can export the final optimization result as an STL file for 3D printing or further analysis in CAD software.
285
+
286
+ #### Command Line Usage
287
+
288
+ ```bash
289
+ python main.py --nelx 32 --nely 16 --nelz 16 \
290
+ --volfrac 0.3 --penal 3.0 --rmin 3.0 \
291
+ --export-stl \
292
+ [--stl-level 0.5] \
293
+ [--smooth-stl] \
294
+ [--smooth-iterations 5]
295
+ ```
296
+
297
+ Options:
298
+ - `--export-stl`: Flag to enable STL export of the final optimization result
299
+ - `--stl-level`: Contour level for the marching cubes algorithm (default: 0.5)
300
+ - `--smooth-stl`: Flag to apply Laplacian smoothing to the mesh (default: True)
301
+ - `--smooth-iterations`: Number of iterations for mesh smoothing (default: 5)
302
+
303
+ ### Animation Generation
304
+
305
+ PyTopo3D can generate animations of the optimization process.
306
+
307
+ ```bash
308
+ python main.py --nelx 32 --nely 16 --nelz 16 \
309
+ --create-animation \
310
+ --animation-frequency 10 \
311
+ --animation-frames 50 \
312
+ --animation-fps 5
313
+ ```
314
+
315
+ Options:
316
+ - `--create-animation`: Flag to enable animation generation
317
+ - `--animation-frequency`: Store every N iterations for the animation (default: 10)
318
+ - `--animation-frames`: Target number of frames in the final animation (default: 50)
319
+ - `--animation-fps`: Frames per second in the generated animation (default: 5)
320
+
321
+ ### Experiment Management
322
+
323
+ PyTopo3D includes a robust experiment management system that automatically:
324
+
325
+ - Creates a uniquely named directory for each optimization run
326
+ - Saves all relevant parameters, inputs, and outputs
327
+ - Generates detailed logs of the optimization process
328
+ - Records performance metrics and convergence data
329
+
330
+ #### Command Line Options
331
+
332
+ ```bash
333
+ python main.py --experiment-name custom_name \
334
+ --description "Detailed description of this experiment" \
335
+ --log-level DEBUG \
336
+ --log-file custom_log.log \
337
+ --verbose
338
+ ```
339
+
340
+ Options:
341
+ - `--experiment-name`: Custom name for the experiment (optional, auto-generated if not provided)
342
+ - `--description`: Description of the experiment stored in the results metadata
343
+ - `--log-level`: Set logging detail level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
344
+ - `--log-file`: Path to a custom log file
345
+ - `--verbose`: Enable more detailed output (sets log level to DEBUG)
346
+ - `--quiet`: Reduce output verbosity (sets log level to WARNING)
347
+
348
+ ## Acknowledgements
349
+
350
+ This code is adapted from [Liu & Tovar's MATLAB code](https://www.top3d.app/) for 3D topology optimization.
351
+
352
+ > K. Liu and A. Tovar, "An efficient 3D topology optimization code written in Matlab", Struct Multidisc Optim, 50(6): 1175-1196, 2014, doi:10.1007/s00158-014-1107-x
353
+
354
+ ## Citation
355
+
356
+ If you use PyTopo3D in your research or work, please cite our paper on ArXiv: [PyTopo3D: A Python Framework for 3D SIMP-based Topology Optimization](https://arxiv.org/abs/2504.05604)
357
+
358
+ > Kim, J. & Kang, N. (2024). PyTopo3D: A Python Framework for 3D SIMP-based Topology Optimization. arXiv preprint arXiv:2504.05604.
359
+
360
+ ```bibtex
361
+ @article{kim2025pytopo3d
362
+ title={PyTopo3D: A Python Framework for 3D SIMP-based Topology Optimization},
363
+ author={Jihoon Kim and Namwoo Kang},
364
+ journal={arXiv preprint arXiv:2504.05604},
365
+ year={2025}
366
+ }
367
+ ```
368
+
369
+ This paper provides a detailed explanation of the implementation, theoretical foundations, and optimizations used in PyTopo3D. Proper citation helps support the continued development of open-source scientific software.