Created
October 29, 2018 21:11
-
-
Save ChristopherHogan/ac8a63cdf0a5554107037ca2d5dda87f to your computer and use it in GitHub Desktop.
Run random simulations
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 argparse | |
from time import time | |
from math import pi | |
from random import randrange | |
import boto3 | |
import meep as mp | |
from meep.materials import Al | |
def write_csv(fn, res): | |
# header = ('aniso_eps,' + | |
# 'aniso_mu,' + | |
# 'nonlinear,' + | |
# 'susceptibility,' + | |
# 'nonzero_cond,' + | |
# 'dft,' + | |
# 'total_pixels,' + | |
# 'pml,' + | |
# 'absorber,' + | |
# 't_connecting,' + | |
# 't_stepping,' + | |
# 't_boundaries,' + | |
# 't_mpi,' + | |
# 't_field_output,' + | |
# 't_fourier_transforming,' + | |
# 't_other,' + | |
# 'total_time\n') | |
with open(fn, 'w') as f: | |
# f.write(header) | |
for line in res: | |
f.write(line) | |
def put_s3_file(fn, bucket, destdir=''): | |
s3 = boto3.client('s3') | |
s3.upload_file(fn, bucket, destdir + fn) | |
print("Uploaded {} to S3".format(fn)) | |
def has_boundary_layer(sim, kind): | |
if sim.boundary_layers: | |
if isinstance(sim.boundary_layers[0], kind): | |
return 1 | |
return 0 | |
def has_pml(sim): | |
has_boundary_layer(sim, mp.PML) and not has_boundary_layer(sim, mp.Absorber) | |
def has_absorber(sim): | |
has_boundary_layer(sim, mp.Absorber) | |
def get_results(sim, total_pixels, total_time): | |
assert len(sim.fragment_stats) == 1 | |
stats = sim.fragment_stats[0] | |
time_sinks = [sim.fields.time_spent_on(x) for x in range(7)] | |
results = "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{total_time:.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, | |
has_pml(sim), | |
has_absorber(sim), | |
*time_sinks, | |
total_time=total_time) | |
def get_random_element(lst): | |
idx = randrange(len(lst)) | |
return lst[idx] | |
def main(args): | |
resolution = 30 | |
bl = get_random_element([[mp.Absorber(thickness=1)], [mp.PML(thickness=1)], []]) | |
s = get_random_element([2, 3, 4]) if bl else get_random_element([1, 2, 3, 4]) | |
d = 1 if bl else 0 | |
cell_size = mp.Vector3(s+d, s+d, s+d) | |
fcen = 1 | |
df = 0.2 | |
sources = [mp.Source(mp.GaussianSource(fcen, df), component=mp.Ex, center=mp.Vector3())] | |
eps_offdiag = get_random_element([mp.Vector3(1.20637481), | |
mp.Vector3(1.20637481, 1.59461922), | |
mp.Vector3(1.20637481, 1.59461922, 1.41411461)]) | |
aniso_mat = mp.Medium(epsilon_diag=mp.Vector3(5.56984083, 9.44501035, 2.09631405), | |
epsilon_offdiag=eps_offdiag) | |
nonlinear_mat = mp.Medium(index=1, chi3=0.1) | |
eps_real = 6.5 | |
eps_imag = 0.2 | |
cond_mat = mp.Medium(epsilon=eps_real, D_conductivity=2 * pi * eps_imag / eps_real) | |
mat = get_random_element([aniso_mat, Al, nonlinear_mat, cond_mat, mp.air]) | |
flux_region = get_random_element([mp.FluxRegion(center=mp.Vector3(z=0.4*s), size=mp.Vector3(s, s)), | |
mp.Near2FarRegion(center=mp.Vector3(z=0.4*s), size=mp.Vector3(s, s)), | |
mp.FluxRegion(center=mp.Vector3(z=-0.4*s), size=mp.Vector3(s, s), weight=-1.0), | |
None]) | |
sim = mp.Simulation(resolution=resolution, | |
cell_size=cell_size, | |
boundary_layers=bl, | |
sources=sources, | |
k_point=mp.Vector3(), | |
default_material=mat) | |
if flux_region: | |
nfreq = get_random_element(list(range(1, 22))) | |
if isinstance(flux_region, mp.FluxRegion): | |
sim.add_flux(fcen, df, nfreq, flux_region) | |
elif isinstance(flux_region, mp.Near2FarRegion): | |
sim.add_near2far(fcen, df, nfreq, flux_region) | |
start = time() | |
sim.run(until_after_sources=100) | |
total_time = time() - start | |
res = get_results(sim, (s * resolution)**3, total_time) | |
write_csv(args.filename, res) | |
bucket = 'hogan-fragment-stats' | |
put_s3_file(args.filename, bucket, 'data/') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', '--filename', help="File name for fragment data") | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment