Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. def pymatgen_to_aiida(pymatgen_structure, StructureData):
  2.  
  3.     # analyze magnetism
  4.     magnetic_structure = CollinearMagneticStructureAnalyzer(pymatgen_structure, make_primitive=False)
  5.  
  6.     has_spin = 0
  7.     if magnetic_structure.is_magnetic:
  8.         if magnetic_structure.is_collinear:
  9.             has_spin=2
  10.         else:
  11.             # TODO...
  12.             raise NotImplemented
  13.             has_spin=4
  14.     else:
  15.         has_spin=1
  16.         return (StructureData(pymatgen=pymatgen_structure), has_spin, {})
  17.  
  18.     # generate collinear spin structure
  19.     structure_with_spin = magnetic_structure.get_structure_with_spin()
  20.    
  21.     # initial definition of cell (is PBC specified by default?)
  22.     cell = structure_with_spin.lattice.matrix.tolist()
  23.     aiida_structure = StructureData(cell=cell)
  24.  
  25.     # collect magnetic elements by name. For each of them create kinds
  26.     magnetic_elements_kinds = {}
  27.     for site in structure_with_spin:
  28.  
  29.         # check (collinear) spin and element name
  30.         spin = site.specie.spin
  31.         element = site.specie.element.symbol
  32.        
  33.         kind_name = None
  34.         if np.abs(spin) > 0:
  35.             # checks if element was already found to be magnetic in a previous site
  36.             # otherwise return an empty dictionary to be filled with the information
  37.             # of this site
  38.             kinds_for_element = magnetic_elements_kinds.get(element, {})
  39.            
  40.             # If the spin of this site is for this element is the same we found
  41.             # previously, just give it the same kind, otherwise add it as
  42.             # a new kind for this element type.
  43.             for kind, kind_spin in kinds_for_element.items():
  44.                 if np.allclose (spin, kind_spin):
  45.                     kind_name = kind
  46.                     break
  47.             else:
  48.                 kind_name = '{}{}'.format(element, len(kinds_for_element)+1)
  49.                 kinds_for_element[kind_name] = spin
  50.            
  51.             # store the updated list of kinds for this element in the full dictionary.
  52.             magnetic_elements_kinds[element] = kinds_for_element
  53.  
  54.         # prepare to add site to AiiDA structure...
  55.         inputs = {
  56.             'symbols': [x.symbol for x in site.species_and_occu.keys()],
  57.             'weights': [x for x in site.species_and_occu.values()],
  58.             'position': site.coords.tolist()
  59.         }
  60.         # ...and also its kind, in case we chose one...
  61.         if kind_name is not None:
  62.             inputs['name'] = kind_name
  63.         # ...and it's done for this one!
  64.         aiida_structure.append_atom(**inputs)
  65.    
  66.     # Finally return structre, type of magnetic order (1 or 2 for the time being)
  67.     # and a dictionary for magnetic elements, where all the kinds are reported together
  68.     # with the value of the spin.
  69.     return (aiida_structure, has_spin, magnetic_elements_kinds )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement