Abstract
We present an overview of, and an introduction to, the general-purpose neutron simulation package McStas. We present the basic principles behind Monte Carlo ray-tracing simulations of neutrons performed in the package and present a few simple examples. We present the implementation of McStas, the status of the package and its use in the neutron community. Finally, we briefly discuss the planned development of the package.
Introduction
Over the last decades, Monte Carlo ray-tracing simulation has become increasingly important for investigating the performance of neutron scattering instrumentation. Few neutron instruments are being built before undergoing a thorough investigation by simulation. In particular, the powerful spallation neutron sources in North America (SNS) [54] and Japan (J-PARC MLF) [49], the upgrade of the UK source (ISIS TS-2) [48], and the coming European Spallation Source (ESS) [47] have heavily utilized neutron simulation software, thereby also pushing the development of the simulation methods.
At the end of last century, neutron instrumentation ray-tracing simulations were performed using home-made monolithic codes. While fairly successful, this approach suffered from limited development ressources – with potential problems of quality assurance – while still creating a vast amount of duplicated effort between different projects and facilities. One exception from the monolithic approach was the general-purpose neutron simulation package NISP [7,52,57] released 1994/95, based on the earlier
Almost simultanously, at the turn of the century, four general simulation packages were launched, following the NISP philosophy, but on more modern software platforms: Most of these packages are still maintained (to a higher or lower degree) and the status of their projects can be found on their respective homepages [50,53,55].
In the view of the authors of this paper, the state of the art of currently available neutron Monte Carlo ray-tracing packages are summarized in Table 1, including points of view on differences and main advantages and disadvantages.
Overview of state of the art in Monte Carlo ray-tracing, with main advantages and disadvantages of the most widely used packages
Overview of state of the art in Monte Carlo ray-tracing, with main advantages and disadvantages of the most widely used packages
Based on the need for instrument developments, the early use of the software packages concentrated on simulating neutron optics, especially guide systems, and concepts for full instruments, including instrument resolutions.
A huge effort was performed in the beginning of this century with systematic comparison between simulation results of guides and optics in different packages and to some extent with actual measured data [58,69]. This work served strongly to track down tricky numerical errors, to increase confidence in the packages themselves, and to establish the ray-tracing technique as such.
With the aim of being able to simulate complete virtual experiments, the last decade has seen a growing demand for – and development of – a suite of scattering sample components.
This is the first article in a series of short reviews of the McStas package – with the planned future articles described in Section 6. For this reason, we will here review the general expressions used for Monte Carlo ray-tracing simulations of neutron scattering, In addition, we will discuss the implementation of the McStas system, its utilization and user community, and the planned future development of the package.
We have aimed this article for neutron scatterers and software developers alike, and we thus begin with a few introductory paragraphs on the nature of neutrons and on the ray-tracing technique. Most material presented here, and other related information, can be found also in the user and component manual for McStas [51].
Monte Carlo simulations are in general a way to perform approximate solutions to complex problems by use of random sampling, thereby performing
As explained in a wikipedia article [56], the thinking behind that numerical experiment can in fact be used to construct a Monte Carlo algorithm for the estimation of
As with all stochastic methods, Monte Carlo methods are prone to statistical variances. To reduce the statistical errors, a number of
The method relevant for neutron simulations is known as
The semiclassical representation of neutron coordinates
In neutron ray-tracing simulations a neutron is represented semiclassically by simultaneously well-defined position,
Formally, this approach violates the laws of quantum mechanics, in particular the Heisenberg uncertainty relations [28], given for pairs of conjugate position/momentum variables by
The validity of the semiclassical approach for neutron scattering is also discussed in detail in Ref. [30]. The similar issues with the semiclassical approximation of the neutron spin will be discussed in the future review on polarized scattering.

Neutron ray interacting with a reflecting surface: 1. The neutron begins with parameters
The physical unit of a neutron ray is neutron rate, or neutron intensity (s−1). On a typical neutron source, the emission rate can amount to 1015/s; an order of magnitude larger than the number of rays that can typically be simulated on a laptop in one year. For this reason, to simulate realistic values for neutron count numbers, a neutron ray in general represents more than a single physical neutron (per second). To accommodate this, each ray contains an additional parameter, the
As one example of importance sampling, the weight factor is manipulated through the simulation. For example, when some physical neutrons are “lost” due to
We proceed along this line of reasoning to calculate the neutron intensity in a beam. Consider a neutron component at a surface perpendicular to the beam. The neutron intensity is given by the sum of all simulated rays reaching this surface:
Often, there is only one non-trivial event to consider. This may,
When a Monte Carlo branch point is reached (selection between several events), we have
Neutron moderators and focusing
Consider a component that may send neutrons out in all directions,
Imagine a McStas moderator component, simulating a moderator that emits
Estimates of simulation uncertainty
In a simulation, like for real experiments, it is important to be able to estimate the statistical uncertainty. We here present a simple derivation of the uncertainty in simulations with weight factors.
Imagine
We now allow the rays to have different, but discrete, weight factors,
Now, let the simulations occur with rays of arbitrary weight factors,
Scaling to real-world statistics
In order to compare with real experiments, in particular when using simulated data as input to data analysis programs, simulated intensities must be converted to average integer counts using an imagined counting time,
Let us now we quantify the effect of scaling with a counting time. The average count number is easily obtained by
If a series of simulated values need to be scaled by the same time,
In conclusion, the count-error-true simulation numbers are
This transformation to integer count numbers is not used in McStas, but must be applied as a post processing of the data after the simulations. Further work is, however, needed on the sampling strategies for future more efficient transformation of errorbars.
The concept and implementation of McStas
As also outlined in the introduction, the philosophy and mindset behind starting the McStas project, as laid out by K.N. Clausen, was to a large extent to minimize duplication of code and efforts by allowing code to be shared [26]. The main ideas that were implemented since the first version of McStas were:
Licensing should follow Open Source standards. Since version 1.8 McStas is released under the GNU General Public License v. 2.0. Earlier versions were licensed under a McStas- and RISØ specific license that was open, but did not allow redestribution.
The code should be modular to allowed sharing models of instrument parts between users. This evolved into the so-called components, explained below.
The syntax of the user-supplied instrument definition should be simple and powerful. For the development of the syntax details, K. Nielsen got input to the syntax, using pen and paper before writing any code, from the RISØ staff, in particular K. Lefmann and H.M. Rønnow wrote the first instrument and handful of components in this way.
The software concepts should first of all make sense to instrument scientists, allowing them to work in a way that felt almost
Documentation should as far as possible be
Simplicity and readability of the code should be preferred over performance.
McStas is implemented in a three-layered structure. The
These levels of code are only modified by a handful of system specialists. The core system also manages the main Monte-Carlo loop that in turn produces a specified number of neutron rays for the simulation.
Supplementing the core system run-time codes, a number of c-files are present to support individual McStas components, such as
The
The

The figure illustrates how the McStas (and McXtrace) codes are layered, including how user, developers and code generation mechanism interact with the various layers of code. The geometrical lay-out of the instrument to simulate is shown at the top panel, while the bottom panes represent the different interacting layers of McStas. The instrument file is shown fully in the large panel to the left; the figure is not to scale. Reproduced from [65].
When McStas is executed on an instrument file, the system compiles the information from the instrument file and the component library and produces an ANSI-C file. This is in turn compiled by a standard C-compiler to an executable code, which is called by the system. After the process has terminated, McStas collects the resulting data files, stores them in specified directories, and visualises the data upon request. Everything can be controlled from a user-friendly GUI interface, or by a scripting language.
McStas has a users manual and a component manual [51], both of which are very comprehensive documents. A more readily accessible documentation may be found in the project home page. Documentation of component functionality is furthermore embedded in the header of the component code. This documentation is
In its first two decades, McStas has seen a steady increase of users. We have chosen to illustrate this with a literature search. We have analyzed the 350 articles citing one (or more) of the basic McStas release articles, and by far most of these articles in fact represent simulation work done by the use of McStas.
In Fig. 3, we show the number of McStas-using articles as a function of publication year. We see that after a slow start (except for a spike in 2002), the publication rate reached 10/y in 2006 and 20/y in 2011. Since 2014, the number has been approx. 30/y, which is partly based on the large instrumentation work related to ESS.

The annual number of articles citing McStas shown as a function of publication year.

