Ejemplo n.º 1
0
    def runTest(self):
        """The test."""

        # . Initialization.
        isOK = True
        log = self.GetLog()

        # . Paths.
        sourcePath = os.path.join(os.getenv("PDYNAMO_ROOT"),
                                  "molecularStructures",
                                  "gaussianGeometryOptimization")
        statesPath = os.path.join(sourcePath, "states.yaml")
        xyzPaths = glob.glob(os.path.join(sourcePath, "xyz", "*.xyz"))
        xyzPaths.sort()

        # . Get the states.
        states = YAMLUnpickle(statesPath)

        # . Loop over the molecules.
        reports = {}
        numberNotConverged = 0
        for xyzPath in xyzPaths:

            # . Basic set up.
            label = os.path.split(xyzPath)[1][0:-4]
            (charge, multiplicity) = states.get(label, (0, 1))
            system = XYZFile_ToSystem(xyzPath)
            system.electronicState = ElectronicState(charge=charge,
                                                     multiplicity=multiplicity)
            system.DefineQCModel(QCModelMNDO("am1", isSpinRestricted=True))
            system.Summary(log=log)

            # . Skip molecules that are too large.
            if len(system.atoms) > _MaximumMoleculeSize: continue

            # . Loop over the optimizers.
            tagReports = {}
            for (tag, minimizer, options) in _Minimizers:

                # . Reset the system.
                system.coordinates3 = XYZFile_ToCoordinates3(xyzPath)
                system.Energy(log=log)

                # . Minimization.
                keywordArguments = dict(options)
                keywordArguments["log"] = log
                cpu = CPUTime()
                tagReports[tag] = minimizer(system, **keywordArguments)
                tagReports[tag]["CPU Time"] = cpu.Current()
                if not tagReports[tag].get("Converged", False):
                    numberNotConverged += 1

            # . Save the results.
            reports[label] = tagReports

        # . Finish up.
        self.ReportsSummary(reports, log=log)
        self.assertTrue(numberNotConverged == 0)
Ejemplo n.º 2
0
    def _Parse(self):
        """Parse parameter files."""
        library = []
        for filename in self.files:
            stem, extension = os.path.splitext(filename)

            # . Load a YAML file
            if extension in (".yaml", ".YAML"):
                yaml = YAMLUnpickle(filename)
                collect = []
                for template in yaml["instances"]:
                    instance = TInstance(
                        label=template["label"],
                        Gmodel=template["Gmodel"],
                        charges=template["charges"],
                        nprotons=template["protons"],
                    )
                    collect.append(instance)
                templateSite = TSite(
                    atomLabels=yaml["atoms"],
                    label=yaml["site"],
                    center=None,
                    instances=collect,
                )

            # . Load an EST file (extended-MEAD format)
            elif extension in (".est", ".EST"):
                reader = ESTFileReader(filename)
                reader.Parse()
                collect = []
                for template in reader.siteInstances:
                    instance = TInstance(
                        label=template["label"],
                        Gmodel=template["Gmodel"],
                        charges=template["charges"],
                        nprotons=template["protons"],
                    )
                    collect.append(instance)
                templateSite = TSite(
                    atomLabels=reader.siteAtoms,
                    label=reader.siteLabel,
                    center=reader.siteCenter,
                    instances=collect,
                )
            # . Add or replace entry
            found = False
            for index, site in enumerate(library):
                if site.label == templateSite.label:
                    found = True
                    break
            if found:
                # . Entry exists in the library, overwrite it
                library[index] = templateSite
            else:
                # . Add a new entry
                library.append(templateSite)
        # . Finish up
        self.library = library
