Created
October 23, 2018 17:29
-
-
Save ChristopherHogan/4f1e8b9e6a89c6e2bd607814cfe0593c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import time | |
from multiprocessing import Pool | |
import boto3 | |
import meep as mp | |
from meep.materials import Al | |
def get_results(fragment_stats, total_pixels, total_time): | |
assert len(fragment_stats) == 1 | |
stats = fragment_stats[0] | |
results = "{},{},{},{},{},{},{},{:.4f}\n" | |
return results.format(stats.num_anisotropic_eps_pixels, | |
stats.num_anisotropic_mu_pixels, | |
stats.num_nonlinear_pixels, | |
stats.num_susceptibility_pixels, | |
stats.num_nonzero_conductivity_pixels, | |
stats.num_dft_pixels, | |
total_pixels, | |
total_time) | |
def absorber_boundaries(s): | |
resolution = 40 | |
d = 1.0 | |
abs_layers = [mp.Absorber(thickness=d)] | |
cell_size = mp.Vector3(s+d, s+d, s+d) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
boundary_layers=abs_layers) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def aniso_medium(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
aniso_mat = mp.Medium(epsilon_diag=mp.Vector3(5.56984083, 9.44501035, 2.09631405), | |
epsilon_offdiag=mp.Vector3(1.20637481, 1.59461922, 1.41411461)) | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
default_material=aniso_mat) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def conductive_medium(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
eps_real = 6.5 | |
eps_imag = 0.2 | |
cond_mat = mp.Medium(epsilon=eps_real, D_conductivity=2*math.pi*eps_imag/eps_real) | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
default_material=cond_mat) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def dft_flux(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
fcen = 1.0 | |
df = 0.2 | |
sources = [mp.Source(mp.GaussianSource(fcen, df), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources) | |
nfreq = 21 | |
sim.add_flux(fcen, df, nfreq, mp.FluxRegion(center=mp.Vector3(z=0.4*s), | |
size=mp.Vector3(s, s))) | |
sim.add_flux(fcen, df, nfreq, mp.FluxRegion(center=mp.Vector3(z=-0.4*s), | |
size=mp.Vector3(s, s), weight=-1.0)) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def dispersive_medium(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
default_material=Al) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def empty(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def empty_complex_fields(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
force_complex_fields=True, | |
sources=sources) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def nonlinear_medium(s): | |
resolution = 40 | |
cell_size = mp.Vector3(s, s, s) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
nonlinear_mat = mp.Medium(index=1, chi3=0.1) | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
default_material=nonlinear_mat) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def pml_boundaries(s): | |
resolution = 40 | |
d = 1.0 | |
pml_layers = [mp.PML(thickness=d)] | |
cell_size = mp.Vector3(s+d, s+d, s+d) | |
sources = [mp.Source(mp.GaussianSource(1, 0.2), | |
component=mp.Ex, | |
center=mp.Vector3())] | |
start = time.time() | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
k_point=mp.Vector3(), | |
sources=sources, | |
boundary_layers=pml_layers) | |
sim.run(until_after_sources=100) | |
total_time = time.time() - start | |
return get_results(sim.fragment_stats, s**3 * resolution, total_time) | |
def write_csv(fn, res): | |
header = 'aniso_eps,aniso_mu,nonlinear,susceptibility,nonzer_cond,dft,total_pixels,total_time\n' | |
with open(fn, 'a') as f: | |
f.write(header) | |
for line in res: | |
f.write(line) | |
def put_s3_file(fn, bucket): | |
s3 = boto3.client('s3') | |
s3.upload_file(fn, bucket, fn) | |
print("Uploaded {} to S3".format(fn)) | |
if __name__ == '__main__': | |
bucket = 'hogan-fragment-stats' | |
pool = Pool(processes=4) | |
sizes = [1, 2, 3, 4] | |
fn = 'absorber_boundaries.csv' | |
res = pool.map(absorber_boundaries, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'aniso_medium.csv' | |
res = pool.map(aniso_medium, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'conductive_medium.csv' | |
res = pool.map(conductive_medium, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'dft_flux.csv' | |
res = pool.map(dft_flux, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'dispersive_medium.csv' | |
res = pool.map(dispersive_medium, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'empty.csv' | |
res = pool.map(empty, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'empty_complex_fields.csv' | |
res = pool.map(empty_complex_fields, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'nonlinear_medium.csv' | |
res = pool.map(nonlinear_medium, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) | |
fn = 'pml_boundaries.csv' | |
res = pool.map(pml_boundaries, sizes) | |
write_csv(fn, res) | |
put_s3_file(fn, bucket) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment