public distro 2.1.0

This commit is contained in:
2019-07-19 12:54:54 +02:00
parent acea809e4e
commit fbd2d4fa8c
40 changed files with 2813 additions and 345 deletions

View File

@ -10,7 +10,7 @@ to run the tests, change to the directory which contains the tests directory, an
@author Matthias Muntwiler, matthias.muntwiler@psi.ch
@copyright (c) 2015-17 by Paul Scherrer Institut @n
@copyright (c) 2015-19 by Paul Scherrer Institut @n
Licensed under the Apache License, Version 2.0 (the "License"); @n
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
@ -102,16 +102,18 @@ class TestClusterFunctions(unittest.TestCase):
@return: None
"""
clu = self.create_cube()
xy2 = clu.data[['x', 'y']].copy()
xy3 = xy2.view((xy2.dtype[0], len(xy2.dtype.names)))
xy3 = np.empty((clu.data.shape[0], 2), np.float32)
xy3[:, 0] = clu.data['x']
xy3[:, 1] = clu.data['y']
ctr = np.asarray((1.0, 0.0, 0.0))
dist = np.linalg.norm(xy3 - ctr[0:2], axis=1)
self.assertAlmostEqual(1.0, dist[0])
self.assertAlmostEqual(0.0, dist[1])
clu.clear()
xy2 = clu.data[['x', 'y']].copy()
xy3 = xy2.view((xy2.dtype[0], len(xy2.dtype.names)))
xy3 = np.empty((clu.data.shape[0], 2), np.float32)
xy3[:, 0] = clu.data['x']
xy3[:, 1] = clu.data['y']
ctr = np.asarray((1.0, 0.0, 0.0))
dist = np.linalg.norm(xy3 - ctr[0:2], axis=1)
self.assertEqual(0, dist.shape[0])
@ -156,7 +158,7 @@ class TestClusterFunctions(unittest.TestCase):
clu.set_emitter(idx=0)
clu.set_emitter(idx=9)
self.assertEqual(2, clu.get_emitter_count())
result = clu.get_emitters()
result = clu.get_emitters(['x', 'y', 'z', 't'])
expect = [(0., 0., 0., 1), (1., 0., 1., 10)]
self.assertEqual(expect, result)
@ -233,7 +235,7 @@ class TestClusterFunctions(unittest.TestCase):
emitter = np.array((0.0, 0.0, 0.0))
clu.add_layer(7, a1, b1, b2)
pos = clu.find_positions(pos=emitter)
self.assertEqual(len(pos), 1)
self.assertEqual(1, len(pos))
def test_add_cluster(self):
clu1 = mc.Cluster()
@ -244,15 +246,18 @@ class TestClusterFunctions(unittest.TestCase):
clu1.add_atom(5, np.asarray([0, 0, -2]), 0)
clu2 = mc.Cluster()
clu2.add_atom(3, np.asarray([-0.2, 0, 0]), 0)
clu2.add_atom(4, np.asarray([0, -0.2, 0]), 0)
clu2.add_atom(5, np.asarray([0, 0.05, -1]), 0)
clu2.add_atom(5, np.asarray([0, 0, -1.01]), 0)
clu2.add_atom(6, np.asarray([0, 0, -1.99]), 0)
clu2.add_atom(3, np.asarray([-0.2, 0, 0]), 0) # unique
clu2.add_atom(4, np.asarray([0, -0.2, 0]), 0) # unique
clu2.add_atom(5, np.asarray([0, 0.05, -1]), 0) # not unique
clu2.add_atom(5, np.asarray([0, 0, -1.09]), 0) # just within tolerance of uniqueness
clu2.add_atom(6, np.asarray([0, 0, -1.99]), 0) # not unique
clu2.add_atom(7, np.asarray([0, 0, -1.10]), 0) # just out of tolerance of uniqueness
clu1.set_rmax(1.5)
clu1.add_cluster(clu2, check_rmax=True, check_unique=True, tol=0.1)
self.assertEqual(clu1.get_atom_count(), 5+2)
self.assertEqual(5+3, clu1.get_atom_count())
self.assertEqual(7, clu1.data['t'][-1])
self.assertEqual(6, clu2.data.shape[0])
def test_find_positions(self):
clu = mc.Cluster()
@ -269,8 +274,14 @@ class TestClusterFunctions(unittest.TestCase):
clu.add_layer(7, a_N, b1, b2)
clu.add_layer(5, a_B, b1, b2)
pos = clu.find_positions(pos=emitter)
self.assertEqual(len(pos), 1)
self.assertEqual(pos[0], 206)
self.assertEqual(1, len(pos))
self.assertEqual(206, pos[0])
# position in the format returned by get_emitters
emitter = (emitter[0], emitter[1], emitter[2], 7)
pos = clu.find_positions(pos=emitter)
self.assertEqual(1, len(pos))
self.assertEqual(206, pos[0])
def test_find_index_cylinder(self):
clu = self.create_cube()
@ -278,11 +289,11 @@ class TestClusterFunctions(unittest.TestCase):
rxy = 0.5
rz = 1.0
idx = clu.find_index_cylinder(pos, rxy, rz, None)
self.assertEqual(len(idx), 2)
self.assertEqual(clu.get_atomtype(idx[0]), 8)
self.assertEqual(clu.get_atomtype(idx[1]), 20)
self.assertEqual(2, len(idx))
self.assertEqual(8, clu.get_atomtype(idx[0]))
self.assertEqual(20, clu.get_atomtype(idx[1]))
idx = clu.find_index_cylinder(pos, rxy, rz, 8)
self.assertEqual(len(idx), 1)
self.assertEqual(1, len(idx))
def test_trim_cylinder(self):
clu = mc.Cluster()
@ -296,12 +307,12 @@ class TestClusterFunctions(unittest.TestCase):
r0 = 2.3
z0 = 4.2
clu.trim_cylinder(r0, z0)
self.assertEqual(clu.data.dtype, clu.dtype)
self.assertEqual(clu.data.shape[0], 21 * 5)
self.assertEqual(clu.data[1]['i'], 2)
self.assertEqual(clu.data[1]['s'], 'N')
self.assertEqual(clu.data[1]['t'], 7)
self.assertEqual(clu.get_emitter_count(), 1)
self.assertEqual(clu.dtype, clu.data.dtype)
self.assertEqual(21 * 5, clu.data.shape[0])
self.assertEqual(2, clu.data[1]['i'])
self.assertEqual('N', clu.data[1]['s'])
self.assertEqual(7, clu.data[1]['t'])
self.assertEqual(1, clu.get_emitter_count())
n_low = np.sum(clu.data['z'] < -z0)
self.assertEqual(0, n_low)
n_high = np.sum(clu.data['z'] > z0)
@ -320,12 +331,12 @@ class TestClusterFunctions(unittest.TestCase):
clu.set_emitter(pos=v_pos)
r0 = 2.3
clu.trim_sphere(r0)
self.assertEqual(clu.data.dtype, clu.dtype)
self.assertEqual(clu.data.shape[0], 39)
self.assertEqual(clu.data[1]['i'], 2)
self.assertEqual(clu.data[1]['s'], 'N')
self.assertEqual(clu.data[1]['t'], 7)
self.assertEqual(clu.get_emitter_count(), 1)
self.assertEqual(clu.dtype, clu.data.dtype)
self.assertEqual(39, clu.data.shape[0])
self.assertEqual(2, clu.data[1]['i'])
self.assertEqual('N', clu.data[1]['s'])
self.assertEqual(7, clu.data[1]['t'])
self.assertEqual(1, clu.get_emitter_count())
n_out = np.sum(clu.data['x']**2 + clu.data['y']**2 + clu.data['z'] > r0**2)
self.assertEqual(0, n_out)
@ -355,9 +366,9 @@ class TestClusterFunctions(unittest.TestCase):
def test_trim_slab(self):
clu = self.create_cube()
clu.trim_slab('x', 0.5, 1.1)
self.assertEqual(clu.data.dtype, clu.dtype)
self.assertEqual(clu.data.shape[0], 9 * 2)
self.assertEqual(clu.get_emitter_count(), 1)
self.assertEqual(clu.dtype, clu.data.dtype)
self.assertEqual(9 * 2, clu.data.shape[0])
self.assertEqual(1, clu.get_emitter_count())
def test_save_to_file(self):
clu = self.create_cube()
@ -367,12 +378,34 @@ class TestClusterFunctions(unittest.TestCase):
clu.save_to_file(f, mc.FMT_XYZ, "qwerty", emitters_only=True)
f.seek(0)
line = f.readline()
self.assertEqual(line, b"2\n", b"line 1: " + line)
self.assertEqual(b"2\n", line, b"line 1: " + line)
line = f.readline()
self.assertEqual(line, b"qwerty\n", b"line 2: " + line)
self.assertEqual(b"qwerty\n", line, b"line 2: " + line)
line = f.readline()
self.assertRegexpMatches(line, b"H +[0.]+ +[0.]+ +[0.]+", b"line 3: " + line)
line = f.readline()
self.assertRegexpMatches(line, b"Si +[01.-]+ +[01.-]+ +[0.]+", b"line 4: " + line)
line = f.readline()
self.assertEqual(line, b"", b"end of file")
self.assertEqual(b"", line, b"end of file")
def test_update_atoms(self):
clu = mc.Cluster()
clu.add_atom(1, np.asarray([0, 0, 0]), 1)
clu.add_atom(3, np.asarray([0, 1, 0]), 0)
clu.add_atom(5, np.asarray([-1, 0, 0]), 0)
clu.add_atom(6, np.asarray([0, -1, 0]), 0)
clu.add_atom(2, np.asarray([1, 0, 0]), 0)
clu.add_atom(4, np.asarray([0, 0, 1]), 0)
other = mc.Cluster()
other.add_atom(1, np.asarray([0, 0, 0]), 1)
other.add_atom(5, np.asarray([-1, 0, 0]), 0)
other.add_atom(2, np.asarray([1, 0, 0]), 0)
other.add_atom(6, np.asarray([0, -1, 0]), 0)
other.add_atom(3, np.asarray([0, 1, 0]), 0)
other.add_atom(4, np.asarray([0, 0, 1]), 0)
other.data['c'] = np.asarray((1, 2, 2, 3, 3, 4))
clu.update_atoms(other, {'c'})
expected = np.asarray((1, 3, 2, 3, 2, 4))
np.testing.assert_array_equal(expected, clu.data['c'])

View File

@ -143,7 +143,7 @@ class TestDatabase(unittest.TestCase):
self.db.insert_model(model5)
results = self.db.query_project_params(project_id=project1)
expected = ['parA', 'parB']
self.assertEqual(expected, sorted(results.keys()))
self.assertEqual(expected, sorted(list(results.keys())))
def test_insert_model(self):
self.setup_sample_database()

71
tests/test_grid.py Normal file
View File

@ -0,0 +1,71 @@
"""
@package tests.test_grid
unit tests for pmsco.optimizers.grid
the purpose of these tests is to help debugging the code.
to run the tests, change to the directory which contains the tests directory, and execute =nosetests=.
@pre nose must be installed (python-nose package on Debian).
@author Matthias Muntwiler, matthias.muntwiler@psi.ch
@copyright (c) 2015-19 by Paul Scherrer Institut @n
Licensed under the Apache License, Version 2.0 (the "License"); @n
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import random
import unittest
import pmsco.optimizers.grid as mo
import pmsco.project as mp
class TestPopulation(unittest.TestCase):
def setUp(self):
random.seed(0)
self.domain = mp.Domain()
self.domain.add_param('A', 1.5, 1.0, 2.0, 0.2)
self.domain.add_param('B', 2.5, 2.0, 3.0, 0.25)
self.domain.add_param('C', 3.5, 3.5, 3.5, 0.0)
self.expected_popsize = 30
self.expected_names = ('_model', '_rfac', 'A', 'B', 'C')
self.pop = mo.GridPopulation()
def tearDown(self):
# after each test method
self.pop = None
@classmethod
def setup_class(cls):
# before any methods in this class
pass
@classmethod
def teardown_class(cls):
# teardown_class() after any methods in this class
pass
def test_setup(self):
self.pop.setup(self.domain)
self.assertEqual(self.pop.positions.dtype.names, self.expected_names)
self.assertEqual(self.pop.positions.shape, (self.expected_popsize,))
self.assertEqual(self.pop.model_count, self.expected_popsize)
check = np.arange(self.expected_popsize)
np.testing.assert_array_equal(self.pop.positions['_model'], check)
check = np.ones(self.expected_popsize) * 2.1
np.testing.assert_array_almost_equal(self.pop.positions['_rfac'], check)
if __name__ == '__main__':
unittest.main()

