Takin: An open-source software for experiment planning, visualisation, and data analysis

Due to the instrument's non-trivial resolution function, measurements on triple-axis spectrometers require extra care from the experimenter in order to obtain optimal results and to avoid unwanted spurious artefacts. We present a free and open-source software system that aims to ease many of the tasks encountered during the planning phase, in the execution and in data treatment of experiments performed on neutron triple-axis spectrometers. The software is currently in use and has been successfully tested at the MLZ, but can be configured to work with other triple-axis instruments and instrument control systems.


Motivation and significance
The invention of the neutron triple-axis spectrometer by Brockhouse [2] has entailed a huge boost in the understanding of dynamics in solid matter. Today, triple-axis spectroscopy has become a standard tool in the investigation of materials in such diverse fields as magnetism, lattice dynamics, superconductivity, and critical phenomena.
A small selection of very recent examples include an investigation of the interactions governing the nematic order of iron-based superconductors [3], helimagnetic band formation in the chiral magnet MnSi [4], and the behaviour of the helimagnetic bands at the conical-ferromagnetic phase transition [5]. But despite their ubiquity in neutron physics, the triple-axis spectrometer is often poorly understood by users in terms of its very complicated resolution function. An understanding of the instrumental resolution is crucial for a correct planning of measurements and data treatment, as resolution effects often lead to artefacts in the spectra -called "spurions" -which cannot be easily distinguished from genuine excitations.
In practice, resolution analysis has led to isolated per-instrument software applications which are often either very complicated to use, too specialised, or lacking the required features (e.g. [6,7,8]). In this article we present the free and open-source software system Takin, which is capable of performing all the required steps in one package and which is general enough to be easily adaptable to new triple-axis instruments. The software furthermore features a fully-integrated graphical user interface (GUI) and is therefore much more accessible than previous command-line tools.

Software description
Takin is a software system encompassing multiple goals. Its basic feature, namely a quick visualisation of both the crystal lattice in real and in reciprocal space and the instrument configuration, is similar to the vTAS software [8]. But whereas vTAS is mainly focused on simulating realistic instrumental limits, e.g. walls in the instrument space, Takin does not include such features, but instead focuses on combining crystallographic information and instrument visualisation with a special emphasis on resolution calculation. Moreover, a further emphasis of the software is data treatment and resolution deconvolution using the Monte-Carlo approach.

Software Functionalities
The main window of the program is divided into two views: a reciprocal and a real-space representation. The reciprocal space includes the Bragg reflections that are allowed by the space group's symmetry operations as well as the elastic structure factors S hkl = |F hkl | 2 for each peak. Given the allowed peaks, the first Brillouin zone is calculated and displayed. Furthermore, the scattering triangle is drawn in the reciprocal space and can be either moved by mouse or imported from scan files (the supported formats include the ones used by MLZ, PSI, ILL and NIST). Alternatively, the software can receive the current live instrument position by a network connection to the triple-axis control software. With respect to network connections, two instrument control systems are currently supported: NICOS [9], the networked instrument control system which is used at the MLZ, and SICS [10], the SINQ instrument control software used at the PSI. Moreover, the object-oriented network architecture in Takin allows for an easy integration of further triple-axis control systems. The second part of the screen is reserved for the real-space view, which shows the real crystal lattice and the Wigner-Seitz cell. The unit cell and its atom positions, which are calculated by the given space group symmetry operations, are shown in a projected representation in this view. As with the reciprocal view, the displayed crystal plane can be freely chosen.
The software warns if the spectrometer -either the real or the simulated one -is in a spurious scattering configuration. To that end Takin includes a module to check for the most important false peak conditions as given by Shirane et al. [11,Ch. 6]. These include the Currat-Axe spurion which -for example -appears if q is parallel to k f and |k f + q| = |k i |. In this configuration the analyser will Bragg-scatter an elastic incoherent signal from the sample which subsequently appears as a faux-inelastic peak. Other spurious signals that are checked for include the higher-order reflections from the monochromator and the analyser and the Bragg tail. The latter appears in the q direction transverse to the lattice vector G and results from the slop- ing of the resolution ellipse with respect to the direction perpendicular to the mean Q and E position. Furthermore, a module for calculating powder peaks is included for a quick assessment of spurious powder scattering from the sample environment, for instance from aluminium or copper. Please note that more specific spurious signals -such as thermal diffuse scattering by the analyser -are not considered in the software.
The entire software package and its associated library [12] are written in modern C++11 [13] and make extensive usage of the Boost [14] C++ template library. The graphical user interface is based on Qt [15] and the Qwt [16] plotting library. Tables with physical information, especially the properties of the 230 space group types and the scattering lengths, are obtained from the Clipper [17] library and online from the NIST tables [18], respectively. We use the magnetic form factors from the ILL tables [19].

