Beispiel #1
0
    def run_namd(self):
        """Runs namd executable with # cores"""

        log('info', 'Running namd with ' + str(self.cores) + ' cores.')

        conf_file = self.restart + self.file_name + '.conf'
        log_file = self.restart + self.file_name + '.log'
        err_file = self.restart + self.file_name + '.err'

        cmd = [self.namd_exe, conf_file]

        if self.cores != 1:
            cmd.insert(1, '+p' + str(self.cores))

        try:
            with open(log_file, 'w') as out, open(err_file, "w") as err:
                process = subprocess.Popen(cmd, stdout=out, stderr=err)

                while process.poll() is None:
                    if self.backup:
                        self.backup_restart()
                    sleep(300)

                else:
                    finish_dynamic(err_file)

        except (PermissionError, FileNotFoundError):
            log('error', 'Namd exe not found! Please specify path with -e.')
def resolve_restart(files, silent):
    """Check for size on restart files"""

    files = files.stdout.readlines()
    if len(files) == 0:
        if not silent:
            log('critical', 'Restart files not found. Aborting!')
        return False

    files_size = dict()
    new_restart = list()
    old_restart = list()
    backup_restart = list()

    for file in files:
        file = file.decode('utf-8').strip().split('\t')

        file_name = file[1]
        files_size[file_name] = file[0]

        if file_name.endswith(".xsc") or file_name.endswith(
                ".coor") or file_name.endswith(".vel"):
            new_restart.append(file_name)
        if file_name.endswith(".old"):
            old_restart.append(file_name)
        if file_name.endswith(".bak"):
            backup_restart.append(file_name)

    return choose_restart(files_size, new_restart, old_restart, backup_restart,
                          silent)
Beispiel #3
0
    def save_conf(self):
        """Save conf file"""

        log('info', 'Saving .conf file at ' + self.restart)
        conf = self.restart + self.file_name + '.conf'
        with open(conf, 'w') as file:
            for line in self.conf_file:
                file.write(line)
Beispiel #4
0
    def prepare_restart(self):
        """Check if output folder exists and is empty"""

        if not os.path.exists(self.restart):
            os.makedirs(self.restart)
        else:
            if len(os.listdir(self.restart)) != 0:
                log('warning', 'Output folder not empty. Subscribing.')
                sleep(1)
def search_previous(path, silent=False):
    """Search restart files on previous folder"""

    if not silent:
        log('info', 'Searching restart files.')

    cmd = 'find ' + path + ' -name "*restart*" -exec du -sh {} \\;'
    output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

    return resolve_restart(output, silent)
Beispiel #6
0
    def read_conf(self):
        """Read conf file to list"""

        with open(self.conf) as conf:
            conf_file = conf.readlines()

            if len(conf_file) == 0:
                log('error', 'Could not open .conf file.')
                return False

            return conf_file
Beispiel #7
0
    def edit_run_steps(self, restart_step):
        """Edit the number of run steps"""

        if self.run:
            log('info', 'Setting run steps to ' + self.run + '.')
            self.update_conf('run ' + self.run)
        else:
            steps = self.get_remaining_steps(restart_step)

            log('info', 'Setting run steps to ' + steps + '.')
            self.update_conf('run ' + steps)
Beispiel #8
0
def finish_dynamic(err):
    """Check for errors on dynamic end"""

    with open(err, 'r') as err_file:
        err_message = err_file.read()

        if len(err_message) > 1:
            log('error', 'Dynamic ended with error status:')
            print(err_message)
        else:
            log('info', 'Dynamic finished.')
def get_restart_step(restart_files):
    """Get last step on xsc file"""

    with open(restart_files['xsc']) as xsc:
        step = xsc.readlines()

        if len(step) == 0:
            log('error', 'Could not open .xsc file.')
            return False

        step = step[2].split(' ')[0]
        log('info', 'Using restart step ' + step + '.')
        return step
Beispiel #10
0
    def comment_conf(self, option):
        """Edit conf file to comment option"""

        option = format_option(option)

        option_index = self.search_option(option[0])
        if option_index:
            if not self.conf_file[option_index].startswith('#'):
                self.conf_file[
                    option_index] = '#' + self.conf_file[option_index]
        else:
            log('warning',
                'Option "' + ' '.join(option) + '" not found. Ignoring.')
            sleep(1)
