# Geometry Refinement

*Geometry refinement allows for controlling the resolution of the surface mesh by defining the smallest geometric features to be captured. It ensures that the mesh accurately represents the geometry down to a specified length scale. GeometryAI needs to be enabled to use this feature.*

---

## Available Options

| *Option* | *Description* | *Applicable* |
|------------|-----------------|--------------|
| **Geometry accuracy** | The smallest length scale to be accurately resolved by the surface mesher. | always |
| **Preserve thin geometry** | Specifies whether to accurately resolve thin geometry features. | always |
| **Min passage size** | Minimum passage size that hidden geometry removal can resolve for this face group. | [**Remove hidden geometry**](../02.mesh-parameters.md#remove-hidden-geometry) is `On` |
| **Sealing size** | Threshold size below which all geometry gaps are automatically closed for this face group. | always |
| **Baffle face treatment** | How baffle faces in this face group are handled: `Remove`, `Thicken`, or `Default`. | [**Remove hidden geometry**](../02.mesh-parameters.md#remove-hidden-geometry) is `On` |
| **Baffle thicken outward** / **Baffle thicken inward** | How far a thickened baffle shell extends past the original face on its more / less exposed side, x **Geometry accuracy**. | [**Baffle face treatment**](../02.mesh-parameters.md#baffle-face-treatment) is `Thicken`, set here or inherited from the global setting |
| **Assigned surfaces** | The surfaces and/or `Box` regions to which the refinement is applied. | always |

---

## Detailed Descriptions

### Geometry accuracy

*Specifies the minimum length scale that the surface meshing process will accurately resolve.*

- **Required**
- **Example:** `0.01 m`

> **Notes:**
> - Start with a larger value and decrease it until the features you need are captured. A smaller value resolves finer detail on these faces, at the cost of more nodes where that detail is and longer meshing time. See {ref}`gai_geometry_accuracy` in the user guide.
> - Features smaller than the `Geometry accuracy` value are simplified, but a very large value does not guarantee that they are removed, so it cannot be used to exclude parts from the mesh.
> - The GeometryAI mesher may stop early due to resource limits before fully achieving the requested accuracy; check the process log for an indication of early termination
> - When the global **Geometry accuracy** in [Mesh parameters](../02.mesh-parameters.md) shows a threshold warning (*too coarse* or *too fine*), using a **Geometry Refinement** on the specific faces that require tighter control is strongly recommended over changing the global value. This keeps the overall mesh size manageable while still resolving critical features accurately.

### Preserve thin geometry

*Specifies whether thin geometry features should be accurately resolved during surface meshing.*

- **Options:** `Enabled`, `Disabled`, or left empty
- **Default:** Empty. The refinement then follows the global **Preserve thin geometry** setting in [Mesh parameters](../02.mesh-parameters.md#preserve-thin-geometry). Select `Enabled` or `Disabled` to override the global setting for this refinement.
> **Notes:**
> - Resolves features with thickness roughly equal to `geometry_accuracy`.
> - Applies only to the assigned surfaces, or to the parts of surfaces lying inside an assigned `Box` region.

### Min passage size

*Minimum passage size that the hidden geometry removal algorithm can resolve for this face group.*

- **Default:** Derived automatically from **Geometry accuracy** and **Sealing size**
- **Units:** Length
- **Example:** `0.005 m`

> **Notes:**
> - Only applicable when **Remove hidden geometry** is enabled in the global mesh parameters
> - Internal regions connected by passages smaller than this value may not be detected and removed.
> - If not specified, the value is derived from the global **Geometry accuracy** and **Sealing size** settings.
### Sealing size

*Threshold size below which all geometry gaps are automatically closed for this face group.*

- **Default:** `None` (falls back to the global **Sealing size** set in mesh parameters)
- **Units:** Length
- **Example:** `0.001 m`

> **Notes:**
> - Only applicable when **GeometryAI** is enabled.
> - When set, overrides the global **Sealing size** value from the mesh parameters for the assigned surfaces.

### Baffle thicken outward / Baffle thicken inward

*How far a thickened baffle shell extends past the original face, on its more exposed side (outward) and on its less exposed side (inward), in multiples of **Geometry accuracy**.*

- **Default:** Empty. Each extent then follows the global value in [Mesh parameters](../02.mesh-parameters.md#baffle-face-treatment), which defaults to `2.0`
- **Example:** `3.0` outward, `1.0` inward

> **Notes:**
> - The two extents add up to the shell thickness, which must be at least 4.0 x **Geometry accuracy**. A smaller total is rejected at validation. An empty extent counts at its inherited global value.
> - To make a thin baffle thicker in physical units, raise either extent or lower **Geometry accuracy** on this refinement.

### Assigned surfaces

*The list of surfaces, and/or `Box` regions, where the geometry refinement will be applied.*

- **Required**
> **Notes:**
> - Must reference valid surface entities in the geometry, or a `Box` region.
> - Surfaces can be assigned from the list using the `+` button or by graphical selection in the viewer.
> - A single refinement may mix surfaces and `Box` regions, except when any baffle setting (**Baffle face treatment** or a thicken extent) is set (see below).

#### Scoping a refinement to a Box region

*Instead of (or alongside) individual surfaces, a refinement can be scoped to a `Box` region.* This is useful when the features needing finer resolution are known by location rather than by face, or when they span many faces that would be tedious to select one by one.

> **Notes:**
> - Inside the box, the values set here override the per-face values of **every** surface passing through it. Anything the box leaves unspecified keeps its per-face value, so a box can tighten **Geometry accuracy** alone.
> - Only `Box` regions are accepted, and a box cannot be assigned while any baffle setting (**Baffle face treatment** or a thicken extent) is set on the same refinement.
> - Boxes from different refinements may overlap. In the overlap each setting takes the more conservative value (finest **Geometry accuracy**, largest **Sealing size**, smallest effective **Min passage size**, **Preserve thin geometry** on if any box enables it); no warning is raised. See {ref}`gai_overlapping_boxes` in the user guide.

---

<details>
<summary><h3 style="display:inline-block"> 💡 Tips</h3></summary>

- Use this refinement to ensure small but important geometric features (e.g., sharp corners, small fillets) are not lost during meshing.
- This setting controls geometry *representation accuracy* (how finely the surface mesh captures the CAD geometry), not directly the overall mesh resolution.

</details>

---

<details>
<summary><h3 style="display:inline-block"> ❓ Frequently Asked Questions</h3></summary>

- **What is a typical value for Geometry accuracy?**  
  > The value is highly dependent on the specific geometry and the scale of the features you want to capture. It should be smaller than the smallest important feature size. For example, for a vehicle simulation, you might want to resolve features on the scale of millimeters.

- **What is the difference between Geometry Refinement and Surface Refinement?**  
  > `Surface Refinement` controls the maximum edge length of surface cells directly. `Geometry Refinement` provides a more automated way of refining the mesh based on geometric curvature and feature size, driven by geometryAI.

</details>

---

<details>
<summary><h3 style="display:inline-block"> 🐍 Python Example Usage</h3></summary>

```{seealso}
Python API:
- {py:class}`~flow360.GeometryRefinement`
```

```python
import flow360 as fl

geometry_refinement = fl.GeometryRefinement(
    name="fine_features_refinement",
    faces=[geometry["wing_surface"], geometry["fuselage_surface"]],
    geometry_accuracy=0.001 * fl.u.m,
    preserve_thin_geometry=True,
    min_passage_size=0.005 * fl.u.m,
    sealing_size=0.0005 * fl.u.m,
)

# The same settings scoped to a Box region instead of named faces. Inside the
# box, geometry_accuracy overrides the per-face value of every surface passing
# through it; settings left unspecified keep their per-face values.
box_refinement = fl.GeometryRefinement(
    name="mirror_region_refinement",
    faces=[
        fl.Box(
            name="mirror_region",
            center=(1.2, 0.8, 0.9) * fl.u.m,
            size=(0.4, 0.3, 0.3) * fl.u.m,
        )
    ],
    geometry_accuracy=0.0005 * fl.u.m,
)
```
</details>
