Statistiques
| Révision :

root / ase / calculators / qmx.py @ 5

Historique | Voir | Annoter | Télécharger (5,84 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
from ase.units import Hartree
12

    
13
from copy import deepcopy
14

    
15
import sys, os
16

    
17
class Qmx(Calculator):
18
    def __init__(self, calculator_high_cluster,  calculator_low_cluster,  calculator_low_system=None,  print_forces=False):
19
        self._constraints=None
20
        
21
        self.calculator_low_cluster = calculator_low_cluster
22
        self.calculator_low_system = calculator_low_system
23
        if self.calculator_low_system is None:
24
            self.calculator_low_system = deepcopy(calculator_low_cluster)
25
        self.calculator_high_cluster = calculator_high_cluster
26
        self.print_forces = print_forces
27

    
28
    def get_energy_subsystem(self, path, calculator, atoms, force_consistent):
29
        # go to directory and calculate energies
30
        print "running energy in: ", path
31
        os.chdir(path)
32
        atoms.set_calculator(calculator)
33
        energy = atoms.get_potential_energy()
34
        os.chdir("..")
35
        return energy
36

    
37
    def get_forces_subsystem(self, path, calculator, atoms):
38
        # go to directory and calculate forces
39
        print "running forces in: ", path
40
        os.chdir(path)
41
        atoms.set_calculator(calculator)
42
        forces = atoms.get_forces()
43
        os.chdir("..")
44
        return forces
45

    
46
    def get_potential_energy(self, embed, force_consistent=False):
47
        # perform energy calculations
48
        e_sys_lo = self.get_energy_subsystem("system.low-level", self.calculator_low_system, embed.get_system(), force_consistent)
49
        e_cl_lo  = self.get_energy_subsystem("cluster.low-level", self.calculator_low_cluster, embed.get_cluster(), force_consistent)
50
        e_cl_hi  = self.get_energy_subsystem("cluster.high-level", self.calculator_high_cluster, embed.get_cluster(), force_consistent)
51
        # calculate energies
52
        energy = e_sys_lo - e_cl_lo + e_cl_hi
53
        # print energies
54
        print "%20s = %15s - %15s + %15s" %("E(C:S)", "E(S-MM)", "E(C-MM)", "E(C-QM)")
55
        print "%20f = %15f - %15f + %15f" %(energy, e_sys_lo, e_cl_lo, e_cl_hi)
56
        # set energies and return
57
        if force_consistent:
58
            self.energy_free = energy
59
            return self.energy_free
60
        else:
61
            self.energy_zero = energy
62
            return self.energy_zero
63

    
64
    def get_forces(self, embed):
65
        atom_map_sys_cl = embed.atom_map_sys_cl
66
        # get forces for the three systems
67
        f_sys_lo = self.get_forces_subsystem("system.low-level", self.calculator_low_system, embed.get_system())
68
        f_cl_lo  = self.get_forces_subsystem("cluster.low-level", self.calculator_low_cluster, embed.get_cluster())
69
        f_cl_hi  = self.get_forces_subsystem("cluster.high-level", self.calculator_high_cluster, embed.get_cluster())
70

    
71
        # forces correction for the atoms
72
        f_cl = f_cl_hi - f_cl_lo
73

    
74
        if self.print_forces:
75
            cluster=embed.get_cluster()
76
            print "Forces: System LOW - Cluster LOW + Cluster HIGH"
77
            for iat_sys in xrange(len(embed)):
78
                print "%-2s (" % embed[iat_sys].get_symbol(),
79
                for idir in xrange(3):
80
                    print "%10.6f" % f_sys_lo[iat_sys][idir], 
81
                print ") <system LOW>"
82

    
83
                iat_cl = atom_map_sys_cl[iat_sys]
84
                if iat_cl > -1:
85
                    print "%s" % "-  (",
86
                    for idir in xrange(3):
87
                        print "%10.6f" % f_cl_lo[iat_cl][idir], 
88
                    print ") <cluster LOW>"
89
                    print "%s" % "+  (",
90
                    for idir in xrange(3):
91
                        print "%10.6f" % f_cl_hi[iat_cl][idir], 
92
                    print ") <cluster HIGH>"
93
                print
94
            print
95
    
96
        # lo-sys + (hi-lo)
97
        for iat_sys in xrange(len(embed)):
98
            iat_cl = atom_map_sys_cl[iat_sys]
99
            if iat_cl > -1:
100
                f_sys_lo[iat_sys] += f_cl[iat_cl]
101
        # some settings for the output
102
        i_change = np.zeros(len(embed), int)
103
        if self.print_forces:
104
            f_sys_lo_orig = f_sys_lo.copy()
105
        # correct gradients
106
        # Reference: Eichler, Koelmel, Sauer, J. of Comput. Chem., 18(4). 1997, 463-477.
107
        for cell_L, iat_cl_sys, iat_sys, r, iat_link in embed.linkatoms:
108
            # calculate the bond distance (r_bond) at the border
109
            xyz = embed[iat_sys].get_position() - embed[iat_cl_sys].get_position() + cell_L
110
            # calculate the bond lenght and the factor f
111
            rbond = np.sqrt(np.dot(xyz, xyz))
112
            f = r / rbond
113
            #normalize xyz
114
            xyz /= rbond
115
            # receive the gradients for the link atom
116
            fL = f_cl[iat_link]
117
            # dot product fL, xyz
118
            fs = np.dot(fL, xyz)
119
            # apply corrections for each direction
120
            i_change[iat_sys] = 1
121
            i_change[iat_cl_sys] = 1
122
            for idir in xrange(3):
123
                # correct the atom in the system
124
                f_sys_lo[iat_sys][idir] = f_sys_lo[iat_sys][idir] + f*fL[idir] - f*fs*xyz[idir]
125
                # correct the atom in the cluster
126
                f_sys_lo[iat_cl_sys][idir] = f_sys_lo[iat_cl_sys][idir] + (1-f)*fL[idir] + f*fs*xyz[idir]
127

    
128
        if self.print_forces:
129
            print " TOTAL FORCE (uncorrected : corrected) for link atoms"
130
            for iat_sys in xrange(len(embed)):
131
                print "%-2s (" % embed[iat_sys].get_symbol(),
132
                for idir in xrange(3):
133
                    print "%10.6f" % f_sys_lo_orig[iat_sys][idir], 
134
                print ") : (", 
135
                for idir in xrange(3):
136
                    print "%10.6f" % f_sys_lo[iat_sys][idir], 
137
                print ")", 
138
                if i_change[iat_sys]:
139
                    print " *", 
140
                print
141
            print
142
        
143
        return f_sys_lo