# Thermal guide

Use [`gsim.thermal`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal> "            thermal") to find the heater power that shifts a waveguide's phase by π, how fast the heater switches, and how much it heats its neighbours. It solves heat conduction in a 2D cross-section of an infinitely long heater over one or more waveguides. Then it re-solves each waveguide's optical mode with the heated refractive index.

The solvers are [scikit-fem](<https://scikit-fem.readthedocs.io>) (heat) and [femwell](<https://helgegehring.github.io/femwell/>) (modes). Install them with the `femwell` extra:

```bash
pip install "gsim[femwell]"
```

Importing `gsim.thermal` works without the extra. Meshing or solving needs it and raises an error that names the extra.

## Configure and solve

The simulation is set up by physical concern, like [`gsim.tcad.Simulation`](<https://gdsfactory.com/gsim/reference/gsim/tcad/index.md#gsim.tcad.Simulation> "            Simulation") and [`gsim.fdtd.Simulation`](<https://gdsfactory.com/gsim/reference/gsim/fdtd/index.md#gsim.fdtd.Simulation> "            Simulation"). A TiN heater 2 µm above a 500 × 220 nm silicon strip, 320 µm long:

```python
import gdsfactory as gf

from gsim import thermal

gf.gpdk.PDK.activate()
xs = gf.cross_section.strip_heater_metal(width=0.5, heater_width=2.0)
component = gf.components.straight(length=320.0, cross_section=xs)

sim = thermal.Simulation()
sim.geometry(component)  # the active PDK's stack; or stack=a gsim LayerStack or a stack YAML path
sim.domain(plane="x=160", window=(-30.0, 30.0))
sim.heater(layers="heater", length_um=320.0)
sim.waveguides.add(name="arm", position_um=0.0)

sim.mesh().plot()  # optional: look at the cut and its mesh first

result = sim.solve(wavelength_um=1.55)
step = sim.solve_transient(power_mw=result.p_pi_mw, duration_us=300.0)
result.report(step)  # P_pi, efficiency, R, V_pi, peak heater rise, tau
```

The [thermo-optic phase shifter tutorial](<https://gdsfactory.com/gsim/nbs/thermal_phase_shifter/index.md>) builds the `stack` for a published heater. It then compares the result with the paper's simulations and measurements, and checks convergence.

Calling a concern applies several settings at once and validates them together; a failed call changes nothing. Single attributes can also be assigned, `sim.heater.length_um = 640.0`, and a whole concern replaced by a model or a mapping, `sim.solver = {"mesh_preset": "coarse"}`. The constructor takes the same concerns as keyword arguments:

```python
sim = thermal.Simulation(
    geometry={"component": component, "stack": stack},
    domain={"plane": "x=160", "window": (-30.0, 30.0)},
    heater={"layers": "heater", "length_um": 320.0},
    waveguides=[{"name": "arm", "position_um": 0.0}],
    solver={"mesh_preset": "coarse"},
)
```

The meshes are built on the first solve, or by `sim.mesh()`, and kept, with the steady solve, until an input they depend on changes. Editing a concern, the stack or the component in place is enough: the next call starts again from it. A change to the transient's time stepping alone keeps the steady solve. `mesh`, `solve` and `solve_transient` take `verbose=True` to print gmsh's meshing progress.

## Units and conventions

| Quantity | Unit |
| --- | --- |
| Lengths, positions, Window, depth | µm |
| Heater power (total over its length) | mW |
| Temperature | K, as a rise above ambient |
| Time | µs |
| Resistance, voltage | Ω, V |
| Phase | rad |
| Material properties (`materials`) | SI: W/m/K, J/kg/K, kg/m³, 1/K, S/m |
| Heat transfer coefficient | W/m²/K |

Field and argument names carry their unit: `length_um`, `power_mw`, `p_pi_mw`, `tau_us`, `heat_transfer_coefficient_w_per_m2k`. A temperature rise above ambient says so: `temperature_rise_k`, `peak_temperature_rise_k`. Material overrides take the stack's own SI field names (`thermal_conductivity`, ...).

The model is linear in power, because k, ρc\_p and dn/dT do not depend on temperature. One solve therefore gives every power. P\_π does not depend on the heater length in 2D. The length only sets the resistance, V\_π and the phase at a given power.

## Concerns

- `geometry(component, *, stack=...)`: the gdsfactory component (heater and waveguides) and its stack, as in `gsim.tcad`: a gsim `LayerStack`, a stack YAML path, or `None` (the default) for the active PDK. The active PDK's stack is taken with its silicon substrate, 100 µm deep, because the substrate is the heat sink: `get_stack(include_substrate=True, substrate_thickness=100.0)`. The RF default of `get_stack()` omits it.
- `domain(...)`: `plane`, the cut (`"x=<um>"` or `"y=<um>"`), across the heater and the waveguides; `window`, the in-plane `(min, max)` extent of the thermal Window in µm; `depth_um`, the Modelled depth.
- `heater(...)`: `layers`, the stack layer name(s) of the heater conductors; `length_um`, the heater length.
- `waveguides`: `add(name=, position_um=, layer=None, window=None, z_range=None, polarization="TE")`, `remove(name)`, `sim.waveguides[name]`, iteration. The first waveguide added is the phase shifter (`waveguides.phase_shifter`); every other one is a crosstalk victim. Each gets its own optical window around its drawn core: by default 1.5 µm beyond the core at each side and 2 µm above and below. `window` and `z_range` set its lateral and vertical extent, for a Mode that reaches further (a warning names the Window when the Mode's field at its edge is too strong). The default extent stops at a layer above or below the core whose index is at least the core's, such as the silicon substrate under a thin BOX: a Mode solve that included it would find that layer's Mode. A Mode that still reaches that edge leaks into the layer, which the model ignores, and the warning says so.
- `materials`: per-run overrides, `sim.materials["tin"] = {"conductivity": 1.6e6}`, keyed on the material name (case-insensitive). The fields are `thermal_conductivity`, `specific_heat`, `mass_density`, `dn_dT`, `conductivity` (electrical), `permittivity` and `loss_tangent`.
- `boundary(...)`: `top` and `sides`, `"adiabatic"` (default) or `"convective"`; `heat_transfer_coefficient_w_per_m2k` (10).
- `solver(...)`: `mesh_preset`, `refined_mesh_size_um`, `max_mesh_size_um`, `region_mesh_size_um`, `degree`, `optical_order`, `time_steps` and `theta` (see below).

The cut must be vertical and cross the heater and the waveguides. Two parameters belong to the model, not to the device, and are worth a convergence check:

- **Window** (`domain.window`): the in-plane extent. Its sides are adiabatic, so make the half-width at least five times the oxide thickness between the heater and the substrate.
- **Modelled depth** (`domain.depth_um`): how much substrate lies under the stack. Its bottom is the heat sink, at ambient. A 2D section has no steady state without a sink at a finite distance, and a 2D model cannot spread heat along the heater the way the real chip does, so the temperature rise keeps growing with depth. How fast depends on the Window. While the Window is wider than the depth, heat spreads sideways and the rise grows as a logarithm of the depth, slowly. Once the depth exceeds the Window, the adiabatic sides confine the heat to a column and the rise grows in proportion to the depth. P\_π therefore depends on the Window and the depth together: on the default PDK stack (100 µm of substrate), a Window of ±30 µm gives 17.41 mW and ±120 µm gives 17.84 mW. The default depth is the stack's substrate thickness; check P\_π against both.

A material override keys on the material name, so every layer made of that material changes. To give the 220 nm core a thin-film conductivity without changing the bulk substrate, give the core its own material name in the stack. The tutorial shows how.

Free convection in air (h ≈ 10 W/m²/K) is negligible next to conduction into the substrate, which is why adiabatic is the default boundary.

## Numerical settings

- [`Solver`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.Solver> "            Solver"): the mesh settings use `gsim.tcad.Solver`'s names. `mesh_preset="coarse" | "default" | "fine"` scales the mesh and sets the number of transient time steps (200, 400, 1000). `refined_mesh_size_um` sets the element size in the heater conductors and the waveguide cores, `max_mesh_size_um` caps it everywhere, and `region_mesh_size_um` sets it per region, winning over both. `time_steps` overrides the step count. The steps are graded: short after switch-on, where the response changes fastest, and doubling in length over eight equal blocks, so one setting serves any duration long enough to settle. `degree=1 | 2 | 3` sets the Lagrange element degree of the heat solve (default 2). On the same mesh, degree 2 cuts the temperature error of degree 1 by about two orders of magnitude for a similar solve time. The optical Mode solve does not depend on it. `optical_order=1 | 2` sets the finite element order of the optical Mode solves (default 2, as in the femwell adapter). On femwell's TiN heater, P\_π at order 2 moves by 0.02 % from the `"coarse"` to the `"fine"` mesh preset, against 0.6 % at order 1, for about 3 times the solve time. `theta` picks the time scheme: 1 (backward Euler, default) or 0.5 (Crank-Nicolson). Backward Euler delays the step response by about half the time step in use, so its `tau_us` and `tau_1e_us` read long by that much: on the quick-start heater over 300 µs, `tau_1e_us` is 14.44 µs, against 14.39 µs with Crank-Nicolson. `solve_transient` warns when the delay exceeds 2 % of `tau_1e_us`; more steps, a shorter duration or Crank-Nicolson shrink it.

## Results

- [`ThermalResult`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.ThermalResult> "            ThermalResult            dataclass   "): `p_pi_mw`, `efficiency_rad_per_mw`, `resistance_ohm`, `v_pi_v`, `crosstalk_rad` (on each victim at P\_π), `crosstalk_ratio` (victim phase over phase-shifter phase), `thermal_resistance_k_per_mw` (at each core), `dneff_per_mw`, `temperature(power_mw)`, `phase(power_mw)` and `drive_power(voltage_v=… | current_a=…)`, which turns a drive voltage or current into heater power in mW.
- [`TemperatureMap`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.TemperatureMap> "            TemperatureMap            dataclass   "): the temperature rise at one power, with `at(position_um, z_um)`, `peak(layer)` and `plot(xlim=, zlim=)`. Pass `ambient_k=` (e.g. 293.15) to `at` or `peak` for an absolute temperature instead of the rise.
- [`StepResponse`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.StepResponse> "            StepResponse            dataclass   "): the phase over time after the heater is switched on (`time_us`, `phase_rad`, `temperature_rise_k` at each core). It gives `tau_us` (the 10–90 % rise time) and `tau_1e_us` (the time to reach 1 − 1/e, about 63 %). In this linear model the fall time equals the rise time.
- [`HeaterReport`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.HeaterReport> "            HeaterReport            dataclass   "): the figures of merit in one table. It prints as text and renders as HTML in notebooks.

## Limits of the 2D model

- No heater ends, contacts, meanders or via spreading. The heater is infinitely long.
- k, ρc\_p and dn/dT do not depend on temperature. At a rise of a few tens of kelvin this is a few-percent effect.
- The heater current runs along the waveguide, with uniform Joule heating over each conductor's cross-section.
- No free-carrier or loss change with temperature.

## Functional core

These lower-level building blocks are public for custom pipelines. They work on meshes and arrays, not on components:

- [`build_cross_section_mesh`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.build_cross_section_mesh> "            build_cross_section_mesh") and [`CrossSectionMesh`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.CrossSectionMesh> "            CrossSectionMesh            dataclass   ")
- [`solve_steady`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.solve_steady> "            solve_steady"), [`solve_transient`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.solve_transient> "            solve_transient"), [`graded_times`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.graded_times> "            graded_times") and [`HeatOperator`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.HeatOperator> "            HeatOperator")
- [`joule_heat_density`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.joule_heat_density> "            joule_heat_density") and [`drive_power_per_length`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.drive_power_per_length> "            drive_power_per_length")
- [`thermo_optic_permittivity`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.thermo_optic_permittivity> "            thermo_optic_permittivity"), [`solve_thermo_optic_shift`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.solve_thermo_optic_shift> "            solve_thermo_optic_shift"), [`ModeShift`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.ModeShift> "            ModeShift            dataclass   ") and [`sample_temperature`](<https://gdsfactory.com/gsim/reference/gsim/thermal/index.md#gsim.thermal.sample_temperature> "            sample_temperature")

A solved temperature rise is a vector of degrees of freedom (DOFs): the mesh vertices first, in `mesh.p` order, then the edge and interior points of a degree 2 or 3 solve. `sample_temperature` reads it at any point, and works out the degree from the vector's length.
