LAMMPS.py 15.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# **************************************************************************
#
# MDANSE: Molecular Dynamics Analysis for Neutron Scattering Experiments
#
# @file      Src/Framework/Jobs/LAMMPS.py
# @brief     Implements module/class/test LAMMPS
#
# @homepage  https://mdanse.org
# @license   GNU General Public License v3 or higher (see LICENSE)
# @copyright Institut Laue Langevin 2013-now
# @authors   Scientific Computing Group at ILL (see AUTHORS)
#
# **************************************************************************

15
import collections
16
import os
17
18
19
20
21
22
23
24
25
import re

import numpy

from MMTK import Atom, AtomCluster
from MMTK import Units
from MMTK.Trajectory import Trajectory, SnapshotGenerator, TrajectoryOutput
from MMTK.Universe import ParallelepipedicPeriodicUniverse

26
from MDANSE import ELEMENTS, REGISTRY
27
from MDANSE.Core.Error import Error
28
from MDANSE.Framework.Jobs.Converter import Converter
29
30
31
32
33
34
35
36
37
38
from MDANSE.Mathematics.Graph import Graph

class LAMMPSConfigFileError(Error):
    pass

class LAMMPSTrajectoryFileError(Error):
    pass

class LAMMPSConfigFile(dict):

39
    def __init__(self, filename,tolerance):
40
41
        
        self._filename = filename
42
43
        
        self._tolerance = tolerance
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

        self['n_bonds'] = None

        self['bonds'] = []
                
        self['n_atoms'] = None
        
        self["n_atom_types"] = None
        
        self["elements"] = {}
                
        unit = open(self._filename, 'r')
        lines = []
        for l in unit.readlines():
            l = l.strip()
            if l:
                lines.append(l)        
        unit.close()
    
        for i, line in enumerate(lines):
                                        
            if self['n_atoms'] is None:
66
                m = re.match("^\s*(\d+)\s*atoms\s*$",line, re.I) 
67
68
69
70
                if m:
                    self['n_atoms'] = int(m.groups()[0])
                  
            if self["n_atom_types"] is None: 
71
                m = re.match("^\s*(\d+)\s*atom types\s*$",line, re.I)             
72
73
74
75
                if m:
                    self["n_atom_types"] = int(m.groups()[0])

            if self['n_bonds'] is None:
76
                m = re.match("^\s*(\d+)\s*bonds\s*$",line, re.I) 
77
78
79
                if m:
                    self['n_bonds'] = int(m.groups()[0])

80
            if re.match("^\s*masses\s*$", line, re.I):
81
82
83
84
                
                if self["n_atom_types"] is None:
                    raise LAMMPSConfigFileError("Did not find the number of atom types.")
                                                                    
Remi Perenon's avatar
Remi Perenon committed
85
86
87
                for j in range(1, self["n_atom_types"]+1):
                    data_line = lines[i+j].strip().split("#")[0] #Remove commentary if any
                    idx, mass = data_line.split()[0:2]
88
89
                    idx = int(idx)
                    mass = float(mass)
90
91
92
                    el = ELEMENTS.match_numeric_property("atomic_weight", mass, tolerance=self._tolerance)
                    nElements = len(el)
                    if nElements == 0:
Remi Perenon's avatar
Remi Perenon committed
93
                        # No element is matching
94
                        raise LAMMPSConfigFileError("The atom %d with defined mass %f could not be assigned with a tolerance of %f. Please modify the mass in the config file to comply with MDANSE internal database" % (idx,mass,self._tolerance))
Remi Perenon's avatar
Remi Perenon committed
95
96
97
98
99
100
101
102
                    elif nElements == 2:
                        # If two elements are matching, these can be the same appearing twice (example 'Al' and 'Al27')
                        if el[0][:min((len(el[0]), len(el[1])))] == el[1][:min((len(el[0]), len(el[1])))]:
                            self["elements"][idx] = el[0]
                        else:
                            raise LAMMPSConfigFileError("The atoms %s of MDANSE database matches the mass %f with a tolerance of %f. Please modify the mass in the config file to comply with MDANSE internal database" % (el,mass,self._tolerance))
                    elif nElements > 2:
                        # More than two elements are matching => error
103
                        raise LAMMPSConfigFileError("The atoms %s of MDANSE database matches the mass %f with a tolerance of %f. Please modify the mass in the config file to comply with MDANSE internal database" % (el,mass,self._tolerance))
Remi Perenon's avatar
Remi Perenon committed
104
105
                    else:
                        # One element is matching => continue
106
                        self["elements"][idx] = el[0]
107

108
            m = re.match("^\s*bonds\s*$",line, re.I)
109
110
111
112
113
114
115
116
117
118
119
            if m:
                for j in range(1, self['n_bonds']+1):
                    _,_,at1,at2 = lines[i+j].split()
                    at1 = int(at1)-1
                    at2 = int(at2)-1
                    self['bonds'].append([at1,at2])
                self['bonds'] = numpy.array(self['bonds'], dtype=numpy.int32)
                                                   
        unit.close()

                
