Module ctoolkit.LAMMPS.LAMMPS

Expand source code
from toolkit.global_vars.ext_libs import *
from toolkit.global_vars.decorators import *
from toolkit.tools import tools

# Class to manage LAMMPS file i/o
class LAMMPS():
    # Initialijze
    def __init__(self):
        self.tools = tools.tools()
        pass

    # This will read atomic positions of an MD run. Probably easy to tune it to read velocities.
    @calculate_time
    def read_snapshots_func(self, filename, atomsline):
        fopen = open(filename, 'r')
        lines = fopen.readlines()
        fopen.close()

        numatoms = int(lines[3].split()[0])
        boundbox_mrk = []
        atoms_mrk = []
        nsteps = 0
        for i, line in enumerate(lines):
            if "BOX BOUNDS" in line:
                boundbox_mrk.append(i+1)
            if atomsline in line:
                atoms_mrk.append(i+1)
            if "TIMESTEP" in line:
                nsteps += 1

        # Boundbox processing
        box = np.zeros([nsteps, 3,3], dtype=float)
        for i, mrk in enumerate(boundbox_mrk):
            box[i, 0, 0] = float(lines[mrk].split()[1])-float(lines[mrk].split()[0])
            box[i, 1, 1] = float(lines[mrk+1].split()[1])-float(lines[mrk+1].split()[0])
            box[i, 2, 2] = float(lines[mrk+2].split()[1])-float(lines[mrk+2].split()[0])

        atoms = np.zeros([nsteps, numatoms, 3], dtype=float)
        id_lammps = np.zeros([nsteps, numatoms], dtype=int)
        for i, mrk in enumerate(atoms_mrk):
            for j in range(numatoms):
                if atomsline == 'ATOMS type x y z':
                    id_lammps[i,j] = int(lines[mrk+j].split()[0])
                    atoms[i,j, :] = np.array(lines[mrk+j].split()[1:4], dtype=float)
                else:
                    atoms[i,j, :] = np.array(lines[mrk+j].split()[0:3], dtype=float)

        self.id_lammps, self.boxes, self.atoms = np.copy(id_lammps), np.copy(box), np.copy(atoms)

    #This is just a wrapper
    @calculate_time
    def read_snapshots(self, filename, style='pos'):
        if style == 'typepos':
            atomsline = 'ATOMS type x y z'
        if style == 'pos':
            atomsline = "ATOMS x y z"
        if style == 'vel':
            atomsline = "ATOMS vx vy vz"

        self.read_snapshots_func(filename, atomsline)

    # A function to read LAMMPS output.
    # This is trivial to modify but for now the 
    # reader needs that the main output has the following structure:
    #
    #   "Step Temp Press Cella Cellb Cellc Volume PotEng"
    #
    @calculate_time
    def read_LAMMPS_output(self, filename):
        fopen = open(filename, 'r')
        lines = fopen.readlines()
        fopen.close()

        # We need this because LAMMPS closes/engages a new loop every time
        # a run command is issued.
        line_starts = []
        line_ends = []
        for i, line in enumerate(lines):
            if "Step Temp Press Cella Cellb Cellc Volume PotEng" in line:
                line_starts.append(i+1)
            if "Loop time" in line:
                line_ends.append(i)

        num_loops = len(line_starts)
    
        step = []
        temp = []
        press = []
        cell = []
        vol = []
        energy = []
        for iloop in range(num_loops):
            # Skip the first result as it's the same as the last loop last step!
            for i in range(line_starts[iloop]+1, line_ends[iloop]):
                step.append(int(lines[i].split()[0]))
                temp.append(float(lines[i].split()[1]))
                press.append(float(lines[i].split()[2]))
                cell.append(np.array(lines[i].split()[3:6], dtype=float))
                vol.append(float(lines[i].split()[6]))
                energy.append(float(lines[i].split()[7]))
        return np.array(step, dtype=int), np.array(temp, dtype=float), np.array(press, dtype=float), np.array(cell,dtype=float), np.array(vol, dtype=float), np.array(energy, dtype=float)

Classes

