Exemple #1
0
def readGulpOutput(system, fileName):
  """
  Reads the gulp output and updates atoms' positions and systems energy
  
  """
  
  system_Type = _costSystemBox
    
  success = False
  error = ""
    
  if not os.path.isfile(fileName):
    error = "File [%s] doesn't exist." % (fileName)
    return success, error, system
  
  try:
    f = open(fileName)
  except:
    error = "Cannot read file [%s]" % (fileName)
    return success, error, system
    
  # optimisation achieved?
  success = Utilities.stringInFile(_constOutOptiAchieved, f)
  success = True
  
  if not success:
    error = "Optimisation has not been achieved"
  
  optiAchievedSection = False
  finalEnergy_eV = None
  
  iniParamsSectionSt = False
  iniParamsCount = 0
  
  finalCoordsSectionSt = False
  finalParamsSectionSt = False
  finalParamsSectionBreakCount = 0
  finalParamsCount = 0
  
  i = 0
  
  # saving name and path  
  name_array = fileName.split("/")
  name_array_len = len(name_array)
  
  name_array = name_array[name_array_len-1].split(".")

  system.name = name_array[0]
  system.path = fileName

  # read in final atom positions
  if success:
    for line in f:
      line = line.strip()
            
      # Looking for the final energy
      if optiAchievedSection:
        if _constOutFinalEnergy in line:
          energyArray = line.split()
          finalEnergy_eV = float(energyArray[3])
          
          # updating the final energy value
          system.totalEnergy = finalEnergy_eV
          system.energyDefinition = "Gulp"
          
          optiAchievedSection = False
      
       # Found the end of the section
      if ((_constOutFinalDerivs in line) or (_constOutFinalDerivsPoly in line)):
        finalParamsSectionSt = False
      
      # Found the end of the section
      
      if ((_constIniParamsCellVol in line) or (_constIniParamsCellParamPoly in line)):
        iniParamsSectionSt = False
      
      if finalCoordsSectionSt:
        array = line.split()
        
        if (len(array) == 7):
          no_str = array[0]

          try:
            no_int = np.int16(no_str)
          except:
            no_int = -1
            
          if no_int == -1: 
            finalCoordsSectionSt = False
          else:

            atomSpecie = array[1]
            atomType =  array[2]
                        
            # Do atom speces and types match?
            if ((not system.specieList[system.specie[i]].lower() == atomSpecie.lower()) or
                (not system.gulpAtomType[i] == atomType)):
              
              error = "atom types do not match %d : %s != %s" % (i, system.specieList[system.specie[i]], atomSpecie)
              return False, error
            
            # Reading the atom positions
            posx = float(array[3])
            posy = float(array[4])
            posz = float(array[5])
            
            # Updating the atom positions
            system.pos[3*i]   = posx
            system.pos[3*i+1] = posy
            system.pos[3*i+2] = posz
                       
            i += 1
            
            if i == system.NAtoms:
              finalCoordsSectionSt = False
                
      if finalParamsSectionSt:
        
        if "-----" in line:
          finalParamsSectionBreakCount += 1
        
        if finalParamsSectionBreakCount < 2:
          array = line.split()
          
          if len(array) == 6:
            # new cell dimensions
            if finalParamsCount == 0:
              system.cellDims_final[0] = np.float64(array[1])
              
            elif finalParamsCount == 1:
              system.cellDims_final[1] = np.float64(array[1])
            
            elif finalParamsCount == 2:
              system.cellDims_final[2] = np.float64(array[1])
            
            # new cell angles:
            elif finalParamsCount == 3:
              system.cellAngles_final[0] = np.float64(array[1])
            
            elif finalParamsCount == 4:
              system.cellAngles_final[1] = np.float64(array[1])
            
            elif finalParamsCount == 5:
              system.cellAngles_final[2] = np.float64(array[1])

            finalParamsCount += 1
        
        else:
          finalParamsSectionSt = False
      
      if iniParamsSectionSt:
        array = line.split()
                
        if system_Type == _costSystemPolymer:

          if len(array) == 3:
            system.cellDims_ini[0] = np.float64(array[0])
            system.cellDims_ini[1] = np.float64(array[1])
            system.cellDims_ini[2] = np.float64(array[2])
                      
        else:
        
          if len(array) == 6:
            # new cell dimensions
            if iniParamsCount == 0:
              system.cellDims_ini[0] = np.float64(array[2])
              system.cellAngles_ini[0] = np.float64(array[5])
              
            elif iniParamsCount == 1:
              system.cellDims_ini[1] = np.float64(array[2])
              system.cellAngles_ini[1] = np.float64(array[5])
              
            elif iniParamsCount == 2:
              system.cellDims_ini[2] = np.float64(array[2])
              system.cellAngles_ini[2] = np.float64(array[5])
            
            iniParamsCount += 1

      # Found the beginning of the section
      if ((_constOutFinalCartCoords in line) or (_constOutFinalFracCoords in line) or 
          (_constOutFinalFracCartCoords in line)):
        finalCoordsSectionSt = True
      
      # Found the beginning of the section
      if ((_constOutFinalParams in line) or (_constOutFinalDerivsPoly in line)):
        finalParamsSectionSt = True
 
       # Found the beginning of the section
      if (_constIniParams in line):
        iniParamsSectionSt = True
      
      if (_constIniParamsPoly in line):
        iniParamsSectionSt = True
        system_Type = _costSystemPolymer

      if _constOutOptiAchieved in line:
        optiAchievedSection = True
  
  success = True
  return success, error
