Beispiel #1
0
def runLogic(inp,i):

    #Sending a job for a worker to compute
    val = inp.getInputValue('integer_inputs')
    arr = inp.getInput('integer_inputs')
    #1 create command
    storageDir = "%s/%s"%(inp.getPersistentDir(),i)

    #the command name should match the executable name of the plugin
    commandName = "math/double"

    args = [arr[i].get()]

    cmd =cpc.command.Command(storageDir
                             ,commandName
                             ,args)


    #2 define how many cores we want for this job
    resources = Resources()
    resources.min.set('cores',1)
    resources.max.set('cores',1)
    resources.updateCmd(cmd)


    #2 add the command to the function output --> will be added to the queue
    fo = inp.getFunctionOutput()
    fo.addCommand(cmd)
Beispiel #2
0
def tune_fn(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        #cpc.util.plugin.testCommand("grompp -version")
        #cpc.util.plugin.testCommand("mdrun -version")
        return
    fo = inp.getFunctionOutput()
    persDir = inp.getPersistentDir()
    mdpfile = procSettings(inp, inp.getOutputDir())
    # copy the topology and include files
    topfile = os.path.join(inp.getOutputDir(), 'topol.top')
    shutil.copy(inp.getInput('top'), topfile)
    incl = inp.getInput('include')
    if incl is not None and len(incl) > 0:
        for i in range(len(incl)):
            filename = inp.getInput('include[%d]' % i)
            if filename is not None:
                # same name, but in one directory.
                nname = os.path.join(inp.getOutputDir(),
                                     os.path.split(filename)[1])
                shutil.copy(filename, nname)
    # and execute grompp
    cmdlist = cmdnames.grompp.split()
    cmdlist += [
        "-f",
        mdpfile,
        "-quiet",
        "-c",
        inp.getInput('conf'),
        "-p",
        'topol.top',  # we made sure it's there
        "-o",
        "topol.tpr"
    ]
    if inp.hasInput('ndx'):
        cmdlist.append('-n')
        cmdlist.append(inp.getInput('ndx'))
    proc = subprocess.Popen(cmdlist,
                            stdin=None,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            cwd=inp.getOutputDir())
    (stdo, stde) = proc.communicate(None)
    if proc.returncode != 0:
        #raise GromacsError("Error running grompp: %s"%
        #                   (open(stdoutfn,'r').read()))
        fo.setError("Error running grompp: %s, %s" % (stdo, stde))
        return fo
    rsrc = Resources()
    tune.tune(rsrc, inp.getInput('conf'),
              os.path.join(inp.getOutputDir(), 'topol.tpr'), persDir)
    fo.setOut('mdp', FileValue(mdpfile))
    fo.setOut('resources', rsrc.setOutputValue())
    return fo
Beispiel #3
0
def tune_fn(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        #cpc.util.plugin.testCommand("grompp -version")
        #cpc.util.plugin.testCommand("mdrun -version")
        return
    fo=inp.getFunctionOutput()
    persDir=inp.getPersistentDir()
    mdpfile=procSettings(inp, inp.getOutputDir())
    # copy the topology and include files 
    topfile=os.path.join(inp.getOutputDir(), 'topol.top')
    shutil.copy(inp.getInput('top'), topfile)
    incl=inp.getInput('include')
    if incl is not None and len(incl)>0:
        for i in range(len(incl)):
            filename=inp.getInput('include[%d]'%i)
            if filename is not None:
                # same name, but in one directory.
                nname=os.path.join(inp.getOutputDir(), os.path.split(filename)[1])
                shutil.copy(filename, nname)
    # and execute grompp
    cmdlist = cmdnames.grompp.split()
    cmdlist += ["-f", mdpfile,
                "-quiet",
                "-c", inp.getInput('conf'),
                "-p", 'topol.top', # we made sure it's there
                "-o", "topol.tpr" ]
    if inp.hasInput('ndx'):
        cmdlist.append('-n')
        cmdlist.append(inp.getInput('ndx'))
    proc=subprocess.Popen(cmdlist, 
                          stdin=None,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          cwd=inp.getOutputDir())
    (stdo, stde) = proc.communicate(None)
    if proc.returncode != 0:
        #raise GromacsError("Error running grompp: %s"%
        #                   (open(stdoutfn,'r').read()))
        fo.setError("Error running grompp: %s, %s"%
                           (stdo, stde))
        return fo
    rsrc=Resources()
    tune.tune(rsrc, inp.getInput('conf'), 
              os.path.join(inp.getOutputDir(), 'topol.tpr'), persDir)
    fo.setOut('mdp', FileValue(mdpfile))
    fo.setOut('resources', rsrc.setOutputValue())
    return fo
Beispiel #4
0
def pairmodel(inp):
    if inp.testing(): 
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("pairmodel --help")
        return 
    persDir=inp.getPersistentDir()
    outDir=inp.getOutputDir()
    fo=inp.getFunctionOutput()
    rsrc=Resources(inp.getInputValue("resources"))
    rsrcFilename=os.path.join(persDir, 'rsrc.dat')
    log.debug("Initializing pairmodel")
    seq1=inp.getInput('seq1')
    seq2=inp.getInput('seq2')
    pdb1=inp.getInput('pdb1')
    pdb2=inp.getInput('pdb2')
    testseq=inp.getInput('testseq')
    #how do we assign suffix name?
    suffixname=pdb1.split('_')[-1]
    if len(suffixname)>3:
        suffixname=suffixname[:-4]
    #do we need to copy files to working directory?
    if inp.getInput('cmdline_options') is not None:
        if has_split:
            cmdlineOpts=shutil.split(inp.getInput('cmdline_options'))
        else:
            cmdlineOpts=inp.getInput('cmdline_options').split()
    else:
        cmdlineOpts=[]
    # now add to the priority if this run has already been started
    # we can always add state.cpt, even if it doesn't exist.
    args=["--seqfile", testseq, "--template1", seq1, "--template2", seq2, 
           "--seqname", os.path.basename(testseq)[:-4], "--samples1", pdb1,
           "--samples2", pdb2, "--suffix", suffixname ]
    args.extend(cmdlineOpts)
    cmd=cpc.command.Command(newdirname, "modeltools/pairmodel", args)
    if inp.hasInput("resources") and inp.getInput("resources") is not None:
        log.debug("resources is %s"%(inp.getInput("resources")))
        #rsrc=Resources(inp.getInputValue("resources"))
        rsrc.updateCmd(cmd)
    log.debug("Adding command")
    fo.addCommand(cmd)
    # and save for further invocations
    rsrc.save(rsrcFilename)
    return fo
Beispiel #5
0
def runLogic(inp, i):

    #Sending a job for a worker to compute
    val = inp.getInputValue('integer_inputs')
    arr = inp.getInput('integer_inputs')
    #1 create command
    storageDir = "%s/%s" % (inp.getPersistentDir(), i)

    #the command name should match the executable name of the plugin
    commandName = "math/double"

    args = [arr[i].get()]

    cmd = cpc.command.Command(storageDir, commandName, args)

    #2 define how many cores we want for this job
    resources = Resources()
    resources.min.set('cores', 1)
    resources.max.set('cores', 1)
    resources.updateCmd(cmd)

    #2 add the command to the function output --> will be added to the queue
    fo = inp.getFunctionOutput()
    fo.addCommand(cmd)
Beispiel #6
0
def pairmodel(inp):
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("pairmodel --help")
        return
    persDir = inp.getPersistentDir()
    outDir = inp.getOutputDir()
    fo = inp.getFunctionOutput()
    rsrc = Resources(inp.getInputValue("resources"))
    rsrcFilename = os.path.join(persDir, 'rsrc.dat')
    log.debug("Initializing pairmodel")
    seq1 = inp.getInput('seq1')
    seq2 = inp.getInput('seq2')
    pdb1 = inp.getInput('pdb1')
    pdb2 = inp.getInput('pdb2')
    testseq = inp.getInput('testseq')
    #how do we assign suffix name?
    suffixname = pdb1.split('_')[-1]
    if len(suffixname) > 3:
        suffixname = suffixname[:-4]
    #do we need to copy files to working directory?
    if inp.getInput('cmdline_options') is not None:
        if has_split:
            cmdlineOpts = shutil.split(inp.getInput('cmdline_options'))
        else:
            cmdlineOpts = inp.getInput('cmdline_options').split()
    else:
        cmdlineOpts = []
    # now add to the priority if this run has already been started
    # we can always add state.cpt, even if it doesn't exist.
    args = [
        "--seqfile", testseq, "--template1", seq1, "--template2", seq2,
        "--seqname",
        os.path.basename(testseq)[:-4], "--samples1", pdb1, "--samples2", pdb2,
        "--suffix", suffixname
    ]
    args.extend(cmdlineOpts)
    cmd = cpc.command.Command(newdirname, "modeltools/pairmodel", args)
    if inp.hasInput("resources") and inp.getInput("resources") is not None:
        log.debug("resources is %s" % (inp.getInput("resources")))
        #rsrc=Resources(inp.getInputValue("resources"))
        rsrc.updateCmd(cmd)
    log.debug("Adding command")
    fo.addCommand(cmd)
    # and save for further invocations
    rsrc.save(rsrcFilename)
    return fo
Beispiel #7
0
def replica(inp):
    if inp.testing(): 
        # if there are no inputs, we're testing wheter the command can run
#        cpc.util.plugin.testCommand("trjcat -version")
#        cpc.util.plugin.testCommand("eneconv -version")
#        cpc.util.plugin.testCommand("gmxdump -version")
        return 
    persDir=inp.getPersistentDir()
    outDir=inp.getOutputDir()
    fo=inp.getFunctionOutput()
    rsrc=Resources(inp.getInputValue("resources"))
    rsrcFilename=os.path.join(persDir, 'rsrc.dat')
    # check whether we need to reinit
    pers=cpc.dataflow.Persistence(os.path.join(inp.getPersistentDir(),
                                               "persistent.dat"))
    init=False
    lasttpr=pers.get('lasttpr')
    newtpr=inp.getInput('tpr')
    


    #if inp.getInputValue('tpr').isUpdated():
    if newtpr!= lasttpr: 
        lasttpr=newtpr
        # there was no previous command.
        # purge the persistent directory, by moving the confout files to a
        # backup directory
        log.debug("(Re)initializing mdrun")
        confout=glob.glob(os.path.join(persDir, "run_???"))
        if len(confout)>0:
            backupDir=os.path.join(persDir, "backup")
            try:
                os.mkdir(backupDir)    
            except:
                pass
            for conf in confout:
                try:
                    os.rename(conf, os.path.join(backupDir, 
                                                 os.path.split(conf)[-1]))
                except:
                    pass
        init=True
        pers.set('lasttpr', lasttpr)
    elif inp.cmd is None:
        return fo
    if init:
        if rsrc.max.get('cores') is None:
            confFile=os.path.join(persDir, 'conf.gro')
            extractConf(newtpr, confFile)
            tune.tune(rsrc, confFile, newtpr, persDir)
        if inp.cmd is not None:
            log.debug("Canceling commands")
            fo.cancelPrevCommands()
        pers.set('initialized', True)
    else:
        if rsrc.max.get('cores') is None:
            rsrc.load(rsrcFilename)
    if inp.cmd is not None:
        log.debug("Return code was %s"%str(inp.cmd.getReturncode()))
    # try to find out whether the run has already finished
    confout=glob.glob(os.path.join(persDir, "run_???", "confout.part*.gro"))
    if len(confout) > 0:
        log.debug("Extracting data. ")
        # confout exists. we're finished. Concatenate all the runs if
        # we need to, but first create the output dict
        extractData(confout, outDir, persDir, fo)
        return fo
    else:
        tfc=TrajFileCollection(persDir)
        # first check whether we got an error code back
        if (inp.cmd is not None) and inp.cmd.getReturncode()!=0:
            # there was a problem. Check the log
            stde=os.path.join(tfc.getLastDir(), "stderr")
            if checkErr(stde, rsrc, newtpr, persDir):
                if os.path.exists(stde):
                    stdef=open(stde, 'r')
                    errmsg=unicode(stdef.read(), errors='ignore')
                    stdef.close()
                    raise MdrunError("Error running mdrun: %s"%errmsg)
        else:
            # now check whether any of the last 4 iterations produced 
            # trajectories
            trajlist=tfc.getTrajList()
            if len(trajlist) > 4:
                ret=False
                for j in range(4):
                    haveTraj=(len(trajlist[-j-1]) > 0)
                    ret=ret or haveTraj  #prevtraj[-j-1]
                if not ret:
                    stde=os.path.join(tfc.getLastDir(), "stderr")
                    if os.path.exists(stde):
                        stdef=open(stde, 'r')
                        errmsg=unicode(stdef.read(), errors='ignore')
                        stdef.close()
                    else:
                        errmsg=""
                    raise MdrunError("Error running mdrun. No trajectories: %s"%
                                     errmsg)
        # Make a new directory with the continuation of this run
        #newdirname=currundir #"run_%03d"%(i+1)
        newdirname=tfc.getNewRunDir()
        try:
            os.mkdir(newdirname)
        except OSError:
            pass
        tpr=newtpr 
        src=os.path.join(inp.getBaseDir(), tpr)
        dst=os.path.join(newdirname,"topol.tpr")
        shutil.copy(src,dst)
        
        # handle command line inputs
        if inp.getInput('cmdline_options') is not None:
            cmdlineOpts=shlex.split(inp.getInput('cmdline_options'))
        else:
            cmdlineOpts=[]
        if inp.getInput('priority') is not None:
            prio=inp.getInput('priority')
        else:
            prio=0
        lastcpt=tfc.getLastCpt()
        # copy the checkpoint to the new cmd dir
        if lastcpt is not None:
            shutil.copy(lastcpt, os.path.join(newdirname,"state.cpt"))
        # now add to the priority if this run has already been started
        completed=tfc.getFractionCompleted(tpr)
        if completed > 0:
            # now the priority ranges from 1 to 4, depending on how
            # far along the simulation is.
            prio += 1+int(3*(completed))
            log.debug("Setting new priority to %d because it's in progress"%
                      prio)
        # we can always add state.cpt, even if it doesn't exist.
        # include the plumed file here
        args=["-quiet", "-s", "topol.tpr", "-noappend", "-cpi", "state.cpt",
               "-rcon", "0.7", "-plumed", "plumed.dat" ]
        args.extend(cmdlineOpts)
        # for the new neighbor search scheme in Gromacs 4.6, set this env 
        # variable
        if lastcpt is not None:
            shutil.copy(lastcpt, os.path.join(newdirname,"state.cpt"))
        # any expected output files.
        newFileNr=tfc.getLastTrajNr()+1
        outputFiles=[ "traj.part%04d.xtc"%newFileNr, 
                      "traj.part%04d.trr"%newFileNr, 
                      "confout.part%04d.gro"%newFileNr, 
                      "ener.part%04d.edr"%newFileNr, 
                      "dhdl.part%04d.xvg"%newFileNr, 
                      "pullx.part%04d.xvg"%newFileNr, 
                      "pullf.part%04d.xvg"%newFileNr,
                      "COLVAR",
                      "HILLS",
                      "bias.dat",
                      "state.cpt", "state_prev.cpt" ]
        log.debug("Expected output files: %s"%outputFiles)
        cmd=cpc.command.Command(newdirname, "replica/mdrun",args,
                                minVersion=cpc.command.Version("4.5"),
                                addPriority=prio,
                                outputFiles=outputFiles)
        if inp.hasInput("resources") and inp.getInput("resources") is not None:
            #log.debug("resources is %s"%(inp.getInput("resources")))
            #rsrc=Resources(inp.getInputValue("resources"))
            rsrc.updateCmd(cmd)
        log.debug("Adding command")
         # copy the plumed file to the run dir
        plumed_inp=inp.getInput("plumed")
        log.debug("Adding the PLUMED file: %s"%plumed_inp)
        src=os.path.join(inp.getBaseDir(),plumed_inp)
        dst=os.path.join(newdirname,"plumed.dat")
        # check if we need to restart metadynamics
        if tfc.lastDir is not None:
          lasthills=os.path.join(tfc.lastDir,"HILLS")
          if os.path.isfile(lasthills):
            plumed_dat=open(plumed_inp,'r').read()
            log.debug("Adding a RESTART statement to the PLUMED file.")
            newplumed=re.sub(r"HILLS","HILLS RESTART",plumed_dat)
            open(dst,"w").write(newplumed)
            newhills=os.path.join(newdirname,"HILLS")
            shutil.copy(lasthills,newhills)
          else: shutil.copy(src,dst)
        else: shutil.copy(src,dst)

        fo.addCommand(cmd)
        if inp.getInputValue('tpr').isUpdated() and inp.cmd is not None:
            log.debug("Canceling commands")
            fo.cancelPrevCommands()
    # and save for further invocations
    rsrc.save(rsrcFilename)
    pers.write()
    return fo
Beispiel #8
0
def mdrun(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.trjcat)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.eneconv)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.gmxdump)
        return
    persDir=inp.getPersistentDir()
    outDir=inp.getOutputDir()
    fo=inp.getFunctionOutput()
    rsrc=Resources(inp.getInputValue("resources"))
    rsrcFilename=os.path.join(persDir, 'rsrc.dat')
    # check whether we need to reinit
    pers=cpc.dataflow.Persistence(os.path.join(persDir,
                                               "persistent.dat"))
    init=False
    lasttpr=pers.get('lasttpr')
    newtpr=inp.getInput('tpr')
    #if inp.getInputValue('tpr').isUpdated():
    if newtpr!= lasttpr:
        lasttpr=newtpr
        # there was no previous command.
        # purge the persistent directory, by moving the confout files to a
        # backup directory
        log.debug("(Re)initializing mdrun")
        confout=glob.glob(os.path.join(persDir, "run_???"))
        if len(confout)>0:
            backupDir=os.path.join(persDir, "backup")
            try:
                os.mkdir(backupDir)
            except OSError:
                pass
            for conf in confout:
                try:
                    os.rename(conf, os.path.join(backupDir,
                                                 os.path.split(conf)[-1]))
                except OSError:
                    pass
        init=True
        pers.set('lasttpr', lasttpr)
    elif inp.cmd is None:
        return fo
    if init:
        if rsrc.max.get('cores') is None:
            confFile=os.path.join(persDir, 'conf.gro')
            extractConf(newtpr, confFile)
            tune.tune(rsrc, confFile, newtpr, persDir)
        if inp.cmd is not None:
            log.debug("Canceling commands")
            fo.cancelPrevCommands()
        pers.set('initialized', True)
    else:
        if rsrc.max.get('cores') is None:
            rsrc.load(rsrcFilename)
    if inp.cmd is not None:
        log.debug("Return code was %s"%str(inp.cmd.getReturncode()))
    # try to find out whether the run has already finished
    confout=glob.glob(os.path.join(persDir, "run_???", "confout.*gro"))
    if len(confout) > 0:
        confoutDir = os.path.dirname(confout[0])
        hasFinalData = checkConfoutDir(confoutDir)
        if hasFinalData:
            log.debug("Extracting data. ")
            # confout exists. we're finished. Concatenate all the runs if
            # we need to, but first create the output dict
            extractData(confout, outDir, persDir, fo)
            return fo

    tfc=TrajFileCollection(persDir)
    lastDir = tfc.getLastDir()
    # first check whether we got an error code back
    if (inp.cmd is not None) and inp.cmd.getReturncode()!=0:
        # there was a problem. Check the log
        if lastDir:
            stde=os.path.join(lastDir, "stderr")
            if checkErr(stde, rsrc, newtpr, persDir):
                if os.path.exists(stde):
                    stdef=open(stde, 'r')
                    errmsg=unicode(stdef.read(), errors='ignore')
                    stdef.close()
                    raise MdrunError("Error running mdrun: %s"%errmsg)
        else:
            log.debug("An error has occured, but no lastDir was found.")

        # now check whether any of the last 4 iterations produced
        # trajectories
        trajlist=tfc.getTrajList()
        if len(trajlist) > 4:
            ret=False
            for j in range(4):
                haveTraj=(len(trajlist[-j-1]) > 0)
                ret=ret or haveTraj  #prevtraj[-j-1]
            if not ret:
                if lastDir:
                    stde=os.path.join(lastDir, "stderr")
                    if os.path.exists(stde):
                        stdef=open(stde, 'r')
                        errmsg=unicode(stdef.read(), errors='ignore')
                        stdef.close()
                    else:
                        errmsg=""
                    raise MdrunError("Error running mdrun. No trajectories: %s"%
                                    errmsg)
                else:
                    raise MdrunError("Error running mdrun. No trajectories and no lastDir was found.")
    # Make a new directory with the continuation of this run
    #newdirname=currundir #"run_%03d"%(i+1)
    newdirname=tfc.getNewRunDir()
    log.debug("Making a new directory for this run: %s" % newdirname)
    try:
        os.mkdir(newdirname)
    except OSError:
        log.debug("Directory already exists.")
        pass
    tpr=newtpr
    src=os.path.join(inp.getBaseDir(), tpr)
    dst=os.path.join(newdirname,"topol.tpr")
    shutil.copy(src,dst)
    # handle command line inputs
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts=shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts=[]
    if inp.getInput('priority') is not None:
        prio=inp.getInput('priority')
    else:
        prio=0
    lastcpt=tfc.getLastCpt()
    # copy the checkpoint to the new cmd dir
    if lastcpt is not None:
        shutil.copy(lastcpt, os.path.join(newdirname,"state.cpt"))
        log.debug("Continuing from checkpoint")
    # now add to the priority if this run has already been started
    completed=tfc.getFractionCompleted(tpr)
    if completed > 0:
        log.debug("Fraction completed: %s" % completed)
        # Already finished, but no confout.gro?
        if completed >= 1:
            log.debug("Iteration finished, but the final coordinates were not written.")
            if tfc.trajlist[-1].get('edr') or tfc.trajlist[-1].get('xtc') or tfc.trajlist[-1].get('trr'):
                log.debug("Last run produced output files without gaps (but no confout.gro). Generating coordinates from checkpoint.")
                confout=tfc.checkpointToConfout()
                if confout:
                    log.debug("Extracting data.")
                    extractData([confout], outDir, persDir, fo)
                    return fo
            else:
                log.debug("Last run did not produce any output files. Cannot generate coordinates from checkpoint.")
        # now the priority ranges from 1 to 4, depending on how
        # far along the simulation is.
        prio += 1+int(3*(completed))
        log.debug("Setting new priority to %d because it's in progress"%
                  prio)
    # we can always add state.cpt, even if it doesn't exist.
    args=["-quiet", "-s", "topol.tpr", "-noappend", "-cpi", "state.cpt",
           "-rcon", "0.7"  ]
    args.extend(cmdlineOpts)
    # for the new neighbor search scheme in Gromacs 4.6, set this env
    # variable

    # any expected output files.
    newFileNr=tfc.getLastTrajNr()+1
    outputFiles=[ "traj.part%04d.xtc"%newFileNr,
                  "traj.part%04d.trr"%newFileNr,
                  "confout.part%04d.gro"%newFileNr,
                  "ener.part%04d.edr"%newFileNr,
                  "dhdl.part%04d.xvg"%newFileNr,
                  "pullx.part%04d.xvg"%newFileNr,
                  "pullf.part%04d.xvg"%newFileNr,
                  "md.part%04d.log"%newFileNr,
                  "state.cpt", "state_prev.cpt" ]
    log.debug("Expected output files: %s"%outputFiles)
    cmd=cpc.command.Command(newdirname, "gromacs/mdrun",args,
                            minVersion=cpc.command.Version("4.5"),
                            addPriority=prio,
                            outputFiles=outputFiles)
    if inp.hasInput("resources") and inp.getInput("resources") is not None:
        #log.debug("resources is %s"%(inp.getInput("resources")))
        #rsrc=Resources(inp.getInputValue("resources"))
        rsrc.updateCmd(cmd)
    log.debug("Adding command")
    fo.addCommand(cmd)
    if inp.getInputValue('tpr').isUpdated() and inp.cmd is not None:
        log.debug("Canceling commands")
        fo.cancelPrevCommands()

    # and save for further invocations
    rsrc.save(rsrcFilename)
    pers.write()
    return fo
