def __init__(self, jobsDir, elements, potFile, alat):
        # Initialise all the required directories' path.
        baseDir = os.path.join(os.path.dirname(os.path.realpath(
            inspect.getfile(inspect.currentframe()))), '..', '..')
        self.templatesDir = os.path.join(baseDir, 'templates')
        self.jobsDir = os.path.join(baseDir, jobsDir, 'jobs', 'new')

        # Initialise the require information and settings.
        self.elements = elements.split()
        self.potFile = potFile
        self.potPath = os.path.join(self.templatesDir, potFile + '.pot')
        self.alat = alat
        error = False # Error flag for exiting program.

        # Check if the required directories exist
        if os.path.isdir(self.templatesDir) is False:
            print('Templates directory does not exists, '
                  'it is required to have the templates files at ')
            print(self.templatesDir + '.')
            error = True

        if os.path.isdir(self.jobsDir) is False:
            print('The directory provided does not exists.')
            error = True

        if (os.path.isfile(self.potPath) is False):
            print('The potential file provided does not exists.')
            error = True

        if error:
            nmod.nexit()
        else:
            os.chdir(self.jobsDir)
 def generateConcentrations(self, num, **kwargs):
     """ Generate the required permutations of concentrations """
     if self.potFile == 'sc_5_elements_b2':
         a, b, c, d, e = 1.0, 0.5, 0.0, 0.5, 0.0
         step = b / (num - 1)
         precision = len(str(step).split('.')[1])
         conc = [None]*5
         conc[0] = nmod.float2str(precision, a)
         
         for i in range(0, num * num):
             x, y = i % num, int(i / num)
             conc[1] = nmod.float2str(precision, b - x * step)
             conc[2] = nmod.float2str(precision, c + x * step)
             conc[3] = nmod.float2str(precision, d - y * step)
             conc[4] = nmod.float2str(precision, e + y * step)
             self.create(conc[0] + '_' + conc[1] + '_' + conc[2]
                         + '_' + conc[3] + '_' + conc[4], **kwargs)
     elif self.potFile == 'fcc_5_elements_l21':
         a, b, c, d, e = 1.0, 1.0, 0.0, 1.0, 0.0
         step = b / (num - 1)
         precision = len(str(step).split('.')[1])
         conc = [None]*5
         conc[0] = nmod.float2str(precision, a)
         
         for i in range(0, num * num):
             x, y = i % num, int(i / num)
             conc[1] = nmod.float2str(precision, b - x * step)
             conc[2] = nmod.float2str(precision, c + x * step)
             conc[3] = nmod.float2str(precision, d - y * step)
             conc[4] = nmod.float2str(precision, e + y * step)
             self.create(conc[0] + '_' + conc[1] + '_' + conc[2]
                         + '_' + conc[3] + '_' + conc[4], **kwargs)
     else:
         print(self.potFile + ' has not yet been implemented.')
         nmod.nexit()
    def __init__(self, jobsDir, elements, potFile, alat):
        numElements = len(elements.split())

        if numElements != 5:
            print('Expecting 5 elements, but ' + numElements
                  + ' (' + elements + ') were inputted.')
            nmod.nexit()

        Compound.__init__(self, jobsDir, elements, potFile, alat)
    def __init__(self, mainDir):
        baseDir = os.path.join(os.path.dirname(os.path.realpath(
            inspect.getfile(inspect.currentframe()))), '..')
        self.mainDir = os.path.join(baseDir, mainDir)
        self.rawDir = os.path.join(self.mainDir, 'raw')
        self.dataDir = os.path.join(self.mainDir, 'data')
        self.analysisDir = os.path.join(self.mainDir, 'analysis')
        self.listDataDir = []
        error = False
        
        # Check if the required directories exist
        if os.path.isdir(self.mainDir) is False:
            print('The directory provided does not exists.')
            error = True

        if (os.path.isdir(self.rawDir) is False
                and os.path.isdir(self.dataDir) is False):
            print('The directory provided does have a raw or data folder.')
            error = True
        else:
            # Check if there are any data in the raw folder.
            numTotal = 0

            if os.path.isdir(self.dataDir) is True:
                numData = len([dirname for dirname in os.listdir(self.dataDir)
                    if os.path.isdir(os.path.join(self.dataDir, dirname))])
                if numData != 0:
                    numTotal += numData
                    self.listDataDir = sorted(os.listdir(self.dataDir))

            if os.path.isdir(self.rawDir) is True:
                numRaw = len([dirname for dirname in os.listdir(self.rawDir)
                    if os.path.isdir(os.path.join(self.rawDir, dirname))])
                if numRaw != 0 and len(self.listDataDir) == 0:
                    numTotal += numRaw
                    self.listDataDir = sorted(os.listdir(self.dataDir))

            if numTotal == 0:
                print('No data in ' + os.path.join(mainDir, 'data'))
                print('or in ' + os.path.join(mainDir, 'raw') + ',')
                print('Make sure that your data are in one of those folders.')
                error = True 
        if error:
            nmod.nexit()
        else:
            os.chdir(self.mainDir)