Ejemplo n.º 3
0
def SetUpSystem(path, forceNoQC=False, useSystemWithTimings=True):
    """Set up the system."""
    # . Get system data.
    systemData = YAMLUnpickle(os.path.join(path, _TestDataFileName))
    # . Get the parameters.
    parameters = CHARMMParameterFiles_ToParameters(
        glob.glob(os.path.join(path, "*.prm")))
    # . Get the test name.
    name = os.path.split(path)[-1]
    # . Generate the system.
    system = CHARMMPSFFile_ToSystem(os.path.join(path, name + ".psfx"),
                                    isXPLOR=True,
                                    parameters=parameters)
    if useSystemWithTimings: system = SystemWithTimings.FromSystem(system)
    system.coordinates3 = XYZFile_ToCoordinates3(
        os.path.join(path, name + ".xyz"))
    system.label = systemData["Label"]
    # . Symmetry.
    if systemData.get("Crystal Class", None) is not None:
        symmetryOptions = systemData["Symmetry Parameters"]
        symmetryOptions["crystalClass"] = _CrystalClasses[
            systemData["Crystal Class"]]
        system.DefineSymmetry(**symmetryOptions)
    # . QC data.
    if not forceNoQC:
        qcData = systemData.get(_QCRegionKey, None)
        if qcData is not None:
            # . Electronic state.
            system.electronicState = ElectronicState(
                charge=qcData.get("Charge", 0),
                multiplicity=qcData.get("Multiplicity", 1))
            # . QC atoms.
            qcAtoms = set()
            for path in qcData["Atoms"]:
                index = system.sequence.AtomIndex(path)
                qcAtoms.add(index)
            system.DefineQCModel(_QCModel, qcSelection=Selection(qcAtoms))
    # . Finish set up.
    system.DefineNBModel(_NBModel)
    return system
Ejemplo n.º 4
0
    def runTest ( self ):
        """The test."""

        # . Paths.
        dataPath = os.path.join ( os.getenv ( "PDYNAMO_PMOLECULE" ), "data", "pdb" )
        if self.resultPath is None: outPath  = os.path.join ( os.getenv ( "PDYNAMO_SCRATCH" ), _Destination )
        else:                       outPath  = os.path.join ( self.resultPath, _Destination )
        if not os.path.exists ( outPath ): os.mkdir ( outPath )
        log = self.GetLog ( )

        # . Models.
        mmModel = MMModelCHARMM ( "c36a2" )
#        mmModel = MMModelOPLS ( "protein" )
        nbModel = NBModelABFS ( )

        # . Get all files.
        pdbFiles = glob.glob ( os.path.join ( dataPath, "*.pdb" ) )
        pdbFiles.sort ( )

        # . Read all PDB files.
        failures      = 0
        otherFailures = 0
        successes     = 0
        for pdbFile in pdbFiles:

            ( head, tail ) = os.path.split ( pdbFile )
            tag = tail[0:-4]

            if log is not None: log.Text ( "\nProcessing " + pdbFile + ":\n" )
            system = PDBFile_ToSystem ( pdbFile, log = log, useComponentLibrary = True )
            BuildHydrogenCoordinates3FromConnectivity ( system )
            try:

                # . Setup.
                if system.coordinates3.numberUndefined > 0: raise
                system.DefineMMModel ( mmModel, log = log )
                system.DefineNBModel ( nbModel )
                system.Summary ( log = log )
                referenceEnergy = system.Energy  ( log = log, doGradients = True )

                # . Pickling.
                pklFile  = os.path.join ( outPath, tag + ".pkl"  )
                yamlFile = os.path.join ( outPath, tag + ".yaml" )
                Pickle     ( pklFile , system )
                YAMLPickle ( yamlFile, system )

                # . Unpickling.
                pklSystem = Unpickle ( pklFile )
                pklSystem.label += " (Pickled)"
                pklSystem.Summary ( log = log )
                pklEnergy = pklSystem.Energy  ( log = log, doGradients = True )
                if math.fabs ( referenceEnergy - pklEnergy  <= _Tolerance ): successes += 1
                else:                                                        failures  += 1

                yamlSystem = YAMLUnpickle ( yamlFile )
                yamlSystem.label += " (YAMLPickled)"
                yamlSystem.Summary ( log = log )
                yamlEnergy = yamlSystem.Energy  ( log = log, doGradients = True )
                if math.fabs ( referenceEnergy - yamlEnergy  <= _Tolerance ): successes += 1
                else:                                                         failures  += 1
 
            except Exception as e:
                otherFailures += 1
                if log is not None: log.Text ( "\nError occurred> " +  e.args[0] + "\n" )

        # . Summary of results.
        if log is not None:
            summary = log.GetSummary ( )
            summary.Start ( "Pickle Tests" )
            summary.Entry ( "Pickle Successes", "{:d}".format ( successes            ) )
            summary.Entry ( "Pickle Failures" , "{:d}".format ( failures             ) )
            summary.Entry ( "Total Tests"     , "{:d}".format ( 2 * len ( pdbFiles ) ) )
            summary.Entry ( "Loop Failures"   , "{:d}".format ( otherFailures        ) )
            summary.Stop  ( )

        # . Success/failure.
        self.assertTrue ( ( failures == 0 ) and ( otherFailures == 0 ) )