View File

@ -20,14 +20,71 @@ Licensed under the Apache License, Version 2.0 (the "License"); @n
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import unittest
import mock
import numpy as np
import os
import unittest
import pmsco.data as data
import pmsco.dispatch as dispatch
import pmsco.project as project
class TestScan(unittest.TestCase):
"""
test case for @ref pmsco.project.Scan class
"""
def test_import_scan_file(self):
base_dir = os.path.dirname(os.path.abspath(__file__))
test_file = os.path.join(base_dir, "..", "projects", "twoatom", "twoatom_energy_alpha.etpai")
scan = project.Scan()
scan.import_scan_file(test_file, "C", "1s")
mode = ['e', 'a']
self.assertEqual(scan.mode, mode)
ae = np.arange(10, 1005, 5)
at = np.asarray([0])
ap = np.asarray([0])
aa = np.arange(-90, 91, 1)
np.testing.assert_array_almost_equal(scan.energies, ae)
np.testing.assert_array_almost_equal(scan.thetas, at)
np.testing.assert_array_almost_equal(scan.phis, ap)
np.testing.assert_array_almost_equal(scan.alphas, aa)
def test_define_scan(self):
scan = project.Scan()
p0 = np.asarray([20])
p1 = np.linspace(1, 4, 4)
p2 = np.linspace(11, 13, 3)
d = {'t': p1, 'e': p0, 'p': p2}
scan.define_scan(d, "C", "1s")
ae = np.asarray([20])
at = np.asarray([1, 2, 3, 4])
ap = np.asarray([11, 12, 13])
aa = np.asarray([0])
np.testing.assert_array_almost_equal(scan.energies, ae)
np.testing.assert_array_almost_equal(scan.thetas, at)
np.testing.assert_array_almost_equal(scan.phis, ap)
np.testing.assert_array_almost_equal(scan.alphas, aa)
re = np.ones(12) * 20
rt = np.asarray([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
rp = np.asarray([11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13])
ra = np.ones(12) * 0
np.testing.assert_array_almost_equal(scan.raw_data['e'], re)
np.testing.assert_array_almost_equal(scan.raw_data['t'], rt)
np.testing.assert_array_almost_equal(scan.raw_data['p'], rp)
np.testing.assert_array_almost_equal(scan.raw_data['a'], ra)
class TestProject(unittest.TestCase):
def setUp(self):
# before each test method