Skip to content

Commit

Permalink
create and delete temporary directory using unittest methods (#1132)
Browse files Browse the repository at this point in the history
* create and delete temporary directory inside unittest methods

* fixes

* put meep.all_wait before rmtree to prevent race condition

* put all_wait before delete_directory in h5test to prevent race condition

* replace shutil.rmtree with meep.delete_directory

* replace setUp/tearDown with setUpClass/tearDownClass

* decorate setUpClass/tearDownClass with classmethod
  • Loading branch information
oskooi authored Feb 25, 2020
1 parent 801b236 commit 1ddec1e
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 93 deletions.
4 changes: 2 additions & 2 deletions doc/docs/Python_User_Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -1582,11 +1582,11 @@ Out[2]: [0, 1, 2, 3, 4, 5, 31, 32]

**`mp.get_GDSII_prisms(material, gdsii_filename, layer, zmin, zmax)`**
Returns a list of `GeometricObject`s with `material` (`mp.Medium`) on layer number `layer` of a GDSII file `gdsii_filename` with `zmin` and `zmax`.
Returns a list of `GeometricObject`s with `material` (`mp.Medium`) on layer number `layer` of a GDSII file `gdsii_filename` with `zmin` and `zmax` (default 0).

**`mp.GDSII_vol(fname, layer, zmin, zmax)`**
Returns a `mp.Volume` read from a GDSII file `fname` on layer number `layer` with `zmin` and `zmax`. This function is useful for creating a `FluxRegion` from a GDSII file as follows
Returns a `mp.Volume` read from a GDSII file `fname` on layer number `layer` with `zmin` and `zmax` (default 0). This function is useful for creating a `FluxRegion` from a GDSII file as follows

```python
fr = mp.FluxRegion(volume=mp.GDSII_vol(fname, layer, zmin, zmax))
Expand Down
16 changes: 10 additions & 6 deletions python/tests/chunks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import unittest
import meep as mp
import os

class TestChunks(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def test_chunks(self):
sxy = 10
cell = mp.Vector3(sxy, sxy, 0)
Expand All @@ -23,7 +30,7 @@ def test_chunks(self):
resolution=resolution,
split_chunks_evenly=False)

sim.use_output_directory(temp_dir)
sim.use_output_directory(self.temp_dir)

top = mp.FluxRegion(center=mp.Vector3(0,+0.5*sxy-dpml), size=mp.Vector3(sxy-2*dpml,0), weight=+1.0)
bot = mp.FluxRegion(center=mp.Vector3(0,-0.5*sxy+dpml), size=mp.Vector3(sxy-2*dpml,0), weight=-1.0)
Expand All @@ -47,7 +54,7 @@ def test_chunks(self):
resolution=resolution,
chunk_layout=sim1)

sim.use_output_directory(temp_dir)
sim.use_output_directory(self.temp_dir)

tot_flux = sim.add_flux(fcen, 0, 1, top, bot, rgt, lft)

Expand All @@ -59,7 +66,4 @@ def test_chunks(self):


if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/cyl_ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import unittest
import meep as mp
import os

def dummy_eps(vec):
return 1.0
Expand All @@ -12,6 +11,14 @@ class TestCylEllipsoid(unittest.TestCase):
ref_Ez = -8.29555720049629e-5
ref_Hz = -4.5623185899766e-5

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def init(self):

c = mp.Cylinder(radius=3, material=mp.Medium(index=3.5))
Expand All @@ -34,7 +41,7 @@ def init(self):
symmetries=symmetries,
resolution=100)

self.sim.use_output_directory(temp_dir)
self.sim.use_output_directory(self.temp_dir)

def print_stuff(sim_obj):
v = mp.Vector3(4.13, 3.75, 0)
Expand Down Expand Up @@ -70,7 +77,4 @@ def test_hz_field(self):


if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
25 changes: 15 additions & 10 deletions python/tests/dft_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

class TestDFTFields(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def init(self):
resolution = 10
n = 3.4
Expand Down Expand Up @@ -62,13 +70,13 @@ def test_get_dft_array(self):
np.testing.assert_equal(thin_x_array.ndim, 1)
np.testing.assert_equal(thin_y_array.ndim, 1)

sim.output_dft(thin_x_flux, os.path.join(temp_dir, 'thin-x-flux'))
sim.output_dft(thin_y_flux, os.path.join(temp_dir, 'thin-y-flux'))
sim.output_dft(thin_x_flux, os.path.join(self.temp_dir, 'thin-x-flux'))
sim.output_dft(thin_y_flux, os.path.join(self.temp_dir, 'thin-y-flux'))

with h5py.File(os.path.join(temp_dir, 'thin-x-flux.h5'), 'r') as thin_x:
with h5py.File(os.path.join(self.temp_dir, 'thin-x-flux.h5'), 'r') as thin_x:
thin_x_h5 = mp.complexarray(thin_x['ez_0.r'][()], thin_x['ez_0.i'][()])

with h5py.File(os.path.join(temp_dir, 'thin-y-flux.h5'), 'r') as thin_y:
with h5py.File(os.path.join(self.temp_dir, 'thin-y-flux.h5'), 'r') as thin_y:
thin_y_h5 = mp.complexarray(thin_y['ez_0.r'][()], thin_y['ez_0.i'][()])

np.testing.assert_allclose(thin_x_array, thin_x_h5)
Expand All @@ -78,18 +86,15 @@ def test_get_dft_array(self):
fields_arr = sim.get_dft_array(dft_fields, mp.Ez, 0)
flux_arr = sim.get_dft_array(dft_flux, mp.Ez, 0)

sim.output_dft(dft_fields, os.path.join(temp_dir, 'dft-fields'))
sim.output_dft(dft_flux, os.path.join(temp_dir, 'dft-flux'))
sim.output_dft(dft_fields, os.path.join(self.temp_dir, 'dft-fields'))
sim.output_dft(dft_flux, os.path.join(self.temp_dir, 'dft-flux'))

with h5py.File(os.path.join(temp_dir, 'dft-fields.h5'), 'r') as fields, h5py.File(os.path.join(temp_dir, 'dft-flux.h5'), 'r') as flux:
with h5py.File(os.path.join(self.temp_dir, 'dft-fields.h5'), 'r') as fields, h5py.File(os.path.join(self.temp_dir, 'dft-flux.h5'), 'r') as flux:
exp_fields = mp.complexarray(fields['ez_0.r'][()], fields['ez_0.i'][()])
exp_flux = mp.complexarray(flux['ez_0.r'][()], flux['ez_0.i'][()])

np.testing.assert_allclose(exp_fields, fields_arr)
np.testing.assert_allclose(exp_flux, flux_arr)

if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
17 changes: 11 additions & 6 deletions python/tests/dispersive_eigenmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def call_chi1(self,material,omega):
n_actual = np.real(np.sqrt(material.epsilon(omega).astype(np.complex128)))

np.testing.assert_allclose(n,n_actual)


@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def verify_output_and_slice(self,material,omega):
# Since the slice routines average the diagonals, we need to do that too:
chi1 = material.epsilon(omega).astype(np.complex128)
Expand All @@ -51,15 +59,15 @@ def verify_output_and_slice(self,material,omega):
resolution=20,
eps_averaging=False
)
sim.use_output_directory(temp_dir)
sim.use_output_directory(self.temp_dir)
sim.init_sim()

# Check to make sure the get_slice routine is working with omega
n_slice = np.sqrt(np.max(sim.get_epsilon(omega)))
self.assertAlmostEqual(n,n_slice, places=4)

# Check to make sure h5 output is working with omega
filename = os.path.join(temp_dir, 'dispersive_eigenmode-eps-000000.00.h5')
filename = os.path.join(self.temp_dir, 'dispersive_eigenmode-eps-000000.00.h5')
mp.output_epsilon(sim,omega=omega)
n_h5 = 0
mp.all_wait()
Expand Down Expand Up @@ -147,7 +155,4 @@ def test_get_with_dispersion(self):


if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/field_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import unittest
import meep as mp
import os

def f(r, ex, hz, eps):
return (r.x * r.norm() + ex) - (eps * hz)
Expand All @@ -15,6 +14,14 @@ class TestFieldFunctions(unittest.TestCase):
cs = [mp.Ex, mp.Hz, mp.Dielectric]
vol = mp.Volume(size=mp.Vector3(1), center=mp.Vector3())

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def init(self):
resolution = 20

Expand Down Expand Up @@ -69,7 +76,7 @@ def init2(self):

def test_integrate_field_function(self):
sim = self.init()
sim.use_output_directory(temp_dir)
sim.use_output_directory(self.temp_dir)
sim.run(until=200)

res1 = sim.integrate_field_function(self.cs, f)
Expand Down Expand Up @@ -99,7 +106,4 @@ def test_max_abs_field_function(self):
self.assertAlmostEqual(res, 0.27593732304637586)

if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/holey_wvg_cavity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
import meep as mp
import numpy as np
import os

class TestHoleyWvgCavity(unittest.TestCase):

Expand Down Expand Up @@ -38,14 +37,22 @@ def setUp(self):
boundary_layers=[mp.PML(self.dpml)],
resolution=20)

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def test_resonant_modes(self):
self.sim.sources = [mp.Source(mp.GaussianSource(self.fcen, fwidth=self.df),
mp.Hz, mp.Vector3())]

self.sim.symmetries = [mp.Mirror(mp.Y, phase=-1),
mp.Mirror(mp.X, phase=-1)]

self.sim.use_output_directory(temp_dir)
self.sim.use_output_directory(self.temp_dir)
h = mp.Harminv(mp.Hz, mp.Vector3(), self.fcen, self.df)
self.sim.run(mp.at_beginning(mp.output_epsilon),
mp.after_sources(h),
Expand Down Expand Up @@ -139,7 +146,4 @@ def test_transmission_spectrum(self):


if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/mpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import time
import unittest
import os

import h5py
import numpy as np
Expand All @@ -26,15 +25,23 @@ class TestModeSolver(unittest.TestCase):
def setUp(self):
self.start = time.time()

self.filename_prefix = os.path.join(temp_dir, self.id().split('.')[-1])
self.filename_prefix = os.path.join(self.temp_dir, self.id().split('.')[-1])
print()
print(self.filename_prefix)
print('=' * 24)

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

def tearDown(self):
end = time.time() - self.start
print("{}: {:.2f}s".format(self.filename_prefix, end))

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def init_solver(self, geom=True):
num_bands = 8
k_points = [
Expand Down Expand Up @@ -1410,7 +1417,4 @@ def test_fractional_lattice(self):


if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/pw_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import unittest

import meep as mp
import os

class TestPwSource(unittest.TestCase):

Expand Down Expand Up @@ -52,9 +51,17 @@ def _pw_amp(x):
boundary_layers=pml_layers,
resolution=resolution
)
self.sim.use_output_directory(temp_dir)
self.sim.use_output_directory(self.temp_dir)
self.s = s

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def test_pw_source(self):
self.sim.run(mp.at_end(mp.output_efield_z), until=400)

Expand All @@ -68,7 +75,4 @@ def test_pw_source(self):
self.assertAlmostEqual(cmath.exp(1j * self.k.dot(v1 - v2)), 0.7654030066070924 - 0.6435512702783076j)

if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
14 changes: 9 additions & 5 deletions python/tests/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

import unittest
import meep as mp
import os

class TestRing(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.temp_dir = mp.make_output_directory()

@classmethod
def tearDownClass(cls):
mp.delete_directory(cls.temp_dir)

def init(self):
n = 3.4
w = 1
Expand All @@ -34,7 +41,7 @@ def init(self):
symmetries=[mp.Mirror(mp.Y)],
boundary_layers=[mp.PML(dpml)])

self.sim.use_output_directory(temp_dir)
self.sim.use_output_directory(self.temp_dir)
self.h = mp.Harminv(mp.Ez, mp.Vector3(r + 0.1), fcen, df)

def test_harminv(self):
Expand Down Expand Up @@ -62,7 +69,4 @@ def test_harminv(self):
self.assertAlmostEqual(fp, -0.08185972142450348)

if __name__ == '__main__':
temp_dir = mp.make_output_directory()
unittest.main()
if mp.am_master():
os.removedirs(temp_dir)
Loading

0 comments on commit 1ddec1e

Please sign in to comment.