-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeNanoCrystalCalculation.py
More file actions
281 lines (228 loc) · 16.4 KB
/
Copy pathmakeNanoCrystalCalculation.py
File metadata and controls
281 lines (228 loc) · 16.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import pickle
import numpy as np
#def optimalZRotation(pts1, pts2):
# ''' Find best overlay (which way around?) for angle around z '''
# from scipy.optimize import fmin #clearly analytic result must be possible #powells method may be better?
# from quaternions import quaternion_rotatn, make_quat_rot2
# from listMathsSlow import rmsd
# def functionOfOverlay(angle, pts1, pts2):
# _quat = make_quat_rot2(angle, np.array([0., 0., 1.]))
# rotatedPts1 = np.array([quaternion_rotatn(x, _quat) for x in pts1])
# return rmsd(rotatedPts1, pts2)
## optimisationOutputs = fmin(functionOfOverlay, 0., (pts1, pts2), full_output=True)
# optimisationOutputs = fmin(functionOfOverlay, 0., (pts2, pts1), full_output=True)
## print optimisationOutputs
# return (make_quat_rot2(optimisationOutputs[0][0], np.array([0., 0., 1.])),
# optimisationOutputs[1])
#print 'draw the molecules to check sort is picking the lowest one along c'
def calculatePerfectSurfaceAngles(surfaceCif, nanocrystalCif):
''' look at bottom molecule surface and return angle such that nanocrystal will match this '''
from analyseClusters import Crystal
from listMathsSlow import overlay_points_RMSD
from quaternions import zyz_angles
surfaceCrystal = Crystal.fromCif(surfaceCif)
nanoCrystal = Crystal.fromCif(nanocrystalCif)
#nanoCrystal is lowest along c axis
nanoCrystal.asymmetricMolecules = [sorted(nanoCrystal.asymmetricMolecules, key = lambda x: np.dot(x.aseAtoms.get_center_of_mass(scaled=False),
nanoCrystal.aseCell[2]))[0]]
#surfaceCrystal is just lowest along c axis (dont need any more at this stage - just want to make 'perfect' cut)
surfaceCrystal.asymmetricMolecules = [sorted(surfaceCrystal.asymmetricMolecules, key = lambda x: np.dot(x.aseAtoms.get_center_of_mass(scaled=False),
surfaceCrystal.aseCell[2]))[0]]
#should assert that both are in a unit cell 0,1
#
nanoAtoms = nanoCrystal.asymmetricMolecules[0].aseAtoms.get_positions()[7:13,:]
surfaceAtoms = surfaceCrystal.asymmetricMolecules[0].aseAtoms.get_positions()[7:13,:]
# print nanoCrystal.asymmetricMolecules[0].aseAtoms.get_chemical_symbols(), surfaceCrystal.asymmetricMolecules[0].aseAtoms.get_chemical_symbols()
print 'assuming numbering system such that ring of atoms is 2nd ring ->7-12', nanoAtoms.shape, surfaceAtoms.shape
_rmsd, _qRot = overlay_points_RMSD(surfaceAtoms - np.mean(surfaceAtoms, axis=0),
nanoAtoms - np.mean(nanoAtoms, axis=0))
assert(_rmsd < 0.1)
return zyz_angles(_qRot)
def makeFilesSingleMolSurface(surfaceCif, nanocrystalCif, surfaceDMA, nanocrystalDMA,
nanocrystalAngles = [0., 0., 0.],
nanocrystalSize = [1, 1, 1],
freezeRotations = False,
manuallyRotateNanocrystal = False,
rotateAllAngles = False):
''' Writes the files needed to run orient (.in is the input)
Also can write other things to visualize components or composite - may be useful as check '''
from analyseClusters import getSpeciesListCIF, Surface, splitASEAtomsToMols, Crystal
from orientIO import NanocrystalOnSurfaceInput
#from ccdc.io import CrystalReader
from ioAndInterfaces import ccdcCrystalToASE
from multipoleFile import MultipoleFile, BOHRTOANGSTROM
from copy import deepcopy
#variables
# nanocrystalAngles = [0., 0., 0.]
#reading filenames
moleculeFilename = 'nanoCryst.punch'
#writing filenames
cellFilename = 'surface.cell'
inputFilename = 'surfaceCalc.in'
#other filenams
potentialFilename = 'fit4orient.pots'
surfaceCrystal = Crystal.fromCif(surfaceCif)
nanoCrystal = Crystal.fromCif(nanocrystalCif)
#keep only lowest molecule along c axis
print "%s mols in nanocrystal %s in surface"%(len(nanoCrystal.asymmetricMolecules),
len(surfaceCrystal.asymmetricMolecules))
nanoCrystal.asymmetricMolecules = [sorted(nanoCrystal.asymmetricMolecules, key = lambda x: np.dot(x.aseAtoms.get_center_of_mass(scaled=False),
nanoCrystal.aseCell[2]))[0]]
print 'MASSIVE HACK - KEEPING BUT A SINGLE MOL'
surfaceCrystal.asymmetricMolecules = [sorted(surfaceCrystal.asymmetricMolecules, key = lambda x: np.dot(x.aseAtoms.get_center_of_mass(scaled=False),
surfaceCrystal.aseCell[2]))[0]]
# If doing this, rotate the nanocrystal so that the bottom molecule of surface is same orientation as it
if manuallyRotateNanocrystal:
from listMathsSlow import overlay_points_RMSD
from quaternions import quaternion_rotatn
targetAtomPositions = sorted(surfaceCrystal.asymmetricMolecules, key = lambda x: np.dot(x.aseAtoms.get_center_of_mass(scaled=False),
surfaceCrystal.aseCell[2]))[0].aseAtoms.get_positions()[7:13,:]
nanoAtoms = nanoCrystal.asymmetricMolecules[0].aseAtoms.get_positions()[7:13,:]
print 'assuming numbering system such that ring of atoms is 2nd ring ->7-12', nanoAtoms.shape, targetAtomPositions.shape
if rotateAllAngles:
print 'dont do this - rotating all angles prevents coplanar surfaces';exit()
_rmsd, _qRot = overlay_points_RMSD(nanoAtoms - np.mean(nanoAtoms, axis=0),
targetAtomPositions - np.mean(targetAtomPositions, axis=0))
assert(_rmsd < 0.1)
else:
#just rotate around z axis - the allowRMSDNoRestriction should not be used- just make sure atomic numbering consistent
from listMathsSlow import optimalZRotation
_rmsd, _qRot = optimalZRotation(nanoAtoms - np.mean(nanoAtoms, axis=0),
targetAtomPositions - np.mean(targetAtomPositions, axis=0),
allowRMSDNoRestriction = False)
def tempDistPrint(x):
print [np.linalg.norm(x[i+1] - x[i]) for i in range(5)]
# tempDistPrint(targetAtomPositions - np.mean(targetAtomPositions, axis=0))
# tempDistPrint(np.array([quaternion_rotatn(x, _qRot) for x in nanoAtoms - np.mean(nanoAtoms, axis=0)]))
#rotate nanocrystal atoms and cell
nanoCrystal.asymmetricMolecules[0].rotateASEAtomsAroundCentroidQuat(_qRot)
nanoCrystal.aseCell = np.array([quaternion_rotatn(x, _qRot) for x in nanoCrystal.aseCell])
print "%s mols in nanocrystal %s in surface"%(len(nanoCrystal.asymmetricMolecules),
len(surfaceCrystal.asymmetricMolecules))
print "Atoms in nanoCrystal moleucules " + " ".join([str(len(x.aseAtoms)) for x in nanoCrystal.asymmetricMolecules])
print "Atoms in surface moleucules " + " ".join([str(len(x.aseAtoms)) for x in surfaceCrystal.asymmetricMolecules])
punchSurface = MultipoleFile(fileName=surfaceDMA)
punchNanocrystal = MultipoleFile(fileName=nanocrystalDMA)
print "%s atoms in surfaceDMA"%(len(punchSurface.aseAtoms()))
print "%s atoms in nanocrystalDMA"%(len(punchNanocrystal.aseAtoms()))
# punchSurface.writeFile('dummyPunchSurf.cif')
# punchNanocrystal.writeFile('dummyPunchNano.cif')
uniqueAtomTypes = list(set(list(surfaceCrystal.uniqueElements()) +\
list(nanoCrystal.uniqueElements())))
#print 'using same atoms for surface and nanocrystal'
print 'automatically setting all H bonded to O or N to Hn type'
inputHandler = NanocrystalOnSurfaceInput()
#displace by c of surfaceCrystal - and add a bit
displacementBohr = (surfaceCrystal.aseCell[2] + np.array([0., 0., 3.5])) / BOHRTOANGSTROM
variables = [['x1', displacementBohr[0], 'Bohr'],
['y1', displacementBohr[1], 'Bohr'],
['z1', displacementBohr[2], 'Bohr'],
['alpha1', nanocrystalAngles[0], 'Degree'],
['beta1', nanocrystalAngles[1], 'Degree'],
['gamma1', nanocrystalAngles[2], 'Degree']]
atomsInCell = [punchSurface.newPositions(m.aseAtoms.positions) for m in surfaceCrystal.asymmetricMolecules]
atomsInNanocrystal = [punchNanocrystal.newPositions(m.aseAtoms.positions) for m in nanoCrystal.asymmetricMolecules]
with open(moleculeFilename, 'w') as outf:
#acidic hydrogens
for im, m in enumerate(atomsInNanocrystal):
m.setAtomTypes(dict([(x, 'Hn') for x in nanoCrystal.asymmetricMolecules[im].indicesAcidicHydrogens()]))
outf.write('\n'.join([x.stringFormat(header = '', printTypes=True)
for x in atomsInNanocrystal]))
print 'passing one molecule in nanocrystal as only needs cell -- clear up later'
aseAtomsNanocrystal = nanoCrystal.asymmetricMolecules[0].aseAtoms
with open(cellFilename, 'w') as outf:
#acidic hydrogens
for im, m in enumerate(atomsInCell):
m.setAtomTypes(dict([(x, 'Hn') for x in surfaceCrystal.asymmetricMolecules[im].indicesAcidicHydrogens()]))
outf.write(inputHandler.cellAndSitesString(surfaceCrystal.aseCell, atomsInCell))
with open(inputFilename, 'w') as outf:
outf.write(inputHandler.inputString(variables = variables,
moleculeFilename = moleculeFilename,
potentialFile = potentialFilename,
cellFile = cellFilename,
nanocrystal = aseAtomsNanocrystal,
surfaceCrystal = surfaceCrystal,
nanocrystalSize = nanocrystalSize,
atomTypes = uniqueAtomTypes,
freezeRotations = freezeRotations)
)
#
# EVERYTHING BELOW HERE IS FOR MY (DHC) DEBUGGING AND LOOKING AT FILES TO CHECK THINGS - NOT USED
#
#write temp.xyz with all atoms
print 'writing temporary xyz file to temp.xyz - note that this used only by DHC, not ORIENT itself'
try:
from ase import Atoms
except ImportError:
print 'Need ASE to print out temp.xyz but not important for ORIENT - ignore this'
return
# superSurface = deepcopy(surfaceCrystal)
# superSurface.filledUnitCellMolecules(superCell=np.array([3,3,2]))
# print [x[1] for x in variables[3:6]]
nanocrystalSize=[1,1,1];print 'hack small nano'
surfaceBounds = inputHandler.boundsShowSurface(nanocrystalSize,
nanoCrystal.aseCell,
surfaceCrystal.aseCell,
[x[1] for x in variables[3:6]])
print nanocrystalSize
# print len(superSurface.asymmetricMolecules)
# totalAtoms = Atoms(symbols = [x.symbol for m in superSurface.asymmetricMolecules for x in m.aseAtoms] +\
# [x.symbol for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms],
# positions = np.vstack([np.array([x.position for m in superSurface.asymmetricMolecules for x in m.aseAtoms]),
# displacementBohr * BOHRTOANGSTROM +\
# np.array([x.position for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms])])
# )
# checkSurfPunch = MultipoleFile()
# checkSurfPunch.initFromFile('surface.cell', _fileFormat='punch')
# checkSurfPunch.initFromFile('dummySurface.punch', _fileFormat='punch')
# checkNanoPunch = MultipoleFile()
# checkNanoPunch.initFromFile('nanoCryst.punch')
# print checkNanoPunch.atomPositions()[0]
# print nanoCrystal.asymmetricMolecules[0].aseAtoms[0].position
# print checkSurfPunch.atomPositions()[0]#
# print surfaceCrystal.asymmetricMolecules[0].aseAtoms[0].position
# tempASE = checkNanoPunch.aseAtoms()
# tempASE.write('nanoCheck.xyz')
# checkNanoPunch.writeFile('nanoCheck.xyz')
# tempASE = checkNanoPunch.aseAtoms()
# checkSurfPunch.writeFile('surfCheck.xyz')
# exit()
# print 'adding big gap'
totalAtoms = Atoms(symbols = [x.symbol for m in surfaceCrystal.asymmetricMolecules for x in m.aseAtoms
for a in xrange(surfaceBounds[0], surfaceBounds[1] + 1)
for b in xrange(surfaceBounds[2], surfaceBounds[3] + 1)] +\
[x.symbol for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms
for a in xrange(0, nanocrystalSize[0])
for b in xrange(0, nanocrystalSize[1])],
positions = np.vstack([np.array([x.position + np.dot(np.array([a, b, 1]), surfaceCrystal.aseCell)
for m in surfaceCrystal.asymmetricMolecules for x in m.aseAtoms
for a in xrange(surfaceBounds[0], surfaceBounds[1] + 1)
for b in xrange(surfaceBounds[2], surfaceBounds[3] + 1)]),
displacementBohr * BOHRTOANGSTROM +\
# np.array([0., 0., 50.]) + displacementBohr * BOHRTOANGSTROM +\
np.array([x.position + np.dot(np.array([a, b, 1]), nanoCrystal.aseCell)
for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms
for a in xrange(0, nanocrystalSize[0])
for b in xrange(0, nanocrystalSize[1])])])
)
# print np.min(np.array([x.position + np.dot(np.array([a, b, 1]), surfaceCrystal.aseCell)
# for m in surfaceCrystal.asymmetricMolecules for x in m.aseAtoms
# for a in xrange(surfaceBounds[0], surfaceBounds[1] + 1)
# for b in xrange(surfaceBounds[2], surfaceBounds[3] + 1)]), axis=0)
# print np.max(np.array([x.position + np.dot(np.array([a, b, 1]), surfaceCrystal.aseCell)
# for m in surfaceCrystal.asymmetricMolecules for x in m.aseAtoms
# for a in xrange(surfaceBounds[0], surfaceBounds[1] + 1)
# for b in xrange(surfaceBounds[2], surfaceBounds[3] + 1)]), axis=0)
# print np.min( displacementBohr * BOHRTOANGSTROM +\
# np.array([x.position + np.dot(np.array([a, b, 1]), nanoCrystal.aseCell)
# for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms
# for a in xrange(0, nanocrystalSize[0])
# for b in xrange(0, nanocrystalSize[1])]), axis=0)
# print np.max( displacementBohr * BOHRTOANGSTROM +\
# np.array([x.position + np.dot(np.array([a, b, 1]), nanoCrystal.aseCell)
# for m in nanoCrystal.asymmetricMolecules for x in m.aseAtoms
# for a in xrange(0, nanocrystalSize[0])
# for b in xrange(0, nanocrystalSize[1])]), axis=0)
# print displacementBohr * BOHRTOANGSTROM
# print surfaceCrystal.aseCell[2]
totalAtoms.write('temp.xyz')