1

I'm new to Python and MDAnalysis. I am trying to use MDAnalysis for analysis. However, I run into this error:

SelectionError: Selection failed: could not convert string to float 'cold'.

I would like to select the molecules named "BF4", "EMI", and "TMA". The selection depend on the z position of the molecules. I have cold and cnew but I cannot compare the value of the z coordinate of each molecule with these two values as they are considered as string and not float.

Could you please help me? Thanks a lot

  with MDAnalysis.Writer("tube.xtc",tube.n_atoms) as W: 
     t=50 
     with open('lineCNT16.gro','w') as f: 
        for ts in universe.trajectory: 
           W.write(tube) 
           tube = sum(sorted(tube, key=lambda x: x.position[2]))  
           lobf = [] 
           chunk = 40 
           for i in range(len(tube) // 40):  # how many times can we chunk the tube? 
            piece = tube[i*chunk:(i+1)*chunk]  # this is selecting [0:20] first, then [20:40] etc 
            position = piece.positions.mean(axis=0) 
            lobf.append(position) 

           print (ts,lobf) 
           mol=29627 
           f.write('Generated by trjconv : Ionic liquid simulation t=  '"%s\n"%t) 
           f.write('18\n') 
           f.write('    1C08      C29626   4.247   4.253   7.544\n') 
           cold = 7.544 
           for position in lobf: 
            a=position[0]/10   
            b=position[1]/10 
            c=position[2]/10 
            cnew=c 
            print(cnew) 
            f.write('    1C08      ')     
            f.write('C')   
            f.write("%0.8s"%mol) 
            f.write('   ')      
            f.write("%0.5s"%a)  
            f.write('   ') 
            f.write("%0.5s"%b)   
            f.write('   ') 
            f.write("%0.5s\n"%c) 
            mol=mol+1 
            BF4=universe.atoms.select_atoms("resname BF4 and prop z >= cold and prop z<= cnew") 
            #EMI=universe.atoms.select_atoms("resname EMI and prop z >= cold and prop z<= cnew") 
            #TMA=universe.atoms.select_atoms("resname TMA and prop z >= cold and prop z<= cnew") 
            #print(EMI) 
            #print(BF4) 
            #print(TMA) 
            cold=cnew 
           f.write('    1C08      C29643   4.247   4.253   12.64\n')  
           f.write('6.15000   6.20000  20.18420\n') 
           t=t+50.00000   
           mol=29627 
iz_
  • 15,923
  • 3
  • 25
  • 40
Candy Deck
  • 13
  • 4

1 Answers1

1
BF4=universe.atoms.select_atoms("resname BF4 and prop z >= cold and prop z<= cnew") 

MDAnalysis expects actual values (float, like 5.7) rather than string("cold"). It is telling you that it needs a number instead of string. For example:

BF4=universe.atoms.select_atoms("resname BF4 and prop z >= 5.2 and prop z<= 7.5"

You have a variable "cold", however you might need to brush up your skills on how to concatenate your selection string with that variable.

Here is one small tutorial that explains it: https://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

mateuszb
  • 1,072
  • 13
  • 26