Skip to content

Commit

Permalink
Enable single-precision floating point unit tests for CI (NanoComp#1698)
Browse files Browse the repository at this point in the history
* lower tolerance of various tests when compiled with single precision

* add more tests

* fix more tests

* fix for test_get_array_metadata.py

* only compile with single precision for Python 3.9 with MPI

* cleanups and tweaks

* fix harmonics.cpp
  • Loading branch information
oskooi committed Jul 22, 2021
1 parent dec62e9 commit 8e4b642
Show file tree
Hide file tree
Showing 32 changed files with 177 additions and 115 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
echo "MEEP_VERSION=${MEEP_VERSION}" >> $GITHUB_ENV
- name: Run configure
if: ${{ !(matrix.enable-mpi == false && matrix.python-version == 3.6) }}
if: ${{ !(matrix.enable-mpi == false && matrix.python-version == 3.6) && !(matrix.enable-mpi == true && matrix.python-version == 3.9) }}
run: |
mkdir -p build &&
pushd build &&
Expand All @@ -151,6 +151,14 @@ jobs:
if: ${{ matrix.enable-mpi == false && matrix.python-version == 3.6 }}
run: ./configure --enable-maintainer-mode --prefix=${HOME}/local --with-libctl=${HOME}/local/share/libctl ${MPICONF}

- name: Run configure with single-precision floating point
if: ${{ matrix.enable-mpi == true && matrix.python-version == 3.9 }}
run: |
mkdir -p build &&
pushd build &&
../configure --enable-maintainer-mode --prefix=${HOME}/local --with-libctl=${HOME}/local/share/libctl ${MPICONF} --enable-single &&
popd
- name: Run make
if: ${{ matrix.enable-mpi == false && matrix.python-version == 3.6 }}
run: make ${MKCHECKFLAGS}
Expand Down
8 changes: 4 additions & 4 deletions python/tests/test_3rd_harm_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import unittest
import meep as mp
import numpy as np
from utils import VectorComparisonMixin


class Test3rdHarm1d(unittest.TestCase):
class Test3rdHarm1d(VectorComparisonMixin, unittest.TestCase):

def setUp(self):
self.sz = 100
Expand Down Expand Up @@ -53,7 +52,8 @@ def test_3rd_harm_1d(self):

harmonics = [self.k, self.amp, mp.get_fluxes(self.trans1)[0], mp.get_fluxes(self.trans3)[0]]

np.testing.assert_allclose(expected_harmonics, harmonics)
tol = 3e-5 if mp.is_single_precision() else 1e-7
self.assertVectorsClose(expected_harmonics, harmonics, epsilon=tol)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_adjoint_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_FD_STEP = 1e-4

# The tolerance for the adjoint and finite difference gradient comparison
_TOL = 2e-2
_TOL = 0.1 if mp.is_single_precision() else 0.02

mp.verbosity(0)

Expand Down
23 changes: 14 additions & 9 deletions python/tests/test_adjoint_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from autograd import tensor_jacobian_product
import unittest
from enum import Enum
from utils import VectorComparisonMixin

MonitorObject = Enum('MonitorObject', 'EIGENMODE DFT')

Expand Down Expand Up @@ -170,7 +171,7 @@ def mapping(x,filter_radius,eta,beta):
return projected_field.flatten()


class TestAdjointSolver(unittest.TestCase):
class TestAdjointSolver(VectorComparisonMixin, unittest.TestCase):

def test_adjoint_solver_DFT_fields(self):
print("*** TESTING DFT ADJOINT FEATURES ***")
Expand All @@ -184,7 +185,7 @@ def test_adjoint_solver_DFT_fields(self):

## compare objective results
print("|Ez|^2 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
np.testing.assert_array_almost_equal(adjsol_obj,S12_unperturbed,decimal=3)
self.assertVectorsClose(adjsol_obj,S12_unperturbed,epsilon=1e-3)

## compute perturbed S12
S12_perturbed = forward_simulation(p+dp, MonitorObject.DFT, frequencies)
Expand All @@ -195,7 +196,8 @@ def test_adjoint_solver_DFT_fields(self):
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
np.testing.assert_array_almost_equal(adj_scale,fd_grad,decimal=5)
tol = 0.3 if mp.is_single_precision() else 0.01
self.assertVectorsClose(adj_scale,fd_grad,epsilon=tol)


def test_adjoint_solver_eigenmode(self):
Expand All @@ -207,21 +209,22 @@ def test_adjoint_solver_eigenmode(self):

## compute unperturbed S12
S12_unperturbed = forward_simulation(p, MonitorObject.EIGENMODE, frequencies)

## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
np.testing.assert_array_almost_equal(adjsol_obj,S12_unperturbed,decimal=3)
self.assertVectorsClose(adjsol_obj,S12_unperturbed,epsilon=1e-3)

## compute perturbed S12
S12_perturbed = forward_simulation(p+dp, MonitorObject.EIGENMODE, frequencies)

## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
np.testing.assert_array_almost_equal(adj_scale,fd_grad,decimal=5)
tol = 0.04 if mp.is_single_precision() else 0.03
self.assertVectorsClose(adj_scale,fd_grad,epsilon=tol)


def test_gradient_backpropagation(self):
Expand Down Expand Up @@ -250,7 +253,8 @@ def test_gradient_backpropagation(self):

## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
np.testing.assert_array_almost_equal(adjsol_obj,S12_unperturbed,decimal=3)
tol = 1e-2 if mp.is_single_precision() else 1e-3
self.assertVectorsClose(adjsol_obj,S12_unperturbed,epsilon=tol)

## compute perturbed S12
S12_perturbed = forward_simulation(mapping(p+dp,filter_radius,eta,beta), MonitorObject.EIGENMODE,frequencies)
Expand All @@ -260,7 +264,8 @@ def test_gradient_backpropagation(self):
adj_scale = (dp[None,:]@bp_adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
np.testing.assert_array_almost_equal(adj_scale,fd_grad,decimal=5)
tol = 0.04 if mp.is_single_precision() else 0.03
self.assertVectorsClose(adj_scale,fd_grad,epsilon=tol)


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions python/tests/test_array_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import meep as mp
import unittest
import numpy as np

class TestArrayMetadata(unittest.TestCase):
Expand Down Expand Up @@ -80,7 +80,11 @@ def vec_func(r):
vec_func_sum = np.sum(W*(xm**2 + 2*ym**2))
pulse_modal_volume = np.sum(W*EpsE2)/np.max(EpsE2) * vec_func_sum

self.assertAlmostEqual(cw_modal_volume/pulse_modal_volume, 1.00, places=2)
if ((mp.count_processors() % 2 == 0) and mp.is_single_precision()):
ref_val = 0.94
else:
ref_val = 1.00
self.assertAlmostEqual(cw_modal_volume/pulse_modal_volume, ref_val, places=2)

if __name__ == '__main__':
unittest.main()
20 changes: 11 additions & 9 deletions python/tests/test_cavity_arrayslice.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import division

import meep as mp
from utils import VectorComparisonMixin
import os
import unittest

import meep as mp
import numpy as np


class TestCavityArraySlice(unittest.TestCase):
class TestCavityArraySlice(VectorComparisonMixin, unittest.TestCase):

data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))
expected_1d = np.load(os.path.join(data_dir, 'cavity_arrayslice_1d.npy'))
Expand Down Expand Up @@ -60,27 +58,31 @@ def test_1d_slice(self):
self.sim.run(until_after_sources=0)
vol = mp.Volume(center=self.center_1d, size=self.size_1d)
hl_slice1d = self.sim.get_array(mp.Hz, vol)
np.testing.assert_allclose(self.expected_1d, hl_slice1d)
tol = 1e-5 if mp.is_single_precision() else 1e-8
self.assertVectorsClose(self.expected_1d, hl_slice1d, epsilon=tol)

def test_2d_slice(self):
self.sim.run(until_after_sources=0)
vol = mp.Volume(center=self.center_2d, size=self.size_2d)
hl_slice2d = self.sim.get_array(mp.Hz, vol)
np.testing.assert_allclose(self.expected_2d, hl_slice2d)
tol = 1e-5 if mp.is_single_precision() else 1e-8
self.assertVectorsClose(self.expected_2d, hl_slice2d, epsilon=tol)

def test_1d_slice_user_array(self):
self.sim.run(until_after_sources=0)
arr = np.zeros(126, dtype=np.float64)
vol = mp.Volume(center=self.center_1d, size=self.size_1d)
self.sim.get_array(mp.Hz, vol, arr=arr)
np.testing.assert_allclose(self.expected_1d, arr)
tol = 1e-5 if mp.is_single_precision() else 1e-8
self.assertVectorsClose(self.expected_1d, arr, epsilon=tol)

def test_2d_slice_user_array(self):
self.sim.run(until_after_sources=0)
arr = np.zeros((126, 38), dtype=np.float64)
vol = mp.Volume(center=self.center_2d, size=self.size_2d)
self.sim.get_array(mp.Hz, vol, arr=arr)
np.testing.assert_allclose(self.expected_2d, arr)
tol = 1e-5 if mp.is_single_precision() else 1e-8
self.assertVectorsClose(self.expected_2d, arr, epsilon=tol)

def test_illegal_user_array(self):
self.sim.run(until_after_sources=0)
Expand Down
30 changes: 18 additions & 12 deletions python/tests/test_cavity_farfield.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import meep as mp
from utils import VectorComparisonMixin
import os
import unittest
import h5py
import numpy as np
import meep as mp


class TestCavityFarfield(unittest.TestCase):
class TestCavityFarfield(VectorComparisonMixin, unittest.TestCase):

data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data'))

Expand Down Expand Up @@ -50,9 +50,13 @@ def run_test(self, nfreqs):

nearfield = sim.add_near2far(
fcen, 0.1, nfreqs,
mp.Near2FarRegion(mp.Vector3(0, 0.5 * w + d1), size=mp.Vector3(2 * dpml - sx)),
mp.Near2FarRegion(mp.Vector3(-0.5 * sx + dpml, 0.5 * w + 0.5 * d1), size=mp.Vector3(0, d1), weight=-1.0),
mp.Near2FarRegion(mp.Vector3(0.5 * sx - dpml, 0.5 * w + 0.5 * d1), size=mp.Vector3(0, d1))
mp.Near2FarRegion(mp.Vector3(0, 0.5 * w + d1),
size=mp.Vector3(2 * dpml - sx)),
mp.Near2FarRegion(mp.Vector3(-0.5 * sx + dpml, 0.5 * w + 0.5 * d1),
size=mp.Vector3(0, d1),
weight=-1.0),
mp.Near2FarRegion(mp.Vector3(0.5 * sx - dpml, 0.5 * w + 0.5 * d1),
size=mp.Vector3(0, d1))
)
sim.run(until=200)
d2 = 20
Expand All @@ -71,12 +75,14 @@ def run_test(self, nfreqs):
ref_hy = mp.complexarray(f['hy.r'][()], f['hy.i'][()])
ref_hz = mp.complexarray(f['hz.r'][()], f['hz.i'][()])

np.testing.assert_allclose(ref_ex, result['Ex'])
np.testing.assert_allclose(ref_ey, result['Ey'])
np.testing.assert_allclose(ref_ez, result['Ez'])
np.testing.assert_allclose(ref_hx, result['Hx'])
np.testing.assert_allclose(ref_hy, result['Hy'])
np.testing.assert_allclose(ref_hz, result['Hz'])
tol = 1e-5 if mp.is_single_precision() else 1e-7
self.assertVectorsClose(ref_ex, result['Ex'], epsilon=tol)
self.assertVectorsClose(ref_ey, result['Ey'], epsilon=tol)
self.assertVectorsClose(ref_ez, result['Ez'], epsilon=tol)
self.assertVectorsClose(ref_hx, result['Hx'], epsilon=tol)
self.assertVectorsClose(ref_hy, result['Hy'], epsilon=tol)
self.assertVectorsClose(ref_hz, result['Hz'], epsilon=tol)


def test_cavity_farfield(self):
self.run_test(nfreqs=1)
Expand Down
14 changes: 10 additions & 4 deletions python/tests/test_chunks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import meep as mp
import unittest


class TestChunks(unittest.TestCase):

Expand Down Expand Up @@ -44,8 +45,12 @@ def test_chunks(self):
sim.save_flux('tot_flux', tot_flux)
sim1 = sim

geometry = [mp.Block(center=mp.Vector3(), size=mp.Vector3(sxy, sxy, mp.inf), material=mp.Medium(index=3.5)),
mp.Block(center=mp.Vector3(), size=mp.Vector3(sxy-2*dpml, sxy-2*dpml, mp.inf), material=mp.air)]
geometry = [mp.Block(center=mp.Vector3(),
size=mp.Vector3(sxy, sxy, mp.inf),
material=mp.Medium(index=3.5)),
mp.Block(center=mp.Vector3(),
size=mp.Vector3(sxy-2*dpml, sxy-2*dpml, mp.inf),
material=mp.air)]

sim = mp.Simulation(cell_size=cell,
geometry=geometry,
Expand All @@ -62,7 +67,8 @@ def test_chunks(self):

sim.run(until_after_sources=mp.stop_when_fields_decayed(50, mp.Ez, mp.Vector3(), 1e-5))

self.assertAlmostEqual(86.90826609300862, mp.get_fluxes(tot_flux)[0])
places = 3 if mp.is_single_precision() else 7
self.assertAlmostEqual(86.90826609300862, mp.get_fluxes(tot_flux)[0], places=places)


if __name__ == '__main__':
Expand Down
13 changes: 6 additions & 7 deletions python/tests/test_dispersive_eigenmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
# * check magnetic profiles
# * once imaginary component is supported, check that

from __future__ import division

import unittest
import meep as mp
from utils import VectorComparisonMixin
import unittest
import numpy as np
from meep import mpb
import h5py
import os

class TestDispersiveEigenmode(unittest.TestCase):
class TestDispersiveEigenmode(VectorComparisonMixin, unittest.TestCase):
# ----------------------------------------- #
# ----------- Helper Functions ------------ #
# ----------------------------------------- #
Expand All @@ -35,8 +33,9 @@ def call_chi1(self,material,frequency):
n = np.real(np.sqrt(np.linalg.inv(chi1inv.astype(np.complex128))))

n_actual = np.real(np.sqrt(material.epsilon(frequency).astype(np.complex128)))

np.testing.assert_allclose(n,n_actual)

tol = 1e-6 if mp.is_single_precision() else 1e-8
self.assertVectorsClose(n, n_actual, epsilon=tol)

@classmethod
def setUpClass(cls):
Expand Down
5 changes: 3 additions & 2 deletions python/tests/test_divide_mpi_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def test_divide_parallel_processes(self):

self.assertEqual(fcens[0],1)
self.assertEqual(fcens[1],0.5)
self.assertAlmostEqual(tot_fluxes[0], 9.8628728533)
self.assertAlmostEqual(tot_fluxes[1], 19.6537275387)
places = 4 if mp.is_single_precision() else 7
self.assertAlmostEqual(tot_fluxes[0], 9.8628728533, places=places)
self.assertAlmostEqual(tot_fluxes[1], 19.6537275387, places=places)

if __name__ == '__main__':
unittest.main()
Expand Down
10 changes: 3 additions & 7 deletions python/tests/test_eigfreq.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from __future__ import division

import unittest
import meep as mp
import cmath
import math
from time import time
import unittest

class TestEigfreq(unittest.TestCase):

@unittest.skipIf(mp.is_single_precision(), "double-precision floating point specific test")
def test_eigfreq(self):
w = 1.2 # width of waveguide
r = 0.36 # radius of holes
d = 1.4 # defect spacing (ordinary spacing = 1)
N = 3 # number of holes on either side of defect
N = 3 # number of holes on either side of defect
sy = 6 # size of cell in y direction (perpendicular to wvg.)
pad = 2 # padding between last hole and PML edge
dpml = 1 # PML thickness
Expand Down
6 changes: 4 additions & 2 deletions python/tests/test_field_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import meep as mp
import unittest


def f(r, ex, hz, eps):
return (r.x * r.norm() + ex) - (eps * hz)
Expand Down Expand Up @@ -95,7 +96,8 @@ def test_integrate2_field_function(self):
sim.run(until_after_sources=10)

res1 = sim.integrate2_field_function(fields2, [mp.Ez], [mp.Ez], f2)
self.assertAlmostEqual(res1, 0.17158099566244897)
places = 6 if mp.is_single_precision() else 7
self.assertAlmostEqual(res1, 0.17158099566244897, places=places)

def test_max_abs_field_function(self):
sim = self.init()
Expand Down
Loading

0 comments on commit 8e4b642

Please sign in to comment.