def ToSymmetry(self, system): """Assign symmetry to a system.""" # . Check for symmetry. if not self.QPARSED: return None else: # . Unpack data. alpha = self.symmetryParameters.alpha beta = self.symmetryParameters.beta gamma = self.symmetryParameters.gamma a = self.symmetryParameters.a b = self.symmetryParameters.b c = self.symmetryParameters.c # . Get the crystal class. if alpha > 80.0: if (a == b) and (a == c): crystalClass = CrystalClassCubic() else: crystalClass = CrystalClassOrthorhombic() else: crystalClass = CrystalClassTriclinic() # . Define symmetry. system.DefineSymmetry(crystalClass=crystalClass, a=a, b=b, c=c, alpha=alpha, beta=beta, gamma=gamma)
def GetSystem ( self, log = logFile ): """Get the system.""" if self.setUpType == "CHARMM": parameters = CHARMMParameterFiles_ToParameters ( self.parameterFiles, log = log ) system = CHARMMPSFFile_ToSystem ( self.setUpFile, isXPLOR = True, log = log, parameters = parameters ) elif self.setUpType == "MOL" : system = MOLFile_ToSystem ( self.setUpFile ) elif self.setUpType == "MOL2" : system = MOL2File_ToSystem ( self.setUpFile ) if self.xyzFile is not None: system.coordinates3 = XYZFile_ToCoordinates3 ( self.xyzFile ) if self.mmModel is not None: system.DefineMMModel ( self.mmModel ) if self.hasSymmetry: system.DefineSymmetry ( crystalClass = CrystalClassCubic ( ), a = self.a ) system.label = self.label system.Summary ( log = log ) return system
def ToSymmetry ( self, system ): """Assign symmetry to a system.""" # . Check for symmetry. if hasattr ( self, "box_dimensions" ): # . Unpack data. beta = self.box_dimensions[0] a = self.box_dimensions[1] b = self.box_dimensions[2] c = self.box_dimensions[3] # . Get the crystal class. if ( beta != 90.0 ): crystalClass = CrystalClassMonoclinic ( ) elif ( a == b ) and ( a == c ): crystalClass = CrystalClassCubic ( ) elif ( a == b ): crystalClass = CrystalClassTetragonal ( ) else: crystalClass = CrystalClassOrthorhombic ( ) # . Define symmetry. system.DefineSymmetry ( crystalClass = crystalClass, a = a, b = b, c = c, beta = beta )
def BuildCubicSolventBox ( molecule, nmolecules, log = logFile, moleculesize = None, excludeHydrogens = True, QRANDOMROTATION = True, randomNumberGenerator = None, scalesafety = 1.1 ): """Build a cubic solvent box.""" # . Get the number of molecules in each direction. nlinear = int ( math.ceil ( math.pow ( float ( nmolecules ), 1.0 / 3.0 ) ) ) # . Get the indices of the occupied sites. sites = sample ( range ( nlinear**3 ), nmolecules ) sites.sort ( ) # . Get the number of atoms in the molecule and an appropriate selection. natoms = len ( molecule.atoms ) selection = Selection.FromIterable ( range ( natoms ) ) # . Get a copy of molecule's coordinates (reorientated). coordinates3 = Clone ( molecule.coordinates3 ) coordinates3.ToPrincipalAxes ( ) # . Get the molecule size depending upon the input options. # . A molecule size has been specified. if moleculesize is not None: size = moleculesize # . Determine the size of the molecule as the diagonal distance across its enclosing orthorhombic box. else: radii = molecule.atoms.GetItemAttributes ( "vdwRadius" ) if excludeHydrogens: atomicNumbers = molecule.atoms.GetItemAttributes ( "atomicNumber" ) for ( i, atomicNumber ) in enumerate ( molecule.atoms.GetItemAttributes ( "atomicNumber" ) ): if atomicNumber == 1: radii[i] = 0.0 ( origin, extents ) = coordinates3.EnclosingOrthorhombicBox ( radii = radii ) size = extents.Norm2 ( ) * scalesafety # . Create the new system - temporarily resetting the coordinates. temporary3 = molecule.coordinates3 molecule.coordinates3 = coordinates3 solvent = MergeByAtom ( nmolecules * [ molecule ] ) molecule.coordinates3 = temporary3 # . Set the system symmetry. solvent.DefineSymmetry ( crystalClass = CrystalClassCubic ( ), a = size * float ( nlinear ) ) # . Set up for random rotations. if QRANDOMROTATION: if randomNumberGenerator is None: randomNumberGenerator = RandomNumberGenerator.WithRandomSeed ( ) rotation = Matrix33.Null ( ) # . Loop over the box sites. origin = 0.5 * float ( 1 - nlinear ) * size n = 0 translation = Vector3.Null ( ) for i in range ( nlinear ): translation[0] = origin + size * float ( i ) for j in range ( nlinear ): translation[1] = origin + size * float ( j ) for k in range ( nlinear ): if len ( sites ) == 0: break translation[2] = origin + size * float ( k ) # . Check for an occupied site. if sites[0] == n: sites.pop ( 0 ) # . Randomly rotate the coordinates. if QRANDOMROTATION: rotation.RandomRotation ( randomNumberGenerator ) solvent.coordinates3.Rotate ( rotation, selection = selection ) # . Translate the coordinates. solvent.coordinates3.Translate ( translation, selection = selection ) # . Increment the selection for the next molecule. selection.Increment ( natoms ) n += 1 # . Do some printing. if LogFileActive ( log ): summary = log.GetSummary ( ) summary.Start ( "Cubic Solvent Box Summary" ) summary.Entry ( "Number of Molecules", "{:d}" .format ( nmolecules ) ) summary.Entry ( "Density (kg m^-3)", "{:.3f}".format ( SystemDensity ( solvent ) ) ) summary.Entry ( "Box Side", "{:.3f}".format ( solvent.symmetryParameters.a ) ) summary.Entry ( "Molecule Size", "{:.3f}".format ( size ) ) summary.Stop ( ) # . Return the cubic system. return solvent
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 ) )
import glob, os, os.path from pBabel import CHARMMParameterFiles_ToParameters, CHARMMPSFFile_ToSystem, XYZFile_ToCoordinates3 from pCore import logFile, CPUTime, Selection, YAMLUnpickle from pMolecule import CrystalClassCubic, ElectronicState, NBModelABFS, QCModelMNDO, SystemWithTimings from pMoleculeScripts import LangevinDynamics_SystemGeometry, VelocityVerletDynamics_SystemGeometry #=================================================================================================================================== # . Parameters. #=================================================================================================================================== # . File and path names. _TestDataFileName = "systemData.yaml" _TestRootPath = "data" # . Other options. _CrystalClasses = {"Cubic": CrystalClassCubic()} _DoDynamics = True _ForceNoQC = False _NBModel = NBModelABFS() _QCModel = QCModelMNDO() _QCRegionKey = "Large QC Region" # "Small QC Region" _Steps = 1000 _UseLangevin = True _UseSystemWithTimings = True #=================================================================================================================================== # . Functions. #=================================================================================================================================== def FindTests(): """Find the tests."""
from pMolecule import CrystalClassCubic, MMModelOPLS, NBModelMonteCarlo from pMoleculeScripts import MergeByAtom methane = MOLFile_ToSystem("../mol/methane.mol") water = MOLFile_ToSystem("../mol/water.mol") # . Systems to create. _ToCreate = (([methane] + 215 * [water], "Methane in Water.", "ch4_water215_cubicBox_mc.xyz", "ch4_water215_cubicBox_mc.pkl"), (216 * [water], "Water Box.", "water216_cubicBox_mc.xyz", "water216_cubicBox_mc.pkl")) for (molecules, label, xyzPath, pklPath) in _ToCreate: system = MergeByAtom(molecules) system.label = label xyzSystem = XYZFile_ToSystem("../xyz/" + xyzPath) sideLength = float(xyzSystem.label.split()[-1]) system.coordinates3 = xyzSystem.coordinates3 system.DefineMMModel(MMModelOPLS("bookSmallExamples")) system.DefineNBModel(NBModelMonteCarlo()) system.DefineSymmetry(crystalClass=CrystalClassCubic(), a=sideLength) system.Summary() Pickle("../pkl/" + pklPath, system)
import os.path from pCore import Coordinates3, logFile, LogFileActive, Matrix33, Selection, TextFileReader , \ Transformation3_FromSymmetryOperationString, Transformation3Container , \ Vector3 from ExportImport import _Importer from pMolecule import CrystalClassCubic, CrystalClassHexagonal, CrystalClassMonoclinic, CrystalClassOrthorhombic, \ CrystalClassRhombohedral, CrystalClassTetragonal, CrystalClassTriclinic, System from STARFileReader import STARFileReader, STARFileTable #=================================================================================================================================== # . Parameters. #=================================================================================================================================== # . Crystal classes. _CrystalClasses = { "cubic" : CrystalClassCubic ( ) , "hexagonal" : CrystalClassHexagonal ( ) , "monoclinic" : CrystalClassMonoclinic ( ) , "orthorhombic" : CrystalClassOrthorhombic ( ) , "rhombohedral" : CrystalClassRhombohedral ( ) , "tetragonal" : CrystalClassTetragonal ( ) , "triclinic" : CrystalClassTriclinic ( ) , "trigonal" : CrystalClassHexagonal ( ) } # . Default disorder group. _DefaultDisorderGroup = "1" # . Undefined character. _UndefinedCharacter = "." #===================================================================================================================================