This code base is providing a shallow c++ wrapper to the truncatad power series algebra module as provided in mad-ng
https://github.com/MethodicalAcceleratorDesign/MAD.
For details see
https://github.com/MethodicalAcceleratorDesign/MAD/blob/dev/src/libgtpsa/README.GTPSA.
http://mad.web.cern.ch/mad/releases/madng/html/index.html
http://mad.web.cern.ch/mad/releases/madng/html/mad_mod_diffalg.html
NB: this code base modifies the C function defintions of the original code. For serious work please checkout the original code, in particular if you are using the "C" language.
The Python Pybind11 <- C++ <- C gtpsa interface was prototyped and implemented by Pierre Schnizer.
- References:
P. Schnizer, W. Khail, J. Bengtsson Small Talk on AT IPAC 2022
https://accelconf.web.cern.ch/ipac2022/papers/tupost029.pdf
L. Deniau, C. Tomoiagă Generalised Truncated Power Series Algebra for Fast Particle Accelerator Transport Maps IPAC 2015
Turned out that the CERN gtpsa map concatenator can not handle parameter dependence; so it had to be reimplemented.
The C++ <- C gtpsa bridge interface is in:
../src/gtpsa/python/src/gtpsa.cc
- /*
- Implementation split up in three parts:
- bridge: defined as template
- operator functions using the bridge
- class using c++ operators defined in a template
- class providing full functionality derived from the template
- This splits the functionality in different parts. Hopefully that
- makes the code a little more maintainable
*/
However, some of the key gtpsa map analysis functions are implemented in the Lua scripting language; see below.
Hence, they have been re-implemented in C++.
num_t doubleord_t unsigned charidx_t int32_tssz_t int32_t
Files:
../src/gtpsa/python/src/gtpsa.cc../src/gtpsa/c++/gtpsa/ss_vect.h../src/gtpsa/c++/gtpsa/ss_vect.cc
Function:
show
E.g. deriv, integ, and poisbra.
Cls in:../src/gtpsa/python/src/gtpsa.cc|is either tpsa or gtpsa.
Files:
1. Python: ../src/gtpsa/python/src/gtpsa.cc2a. ../src/gtpsa/c++/gtpsa/intern/with_operators.hpp2b. ../src/gtpsa/python/src/gtpsa_delegator.h3. ../src/gtpsa/c++/gtpsa/bridge/bridge.hpp1. C++: ../src/gtpsa/c++/gtpsa/tpsa.hppstatic_cast<const tpsa::base&>({tpsa parameter})tpsa({return tpsa::base value})2a. ../src/gtpsa/c++/gtpsa/intern/with_operators.hpp3. ../src/gtpsa/c++/gtpsa/bridge/bridge.hpp4. ../src/gtpsa/c++/gtpsa/mad/wrapper.tpp5. C: ../src/gtpsa/mad-ng/src/mad_tpsa_ops.c
../src/gtpsa/python/src/gtpsa.cc"tpsa" gpy::TpsaWithNamedIndex"_TPSAWithOp""_tpsa" TpsaOp = gtpsa::TpsaWithOp<gtpsa::TpsaTypeInfo>CtpsaOp = gtpsa::TpsaWithOp<gtpsa::CTpsaTypeInfo>"ctpsa" gpy::CTpsaWithNamedIndex"_CTPSAWithOp""_ctpsa" gtpsa::CTpsaWithOp
E.g. inv, pinv, and compose.
Files:
1. Python: ../src/gtpsa/python/src/ss_vect.cc2. ../src/gtpsa/c++/gtpsa/ss_vect.h3. ../src/gtpsa/c++/gtpsa/ss_vect.cc4. ../src/gtpsa/c++/gtpsa/mad/container_wrapper.tpp5. C: ../src/gtpsa/mad-ng/src/mad_tpsa_comp.c
Indexing from ss_vect_tpsa & conversion to tpsa (Should be refactored/simplified - i.e., implement the operator[])
E.g.:map.x.to_tpsa or map.iloc[0].to_tpsa()
Files for to_tpsa:
Python:../src/gtpsa/python/src/gtpsa.cc../src/gtpsa/python/src/gtpsa_delegator.h../src/gtpsa/python/src/gtpsa_variant.cc
The gtpsa Python Pybind11 <- C++ part is in:
Python interface:
../python/src/thor_scsi.cc
M_to_h_DFh_DF_to_MCtoRRtoCGoFixMap_Norm
and the implementation:
../src/gtpsa/c++/gtpsa/lielib.cc
../src/gtpsa/python/src/desc.cc
number_of_variables(ord_t *mo_=0, int *np_=0, ord_t *po_=0) -> intmaximum_orders(int nn=0, ord_t *no=nullptr) -> intmaximum_length(ord_t mo) -> intmono(idx_t i, std::vector<ord_t> *m) -> intindexsm -> int# E.g.:print(desc.number_of_variables(0, 0, 0))exps = np.zeros(nv, dtype=int)ord = desc.mono(0, exps)print(ord, exps)print(desc.index([1, 0, 0, 0, 0, 0, 0]))../src/gtpsa/python/src/ss_vect.cc
# Support a .loc["x"] access to the elements.template<class WrappedClass, class P_MGR, typename T># print (__str__) calls:pstriloc[]# E.g.:map.iloc[k]getOrderset_zero(void)truncate# E.g.:desc.truncate(3)TPSA map operations:
deriv(integ)mnrmfld2vecfgradliebraexppblogpbcomposeinvpinv../src/gtpsa/python/src/gtpsa.cc
- and
../src/gtpsa/python/src/gtpsa_delegator.h
# For functions returning a tpsa.print(Sets eps 1e-30 vs. 0 for the gtpsa print function to supress printing of zeroes)lengthget_description# E.g.:print(a.get_description())getsetgetvsetv...
The gtpsa C++ <- C functions are in:
../src/gtpsa/c++/gtpsa/python/objects_with_named_index.h
Basis arithmetic operators: [+, -, *, /,...].../src/gtpsa/c++/gtpsa/bridge/bridge.hpp
clear(void)# Parameters: (constant part, monomial index, value).setVariable(const base_type v, const idx_t iv = 0, const base_type scale = 0).# Return order & exponents for monomial with index i.mono(idx_t i, std::vector<ord_t> *m) -> int# Return index for monomial m.# use string for the exponents:index(std::string s)# use array for the exponents:index(const std::vector<ord_t> &m)# sparse monomials [(i, o)]:indexsm(const std::vector<int> m)# Return a pair (.first, .second) for ???cycle(const idx_t i, std::vector<ord_t> *m)cst()# Get constant term.get(void) get()# Get monget(const idx_t i) get(46)get(const std::string s) get()get(const std::vector<ord_t> &m) get(std::vector<ord_t>{2, 0, 0, 0, 0, 0, 0})getsm(const std::vector<int> &m)set(void)...# The 1st parameter is the offset - set to 1, to skip constant part: 0..getv(idx_t i, std::vector<base_type> *v)setv(idx_t i, const std::vector<base_type> &v)rgetordercstpowadddifsubmuldivaccsclinvinvsqrtrderivrinteg...../src/gtpsa/c++/gtpsa/mad/wrapper.tpp
print()print("", 1e-30, 0, stdout)For TPSA vector -- use cout << for map.rgetOrdersetvar(const GTPSA_BASE_T v, const idx_t iv = 0, const GTPSA_BASE_T scl = 0)mono(const idx_t i, std::vector<ord_t> *m)idxs(const std::string s)idxm(const std::vector<ord_t> &m)idxsm(const std::vector<int> m)cycle(const idx_t i, std::vector<ord_t> *m, GTPSA_BASE_T *v)get0(void) get()geti(const idx_t i) get(46)gets(const std::string s) get()getm(const std::vector<ord_t> &m) get(std::vector<ord_t>{2, 0, 0, 0, 0, 0, 0})getsm(const std::vector<int> &m)# The 1st parameter is offset - 1 to skip constant part: 0..getv(const idx_t i, std::vector<GTPSA_BASE_T> *v)setv(const idx_t i, const std::vector<GTPSA_BASE_T> &v)a*x[0]+bset0(const num_t a, const num_t b)a*x[i]+bseti(const idx_t i, const num_t a, const num_t b)a*x[m]+bsets(const std::string &s, const num_t a, const num_t b)a*x[m]+bsetm(const std::vector<ord_t> &m, const num_t a, const num_t b)rderivrinteg../src/gtpsa/c++/gtpsa/mad/tpsa_wrapper.hpp Wrapper for C++ <- C.
normequ../src/gtpsa/c++/gtpsa/bridge/container.hpp
sizegetMaximumOrdercomputeNormrvec2fld...../src/gtpsa/c++/gtpsa/mad/container_wrapper.tpp
sizegetMaximumOrdercomputeNormrvec2fldfld2vecfgradrliebrarexppbrlogpbrcompose (which call compose in the gtpsa library)rminvrpminv../src/gtpsa/c++/gtpsa/intern/with_operators.hpp
# The Python interface for maps calls:pstr# which calls:show()# For TPSA vector: only prints leading order - level parameter not implemented.show(stdout, level)print("", eps, 0)operator<<
The gtpsa print functions are in:
../src/gtpsa/mad-ng/src/mad_tpsa.c
mad_tpsa_setvar(tpsa_t *t, num_t v, idx_t iv, num_t scl)mad_tpsa_mono(const tpsa_t *t, idx_t i, ssz_t n, ord_t m[])mad_tpsa_idxs(const tpsa_t *t, ssz_t n, str_t s)mad_tpsa_idxm(const tpsa_t *t, ssz_t n, const ord_t m[])mad_tpsa_idxsm(const tpsa_t *t, ssz_t n, const int m[])mad_tpsa_cycle(const tpsa_t *t, idx_t i, ssz_t n, ord_t m[], num_t *v)mad_tpsa_get0(const tpsa_t *t)mad_tpsa_geti(const tpsa_t *t, idx_t i)mad_tpsa_gets(const tpsa_t *t, ssz_t n, str_t s)mad_tpsa_getm(const tpsa_t *t, ssz_t n, const ord_t m[])mad_tpsa_getsm(const tpsa_t *t, ssz_t n, const int m[])# The 2nd parameter is offset - 1 to skip constant part: 0..mad_tpsa_getv(const tpsa_t *t, idx_t i, ssz_t n, num_t v[])../src/gtpsa/mad-ng/src]/mad_tpsa_io.c
../src/gtpsa/mad-ng/src]/mad_tpsa_comp.c
print_damap
The general gtpsa C++ <- C interface is in:
../src/gtpsa/c++/gtpsa/desc.hpp
../src/gtpsa/c++/gtpsa/desc.cc
show# Prints out info, e.g.:# id=2, nn=7, nv=7, np=0, mo=5, po=0, to=5, uno=0, no=[5555555]info(FILE * fp = nullptr)getDescription()-># Get all the info:getInfo# e.g.:.getDescription()->getInfo()getNvmaxOrdmaxLengetNumberOfVariablesgetVariablesMaximumOrdergetNumberOfParametersgetParametersMaximumOrdergetTotalNumbergetOrderPerParametergetNv(ord_t *mo_=0, int *np_=0, ord_t *po_=0)maxOrd(int nn=0, ord_t *no=nullptr)maxLen(ord_t mo)# Sets to for all gtpsa elements???trunc(const ord_t to)# E.g.:.getDescription()->trunc(k)../src/gtpsa/c++/gtpsa/ss_vect.cc
# For functions returning an ss_vect<>.# For general indexing:idx()ss_vect_n_dimss_vectstate_space# For TPSA map: only prints leading order - level parameter not implemented.show(std::ostream &strm, int level = 1, bool with_endl = true)jacobianhessianset_zeroset_identitysetConstantsetJacobiansetHessianrcompose../src/gtpsa/c++/gtpsa/funcs.h
sqrtexplog...../src/gtpsa/c++/gtpsa/lielib.cc
M_to_h_DFh_DF_to_MCtoRRtoCGoFixMap_Norm
TPSA descriptor operations:
../src/gtpsa/mad-ng/src/mad_desc.h
../src/gtpsa/mad-ng/src/mad_desc.c
int mad_desc_getnv(const D *d, ord_t *mo_, int *np_, ord_t *po_)ord_t mad_desc_maxord(const D *d, int n, ord_t no_[n])# Sets to for all gtpsa elements???ord_t mad_desc_gtrunc(const desc_t *d, ord_t to)void mad_desc_info(const D *d, FILE *fp_)
TPSA vector operations:
../src/gtpsa/mad-ng/src/mad_tpsa.h
../src/gtpsa/mad-ng/src/mad_tpsa_ops.c
addsub...integderivpoisbra......cutord
TPSA map operations:
../src/gtpsa/mad-ng/src/mad_tpsa_comp.c
Localprint_damapPubliccomposetranslateeval../src/gtpsa/mad-ng/src]/mad_tpsa_comp_s.tc
compose../src/gtpsa/mad-ng/src]/mad_tpsa_minv.c
minvpinv../src/gtpsa/mad-ng/src/mad_tpsa_mops.c
Localprint_damapPublicexppblogpbliebrafgradCompute (Eq. (34)): | G(x;0) = -J grad.f(x;0)vec2fldCompute(Eqs. (34)-(37)): | f(x;0) = int_0^x J G(x';0) dx' = x^t J phi G(x;0)fld2vecmnrm (norm)
Also, a few are in:
(coded in Lua)
../src/gtpsa/mad-ng/src/madl_damap.mad
map_ctorfactor_mapFactored Lie of exponential and poisson bracket:| r = exp(:y1:) exp(:y2:)... xlieexppbflofacg...../src/gtpsa/madl_gphys.mad
make_symp (Make map symplectic, thesis by Liam Healy)| L. Healy Lie-Algebraic Methods for Treating Lattice Parameter Errors in Particle | Accelerators Thesis, Univ. of Maryland, 1986.gphys.normal_ng (Map normal form)normal_c (Phasor basis)
The Lua scripting language (Portuguese: lua -> moon) was created by the Computer Graphics Technology Group (Tecgraf) at the PUC Univ., Rio de Janeiro, Brazil in 1993:
https://www.lua.org/about.html
LuaJiT is a just-in-time compiler:
https://luajit.org/luajit.html