|
|
|
|
![]() |
![]() |
![]() | |||||||||||||||
|
About McStas Documentation |
5.5 Writing component definitionsThe purpose of a McStas component is to model the interaction of a neutron with a physical component of a real instrument. Given the state of the incoming neutron ray, the component definition calculates the state of the neutron ray when it leaves the component. The calculation of the effect of the component on the neutron is performed by a block of embedded C code. One example of a component definition is given in section 5.5.12, and all component definitions can be found on the McStas web-page [Mcsa] and are described in the McStas component manual. There exists a large number of functions and constants available in order to write efficient components. See appendix A.2 for
5.5.1 The component definition header1DEFINE COMPONENT name 2 This marks the beginning of the definition, and defines the name of the component. 1DEFINITION PARAMETERS (d_1, d_2, ...) 2SETTING PARAMETERS (s_1, s_2, ...) 3 This declares the definition and setting parameters of the component. These parameters can be
accessed from all sections of the component (see below), as well as in Setting parameters are translated into C variables usually of type Beyond the basic
However, because of the use of Outside the There are a few cases where the use of definition parameters instead of setting parameters
makes sense. If the parameter is not numeric, nor a character string (i.e. an array, for
example), a setting parameter cannot be used. Also, because of the use of Setting parameters may optionally be declared to be of type 1OUTPUT PARAMETERS (s_1, s_2, ...) 2 This declares a list of C identifiers (variables, functions) that are output parameters (i.e. global) for the component. Output parameters are used to hold values that are computed by the component itself, rather than being passed as input. This could for example be a count of neutrons in a detector or a constant that is precomputed to speed up computation. Using OUTPUT PARAMETERS is strongly recommended for DECLARE and internal/global
component variables and functions in order to prevent that instances of the same component
use the same variable names. Moreover (see section 5.5.4 below), these may be accessed
from any other instrument part (e.g. using the The OUTPUT PARAMETERS section is optional. Optional component parametersJust as for instrument parameters, the definition and setting parameters of a component may be given a default value. Parameters with default values are called optional parameters, and need not be given an explicit value when the component is used in an instrument definition. A parameter is given a default value using the syntax “param = value”. For example 1SETTING PARAMETERS (radius, height, pack= 1) 2 Here Optional parameters can greatly increase the convenience for users of components with many parameters that have natural default values which are seldom changed. Optional parameters are also useful to preserve backwards compatibility with old instrument definitions when a component is updated. New parameters can be added with default values that correspond to the old behavior, and existing instrument definitions can be used with the new component without changes. However, optional parameters should not be used in cases where no general natural default value exists. For example, the length of a guide or the size of a slit should not be given default values. This would prevent the error messages that should be given in the common case of a user forgetting to set an important parameter. 5.5.2 The NOACC flagA component definition may be marked 1DEFINE COMPONENT name NOACC 2 to declare that this component class can never be compiled for GPU/OpenACC execution
(section 4.5.4 in the User Manual) – e.g. because its TRACE section calls out to a C library that
has no GPU-compatible implementation. This automatically pulls in the -DFUNNEL dependency
flag, which enables McStas’s CPU/GPU “funneling” mode so that an instrument mixing
GPU-friendly and NOACC components can still be run with 5.5.3 The DEPENDENCY line1DEPENDENCY "-lLIB1 -lLIB2 .." 2 This optional line indicates any external library which are needed by the component. It should be used when the component code calls external functions and routines which should be available when compiling the instrument. This line must be indicated just before the SHARE and DECLARE sections. Refer to the section 5.3.2 for more information. 5.5.4 The DECLARE section1DECLARE 2%{ 3// C code declarations (variables, definitions, functions) 4// These are usually OUTPUT parameters to avoid name conflicts 5%} 6 This gives C declarations of global variables, functions, etc. that are used by the component code. This may for instance be used to declare a neutron counter for a detector component. This section is optional. Note that any variables declared in a 1DEFINE COMPONENT Detector 2OUTPUT PARAMETERS (counts) 3DECLARE 4%{ 5 int counts; 6%} 7... 8 The idea is that the counts variable counts the number of neutrons detected. In the
instrument definition, the counts parameter may be referenced using the 1COMPONENT d1 = Detector() 2... 3COMPONENT d2 = Detector() 4... 5FINALLY 6%{ 7 printf("Detector counts: d1 = %d, d2 = %d\n", 8 MC_GETPAR(d1,counts), MC_GETPAR(d2,counts)); 9%} 10 This way, McStas takes care to rename transparently the two ’counts’ OUTPUT parameters so that they are distinct, and can be accessed from elsewhere in the instrument (EXTEND, FINALLY, SAVE, ...) or from other components. This particular example is outdated since McStas monitors will themselves output their contents. 5.5.5 The SHARE section1SHARE 2%{ 3// C code shared declarations (variables, definitions, functions) 4// These should not be OUTPUT parameters 5%} 6 The SHARE section has the same role as DECLARE except that when using more than one
instance of the component, it is inserted only once in the simulation code. No occurrence of the
items to be shared should be in the OUTPUT parameter list (not to have McStas rename the
identifiers). This is particularly useful when using many instances of the same component (for
instance guide elements). If the declarations were in the DECLARE section, McStas
would duplicate it for each instance (making the simulation code longer). A typical
example is to have shared variables, functions, type and structure definitions that
may be used from the component TRACE section. For an example of SHARE, see the
samples/Single_crystal component. The 5.5.6 The INITIALIZE section1INITIALIZE 2%{ 3// C code initialization 4%} 5 This gives C code that will be executed once at the start of the simulation, usually to initialize any variables declared in the DECLARE section. This section is optional. Component setting parameters may be modified in this section, affecting the rest of the component. 5.5.7 The TRACE section1TRACE 2%{ 3// C code to compute neutron interaction with component 4%} 5 This performs the actual computation of the interaction between the neutron ray and the
component. The C code should perform the appropriate calculations and assign the resulting
new neutron state to the state parameters. Most components will require propagation routines
to reach the component entrance/area. Special macros The C code may also execute the special macro ABSORB to indicate that the neutron has
been absorbed in the component and the simulation of that neutron will be aborted.
On the other hand, if the neutron event should be allowed be backpropagated, the
special macro 5.5.8 The SAVE section1SAVE 2%{ 3// C code to execute in order to save data 4%} 5 This gives code that will be executed when the simulation ends, or is requested to save data, for instance when receiving a USR2 signal (on Unix systems, see section 4.4), or when triggered by the Progress_bar(flag_save=1) component. This might be used by monitors and detectors in order to write results. An extension depending on the selected output format (see section 4.4) is automatically appended to file names, if these latter do not contain extension. In order to work properly with the common output file format used in McStas, all monitor/detector components should use standard macros for writing data in the SAVE or FINALLY section, as explained below. In the following, we use \(N = \sum _i p_i^0\) to denote the count of detected neutron events, \(p = \sum _i p_i\) to denote the sum of the weights of detected neutrons, and \(\textit {p2} = \sum _i p_i^2\) to denote the sum of the squares of the weights, as explained in section 3.2.1. As a default, all monitors using the standard macros will display the integral \(p\) of the monitor bins, as well as the 2\(^{nd}\) moment \(\sigma \) and the number of statistical events \(N\). This will result in a line such as: 1Detector: CompName I=p CompName ERR=sigma CompNameN=N "filename" 2 For 1D, 2D and 3D monitors/detectors, the data histogram store in the files is given per bin
when the signal is the neutron intensity (i.e. most of the cases). Most monitors define binning
for an \(x_n\) axis value as the sum of events falling into the \([ x_n x_{n+1} ]\) range, i.e the bins are not centered, but
left aligned. Using the Monitor_nD component, it is possible to monitor other signals using
the ’ The results of a single detector/monitor are written using the following macro: 1DETECTOR_OUT_0D(t, N, p, p2) 2 Here, t is a string giving a short descriptive title for the results, e.g. “Single monitor”. One-dimensional detectors/monitors The results of a one-dimensional detector/monitor are written using the following macro: 1DETECTOR_OUT_1D(t, 2 xlabel, ylabel, 3 xvar, x_min, x_max, m, 4 &N[0], &p[0], &p2[0], 5 filename) 6 Here,
Two-dimensional detectors/monitors The results of a two-dimensional detector/monitor are written to a file using the following macro: 1DETECTOR_OUT_2D(t, 2 xlabel, ylabel, 3 x_min, x_max, y_min, y_max, m, n, 4 &N[0][0], &p[0][0], &p2[0][0], 5 filename) 6 Here,
Note that for a two-dimensional detector array, the first dimension is along the X axis and the second dimension is along the Y axis. This means that element \((i_x,i_y)\) can be obtained as \(p[i_x*n+i_y]\) if \(p\) is a pointer to the first element. The results of a monitor that records the mean of a quantity \(v\) (e.g. the polarisation) rather than an intensity are written using the following macros: 1DETECTOR_OUT_0D_MEAN(t, N, w, w2, wv, wv2) 2 3DETECTOR_OUT_1D_MEAN(t, 4 xlabel, ylabel, 5 xvar, x_min, x_max, m, 6 &N[0], &w[0], &w2[0], &wv[0], &wv2[0], 7 filename) 8 9DETECTOR_OUT_2D_MEAN(t, 10 xlabel, ylabel, 11 x_min, x_max, y_min, y_max, m, n, 12 &N[0][0], &w[0][0], &w2[0][0], &wv[0][0], &wv2[0][0], 13 filename) 14 Here, with the sums taken over the events,
and the other arguments are as for the macros above. For an unweighted mean, use \(p_j=1\), i.e. \(N\) for both \(w\) and w2. The sums are not changed by the macros, which also merge them between MPI nodes, so the component must not do this itself. The output in each bin is the mean \(\bar {v}=\textit {wv}/w\), with the error \begin{equation} \sigma = \sqrt {\frac {N}{N-1}\,\frac {\textit {wv2}/w-\bar {v}^2}{N_\mathrm {eff}}}, \qquad N_\mathrm {eff} = w^2/\textit {w2} . \end{equation} As for intensities, the error of a bin with
one event is set to its value. The total written in the file header and on the Detector: line is
the mean over all bins, and the header has the field mean: yes (mean: no for all other
output).
Three-dimensional detectors/monitors There is no three-dimensional DETECTOR_OUT macro. Three-dimensional data can be recorded in NeXus format with Monitor_nD in event (list) mode, using pixel identifiers. 5.5.9 The FINALLY section1FINALLY 2%{ 3// C code to execute at end of simulation ... 4%} 5 This gives code that will be executed when the simulation has ended. This might be used to free memory and print out final results from components, e.g. the simulated intensity in a detector. This section also triggers the SAVE section to be executed. 5.5.10 The MCDISPLAY section1MCDISPLAY 2%{ 3// C code to draw a sketch of the component ... 4%} 5 This gives C code that draws a sketch of the component in the plots produced by the
The MCDISPLAY section is optional. If it is omitted, This command, if present, must be the first in the section. It takes a single argument: a
string containing zero or more of the letters “x”, “y” and “z”. It causes the drawing to be
enlarged along the specified axis in case 1 magnify("xy"); The line command takes the following form: 1line(x_1, y_1, z_1, x_2, y_2, z_2) It draws a line between the points \((x_1, y_1, z_1)\) and \((x_2, y_2, z_2)\). The dashed_line command takes the following form: 1dashed_line(x_1, y_1, z_1, x_2, y_2, z_2, n) 2 It draws a dashed line between the points \((x_1, y_1, z_1)\) and \((x_2, y_2, z_2)\) with \(n\) equidistant spaces. The multiline command takes the following form: 1 \texttt{multiline(n, x_1, y_1, z_1, ..., x_n, y_n, z_n)} 2 It draws a series of lines through the \(n\) points \((x_1, y_1, z_1)\), \((x_2, y_2, z_2)\), …, \((x_n, y_n, z_n)\). It thus accepts a variable number of arguments depending on the value of \(n\). This exposes one of the nasty quirks of C since no type checking is performed by the C compiler. It is thus very important that all arguments to multiline (except \(n\)) are valid numbers of type double. A common mistake is to write 1 multiline(3, x, y, 0, ...) 2 which will silently produce garbage output. This must instead be written as 1 multiline(3, (double)x, (double)y, 0.0, ...) 2 The rectangle command takes the following form: 1rectangle(plane, x, y, z, width, height) 2 Here plane should be either
The box command takes the following form: 1box(x, y, z, xwidth, yheight, zlength) The command draws a box with the center at \((x, y, z)\) and the size xwidth \(\times \) yheight \(\times \) zlength. The circle command takes the following form: 1circle(plane, x, y, z, r) Here plane should be either 5.5.11 The end of the component definition1END This marks the end of the component definition. 5.5.12 A component example: SlitA simple example of the component Slit is given. |
||||||||||||||||||||
| |||||||||||||||||||||