-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathljcalculator.py
More file actions
executable file
·59 lines (42 loc) · 1.66 KB
/
ljcalculator.py
File metadata and controls
executable file
·59 lines (42 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
"""Demonstration of using PairQuantity class for calculation of Lennard
Jones potential.
Vij = 4 * ( rij ** -12 - rij ** -6 )
"""
import sys
from diffpy.srreal.pairquantity import PairQuantity
from diffpy.structure import Structure
class LennardJonesCalculator(PairQuantity):
# Initialization. The size of the result array is always 1.
def __init__(self):
PairQuantity.__init__(self)
self._resizeValue(1)
return
def __call__(self, structure):
"""Return LJ potential for a specified structure."""
values = self.eval(structure)
return values[0]
def _addPairContribution(self, bnds, sumscale):
"""Add Lennard-Jones contribution from a single pair of atoms.
bnds -- a BaseBondGenerator instance that contains information
about the current pair of atom sites.
sumscale -- integer multiplicity of the current pair. It may
be negative in case of fast value updates.
No return value. Updates _value, the internal results array.
"""
rij = bnds.distance()
ljij = 4 * (rij**-12 - rij**-6)
self._value[0] += sumscale * ljij / 2.0
return
# class LennardJonesCalculator
def main():
# load structure from a specified file, by default "lj50.xyz"
filename = len(sys.argv) > 1 and sys.argv[1] or "lj50.xyz"
stru = Structure()
stru.read(filename)
# create an instance of LennardJonesCalculator
ljcalc = LennardJonesCalculator()
# calculate and print the LJ potential.
print("LJ potential of %s is %g" % (filename, ljcalc(stru)))
if __name__ == "__main__":
main()