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.

Usage:

parfor variable_name = initial_value, final_value, step do
--simulation_code
parend

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
S:SetNumG(100)
-- 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
S:AddMaterial("Si",{12.5,0.001})
-- a fake material Si with real and imaginary parts of the dielectric constant being 12.5 and 0.001, respective
S:AddMaterial("Vacuum",{1,0})
-- vacuum
--Material Structure Definition
t_Si = 0.5
-- thickness of silicon laye,1um
S:AddLayer('AirAbove',0,'Vacuum')
S:AddLayer('Si',t_Si,'Si')
S:SetLayerPatternCircle('Si','Vacuum',{PC_period/2,PC_period/2},hole_radius)
S:AddLayer('Bottom',0,'Vacuum')
--Incident Wave Definition
S:SetExcitationPlanewave(
{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
S:SetFrequency(freq)
forward= S:GetPoyntingFlux('Bottom',0)
wavelength_nm = wavelength*1000
print(PC_period,hole_radius,wavelength_nm,forward)
parend