120
class LAMMPSConverter(Converter):
121
122
123
    """
    Converts a LAMMPS trajectory to a MMTK trajectory.
    """
124
                  
125
126
    label = "LAMMPS"

127
    settings = collections.OrderedDict()        
128
129
130
131
    settings['config_file'] = ('input_file', {'label':"LAMMPS configuration file",
                                              'default':os.path.join('..','..','..','Data','Trajectories','LAMMPS','glycyl_L_alanine_charmm.config')})
    settings['trajectory_file'] = ('input_file', {'label':"LAMMPS trajectory file",
                                                  'default':os.path.join('..','..','..','Data','Trajectories','LAMMPS','glycyl_L_alanine_charmm.lammps')})
132
    settings['mass_tolerance'] = ('float', {'label':"mass tolerance (uma)", 'default':1.0e-3, 'mini':1.0e-9})
133
    settings['time_step'] = ('float', {'label':"time step (fs)", 'default':1.0, 'mini':1.0e-9})        
134
    settings['n_steps'] = ('integer', {'label':"number of time steps (0 for automatic detection)", 'default':0, 'mini':0})
135
    settings['output_files'] = ('output_files', {'formats':["netcdf"]})
136
137
138
139
140
141
142
143
144
    
    def initialize(self):
        '''
        Initialize the job.
        '''
                
        # The number of steps of the analysis.
        self.numberOfSteps = self.configuration["n_steps"]["value"]
        
145
        self._lammpsConfig = LAMMPSConfigFile(self.configuration["config_file"]["value"],self.configuration["mass_tolerance"]["value"])
146
147
148
149
        
        self.parse_first_step()
        
        # A MMTK trajectory is opened for writing.
150
        self._trajectory = Trajectory(self._universe, self.configuration['output_files']['files'][0], mode='w')
151
152
153
154
155

        self._nameToIndex = dict([(at.name,at.index) for at in self._universe.atomList()])

        # A frame generator is created.
        self._snapshot = SnapshotGenerator(self._universe, actions = [TrajectoryOutput(self._trajectory, ["all"], 0, None, 1)])
156
157
158
159
160
161
162
163

        # Estimate number of steps if needed
        if self.numberOfSteps == 0:
            self.numberOfSteps = 1
            for line in self._lammps:
                if line.startswith("ITEM: TIMESTEP"):
                    self.numberOfSteps += 1

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
        self._lammps.seek(0,0)

        self._start = 0

    def run_step(self, index):
        """Runs a single step of the job.
        
        @param index: the index of the step.
        @type index: int.

        @note: the argument index is the index of the loop note the index of the frame.      
        """

        for _ in range(self._itemsPosition["TIMESTEP"][0]):
            line = self._lammps.readline()
            if not line:
                return index, None

        timeStep = Units.fs*float(self._lammps.readline())*self.configuration['time_step']['value']

        for _ in range(self._itemsPosition["TIMESTEP"][1], self._itemsPosition["BOX BOUNDS"][0]):
            self._lammps.readline()

        abcVectors = numpy.zeros((9), dtype=numpy.float64)
        temp = [float(v) for v in self._lammps.readline().split()]
        if len(temp) == 2:
            xlo, xhi = temp
            xy = 0.0
        elif len(temp) == 3:
            xlo, xhi, xy = temp
        else:
            raise LAMMPSTrajectoryFileError("Bad format for A vector components")

        temp = [float(v) for v in self._lammps.readline().split()]
        if len(temp) == 2:
            ylo, yhi = temp
            xz = 0.0
        elif len(temp) == 3:
            ylo, yhi, xz = temp
        else:
            raise LAMMPSTrajectoryFileError("Bad format for B vector components")

        temp = [float(v) for v in self._lammps.readline().split()]
        if len(temp) == 2:
            zlo, zhi = temp
            yz = 0.0
        elif len(temp) == 3:
            zlo, zhi, yz = temp
        else:
            raise LAMMPSTrajectoryFileError("Bad format for C vector components")
                      
        # The ax component.                                      
        abcVectors[0] = xhi - xlo
        
        # The bx and by components.                                      
        abcVectors[3] = xy
        abcVectors[4] = yhi - ylo
        
        # The cx, cy and cz components.                                      
        abcVectors[6] = xz
        abcVectors[7] = yz
        abcVectors[8] = zhi - zlo

        abcVectors *= Units.Ang

        for _ in range(self._itemsPosition["BOX BOUNDS"][1], self._itemsPosition["ATOMS"][0]):
            self._lammps.readline()

        self._universe.setCellParameters(abcVectors)

        conf = self._universe.configuration()

        for i,_ in enumerate(range(self._itemsPosition["ATOMS"][0], self._itemsPosition["ATOMS"][1])):
            temp = self._lammps.readline().split()
