def test_cell_tools(): # test known values: simple cubic cell_a = 5.0 cell = np.identity(3) * cell_a volume = cell_a**3.0 cryst_const = np.array([cell_a] * 3 + [90.0] * 3) np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const) np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell) np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const)) np.testing.assert_almost_equal(volume, crys.volume_cell(cell)) np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell) np.testing.assert_array_almost_equal( crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const) # random # # volume : volume_cc() always returns positive values, whereas det() and # volume_cell() may return the volume with negative sign but correct # magnitude. # cell : A random cell does also have a random orientation in space. It # does NOT conform the usual convention: a along x, b in x-y plane. # However, the cryst_const and volume must be invariant. cell = np.random.rand(3, 3) cryst_const = crys.cell2cc(cell) volume = abs(np.linalg.det(cell)) np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const)) np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell))) # this must always fail for random cells try: np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell) except AssertionError: pass # Here, we convert cryst_const to a *different* cell which conforms to the # orientation convention, and back to cryst_const. np.testing.assert_array_almost_equal( crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const) # 3d cell = rand(100, 3, 3) cc = crys.cell2cc3d(cell, axis=0) vol_cell = np.abs(crys.volume_cell3d(cell, axis=0)) vol_cc = crys.volume_cc3d(cc, axis=0) assert crys.cell2cc3d(cell, axis=0).shape == (100, 6) assert crys.cc2cell3d(cc, axis=0).shape == (100, 3, 3) assert vol_cc.shape == (100, ) assert vol_cell.shape == (100, ) aaae(vol_cell, vol_cc) aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
def test_cell_tools(): # test known values: simple cubic cell_a = 5.0 cell = np.identity(3)*cell_a volume = cell_a**3.0 cryst_const = np.array([cell_a]*3 + [90.0]*3) np.testing.assert_array_almost_equal(crys.cell2cc(cell), cryst_const) np.testing.assert_array_almost_equal(crys.cc2cell(cryst_const), cell) np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const)) np.testing.assert_almost_equal(volume, crys.volume_cell(cell)) np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell) np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const) # random # # volume : volume_cc() always returns positive values, whereas det() and # volume_cell() may return the volume with negative sign but correct # magnitude. # cell : A random cell does also have a random orientation in space. It # does NOT conform the usual convention: a along x, b in x-y plane. # However, the cryst_const and volume must be invariant. cell = np.random.rand(3,3) cryst_const = crys.cell2cc(cell) volume = abs(np.linalg.det(cell)) np.testing.assert_almost_equal(volume, crys.volume_cc(cryst_const)) np.testing.assert_almost_equal(volume, abs(crys.volume_cell(cell))) # this must always fail for random cells try: np.testing.assert_array_almost_equal(crys.cc2cell(crys.cell2cc(cell)), cell) except AssertionError: pass # Here, we convert cryst_const to a *different* cell which conforms to the # orientation convention, and back to cryst_const. np.testing.assert_array_almost_equal(crys.cell2cc(crys.cc2cell(cryst_const)), cryst_const) # 3d cell = rand(100,3,3) cc = crys.cell2cc3d(cell, axis=0) vol_cell = np.abs(crys.volume_cell3d(cell, axis=0)) vol_cc = crys.volume_cc3d(cc, axis=0) assert crys.cell2cc3d(cell, axis=0).shape == (100,6) assert crys.cc2cell3d(cc, axis=0).shape == (100,3,3) assert vol_cc.shape == (100,) assert vol_cell.shape == (100,) aaae(vol_cell, vol_cc) aaae(crys.cell2cc3d(crys.cc2cell3d(cc)), cc)
def get_random_cryst_const(self): """Create random cryst_const. Returns ------- cryst_const : 1d array (6,) Raises ------ RandomStructureFail """ def _get(): return np.concatenate( (uniform(self.length_range[0], self.length_range[1], 3), uniform(self.angle_range[0], self.angle_range[1], 3))) cnt = 1 while cnt <= self.cell_maxtry: cc = _get() vol = volume_cc(cc) if not self.vol_range[0] <= vol <= self.vol_range[1]: cc = _get() cnt += 1 else: self.counters['cryst_const'] = cnt return cc raise RandomStructureFail("failed creating random cryst_const")
def get_random_cryst_const(self): """Create random cryst_const. Returns ------- cryst_const : 1d array (6,) Raises ------ RandomStructureFail """ def _get(): return np.concatenate((uniform(self.length_range[0], self.length_range[1], 3), uniform(self.angle_range[0], self.angle_range[1], 3))) cnt = 1 while cnt <= self.cell_maxtry: cc = _get() vol = volume_cc(cc) if not self.vol_range[0] <= vol <= self.vol_range[1]: cc = _get() cnt += 1 else: self.counters['cryst_const'] = cnt return cc raise RandomStructureFail("failed creating random cryst_const")