Ejemplo n.º 5
0
def RunTutorials ( ):
    """Main function."""

    # . Get the run options.
    options = InstallationOptions.FromCommandLine ( )

    # . There are errors.
    if len ( options.notFound ) > 0:
        print ( "\nUnknown tutorials were specified:" )
        for name in options.notFound:
            print ( "  {:s}".format ( name ) )

    # . There are no tutorials.
    elif len ( options.found ) == 0:
        print ( "\nNo tutorials were found." )

    # . There are tutorials.
    else:

        # . Global header.
        print ( "\nRunning tutorials ..." )
        print ( "\nPlease be patient ... this will take some time ..." )

        # . Set the root directory.
        rootPath = os.path.join ( os.getenv ( "PDYNAMO_SCRATCH" ), _TutorialPath )
        if not os.path.exists ( rootPath ): os.mkdir ( rootPath )
        print ( "\nOutputs and other files are in " + rootPath + "." )

        # . Loop over tutorials.
        totalTime = 0.0
        for name in options.found:

            # . Set the output directories.
            namePath   = os.path.join ( rootPath, name        )
            errorPath  = os.path.join ( namePath, _ErrorPath  )
            outputPath = os.path.join ( namePath, _OutputPath )
            if not os.path.exists ( namePath   ): os.mkdir ( namePath   )
            if not os.path.exists ( errorPath  ): os.mkdir ( errorPath  )
            if not os.path.exists ( outputPath ): os.mkdir ( outputPath )

            # . Get the scripts.
            scripts = YAMLUnpickle ( os.path.join ( name, _IndexFileName ) )
            if len ( scripts ) <= 0: continue

            # . Local header.
            print ( "\n" + 80 * "-"   )
            print ( "Tutorial: {:s}".format ( name ) )
            print ( 80 * "-" + "\n"   )

            # . Loop over scripts.
            numberOfSuccesses = 0
            tutorialTime      = 0.0
            for items in scripts:

                # . Script name and path.
                if isinstance ( items, list ):
                    scriptName = items[-1]
                    scriptPath = os.path.join ( *items )
                else:
                    scriptName = items
                    scriptPath = items

                # . File names.
                errorFile  = os.path.join ( errorPath , scriptName + _ErrorExtension  )
                outFile    = os.path.join ( outputPath, scriptName + _OutputExtension )
                scriptFile = os.path.join ( name      , scriptPath + _ScriptExtension )
                if     os.path.exists ( errorFile  ): os.remove ( errorFile )
                if     os.path.exists ( outFile    ): os.remove ( outFile   )

                # . Check that the script file exists.
                if os.path.exists ( scriptFile ):

                    # . Run the script.
                    isOK  = True
                    time0 = time.time ( )
                    eFD   = open ( errorFile, "w" )
                    oFD   = open ( outFile  , "w" )
                    try:
                        process = subprocess.Popen ( [ _PythonCommand, scriptFile ], stderr = eFD, stdout = oFD )
                        process.wait ( )
                    except Exception as e:
                        eFD.write ( e )
                        isOK = False
                    scriptTime = time.time ( ) - time0
                    tutorialTime  += scriptTime

                    # . Close files.
                    eFD.close ( )
                    oFD.close ( )

                    # . Check for a non-empty error file.
                    if isOK:
                        eFD   = open ( errorFile, "r" )
                        lines = eFD.readlines ( )
                        eFD.close ( )
                        isOK  = ( len ( lines ) == 0 )
                        if isOK: os.remove ( errorFile )

                # . Script file not found.
                else:
                    isOK = False

                # . Get the result.
                if isOK:
                    numberOfSuccesses += 1
                    resultLabel = "Pass"
                else:
                    resultLabel = "Error"

                # . Printing.
                print ( "{:s}{:10s}{:>20s}".format ( scriptPath.ljust ( _LabelWidth ), resultLabel, CPUTime.TimeToString ( scriptTime ) ) )

            # . Print a terminating message.
            if len ( scripts ) > 1:
                resultLabel = "{:d}/{:d}".format ( numberOfSuccesses, len ( scripts ) )
                print ( "{:s}{:10s}{:>20s}".format ( "** Totals **".ljust ( _LabelWidth ), resultLabel, CPUTime.TimeToString ( tutorialTime ) ) )

            # . Finish up.
            totalTime += tutorialTime

        # . Final totals.
        print ( "\n" + 80 * "-" )
        if len ( options.found ) > 1:
            print ( "\nTotal time used: " + CPUTime.TimeToString ( totalTime, compact = False ) + "." )
