0% found this document useful (0 votes)
36 views

Object Oriented C++ Software Components For Accelerator Design

This document discusses object-oriented C++ software components being developed at Tech-X Corporation for accelerator design and modeling. It describes several C++ class libraries including: TxSTD for standard utilities, TxID for data structures, TxAC for accelerator modeling, an X/Motif GUI for visualization, TxAN for analysis/simulation, and LION++ for optimization. It emphasizes that object-oriented programming allows components to work independently yet together through inheritance, polymorphism and encapsulation. The goal is to make the libraries interoperable as true software components using "traits" mechanisms.

Uploaded by

RajeshGupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Object Oriented C++ Software Components For Accelerator Design

This document discusses object-oriented C++ software components being developed at Tech-X Corporation for accelerator design and modeling. It describes several C++ class libraries including: TxSTD for standard utilities, TxID for data structures, TxAC for accelerator modeling, an X/Motif GUI for visualization, TxAN for analysis/simulation, and LION++ for optimization. It emphasizes that object-oriented programming allows components to work independently yet together through inheritance, polymorphism and encapsulation. The goal is to make the libraries interoperable as true software components using "traits" mechanisms.

Uploaded by

RajeshGupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Proceedings of the 1999 Particle Accelerator Conference, New York, 1999

OBJECT ORIENTED C++ SOFTWARE COMPONENTS FOR


ACCELERATOR DESIGN*

D. L. Bruhwiler,# J. R. Cary,+ and S. G. Shasharina, Tech-X Corporation, Boulder, CO

Abstract classes to support the same interface but fulfill requests


