neurochem_calculator.py 3.17 KB
Newer Older
1
2
3
4
5
6
7
import os
import ase
import pyNeuroChem
import ase_interface
import numpy

path = os.path.dirname(os.path.realpath(__file__))
8
builtin_path = os.path.join(path, '../../torchani/resources/ani-1x_8x/')
9
10
11
12
13
14
15
const_file = os.path.join(builtin_path, 'rHCNO-5.2R_16-3.5A_a4-8.params')
sae_file = os.path.join(builtin_path, 'sae_linfit.dat')
network_dir = os.path.join(builtin_path, 'train0/networks/')
radial_length = 64
conv_au_ev = 27.21138505
all_species = ['H', 'C', 'N', 'O']
species_indices = {all_species[i]: i for i in range(len(all_species))}
16
nc = pyNeuroChem.molecule(const_file, sae_file, network_dir, 0)
17
18


19
20
21
22
23
def calc():
    calc = ase_interface.ANI(False)
    calc.setnc(nc)
    return calc

24

25
class NeuroChem:
26
27

    def _get_radial_part(self, fullaev):
28
29
30
31
        if len(fullaev.shape) == 3:
            return fullaev[:, :, :radial_length]
        assert len(fullaev.shape) == 2
        return fullaev[:, :radial_length]
32
33

    def _get_angular_part(self, fullaev):
34
35
36
37
        if len(fullaev.shape) == 3:
            return fullaev[:, :, radial_length:]
        assert len(fullaev.shape) == 2
        return fullaev[:, radial_length:]
38
39
40
41

    def _per_conformation(self, coordinates, species):
        atoms = coordinates.shape[0]
        mol = ase.Atoms(''.join(species), positions=coordinates)
42
        mol.set_calculator(calc())
43
44
45
46
47
48
        energy = mol.get_potential_energy() / conv_au_ev
        aevs = [mol.calc.nc.atomicenvironments(j) for j in range(atoms)]
        force = mol.get_forces() / conv_au_ev
        aevs = numpy.stack(aevs)
        return aevs, energy, force

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def __call__(self, args):
        if len(args) == 2:
            return self.from_coordinates_and_species(*args)
        return self.from_atoms_obj(args)

    def from_atoms_obj(self, mol):
        natoms = len(mol)
        mol = mol.copy()
        mol.set_calculator(calc())
        energy = mol.get_potential_energy() / conv_au_ev
        aevs = [mol.calc.nc.atomicenvironments(j) for j in range(natoms)]
        aevs = numpy.stack(aevs)
        force = mol.get_forces() / conv_au_ev
        cell = mol.get_cell(complete=True)
        pbc = mol.get_pbc().astype(numpy.uint8)
        coordinates = mol.get_positions()
        species = numpy.array([species_indices[i] for i in mol.get_chemical_symbols()])
        return coordinates, species, self._get_radial_part(aevs), \
            self._get_angular_part(aevs), energy, force, cell, pbc

    def from_coordinates_and_species(self, coordinates, species):
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        if len(coordinates.shape) == 2:
            coordinates = coordinates.reshape(1, -1, 3)
        conformations = coordinates.shape[0]
        results = [self._per_conformation(
            coordinates[i], species) for i in range(conformations)]
        aevs, energies, forces = zip(*results)
        aevs = numpy.stack(aevs)
        energies = numpy.stack(energies)
        forces = numpy.stack(forces)
        species = numpy.array([species_indices[i] for i in species])
        species = species.reshape(1, -1)
        species = numpy.broadcast_to(species,
                                     (coordinates.shape[0], species.shape[1]))
        return coordinates, species, self._get_radial_part(aevs), \
            self._get_angular_part(aevs), energies, forces