A fix to the QE module

tl;dc: I have fixed an error in the QE module. Check out the new version here: http://www.levilentz.com/Codes/QE.Index.py

Hello All,

As you know, I have been working on and constantly expanding my QE module as I need to. You can read about it here, and here, and here.

I started it about a year ago when I was trying to learn the ins and outs of python, and boy has it changed over the years as I have learned python. However there was one big error in the code, when it would read in atoms into the self.atoms array they would be a list of lists that would look something like this:

[...]
['Si',0.0,0.0,0.0]
[...]

Can you see the error here? First I have a sting in there, second I sometimes will want to do math with the positions. As of now, I cannot do that. Each time I want to do anything I have to extract the [0.0,0.0,0.0] do the math on it, then put it back. This is a terrible coding practice. The first thing you might think I could do is use a dictionary such as:

self.atoms['Si'] = np.array([0.0,0.0,0.0])

But that would quickly not be tractable as I will always have more than one Si atom in my simulation (presumably). To get around this, I wanted to use a dictionary, however a classic dictionary will need to be extended. To accomplish this, I made another class to accompany the QE class to keep track of the atom indexes. This means that if I have several Si atoms they would get stored in the dictionary as ‘Si0’, ‘Si1’, ‘Si2’, etc.

Here is this new class. I have called it ‘index’ because I am very creative.

class index():
  def __init__(self):
    self.keys = {}

  def sanitize(self,s):
    return ''.join([i for i in s if not i.isdigit()])
  
  def key(self,specie):
    if specie not in self.keys:
      self.keys[specie] = 0
    else:
      self.keys[specie] += 1
    return specie + str(self.keys[specie])
  
  def reset(self):
    self.keys.clear()

Essentially this class internally stores a dictionary called ‘keys.’ This keeps track of the total number of atoms x. To fill this up, you pass it into the class by calling key(specie). This increments the count so that if you have 4 Si atoms, it would be internally as {‘Si’:4}. When you pass another Si atom into it, it would return Si5.

Two other functions are also required: sanitize is used to remove the numbers from the key. When you pass it ‘Si4’ it will return Si. The reset function simply clears the dictionary in case we need to pass a new set of data into the index class. This is important for if we use the file_process command on a vc-relax output file.

You can find the updated QE module can be found here: http://www.levilentz.com/Codes/QE.Index.py

Happy computing,

Levi

2 comments Add yours

Leave a Reply

Your email address will not be published. Required fields are marked *