How to run your simulations faster using parfor

Parfor runs a loop on multiple CPUs to greatly increase throughput. Use it for simulations that require parameter sweeps. Parfor is smart and will optimize the number of CPUs for the most efficient runtime.


parfor variable_name = initial_value, final_value, step do

In this example, we will calculate the transmission spectra for a photonic crystal consisting of air holes in a dielectric slab. The wavelength ranges from 0.6 to 0.8 micrometer. We will also vary the period and hole sizes to study a range of different photonic crystals. The period varies from 0.4 to 0.5 micrometer with a step of 0.05 micrometer. The radius of the hole varies from 0.1 to 0.15 micrometer with a step of 0.01 micrometer.

The calculated transmission spectrum is shown by the figure below.

The Lua script for this case is shown below and can be downloaded here.

Run Example
parfor PC_period = 0.4,0.5,0.05 do
-- structural parameter scan for the peirod of the photonic crystal. set PC_period to a constant if you want to fix the period
parfor hole_radius = 0.1,0.15,0.01 do
-- structural parameter scan for the radius of the hole size
parfor wavelength=0.6,0.8,0.0005 do
-- wavelength scan, from 0.6 to 0.8 micron with a step of 0.01 micron
S = S4.NewSimulation()
S:SetLattice({PC_period,0}, {0,PC_period})
-- the vectors of the lattic of the photonic crystal
-- This number determines the accuracy: large number for better accuracy.
-- 50 - 200 is a good starting point for periods smaller than the wavelength.
-- Larger periods require higher numbers, but do not go far beyond 1000.
--Material Definition
-- a fake material Si with real and imaginary parts of the dielectric constant being 12.5 and 0.001, respective
-- vacuum
--Material Structure Definition
t_Si = 0.5
-- thickness of silicon laye,1um
--Incident Wave Definition
{0,0}, -- incidence angles (spherical coordinates: phi in [0,180], theta in [0,360])
{0,0}, -- s-polarization amplitude and phase (in degrees)
{1,0}) -- p-polarization amplitude and phase
--Calculation of Spectrum
freq = 1/wavelength
forward= S:GetPoyntingFlux('Bottom',0)
wavelength_nm = wavelength*1000