示例#5
0
def submitArray(mainDir, pbsFile, start, end, **kwargs):
    """ Submit many array jobs without overloading the task farm """
    # Define all the required directories.
    baseDir = os.path.join(os.path.dirname(os.path.realpath(
        inspect.getfile(inspect.currentframe()))), '..')
    templatesDir = os.path.join(baseDir, 'templates')
    jobsDir = os.path.join(baseDir, mainDir, 'jobs')

    # Set default settings.
    settings = {
        'step': 10,
        'interval': 300
    }

    # Replace default settings with user defined settings.
    for key, value in kwargs.iteritems():
        settings[key] = value

    if checkRequired(mainDir, templatesDir, jobsDir):
        nmod.nexit()
    else:
        os.chdir(jobsDir)

    iterations = int(math.ceil((end - start) / settings['step']))
    last = iterations - 1 # Last iteration point for checks later.
    subCmd = ['qsub', pbsFile]
    checkInterval = settings['interval']

    timeStart = int(time.time())
    timePrev = timeStart

    # Submit the first batch of jobs.
    reps = {
        'tmpTSTART' : start,
        'tmpTEND'   : settings['step'] + start - 1
    }
    nmod.modFile(pbsFile,
        os.path.join(templatesDir, pbsFile), reps)
    subprocess.Popen(subCmd)

    for i in range(1, iterations):
        # Check if any jobs are still idle or blocked.
        # If there are, check again after an interval.
        # Only submit the next batch of jobs when there are no queued jobs.
        time.sleep(3) # Sleep to make sure the jobs are submitted.

        while True:
            # Read in the showq command as an array separated by newline.
            p = subprocess.Popen(['showq', '-u', 'phukgm'],
                                 stdout = subprocess.PIPE,
                                 stderr = subprocess.PIPE)
            stdout, _ = p.communicate()
            stdout = stdout.decode()
            stdout = stdout.split('\n')

            # Check if queued jobs are non-zero.
            if ('Idle Jobs: 0' in stdout[-2]
                    and 'Blocked Jobs: 0' in stdout[-2]):
                timePrev = time.time()
                break
            else:
                print('There are still idle and blocked jobs after '
                      + nmod.seconds2str(time.time() - timePrev) + '.')
                timePrev = time.time()
                time.sleep(checkInterval) # Check jobs interval.


        # Submit the next batch of jobs.
        # Make sure the last iteration has the correct
        # ending point of -t (tempTEND).
        if i == last:
            print('Submitting -t ' + str(i * settings['step'] + start)
                  + '-' + str(end) + '...')
            reps = {
                'tmpTSTART' : i * settings['step'] + start,
                'tmpTEND'   : end
            }
        else:
            print('Submitting -t ' + str(i * settings['step'] + start)
                  + '-' + str((i+1) * settings['step'] + start - 1) + '...')
            reps = {
                'tmpTSTART' : i * settings['step'] + start,
                'tmpTEND'   : (i+1) * settings['step'] + start - 1
            }

        nmod.modFile(pbsFile,
            os.path.join(templatesDir, pbsFile), reps)
        subprocess.Popen(subCmd)

    timeTaken = nmod.seconds2str(time.time() - timeStart)
    print('All jobs submitted. Time taken: ' + timeTaken + '.')