class LAMMPS
Expand source code
class LAMMPS():
    # Initialijze
    def __init__(self):
        self.tools = tools.tools()
        pass

    # This will read atomic positions of an MD run. Probably easy to tune it to read velocities.
    @calculate_time
    def read_snapshots_func(self, filename, atomsline):
        fopen = open(filename, 'r')
        lines = fopen.readlines()
        fopen.close()

        numatoms = int(lines[3].split()[0])
        boundbox_mrk = []
        atoms_mrk = []
        nsteps = 0
        for i, line in enumerate(lines):
            if "BOX BOUNDS" in line:
                boundbox_mrk.append(i+1)
            if atomsline in line:
                atoms_mrk.append(i+1)
            if "TIMESTEP" in line:
                nsteps += 1

        # Boundbox processing
        box = np.zeros([nsteps, 3,3], dtype=float)
        for i, mrk in enumerate(boundbox_mrk):
            box[i, 0, 0] = float(lines[mrk].split()[1])-float(lines[mrk].split()[0])
            box[i, 1, 1] = float(lines[mrk+1].split()[1])-float(lines[mrk+1].split()[0])
            box[i, 2, 2] = float(lines[mrk+2].split()[1])-float(lines[mrk+2].split()[0])

        atoms = np.zeros([nsteps, numatoms, 3], dtype=float)
        id_lammps = np.zeros([nsteps, numatoms], dtype=int)
        for i, mrk in enumerate(atoms_mrk):
            for j in range(numatoms):
                if atomsline == 'ATOMS type x y z':
                    id_lammps[i,j] = int(lines[mrk+j].split()[0])
                    atoms[i,j, :] = np.array(lines[mrk+j].split()[1:4], dtype=float)
                else:
                    atoms[i,j, :] = np.array(lines[mrk+j].split()[0:3], dtype=float)

        self.id_lammps, self.boxes, self.atoms = np.copy(id_lammps), np.copy(box), np.copy(atoms)

    #This is just a wrapper
    @calculate_time
    def read_snapshots(self, filename, style='pos'):
        if style == 'typepos':
            atomsline = 'ATOMS type x y z'
        if style == 'pos':
            atomsline = "ATOMS x y z"
        if style == 'vel':
            atomsline = "ATOMS vx vy vz"

        self.read_snapshots_func(filename, atomsline)

    # A function to read LAMMPS output.
    # This is trivial to modify but for now the 
    # reader needs that the main output has the following structure:
    #
    #   "Step Temp Press Cella Cellb Cellc Volume PotEng"
    #
    @calculate_time
    def read_LAMMPS_output(self, filename):
        fopen = open(filename, 'r')
        lines = fopen.readlines()
        fopen.close()

        # We need this because LAMMPS closes/engages a new loop every time
        # a run command is issued.
        line_starts = []
        line_ends = []
        for i, line in enumerate(lines):
            if "Step Temp Press Cella Cellb Cellc Volume PotEng" in line:
                line_starts.append(i+1)
            if "Loop time" in line:
                line_ends.append(i)

        num_loops = len(line_starts)
    
        step = []
        temp = []
        press = []
        cell = []
        vol = []
        energy = []
        for iloop in range(num_loops):
            # Skip the first result as it's the same as the last loop last step!
            for i in range(line_starts[iloop]+1, line_ends[iloop]):
                step.append(int(lines[i].split()[0]))
                temp.append(float(lines[i].split()[1]))
                press.append(float(lines[i].split()[2]))
                cell.append(np.array(lines[i].split()[3:6], dtype=float))
                vol.append(float(lines[i].split()[6]))
                energy.append(float(lines[i].split()[7]))
        return np.array(step, dtype=int), np.array(temp, dtype=float), np.array(press, dtype=float), np.array(cell,dtype=float), np.array(vol, dtype=float), np.array(energy, dtype=float)

Methods

def read_LAMMPS_output(*args, **kwargs)
Expand source code
def inner1(*args, **kwargs):

    # storing time before function execution
    begin = time.time()

    val = func(*args, **kwargs)

    # storing time after function execution
    end = time.time()
    timer_name = func.__name__
    timer_time = end-begin
    if timer_name in timers_dict:
        timers_dict[timer_name] += timer_time
    else:
        timers_dict[timer_name] = timer_time

    return val
def read_snapshots(*args, **kwargs)
Expand source code
def inner1(*args, **kwargs):

    # storing time before function execution
    begin = time.time()

    val = func(*args, **kwargs)

    # storing time after function execution
    end = time.time()
    timer_name = func.__name__
    timer_time = end-begin
    if timer_name in timers_dict:
        timers_dict[timer_name] += timer_time
    else:
        timers_dict[timer_name] = timer_time

    return val
def read_snapshots_func(*args, **kwargs)
Expand source code
def inner1(*args, **kwargs):

    # storing time before function execution
    begin = time.time()

    val = func(*args, **kwargs)

    # storing time after function execution
    end = time.time()
    timer_name = func.__name__
    timer_time = end-begin
    if timer_name in timers_dict:
        timers_dict[timer_name] += timer_time
    else:
        timers_dict[timer_name] = timer_time

    return val