|
|
|
The Computing for Science (CS) group supports ILL scientists, students and visitors in a number of activities including data analysis, instrument simulation and sample simulation.

McStas - TutorialMcStas - ILL- Tutorial for instrument and component design
AbstractThe aim of this page is to show you step by step how to describe and simulate an instrument or a component. The McStas graphic tools will also be presented. You can also look at the alternative tutorial made by the Risoe team. Refer to the McStas manual for more informations. Other tutorials:
A - Writing an Instrument simulationA-1: The instrument overviewWe here plan to write a simple but realistic desciption of the IN14 3-axis spectrometer (TAS) at ILL, and use it with a Vanadium sample. This instrument is basically composed of :
Of course, one can add some other components after (supermirror guide, sollers, slits, other monitors, etc...). A-2: Where to reach the McStas softwareThe McStas package has been installed at ILL on most Linux and SGI (Silicon Graphics-Unix) machines (such as mica) thanks to RG, as well as the required perl/pgplot extensions for design and results visualization. Scilab and Matlab viewers are also available. Of course, you can also install McStas on your own computer, but try anyway to maintain a unique component library, for portability. Note: If you use McStas for the first time on an ILL Silicon Graphics Machine (SGI/IRIX) such as orus, mica, pandora, lotus or qaz, you have to install some environement variables for Perl and Pgplot. Refer to this matter in the installation guide. So, just login on a machine and open a terminal/console window xterm for instance From there, you need to create a text file where to describe the instrument ('in14_tut.instr' for instance); open a text editor (I recommand Nedit 5.0) nedit in14_tut.instr or an equivalent Unix : syntax highlighting for McStas files
You can also use the McStas GUI instrument editor by typing mcgui <instr_name.instr> The McStas manual may be accessed within mcgui/Help menu, or with the 'mcdoc --manual' command. Additionally, the 'mcdoc --tools' and 'mcdoc --show' will list the McStas tools, and open the Component Library help respectively. The Component manual is also available frokm there. You can then begin to type in the instrument definition file, or open an already existing one (select File Menu / Open Instrument/Edit).
A-3: Let's start !!Before continuing, we should wonder what parameters will be required by the instrument. In the example that I provide,, I choosed to enter parameters
These will be asked by the instrument simulation at computation start. You can use as many numerical parameters as you need (max is 1000 !). Character string parameters are also permitted (if you set their type to 'string' or 'char *'). The parameter values usually define the instrumental geometry and physical parameters. They may be used and modified during the simulation. Globally, an instrument is described following the general template below. We are now going to study the IN14 instrument described in the overview. DEFINE INSTRUMENT IN14(Ki,Q,E) followed by comments between '/*' and '*/' to identify author, date, instrument, parameters, usage, and a typical example. DEFINE INSTRUMENT IN14(Ki=2.662,Q=2,E=0) A-4: Describing the instrument structureEach part of the instrument is indicated as a component, positioned and rotated with specified values. So, you first need to look at the component library to search if a given component already exists. If you can't find it, then you may need to write one corresponding to your needs (as we shall see further in this tutorial). In order to build the instrument simulation, McStas first looks for components in the current instrument location, and then refers to the common component library (e.g. /usr/local/lib/mcstas on Unix machines) The description of the instrument is a sequential listing into the TRACE section. This means that you should not put two 'active' components exactly at the same place, overlap or include one into an other. Usually, you can separate them by say 1 mm if you really want to put them at 'nearly' the same place. Monitors can be positioned at the same place, as they usually do not affect the neutron beam. Anyway, you may build component groups. Look into the McStas manual for more. Thus, we can write the skeleton of the instrument (following the informations given in the overview). The components have a generic name (from the McStas library, for instance Source_flux) and a specific name identifier, only valid for your simulation (for instance source). TRACE COMPONENT cold_guide = Guide() /* guide between source and mono */ COMPONENT mono_craddle = Arm() /* this is the monochromator craddle */ COMPONENT mono = Monochromator() /* Monochromator */ COMPONENT out_mono = Arm() /* this is the mono-sample axis */ COMPONENT mono_soller = Soller() /* a Soller collimator */ COMPONENT focus_sample = Arm() /* this is the sample craddle */ COMPONENT sample = V_sample() /* Sample */ COMPONENT out_sample = Arm() /* this is the sample-ana axis */ COMPONENT focus_ana = Arm() /* this is the analyzer craddle */ COMPONENT ana = Monochromator() /* Analyzer */ COMPONENT out_ana = Arm() /* this is the ana-det axis */ COMPONENT focus_det = Arm() /* this is the detector craddle */ COMPONENT Detector = Monitor() /* detector */
Rotations are around those axis, after the AT translation is performed. These operations are usually made in a given reference coordinate system (precised with the RELATIVE keyword, followed by one of your instrument component-identifier, such as ana or focus_det). As you can see in this skeleton, we introduced the angles A1 to A6 and the distances L1 to L4 in order to position the various components. These values must be defined somewhere. This will be explained in the computations and internal variables section below.
Monochromator(<my monochromator parameters=my values> , ...) Thus when you need to use a specific component, you should first look at it's documentation or source code header, in order to look at the required parameters (in future versions, components will also have some default value parameters if not provided by user). Here is a component parameters example for the source :
The component name of the parameter (left part of the =, for instace radius) is fixed by the component definition. It can not be changed in your instrument. In fact, we shall use a simple model of flat energy flux distribution for the source, flat monochromator and analyzer, and a single detector at the end. The Vanadium sample scattering is 'focussed' towards the analyzer. No need to scatter somewhere else, and loose neutrons ! Note: when using McGUI, the filenames (e.g. for monitors) should be specified between double quotes ("). A-5: Internal variables and computations (embeded C code)Defining some component parameters or variables internal to the instrument simulation is done in two steps. If you need to use such computations (mathematical expressions), you must compute them with embeded C code before calling the component. Obviously, computing some paranmeters needed to position, rotate and describe components is an essential point of the simulation. This is done in the DECLARE and INITIALIZE sections, before the TRACE keyword.
The computation of these values is done in two steps. In the DECLARE section, which is optional if you don't use variables in your instrument (see the in14_tut.instr file), you indicate between symbols '%{' and '}%' what C variables you are going to use.
You can assign a default value in the C declaration section. The variables declared in this section are global. You can use them in the whole instrument (INITIALIZE, TRACE and FINALLY sections) as shown below. In the case of string arrays (character strings), you may need to dynamically allocate some memory with the malloc C function. Do not forget to free the allocated memory when it is not needed anymore (with the free C funtion). Please refer for this matter to a C language book, such as the Kernigan and Richie, C language, Prentice Hall, London, 1994. An example of dynamical allocation is shown in the Monitor_nD component (which calls the MCSTAS/share/monitor_nd-lib shared library). Now, you can look at the DECLARE section of the instrument definition file. You will see that we introduce here the variables Ei, L1 to L4, A1 to A6, mono_q and ana_q, but also some other variables (DM, DA) that can be easely changed by the user, as they are all gathered in the same zone of the instrument source file. The variables Kf and Ef are declared also in case you would need them somewhere esle in the instrument.
In the INITIALIZE section, which follows the DECLARE section, you can also declare some C variables, but these will only be valid inside the INITIALIZE section. For instance, in our example, we declare Vi, Vf and others... In a typical TAS instrument, the incident Ki, transfert Q and final Kf neutron wavevector modulus, are related to the geometrical angles A1 to A6 by classical expressions : Bragg reflexions 2 pi/Ki = 2 DM sin (A1) and 2 pi/Kf = 2 DA sin (A5) Momentum exchange rule Q2 = Ki2 + Kf2 - 2 | Ki.Kf | cos (A4) Energy exchange rule Ef = Ei - E Geometrical relations A2 = 2*A1 with the monochromator and analyzer lattice spacings DM and DA respectively (in Angstroms). The value of A3 deends on the sample orientations. We shall use here A3=A4/2. mono_q = 2*PI*ORDER/DM; while DM and DA have been declared and initialized in the DECLARE section. ORDER is the order of the Bragg reflexion (usually 1). At the end of the INITIALIZE section, all variables defined in the DECLARE section or used in the TRACE section should have been defined and assigned. A-6: The end of the simulation : END and FINALLYAt the end of the simulation file, place the keyword END. That's all. If you want to do some stuff at the end of the computation (display some parameter values, free some C memory allocation blocks...), you are invited to do that in the FINALLY section (C code between the usual %{ and %} signs). It is executed at the end of the simulation. In our example we do not use that section. A-7: Looking at the instrument : mcdisplayNow that the instrument description file is complete, you can look at the instrument with the mcdisplay tool. You need to process the in14_tut.instr through mcstas, with the trace option mcstas --trace in14_tut.instr Compile the simulation with math library (-lm) gcc -O -o in14_tut.out in14_tut.c -lm These two latter commands may be performed automatically with the mcrun in14_tut.instr -n0 Then enter the instrument parameters, defining the geometry mcdisplay in14_tut.out -n 1e6 Ki=2.662 Q=2 E=0.2 A window will pop-up, showing the whole instrument top view (from 'up-there'). Depending on the Plotter (set with the MCSTAS_FORMAT env. variable or the 'mcgui/Run menu/Choose backend') PGPLOT, Scilab and Matlab may be used. These operations can be done in mcgui just by selecting Run Menu / Run Simulation. A parameter window will pop-up ; enter the values of Ki=2.662, Q=2 and E=0.2, click on the 'trace' option, and 'Start' button.
You can identify on this picture the main H53 guide (cyan long rectangle), and the instrument itself on the right side. It is possible to zoom on the spectrometer with the middle mouse button. Right button resets the full view.
The simulation should also display the message
And you can type 'Q' in the PGPlot window to stop viewing the instrument. If you rather press the spacebar, you can look at each neutron (a moving dot on the PGPlot window) passing. The Z or middle mouse button will zoom. Scilab and Matlab plotters will show a 3D view of the instrument. You may select a portion of the instrument to look at. In fact, each McStas component includes a simple drawing of the component in the MCDISPLAY section. A-8: Launching a simulation and scans : mcrunIn order to perform scans on a specific parameter, you need to first compile the instrument. The 'trace' option is not needed if you don't want to view the instrument (it slowers the simulation).
You need to process the in14_tut.instr through mcstas mcstas in14_tut.instr Compile the simulation with math library (-lm) gcc -O -o in14_tut.out in14_tut.c -lm You may launch a single simulation in14_tut.out -n 1e6 Ki=2.662 Q=2 E=0 These three commands may be executed automatically with mcrun -n 1e6 in14_tut.instr Ki=2.662 Q=2 E=0 The course of the simulation will then be displayed as follows (execution takes 8 seconds on my LinuxPPC):
And a mcstas.sim file containing informations about the last simulation will be created. mcrun -N9 -n 1e6 in14_tut.out -f in14.sim Ki=2.662 Q=2 E=-1,1 The simulation results (output of Monitor) is written in the in14.sim file, and displayed on screen. A mcstas.sim file is also gathering informations about the last performed scan. You can plot the results of the simulation with the mcplot command (see below) using the PGPLOT, Matlab and Scilab backend, or any data analysis tool, such as Our Applications for Matlab(R) or the freeware GNU Octave.
A-9: Looking at results : mcplotThe mcplot tool is designed in order to display the results of a single simulation or a full scan. It looks for the simulation file (usually called mcstas.sim) which is created for each simulation generating some output detector files. In order to test it, we add a PSD (position sensitive detector) just on the sample, in odrer to obtain a photo of the neutron beam. Between the focus_sample and the out_sample components of the instrument TRACE section, we add :
Re-compile and run mcrun -n 1e6 in14_tut.instr Ki=2.662 Q=2 E=0 The simulation displays :
Then, display the PSD photo by typing mcplot This tool can handle all detectors installed in your simulation. With PGPLOT, type 'Q' to exit. 'P' and 'C' keys generate PS and color PS files.
Displaying the simulation results can also be performed from the mcgui tool, just selecting the 'plot results' button in the Run Menu / Run Simulation. The same result (PSD monitor is a x/y monitor) can be obtained with the Monitor_nD component with
A-10: How to improve this simulationThis tutorial has shown you how to design an already quite complex instrument. But there are a number of other additional stuff that may improve significantly the efficiency of your simulations. Look at the ILL McStas library.
Focusing the energy of the source
Using a Maxwellian source
Using an external file describing the flux
Using a flux optimizer
Using a bent monochromator or analyzer
An instrument template for McStasDEFINE INSTRUMENT instrument_name(instrument_parameter1, instrument_parameter2, ...) B - Writing a new componentThis section is not written yet, sorry... |