238
            idx = self._nameToIndex[self._rankToName[int(temp[0])-1]]
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
            conf.array[idx,:] = numpy.array([temp[self._x],temp[self._y],temp[self._z]],dtype=numpy.float64)

        if self._fractionalCoordinates:
            conf.array = self._universe._boxToRealPointArray(conf.array)
        else:
            conf.array *= Units.Ang
            
        # The whole configuration is folded in to the simulation box.
        self._universe.foldCoordinatesIntoBox()

        # A snapshot is created out of the current configuration.
        self._snapshot(data = {'time': timeStep})

        self._start += self._last
        
        return index, None

    def combine(self, index, x):
        """
        @param index: the index of the step.
        @type index: int.
        
        @param x:
        @type x: any.
        """

        pass

    def finalize(self):
        """
        Finalize the job.
        """
        
        self._lammps.close()

        if self._lammpsConfig["n_bonds"] is not None:
            netcdf = self._trajectory.trajectory.file
276
277
278
            netcdf.createDimension("MDANSE_NBONDS",self._lammpsConfig["n_bonds"])
            netcdf.createDimension("MDANSE_TWO",2)
            VAR = netcdf.createVariable("mdanse_bonds", numpy.dtype(numpy.int32).char, ("MDANSE_NBONDS","MDANSE_TWO"))
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
            VAR.assignValue(self._lammpsConfig["bonds"]) 
                
        # Close the output trajectory.
        self._trajectory.close()
                        
    def parse_first_step(self):

        self._lammps = open(self.configuration["trajectory_file"]["value"], 'r')        

        self._itemsPosition = collections.OrderedDict()

        self._universe = None
        comp = -1

        while True:

            line = self._lammps.readline()
            comp += 1

            if not line:
                break

            if line.startswith("ITEM: TIMESTEP"):
                self._itemsPosition["TIMESTEP"] = [comp+1, comp+2]
                continue

            elif line.startswith("ITEM: BOX BOUNDS"):
                self._itemsPosition["BOX BOUNDS"] = [comp+1, comp+4]
                continue

            elif line.startswith("ITEM: ATOMS"):
                
                keywords = line.split()[2:]
                
                self._id = keywords.index("id")
                self._type = keywords.index("type")
Remi Perenon's avatar
Remi Perenon committed
315
                
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
                # Field name is <x,y,z> or cd ..<x,y,z>u if real coordinates and <x,y,z>s if fractional ones
                self._fractionalCoordinates = False
                try:
                    self._x = keywords.index("x")
                    self._y = keywords.index("y")
                    self._z = keywords.index("z")
                except ValueError:
                    try:
                        self._x = keywords.index("xu")
                        self._y = keywords.index("yu")
                        self._z = keywords.index("zu")
                    except ValueError:
                        try:
                            self._x = keywords.index("xs")
                            self._y = keywords.index("ys")
                            self._z = keywords.index("zs")
                            self._fractionalCoordinates = True
                        except ValueError:
                            raise LAMMPSTrajectoryFileError("No coordinates could be found in the trajectory")
335
336
337
338
339
340
341
342
343
                    
                self._rankToName = {}
                
                g = Graph()
                self._universe = ParallelepipedicPeriodicUniverse()
                self._itemsPosition["ATOMS"] = [comp+1,comp+self._nAtoms+1]                                
                for i in range(self._nAtoms):
                    temp = self._lammps.readline().split()
                    idx = int(temp[self._id])-1
344
345
                    ty = int(temp[self._type])
                    name = "%s%d" % (self._lammpsConfig["elements"][ty],idx)
346
                    self._rankToName[int(temp[0])-1] = name
347
                    g.add_node(idx, element=self._lammpsConfig["elements"][ty], atomName=name)
348
349
350
351
352
353
354
                    
                if self._lammpsConfig["n_bonds"] is not None:
                    for idx1,idx2 in self._lammpsConfig["bonds"]:
                        g.add_link(idx1,idx2)
                
                for cluster in g.build_connected_components():
                    if len(cluster) == 1:
355
356
357
                        node = cluster.pop()
                        obj = Atom(node.element, name=node.atomName)
                        obj.index = node.name
358
359
360
                    else:
                        atList = []
                        for atom in cluster:
361
                            at = Atom(atom.element, name=atom.atomName)
362
363
364
365
366
367
368
369
370
371
372
373
374
                            atList.append(at) 
                        c = collections.Counter([at.element for at in cluster])
                        name = "".join(["%s%d" % (k,v) for k,v in sorted(c.items())])
                        obj = AtomCluster(atList, name=name)
                        
                    self._universe.addObject(obj)
                self._last = comp + self._nAtoms + 1

                break
                    
            elif line.startswith("ITEM: NUMBER OF ATOMS"):
                self._nAtoms = int(self._lammps.readline())
                comp += 1
375
376
377
                continue
            
REGISTRY['lammps'] = LAMMPSConverter
378