Example #1
0
def test_random_distance():

    system = System(box_size=[10, 10, 10])
    methane = Molecule(os.path.join(here, 'data', 'methane.xyz'))
    system.add_molecules(methane, n=4)

    config = system.random(min_dist_threshold=1.5)

    for coord in config.coordinates():
        assert np.min(coord) > 0.0
        assert np.max(coord) < 10 - 1.5
Example #2
0
def test_perturbation():

    system = System(box_size=[7, 7, 7])
    system.add_molecules(h2o, n=5)
    config = system.random(min_dist_threshold=1.5, sigma=0.1, max_length=0.2)

    pcoords = config.coordinates()

    # Adding a displacement to each atom should still afford a reasonably
    # sensible structure (no short distances)
    dist_matrix = distance_matrix(pcoords, pcoords)
    dist_matrix += np.identity(len(pcoords))
    assert np.min(dist_matrix) > 0.6
Example #3
0
def test_density():
    """Test the density calculation with a few water boxes"""

    req_density = 1.0
    mw = 18.02  # Density of water

    for n_waters in [1, 2, 3, 5, 7, 127]:
        length = (((mw * n_waters / (6.022E23 * req_density)) / 1E6)**(1 / 3) *
                  1E10)

        system = System(box_size=[length, length, length])
        system.add_molecules(h2o, n=n_waters)

        assert np.abs(system.density - req_density) < 1E-3
Example #4
0
def test_autode_parallel_orca():

    if 'GT_ORCA' not in os.environ or not os.environ['GT_ORCA'] == 'True':
        return

    # Check that orca runs work
    water = System(box_size=[10, 10, 10])
    water.add_molecules(h2o, n=2)

    configs = Data()
    for _ in range(3):
        configs += water.random(with_intra=True)

    configs.parallel_orca()
Example #5
0
def test_autode_xtb():

    if 'GT_XTB' not in os.environ or not os.environ['GT_XTB'] == 'True':
        return

    # Check that orca and xtb runs work
    water = System(box_size=[10, 10, 10])
    water.add_molecules(h2o, n=5)

    config = water.random()
    config.run_xtb()

    assert config.energy is not None
    assert config.forces is not None
Example #6
0
def test_print_gro_file():

    water_box = System(box_size=[10, 10, 10])
    water_box.add_molecules(molecule=get_solvent('h2o'), n=10)

    for molecule in water_box.molecules:
        molecule.set_mm_atom_types()

    config = Configuration(system=water_box)
    config.wrap()
    config.print_gro_file(system=water_box)

    assert os.path.exists('input.gro')
    os.remove('input.gro')
Example #7
0
def test_n_mols():

    system = System(box_size=[5, 5, 5])
    system.add_molecules(methane, n=1)
    system.add_molecules(h2o, n=1)
    assert system.n_unique_molecules == 2

    system.add_molecules(h2o, n=5)
    assert system.n_unique_molecules == 2
Example #8
0
def test_grid_positions():

    n_molecules = 10
    density = 0.786  # g cm-3
    mw = 3 * 1.01 + 2 * 12.01 + 14.01
    length = ((mw * n_molecules / (6.022E23 * density)) / 1E6)**(1 / 3) * 1E10

    system = System(box_size=[length, length, length])
    system.add_molecules(Molecule(os.path.join(here, 'data', 'mecn.xyz')),
                         n=n_molecules)

    config = system.grid(min_dist_threshold=1.7)
    config.save(filename='test_random.xyz')

    assert os.path.exists('test_random.xyz')

    os.remove('test_random.xyz')
Example #9
0
def test_random_grid_positions():

    system = System(box_size=[10, 12, 14])
    methane = Molecule(os.path.join(here, 'data', 'methane.xyz'))
    system.add_molecules(methane, n=5)

    config = system.random(grid=True)
    config.save(filename='test_random.xyz')

    # Minimum pairwise distance should be ~ the C-H distance (1.109 Å)
    atoms = xyz_file_to_atoms('test_random.xyz')
    coords = np.array([atom.coord for atom in atoms])
    dist_matrix = distance_matrix(coords, coords)

    # Distance matrix has zeros along the diagonals so add the identity
    assert np.min(dist_matrix + 9 * np.identity(len(coords))) > 1.1

    os.remove('test_random.xyz')
Example #10
0
def test_histogram():

    water_dimer = System(box_size=[7, 7, 7])
    water_dimer.add_molecules(h2o, n=2)

    data = Data(name='test')
    data += water_dimer.random()

    # Can't histogram energies and forces with no energies
    with pytest.raises(NoEnergy):
        data.histogram(name='test')

    data[0].energy = 1
    data[0].forces = np.zeros(shape=(6, 3))

    data.histogram(name='test')
    assert os.path.exists('test.png')
    os.remove('test.png')
