Git Product home page Git Product logo

apace's Introduction

apace

Python Version PyPI CI Docs License: GPL v3

apace is yet another particle accelerator code designed for the optimization of beam optics. It is available as Python package and aims to provide a convenient and straightforward API to make use of Python's numerous scientific libraries.

Installing

Install and update using pip:

pip install -U apace

Requirements

  • Python 3.6 or higher (CPython or PyPy)
  • CFFI 1.0.0 or higher
  • NumPy/SciPy
  • Matplotlib

Quick Start

Import apace:

import apace as ap

Create a ring consisting out of 8 FODO cells:

d1 = ap.Drift('D1', length=0.55)
b1 = ap.Dipole('B1', length=1.5, angle=0.392701, e1=0.1963505, e2=0.1963505)
q1 = ap.Quadrupole('Q1', length=0.2, k1=1.2)
q2 = ap.Quadrupole('Q2', length=0.4, k1=-1.2)
fodo_cell = ap.Lattice('FODO', [q1, d1, b1, d1, q2, d1, b1, d1, q1])
fodo_ring = ap.Lattice('RING', [fodo_cell] * 8)

Calculate the Twiss parameters:

twiss = ap.Twiss(fodo_ring)

Plot horizontal and vertical beta functions using matplotlib:

import matplotlib.pyplot as plt
plt.plot(twiss.s, twiss.beta_x, twiss.s, twiss.beta_y)

Links

License

GNU General Public License v3.0

apace's People

Contributors

felix-andreas avatar julrei avatar paulgoslawski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jianghongping

apace's Issues

[Security] Workflow ci.yml is using vulnerable action actions/checkout

The workflow ci.yml is referencing action actions/checkout using references v1. However this reference is missing the commit a6747255bd19d7a757dbdda8c654a9f84db19839 which may contain fix to the some vulnerability.
The vulnerability fix that is missing by actions version could be related to:
(1) CVE fix
(2) upgrade of vulnerable dependency
(3) fix to secret leak and others.
Please consider to update the reference to the action.

Python 3.8: Cached property

Python 3.8 now has a functools.cached_property

from functools import cached_property

class Foo:
    def __init__(self):
        self._x = 0

    @cached_property
    def x(self):
        print("Increment x!")
        return self._x

Create a new foo:

>>> foo = Foo()
>>> foo.x
Increment x!
1
>>> foo.x
1

The cache can be invalidated with:

>>> del foo.x
>>> foo.x
Increment x!
2

Rounding error in floating point numbers for `length` attribute

Maybe it is better to use Decimal floating point numbers instead of the default binary floating point numbers. Another possiblity would be to use mm and an int type to store the length.

For the length attribute, the wrong representation of decimal numbers can become a problem: When accumulating the element ending position for a FODO lattice

{
  "name": "FODO_RING",
  "description": "This is the simplest possible strong focusing lattice.",
  "elements": {
    "D1": ["Drift", {"length": 0.55}],
    "Q1": ["Quadrupole", {"length": 0.2, "k1": 1.2}],
    "Q2": ["Quadrupole", {"length": 0.4, "k1": -1.2}],
    "B1": ["Dipole", {"length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}]
  },
  "sub_lattices": {
    "FODO": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
  },
  "lattice": ["FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO"]
}

the length of the first cell gets calculated correctly. Cells beginning with the 6th cell have the wrong length:

First cell:
------------
end 0.75 D1
end 2.25 B1
end 2.8 D1
end 3.1999999999999997 Q2
end 3.75 D1
end 5.25 B1
end 5.8 D1
end 6.0 Q1
...
6th cell:
----------
end 30.200000000000003 Q1
end 30.750000000000004 D1
end 32.25 B1
end 32.8 D1
end 33.199999999999996 Q2
...
8th cell:
end 47.799999999999976 D1
end 47.99999999999998 Q1

The problem is, that this is inconsistent with the length attribute of the ring:

>>> ap.Lattice.from_file("fodo_ring.json").length
48.0

which, returns the "correct" length. This is because the Lattice object will add the length of all its sub_lattices (which have length 6.0) instead of adding the length of all its elements (like shown above).

Implement API to change n_kicks

Maybe it would be the best to just pass n_kicks as parameter to TransferMatrices and do not allow it to be changed after an instance was created. This would make the implementation easier.

Tests:

  • test should use as little as possible kicks to be fast

Create Portfolio for given Latticefile

Portfolio like DIN A4 PDF with all important properties of Lattice:

  • nice formatted table of elements and cells and all there attributes (length, angle, k1, k2, ...)
  • plot of all Twiss parameter
  • Tunes fractional and integer (also in kHz)
  • natural chromaticity
  • floorplot (maybe in later iterations)

Create tutorials for Documentation

  • Load lattice from file
  • Save lattice from file
  • Change magnets
  • Linear Beam Dynamics
  • Create Necktie plot
  • Matrix Tracking
  • Tune from particle oscillation

Run Automated Tests

Run automated tests for twiss parameter:

  • Use Pytests
  • check if Tunes are correct
  • check beta, alpha, gamma functions for individual points

Test with pypy

Currently blocked by github actions, as it does not support pypy3.6.9

Names of elements should be unique

different elements can not have the same name. If, then calculations error occurs in MatrixCalculation without warning or errror message.

add positions dict

add dict which returns a tuple of positions for a given element! similar to indices

Improve the workflow with the `Twiss` object

Currently an instance of the Twiss class is linked a given Lattice object. If the Lattice object changes so will the Twiss object.

This can be counter-intuitive if one wants to save some reference values before optimizing a given lattice and can lead to mistakes:

twiss = ap.Twiss(lattice)
ref_beta_x = twiss.beta_x

# do the opitmization
beta_x = twiss.beta_x

Now ref_beta_x is bound to the same underlying numpy.ndarray as beta_x. This might be error prone.

One solution to that would be to always allocate a new numpy.ndarray when calculating th twiss parameter instead of reusing the old one. Test how large the overhead would be!

pytest failed on ubuntu 18.04

jvl@mlsr03:~/analysis/FelixAndreas/apace on master
$pytest 
===================================== test session starts =====================================
platform linux -- Python 3.7.0, pytest-5.4.1, py-1.6.0, pluggy-0.13.1
rootdir: /home/jvl/analysis/FelixAndreas/apace, inifile: setup.cfg, testpaths: tests
plugins: openfiles-0.3.0, remotedata-0.3.0, arraydiff-0.2, doctestplus-0.1.3
collected 23 items                                                                            

tests/test_classes.py EEEEE                                                             [ 21%]
tests/test_cli.py E                                                                     [ 26%]
tests/test_io.py EE                                                                     [ 34%]
tests/test_matrix_method.py E                                                           [ 39%]
tests/test_plot.py EE                                                                   [ 47%]
tests/test_tracking.py EE                                                               [ 56%]
tests/test_tracking_integration.py E                                                    [ 60%]
tests/test_twiss.py EEEEEEEEE                                                           [100%]

=========================================== ERRORS ============================================
____________________________ ERROR at setup of test_length_changed ____________________________

item = <Function test_length_changed>

    def pytest_runtest_setup(item):
    
>       remote_data = item.get_marker('remote_data')
E       AttributeError: 'Function' object has no attribute 'get_marker'

../../../anaconda3/lib/python3.7/site-packages/pytest_remotedata/plugin.py:64: AttributeError
_____________________________ ERROR at setup of test_unique_names _____________________________

...

=============================== 4 warnings, 23 errors in 0.30s ================================

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.