Skip to content

2D Meshing

fiber_matrix.meshing.gmsh_mesher

GmshMesher

Handles meshing of the RVE using GMSH.

Source code in fiber_matrix/meshing/gmsh_mesher.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
class GmshMesher:
    """Handles meshing of the RVE using GMSH."""

    def __init__(self, mesh_name: str = "FiberMatrixRVE"):
        """
        Parameters
        ----------
        mesh_name : str, optional
            The name prefix for generated mesh files. Default is "FiberMatrixRVE".
        """
        self.mesh_name = mesh_name
        self._check_gmsh()

    def _check_gmsh(self):
        """Checks if the gmsh library is imported.

        Raises
        ------
        ImportError
            If gmsh is not available.
        """
        if gmsh is None:
            raise ImportError(
                "GMSH python library is not installed. Please install it using 'pip install gmsh'."
            )

    def create_mesh(
        self,
        fibers: List[PeriodicPrimaryFiber],
        boundaries: List[LinearBoundary],
        mesh_size_factor: float = 1.0,
        visualize_gui: bool = False,
        check_periodicity: bool = False,
    ):
        """Creates the mesh using GMSH.

        Parameters
        ----------
        fibers : List[PeriodicPrimaryFiber]
            List of fibers to include in the mesh.
        boundaries : List[LinearBoundary]
            List of boundaries defining the RVE.
        mesh_size_factor : float, optional
            Factor to control mesh refinement. Default is 1.0.
        visualize_gui : bool, optional
            If True, launches GMSH GUI to visualize the geometry/mesh. Default is False.
        check_periodicity : bool, optional
            If True, asserts that the generated mesh nodes on periodic boundaries match. Default is False.

        Notes
        -----
        To ensure robust boolean operations (OpenCASCADE kernel), the geometry is temporarily
        scaled up such that the RVE extent is order ~1.0. This avoids precision issues with
        very small coordinates (e.g. 1e-6). The geometry is scaled back to original size
        after boolean fragmenting and before mesh generation.
        """

        gmsh.initialize()
        gmsh.model.add(self.mesh_name)

        # Use OpenCASCADE kernel for robust boolean operations

        # 1. Create RVE Polygon
        # The boundaries might not be in order. We need to chain them.

        # Gather all points for scaling extent
        all_b_points = []
        for b in boundaries:
            all_b_points.append(b.points[0])
            all_b_points.append(b.points[1])
        all_b_points = np.array(all_b_points)

        # Boolean operations require the points to be sufficiently large in magnitude
        rve_extent = np.linalg.norm(
            np.max(all_b_points, axis=0) - np.min(all_b_points, axis=0)
        )
        scale_factor = 1.0 / rve_extent

        # Sort boundaries to form a continuous loop
        ordered_chain = []  # List of (LinearBoundary, start_point, end_point)
        remaining_boundaries = list(boundaries)

        # Pick the first one
        if not remaining_boundaries:
            raise ValueError("No boundaries provided.")

        current_b = remaining_boundaries.pop(0)
        # Orientation of the first one defines the loop direction
        current_start = current_b.points[0] * scale_factor
        current_end = current_b.points[1] * scale_factor

        ordered_chain.append((current_b, current_start, current_end))

        # Iteratively find the next connected boundary
        while remaining_boundaries:
            found_idx = -1
            found_orientation = 0  # 0: p0->p1, 1: p1->p0

            for i, b in enumerate(remaining_boundaries):
                p0 = b.points[0] * scale_factor
                p1 = b.points[1] * scale_factor

                # Check connectivity to current_end
                if np.linalg.norm(p0 - current_end) < 1e-4:
                    found_idx = i
                    found_orientation = 0
                    break
                elif np.linalg.norm(p1 - current_end) < 1e-4:
                    found_idx = i
                    found_orientation = 1
                    break

            if found_idx == -1:
                raise RuntimeError(
                    f"Could not find connected boundary in loop during meshing. Current tip: {current_end}"
                )

            b = remaining_boundaries.pop(found_idx)
            if found_orientation == 0:
                ordered_chain.append(
                    (b, b.points[0] * scale_factor, b.points[1] * scale_factor)
                )
                current_end = b.points[1] * scale_factor
            else:
                ordered_chain.append(
                    (b, b.points[1] * scale_factor, b.points[0] * scale_factor)
                )
                current_end = b.points[0] * scale_factor

        # Create GMSH Lines
        occ_line_tags = []
        first_pt_tag = gmsh.model.occ.addPoint(
            ordered_chain[0][1][0], ordered_chain[0][1][1], 0
        )
        prev_pt_tag = first_pt_tag

        for i in range(len(ordered_chain)):
            item = ordered_chain[i]
            # If last segment, connect to first point
            if i == len(ordered_chain) - 1:
                next_pt_tag = first_pt_tag
            else:
                p_end = item[2]
                next_pt_tag = gmsh.model.occ.addPoint(p_end[0], p_end[1], 0)

            l_tag = gmsh.model.occ.addLine(prev_pt_tag, next_pt_tag)
            occ_line_tags.append(l_tag)
            prev_pt_tag = next_pt_tag

        rve_wire = gmsh.model.occ.addWire(occ_line_tags)
        rve_face = gmsh.model.occ.addPlaneSurface([rve_wire])

        # 2. Add Fibers (Disks)
        fiber_disks = []

        def add_fiber_disk(f):
            return gmsh.model.occ.addDisk(
                f.center[0] * scale_factor,
                f.center[1] * scale_factor,
                0,
                f.radius * scale_factor,
                f.radius * scale_factor,
            )

        for f in fibers:
            fiber_disks.append(add_fiber_disk(f))
            for g in f.ghost_fibers:
                fiber_disks.append(add_fiber_disk(g))

        gmsh.model.occ.synchronize()

        if visualize_gui:
            gmsh.fltk.run()

        # 3. Clip fibers to RVE using Intersect
        # We want the part of fibers INSIDE the RVE.
        # Object=Fibers, Tool=RVE

        rve_dimtag = (2, rve_face)
        fiber_dimtags = [(2, t) for t in fiber_disks]

        # Intersect(Fibers, RVE). removeObject=True (consume fibers), removeTool=False (keep RVE).
        clipped_fibers_dimtags, _ = gmsh.model.occ.intersect(
            fiber_dimtags, [rve_dimtag], removeObject=True, removeTool=False
        )

        gmsh.model.occ.synchronize()

        if visualize_gui:
            gmsh.fltk.run()

        # 4. Fragment
        # Embed the clipped fibers into the RVE face.
        out_dimtags, out_dimtags_map = gmsh.model.occ.fragment(
            [rve_dimtag], clipped_fibers_dimtags
        )

        if not out_dimtags_map:
            raise RuntimeError(
                f"GMSH Fragment operation returned empty map. "
                f"rve_dimtag={rve_dimtag}, # clipped_fibers={len(clipped_fibers_dimtags)}"
            )

        # Now that boolean operations are done, we can scale the geometry back to its original size.
        gmsh.model.occ.dilate(
            out_dimtags,
            0,
            0,
            0,
            1.0 / scale_factor,
            1.0 / scale_factor,
            1.0 / scale_factor,
        )

        gmsh.model.occ.synchronize()
        if visualize_gui:
            gmsh.fltk.run()

        # 5. Identify Matrix vs Fibers
        # use out_dimtags_map from fragment operation to trace lineage.

        # Collect all tags that come from the fiber inputs
        fiber_surface_tags = set()
        # Inputs to fragment: [rve_dimtag] + clipped_fibers_dimtags
        # Index 0 is RVE. Indices 1..N are Fibers.

        for i in range(len(clipped_fibers_dimtags)):
            # Map index is 1 + i
            generated_dimtags = out_dimtags_map[1 + i]
            for dt in generated_dimtags:
                fiber_surface_tags.add(dt[1])

        # Collect all tags that come from the RVE input
        rve_related_tags = set()
        for dt in out_dimtags_map[0]:
            rve_related_tags.add(dt[1])

        # Matrix surfaces are those in RVE lineage that are NOT in Fiber lineage
        final_matrix_tags = list(rve_related_tags - fiber_surface_tags)
        final_fiber_tags = list(fiber_surface_tags)

        gmsh.model.occ.synchronize()

        # 6. Apply Periodic Conditions
        # Re-fetch lines as they might have been split
        lines = gmsh.model.getEntities(1)
        boundary_line_map = {b: [] for b in boundaries}

        # Calculate a relative tolerance for boundary matching based on RVE extent
        boundary_dist_tolerance = rve_extent * 1e-8

        for l in lines:
            tag = l[1]
            com = gmsh.model.occ.getCenterOfMass(1, tag)
            for b_idx, b in enumerate(boundaries):
                # Distance to segment
                dist = b.get_distance_to_fiber(np.array(com[:2]))
                if dist < boundary_dist_tolerance:
                    boundary_line_map[b].append(tag)

        for b in boundaries:
            if b.type == BoundaryType.PERIODIC and b.pair is not None:
                if hasattr(b, "index") and b.index < b.pair.index:
                    secondary_tags = boundary_line_map[b]
                    primary_tags = boundary_line_map[b.pair]

                    # Simple translation vector from Primary to Secondary
                    # P + T = S  => T = S - P
                    # Let's take midpoint of boundary
                    mid_s = (b.points[0] + b.points[1]) / 2.0
                    mid_p = (b.pair.points[0] + b.pair.points[1]) / 2.0
                    trans = mid_s - mid_p

                    translation = [
                        1,
                        0,
                        0,
                        trans[0],
                        0,
                        1,
                        0,
                        trans[1],
                        0,
                        0,
                        1,
                        0,
                        0,
                        0,
                        0,
                        1,
                    ]

                    if secondary_tags and primary_tags:
                        matched_secondary, matched_primary = (
                            self._get_periodic_boundary_mapping(
                                secondary_tags, primary_tags, translation
                            )
                        )

                        if matched_secondary:
                            if len(matched_secondary) != len(secondary_tags) or len(
                                matched_primary
                            ) != len(primary_tags):
                                print(
                                    f"Warning: Partial periodic match for boundary {b.index} vs {b.pair.index}. Matched {len(matched_secondary)} segments."
                                )
                                print(
                                    f"  Total Secondary: {len(secondary_tags)}, Total Primary: {len(primary_tags)}"
                                )

                            gmsh.model.mesh.setPeriodic(
                                1, matched_secondary, matched_primary, translation
                            )
                        else:
                            print(
                                f"Warning: No matching periodic segments found for boundary {b.index} vs {b.pair.index}."
                            )

        # 7. Physical Groups and Generation
        p_matrix = gmsh.model.addPhysicalGroup(2, final_matrix_tags)
        gmsh.model.setPhysicalName(2, p_matrix, "Matrix")

        p_fibers = gmsh.model.addPhysicalGroup(2, final_fiber_tags)
        gmsh.model.setPhysicalName(2, p_fibers, "Fibers")

        if mesh_size_factor:
            gmsh.model.mesh.setSize(gmsh.model.getEntities(0), mesh_size_factor)

        gmsh.model.mesh.generate(2)

        if visualize_gui:
            gmsh.fltk.run()

        gmsh.write(self.mesh_name + ".msh")
        gmsh.write(self.mesh_name + ".vtk")

        if check_periodicity:
            self._check_periodicity(
                boundaries, boundary_line_map, boundary_dist_tolerance
            )

        gmsh.finalize()

    def _is_inside_fiber(self, point_3d, fiber):
        """Checks if a point lies strictly inside a fiber (excluding boundary).

        Parameters
        ----------
        point_3d : Sequence[float]
            The 3D point to check (z is ignored).
        fiber : Fiber
            The fiber to check against.

        Returns
        -------
        bool
            True if the point is inside the fiber radius (with a small buffer).
        """
        dist = np.linalg.norm(np.array(point_3d[:2]) - fiber.center)
        return dist < fiber.radius * (1.0 - 1e-6)

    def _check_periodicity(self, boundaries, boundary_line_map, tolerance):
        """Verifies that nodes on periodic boundaries match up.

        Parameters
        ----------
        boundaries : List[LinearBoundary]
            List of boundaries to check.
        boundary_line_map : dict
            Mapping from boundaries to curve tags.
        tolerance : float
            Distance tolerance for node matching.
        """
        for b in boundaries:
            if b.type == BoundaryType.PERIODIC and b.pair is not None:
                # Ensure we only check once per pair
                if hasattr(b, "index") and b.index < b.pair.index:
                    secondary_tags = boundary_line_map[b]
                    primary_tags = boundary_line_map[b.pair]

                    if not secondary_tags or not primary_tags:
                        continue

                    # Calculate translation
                    mid_s = (b.points[0] + b.points[1]) / 2.0
                    mid_p = (b.pair.points[0] + b.pair.points[1]) / 2.0
                    trans = mid_s - mid_p

                    # Get nodes
                    secondary_node_tags = []
                    secondary_coords = []
                    for t in secondary_tags:
                        node_tags, coords, _ = gmsh.model.mesh.getNodes(
                            1, t, includeBoundary=True
                        )
                        secondary_node_tags.extend(node_tags)
                        for i in range(0, len(coords), 3):
                            secondary_coords.append(coords[i : i + 3])

                    primary_node_tags = []
                    primary_coords = []
                    for t in primary_tags:
                        node_tags, coords, _ = gmsh.model.mesh.getNodes(
                            1, t, includeBoundary=True
                        )
                        primary_node_tags.extend(node_tags)
                        for i in range(0, len(coords), 3):
                            # Apply translation to primary nodes
                            p = np.array(coords[i : i + 3])
                            p[0] += trans[0]
                            p[1] += trans[1]
                            primary_coords.append(p)

                    primary_node_tags = np.array(primary_node_tags)
                    secondary_node_tags = np.array(secondary_node_tags)

                    if not secondary_coords or not primary_coords:
                        continue

                    # Robust comparison using KDTree
                    secondary_coords = np.array(secondary_coords)
                    primary_coords = np.array(primary_coords)

                    tree = KDTree(secondary_coords)
                    # Query distances to nearest neighbors
                    dists, closest_indices = tree.query(primary_coords, k=1)

                    unmatched_indices = np.where(dists > tolerance)[0]

                    if len(unmatched_indices) > 0:
                        max_msg = (
                            f"Max discrepancy: {np.max(dists[unmatched_indices]):.2e}"
                        )

                        unmatched_primary_tags = primary_node_tags[unmatched_indices]
                        unmatched_closest_indices = closest_indices[unmatched_indices]
                        unmatched_secondary_tags = secondary_node_tags[
                            unmatched_closest_indices
                        ]

                        # Show up to 10 unmatched nodes for diagnostic purposes
                        num_show = 10
                        unmatched_details = []
                        for i in range(min(num_show, len(unmatched_indices))):
                            idx = unmatched_indices[i]
                            p_tag = unmatched_primary_tags[i]
                            s_tag = unmatched_secondary_tags[i]
                            dist = dists[idx]
                            unmatched_details.append(
                                f"  Primary Tag {p_tag} -> Closest Secondary Tag {s_tag} (dist: {dist:.2e})"
                            )

                        unmatched_str = "\n".join(unmatched_details)
                        if len(unmatched_indices) > num_show:
                            unmatched_str += (
                                f"\n  ... and {len(unmatched_indices) - num_show} more."
                            )

                        raise RuntimeError(
                            f"Periodic mesh verification failed for boundary {b.index} vs {b.pair.index}.\n"
                            f"Total # of Primary Nodes: {len(primary_coords)}\n"
                            f"Total # of Unmatched Nodes (dist > {tolerance}): {len(unmatched_indices)}\n"
                            f"{max_msg}\n"
                            f"Unmatched Node Details:\n{unmatched_str}"
                        )
                    else:
                        avg_dist = np.mean(dists) if len(dists) > 0 else 0
                        print(
                            f"Periodic check passed (KDTree) for boundary {b.index} <-> {b.pair.index} (avg dist: {avg_dist:.2e})"
                        )

    def _get_periodic_boundary_mapping(
        self,
        secondary_tags: List[int],
        primary_tags: List[int],
        translation: List[float],
    ) -> Tuple[List[int], List[int]]:
        """Finds a 1-to-1 mapping between secondary and primary curve segments based on CoM.

        Parameters
        ----------
        secondary_tags : List[int]
            List of curve tags on the secondary boundary.
        primary_tags : List[int]
            List of curve tags on the primary boundary.
        translation : List[float]
            The 16-element transformation matrix (GMSH format).

        Returns
        -------
        Tuple[List[int], List[int]]
            A tuple of (matched_secondary_tags, matched_primary_tags).
        """
        matched_secondary = []
        matched_primary = []
        remaining_primaries = list(primary_tags)
        trans_vec = np.array(translation[3:12:4])  # Extract [tx, ty, tz]

        # In GMSH: Secondary = Primary + Translation, so Primary = Secondary - Translation
        for s_tag in secondary_tags:
            s_com = np.array(gmsh.model.occ.getCenterOfMass(1, s_tag))
            expected_p_com = s_com - trans_vec

            best_idx = -1
            min_dist = 1e-6  # Tolerance

            for i, p_tag in enumerate(remaining_primaries):
                p_com = np.array(gmsh.model.occ.getCenterOfMass(1, p_tag))
                dist = np.linalg.norm(p_com - expected_p_com)
                if dist < min_dist:
                    min_dist = dist
                    best_idx = i

            if best_idx != -1:
                matched_secondary.append(s_tag)
                matched_primary.append(remaining_primaries[best_idx])
                remaining_primaries.pop(best_idx)

        return matched_secondary, matched_primary

