Skip to content
Snippets Groups Projects
Verified Commit ac9b4369 authored by Anton Akhmerov's avatar Anton Akhmerov
Browse files

raise a proper error in check_symmetry

closes #44
parent b0a3dac6
No related branches found
No related tags found
No related merge requests found
Pipeline #160683 passed
...@@ -353,19 +353,28 @@ def check_symmetry(family, symmetries, num_digits=None): ...@@ -353,19 +353,28 @@ def check_symmetry(family, symmetries, num_digits=None):
In the case that the input family has been rounded, num_digits In the case that the input family has been rounded, num_digits
should be the number of significant digits to which the family should be the number of significant digits to which the family
was rounded. was rounded.
"""
Raises
------
ValueError
If the family does not satisfy the symmetry.
"""
def fail():
raise ValueError(f'Member {member} does not satisfy symmetry {symmetry}.')
for symmetry in symmetries: for symmetry in symmetries:
# Iterate over all members of the family # Iterate over all members of the family
for member in family: for member in family:
if isinstance(symmetry, PointGroupElement): if isinstance(symmetry, PointGroupElement):
if num_digits is None: if num_digits is None:
assert symmetry.apply(member) == member if symmetry.apply(member) != member:
fail()
else: else:
assert symmetry.apply(member).around(num_digits) == member.around(num_digits) if symmetry.apply(member).around(num_digits) != member.around(num_digits):
fail()
elif isinstance(symmetry, ContinuousGroupGenerator): elif isinstance(symmetry, ContinuousGroupGenerator):
# Continous symmetry if applying it returns zero # Continous symmetry if applying it returns zero
assert symmetry.apply(member) == {} if symmetry.apply(member) != {}:
fail()
def constrain_family(symmetries, family, sparse_linalg=False): def constrain_family(symmetries, family, sparse_linalg=False):
......
from pytest import raises
import sympy import sympy
import numpy as np import numpy as np
import scipy.linalg as la import scipy.linalg as la
...@@ -58,6 +60,13 @@ def test_check_symmetry(): ...@@ -58,6 +60,13 @@ def test_check_symmetry():
else: # Symmetry commutes else: # Symmetry commutes
assert np.allclose(Left - Right, 0) assert np.allclose(Left - Right, 0)
# Test correctly raising a ValueError
with raises(ValueError):
check_symmetry(
[Model({1: np.eye(2)}, momenta=["k_x"])],
[chiral(1, np.diag([1, -1]))]
)
def test_bloch_generator(): def test_bloch_generator():
"""Square lattice with time reversal and rotation symmetry, such that all hoppings are real. """ """Square lattice with time reversal and rotation symmetry, such that all hoppings are real. """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment