> ## Documentation Index
> Fetch the complete documentation index at: https://khaos-ml.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Physics

> PDE surrogates: sweeps, design screening, solver validation.

## Sweep a permeability field

**Problem:** see how pressure responds to hundreds of permeability fields without
running a solver for each. **Uses:** Khaos, NumPy and Matplotlib.

```python theme={null}
import khaos
import numpy as np
import matplotlib.pyplot as plt

darcy = khaos.load("pde_darcy_flow_2d")

rng = np.random.default_rng(0)
fields = [rng.lognormal(size=(128, 128)) for _ in range(500)]

peak_pressure = [darcy(k).prediction.max() for k in fields]

plt.hist(peak_pressure, bins=30)
plt.xlabel("peak pressure")
plt.show()
print(np.percentile(peak_pressure, [5, 50, 95]))
# [0.62 0.94 1.51]
```

## Screen heat-source placements

**Problem:** find where to place a component on a board so its hot spot is as cool
as possible. **Uses:** Khaos and SciPy.

```python theme={null}
import khaos
import numpy as np
from scipy.optimize import minimize

heat = khaos.load("pde_heat_diffusion_2d")

def source_grid(x, y):
    g = np.zeros((64, 64), dtype=np.float32)
    g[int(y), int(x)] = 1.0
    return g

def hottest_spot(params):
    x, y = np.clip(params, 0, 63)
    return heat(source_grid(x, y)).prediction.max()

best = minimize(hottest_spot, x0=[32, 32], method="Nelder-Mead")
print(best.x, best.fun)
# [14.2 47.9] 0.318
```

## Validate a surrogate against a solver

**Problem:** check that the surrogate agrees with a classical solver before trusting
it in a design loop. **Uses:** Khaos and your own solver.

```python theme={null}
import khaos
import numpy as np

wave = khaos.load("pde_wave_evolution_2d")

initial = make_initial_pulse()           # (64, 64) array
surrogate = wave(initial).prediction
reference = run_finite_difference(initial, steps=200)   # your solver

rel_error = np.linalg.norm(surrogate - reference) / np.linalg.norm(reference)
print(f"relative error: {rel_error:.3%}")
# relative error: 2.140%
```

## Steady flow around an obstacle

**Problem:** get a velocity field from a boundary description. **Uses:** Khaos
alone.

```python theme={null}
flow = khaos.load("pde_navier_stokes_steady_2d")(boundary_grid)
print(flow.prediction.shape)
# (128, 128, 2)    <- x and y velocity components
```

## Inputs and outputs for each model

These are real outputs of the v1 models, run through their exported ONNX files on held-out examples. The shapes listed are the exact ONNX inputs and outputs; `batch` and other named sizes are dynamic.

### `pde_darcy_flow_2d`

A scenario in, the pressure field out, on a 64 x 64 grid. The fields, boundary conditions and parameters go in as raw physical values; normalization is inside the model. The reference is the numerical solver's answer for the same scenario.

**Model inputs:** `x float32 [batch, 4, 64, 64]`\
**Model outputs:** `y float32 [batch, 1, 64, 64]`

**Input**

```python theme={null}
# x: scenario channels on a 64 x 64 grid
x[0]  # permeability: min 0.10, max 6.41
x[1]  # source: min 0.56, max 0.56
x[2]  # dirichlet_mask: min 0.00, max 1.00
x[3]  # dirichlet_value: min -0.61, max 0.89
```

**Output**

```json theme={null}
{
  "output_shape": [
    1,
    64,
    64
  ],
  "pressure_centre_row_x28_to_x34": [
    [
      -0.238,
      -0.253,
      -0.255,
      -0.26,
      -0.251,
      -0.227
    ]
  ],
  "solver_reference_centre_row": [
    [
      -0.232,
      -0.223,
      -0.213,
      -0.204,
      -0.195,
      -0.187
    ]
  ],
  "relative_L2_error_vs_solver": 0.228
}
```

### `pde_navier_stokes_steady_2d`

A scenario in, the velocity (u, v) field out, on a 64 x 64 grid. The fields, boundary conditions and parameters go in as raw physical values; normalization is inside the model. The reference is the numerical solver's answer for the same scenario.