__init__(mesh_name='FiberMatrixRVE')

Parameters:

Name Type Description Default
mesh_name str

The name prefix for generated mesh files. Default is "FiberMatrixRVE".

'FiberMatrixRVE'
Source code in fiber_matrix/meshing/gmsh_mesher.py
18
19
20
21
22
23
24
25
26
def __init__(self, mesh_name: str = "FiberMatrixRVE"):
    """
    Parameters
    ----------
    mesh_name : str, optional
        The name prefix for generated mesh files. Default is "FiberMatrixRVE".
    """
    self.mesh_name = mesh_name
    self._check_gmsh()

create_mesh(fibers, boundaries, mesh_size_factor=1.0, visualize_gui=False, check_periodicity=False)

Creates the mesh using GMSH.

Parameters:

Name Type Description Default
fibers List[PeriodicPrimaryFiber]

List of fibers to include in the mesh.

required
boundaries List[LinearBoundary]

List of boundaries defining the RVE.

required
mesh_size_factor float

Factor to control mesh refinement. Default is 1.0.

1.0
visualize_gui bool

If True, launches GMSH GUI to visualize the geometry/mesh. Default is False.

False
check_periodicity bool

If True, asserts that the generated mesh nodes on periodic boundaries match. Default is False.

False
Notes

