Skip to content

Commit

Permalink
blacken notebooks, add flake8 config and script for adding type annot…
Browse files Browse the repository at this point in the history
…ations (#2177)
  • Loading branch information
joamatab authored Jul 30, 2022
1 parent 442b921 commit 5751466
Show file tree
Hide file tree
Showing 44 changed files with 4,277 additions and 2,666 deletions.
24 changes: 24 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[flake8]
max-line-length = 88
max-complexity = 57
select = B,C,E,F,W,T4,B9
ignore = E501, E503, E722, W503, W503, E203, B950, B305, B018, B902, B020

extend-ignore =
RST303

exclude =
.git,
__pycache__,
lib/,
docs/source/conf.py,
build,
dist,
.ipynb_checkpoints,
.tox,
extra,
deprecated,
gdslib,
.tox,
.mypy_cache,
venv
11 changes: 11 additions & 0 deletions add_type_hints.xonsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
from tqdm.auto import tqdm


py.test --monkeytype-output=./monkeytype.sqlite3

for i, m in enumerate(tqdm($(monkeytype list-modules).split('\n'))):
if m.startswith(''):
print(i, m)
monkeytype stub @(m)
monkeytype apply @(m)
Binary file added monkeytype.sqlite3
Binary file not shown.
161 changes: 97 additions & 64 deletions python/examples/3rd-harm-1d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"import meep as mp\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"\n",
"%matplotlib notebook"
]
},
Expand All @@ -51,12 +52,12 @@
"metadata": {},
"outputs": [],
"source": [
"sz = 100 # size of cell in z direction\n",
"fcen = 1 / 3.0 # center frequency of source\n",
"df = fcen / 20.0 # frequency width of source\n",
"amp = 1 # amplitude of source\n",
"k = 10**-5 # Kerr susceptibility\n",
"dpml = 1.0 # PML thickness"
"sz = 100 # size of cell in z direction\n",
"fcen = 1 / 3.0 # center frequency of source\n",
"df = fcen / 20.0 # frequency width of source\n",
"amp = 1 # amplitude of source\n",
"k = 10**-5 # Kerr susceptibility\n",
"dpml = 1.0 # PML thickness"
]
},
{
Expand Down Expand Up @@ -109,8 +110,12 @@
"metadata": {},
"outputs": [],
"source": [
"sources = mp.Source(mp.GaussianSource(fcen, fwidth=df), component=mp.Ex,\n",
" center=mp.Vector3(0, 0, -0.5*sz + dpml), amplitude=amp)"
"sources = mp.Source(\n",
" mp.GaussianSource(fcen, fwidth=df),\n",
" component=mp.Ex,\n",
" center=mp.Vector3(0, 0, -0.5 * sz + dpml),\n",
" amplitude=amp,\n",
")"
]
},
{
Expand All @@ -130,16 +135,22 @@
"fmin = fcen / 2.0\n",
"fmax = fcen * 4\n",
"\n",
"sim = mp.Simulation(cell_size=cell,\n",
" geometry=[],\n",
" sources=[sources],\n",
" boundary_layers=[pml_layers],\n",
" default_material=default_material,\n",
" resolution=resolution,\n",
" dimensions=dimensions)\n",
"sim = mp.Simulation(\n",
" cell_size=cell,\n",
" geometry=[],\n",
" sources=[sources],\n",
" boundary_layers=[pml_layers],\n",
" default_material=default_material,\n",
" resolution=resolution,\n",
" dimensions=dimensions,\n",
")\n",
"\n",
"trans = sim.add_flux(0.5 * (fmin + fmax), fmax - fmin, nfreq,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5*sz - dpml - 0.5)))"
"trans = sim.add_flux(\n",
" 0.5 * (fmin + fmax),\n",
" fmax - fmin,\n",
" nfreq,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5)),\n",
")"
]
},
{
Expand Down Expand Up @@ -178,8 +189,11 @@
}
],
"source": [
"sim.run(until_after_sources=mp.stop_when_fields_decayed(\n",
" 50, mp.Ex, mp.Vector3(0, 0, 0.5*sz - dpml - 0.5), 1e-6))"
"sim.run(\n",
" until_after_sources=mp.stop_when_fields_decayed(\n",
" 50, mp.Ex, mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5), 1e-6\n",
" )\n",
")"
]
},
{
Expand Down Expand Up @@ -212,10 +226,10 @@
"spectra = mp.get_fluxes(trans)\n",
"\n",
"plt.figure(dpi=150)\n",
"plt.semilogy(freqs,spectra)\n",
"plt.semilogy(freqs, spectra)\n",
"plt.grid(True)\n",
"plt.xlabel('Frequency')\n",
"plt.ylabel('Transmitted Power (a.u.)')\n",
"plt.xlabel(\"Frequency\")\n",
"plt.ylabel(\"Transmitted Power (a.u.)\")\n",
"plt.show()"
]
},
Expand All @@ -234,40 +248,55 @@
"metadata": {},
"outputs": [],
"source": [
"def run_chi3(k_pow,amp=1):\n",
" k = 10**k_pow \n",
"def run_chi3(k_pow, amp=1):\n",
" k = 10**k_pow\n",
" default_material = mp.Medium(index=1, chi3=k)\n",
" \n",
" sources = mp.Source(mp.GaussianSource(fcen, fwidth=df), component=mp.Ex,\n",
" center=mp.Vector3(0, 0, -0.5*sz + dpml), amplitude=amp)\n",
" \n",
" sim = mp.Simulation(cell_size=cell,\n",
" geometry=[],\n",
" sources=[sources],\n",
" boundary_layers=[pml_layers],\n",
" default_material=default_material,\n",
" resolution=resolution,\n",
" dimensions=dimensions)\n",
"\n",
" trans = sim.add_flux(0.5 * (fmin + fmax), fmax - fmin, nfreq,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5*sz - dpml - 0.5)))\n",
" \n",
" sources = mp.Source(\n",
" mp.GaussianSource(fcen, fwidth=df),\n",
" component=mp.Ex,\n",
" center=mp.Vector3(0, 0, -0.5 * sz + dpml),\n",
" amplitude=amp,\n",
" )\n",
"\n",
" sim = mp.Simulation(\n",
" cell_size=cell,\n",
" geometry=[],\n",
" sources=[sources],\n",
" boundary_layers=[pml_layers],\n",
" default_material=default_material,\n",
" resolution=resolution,\n",
" dimensions=dimensions,\n",
" )\n",
"\n",
" trans = sim.add_flux(\n",
" 0.5 * (fmin + fmax),\n",
" fmax - fmin,\n",
" nfreq,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5)),\n",
" )\n",
"\n",
" # Single frequency point at omega\n",
" trans1 = sim.add_flux(fcen, 0, 1,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5*sz - dpml - 0.5)))\n",
" trans1 = sim.add_flux(\n",
" fcen, 0, 1, mp.FluxRegion(mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5))\n",
" )\n",
"\n",
" # Singel frequency point at 3omega\n",
" trans3 = sim.add_flux(3 * fcen, 0, 1,\n",
" mp.FluxRegion(mp.Vector3(0, 0, 0.5*sz - dpml - 0.5)))\n",
" \n",
" sim.run(until_after_sources=mp.stop_when_fields_decayed(\n",
" 50, mp.Ex, mp.Vector3(0, 0, 0.5*sz - dpml - 0.5), 1e-6))\n",
" \n",
" trans3 = sim.add_flux(\n",
" 3 * fcen, 0, 1, mp.FluxRegion(mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5))\n",
" )\n",
"\n",
" sim.run(\n",
" until_after_sources=mp.stop_when_fields_decayed(\n",
" 50, mp.Ex, mp.Vector3(0, 0, 0.5 * sz - dpml - 0.5), 1e-6\n",
" )\n",
" )\n",
"\n",
" omega_flux = mp.get_fluxes(trans1)\n",
" omega3_flux = mp.get_fluxes(trans3)\n",
" freqs = mp.get_flux_freqs(trans)\n",
" spectra = mp.get_fluxes(trans)\n",
" \n",
"\n",
" return freqs, spectra, omega_flux, omega3_flux"
]
},
Expand Down Expand Up @@ -355,13 +384,13 @@
}
],
"source": [
"k_pow = [-3,-2,-1,0]\n",
"k_pow = [-3, -2, -1, 0]\n",
"freqs = []\n",
"spectra = []\n",
"for k_iter in k_pow:\n",
" freqs_iter, spectra_iter, omega_flux, omega3_flux = run_chi3(k_iter)\n",
" spectra.append(spectra_iter)\n",
" freqs = freqs_iter # Each iteration will simulate over the same frequencies, so just remember the last set."
" freqs = freqs_iter # Each iteration will simulate over the same frequencies, so just remember the last set."
]
},
{
Expand All @@ -384,10 +413,10 @@
],
"source": [
"plt.figure(dpi=150)\n",
"plt.semilogy(freqs,np.array(spectra).T)\n",
"plt.semilogy(freqs, np.array(spectra).T)\n",
"plt.grid(True)\n",
"plt.xlabel('Frequency')\n",
"plt.ylabel('Transmitted Power (a.u.)')\n",
"plt.xlabel(\"Frequency\")\n",
"plt.ylabel(\"Transmitted Power (a.u.)\")\n",
"plt.legend([\"$\\chi^{{(3)}} = 10^{{{}}}$\".format(i) for i in k_pow])\n",
"plt.show()"
]
Expand Down Expand Up @@ -432,7 +461,7 @@
],
"source": [
"_, _, omega_flux_cal, omega3_flux_cal = run_chi3(-16)\n",
"print(\"Omega: {}, 3Omega: {}\".format(omega_flux_cal[0],omega3_flux_cal[0]))"
"print(\"Omega: {}, 3Omega: {}\".format(omega_flux_cal[0], omega3_flux_cal[0]))"
]
},
{
Expand Down Expand Up @@ -787,7 +816,7 @@
}
],
"source": [
"pts = np.linspace(-6,0,20)\n",
"pts = np.linspace(-6, 0, 20)\n",
"_, _, omega_psd, omega3_psd = zip(*[run_chi3(k_iter) for k_iter in pts])"
]
},
Expand All @@ -810,15 +839,15 @@
}
],
"source": [
"quad = (10 ** pts) ** 2\n",
"quad = (10**pts) ** 2\n",
"\n",
"plt.figure(dpi=150)\n",
"plt.loglog(10 ** pts,np.array(omega_psd)/omega_flux_cal[0],'o-',label='$\\omega$')\n",
"plt.loglog(10 ** pts,np.array(omega3_psd)/omega_flux_cal[0],'o-',label='$3\\omega$')\n",
"plt.loglog(10**pts,quad,'k',label='quadratic line')\n",
"plt.loglog(10**pts, np.array(omega_psd) / omega_flux_cal[0], \"o-\", label=\"$\\omega$\")\n",
"plt.loglog(10**pts, np.array(omega3_psd) / omega_flux_cal[0], \"o-\", label=\"$3\\omega$\")\n",
"plt.loglog(10**pts, quad, \"k\", label=\"quadratic line\")\n",
"plt.grid(True)\n",
"plt.xlabel('$\\chi^{(3)}$')\n",
"plt.ylabel('Transmission/ Incident Power')\n",
"plt.xlabel(\"$\\chi^{(3)}$\")\n",
"plt.ylabel(\"Transmission/ Incident Power\")\n",
"plt.legend()\n",
"plt.show()"
]
Expand Down Expand Up @@ -879,11 +908,15 @@
}
],
"source": [
"_, _, omega_flux_1, omega3_flux_1 = run_chi3(-3,1)\n",
"_, _, omega_flux_2, omega3_flux_2 = run_chi3(-6,10)\n",
"_, _, omega_flux_1, omega3_flux_1 = run_chi3(-3, 1)\n",
"_, _, omega_flux_2, omega3_flux_2 = run_chi3(-6, 10)\n",
"\n",
"print('-------------------------------')\n",
"print(\"Difference between powers: {}%\".format(abs(omega3_flux_1[0]-omega3_flux_2[0])/omega3_flux_1[0] * 100))"
"print(\"-------------------------------\")\n",
"print(\n",
" \"Difference between powers: {}%\".format(\n",
" abs(omega3_flux_1[0] - omega3_flux_2[0]) / omega3_flux_1[0] * 100\n",
" )\n",
")"
]
},
{
Expand Down
Loading

0 comments on commit 5751466

Please sign in to comment.