Example #11
0
def test_gap():

    water_dimer = System(box_size=[7, 7, 7])
    water_dimer.add_molecules(h2o, n=2)

    data = Data(name='test')
    for _ in range(2):
        data += water_dimer.random()

    assert len(data) == 2

    coords1 = data[0].coordinates()
    assert coords1.shape == (6, 3)

    coords2 = data[1].coordinates()

    # Coordinates should be somewhat different
    assert np.min(distance_matrix(coords1, coords2)) > 1E-3
Example #12
0
def test_dftb_plus():

    water_box = System(box_size=[5, 5, 5])

    config = water_box.configuration()
    config.set_atoms(xyz_filename=os.path.join(here, 'data', 'h2o_10.xyz'))

    if 'GT_DFTB' not in os.environ or not os.environ['GT_DFTB'] == 'True':
        return

    config.run_dftb()
    assert config.energy is not None

    forces = config.forces
    assert type(forces) is np.ndarray
    assert forces.shape == (30, 3)

    # Should all be non-zero length force vectors in ev Å^-1
    assert all(0 < np.linalg.norm(force) < 70 for force in forces)
Example #13
0
def test_load():

    xyz_path = os.path.join(here, 'data', 'configs.xyz')

    configs = ConfigurationSet()
    configs.load(xyz_path)

    assert len(configs) == 2

    # Should default to uncharged singlet
    for config in configs:
        assert config.box is not None
        assert config.charge == 0
        assert config.mult == 1

    zn_aq = System(gt.Ion('Zn', charge=2), box_size=[12, 12, 12])
    zn_aq.add_solvent('h2o', n=52)

    configs = ConfigurationSet()
    configs.load(xyz_path, system=zn_aq)
    for config in configs:
        assert config.box is not None
        assert config.charge == 2
        assert config.mult == 1

    configs = ConfigurationSet()
    configs.load(xyz_path, charge=2)
    for config in configs:
        assert config.box is not None
        assert config.charge == 2
        assert config.mult == 1

    with open('tmp.xyz', 'w') as test_xyz:
        print('1\n', file=test_xyz)

    # Should fail to load with no atoms in the xyz file
    configs = ConfigurationSet()
    with pytest.raises(LoadingFailed):
        configs.load(filename='tmp.xyz')

    os.remove('tmp.xyz')
Example #14
0
def test_with_charges():

    # Slightly modified ASE calculator to be compatible with the latest DFTB+
    calculator = DFTB()
    assert isinstance(calculator, Dftb)

    # Can't get the fermi level with no calculation run
    with pytest.raises(TypeError):
        calculator.read_fermi_levels()

    if 'GT_DFTB' not in os.environ or not os.environ['GT_DFTB'] == 'True':
        return

    # Check that charges work
    na_water = System(box_size=[10, 10, 10])
    na_water.add_molecules(Ion('Na', charge=1), n=1)
    na_water.add_molecules(h2o, n=5)

    assert na_water.charge() == 1

    config = na_water.random()
    config.run_dftb()
Example #15
0
def test_system():

    system = System(box_size=[5, 5, 5])
    # No molecules yet in the system
    assert len(system) == 0

    system += h2o

    assert len(system) == 1

    system.add_molecules(h2o, 10)
    assert len(system) == 11

    two_waters = [h2o, h2o]
    system += two_waters
    assert len(system) == 13
    assert system.n_unique_molecules == 1

    assert str(system) == 'H2O_13' or str(system) == 'OH2_13'

    # Should be able to print an xyz file of the configuration
    system.configuration().save(filename='test.xyz')
    assert os.path.exists('test.xyz')
    os.remove('test.xyz')
Example #16
0
from gaptrain.loss import RMSE
from gaptrain.configurations import ConfigurationSet
from gaptrain.systems import System
import numpy as np

system = System(box_size=[10, 10, 10])
system.add_solvent('h2o', n=3)


def test_rmse():

    configs = ConfigurationSet()
    for _ in range(2):
        configs += system.random()

    configs[0].energy = 1
    configs[1].energy = 2

    true_configs = configs.copy()
    true_configs[0].energy = 1.1
    true_configs[1].energy = 1.8

    rmse = RMSE(configs, true_configs)

    expected = np.sqrt(((1 - 1.1)**2 + (2 - 1.8)**2) / 2.0)
    assert np.abs(rmse.energy - expected) < 1E-6
Example #17
0
from gaptrain.exceptions import LoadingFailed
from gaptrain.systems import System
from gaptrain.molecules import Molecule
from gaptrain.solvents import get_solvent
import gaptrain as gt
import numpy as np
import pytest
import ase
import os

here = os.path.abspath(os.path.dirname(__file__))
h2o = Molecule(os.path.join(here, 'data', 'h2o.xyz'))


side_length = 7.0
system = System(box_size=[side_length, side_length, side_length])
system.add_molecules(h2o, n=3)


def test_print_exyz():

    configs = ConfigurationSet(name='test')

    for _ in range(5):
        configs += system.random()

    configs.save()
    assert os.path.exists('test.xyz')
    os.remove('test.xyz')

    # If the energy and forces are set for all the configurations an exyz