Exemple #2
0
def readGulpOutputPolymerOutput(polymer, fileName):
  """
  Reads the gulp output as polymer
  
  """
  
  success = False
  error = ""
    
  if not os.path.isfile(fileName):
    error = "File [%s] doesn't exist." % (fileName)
    return success, error, polymer
  
  try:
    f = open(fileName)
  except:
    error = "Cannot read file [%s]" % (fileName)
    return success, error, polymer
    
  # optimisation achieved?
  success = Utilities.stringInFile(_constOutOptiAchieved, f)
  success = True
  
  if not success:
    error = "Optimisation has not been achieved"
  
  optiAchievedSection = False
  finalEnergy_eV = None
  
  finalCoordsSectionSt = False
  finalParamsSectionSt = False
  finalParamsSectionBreakCount = 0
  finalParamsCount = 0
  
  i = 0
  
  # read in final atom positions
  if success:
    for line in f:
      line = line.strip()
            
      # Looking for the final energy
      if optiAchievedSection:
        if _constOutFinalEnergy in line:
          energyArray = line.split()
          finalEnergy_eV = float(energyArray[3])
          
          # updating the final energy value
          polymer.totalEnergy = finalEnergy_eV
          polymer.energyDefinition = "Gulp"
          
          optiAchievedSection = False
      
       # Found the end of the section
      if ((_constOutFinalDerivs in line) or (_constOutFinalDerivsPoly in line)):
        finalParamsSectionSt = False
      
      # Found the end of the section
      
      if finalCoordsSectionSt:
        array = line.split()
        
        if (len(array) == 7):
          no_str = array[0]

          try:
            no_int = np.int16(no_str)
          except:
            no_int = -1
            
          if no_int == -1: 
            finalCoordsSectionSt = False
          else:

            atomSpecie = array[1]
            atomType =  array[2]
                        
            # Do atom speces and types match?
            if ((not polymer.specieList[polymer.specie[i]].lower() == atomSpecie.lower()) or
                (not polymer.gulpAtomType[i] == atomType)):
              
              error = "atom types do not match %d : %s != %s" % (i, polymer.specieList[polymer.specie[i]], atomSpecie)
              return False, error
            
            # Reading the atom positions
            posx = float(array[3])
            posy = float(array[4])
            posz = float(array[5])
            
            # Updating the atom positions
            polymer.pos[3*i]   = posx
            polymer.pos[3*i+1] = posy
            polymer.pos[3*i+2] = posz
                       
            i += 1
            
            if i == polymer.NAtoms:
              finalCoordsSectionSt = False
                
      if finalParamsSectionSt:
        
        if "-----" in line:
          finalParamsSectionBreakCount += 1
        
        if finalParamsSectionBreakCount < 2:
          array = line.split()
              
          if len(array) == 6:
            # new cell dimensions
            if finalParamsCount == 0:
              polymer.cellDims_final[0] = np.float64(array[1])
              
            elif finalParamsCount == 1:
              polymer.cellDims_final[1] = np.float64(array[1])
            
            elif finalParamsCount == 2:
              polymer.cellDims_final[2] = np.float64(array[1])
            
            # new cell angles:
            elif finalParamsCount == 3:
              polymer.cellAngles_final[0] = np.float64(array[1])
            
            elif finalParamsCount == 4:
              polymer.cellAngles_final[1] = np.float64(array[1])
            
            elif finalParamsCount == 5:
              polymer.cellAngles_final[2] = np.float64(array[1])

            finalParamsCount += 1
        
        else:
          
          finalParamsSectionSt = False
          
      # Found the beginning of the section
      if ((_constOutFinalCartCoords in line) or (_constOutFinalFracCoords in line) or 
          (_constOutFinalFracCartCoords in line)):
        finalCoordsSectionSt = True
      
      # Found the beginning of the section
      if _constOutFinalDerivsPoly in line:
        finalParamsSectionSt = True
 
      if _constOutOptiAchieved in line:
        optiAchievedSection = True
  
  # saving name and path  
  name_array = fileName.split("/")
  name_array_len = len(name_array)
  polymer.name = name_array[name_array_len-1].split(".")[0]
  polymer.path = fileName
  
  if success:
    # multiplying the fractional coordinate of the polymer with the cell parameter
    for i in range(polymer.NAtoms):
      polymer.pos[i*3] *= polymer.cellDims_final[0]
    
    polymer.cellDims = copy.deepcopy(polymer.cellDims_final)
    polymer.cellAngles = copy.deepcopy(polymer.cellAngles_final)
    
  success = True
  return success, error