Ejemplo n.º 6
0
    def runTest ( self ):
        """The test."""

        # . Initialization.
        failures      = 0
        otherFailures = 0
        successes     = 0

        # . Paths.
        dataPath = os.path.join ( os.getenv ( "PDYNAMO_PMOLECULE" ), "data", "pdb" )
        if self.resultPath is None: outPath  = os.path.join ( os.getenv ( "PDYNAMO_SCRATCH" ), _Destination )
        else:                       outPath  = os.path.join ( self.resultPath, _Destination )
        if not os.path.exists ( outPath ): os.mkdir ( outPath )
        log = self.GetLog ( )

        # . Models.
        mmModel = MMModelCHARMM ( "c36a2" )
        nbModel = NBModelABFS ( )
        qcModel = QCModelMNDO ( )

        # . Get the file.
        pdbFile = os.path.join ( dataPath, "2E4E_folded_solvated.pdb" )
        ( head, tail ) = os.path.split ( pdbFile )
        tag = tail[0:-4]

        if log is not None: log.Text ( "\nProcessing " + pdbFile + ":\n" )
        system = PDBFile_ToSystem ( pdbFile, log = log, useComponentLibrary = True )
        try:

            # . Fixed atoms.
            fixedAtoms = Selection ( ~ AtomSelection.FromAtomPattern ( system, "A:*:*" ) )

            # . QC selection.
            indices = set ( )
            for atomTag in _Tags:
                indices.add ( system.sequence.AtomIndex ( "A:TYR.2:" + atomTag ) )
            tyrosine = Selection ( indices )

            # . Setup.
            system.electronicState = ElectronicState ( charge = 0, multiplicity = 1 )
            system.DefineFixedAtoms ( fixedAtoms )
            system.DefineSymmetry   ( crystalClass = CrystalClassCubic ( ), a = _BoxSize )
            system.DefineMMModel    ( mmModel, log = log )
            system.DefineQCModel    ( qcModel, qcSelection = tyrosine )
            system.DefineNBModel    ( nbModel )
            system.Summary ( log = log )
            referenceEnergy = system.Energy  ( log = log, doGradients = True )

            # . Pickling.
            pklFile  = os.path.join ( outPath, tag + ".pkl"  )
            yamlFile = os.path.join ( outPath, tag + ".yaml" )
            Pickle     ( pklFile , system )
            YAMLPickle ( yamlFile, system )

            # . Unpickling.
            pklSystem = Unpickle ( pklFile )
            pklSystem.label += " (Pickled)"
            pklSystem.Summary ( log = log )
            pklEnergy = pklSystem.Energy  ( log = log, doGradients = True )
            if math.fabs ( referenceEnergy - pklEnergy  <= _Tolerance ): successes += 1
            else:                                                        failures  += 1

            yamlSystem = YAMLUnpickle ( yamlFile )
            yamlSystem.label += " (YAMLPickled)"
            yamlSystem.Summary ( log = log )
            yamlEnergy = yamlSystem.Energy  ( log = log, doGradients = True )
            if math.fabs ( referenceEnergy - yamlEnergy  <= _Tolerance ): successes += 1
            else:                                                         failures  += 1

        except Exception as e:
            otherFailures += 1
            if log is not None: log.Text ( "\nError occurred> " +  e.args[0] + "\n" )

        # . Summary of results.
        if log is not None:
            summary = log.GetSummary ( )
            summary.Start ( "Pickle Tests" )
            summary.Entry ( "Pickle Successes", "{:d}".format ( successes            ) )
            summary.Entry ( "Pickle Failures" , "{:d}".format ( failures             ) )
            summary.Entry ( "Total Tests"     , "2"                                    )
            summary.Entry ( "Loop Failures"   , "{:d}".format ( otherFailures        ) )
            summary.Stop  ( )

        # . Success/failure.
        self.assertTrue ( ( failures == 0 ) and ( otherFailures == 0 ) )
