## MEEP and GDSFactory Installation Guide
This guide covers the installation of MEEP (with Python interface) and GDSFactory for parametric photonics simulation in a WSL environment, including all key dependencies and common troubleshooting tips.
### 1. Prerequisites: WSL and Conda
Ensure that Windows Subsystem for Linux (WSL) and Conda (Miniconda/Anaconda) are installed and ready to use. If not, follow the official instructions:
- [WSL installation guide](https://docs.microsoft.com/en-us/windows/wsl/install)
- [Miniconda installation guide](https://docs.conda.io/en/latest/miniconda.html)
### 2. Change to Your Working Directory
For example, to work on D drive:
```bash
cd /mnt/d/MEEP
```
### 3. Environment Setup with Conda-forge and pip
Create an environment YAML file (`env.yml`) with the following content:
```yaml
name: photonics-mpi
channels:
- conda-forge
dependencies:
- python=3.11
- numpy=1.26 # NOTE: Do not upgrade to numpy 2.x (see below)
- pymeep
- pymeep-extras
- h5py
- matplotlib
- gdsfactory
- pip
- pip:
- gplugins
- femwell
- skfem
```
**Note:**
- `numpy=1.26` is required for pymeep and some scientific libraries. Newer numpy 2.x versions are NOT compatible with most C/C++ extension packages as of 2024. Do not upgrade numpy beyond 1.x in this environment.
- The latest versions of `gplugins`, `femwell`, and `skfem` are best installed via pip for compatibility.
#### Create and Activate the Environment
```bash
conda env create -f env.yml
conda activate photonics-mpi
```
#### Verify Installation
```bash
python -c "import numpy as np; print(np.__version__)"
python -c "import meep; print(meep.__version__)"
python -c "import gdsfactory; print(gdsfactory.__version__)"
python -c "import gplugins.gmeep as gmeep; print('gplugins.gmeep OK')"
```
Check all environments with:
```bash
conda env list
```
### 4. Gmsh and Visualization Dependencies (WSL)
Some GDSFactory/FEM mesh workflows require additional Linux system libraries. Install all recommended packages in WSL:
```bash
sudo apt update
sudo apt install -y \
libxinerama1 \
libxft2 \
libxrender1 \
libxi6 \
libxext6 \
libxrandr2 \
libxcursor1 \
libgl1 \
libglu1-mesa \
libx11-6 \
libsm6 \
libice6 \
libfontconfig1 \
libfreetype6 \
libxcb1
```
Verify gmsh dependency is OK:
```bash
python -c "import gmsh; gmsh.initialize(); gmsh.finalize(); print('Gmsh OK')"
```
You should see:
```
Gmsh OK
```
If not, carefully read the missing library message and install any additional missing packages.
### 5. Register Jupyter Kernel (Optional but Recommended)
You may want to run Jupyter Notebooks using this environment. First activate your environment:
```bash
conda activate photonics-mpi
```
Install Jupyter Notebook (if not already installed):
```bash
conda install notebook ipykernel
```
Register your environment as a Jupyter kernel:
```bash
python -m ipykernel install --user --name photonics-mpi --display-name "Photonics MPI"
```
Now, in any Jupyter Notebook/Lab (from any base environment), you can select the "Photonics MPI" kernel.
### 6. Start Working
Switch to your working directory:
```bash
cd /mnt/d/MEEP_project
jupyter notebook
```
> [!Tip]
> Always ensure output folders (such as `data/`, `output/`, etc.) exist before running scripts that save files. Use `os.makedirs("data", exist_ok=True)` in Python if needed.
Deactivate the environment after finishing work:
```bash
conda deactivate
```
### 7. Troubleshooting & Notes
- If you get `ImportError` or `OSError` related to missing `.so` libraries, rerun the `sudo apt install ...` for missing system libraries and restart your terminal.
- If you see `numpy.core.multiarray failed to import`, ensure numpy is **1.x** in your environment.
- If you update any critical packages (pymeep, numpy, gplugins), always re-check your workflow; breaking changes are possible with rapidly updating scientific Python tools.
- If you wish to use KLayout GUI for GDS visualization, install KLayout separately on Windows ([https://www.klayout.de/](https://www.klayout.de/)) and set up the GDS output directory as a shared mount.
- For batch jobs or parallel MEEP simulation, MPI is supported out of the box if installed with pymeep from conda-forge. See the official [Meep MPI documentation](https://meep.readthedocs.io/en/latest/Parallel_Computing/) for advanced usage.
---
This workflow enables parametric photonic device design, modal analysis, and FDTD simulation under a unified Python environment. All tools and scripts are ready for automation, batch simulation, and reproducible research.