To ensure robust boolean operations (OpenCASCADE kernel), the geometry is temporarily scaled up such that the RVE extent is order ~1.0. This avoids precision issues with very small coordinates (e.g. 1e-6). The geometry is scaled back to original size after boolean fragmenting and before mesh generation.

Source code in fiber_matrix/meshing/gmsh_mesher.py
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def create_mesh(
    self,
    fibers: List[PeriodicPrimaryFiber],
    boundaries: List[LinearBoundary],
    mesh_size_factor: float = 1.0,
    visualize_gui: bool = False,
    check_periodicity: bool = False,
):
    """Creates the mesh using GMSH.

    Parameters
    ----------
    fibers : List[PeriodicPrimaryFiber]
        List of fibers to include in the mesh.
    boundaries : List[LinearBoundary]
        List of boundaries defining the RVE.
    mesh_size_factor : float, optional
        Factor to control mesh refinement. Default is 1.0.
    visualize_gui : bool, optional
        If True, launches GMSH GUI to visualize the geometry/mesh. Default is False.
    check_periodicity : bool, optional
        If True, asserts that the generated mesh nodes on periodic boundaries match. Default is False.

    Notes
    -----
    To ensure robust boolean operations (OpenCASCADE kernel), the geometry is temporarily
    scaled up such that the RVE extent is order ~1.0. This avoids precision issues with
    very small coordinates (e.g. 1e-6). The geometry is scaled back to original size
    after boolean fragmenting and before mesh generation.
    """

    gmsh.initialize()
    gmsh.model.add(self.mesh_name)

    # Use OpenCASCADE kernel for robust boolean operations

    # 1. Create RVE Polygon
    # The boundaries might not be in order. We need to chain them.

    # Gather all points for scaling extent
    all_b_points = []
    for b in boundaries:
        all_b_points.append(b.points[0])
        all_b_points.append(b.points[1])
    all_b_points = np.array(all_b_points)

    # Boolean operations require the points to be sufficiently large in magnitude
    rve_extent = np.linalg.norm(
        np.max(all_b_points, axis=0) - np.min(all_b_points, axis=0)
    )
    scale_factor = 1.0 / rve_extent

    # Sort boundaries to form a continuous loop
    ordered_chain = []  # List of (LinearBoundary, start_point, end_point)
    remaining_boundaries = list(boundaries)

    # Pick the first one
    if not remaining_boundaries:
        raise ValueError("No boundaries provided.")

    current_b = remaining_boundaries.pop(0)
    # Orientation of the first one defines the loop direction
    current_start = current_b.points[0] * scale_factor
    current_end = current_b.points[1] * scale_factor

    ordered_chain.append((current_b, current_start, current_end))

    # Iteratively find the next connected boundary
    while remaining_boundaries:
        found_idx = -1
        found_orientation = 0  # 0: p0->p1, 1: p1->p0

        for i, b in enumerate(remaining_boundaries):
            p0 = b.points[0] * scale_factor
            p1 = b.points[1] * scale_factor

            # Check connectivity to current_end
            if np.linalg.norm(p0 - current_end) < 1e-4:
                found_idx = i
                found_orientation = 0
                break
            elif np.linalg.norm(p1 - current_end) < 1e-4:
                found_idx = i
                found_orientation = 1
                break

        if found_idx == -1:
            raise RuntimeError(
                f"Could not find connected boundary in loop during meshing. Current tip: {current_end}"
            )

        b = remaining_boundaries.pop(found_idx)
        if found_orientation == 0:
            ordered_chain.append(
                (b, b.points[0] * scale_factor, b.points[1] * scale_factor)
            )
            current_end = b.points[1] * scale_factor
        else:
            ordered_chain.append(
                (b, b.points[1] * scale_factor, b.points[0] * scale_factor)
            )
            current_end = b.points[0] * scale_factor

    # Create GMSH Lines
    occ_line_tags = []
    first_pt_tag = gmsh.model.occ.addPoint(
        ordered_chain[0][1][0], ordered_chain[0][1][1], 0
    )
    prev_pt_tag = first_pt_tag

    for i in range(len(ordered_chain)):
        item = ordered_chain[i]
        # If last segment, connect to first point
        if i == len(ordered_chain) - 1:
            next_pt_tag = first_pt_tag
        else:
            p_end = item[2]
            next_pt_tag = gmsh.model.occ.addPoint(p_end[0], p_end[1], 0)

        l_tag = gmsh.model.occ.addLine(prev_pt_tag, next_pt_tag)
        occ_line_tags.append(l_tag)
        prev_pt_tag = next_pt_tag

    rve_wire = gmsh.model.occ.addWire(occ_line_tags)
    rve_face = gmsh.model.occ.addPlaneSurface([rve_wire])

    # 2. Add Fibers (Disks)
    fiber_disks = []

    def add_fiber_disk(f):
        return gmsh.model.occ.addDisk(
            f.center[0] * scale_factor,
            f.center[1] * scale_factor,
            0,
            f.radius * scale_factor,
            f.radius * scale_factor,
        )

    for f in fibers:
        fiber_disks.append(add_fiber_disk(f))
        for g in f.ghost_fibers:
            fiber_disks.append(add_fiber_disk(g))

    gmsh.model.occ.synchronize()

    if visualize_gui:
        gmsh.fltk.run()

    # 3. Clip fibers to RVE using Intersect
    # We want the part of fibers INSIDE the RVE.
    # Object=Fibers, Tool=RVE

    rve_dimtag = (2, rve_face)
    fiber_dimtags = [(2, t) for t in fiber_disks]

    # Intersect(Fibers, RVE). removeObject=True (consume fibers), removeTool=False (keep RVE).
    clipped_fibers_dimtags, _ = gmsh.model.occ.intersect(
        fiber_dimtags, [rve_dimtag], removeObject=True, removeTool=False
    )

    gmsh.model.occ.synchronize()

    if visualize_gui:
        gmsh.fltk.run()

    # 4. Fragment
    # Embed the clipped fibers into the RVE face.
    out_dimtags, out_dimtags_map = gmsh.model.occ.fragment(
        [rve_dimtag], clipped_fibers_dimtags
    )

    if not out_dimtags_map:
        raise RuntimeError(
            f"GMSH Fragment operation returned empty map. "
            f"rve_dimtag={rve_dimtag}, # clipped_fibers={len(clipped_fibers_dimtags)}"
        )

    # Now that boolean operations are done, we can scale the geometry back to its original size.
    gmsh.model.occ.dilate(
        out_dimtags,
        0,
        0,
        0,
        1.0 / scale_factor,
        1.0 / scale_factor,
        1.0 / scale_factor,
    )

    gmsh.model.occ.synchronize()
    if visualize_gui:
        gmsh.fltk.run()

    # 5. Identify Matrix vs Fibers
    # use out_dimtags_map from fragment operation to trace lineage.

    # Collect all tags that come from the fiber inputs
    fiber_surface_tags = set()
    # Inputs to fragment: [rve_dimtag] + clipped_fibers_dimtags
    # Index 0 is RVE. Indices 1..N are Fibers.

    for i in range(len(clipped_fibers_dimtags)):
        # Map index is 1 + i
        generated_dimtags = out_dimtags_map[1 + i]
        for dt in generated_dimtags:
            fiber_surface_tags.add(dt[1])

    # Collect all tags that come from the RVE input
    rve_related_tags = set()
    for dt in out_dimtags_map[0]:
        rve_related_tags.add(dt[1])

    # Matrix surfaces are those in RVE lineage that are NOT in Fiber lineage
    final_matrix_tags = list(rve_related_tags - fiber_surface_tags)
    final_fiber_tags = list(fiber_surface_tags)

    gmsh.model.occ.synchronize()

    # 6. Apply Periodic Conditions
    # Re-fetch lines as they might have been split
    lines = gmsh.model.getEntities(1)
    boundary_line_map = {b: [] for b in boundaries}

    # Calculate a relative tolerance for boundary matching based on RVE extent
    boundary_dist_tolerance = rve_extent * 1e-8

    for l in lines:
        tag = l[1]
        com = gmsh.model.occ.getCenterOfMass(1, tag)
        for b_idx, b in enumerate(boundaries):
            # Distance to segment
            dist = b.get_distance_to_fiber(np.array(com[:2]))
            if dist < boundary_dist_tolerance:
                boundary_line_map[b].append(tag)

    for b in boundaries:
        if b.type == BoundaryType.PERIODIC and b.pair is not None:
            if hasattr(b, "index") and b.index < b.pair.index:
                secondary_tags = boundary_line_map[b]
                primary_tags = boundary_line_map[b.pair]

                # Simple translation vector from Primary to Secondary
                # P + T = S  => T = S - P
                # Let's take midpoint of boundary
                mid_s = (b.points[0] + b.points[1]) / 2.0
                mid_p = (b.pair.points[0] + b.pair.points[1]) / 2.0
                trans = mid_s - mid_p

                translation = [
                    1,
                    0,
                    0,
                    trans[0],
                    0,
                    1,
                    0,
                    trans[1],
                    0,
                    0,
                    1,
                    0,
                    0,
                    0,
                    0,
                    1,
                ]

                if secondary_tags and primary_tags:
                    matched_secondary, matched_primary = (
                        self._get_periodic_boundary_mapping(
                            secondary_tags, primary_tags, translation
                        )
                    )

                    if matched_secondary:
                        if len(matched_secondary) != len(secondary_tags) or len(
                            matched_primary
                        ) != len(primary_tags):
                            print(
                                f"Warning: Partial periodic match for boundary {b.index} vs {b.pair.index}. Matched {len(matched_secondary)} segments."
                            )
                            print(
                                f"  Total Secondary: {len(secondary_tags)}, Total Primary: {len(primary_tags)}"
                            )

                        gmsh.model.mesh.setPeriodic(
                            1, matched_secondary, matched_primary, translation
                        )
                    else:
                        print(
                            f"Warning: No matching periodic segments found for boundary {b.index} vs {b.pair.index}."
                        )

    # 7. Physical Groups and Generation
    p_matrix = gmsh.model.addPhysicalGroup(2, final_matrix_tags)
    gmsh.model.setPhysicalName(2, p_matrix, "Matrix")

    p_fibers = gmsh.model.addPhysicalGroup(2, final_fiber_tags)
    gmsh.model.setPhysicalName(2, p_fibers, "Fibers")

    if mesh_size_factor:
        gmsh.model.mesh.setSize(gmsh.model.getEntities(0), mesh_size_factor)

    gmsh.model.mesh.generate(2)

    if visualize_gui:
        gmsh.fltk.run()

    gmsh.write(self.mesh_name + ".msh")
    gmsh.write(self.mesh_name + ".vtk")

    if check_periodicity:
        self._check_periodicity(
            boundaries, boundary_line_map, boundary_dist_tolerance
        )

    gmsh.finalize()