Skip to content

Commit

Permalink
04_19_2024: cleanup + pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
i-a-morozov committed Apr 19, 2024
1 parent 041296c commit cf36182
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.1
hooks:
- id: ruff
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ disable=[
"redefined-outer-name",
"not-callable"
]

[tool.pytest.ini_options]
filterwarnings = [
"ignore::DeprecationWarning"
]
30 changes: 29 additions & 1 deletion twiss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@
Version and aliases
"""
__version__ = '0.2.4'
__version__ = '0.2.5'

__all__ = [
'symplectify',
'is_symplectic',
'rotation',
'twiss',
'is_stable',
'propagate',
'advance',
'normal_to_wolski',
'wolski_to_normal',
'parametric',
'lb_normal',
'cs_normal',
'wolski_to_lb',
'lb_to_wolski',
'wolski_to_cs',
'cs_to_wolski',
'invariant',
'lb_invariant',
'cs_invariant',
'transport',
'wolski_transport',
'lb_transport',
'cs_transport',
'momenta',
'normal'
]

from twiss.matrix import symplectify
from twiss.matrix import is_symplectic
Expand Down
4 changes: 2 additions & 2 deletions twiss/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def normal(mean:Tensor,
>>> wx, wy = ws
>>> torch.allclose(db.covariance_matrix, ex*wx + ey*wy)
True
>>> torch.allclose(db.sample((2**22, )).T.cov(), db.covariance_matrix)
>>> torch.allclose(db.sample((2**20, )).T.cov(), db.covariance_matrix)
True
"""
for _ in wolski:
emittance = emittance.unsqueeze(-1)
Expand Down
16 changes: 8 additions & 8 deletions twiss/wolski.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ def twiss(m:Tensor, *,
m_s = torch.block_diag(*[b_s for _ in range(d)])
m_c = torch.block_diag(*[b_c for _ in range(d)])

l, v = torch.linalg.eig(m)
l, v = l.reshape(d, -1), v.T.reshape(d, -1, 2*d)
e, v = torch.linalg.eig(m)
e, v = e.reshape(d, -1), v.T.reshape(d, -1, 2*d)

u = torch.zeros_like(v)
for i, (v1, v2) in enumerate(v):
u[i] = v[i]/(-1j*(v1 @ m_s.to(cdtype) @ v2)).abs().sqrt()

k = torch.zeros_like(l)
k = torch.zeros_like(e)
v = torch.zeros_like(u)
for i in range(d):
o = torch.clone(l[i].log()).imag.argsort()
k[i], v[i] = l[i, o], u[i, o]
o = torch.clone(e[i].log()).imag.argsort()
k[i], v[i] = e[i, o], u[i, o]

t = 1.0 - k.log().abs().mean(-1)/(2.0*pi)

Expand Down Expand Up @@ -273,6 +273,6 @@ def advance(n:Tensor,
"""
d = len(n) // 2
i = torch.arange(d, dtype=torch.int64, device=n.device)
l = m @ n
f = mod(torch.arctan2(l[2*i, 2*i + 1], l[2*i, 2*i]), 2.0*pi)
return f, l @ rotation(*(-f))
k = m @ n
f = mod(torch.arctan2(k[2*i, 2*i + 1], k[2*i, 2*i]), 2.0*pi)
return f, k @ rotation(*(-f))

0 comments on commit cf36182

Please sign in to comment.