|
| 1 | +.. _workflows-stl-geometry-preparation: |
| 2 | + |
| 3 | +STL Geometry Preparation for WarpX |
| 4 | +================================== |
| 5 | + |
| 6 | +WarpX has the ability to define the embedded boundary (EB) in a simulation using externally provided STL (STereoLithography) files. |
| 7 | +STL files are a common format for representing 3D geometries and can be used to define complex embedded boundaries in WarpX simulations, without having to define extensive EB implicit functions. |
| 8 | +However, there are some limitations to using STL files which must be resolved, including: |
| 9 | + |
| 10 | +1. Multiple STL files representing different parts of a device must be combined into a single file |
| 11 | +2. The STL geometry is "watertight" (The STL file shouldn't have duplicate faces or vertices, overlapping vertices should be merged, and there shouldn't be any gaps or holes in the mesh) |
| 12 | +3. The STL file is properly formatted for WarpX input |
| 13 | + |
| 14 | +.. 2. The STL file shouldn't have duplicate faces or vertices, and overlapping vertices should be merged. (issues with vertices can cause the MLMG solver to crash) |
| 15 | +
|
| 16 | +Step 1: Combining Multiple STL Files |
| 17 | +------------------------------------ |
| 18 | + |
| 19 | +If your device consists of multiple parts stored in separate STL files, you'll need to combine them into a single STL file. |
| 20 | + |
| 21 | +Using MeshLab |
| 22 | +^^^^^^^^^^^^^ |
| 23 | + |
| 24 | +`MeshLab <https://www.meshlab.net/>`__ is a free, open-source tool for processing 3D meshes: |
| 25 | + |
| 26 | +1. Open MeshLab |
| 27 | +2. Import the first STL file: ``File -> Import Mesh`` |
| 28 | +3. Import additional STL files: ``File -> Import Mesh`` (repeat for each file) |
| 29 | +4. All parts should now be visible in the same scene |
| 30 | +5. Create a union of the two parts, either by ``Filters -> Remeshing, Simplification and Reconstruction -> Mesh Boolean: Union`` |
| 31 | + or by right-clicking on one of the parts in the Layer Dialog panel on the right and clicking ``Mesh Boolean: Union`` |
| 32 | +6. Select the union and export the combined mesh: ``File -> Export Mesh As`` |
| 33 | +7. Choose STL format and save |
| 34 | + |
| 35 | +Using Python with trimesh |
| 36 | +^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 37 | + |
| 38 | +You can also use Python with the `trimesh <https://github.com/mikedh/trimesh>`__ library: |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | +
|
| 42 | + import trimesh |
| 43 | +
|
| 44 | + # Load individual STL files |
| 45 | + mesh1 = trimesh.load('part1.stl') |
| 46 | + mesh2 = trimesh.load('part2.stl') |
| 47 | +
|
| 48 | + # Combine meshes |
| 49 | + combined = trimesh.util.concatenate([mesh1, mesh2]) |
| 50 | +
|
| 51 | + # Export combined mesh |
| 52 | + combined.export('device_combined.stl') |
| 53 | +
|
| 54 | +Step 2: Making the Geometry Watertight and Removing and Merging Duplicate Faces and Vertices |
| 55 | +--------------------------------------------------------------------------------------------------- |
| 56 | + |
| 57 | + |
| 58 | +WarpX requires that STL geometries be "watertight" (manifold), meaning the mesh has no holes, gaps, or open edges. |
| 59 | +This ensures that WarpX can properly determine which regions are inside or outside the geometry. |
| 60 | +STL files that have overlapping or duplicate faces and vertices can cause the MLMG solver to crash, while a non-manifold file can be inherently leaky for Poynting flux in an EM simulation. |
| 61 | +These issues with the mesh might happen while exporting the STL file from a CAD program (such as SolidWorks) |
| 62 | + |
| 63 | +Using MeshLab |
| 64 | +^^^^^^^^^^^^^ |
| 65 | + |
| 66 | +To check and repair a mesh in MeshLab: |
| 67 | + |
| 68 | +1. **Check for issues:** |
| 69 | + |
| 70 | + * ``Filters -> Quality Measure and Computations -> Compute Topological Measures`` |
| 71 | + * Look for holes in the output |
| 72 | + * Look for non-manifold edges and vertices in the output |
| 73 | + |
| 74 | +2. **Repair the mesh:** |
| 75 | + |
| 76 | + * When loading in the mesh, there is an option to ``Unify Duplicated Vertices in STL files``, this will perform the next steps automatically, but gives the user less control. |
| 77 | + * ``Filters -> Cleaning and Repairing -> Remove Duplicate Faces`` |
| 78 | + * ``Filters -> Cleaning and Repairing -> Remove Duplicate Vertices`` |
| 79 | + * ``Filters -> Cleaning and Repairing -> Merge Close Vertices`` |
| 80 | + |
| 81 | + * This filter gives the user an option to set an absolute or relative tolerance on the distance between the vertices to merge. |
| 82 | + |
| 83 | + .. * ``Filters -> Cleaning and Repairing -> Remove Zero Area Faces`` |
| 84 | + .. * ``Filters -> Remeshing, Simplification and Reconstruction -> Close Holes`` |
| 85 | +
|
| 86 | +3. **Verify the mesh is watertight:** |
| 87 | + |
| 88 | + * Run ``Filters -> Quality Measure and Computations -> Compute Topological Measures`` again |
| 89 | + * Check that the mesh has no holes |
| 90 | + * Check that the mesh is manifold (no warnings about non-manifold edges) |
| 91 | + |
| 92 | + * An STL file with non-manifold edges won't necessarily crash a simulation, but may present other numerical issues. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +Step 3: Using STL Files in WarpX |
| 98 | +-------------------------------- |
| 99 | + |
| 100 | +Once you have a single, watertight STL file, you can use it in WarpX by setting the following parameters in your input file: |
| 101 | + |
| 102 | +.. code-block:: text |
| 103 | +
|
| 104 | + # Specify that the embedded boundary comes from an STL file |
| 105 | + eb2.geom_type = stl |
| 106 | +
|
| 107 | + # Path to your STL file |
| 108 | + eb2.stl_file = path/to/device_watertight.stl |
| 109 | +
|
| 110 | +You can also optionally set an electric potential on the embedded boundary: |
| 111 | + |
| 112 | +.. code-block:: text |
| 113 | +
|
| 114 | + # Define electric potential at the embedded boundary (optional) |
| 115 | + warpx.eb_potential(x,y,z,t) = "voltage_function" |
| 116 | +
|
| 117 | +For more details on embedded boundary parameters, see: |
| 118 | + |
| 119 | +* `Embedded Boundary Input Parameters <https://warpx.readthedocs.io/en/latest/usage/parameters.html#embedded-boundary-conditions:~:text=in%20this%20case.-,Embedded%20Boundary%20Conditions,-%EF%83%81>`__ |
| 120 | +* `Embedded Boundary PICMI Input Parameters <https://warpx.readthedocs.io/en/latest/usage/python.html#pywarpx.picmi.EmbeddedBoundary:~:text=Custom%20class%20to,translated%20and%20inverted.>`__ |
| 121 | + |
| 122 | +Tips and Best Practices |
| 123 | +----------------------- |
| 124 | + |
| 125 | +* *Units:* Ensure your STL file uses the same unit system as your WarpX simulation |
| 126 | +* *Scale:* If needed, scale your geometry in your CAD software or mesh editor before exporting |
| 127 | +* *Orientation:* Check that your geometry is properly oriented relative to WarpX's coordinate system |
| 128 | +* *Resolution:* The STL mesh resolution should be appropriate for your simulation - too coarse may miss important features, too fine may slow down initialization |
| 129 | + |
| 130 | + * It's generally a good idea to simplify your geometery and remove features that are significantly smaller than the WarpX simulation grid |
| 131 | + |
| 132 | +* *Binary vs ASCII:* WarpX can read both binary and ASCII STL files, but binary files are typically smaller and faster to load |
| 133 | + |
| 134 | + |
| 135 | +Examples |
| 136 | +-------- |
| 137 | + |
| 138 | +.. note:: |
| 139 | + |
| 140 | + **TODO**: WarpX does not yet have examples that use STL files for embedded boundaries. |
| 141 | + |
| 142 | + Current embedded boundary examples in ``Examples/Tests/embedded_boundary_*`` use analytical |
| 143 | + functions to define geometries. A complete example demonstrating STL file usage will be |
| 144 | + added in the future. |
0 commit comments