Resolution
Resolution Calculation. The basic resolution calculation module features a C++ re-implementation of the Cooper-Nathans [20] and Popovici [21] algorithms from Rescal 5 [7]. Furthermore, a novel implementation of the Eckold-Sobolev approach to the resolution function [22] is available. The latter algorithm is especially interesting for a realistic treatment of the common feature of monochromator and analyser focusing, which increases the neutron flux, but also increases the resolution volume. Each algorithm calculates the covariance matrix for the wavevector Q and energy transfer E of the neutron at a specific instrument position. The inverse of the covariance matrix is the instrumental resolution matrix R -a quadric describing a fourdimensional ellipsoid. The angles of the ellipsoid with respect to the Q and E axes and the widths with respect to the ellipsoid axes are determined using the principal axis theorem which involves solving the eigenvector problem. The software uses a specialised, fast implementation for this reduced eigenvector calculation involving symmetrical matrices. The result is displayed (see figure 2) using two-and three-dimensional projections of the ellipsoid in either the Q , Q ⊥ , Q z , E or the fractional h, k, l crystal coordinate system.
The native C++ implementation of the algorithms is fast enough for a real-time updating and displaying of the resolution function even on low-end hardware. On multi-core or multi-processor systems further performance is gained by taking advantage of the symmetric treatment of the monochromator and the analyser resolution volumes in the Eckold-Sobolev method which allows for an efficient parallelisation of the calculations.
Resolution Convolution. Measurements using a triple-axis spectrometer do not yield the actual dynamical structure factor S (Q, ω) (where ω = E/ ), but instead give a convolution of S with the instrumental resolution R. Using an externally supplied theoretical S (Q, ω) function, an experiment can thus be simulated, e.g. for planning future measurements, given the R matrix.
Takin contains a module for Monte-Carlo simulation of the convolution integral (see figure 3). The Monte-Carlo approach includes generating a predefined number of neutrons inside the four-dimensional resolution ellipsoid, followed by sampling S (Q, ω) with the Q and E coordinates of the Monte-Carlo neutrons.
The S (Q, ω) function for the excitations to be simulated can be supplied as either a table or as a Python [23] script or -in case performance is crucial -as a native C++ class which has to be derived from a special interface. For tabulated S (Q, ω) values, the values from the table are used to generate a four-dimensional binary search tree (a k-d tree [24]) which can then be very efficiently used to query nearest positions by the Monte-Carlo simulation. The Monte-Carlo simulation itself is sped up by making use of all available processor cores for all model sources except the Python scripts (the latter being due to threading limitations in the Python interpreter).
Resolution Convolution Fit. Free parameters can be defined in the different theoretical S (Q, ω) models. In the Python model -for instance -all global variables are automatically determined upon loading the script and offered as fit parameters in the convolution fitter tool. User-selected free parameters are used to perform a χ 2 minimisation in order to get the optimal fit of the resolution convolution to experimental measurements. For performing the minimisation, the Minuit software library [25] is used.
Due to its complexity, the fitter program module is currently only accessible via a command-line interface and takes a special job file format as input. In order to be in unison with the otherwise fully GUI-based design of the software a fully graphical convolution fitter is planned for the near future.

Illustrative Example
As Takin contains many different triple-axis features, we restrict ourselves to showing one specific functionality here: planning an experiment using a simulated scan, which can be fully and quickly done in the GUI. In the convolution dialog (figure 3, upper panel), a file describing the crystal and the instrument geometry have to be given along with a model file containing the dynamical structure factor S (Q, ω). The crystal and instrument files are defined beforehand in the Takin main view (figure 1).
As described earlier, the S (Q, ω) model file can be either a C++ class, a table or a Python script. In the present example, we use the chiral magnet MnSi as a sample, MIRA [26,27] as the instrument and the helimagnon model from [4] as a theoretical S (Q, ω) function. An arbitrary scan path in (Q, E) space can next be defined and simulated. Here, we simulate an energy scan at Q = (1.043 0.957 0) rlu using a fixed incident wavenumber, The resulting plot (figure 3, lower panel) shows that scans at this k i are viable on MIRA as four individual helimagnon bands can be resolved for E < 0, which is the focusing direction for the defined positions. For the defocused measurement positions at E > 0 the simulation shows that the bands cannot be resolved anymore and instead only one broad, smeared-out peak will be discernible.
In a further step, Takin can be used for convolution fits to experimentally measured data. We can confirm an excellent agreement of the calculated resolution functions using a focusing monochromator and measurements of the helimagnon bands in MnSi [5] as depicted in figure 4.

