Statistiques
| Révision :

root / ase / calculators / qmx.py @ 2

Historique | Voir | Annoter | Télécharger (3,69 ko)

1
""" This is a QM:MM embedded system for ASE
2

3
torsten.kerber@ens-lyon.fr
4
"""
5

    
6
import ase
7
import ase.atoms
8
import numpy as np
9
from general import Calculator
10
from ase.embed import Embed
11

    
12
import sys, os
13

    
14
class Qmx(Calculator):
15
    def __init__(self, calculator_low, calculator_high):
16
        self.string_params = {}
17
        
18
        self.forces=np.zeros((2,2))
19
        self._constraints=None
20
        
21
        self.calculator_low = calculator_low
22
        self.calculator_high = calculator_high
23
                
24
    def get_energy_subsystem(self, path, calculator, atoms, force_consistent):
25
        os.chdir(path)
26
        calculator.set_atoms(atoms)
27
        energy = calculator.get_potential_energy(atoms)
28
        os.chdir("..")
29
        return energy
30
        
31
    def get_forces_subsystem(self, path, calculator, atoms):
32
        os.chdir(path)
33
        calculator.set_atoms(atoms)
34
        forces = calculator.get_forces(atoms)
35
        os.chdir("..")
36
        return forces
37
        
38
        
39
    def get_potential_energy(self, embed, force_consistent=False):
40
        # perform energy calculations 
41
        e_sys_lo = self.get_energy_subsystem("system.low-level", self.calculator_low, embed.get_system(), force_consistent)
42
        e_cl_lo  = self.get_energy_subsystem("cluster.low-level", self.calculator_low, embed.get_cluster(), force_consistent)
43
        e_cl_hi  = self.get_energy_subsystem("cluster.high-level", self.calculator_high, embed.get_cluster(), force_consistent)
44
        # calculate energies
45
        energy = e_sys_lo - e_cl_lo + e_cl_hi
46
        print "%20s=%15s - %15s + %15s" %("E(C:S)", "E(S-MM)", "E(C-MM)", "E(C-QM)")
47
        print "%20f=%15f - %15f + %15f" %(energy, e_sys_lo, e_cl_lo, e_cl_hi)
48
        if force_consistent:
49
            self.energy_free = energy
50
            return self.energy_free
51
        else:
52
            self.energy_zero = energy
53
            return self.energy_zero
54

    
55
    def get_forces(self, embed):
56
        atom_map_sys_cl = embed.atom_map_sys_cl
57
        # get forces for the three systems
58
        f_sys_lo = self.get_forces_subsystem("system.low-level", self.calculator_low, embed.get_system())
59
        f_cl_lo  = self.get_forces_subsystem("cluster.low-level", self.calculator_low, embed.get_cluster())
60
        f_cl_hi  = self.get_forces_subsystem("cluster.high-level", self.calculator_high, embed.get_cluster())
61
        # forces correction for the atoms
62
        f_cl = f_cl_hi - f_cl_lo
63
        #number of atoms
64
        nat_sys = len(embed)
65
        # lo-sys + (hi-lo)
66
        for iat_sys in xrange(nat_sys):
67
            iat_cl = atom_map_sys_cl[iat_sys]
68
            if iat_cl > -1:
69
                f_sys_lo[iat_sys] += f_cl[iat_cl]
70
        
71
        # correct gradients
72
        # Reference: Eichler, Koelmel, Sauer, J. of Comput. Chem., 18(4). 1997, 463-477.
73
        for cell_L, iat_cl, iat_sys, r, iat_link in embed.linkatoms:
74
            # calculate the bond distance (r_bond) at the border
75
            xyz = embed[iat_sys].get_position() - embed.get_cluster()[iat_cl].get_position() + cell_L
76
            
77
            # calculate the bond lenght and the factor f
78
            rbond = np.sqrt(np.dot(xyz, xyz))
79
            f = r / rbond
80
            #normalize xyz
81
            xyz /= rbond
82
            
83
            # receive the gradients for the link atom
84
            fH = f_cl[iat_link]
85
            # Skalarprodukt fH, xyz
86
            fs = np.dot(xyz, fH)
87
            
88
            for idir in xrange(3):
89
                # correct the atom in the system
90
                f_sys_lo[iat_sys][idir] += f*fH[idir] - f*fs*xyz[idir]
91
                # correct the atom in the cluster
92
                f_sys_lo[iat_cl][idir] += (1-f)*fH[idir] + f*fs*xyz[idir]
93
        return f_sys_lo
94

    
95
    def set_atoms(self, atoms):
96
        return
97
           
98
            
99