In order to meet these ambitious goals, it was decided that McXtrace should be based on its own meta-language, specially designed for simulating scattering beamlines. Simulations are written in this meta-language by the user, and the McXtrace compiler automatically translates them into efficient simulation programs written in ANSI-C.
In realizing the design of McXtrace, the task was separated into four conceptual layers:
Modeling the physical processes of scattering, i.e. the calculation of the fate of a photon that passes through the individual components of the beamline (absorption, scattering at a particular angle, etc.)
Modeling of the overall beamline geometry, mainly consisting of the type and position of the individual components.
Accurate calculation, using Monte Carlo techniques, of beamline properties such as resolution function from the result of ray-tracing of a large number of photons. This includes estimating the accuracy of the calculation.
Presentation of the calculations, graphical or otherwise.
Though obviously interrelated, these four layers can be treated independently, and this is reflected in the overall system architecture of McXtrace. The user will in many situations be interested in knowing the details only in some of the layers. For example, one user may merely look at some results prepared by others, without worrying about the details of the calculation. Another user may simulate a new instrument without having to reinvent the code for simulating the individual components in the instrument. A third user may write an intricate simulation of a complex component, e.g. a detailed description of a high resolution fast chopper, and expect other users to easily benefit from his/her work, and so on. McXtrace attempts to make it possible to work at any combination of layers in isolation by separating the layers as much as possible in the design of the system and in the meta-language in which simulations are written.
The usage of a special meta-language and an automatic compiler has several advantages over writing a big monolithic program or a set of library functions in C, Fortran, or another general-purpose programming language. The meta-language is more powerful; specifications are much simpler to write and easier to read when the syntax of the specification language reflects the problem domain. For example, the geometry of beamlines would be much more complex if it were specified in C code with static arrays and pointers. The compiler can also take care of the low-level details of interfacing the various parts of the specification with the underlying C implementation language and each other. This way, users do not need to know about McXtrace internals to write new component or beamline definitions, and even if those internals change in later versions of McXtrace, existing definitions can be used without modification.
The McXtrace system also utilizes the meta-language to let the McXtrace compiler generate as much code as possible automatically, letting the compiler handle some of the things that would otherwise be the task of the user/programmer. Correctness is improved by having a well-tested compiler generate code that would otherwise need to be specially written and debugged by the user for every beamline or component. Efficiency is also improved by letting the compiler optimize the generated code in ways that would be time-consuming or difficult for humans to do. Furthermore, the compiler can generate several different simulations from the same specification, for example to optimize the simulations in different ways, to generate a simulation that graphically displays x-ray trajectories, and possibly other things in the future that were not even considered when the original instrument specification was written.
The design of McXtrace makes it well suited for doing “what if…” types of simulations. Once an instrument has been defined, questions such as “what if a slit was inserted”, “what if a focusing monochromator was used instead of a flat one”, “what if the sample was offset 0.2 mm from the center of the axis” and so on are easy to answer. Within minutes the beamline definition can be modified and a new simulation program generated. It also makes it simple to debug new components. A test beamline definition may be written containing a source, the component to be tested, and whatever monitors are useful, and the component can be thoroughly tested before being used in a complex simulation with many different components.
The McXtrace system is based on ANSI-C, making it both efficient and portable. The meta-language allows the user to embed arbitrary C code in the specifications. Flexibility is thus ensured since the full power of the C language is available if needed.