Z2PackDev / symmetry_representation

Describing and constructing symmetry operations and their representations.
https://symmetry-representation.greschd.ch
Apache License 2.0
6 stars 4 forks source link

Some quirks with Spin: 'tuple' object has no attribute 'total' #14

Closed MSteggles closed 8 months ago

MSteggles commented 8 months ago

Hi,

Thanks for making such a helpful package, I've found it really useful. I've found a bit of a quirk with the Orbital/Spin classes; generating time-reversal matrices will often return 'tuple object has no attribute total', caused by the spin not importing properly into the Orbital class (using Python3.11). For example:

import symmetry_representation as sr

fct = sr.WANNIER_ORBITALS['d'][0]

spin_up = sr.Spin(total=0.5, z_component=0.5)

orb = sr.Orbital(position=0, function_string=fct, spin=spin_up)

type(orb.spin)

returns tuple, not the correct sr object.

Changing line 45 of _orbitals.py from self.spin = spin to self.spin = Spin(total = spin[0], z_component = spin[1]) works fine, although feels a little dirty. Any idea why this is happening? The spin being written to self.spin is a sr object. Given this works now it's not a particularly pressing concern but I thought you'd appreciate hearing it.

Regards, Matthew

greschd commented 8 months ago

Hi @MSteggles,

I can't seem to reproduce this issue. Is this the full code with which you initially saw the issue, or a simplified variant?

Also, could you provide some more details regarding your platform / install? What's the OS, and output of pip freeze?

Cheers, Dominik

greschd commented 8 months ago

At a complete guess, if there was a trailing comma on the line defining the spin, it would be a tuple:

import symmetry_representation as sr

fct = sr.WANNIER_ORBITALS['d'][0]

spin_up = sr.Spin(total=0.5, z_component=0.5),

orb = sr.Orbital(position=0, function_string=fct, spin=spin_up)

type(orb.spin) # now it's tuple
MSteggles commented 8 months ago

Hi,

Thanks for the quick response, I only just got a chance to look into this again; I'm very sorry but I've checked my previous minimal input and you're correct, that works fine! I copied the wrong damn code snippet. I've just found the problem - I was using a zip(spin_up, spin_down) instead of a list, which breaks the default behaviour as it converts the objects into, surprise surprise, tuples.

import symmetry_representation as sr

spin_up = sr.Spin(total=0.5, z_component=0.5)
spin_down = sr.Spin(total=0.5, z_component=-0.5)

for spin in (spin_up, spin_down):
    print(type(spin))  # Returns sr object

for spin in zip(spin_up, spin_down):
    print(type(spin))  # Returns tuple

Not sure why the zip was there, totally my mistake :-) This also explains why my fix worked; it takes the components of the tuples and restores them to being sr objects.

Thank you very much for your time! Matthew

greschd commented 8 months ago

No worries, and happy to help!