# 2D Grating Coupler

This notebook demonstrates **2D FDTD in the XZ cross-section plane** using `gsim.meep`. Unlike the top-down XY effective-index sim, this one models the vertical stack (substrate / BOX / core / cladding) and a Gaussian-beam fiber source above the chip — the standard grating-coupler workflow.

**Requirements:** GDSFactory+ account for cloud simulation.

### Load a grating coupler + feed straight

```python
import gdsfactory as gf

gf.gpdk.PDK.activate()

c = gf.Component()
gc = gf.components.grating_coupler_elliptical(fiber_angle=0.0)

gc_r = c.add_ref(gc)
s_r = c.add_ref(gf.components.straight(length=3))
s_r.connect("o1", gc_r.ports["o1"])
c.add_port("o2", port=s_r.ports["o2"])
c
```

### Configure the XZ 2D simulation

Key differences from the XY notebook: - `sim.solver(mode="2d", y_cut="auto")` picks the vertical XZ cross-section sim. - `sim.source_fiber(...)` replaces the port-based mode source. - `sim.monitors = ["o2"]` monitors the waveguide end (feed straight).

```python
from gsim import meep
from gsim.common.stack import get_stack
from gsim.meep.models.api import Material

stack = get_stack()  # auto-detects active PDK

sim = meep.Simulation()

sim.geometry(component=c, stack=stack)
sim.materials = {
    "si": Material(refractive_index=3.47),
    "SiO2": Material(refractive_index=1.44),
}

sim.solver(resolution=25, mode="2d", y_cut="auto", save_animation=True)
sim.solver.stop_when_energy_decayed()

sim.source_fiber(
    x=25.0,
    z=2,
    angle_deg=-6.0,
    waist=5.2,
    wavelength=1.55,
    wavelength_span=0.06,
    polarization="TE",
)

sim.monitors = ["o2"]
sim.domain(
    pml=1.0,
    margin_x=0.5,
    margin_y=0.5,
    margin_z=(1.5, 0),
    extend_into_pml=True,
)
sim.num_freqs = 21

print(sim.validate_config())
```

```text
Stack validation: PASSED
Info:
  - Stopping: energy_decay (dt=20.0, decay_by=0.01, cap=2000.0)
```

### Preview the XZ cross-section

```python
sim.plot_2d(slices="y", aspect="auto")
```

```text
simulation.py:2104: FutureWarning: Use of domain.margin_z is deprecated and will be removed in a future release; replace it with domain.z_bounds=(-1.5, 4.6).
  result = self.build_config()
materials.py:541: DispersionCoverageWarning: no dispersion model covers wavelength=1.55 um (frequency=1.93414e+14 Hz); models: sellmeier (2.5-12 um). Base permittivity/conductivity are not validated at this wavelength
  resolved = resolve_material_at_wavelength(
materials.py:449: DispersionCoverageWarning: no dispersion model covers wavelength=1.55 um (frequency=1.93414e+14 Hz); models: sellmeier (2.5-12 um). Base permittivity/conductivity are not validated at this wavelength
  resolved = resolve_material_at_wavelength(
```

### Run the simulation

```python
result = sim.run(check_cache=True)
```

```text
  meep-0dbbd0bb  [####################] 100%  complete  elapsed 0m 00s


Extracting results.tar.gz...
Downloaded 325 files to sim-data-meep-0dbbd0bb
```

```python
result.plot_interactive()
```

```python
result.show_animation()
```

 Your browser does not support the video tag.

```python

```