differently at run time. This makes extensibility possible
Object oriented programming techniques make it possi- – a new class that implements a new algorithm can in-
ble for accelerator designers to independently develop herit from an existing class and be used in its place.
C++ software components that can work together. As an C++ is the best available OOP language for scientific ap-
example of this approach, we discuss some of the soft- plications. The “expression template” technique [11-13]
ware components being developed at Tech-X Corpora- allows C++ code to perform as well as optimized For-
tion, including: TxSTD, a library of standard utilities [1]; tran77 in vector loop comparisons. Furthermore, the use
TxID, a library of data-holding and nonlinear dynamics of "generic programming" and "template meta-program-
classes [2]; TxAC, an accelerator modeling class library ming" methods has yielded C++ linear algebra solvers
[3]; an X/Motif library used in the MAPA application [4] that are faster than Fortran77. [14] The base of numeri-
for interactive visualisation of dynamical systems such cal libraries for C++ is rapidly increasing, [15] and, be-
as particle accelerators; TxAN, a library of analysis and cause C++ is a superset of the well-established C lan-
simulation classes relevant to dynamical systems; and guage, it has access to a great wealth of legacy C code.
the LION++ nonlinear optimization library [5].
2 PUTTING THE PIECES TOGETHER
1 OBJECT-ORIENTED PROGRAMMING
Leo Michelotti developed the first C++ class library for
The computer software industry has been rapidly moving modeling beam dynamics in an accelerator [16] and de-
towards the use of object oriented programming (OOP) serves credit for introducing the accelerator physics com-
[Ref.'s 6-10]. OOP provides a superior mechanism for munity to the benefits of C++ and object oriented pro-
rapid development and testing, as well as ease of mainte- gramming. Other C++ codes are now under
nance and extensibility for large scientific codes. development, including MAD-9 [17], LEGO [18],
OOP consists of designing classes, where a class is a for- Teapot++ and MAPA [4,19]. Unfortunately, these
mat for holding and interacting with data. An object is a codes are independently developed, and the C++
particular instance of a class, just as 3.1 is a particular libraries used in one code cannot be used in the others.
instance of a real number. Each object has its own data, The goal of the CLASSIC project [20] was to standard-
while all objects of a given class share the same func- ize the structure and interface of a C++ accelerator dy-
tions (called methods). A program is constructed by first namics library in order to promote the sharing and reuse
defining the classes. The program instantiates objects of code among the various developers. We propose an
corresponding to the classes and, by calling the public alternative approach – the use of template based “traits”
methods of these objects, manipulates or displays the mechanisms [21-23] – to make C++ accelerator class
data. The public methods of a class define its interface. libraries interoperable and, hence, true software compo-
The three defining properties of OOP are encapsulation, nents. First, we briefly describe the class libraries under
inheritance and polymorphism. Encapsulation refers to development at Tech-X Corporation, then we elaborate
the fact that objects are accessed only through a public further on the use of traits.
interface, while their internal data and implementations
remain hidden. This feature ensures that the code is safe
3 ACCELERATOR DYNAMICS LIBRARY
from unwanted modifications. Inheritance allows the
programmer to define new classes that inherit most of The Tech-X accelerator dynamics library TxAC, now at
their coding from existing classes, only modifying or the first alpha release, has been used to successfully
adding data and methods as needed. This allows for model the Advanced Light Source, finding correct values
flexibility to increase the capability of an application in for tunes and dynamic apertures. TxAC includes an SIF
ways not foreseen by the original programmers, without parser (Standard Interchange Format, the MAD-8 input
rewriting the code. Polymorphism allows different language [24]) that can create an accelerator model
_______________________ (beamlines, elements, and properties of the charged par-
* Work supported by Tech-X Corporation and by the U.S. Department ticle) with full support for mathematical formulas.
of Energy, grant no. DE-FG03-96ER82292. TxAC comes with a number of built-in element types
# Email: [email protected] (e.g. quadrupoles, dipoles, thin RF cavities) which use
+ Also, University of Colorado Physics Department, Boulder, CO analytical models for the electric and magnetic fields.
Each element type knows how to calculate the phase

0-7803-5573-3/99/$10.00@1999 IEEE. 369


Proceedings of the 1999 Particle Accelerator Conference, New York, 1999

space map for particles passing through it, so the code while any parameters relevant to the simulation are
can be used for tracking, and it allows the user to specify stored in the TxDataSet class of TxID. The X/Motif
whether tracking should use 2, 4, or 6 dimensions. [19] GUI can extract data from these classes, thus allowing
TxAC also includes a beamline class, allowing the user the user interactive control over the simulation with im-
to define an arbitrarily deep hierarchy of beamlines mediate rendering of the resulting data.
within beamlines -- critical for describing large rings,
which may consist of only a dozen or so unique elements 5 GRAPHICAL USER INTERFACE
that are repeated in various combinations for a total of
The X/Motif GUI supports on-line tracking and renders
hundreds or thousands. Synchrotron radiation effects are
the resulting surface-of-section (SOS) plots. Initial con-
optional.
ditions for the next particle can be specified simply by
The TxAC class hierarchy is shown in Fig. 1. A new
clicking in the windows. These SOS plots can be resized
element type can derive from the most appropriate part
interactively, and the phase space variables can be paired
of this hierarchy and thus obtain most of the needed
up in any order. Fixed points can also be found.
features. New features can then be added in the derived
The GUI also allows one to browse the local file system
C++ class, which must also provide the code that im-
for an appropriate input file, and then to save changes in
plements tracking through the element. Once a new
the same file or in a new file. The GUI can plot a sche-
element class has been defined, it need only be regis-
matic layout of the accelerator, warning the user if, for
tered with a name in the TxacElements file and, upon
example, a ring does not close on itself.
recompilation, the new element type will be supported
The GUI provides appropriate windows for interactively
by the parser and the graphical user interface (GUI).
changing any relevant parameters for the accelerator, for
individual elements or for the analyses. For each type of
TxID TxMap
analysis, the GUI provides a menu item allowing the
user to activate the simulation, and then renders line
GUI TxDataSet TxTransMap
plots of the resulting data. Developers can define new
TxDataTransMap
element classes or analysis classes through inheritance,
TxDataAndMap
and these new types will be directly supported by the
ElemGeometry ElementImg
Accelerator GUI after compiling the new classes and relinking the
application.
Element SimpElemImg BeamlineImg

Drift ThinElement Steppable Beamline FlatElement


6 LION++ OPTIMIZATION LIBRARY
Solenoid
ElSeparator
LION++ [5] is a suite of flexible and extensible C++
Multipole Quadrupole
Cavity Sector Bend
TxacElements software components for numerical computing. Still be-
Kicker ThkMultipole ing actively developed, the present release features
Monitor
TxOptSlv, a library for the optimization of nonlinear
user-specified functions, and TxBase, a library of unary
Figure 1: Schematic showing TxID object hierarchy (upper
right), the TxAC object hierarchy (bottom) and one example functors and other general utilities. LION++ takes full
of interface with the X/Motif graphical user interface (upper advantage of sophisticated templating techniques and
left). object oriented design in order to provide users with
maximum flexibility in the choice of argument type and
4 ACCELERATOR ANALYSIS LIBRARY return type for the merit function that needs to be opti-
mized and in the configuration of options for the built-in
The accelerator analysis library TxAN uses TxAC to cal-
algorithms.
culate matched Twiss parameters (or the linear and 2nd-
Three multidimensional algorithms have been imple-
order dispersion) for any closed orbit or to propagate
mented, including nonlinear simplex and Powell, which
specified initial values element by element through a
do not need the gradient of the function, and one due to
beamline. TxAN can also calculate the position and ori-
Fletcher, Reeves, Polak and Rebiere (FRPR), which
entation of all beamline elements in a global Cartesian
does require access to the gradient. The Powell and
coordinate system, or use Monte Carlo methods to
FRPR algorithms require access to 1-D line optimization
propagate the RMS moments of a particle distribution.
algorithms. Three 1-D algorithms have been imple-
The classes of TxAN inherit from the data holding
mented, including golden section and Brent, which do
classes of TxID and define a new data holding class for
not need the function derivative, and a modified secant
storing the analysis results. Developers can define a new
algorithm, which does require access to the derivative.
C++ class for accelerator simulation, inheriting appro-
At present, all of the optimization algorithms are uncon-
priately from the TxAN hierarchy. The data resulting
strained although we are currently implementing algo-
from the new simulation is stored in the plot data class,
rithms that constrain the arguments in various ways. We

370
Proceedings of the 1999 Particle Accelerator Conference, New York, 1999

are also now implementing the Levenberg-Marquardt al- accelerator class library, allowing for direct model inter-
gorithm, which is effective for nonlinear least squares comparisons.
fitting and, more generally, for simultaneous optimiza- There are two prices to be paid for this approach. The
tion of many nonlinear functions. LION++ is readily first is that such heavy use of templates can lead to very
extensible so developers can implement their favorite long compilation times. The second is that the use of
algorithms or create a thin interface to other C and C++ traits requires the use of rather awkward syntax in the
algorithms, all with the same convenient user interface. source code for the accelerator class library.

7 THE TRAITS MECHANISM 8 CONCLUSIONS


“Traits” are defined through a template class or struct. A “container free” approach to the development of C++
For example, Fig. 2 shows the definition of two traits for class libraries for the modeling and design of accelera-
a 1-D array class: the type of the argument held by the tors is an elegant and relatively straightforward way to
array and a public method Resize() that will change the make the many existing class libraries interoperable.
length of the array. It is assumed that the 1-D array sup- This approach places some burdens on the library devel-
ports the [] operator for accessing the elements. The oper, but it avoids the difficulty of trying to convince
TxArrayWrap class in Fig. 2 wraps a simple C-style developers that they should base their code on some
pointer, which does not have built-in resizing capability. standard class library design.
In LION++, the argument list for multidimensional func- The authors thank Julian Cummings for discussions re-
tions is declared to be of type VecType, and all optimi- garding the use of traits and John Verboncoeur for
zation classes are templated over VecType. Users must discussions regarding object-oriented programming.
instantiate a templated optimization object, where the
template parameter specifies the argument type of the 9 REFERENCES
function to be optimized. Thus, LION++ is “container-
free" as defined in Ref. [23]. [1] TxSTD page, URL http://www.techxhome.com/freestuff/txstd
VecType
[2] TxID page, URL http://www.techxhome.com/freestuff/txid
struct TxUnaryContainerTraits<std::vector<ArgType>>
struct TxUnaryContainerTraits
typedef ArgType ValueType;
[3] TxAC page, URL http://www.techxhome.com/freestuff/txac
static void Resize(std::vector<ArgType>& vec, int dim) { [4] MAPA page, URL http://www.techxhome.com/products/mapa
vec.resize(dim); }
[5] LION++ page, URL http://www.techxhome.com/products/lion
struct TxUnaryContainerTraits<TxArrayWrap<ArgType>> [6] G. Booch, “Object Oriented Development,” IEEE Transactions on
typedef ArgType ValueType; Software Engineering 12, 211 (1986).
static void Resize(TxArrayWrap<ArgType>& array, int dim) {
array.Resize(dim); } [7] B. Stroustrup, The C++ Programming Language, Third Ed.
(Addison-Wesley, Reading, Massachusetts, 1998).
VecType, RetType
struct TxBinaryContainerTraits<std::vector<ArgType>, RetType> [8] S. B. Lippman and J. Lajoie, C++ Primer, Third Ed. (Addison-
struct TxBinaryContainerTraits
typedef typename TxBinaryNumberTraits<ArgType, RetType> ::
PromoteType PromoteType;
Wesley, Reading, Massachusetts, 1998).
typedef std::vector<PromoteType> PromoteVectorType; [9] E. Gamma, R. Helm et. al, Design Patterns (Addison-Wesley,
Reading, Massachusetts, 1995).
Figure 2: Unary (above) and binary (below) traits for [10] A. Elins, Principles of Object Oriented Software Development
containers (user-defined 1-D arrays). (Addison-Wesley, Reading, Massachusetts, 1994).
[11] A. D. Robison, "C++ Gets Faster for Scientific Computing",
The method for tracking in an accelerator library (called Computers in Physics 10, 458 (1996).
[12] T. Veldhuizen, "Expression Templates," C++ Report 7 (1995).
Advance() in TxAC) could be templated over the type of
[13] S. Haney, "Beating the Abstraction Penalty in C++ Using Expres-
the class that is to be tracked. Then the traits mechanism sion Templates", Computers in Physics 10, 552 (1996).
could be used to allow this class to be any of: a) a single [14] The Matrix Template Library (MTL) home page at URL
particle of precision float, double, etc.; b) a 1-D array of http://www.lsc.nd.edu/research/mtl/
particles, with the array type completely arbitrary; c) a [15] URL http://monet.uwaterloo.ca/blitz/oon.html#libraries
[16] L. Michelotti, “MXYZPTLK and Beamline: C++ Objects for
generalized user-defined particle class; or d) any C++ Beam Physics,” AIP Conf. Proc. 255 (Corpus Christi, 1992).
differential algebra (DA) library. Thus, the traits mecha- [17] MAD-9 web site, URL http://wwwslap.cern.ch/~fci/mad/mad9
nism immediately allows for some interchange of soft- [18] Y. Cai, M. Donald, J. Irwin, Y. Yan, “Lego: A Modular Accel-
ware components between different C++ code develop- erator Designer Code,”SLAC-7642, August 1997.
[19] D. L. Bruhwiler, J. R. Cary and S. G. Shasharina., Proc. Sixth
ers (particle classes and DA libraries).
European Particle Accelerator Conf., (Stockholm, June, 1998).
Taking this idea a step further, one could template the [20] CLASSIC web site, URL http://wwwslap.cern.ch/classic
controlling class of the accelerator dynamics library [21] N. C. Meyers, “Traits: a New and Useful Template Technique,”
(called Accelerator in TxAC) over the element type. In C++ Report 7 (1995); URL http://www.cantrip.org/traits.html
this case, the “traits” would include methods for reading [22] T. Veldhuizen, “Using C++ Trait Classes for Scientific Comput-
ing;” URL http://monet.uwaterloo.ca/~tveldhui/papers/traits.html
and writing the physical data associated with an element, [23] G. Furnish, “Container-Free Numerical Algorithms in C++,”
tracking a particle (or array, or DA vector) through an Computers in Physics 12 (3) (May, 1998).
element, etc. With this approach, the developer of one [24] MAD-8 web site, URL http://wwwslap.cern.ch/~fci/mad/mad8
class library could use the element classes from another

371

You might also like