LAMMPS.py 13.7 KB
Newer Older
1 2 3 4 5
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
6 7
#71 avenue des Martyrs
#38000 Grenoble Cedex 9
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Apr 10, 2015

30
:author: Eric C. Pellegrini
31 32 33
'''

import collections
34
import os
35 36 37 38 39 40 41 42 43
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

44
from MDANSE import ELEMENTS, REGISTRY
45
from MDANSE.Core.Error import Error
46
from MDANSE.Framework.Jobs.Converter import Converter
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
from MDANSE.Mathematics.Graph import Graph

class LAMMPSConfigFileError(Error):
    pass

class LAMMPSTrajectoryFileError(Error):
    pass

class LAMMPSConfigFile(dict):

    def __init__(self, filename):
        
        self._filename = filename

        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:
                m = re.match("^(\d+) atoms$",line, re.I) 
                if m:
                    self['n_atoms'] = int(m.groups()[0])
                  
            if self["n_atom_types"] is None: 
                m = re.match("^(\d+) atom types$",line, re.I)             
                if m:
                    self["n_atom_types"] = int(m.groups()[0])

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

            if re.match("^masses$", line, re.I):
                
                if self["n_atom_types"] is None:
                    raise LAMMPSConfigFileError("Did not find the number of atom types.")
                                                                    
                for j in range(1, self["n_atom_types"]+1):
                    idx, mass = lines[i+j].strip().split()
103 104 105 106 107 108
                    tolerance = 1.0e-3
                    idx = int(idx)
                    mass = float(mass)
                    el = ELEMENTS.match_numeric_property("atomic_weight", mass, tolerance=tolerance)
                    if len(el) != 1:
                        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,tolerance))
109 110 111 112 113 114 115 116 117 118 119 120 121 122
                    self["elements"][idx] = el[0]

            m = re.match("^bonds$",line, re.I)
            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()

                
123
class LAMMPSConverter(Converter):
124 125 126
    """
    Converts a LAMMPS trajectory to a MMTK trajectory.
    """
127
                  
128 129
    label = "LAMMPS"

130
    settings = collections.OrderedDict()        
131 132 133 134
    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')})
135 136 137
    settings['time_step'] = ('float', {'label':"time step (fs)", 'default':1.0, 'mini':1.0e-9})        
    settings['n_steps'] = ('integer', {'label':"number of time steps", 'default':1, 'mini':0})        
    settings['output_file'] = ('output_files', {'formats':["netcdf"]})
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
    
    def initialize(self):
        '''
        Initialize the job.
        '''
                
        # The number of steps of the analysis.
        self.numberOfSteps = self.configuration["n_steps"]["value"]
        
        self._lammpsConfig = LAMMPSConfigFile(self.configuration["config_file"]["value"])
        
        self.parse_first_step()
        
        # A MMTK trajectory is opened for writing.
        self._trajectory = Trajectory(self._universe, self.configuration['output_file']['files'][0], mode='w')

        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)])

        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()
            idx = self._nameToIndex[self._rankToName[i]]
            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
271 272 273
            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"))
274 275 276 277 278 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            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")
                
                try:
                    self._x = keywords.index("x")
                    self._y = keywords.index("y")
                    self._z = keywords.index("z")
                except ValueError:
                    try:
                        self._x = keywords.index("xs")
                        self._y = keywords.index("ys")
                        self._z = keywords.index("zs")
                    except ValueError:
                        raise LAMMPSTrajectoryFileError("No coordinates could be found in the trajectory")
                    else:
                        self._fractionalCoordinates = True
                        
                else:
                    self._fractionalCoordinates = False
                    
                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
                    ty = temp[self._type]
                    name = "%s%s" % (self._lammpsConfig["elements"][ty],idx)
                    self._rankToName[i] = name
339
                    g.add_node(idx, element=self._lammpsConfig["elements"][ty], atomName=name)
340 341 342 343 344 345 346
                    
                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:
347 348 349
                        node = cluster.pop()
                        obj = Atom(node.element, name=node.atomName)
                        obj.index = node.name
350 351 352
                    else:
                        atList = []
                        for atom in cluster:
353
                            at = Atom(atom.element, name=atom.atomName)
354 355 356 357 358 359 360 361 362 363 364 365 366
                            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
367 368 369 370
                continue
            
REGISTRY['lammps'] = LAMMPSConverter