# Running MEEP Simulations

[MEEP](<https://meep.readthedocs.io/>) is an open-source FDTD electromagnetic simulator. This notebook demonstrates using the `gsim.meep` API to run an S-parameter simulation on a photonic Y-branch.

**Requirements:**

- [GDSFactory+](<https://gdsfactory.com>) account for cloud simulation

### Load a pcell from the active PDK

```python
import gdsfactory as gf

pdk = gf.get_active_pdk("generic")
```

```python
import gdsfactory as gf

c = gf.get_component("coupler")

cc = c.copy()
cc.draw_ports()
cc
```

### Configure and run simulation

```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.source(port="o1", wavelength=1.55, wavelength_span=0.04)
sim.monitors = ["o1", "o2", "o3", "o4"]
sim.domain(pml=1.0, margin_x=0.5, margin_y=0.5, z_bounds=(-1.0, 1.5))
sim.solver(resolution=20, save_animation=True, verbose_interval=5.0)
sim.num_freqs = 51
sim.solver.stop_when_energy_decayed()

print(sim.validate_config())
```

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

```python
sim.plot_2d(slices="xz")
```

```text
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 simulation on cloud

```python
# Run on GDSFactory+ cloud
result = sim.run(check_cache=True)
```

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


Extracting results.tar.gz...
Downloaded 487 files to sim-data-meep-8f8d434a
```

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

```python
result.plot_interactive(phase=True)
```

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

 Your browser does not support the video tag.
