Metadata-Version: 2.4
Name: vcti-usd-model-converge-h5
Version: 0.2.0
Summary: Converge CFD post files to the VCollab USD model stage: streams as component groups and slabs, boundary surfaces triangulated as atoms, parcels as a point component, cell and parcel variables as fields under a selectable averaging rule, and one frame per file on the time or crank-angle axis the run records
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-usd-model-converge-h5
Project-URL: Changelog, https://github.com/vcollab/vcti-python-usd-model-converge-h5/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: vcti-reader-converge-h5>=0.1.0
Requires-Dist: vcti-usd-schema-model>=0.3.0
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Dynamic: license-file

# Converge to USD Model

Converge CFD post files to the VCollab USD model stage: streams as component groups and slabs, boundary surfaces triangulated as atoms, parcels as a point component, cell and parcel variables as fields under a selectable averaging rule, and one frame per file on the time or crank-angle axis the run records

## Overview

A Converge run leaves one HDF5 post file per output step, each holding
polyhedral streams with named boundaries, cell-centered variables and
parcels. The VCollab model stage is the USD stage the server builds for
one model and projects into the viewer protocol: renderer-ready surfaces,
fields with one value per point, and result frames. This package is the
converter between the two. It reads the run through
`vcti-reader-converge-h5`, decides how each Converge thing becomes a
model thing, writes the stage with the authoring helpers of
`vcti-usd-schema-model`, and checks the result with that package's
validator before returning.

What the stage holds, for a run with streams `00` and `01`:

| Converge | Model |
|---|---|
| Stream `00` | Assembly `hierarchy/Stream_00` |
| Its boundaries | Occurrence `Stream_00/Boundaries`, one `VctComponent` per boundary, one atom each |
| Its parcels | Occurrence `Stream_00/Parcels`, one component, present only when some file has parcels |
| Boundary faces | Triangles and lines on one slab, vertices compacted per boundary, normals computed |
| A cell variable | A `VctField` with `vct:sourceAssociation = cell` and the mapping rule used |
| A parcel variable | A `VctField` named `Parcel_…`, values as read |
| A post file | A `VctFrame` under `frames/steps` with its `OUTPUT_TIME` as physical time or crank angle |
| A mesh change | Another member of the occurrence's `VctMeshSequence`; unchanged files reuse the member |
| Every array | Bytes in `buffers/` beside the stage, located through `VctBufferAPI` |

## Installation

```bash
pip install vcti-usd-model-converge-h5
```

### In `requirements.txt`

```
vcti-usd-model-converge-h5>=0.2.0
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-usd-model-converge-h5>=0.2.0",
]
```

---

## Quick Start

The snippets below use the synthetic post file the reader package ships,
laid out as a real one is. Replace the glob with real files and nothing
else changes.

### Convert a run

```python
from vcti.reader.converge.h5 import write_synthetic
from vcti.usd.model.converge.h5 import convert

for step, time in ((1, 1.0e-3), (2, 2.0e-3), (3, 3.0e-3)):
    write_synthetic(f"post{step:06d}.h5", output_time=time)

path = convert("post*.h5", "stage/model.usda", mapping="averagedAll")
print(path)
```

The files are found, probed and ordered by the output time each records.
The stage is validated before `convert` returns; a stage that fails is a
bug, and the failures come back in the exception's message.

### Look at what was written

```python
from vcti.usd.schema.model import open_stage

stage = open_stage(path)
for assembly in stage.GetPrimAtPath("/Model/hierarchy").GetChildren():
    for occurrence in assembly.GetChildren():
        group = occurrence.GetChildren()[0]
        print(assembly.GetName(), occurrence.GetName(), [c.GetName() for c in group.GetChildren()])

for frame in stage.GetPrimAtPath("/Model/frames/steps").GetChildren():
    print(frame.GetName(), frame.GetAttribute("vct:physicalTime").Get())

print([f.GetName() for f in stage.GetPrimAtPath("/Model/fields").GetChildren()])
```

A boundary called `walls (ID = 1)` becomes the component `walls_ID_1`; its
original name and Converge id are on the component as `attr:name` and
`attr:boundaryId`.

### Choose how cell values reach the points

```python
from vcti.usd.model.converge.h5 import MAPPINGS

print(MAPPINGS)
convert("post*.h5", "flat/model.usda", mapping="unaveraged")
```

| `mapping` | One value per point is | Points |
|---|---|---|
| `averagedAll` (default) | the mean of every cell touching the point's stream vertex, through interior faces too | one per boundary and vertex |
| `averagedWithinComponent` | the mean of the cells behind the point's own boundary's faces at that vertex | one per boundary and vertex |
| `unaveraged` | the cell behind the face; every face corner is its own point, so faces shade flat | one per face corner |

Vertices on two boundaries are duplicated under every rule, because each
boundary is an atom and the schema wants one atom per point. The rule only
decides what each copy carries. The token is recorded on every field and
in its provenance.

### Name the quantities

Post files carry no units. A table keyed by variable name supplies the
quantity and units; the default covers pressure, velocity, temperature,
density and a few more, and yours extends or overrides it.

```python
convert(
    "post*.h5",
    "bar/model.usda",
    quantities={"PRESSURE": ("pressure", "bar"), "IDREG": ("regionId", "1")},
)
```

### The pieces on their own

```python
from vcti.reader.converge.h5 import ConvergeH5Reader
from vcti.usd.model.converge.h5 import CellToPoint, extract_surface

with ConvergeH5Reader("post000001.h5") as post:
    stream = post.stream("00")
    surface = extract_surface(stream)
    rule = CellToPoint("averagedAll", surface, stream)
    pressure = rule.apply(stream.cell_values("PRESSURE"))
    print(surface.num_points, surface.triangles.shape, pressure.shape)
```

---

## Key API

| Name | Role |
|---|---|
| `convert(sources, output, *, mapping, quantities, parcels, check)` | The whole run to a validated stage; returns the root layer path |
| `MAPPINGS`, `AVERAGED_ALL`, `AVERAGED_WITHIN_COMPONENT`, `UNAVERAGED` | The cell-to-point rules |
| `extract_surface(stream, per_face)` → `Surface` | A stream's boundary faces as points, normals, atom ids, triangles and lines |
| `CellToPoint(rule, surface, stream).apply(values)` | A rule prepared once per mesh version, applied per variable |
| `field_spec(variable, family, association, mapping, quantities)` | What a variable becomes as a field; `DEFAULT_QUANTITIES` is the built-in table |
| `StageWriter` | The authoring layer: assemblies, occurrences, versions, frames, fields, located arrays |
| `prim_name`, `unique_names`, `stream_name` | Converge labels as prim names |

## Performance

A real file with thirteen million faces converts in about forty seconds
per step on a laptop: a third extracting the surface, a third preparing
the averaging incidence, a third mapping some twenty variables. Buffers
for such a step are about half a gigabyte. Everything is numpy; nothing
is compiled.

## Dependencies

- `numpy`
- `vcti-reader-converge-h5`, which finds and opens the post files.
- `vcti-usd-schema-model`, which defines the stage this package writes,
  provides the authoring helpers, and validates the result. It brings
  `usd-core`.

---

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| Understand why the stage looks the way it does | [docs/design.md](docs/design.md) |
| Find your way around the source | [docs/source-guide.md](docs/source-guide.md) |
| See what changed | [CHANGELOG.md](CHANGELOG.md) |