Ejemplo n.º 7
0
    def WriteJobFiles(self, log=logFile):
        """Write files: PQR, FPT, OGM and MGM."""
        if self.isInitialized:
            # . Get atomic charges and radii for the system
            system = self.owner

            systemCharges = system.AtomicCharges()
            systemRadii = []
            systemTypes = system.energyModel.mmAtoms.AtomTypes()
            radii = YAMLUnpickle("%s/%s" % (YAMLPATHIN, "radii.yaml"))

            for atomType in systemTypes:
                if radii.has_key(atomType):
                    radius = radii[atomType]
                else:
                    generalAtomType = "%s*" % atomType[0]
                    if radii.has_key(generalAtomType):
                        radius = radii[generalAtomType]
                    else:
                        raise ContinuumElectrostaticsError(
                            "Cannot find atomic radius for atom type %s" %
                            atomType)
                systemRadii.append(radius)

            # . Prepare scratch space
            if not os.path.exists(self.pathScratch):
                try:
                    os.makedirs(self.pathScratch)
                except:
                    raise ContinuumElectrostaticsError(
                        "Cannot create scratch directory %s" %
                        self.pathScratch)

            # . Create subdirectories, if necessary
            if self.splitToDirectories:
                for site in self.sites:
                    sitePqr = site.instances[0].sitePqr
                    directory = os.path.dirname(sitePqr)
                    if not os.path.exists(directory):
                        try:
                            os.makedirs(directory)
                        except:
                            raise ContinuumElectrostaticsError(
                                "Cannot create directory %s" % directory)

            # . Write PQR, OGM and MGM files of all instances of all sites
            for site in self.sites:
                site._WriteMEADFiles(system, systemCharges, systemRadii)

            # . Write background PQR file
            PQRFile_FromSystem(self.pathPqrBack,
                               system,
                               selection=Selection(self.backAtomIndices),
                               charges=systemCharges,
                               radii=systemRadii)

            # . Write full-protein PQR file (to be used as eps2set_region)
            PQRFile_FromSystem(self.pathPqrProtein,
                               system,
                               selection=Selection(self.proteinAtomIndices),
                               charges=systemCharges,
                               radii=systemRadii)

            # . Write FPT-file
            lines = []
            for siteIndex, site in enumerate(self.sites):
                for instanceIndex, instance in enumerate(site.instances):
                    for atomIndex, charge in zip(site.siteAtomIndices,
                                                 instance.charges):
                        x, y, z = system.coordinates3[atomIndex]
                        line = "%d %d %f %f %f %f\n" % (
                            siteIndex, instanceIndex, x, y, z, charge)
                        lines.append(line)
            WriteInputFile(self.pathFptSites, lines)

            self.isFilesWritten = True