The number of articles citing McStas, divided into use categories, shown as a function of 5-year intervals.
It is highly interesting to study what McStas has been used for, judging from this literature base. Figure 4 shows the six main use categories of the packages, distributed on 5-year periods. Although the overall use of McStas has increased by a factor 4 over the years, the distribution between the categories has remained remarkably constant. Let us present the themes one by one (all will be described in much more detail in later issues of this article series):
NIST (MACS) [35],
CARR (HIPD) [67]
ANSTO (TAIPAN) [8]
ILL (D7) [60]
ISIS (ENGIN-X) [37]
FRM-2 (KWS-2) [34]
HZB (FLEX) [22]
PSI (RITA-II) [24]
the Algier reactor the Argentine reactor CSNS Dhruva ESS HANARO Dubna LLB PIK
Most of the instrument simulation publications are related to the newest sources: ESS (24 papers), J-PARC (20 papers), and FRM-2 (13 papers), but also much activity is related to ILL (12 papers) and SNS (7 papers). Examples are too plenty to be given at this stage, but we will elaborate on the topic later in this review series.
As a separate task, McStas has been used in two demanding studies of the complete instrument suite for ESS, first to optimize the pulse structure [25], then to optimize the moderator geometry [2].
In addition to this bibliometric study, we have performed a survey of the McStas user community during the autumn of 2018. Ten questions were posed via the SurveyMonkey platform [42] in the period from November 6th 2018 to December 3rd 2018. All questions were voluntary/optional and contained the possibility of free-form input via an ‘other’ text field. In total 52 responses were received, out of which 47 filled demographic data. The full McStas user base is estimated to be approximately 400, based on the current 252 members of the McStas user mailinglist and knowing from experience that not all users register.
The main findings of the survey are that:
The McStas users are to a high degree running the most recent version of McStas, at the time of survey v. 2.4.1. Linux is the dominant operating system, followed by Windows 10 and macOS. Some users are now running the Linux binaries under Windows 10 via the Windows Subsystem for Linux [44]. McStas users are distributed over all of the continents that have access to neutron facilities. McStas users are mostly based at neutron facilities, but the package also finds use at universities and in industry. Most users are now routinely using our modernised Python based gui and command-line utitlites. The vast majority of McStas users are very happy about the McStas installation process, daily use and the available user support.
For those interested in the full details, a full report of the survey is published at the McStas website [43].
As our user survey demonstrates, McStas is as a project alive and healthy with a 20 year long history of development and a stable user community. What separates McStas from the alternative simulation packages is mainly the applied code-generation technique, the quality of documentation and the strongly collaborative nature of the package, allowing users to contribute. To stay alive and healthy however, a sustained effort is needed in terms of manpower, resources, support and development.
The main focal points for the development in the coming years are:
Lower the typical release cycle of 12–18 months to 3–6 months. Updating more often lowers ‘time to market’ for new ideas, solutions and bug-fixes. McStas 2.5 was released in December 2018 and 2.5.1 will be released in the spring of 2019. Make our test/validation effort more stringent and use more of a ‘continuous integration’ approach to testing. All components should have output test data that we can monitor against on a continuous basis. Today only a scalar numerical output defines a ‘test’ – in the future we would like to compare at a more detailed level of data in individual bins of individual monitors. Enrich the sample library even further. A natural layer of growth is the Union [5] concept by Mads Bertelsen which allows to separate geometry and physical processes by defining an A modernization of the code generation layer is under way, which will allow us to move to a The classical McStas code uses c-preprocessor [45] define instructions to locally change the meaning of a variable or parameter name. This means that e.g. a geometrical parameter symbol The modernized code-generation will ease the process of targeting modern GPU-based architectures.
The reader is further encouraged to consult our standard references [65] and [64] for a relatively recent and complete description of McStas.
This article should be seen as an introduction to a series of McStas review papers. Planned themes in this series cover the main uses of McStas described above:
Components Guide systems Instrument simulations and virtual experiments Modeling of scattering from samples Simulation of polarized neutrons, including spin precession
In addition to a review of these important cases, we also will describe a few more technical matters that deserve a more thorough presentation than what has been given in literature so far:
Is is our aim that this article series on McStas will serve to share knowledge of the package and its utilization, as well as enhance the capabilities for designing and modeling neutron instrumentation worldwide.
Footnotes
Acknowledgements
It is a pleasure to thank everyone involved in the McStas project over the decades. In chronological order: K.N. Clausen, K. Nielsen, H.M. Rønnow, E. Farhi, P.-O. Åstrand, K. Lieutenant, P. Christiansen, E.B. Knudsen, U. Filges, J. Garde, and M. Bertelsen.