Impact
With Takin we hope to ease the life of triple-axis users in all aspects of experiment planning, execution and data analysis. In the planning phase it can be used to quickly and graphically assess if the experiment is possible on the given instrument: for example if the angles are in the allowed range; whether the scattering triangle is closed; or the resolution is good enough.  [4]. Four helimagnon bands can be resolved in the focusing direction, which for ki = const. and the given Q is at E < 0. In the non-focusing direction at E > 0 the bands cannot be resolved and appear as one broad peak. The incoherent elastic contribution is not visible. total conv. Exp. Figure 4: An example convolution fit to experimental data [5]. The data points were measured on MIRA [26,27], the lines are obtained from the Takin convolution fitter using the model given in [4]. The momentum transfer q of this data set differs from the one shown in figure 3.
During the experiment, the software shows the live position of the instrument in reciprocal space, draws the corresponding resolution function at this position and warns of possible spurions. After the experiment, Takin can be used "offline" to do convolution simulations and convolution fits to the measured spectra providing a choice between different resolution algorithms.
The latest version of the software is currently in regular use at the MLZ instruments MIRA [26,27] and PANDA [28]. Moreover, the software has been successfully tested to work at the SINQ instrument TASP [29] in "live mode" during a magnon measurement.
The first comprehensive applications for the resolution fitting capabilities of Takin will be presented in two upcoming papers by the authors. The first one is concerned with the evolution of the helimagnon band structure in the vicinity of the conical-ferromagnetic phase transition of MnSi [5], the second one examines an anomalous phonon behaviour at the cubic-tetragonal transition in the spinel vanadate MgV 2 O 4 [30].

Conclusion
We presented a software system for performing calculations and visualisations of inelastic neutron scattering with a special emphasis on the resolution function of triple-axis spectrometers.
For future developments, it is planned to offer general support for timeof-flight spectrometers. Currently, the software already contains a module for the resolution calculation and convolution using the Violini method [31].
Furthermore the convolution fitter will be equipped with advanced scripting capabilities and also included in the main GUI.

Introduction
Several updates to the free and open-source software (FOSS) Takin [2, 3] have been made over the course of the last year. Apart from general performance optimisations and improvements of the existing programme modules, new features have been introduced. Of these features, we present two selected ones in the present short paper.

3D Brillouin zones
The previous version of our software included code to calculate a twodimensional (2D) slice of the first Brillouin zone relevant for the selected scattering plane. For complexity and performance reasons, the code used for the task remained purely 2D. While this gives reasonable results in most cases, it is only an approximation.
For the present version, we created a new programme module which calculates the full three-dimensional (3D) Brillouin zone from the single-crystal parameters and the space group on-the-fly. The code is highly optimised and has only a very slight performance penalty when compared to the old 2D version. The Brillouin zone can now either be visualised in the software using OpenGL [4] or be exported to the open X3D file format. The X3D export allows for processing the data in external applications, for instance the FOSS programme Blender [5]. Furthermore, 2D cuts of the 3D Brillouin zones are generated using the selected scattering plane. These cuts are presented in the main reciprocal space display, replacing the old pure-2D code. An example for these calculations is shown in Fig. 1.

Scripting interfaces
In its resolution convolution code, Takin uses externally supplied scripts for the calculation of the dynamical structure factor S (Q, E). In addition to Python [6], we now support the novel scripting language Julia [7] for this task. As Julia is just-in-time compiled, it considerably speeds up the calculation of the convolution integral.
In order to circumvent threading limitations in the Python interpreter and for stability reasons, both the Python and Julia scripting interfaces are now separated from the main programme. S (Q, E) script modules are now spawned in their own process space and communicate with Takin using a shared-memory messaging system.