# Palace Width Sweep: Parallel Simulation

Sweep the microstrip width from 5 to 50 um (5 points), run all simulations in parallel on GDSFactory+ cloud using the non-blocking API, then compare S11 and S21 across widths.

**Requirements:**

- IHP PDK: `uv pip install ihp-gdsfactory`
- [GDSFactory+](<https://gdsfactory.com>) account for cloud simulation

### Define the sweep

```python
import numpy as np

widths = np.arange(2, 21, 4)
```

### Build components and configure simulations

```python
import gdsfactory as gf
from ihp import LAYER, PDK, cells

from gsim.common.stack import get_stack
from gsim.palace import DrivenSim

PDK.activate()

stack = get_stack()  # auto-detects active PDK

sims = []

for w in widths:
    # Build component
    c = gf.Component()
    r1 = c << cells.straight_metal(length=1000, width=w)

    r = c.get_region(layer=LAYER.TopMetal2drawing)
    r_sized = r.sized(+20000)
    c.add_polygon(r_sized, layer=LAYER.Metal1drawing)
    c.add_ports(r1.ports)

    # Configure simulation
    sim = DrivenSim()
    sim.set_output_dir(f"./palace-sim-w{w:.1f}")
    sim.set_geometry(c)
    sim.set_stack(stack)
    # Surround the design with air. Without this the absorbing boundary sits
    # directly on the passivation, microns above the trace, and adds large
    # spurious loss and reflection.
    sim.set_airbox(margin_x=50, margin_y=50, z_above=100, z_below=100)

    for port in c.ports:
        sim.add_port(
            port.name, from_layer="metal1", to_layer="topmetal2", geometry="interlayer"
        )

    sim.set_driven(fmin=1e9, fmax=100e9, num_points=80)
    sim.mesh(preset="default")

    sims.append(sim)

print(f"Configured {len(sims)} simulations")
```

```text
Small conductor feature detected (2.000 um) may be under-resolved by refined_mesh_size=5.000 um. The heuristic target is <= 1.000 um (2 cells/feature). Pass auto_size=True to scale the mesh down.


Small conductor feature detected (6.000 um) may be under-resolved by refined_mesh_size=5.000 um. The heuristic target is <= 3.000 um (2 cells/feature). Pass auto_size=True to scale the mesh down.


Configured 5 simulations
```

### Upload and start all jobs (non-blocking)

```python
# Upload and start all jobs without waiting
job_ids = []
for sim in sims:
    job_id = sim.run(wait=False, check_cache=True)
    job_ids.append(job_id)

print(f"Started {len(job_ids)} jobs: {job_ids}")
```

```text
Info    : Reading 'palace-sim-w2.0/palace.msh'...
Info    : 9203 nodes
Info    : 69098 elements
Info    : Done reading 'palace-sim-w2.0/palace.msh'
Info    : Reading 'palace-sim-w2.0/palace.msh'...
Info    : 9203 nodes
Info    : 69098 elements
Info    : Done reading 'palace-sim-w2.0/palace.msh'
Info    : Reading 'palace-sim-w2.0/palace.msh'...
Info    : 9203 nodes


Info    : 69098 elements


Info    : Done reading 'palace-sim-w2.0/palace.msh'


Cache hit: reusing job 01a163d4-0d48-7747-8d96-86cd2e5aaaec
Info    : Reading 'palace-sim-w6.0/palace.msh'...
Info    : 9838 nodes
Info    : 74046 elements
Info    : Done reading 'palace-sim-w6.0/palace.msh'
Info    : Reading 'palace-sim-w6.0/palace.msh'...
Info    : 9838 nodes
Info    : 74046 elements
Info    : Done reading 'palace-sim-w6.0/palace.msh'
Info    : Reading 'palace-sim-w6.0/palace.msh'...
Info    : 9838 nodes
Info    : 74046 elements


Info    : Done reading 'palace-sim-w6.0/palace.msh'
Cache hit: reusing job 01a163d4-15b9-72cb-b53b-83114592f5e0
Info    : Reading 'palace-sim-w10.0/palace.msh'...
Info    : 9784 nodes
Info    : 73826 elements


Info    : Done reading 'palace-sim-w10.0/palace.msh'
Info    : Reading 'palace-sim-w10.0/palace.msh'...
Info    : 9784 nodes
Info    : 73826 elements
Info    : Done reading 'palace-sim-w10.0/palace.msh'
Info    : Reading 'palace-sim-w10.0/palace.msh'...
Info    : 9784 nodes
Info    : 73826 elements
Info    : Done reading 'palace-sim-w10.0/palace.msh'


Cache hit: reusing job 01a163d5-9a12-74e5-b720-f60e1a6f2512
Info    : Reading 'palace-sim-w14.0/palace.msh'...
Info    : 9643 nodes
Info    : 72801 elements
Info    : Done reading 'palace-sim-w14.0/palace.msh'
Info    : Reading 'palace-sim-w14.0/palace.msh'...
Info    : 9643 nodes
Info    : 72801 elements
Info    : Done reading 'palace-sim-w14.0/palace.msh'
Info    : Reading 'palace-sim-w14.0/palace.msh'...
Info    : 9643 nodes
Info    : 72801 elements


Info    : Done reading 'palace-sim-w14.0/palace.msh'
Cache hit: reusing job 01a163d5-a2e2-751b-b843-aaf15ab33440
Info    : Reading 'palace-sim-w18.0/palace.msh'...
Info    : 9918 nodes
Info    : 74998 elements


Info    : Done reading 'palace-sim-w18.0/palace.msh'
Info    : Reading 'palace-sim-w18.0/palace.msh'...
Info    : 9918 nodes
Info    : 74998 elements
Info    : Done reading 'palace-sim-w18.0/palace.msh'
Info    : Reading 'palace-sim-w18.0/palace.msh'...
Info    : 9918 nodes
Info    : 74998 elements
Info    : Done reading 'palace-sim-w18.0/palace.msh'


Cache hit: reusing job 01a163d4-2d7d-7649-ba1e-8262b5e55ad7
Started 5 jobs: ['01a163d4-0d48-7747-8d96-86cd2e5aaaec', '01a163d4-15b9-72cb-b53b-83114592f5e0', '01a163d5-9a12-74e5-b720-f60e1a6f2512', '01a163d5-a2e2-751b-b843-aaf15ab33440', '01a163d4-2d7d-7649-ba1e-8262b5e55ad7']
```

### Wait for all jobs to complete

```python
import gsim

# Poll all jobs concurrently, download and parse results
results = gsim.wait_for_results(job_ids)
```

```text
Waiting for 5 jobs...
  palace-db9147a1                [####################] 100% complete     elapsed 0m 00s
  palace-e9106bbb                [####################] 100% complete     elapsed 0m 00s
  palace-c7a14914                [####################] 100% complete     elapsed 0m 00s
  palace-8859b12b                [####################] 100% complete     elapsed 0m 00s
  palace-941c9571                [####################] 100% complete     elapsed 0m 00s


Extracting results.tar.gz...
Downloaded 12 files to sim-data-palace-db9147a1


Extracting results.tar.gz...
Downloaded 12 files to sim-data-palace-e9106bbb


Extracting results.tar.gz...
Downloaded 12 files to sim-data-palace-c7a14914


Extracting results.tar.gz...
Downloaded 12 files to sim-data-palace-8859b12b


Extracting results.tar.gz...
Downloaded 12 files to sim-data-palace-941c9571
```

### Plot S11 and S21 comparison

```python
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))

for w, sp in zip(widths, results, strict=True):
    ax1.plot(sp.freq, sp.s11.db, label=f"w={w:.1f} um")
    ax2.plot(sp.freq, sp.s21.db, label=f"w={w:.1f} um")

ax1.set(
    xlabel="Frequency (GHz)", ylabel="|S11| (dB)", title="S11 — Return Loss vs Width"
)
ax1.legend()
ax1.grid(True)

ax2.set(
    xlabel="Frequency (GHz)", ylabel="|S21| (dB)", title="S21 — Insertion Loss vs Width"
)
ax2.legend()
ax2.grid(True)

plt.tight_layout()
```
