In order to meet these ambitious goals, it was decided that McStas should be based on its own meta-language (also known as domain-specific language), specially designed for simulating neutron scattering instruments. Simulations are written in this language by the user, and the McStas compiler automatically translates them into efficient simulation programs written in ISO-C.
In realizing the design of McStas, the task was separated into four conceptual layers, listed below and also detailed in Figure 3.1
Graphical user interface and scripting layer, presentation of the calculations, graphical or otherwise. (aka. the tool layer).
Modeling of the overall instrument geometry, mainly consisting of the type and position of the individual components.
Modeling the physical processes of neutron scattering, i.e. the calculation of the fate of a neutron that passes through the individual components of the instrument (absorption, scattering at a particular angle, etc.)
Accurate calculation, using Monte Carlo techniques, of instrument properties such as resolution function from the result of ray-tracing of a large number of neutrons. This includes estimating the accuracy of the calculation.
If you don’t want to read this manual in full, go directly to the brief introduction in chapter 3.2.
Though obviously interrelated, these four layers can be treated independently, and this is reflected in the overall system architecture of McStas. 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 rotating velocity selector, and expect other users to easily benefit from his/her work, and so on. McStas 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 instruments 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 McStas internals to write new component or instrument definitions, and even if those internals change in later versions of McStas, existing definitions can be used without modification.
The McStas system also utilizes the meta-language to let the McStas 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 instrument 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 neutron trajectories, and possibly other things in the future that were not even considered when the original instrument specification was written.
The design of McStas 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 2 mm from the center of the axis” and so on are easy to answer. Within minutes the instrument definition can be modified and a new simulation program generated. It also makes it simple to debug new components. A test instrument definition may be written containing a neutron 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 McStas system is based on ISO-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.