Beispiel #11
0
    def main(self):
        """Main routine"""

        # Gets conf file
        self.conf = self.search_conf()
        if not self.conf:
            return False
        sleep(1)

        # Prepares restart files
        restart_files = search_previous(self.previous)
        if not restart_files:
            return False
        sleep(1)
        self.file_name = self.get_file_name(restart_files)

        # Loads conf file in memory for editing
        self.conf_file = self.read_conf()
        if not self.conf_file:
            return False
        sleep(1)

        # Gets last step
        restart_step = get_restart_step(restart_files)
        if not restart_step:
            return False
        sleep(1)

        # Analyze restart folder
        self.prepare_restart()
        sleep(1)

        # Make basic edits on conf file
        self.configure_restart(restart_step, restart_files)
        sleep(1)

        # Edit optional arguments on conf file
        self.configure_optional()
        self.save_conf()
        sleep(1)

        # Run namd when enabled
        if self.namd:
            self.run_namd()
        else:
            log('info', 'Done.')
Beispiel #12
0
    def __init__(self, **kwargs):
        self.conf = kwargs['conf']
        self.backup = kwargs['backup']
        self.namd = kwargs['namd']
        self.namd_exe = kwargs['namd_exe']
        self.options = kwargs['options']
        self.previous = os.path.abspath(kwargs['previous']) + '/'
        self.restart = os.path.abspath(kwargs['restart']) + '/'
        self.run = kwargs['run']
        self.cores = kwargs['threads']
        self.file_name = kwargs['file_name']

        self.conf_file = None

        try:
            self.main()
        except KeyboardInterrupt:
            log('error', 'Interrupted by user.')
def annotate_restart(file_list, silent):
    """Create dict with restart files"""

    annotated_files = dict()
    for file in file_list:
        if '.restart.xsc' in file:
            annotated_files['xsc'] = file
        elif '.restart.coor' in file:
            annotated_files['coor'] = file
        elif '.restart.vel' in file:
            annotated_files['vel'] = file
        else:
            if not silent:
                log('error', 'Missing restart file.')
            return False

    if not silent:
        log('info', 'Restart files ready.')

    return annotated_files
Beispiel #14
0
    def configure_restart(self, restart_step, restart_files):
        """Make basic edits on conf file"""

        log('info', 'Preparing .conf file.')

        restart_insert = [
            'set outputname ' + self.restart + self.file_name,
            'bincoordinates ' + restart_files['coor'],
            'binvelocities ' + restart_files['vel'],
            'extendedSystem ' + restart_files['xsc'],
            'firsttimestep ' + restart_step
        ]
        restart_comment = ['temperature', 'minimize', 'reinitvels']

        coord_index = self.search_option('coordinates') + 1
        self.conf_file.insert(coord_index, '\n')

        self.edit_run_steps(restart_step)

        for option in restart_insert:
            self.update_conf(option, coord_index)

        for option in restart_comment:
            self.comment_conf(option)
Beispiel #15
0
    def search_conf(self):
        """Search conf files archive"""

        if self.conf is not None:
            if self.conf.endswith('.conf'):
                return self.conf
            else:
                log('error', 'Please provide a valid .conf file.')
                return False

        log('info', 'Searching conf file.')
        cmd = 'find ' + self.previous + ' -name "*conf"'
        output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        output = output.stdout.readlines()
        if len(output) != 1:
            log('error', 'Conf file not found! Please specify path with -c.')
            return False
        else:
            return output[0].decode('utf-8').strip()
def choose_restart(files_dic, new, old, back, silent):
    """Choose restart files not empty"""

    if len(new) >= 3:
        for file in new:
            if files_dic[file] == '0':
                break
            else:
                return annotate_restart(new, silent)

    if len(old) >= 3:
        for file in old:
            if files_dic[file] == '0':
                break
            else:
                if not silent:
                    log(
                        'warning',
                        'Restart files empty or missing, using old restart files.'
                    )
                sleep(1)
                return annotate_restart(old, silent)

    if len(back) >= 3:
        for file in back:
            if files_dic[file] == '0':
                break
            else:
                if not silent:
                    log(
                        'warning',
                        'Restart files empty or missing, using backup restart files.'
                    )
                sleep(1)
                return annotate_restart(back, silent)

    if not silent:
        log('critical', 'Restart files empty or missing. Aborting!')
    return False
Beispiel #17
0
    def configure_optional(self):
        """Make additional edits on conf file"""

        for item in self.options:
            log('info', 'Setting parameter "' + ' '.join(item) + '".')
            self.update_conf(' '.join(item))