Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #251 from simphony/fix-nan-value-for-primitive-cell
Browse files Browse the repository at this point in the history
ValueError if the angle(s) for rhombohedral/triclinic are too big
  • Loading branch information
kitchoi committed Dec 18, 2015
2 parents 7c0965c + 13ed28f commit 4228c93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions simphony/cuds/primitive_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,16 @@ def for_rhombohedral_lattice(cls, a, alpha):

cosa = np.cos(alpha)
sina = np.sin(alpha)
delta2 = sina**2 - ((cosa-cosa**2) / sina)**2
if delta2 < 0:
message = ('Angle is too big (>120 degree). '
'Third vector is imaginary')
raise ValueError(message)

p1 = (a, 0, 0)
p2 = (a*cosa, a*sina, 0)
p3 = (a*cosa, a*(cosa-cosa**2) / sina,
a*np.sqrt(sina**2 - ((cosa-cosa**2) / sina)**2))
a*np.sqrt(delta2))
return cls(p1, p2, p3, BravaisLattice.RHOMBOHEDRAL)

@classmethod
Expand Down Expand Up @@ -493,11 +498,15 @@ def for_triclinic_lattice(cls, a, b, c, alpha, beta, gamma):
sinb = np.sin(beta)
cosg = np.cos(gamma)
sing = np.sin(gamma)
delta2 = sinb**2 - ((cosa-cosb*cosg) / sing)**2
if delta2 < 0:
message = "Angles are too big. Third vector is imaginary"
raise ValueError(message)

p1 = (a, 0, 0)
p2 = (b*cosg, b*sing, 0)
p3 = (c*cosb, c*(cosa-cosb*cosg) / sing,
c*np.sqrt(sinb**2 - ((cosa-cosb*cosg) / sing)**2))
c*np.sqrt(delta2))
return cls(p1, p2, p3, BravaisLattice.TRICLINIC)

@property
Expand Down
8 changes: 8 additions & 0 deletions simphony/cuds/tests/test_primitive_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def test_primitive_cell_for_rhombohedral_lattice(self):
with self.assertRaises(ValueError):
PrimitiveCell.for_rhombohedral_lattice(1, np.pi)

with self.assertRaises(ValueError):
# angle too big
PrimitiveCell.for_rhombohedral_lattice(1, np.pi*2./3.+0.1)

cosa = np.cos(self.alpha)
sina = np.sin(self.alpha)

Expand Down Expand Up @@ -225,6 +229,10 @@ def test_primitive_cell_for_triclinic_lattice(self):
with self.assertRaises(ValueError):
PrimitiveCell.for_triclinic_lattice(1, 2, 3, 0.1, 0.2, 0.8)

with self.assertRaises(ValueError):
# angles too big
PrimitiveCell.for_triclinic_lattice(1, 2, 3, 2.1, 2.1, 2.1)

cosa = np.cos(self.alpha)
cosb = np.cos(self.beta)
sinb = np.sin(self.beta)
Expand Down

0 comments on commit 4228c93

Please sign in to comment.