Пример #1
0
def create_random_xyz_traj_to_write(request):

    natoms, frames, comment, expected_cell, precision = request.param

    a, b, c, alpha, beta, gamma = mt.h2abc_deg(expected_cell)

    fmt_header = "# CELL(abcABC): %10.5f  %10.5f  %10.5f  %10.5f  %10.5f  %10.5f  %s"

    comment = fmt_header % (a, b, c, alpha, beta, gamma, comment)

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms, frames, comment)
    filedesc.seek(0)

    masses = [Elements.mass(_am) for _am in atom_names]

    cell_list = []
    atoms_list = []

    for _fr in xrange(frames):
        cell = Cell(expected_cell)
        atoms = Atoms(natoms)
        atoms.q[:] = xyz[_fr * natoms * 3:(_fr + 1) * natoms * 3]
        atoms.names = atom_names[_fr * natoms:(_fr + 1) * natoms]
        atoms.m[:] = masses[_fr * natoms:(_fr + 1) * natoms]
        atoms_list.append(atoms)
        cell_list.append(cell)

    return (filedesc, atoms_list, cell_list, comment, precision)
Пример #2
0
def test_process_units_object(units_preparation):

    natoms, frames, comment, conver_xyz, conver_cell = units_preparation
    output = 'objects'

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(
        natoms, frames, comment)

    cell = mt.abc2h(1.0, 1.0, 1.0, np.pi / 2.0, np.pi / 2.0, np.pi / 2.0)

    masses = []
    _count = 0

    for _at in atom_names:
        print(Elements.mass(_at), _count, _at)
        masses.append(Elements.mass(_at))
        _count += 1

    masses = np.array(masses)

    res = testing.process_units(comment,
                                cell.copy(),
                                xyz.copy(),
                                np.array(atom_names).copy(),
                                np.array(masses).copy(),
                                output=output)

    npt.assert_array_almost_equal(res['atoms'].q, xyz * conver_xyz, 5)
    npt.assert_array_almost_equal(res['atoms'].m, masses, 5)
    npt.assert_array_almost_equal(res['cell'].h, cell * conver_cell, 5)
    npt.assert_array_equal(res['atoms'].names, atom_names, 5)
Пример #3
0
def test_process_units_noobj(units_preparation):

    natoms, frames, comment, conver_xyz, conver_cell = units_preparation
    output = 'noobj'

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(
        natoms, frames, comment)

    cell = mt.abc2h(1.0, 1.0, 1.0, np.pi / 2.0, np.pi / 2.0, np.pi / 2.0)

    masses = []
    _count = 0
    print('atom_names', atom_names)
    for _at in atom_names:
        print(Elements.mass(_at), _count, _at)
        masses.append(Elements.mass(_at))
        _count += 1

    masses = np.array(masses)
    type(masses)
    res = testing.process_units(comment,
                                cell.copy(),
                                xyz.copy(),
                                np.array(atom_names).copy(),
                                np.array(masses).copy(),
                                output=output)

    print(xyz, res['data'])
    npt.assert_array_almost_equal(res['data'], xyz * conver_xyz, 5)
    npt.assert_array_almost_equal(res['masses'], masses, 5)
    npt.assert_array_almost_equal(res['cell'], cell * conver_cell, 5)
    npt.assert_array_equal(res['names'], atom_names, 5)
    assert res['natoms'] == natoms
Пример #4
0
def test_process_units_object(units_preparation):

    natoms, frames, comment, conver_xyz, conver_cell = units_preparation
    output = 'objects'

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms,
                                                          frames, comment)

    cell = mt.abc2h(1.0, 1.0, 1.0, np.pi / 2.0, np.pi / 2.0, np.pi / 2.0)

    masses = []
    _count = 0

    for _at in atom_names:
        print Elements.mass(_at), _count, _at
        masses.append(Elements.mass(_at))
        _count += 1

    masses = np.array(masses)

    res = testing.process_units(comment, cell.copy(), xyz.copy(), np.array(atom_names).copy(), np.array(masses).copy(), output=output)

    npt.assert_array_almost_equal(res['atoms'].q, xyz * conver_xyz, 5)
    npt.assert_array_almost_equal(res['atoms'].m, masses, 5)
    npt.assert_array_almost_equal(res['cell'].h, cell * conver_cell, 5)
    npt.assert_array_equal(res['atoms'].names, atom_names, 5)
Пример #5
0
def test_process_units_noobj(units_preparation):

    natoms, frames, comment, conver_xyz, conver_cell = units_preparation
    output = 'noobj'

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms,
                                                          frames, comment)

    cell = mt.abc2h(1.0, 1.0, 1.0, np.pi / 2.0, np.pi / 2.0, np.pi / 2.0)

    masses = []
    _count = 0
    print 'atom_names', atom_names
    for _at in atom_names:
        print Elements.mass(_at), _count, _at
        masses.append(Elements.mass(_at))
        _count += 1

    masses = np.array(masses)
    type(masses)
    res = testing.process_units(comment, cell.copy(), xyz.copy(), np.array(atom_names).copy(), np.array(masses).copy(), output=output)

    print xyz, res['data']
    npt.assert_array_almost_equal(res['data'], xyz * conver_xyz, 5)
    npt.assert_array_almost_equal(res['masses'], masses, 5)
    npt.assert_array_almost_equal(res['cell'], cell * conver_cell, 5)
    npt.assert_array_equal(res['names'], atom_names, 5)
    assert res['natoms'] == natoms
Пример #6
0
def create_xyz_sample_file(request):
    """ Create a fake xyz file and build the atoms and cell object from it.
    """

    natoms, frames, comment, expected_cell, units_conv_at, units_conv_cell = request.param

    filedesc, xyz, atoms_names = xyz_gen.xyz_traj_filedesc(natoms, frames, comment)

    # init_file needs to read from a real file...
    tmp_file = tmp.NamedTemporaryFile(mode='wr', prefix='ipi_testing-tmp', delete=False)
    tmp_file.seek(0)
    tmp_file.write(filedesc.read())
    tmp_file.close()
    filedesc.close()

    masses = np.zeros(natoms * frames)
    for _ii, _at in enumerate(atoms_names):
        masses[_ii] = Elements.mass(_at)

    ratoms = []
    for _fr in range(frames):
        ratoms.append(Atoms(natoms))
        ratoms[-1].q = xyz[_fr * natoms * 3:3 * (_fr + 1) * natoms] * units_conv_at
        ratoms[-1].m = masses[_fr * natoms:(_fr + 1) * natoms]
        ratoms[-1].names = atoms_names[_fr * natoms:(_fr + 1) * natoms]

    cell = Cell(expected_cell * units_conv_cell)

    # remove temp file created during this test
    def delete_files_after_testing():
        if os.path.isfile(tmp_file.name):
            os.remove(tmp_file.name)

    request.addfinalizer(delete_files_after_testing)
    return tmp_file, cell, ratoms
Пример #7
0
def prepare_read_file(request):

    natoms, nframes, comment, output_type, file_type, expected_cell, unit_conv_cell, unit_conv_q = request.param

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms, nframes, comment)

    expected_q = xyz.copy()
    expected_names = atom_names[:]

    # if comment.find('CELL') < 0:
    #     unit_conv_cell = 1.0

    return file_type, filedesc, output_type, expected_q, expected_cell, expected_names, unit_conv_cell, unit_conv_q
Пример #8
0
def prepare_read_file(request):

    natoms, nframes, comment, output_type, file_type, expected_cell, unit_conv_cell, unit_conv_q  = request.param

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms, nframes, comment)

    expected_q = xyz.copy()
    expected_names = atom_names[:]

    if comment.find('CELL') < 0:
        unit_conv_cell = 1.0

    return file_type, filedesc, output_type, expected_q, expected_cell, expected_names, unit_conv_cell, unit_conv_q
Пример #9
0
def create_random_xyz_traj_to_read(request):

    natoms, frames, comment, expected_cell, precision = request.param

    filedesc, xyz, atom_names = xyz_gen.xyz_traj_filedesc(natoms,
                                                          frames, comment)
    filedesc.seek(0)

    check_comment = re.match('# CELL[\(\[\{]H[\)\]\}]: ([-0-9\.?Ee ]*)\s*', comment)  # pylint: disable=W1401

    if check_comment:
        genh = np.array(check_comment.group(1).split()[:9], float).reshape((3, 3))
        invgenh = np.linalg.inv(genh)
        for _ui in xrange(natoms * frames):
            _uu = np.array([xyz[3 * _ui], xyz[3 * _ui + 1], xyz[3 * _ui + 2]])
            _us = np.dot(_uu, invgenh)
            _uu = np.dot(expected_cell, _us)
            xyz[3 * _ui], xyz[3 * _ui + 1], xyz[3 * _ui + 2] = _uu

    return (filedesc, xyz, atom_names, natoms, frames,
            comment, expected_cell, precision)