**Model inputs:** `x float32 [batch, 3, 64, 64]; params float32 [batch, 1]`\
**Model outputs:** `y float32 [batch, 2, 64, 64]`

**Input**

```python theme={null}
# x: scenario channels on a 64 x 64 grid
x[0]  # solid_mask: min 0.00, max 1.00
x[1]  # wall_u: min -0.28, max 0.00
x[2]  # wall_v: min -0.00, max 0.21
params = [0.0812]
```

**Output**

```json theme={null}
{
  "output_shape": [
    2,
    64,
    64
  ],
  "velocity (u, v)_centre_row_x28_to_x34": [
    [
      0.066,
      0.067,
      0.067,
      0.067,
      0.067,
      0.067
    ],
    [
      -0.029,
      -0.029,
      -0.03,
      -0.03,
      -0.029,
      -0.029
    ]
  ],
  "solver_reference_centre_row": [
    [
      0.066,
      0.067,
      0.067,
      0.067,
      0.067,
      0.067
    ],
    [
      -0.028,
      -0.028,
      -0.029,
      -0.029,
      -0.028,
      -0.028
    ]
  ],
  "relative_L2_error_vs_solver": 0.023
}
```

### `pde_heat_diffusion_2d`

A scenario in, the temperature field out, on a 64 x 64 grid, t = 0.50. The fields, boundary conditions and parameters go in as raw physical values; normalization is inside the model. The reference is the numerical solver's answer for the same scenario.

**Model inputs:** `x float32 [batch, 4, 64, 64]; t float32 [batch]; params float32 [batch, 1]`\
**Model outputs:** `y float32 [batch, 1, 64, 64]`

**Input**

```python theme={null}
# x: scenario channels on a 64 x 64 grid
x[0]  # initial_temperature: min -3.77, max 3.13
x[1]  # dirichlet_mask: min 0.00, max 1.00
x[2]  # dirichlet_value: min -0.91, max 0.95
x[3]  # source: min -9.43, max -0.00
params = [0.0124]
t = 0.50
```

**Output**

```json theme={null}
{
  "output_shape": [
    1,
    64,
    64
  ],
  "temperature_centre_row_x28_to_x34": [
    [
      -0.809,
      -0.669,
      -0.544,
      -0.44,
      -0.352,
      -0.267
    ]
  ],
  "solver_reference_centre_row": [
    [
      -0.836,
      -0.693,
      -0.563,
      -0.446,
      -0.345,
      -0.259
    ]
  ],
  "relative_L2_error_vs_solver": 0.049
}
```

### `pde_wave_evolution_2d`

A scenario in, the displacement and velocity field out, on a 64 x 64 grid, t = 0.60. The fields, boundary conditions and parameters go in as raw physical values; normalization is inside the model. The reference is the numerical solver's answer for the same scenario.

**Model inputs:** `x float32 [batch, 4, 64, 64]; t float32 [batch]; params float32 [batch, 1]`\
**Model outputs:** `y float32 [batch, 2, 64, 64]`

**Input**

```python theme={null}
# x: scenario channels on a 64 x 64 grid
x[0]  # initial_displacement: min 0.00, max 1.68
x[1]  # initial_velocity: min 0.00, max 0.00
x[2]  # fixed_edge_mask: min 0.00, max 1.00
x[3]  # free_edge_mask: min 0.00, max 0.00
params = [0.7579]
t = 0.60
```

**Output**

```json theme={null}
{
  "output_shape": [
    2,
    64,
    64
  ],
  "displacement and velocity_centre_row_x28_to_x34": [
    [
      -0.325,
      -0.319,
      -0.316,
      -0.324,
      -0.322,
      -0.332
    ],
    [
      0.778,
      0.642,
      0.488,
      0.763,
      0.43,
      0.318
    ]
  ],
  "solver_reference_centre_row": [
    [
      -0.252,
      -0.257,
      -0.261,
      -0.264,
      -0.268,
      -0.272
    ],
    [
      1.65,
      1.615,
      1.566,
      1.52,
      1.488,
      1.475
    ]
  ],
  "relative_L2_error_vs_solver": 0.545
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.