-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arrayslice #96
Merged
Merged
Arrayslice #96
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
dfc4edf
trying mkdocs stuff on master branch as readthedocs seems to have tro…
HomerReid 59dface
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 59b2315
synced with stevengj/master
HomerReid 7b4a40a
sync with stevenj master
HomerReid 73c728a
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid eb70f1d
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 2661470
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 6b81dd9
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid d6a1c33
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid bcaec1e
updates
HomerReid d3e57d5
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 9db22ba
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 25f1abd
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 62b6020
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 49d7b20
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 0f9293b
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 47090d5
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 3348c4a
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 0a395dc
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid c68d2d7
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid bdb08d9
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 82893b7
Merge branch 'master' of https://github.com/StevenGJ/meep
HomerReid 016a1a5
added src/array_slice.cpp and tests/array_slice_test.cpp
HomerReid 8501203
added src/array_slice.cpp and tests/array_slice_test.cpp
HomerReid 94efcd6
updates
HomerReid 6ab9308
updates
HomerReid 8922e9f
added real_valued and complex_valued array_slice entry points and add…
HomerReid be055bc
revamped tests/array_slice_test to eliminate reference to h5 files
HomerReid beafa7a
updates
HomerReid 6a46db9
updates
HomerReid 320d6d3
added libmeepgeom/cavity-arrayslice_ll.cpp
HomerReid 826fbf3
added libmeepgeom/cavity-arrayslice_ll.cpp
HomerReid da02fd3
updates to libmeepgeom/cavity-arrayslice.cpp
HomerReid 67e11f5
updates to libmeepgeom/array_slice_test.cpp
HomerReid f360be0
added python/examples/cavity_arrayslice.py
HomerReid 9ae514b
finalized python/examples/cavity_arrayslice.py and typemaps in python…
HomerReid 4a0f847
updates to tests/array_slice_test.cpp
HomerReid 4f27692
added libmeepgeom/array-slice-ll.cpp test
HomerReid 09d16f2
updates to libmeepgeom/cavity-arrayslice.cpp
HomerReid fc5dc40
updates to Makefile.am
HomerReid 6f6b1c1
updates to libmeepgeom/array-slice-ll.cpp test code
HomerReid 4a3e9e5
added libmeepgeom/array-slice-ll.h5 binary reference data file for ar…
HomerReid a5b97b1
update .travis.yml
HomerReid e94a272
try revising .travis.yml to avoid annoying link error that has reappe…
HomerReid b49c778
fixed missing file
HomerReid f936927
fixed missing file
HomerReid bc3710b
update .travis.yml in attempt to decipher inscrutable travis test fai…
HomerReid e6e6a93
added array-slice-ll test to libmeepgeom tests
HomerReid 34d39c7
removed array-slice-ll test from libmeepgeom
HomerReid c8a0671
removed array-slice-ll from libmeepgeom tests
HomerReid 9bd18bd
deleted .travis.yml and replaced it with version from master branch
HomerReid 114b0cc
added support for derived_components in array_slice
HomerReid b5e3c84
rebased; restored array-slice-ll test in libmeepgeom
HomerReid 7864a49
added array-slice-ll to CHECKS in libmeepgeom/Makefile.am
HomerReid 2bc6f3e
Merge branch 'master' of https://github.com/StevenGJ/meep into arrays…
HomerReid 3359a6a
pulled updated .travis.yml
HomerReid 6c9f61a
updated libmeepgeom/array-slice-ll to find data file correctly in out…
HomerReid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <complex> | ||
|
||
#include "meep.hpp" | ||
|
||
#include "ctl-math.h" | ||
#include "ctlgeom.h" | ||
|
||
#include "meepgeom.hpp" | ||
|
||
#ifndef DATADIR | ||
#define DATADIR "./" | ||
#endif | ||
|
||
using namespace meep; | ||
|
||
typedef std::complex<double> cdouble; | ||
|
||
vector3 v3(double x, double y=0.0, double z=0.0) | ||
{ | ||
vector3 v; | ||
v.x=x; v.y=y; v.z=z; | ||
return v; | ||
} | ||
|
||
// passthrough field function | ||
cdouble default_field_function(const cdouble *fields, | ||
const vec &loc, void *data_) | ||
{ | ||
(void) loc; // unused | ||
(void) data_; // unused | ||
return fields[0]; | ||
} | ||
|
||
/***************************************************************/ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
#define RELTOL 1.0e-6 | ||
double Compare(double *d1, double *d2, int N, const char *Name) | ||
{ | ||
double Norm1=0.0, Norm2=0.0, NormDelta=0.0; | ||
for(int n=0; n<N; n++) | ||
{ Norm1 += d1[n]*d1[n]; | ||
Norm2 += d2[n]*d2[n]; | ||
NormDelta += (d1[n]-d2[n])*(d1[n]-d2[n]); | ||
}; | ||
Norm1=sqrt(Norm1); | ||
Norm2=sqrt(Norm2); | ||
NormDelta=sqrt(NormDelta); | ||
double RelErr = NormDelta / (0.5*(Norm1+Norm2)); | ||
if (RelErr > RELTOL) | ||
abort("fail: rel error in %s data = %e\n",Name,RelErr); | ||
return RelErr; | ||
} | ||
|
||
double Compare(cdouble *d1, cdouble *d2, int N, const char *Name) | ||
{ | ||
double Norm1=0.0, Norm2=0.0, NormDelta=0.0; | ||
for(int n=0; n<N; n++) | ||
{ Norm1 += norm(d1[n]); | ||
Norm2 += norm(d2[n]); | ||
NormDelta += norm(d1[n]-d2[n]); | ||
}; | ||
Norm1=sqrt(Norm1); | ||
Norm2=sqrt(Norm2); | ||
NormDelta=sqrt(NormDelta); | ||
double RelErr = NormDelta / (0.5*(Norm1+Norm2)); | ||
if (RelErr > RELTOL) | ||
abort("fail: rel error in %s data = %e\n",Name,RelErr); | ||
return RelErr; | ||
} | ||
|
||
/***************************************************************/ | ||
/* dummy material function needed to pass to structure( ) */ | ||
/* constructor as a placeholder before we can call */ | ||
/* set_materials_from_geometry */ | ||
/***************************************************************/ | ||
double dummy_eps(const vec &) { return 1.0; } | ||
|
||
/***************************************************************/ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
void usage(char *progname) | ||
{ master_printf("usage: %s [options]\n",progname); | ||
master_printf("options: \n"); | ||
master_printf(" --use-symmetry use geometric symmetries\n"); | ||
master_printf(" --write-files write reference data files\n"); | ||
abort(); | ||
} | ||
|
||
/***************************************************************/ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
int main(int argc, char *argv[], char **envp) | ||
{ | ||
initialize mpi(argc, argv); | ||
|
||
/***************************************************************/ | ||
/* parse command-line options **********************************/ | ||
/***************************************************************/ | ||
bool use_symmetry=false; | ||
bool write_files=false; | ||
for(int narg=1; narg<argc; narg++) | ||
{ if ( argv[narg]==0 ) | ||
continue; | ||
if (!strcasecmp(argv[narg],"--use-symmetry") ) | ||
{ use_symmetry=true; | ||
master_printf("Using symmetry.\n"); | ||
} | ||
else if (!strcasecmp(argv[narg],"--write-files") ) | ||
{ write_files=true; | ||
master_printf("writing HDF5 data files"); | ||
} | ||
else | ||
{ master_printf("unknown command-line option %s (aborting)",argv[narg]); | ||
usage(argv[0]); | ||
}; | ||
}; | ||
|
||
/***************************************************************/ | ||
/* initialize geometry, similar to holey_wvg_cavity **********/ | ||
/***************************************************************/ | ||
double eps=13.0; // dielectric constant of waveguide | ||
double w=1.2; // width of waveguide | ||
double r=0.36; // radius of holes | ||
double d=1.4; // defect spacing (ordinary spacing = 1) | ||
int N=3; // number of holes on either side of defect | ||
double sy=6.0; // size of cell in y direction (perpendicular to wvg.) | ||
double pad=2.0; // padding between last hole and PML edge | ||
double dpml=1.0; // PML thickness | ||
double sx = 2.0*(pad + dpml + N) + d -1.0; // size of cell in x dir | ||
double resolution=20.0; | ||
geometry_lattice.size.x=sx; | ||
geometry_lattice.size.y=sy; | ||
geometry_lattice.size.z=0.0; | ||
grid_volume gv = voltwo(sx, sy, resolution); | ||
gv.center_origin(); | ||
symmetry sym = use_symmetry ? -mirror(Y,gv) : identity(); | ||
structure the_structure(gv, dummy_eps, pml(dpml), sym); | ||
material_type vacuum = meep_geom::vacuum; | ||
material_type dielectric = meep_geom::make_dielectric(eps); | ||
geometric_object objects[7]; | ||
vector3 origin = v3(0.0, 0.0, 0.0); | ||
vector3 xhat = v3(1.0, 0.0, 0.0); | ||
vector3 yhat = v3(0.0, 1.0, 0.0); | ||
vector3 zhat = v3(0.0, 0.0, 1.0); | ||
vector3 size = v3(ENORMOUS, w, ENORMOUS); | ||
double x0 = 0.5*d; | ||
double deltax = 1.0; | ||
double height = ENORMOUS; | ||
objects[0] = make_block(dielectric, origin, xhat, yhat, zhat, size); | ||
int no=1; | ||
for(int n=0; n<N; n++) | ||
{ vector3 center=v3(x0 + n*deltax, 0.0, 0.0); | ||
objects[no++] = make_cylinder(vacuum, center, r, height, zhat); | ||
}; | ||
for(int n=0; n<N; n++) | ||
{ vector3 center=v3(-x0 - n*deltax, 0.0, 0.0); | ||
objects[no++] = make_cylinder(vacuum, center, r, height, zhat); | ||
}; | ||
geometric_object_list g={ no, objects }; | ||
meep_geom::set_materials_from_geometry(&the_structure, g); | ||
fields f(&the_structure); | ||
|
||
/***************************************************************/ | ||
/* add source and timestep until source has finished (no later)*/ | ||
/***************************************************************/ | ||
double fcen = 0.25; // pulse center frequency | ||
double df = 0.2; // pulse width (in frequency) | ||
gaussian_src_time src(fcen, df); | ||
component src_cmpt = Hz; | ||
f.add_point_source(src_cmpt, src, vec(0.0, 0.0)); | ||
while( f.round_time() < f.last_source_time()) | ||
f.step(); | ||
|
||
/***************************************************************/ | ||
/***************************************************************/ | ||
/***************************************************************/ | ||
double xMin = -0.25*sx, xMax=+0.25*sx; | ||
double yMin = -0.15*sy, yMax=+0.15*sy; | ||
volume v1d( vec(xMin, 0.0), vec(xMax, 0.0) ); | ||
volume v2d( vec(xMin, yMin), vec(xMax, yMax) ); | ||
|
||
int rank, dims1D[1], dims2D[2]; | ||
cdouble *file_slice1d=0; | ||
double *file_slice2d=0; | ||
|
||
#define H5FILENAME DATADIR"array-slice-ll" | ||
#define NX 126 | ||
#define NY 38 | ||
if (write_files) | ||
{ | ||
h5file *file = f.open_h5file(H5FILENAME); | ||
f.output_hdf5(Hz, v1d, file); | ||
f.output_hdf5(Sy, v2d, file); | ||
master_printf("Wrote binary data to file %s.h5\n",H5FILENAME); | ||
delete file; | ||
exit(0); | ||
} | ||
else | ||
{ | ||
// | ||
// read 1D and 2D array-slice data from HDF5 file | ||
// | ||
h5file *file = f.open_h5file(H5FILENAME, h5file::READONLY); | ||
double *rdata = file->read("hz.r", &rank, dims1D, 1); | ||
if (rank!=1 || dims1D[0]!=NX) | ||
abort("failed to read 1D data(hz.r) from file %s.h5",H5FILENAME); | ||
double *idata = file->read("hz.i", &rank, dims1D, 1); | ||
if (rank!=1 || dims1D[0]!=NX) | ||
abort("failed to read 1D data(hz.i) from file %s.h5",H5FILENAME); | ||
file_slice1d = new cdouble[dims1D[0]]; | ||
for(int n=0; n<dims1D[0]; n++) | ||
file_slice1d[n] = cdouble(rdata[n], idata[n]); | ||
delete[] rdata; | ||
delete[] idata; | ||
|
||
file_slice2d = file->read("sy", &rank, dims2D, 2); | ||
if (rank!=2 || dims2D[0]!=NX || dims2D[1]!=NY) | ||
abort("failed to read 2D reference data from file %s.h5",H5FILENAME); | ||
delete file; | ||
|
||
// | ||
// generate 1D and 2D array slices and compare to | ||
// data read from file | ||
// | ||
rank=f.get_array_slice_dimensions(v1d, dims1D); | ||
if (rank!=1 || dims1D[0]!=NX) | ||
abort("incorrect dimensions for 1D slice"); | ||
cdouble *slice1d=f.get_complex_array_slice(v1d, Hz); | ||
double RelErr1D=Compare(slice1d, file_slice1d, NX, "Hz_1d"); | ||
master_printf("1D: rel error %e\n",RelErr1D); | ||
|
||
rank=f.get_array_slice_dimensions(v2d, dims2D); | ||
if (rank!=2 || dims2D[0]!=NX || dims2D[1]!=NY) | ||
abort("incorrect dimensions for 2D slice"); | ||
double *slice2d=f.get_array_slice(v2d, Sy); | ||
double RelErr2D=Compare(slice2d, file_slice2d, NX*NY, "Sy_2d"); | ||
master_printf("2D: rel error %e\n",RelErr2D); | ||
|
||
}; // if (write_files) ... else ... | ||
|
||
return 0; | ||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import unittest | ||
import meep as mp | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
################################################## | ||
# set up the geometry | ||
################################################## | ||
eps = 13 | ||
w = 1.2 | ||
r = 0.36 | ||
d = 1.4 | ||
N = 3 | ||
sy = 6 | ||
pad = 2 | ||
dpml = 1 | ||
sx = (2 * (pad + dpml + N)) + d - 1 | ||
fcen = 0.25 | ||
df = 0.2 | ||
nfreq = 500 | ||
|
||
cell = mp.Vector3(sx, sy, 0) | ||
|
||
blk = mp.Block(size=mp.Vector3(1e20, w, 1e20), | ||
material=mp.Medium(epsilon=eps)) | ||
|
||
geometry = [blk] | ||
|
||
for i in range(3): | ||
geometry.append(mp.Cylinder(r, center=mp.Vector3(d / 2 + i))) | ||
|
||
for i in range(3): | ||
geometry.append(mp.Cylinder(r, center=mp.Vector3(d / -2 - i))) | ||
|
||
sim = mp.Simulation(cell_size=cell, | ||
geometry=geometry, | ||
sources=[], | ||
boundary_layers=[mp.pml(dpml)], | ||
resolution=20) | ||
|
||
################################################## | ||
# add sources | ||
################################################## | ||
sim.sources = [mp.Source(mp.GaussianSource(fcen, fwidth=df), | ||
mp.Hz, mp.Vector3())] | ||
|
||
#sim.symmetries = [mp.Mirror(mp.Y, phase=-1), mp.Mirror(mp.X, phase=-1)] | ||
|
||
################################################## | ||
# run until sources are finished (and no later) | ||
################################################## | ||
sim._run_sources_until(0,[]) | ||
|
||
################################################## | ||
# get 1D and 2D array slices | ||
################################################## | ||
xMin = -0.25*sx; | ||
xMax = +0.25*sx; | ||
yMin = -0.15*sy; | ||
yMax = +0.15*sy; | ||
|
||
# 1D slice of Hz data | ||
dims1d=np.zeros(3,dtype=np.int32) | ||
v1d=mp.volume( mp.vec(xMin, 0.0), mp.vec(xMax, 0.0) ) | ||
rank1d = sim.fields.get_array_slice_dimensions(v1d, dims1d); | ||
NX1 = dims1d[0]; | ||
slice1d = np.zeros(NX1, dtype=np.double); | ||
sim.fields.get_array_slice(v1d, mp.Hz, slice1d); | ||
|
||
# 2D slice of Hz data | ||
dims2d=np.zeros(3,dtype=np.int32) | ||
v2d=mp.volume( mp.vec(xMin, yMin), mp.vec(xMax, yMax) ) | ||
rank2d = sim.fields.get_array_slice_dimensions(v2d, dims2d); | ||
NX2 = dims2d[0]; | ||
NY2 = dims2d[1]; | ||
N2 = NX2*NY2; | ||
slice2d = np.zeros(N2, dtype=np.double); | ||
sim.fields.get_array_slice(v2d, mp.Hz, slice2d); | ||
|
||
# plot 1D slice | ||
plt.subplot(1,2,1); | ||
x1d=np.linspace(xMin, xMax, dims1d[0]); | ||
plt.plot(x1d, slice1d); | ||
|
||
# plot 2D slice | ||
plt.subplot(1,2,2); | ||
dy = (yMax-yMin) / dims2d[1]; | ||
dx = (xMax-xMin) / dims2d[0]; | ||
(x2d,y2d)=np.mgrid[ slice(xMin, xMax, dx), slice(yMin, yMax, dy)]; | ||
plt.contourf( x2d, y2d, np.reshape(slice2d, (dims2d[0], dims2d[1])) ) | ||
plt.colorbar(); | ||
plt.show() |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChristopherHogan will need to add a higher-level API function (added to the
Simulation
class, probably) that does thereshape
etc automatically, but this is fine for now.