Skip to content

Commit

Permalink
tests: Refactor data loading and RMS calculations
Browse files Browse the repository at this point in the history
Use numpy slices to read data loaded from .txt files. Use averages
for Coulomb force difference checks in all 3 Coulomb cloud tests.
Replace the broken RMS calculation in the LJ test with an average.
  • Loading branch information
jngrad committed Mar 26, 2021
1 parent 2293827 commit 5b28694
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 74 deletions.
28 changes: 9 additions & 19 deletions testsuite/python/coulomb_cloud_wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class CoulombCloudWall(ut.TestCase):
"""

S = espressomd.System(box_l=[1.0, 1.0, 1.0])
data = np.genfromtxt(tests_common.abspath(
"data/coulomb_cloud_wall_system.data"))

forces = {}
tolerance = 1E-3

# Reference energy from p3m in the tcl test case
Expand All @@ -48,18 +49,10 @@ def setUp(self):
self.S.time_step = 0.01
self.S.cell_system.skin = 0.4

data = np.genfromtxt(tests_common.abspath(
"data/coulomb_cloud_wall_system.data"))

# Add particles to system and store reference forces in hash
# Input format: id pos q f
for row in data:
pid = int(row[0])
pos = row[1:4]
q = row[4]
f = row[5:]
self.S.part.add(id=pid, pos=pos, q=q)
self.forces[pid] = f
self.S.part.add(pos=self.data[:, 1:4], q=self.data[:, 4])
self.forces = self.data[:, 5:8]

def tearDown(self):
self.S.part.clear()
Expand All @@ -69,21 +62,18 @@ def compare(self, method_name, energy=True, prefactor=None):
# Compare forces and energy now in the system to stored ones

# Force
force_abs_diff = 0.
for p in self.S.part:
force_abs_diff += np.linalg.norm(
p.f / prefactor - self.forces[p.id])
force_abs_diff /= len(self.S.part)
force_diff = np.linalg.norm(self.S.part[:].f / prefactor - self.forces,
axis=1)
self.assertLess(
np.mean(force_diff), self.tolerance,
msg="Absolute force difference too large for method " + method_name)

# Energy
if energy:
self.assertAlmostEqual(
self.S.analysis.energy()["total"] / prefactor,
self.reference_energy, delta=self.tolerance,
msg="Absolute energy difference too large for " + method_name)
self.assertLessEqual(
force_abs_diff, self.tolerance,
"Absolute force difference too large for method " + method_name)

# Tests for individual methods

Expand Down
27 changes: 8 additions & 19 deletions testsuite/python/coulomb_cloud_wall_duplicated.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class CoulombCloudWall(ut.TestCase):
"""This compares p3m, p3m_gpu electrostatic forces and energy against
stored data."""
S = espressomd.System(box_l=[1.0, 1.0, 1.0])
np.random.seed(seed=42)
data = np.genfromtxt(
abspath("data/coulomb_cloud_wall_duplicated_system.data"))

forces = {}
tolerance = 1E-3

# Reference energy from p3m in the tcl test case
Expand All @@ -45,18 +45,10 @@ def setUp(self):
self.S.time_step = 0.01
self.S.cell_system.skin = 0.4

data = np.genfromtxt(
abspath("data/coulomb_cloud_wall_duplicated_system.data"))

# Add particles to system and store reference forces in hash
# Input format: id pos q f
for row in data:
pid = int(row[0])
pos = row[1:4]
q = row[4]
f = row[5:]
self.S.part.add(id=pid, pos=pos, q=q)
self.forces[pid] = f
self.S.part.add(pos=self.data[:, 1:4], q=self.data[:, 4])
self.forces = self.data[:, 5:8]

def tearDown(self):
self.S.part.clear()
Expand All @@ -66,20 +58,17 @@ def compare(self, method_name, energy=True):
# Compare forces and energy now in the system to stored ones

# Force
force_abs_diff = 0.
for p in self.S.part:
force_abs_diff += np.linalg.norm(p.f - self.forces[p.id])
force_abs_diff /= len(self.S.part)
force_diff = np.linalg.norm(self.S.part[:].f - self.forces, axis=1)
self.assertLess(
np.mean(force_diff), self.tolerance,
msg="Absolute force difference too large for method " + method_name)

# Energy
if energy:
self.assertAlmostEqual(
self.S.analysis.energy()["total"], self.reference_energy,
delta=self.tolerance,
msg="Absolute energy difference too large for " + method_name)
self.assertLess(
force_abs_diff, self.tolerance,
msg="Absolute force difference too large for method " + method_name)

# Tests for individual methods

Expand Down
30 changes: 9 additions & 21 deletions testsuite/python/coulomb_mixed_periodicity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ class CoulombMixedPeriodicity(ut.TestCase):
"""Test mixed periodicity electrostatics"""

S = espressomd.System(box_l=[1.0, 1.0, 1.0])
buf_node_grid = S.cell_system.node_grid
S.thermostat.turn_off()
forces = {}
data = np.genfromtxt(tests_common.abspath(
"data/coulomb_mixed_periodicity_system.data"))

tolerance_force = 5E-4
tolerance_energy = 1.8E-3
generate_data = False

# Reference energy from MMM2D
reference_energy = 216.640984711
Expand All @@ -45,18 +44,10 @@ def setUp(self):
self.S.time_step = 0.01
self.S.cell_system.skin = 0.

data = np.genfromtxt(tests_common.abspath(
"data/coulomb_mixed_periodicity_system.data"))

# Add particles to system and store reference forces in hash
# Input format: id pos q f
for row in data:
pid = int(row[0])
pos = row[1:4]
q = row[4]
f = row[5:]
self.S.part.add(id=pid, pos=pos, q=q)
self.forces[pid] = f
self.S.part.add(pos=self.data[:, 1:4], q=self.data[:, 4])
self.forces = self.data[:, 5:8]

def tearDown(self):
self.S.part.clear()
Expand All @@ -66,20 +57,17 @@ def compare(self, method_name, energy=True):
# Compare forces and energy now in the system to stored ones

# Force
rms_force_diff = 0.
for p in self.S.part:
rms_force_diff += np.sum((p.f - self.forces[p.id])**2)
rms_force_diff = np.sqrt(rms_force_diff / len(self.S.part))
force_diff = np.linalg.norm(self.S.part[:].f - self.forces, axis=1)
self.assertLessEqual(
np.mean(force_diff), self.tolerance_force,
"Absolute force difference too large for method " + method_name)

# Energy
if energy:
self.assertAlmostEqual(
self.S.analysis.energy()["total"],
self.reference_energy, delta=self.tolerance_energy,
msg="Absolute energy difference too large for " + method_name)
self.assertLessEqual(
rms_force_diff, self.tolerance_force,
"Absolute force difference too large for method " + method_name)

# Tests for individual methods

Expand Down
22 changes: 7 additions & 15 deletions testsuite/python/lj.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
@utx.skipIfMissingFeatures(["LENNARD_JONES"])
class LennardJonesTest(ut.TestCase):
system = espressomd.System(box_l=[1.0, 1.0, 1.0])
pos, forces = np.loadtxt(
abspath('data/lj_system.dat'))[:, 1:].reshape((-1, 2, 3)).swapaxes(0, 1)
data = np.loadtxt(abspath('data/lj_system.dat'))
pos = data[:, 1:4]
forces = data[:, 4:7]

def setUp(self):
self.system.part.clear()
Expand All @@ -46,19 +47,10 @@ def setUp(self):
self.system.part.add(pos=self.pos)

def check(self):
rms = 0.0
max_df = 0.0

for p, ref_force in zip(self.system.part, self.forces):
for j in range(3):
df2 = (ref_force[j] - p.f[j])**2
rms += df2
max_df = max(max_df, (df2)**0.5)

rms = rms**0.5

self.assertLess(rms, 1e-5)
self.assertLess(max_df, 1e-5)
f_diff = np.linalg.norm(self.system.part[:].f - self.forces, axis=1)
max_deviation = np.max(np.abs(self.system.part[:].f - self.forces))
self.assertLess(np.mean(f_diff), 1e-7)
self.assertLess(max_deviation, 1e-5)

def test_dd(self):
self.system.cell_system.set_domain_decomposition(
Expand Down

0 comments on commit 5b28694

Please sign in to comment.