Beispiel #9
0
def mdrun(inp):
    cmdnames = cmds.GromacsCommands()
    if inp.testing():
        # if there are no inputs, we're testing wheter the command can run
        cpc.util.plugin.testCommand("%s -version" % cmdnames.trjcat)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.eneconv)
        cpc.util.plugin.testCommand("%s -version" % cmdnames.gmxdump)
        return
    persDir = inp.getPersistentDir()
    outDir = inp.getOutputDir()
    fo = inp.getFunctionOutput()
    rsrc = Resources(inp.getInputValue("resources"))
    rsrcFilename = os.path.join(persDir, 'rsrc.dat')
    # check whether we need to reinit
    pers = cpc.dataflow.Persistence(os.path.join(persDir, "persistent.dat"))
    init = False
    lasttpr = pers.get('lasttpr')
    newtpr = inp.getInput('tpr')
    #if inp.getInputValue('tpr').isUpdated():
    if newtpr != lasttpr:
        lasttpr = newtpr
        # there was no previous command.
        # purge the persistent directory, by moving the confout files to a
        # backup directory
        log.debug("(Re)initializing mdrun")
        confout = glob.glob(os.path.join(persDir, "run_???"))
        if len(confout) > 0:
            backupDir = os.path.join(persDir, "backup")
            try:
                os.mkdir(backupDir)
            except OSError:
                pass
            for conf in confout:
                try:
                    os.rename(conf,
                              os.path.join(backupDir,
                                           os.path.split(conf)[-1]))
                except OSError:
                    pass
        init = True
        pers.set('lasttpr', lasttpr)
    elif inp.cmd is None:
        return fo
    if init:
        if rsrc.max.get('cores') is None:
            confFile = os.path.join(persDir, 'conf.gro')
            extractConf(newtpr, confFile)
            tune.tune(rsrc, confFile, newtpr, persDir)
        if inp.cmd is not None:
            log.debug("Canceling commands")
            fo.cancelPrevCommands()
        pers.set('initialized', True)
    else:
        if rsrc.max.get('cores') is None:
            rsrc.load(rsrcFilename)
    if inp.cmd is not None:
        log.debug("Return code was %s" % str(inp.cmd.getReturncode()))
    # try to find out whether the run has already finished
    confout = glob.glob(os.path.join(persDir, "run_???", "confout.*gro"))
    if len(confout) > 0:
        confoutDir = os.path.dirname(confout[0])
        hasFinalData = checkConfoutDir(confoutDir)
        if hasFinalData:
            log.debug("Extracting data. ")
            # confout exists. we're finished. Concatenate all the runs if
            # we need to, but first create the output dict
            extractData(confout, outDir, persDir, fo)
            return fo

    tfc = TrajFileCollection(persDir)
    lastDir = tfc.getLastDir()
    # first check whether we got an error code back
    if (inp.cmd is not None) and inp.cmd.getReturncode() != 0:
        # there was a problem. Check the log
        if lastDir:
            stde = os.path.join(lastDir, "stderr")
            if checkErr(stde, rsrc, newtpr, persDir):
                if os.path.exists(stde):
                    stdef = open(stde, 'r')
                    errmsg = unicode(stdef.read(), errors='ignore')
                    stdef.close()
                    raise MdrunError("Error running mdrun: %s" % errmsg)
        else:
            log.debug("An error has occured, but no lastDir was found.")

        # now check whether any of the last 4 iterations produced
        # trajectories
        trajlist = tfc.getTrajList()
        if len(trajlist) > 4:
            ret = False
            for j in range(4):
                haveTraj = (len(trajlist[-j - 1]) > 0)
                ret = ret or haveTraj  #prevtraj[-j-1]
            if not ret:
                if lastDir:
                    stde = os.path.join(lastDir, "stderr")
                    if os.path.exists(stde):
                        stdef = open(stde, 'r')
                        errmsg = unicode(stdef.read(), errors='ignore')
                        stdef.close()
                    else:
                        errmsg = ""
                    raise MdrunError(
                        "Error running mdrun. No trajectories: %s" % errmsg)
                else:
                    raise MdrunError(
                        "Error running mdrun. No trajectories and no lastDir was found."
                    )
    # Make a new directory with the continuation of this run
    #newdirname=currundir #"run_%03d"%(i+1)
    newdirname = tfc.getNewRunDir()
    log.debug("Making a new directory for this run: %s" % newdirname)
    try:
        os.mkdir(newdirname)
    except OSError:
        log.debug("Directory already exists.")
        pass
    tpr = newtpr
    src = os.path.join(inp.getBaseDir(), tpr)
    dst = os.path.join(newdirname, "topol.tpr")
    shutil.copy(src, dst)
    # handle command line inputs
    if inp.getInput('cmdline_options') is not None:
        cmdlineOpts = shlex.split(inp.getInput('cmdline_options'))
    else:
        cmdlineOpts = []
    if inp.getInput('priority') is not None:
        prio = inp.getInput('priority')
    else:
        prio = 0
    lastcpt = tfc.getLastCpt()
    # copy the checkpoint to the new cmd dir
    if lastcpt is not None:
        shutil.copy(lastcpt, os.path.join(newdirname, "state.cpt"))
        log.debug("Continuing from checkpoint")
    # now add to the priority if this run has already been started
    completed = tfc.getFractionCompleted(tpr)
    if completed > 0:
        log.debug("Fraction completed: %s" % completed)
        # Already finished, but no confout.gro?
        if completed >= 1:
            log.debug(
                "Iteration finished, but the final coordinates were not written."
            )
            if tfc.trajlist[-1].get('edr') or tfc.trajlist[-1].get(
                    'xtc') or tfc.trajlist[-1].get('trr'):
                log.debug(
                    "Last run produced output files without gaps (but no confout.gro). Generating coordinates from checkpoint."
                )
                confout = tfc.checkpointToConfout()
                if confout:
                    log.debug("Extracting data.")
                    extractData([confout], outDir, persDir, fo)
                    return fo
            else:
                log.debug(
                    "Last run did not produce any output files. Cannot generate coordinates from checkpoint."
                )
        # now the priority ranges from 1 to 4, depending on how
        # far along the simulation is.
        prio += 1 + int(3 * (completed))
        log.debug("Setting new priority to %d because it's in progress" % prio)
    # we can always add state.cpt, even if it doesn't exist.
    args = [
        "-quiet", "-s", "topol.tpr", "-noappend", "-cpi", "state.cpt", "-rcon",
        "0.7"
    ]
    args.extend(cmdlineOpts)
    # for the new neighbor search scheme in Gromacs 4.6, set this env
    # variable

    # any expected output files.
    newFileNr = tfc.getLastTrajNr() + 1
    outputFiles = [
        "traj.part%04d.xtc" % newFileNr,
        "traj.part%04d.trr" % newFileNr,
        "confout.part%04d.gro" % newFileNr,
        "ener.part%04d.edr" % newFileNr,
        "dhdl.part%04d.xvg" % newFileNr,
        "pullx.part%04d.xvg" % newFileNr,
        "pullf.part%04d.xvg" % newFileNr,
        "md.part%04d.log" % newFileNr, "state.cpt", "state_prev.cpt"
    ]
    log.debug("Expected output files: %s" % outputFiles)
    cmd = cpc.command.Command(newdirname,
                              "gromacs/mdrun",
                              args,
                              minVersion=cpc.command.Version("4.5"),
                              addPriority=prio,
                              outputFiles=outputFiles)
    if inp.hasInput("resources") and inp.getInput("resources") is not None:
        #log.debug("resources is %s"%(inp.getInput("resources")))
        #rsrc=Resources(inp.getInputValue("resources"))
        rsrc.updateCmd(cmd)
    log.debug("Adding command")
    fo.addCommand(cmd)
    if inp.getInputValue('tpr').isUpdated() and inp.cmd is not None:
        log.debug("Canceling commands")
        fo.cancelPrevCommands()

    # and save for further invocations
    rsrc.save(rsrcFilename)
    pers.write()
    return fo