# Simulation

There are a variety of simulation tools available in Horace, so that if you have a theoretical model to describe your data you can simulate the results for the specific data points that you measured.

**IMPORTANT** When simulating an S(Q,w) model (see sqw_eval below), bear in mind the difference between what is calculated for equivalent dnd and sqw datasets. See FAQ.

## func_eval

This evaluates a user-supplied function at the x, y, z, ... values of an n-dimensional dataset, or array of datasets. The syntax is as follows:

wout = func_eval(win, @myfunc, p); wout = func_eval(win, @myfunc, p, 'all');

`win`

is the input dataset or array of datasets (sqw or dnd type) for which you wish to perform a simulation.

`myfunc`

is the name of a user-defined function to calculate the intensity at the points in the dataset(s). The function must be of the form `y = mfunc(x1, x2, ..., xn, pars)`

, e.g. `y = gauss2d(x1, x2, [amplitude, centre1, centre2, width1, width2, background])`

and accept equal sized arrays that contain the x1, x2, ... values.

`p`

can be a row-vector containing the parameters needed by the function. More generally it is a cell array containing information for the function `myfunc`

.

The optional keyword `'all'`

is used if you wish to calculate the intensity from the function over the whole domain covered by the input dataset. In other words, your dataset may contain gaps due to the trajectory of the detectors through reciprocal space, but you may wish to simulate the scattering even in the gaps.

## sqw_eval

wout = sqw_eval(win, @my_sqw_func, p); wout = sqw_eval(win, @my_sqw_func, p, 'all');

The syntax for `sqw`

is almost identical to that of `func_eval`

. The only difference is the form of the function required `my_sqw_func`

.

`my_sqw_func`

**must** be of the form

weight = my_sqw_func(qh, qk, ql, en, p)

where `qh, qk, ql, en`

area arrays that contain the co-ordinates in h, k, l and energy of every point in the dataset, irrespective of the dimensionality of that dataset. As before, `p`

is a row-vector containing the parameters required by the function. Note that the output `weight`

can take two possible forms. Normally it will be an array the same size as the input coordinates `qh, qk, ql, en`

, however if there is more than one dispersion relation (e.g. from multiple domains) then `weight`

can also be a cell array, where each element is an array the same size as `qh, qk, ql, en`

.

One would generally use `sqw_eval`

in preference to `func_eval`

if, for example, one had a model of the spin-wave cross-section for magnetic scattering.

## dispersion

Calculate dispersion relation for dataset or array of datasets.

wdisp = dispersion (win, dispreln, p) % dispersion only [wdisp,weight] = dispersion (win, dispreln, p) % dispersion and spectral weight

The output dataset (or cell array of data sets), `wdisp`

, will retain only the Q axes, and the signal array(s) will contain the values of energy along the Q axes. If the dispersion relation returns the spectral weight, this will be placed in the error array (actually the square of the spectral weight is put in the error array). In the case when the dispersion has been calculated on a plane in momentum (i.e. wdisp is IX_datset_2d) then the plot function

`ps2`

(for plot_surface2)

ps2(wdisp)

will plot a surface with the z axis as energy and coloured according to the spectral weight.

**If you wish to overplot a dispersion relation on top of, for example, a Q-E slice from your data, then you would use:**

plot(my_qe_slice) ploc(wdisp)

Note that in the above there must not be a *keep* command between plotting the QE slice and plotting the dispersion, since the `ploc`

command works on the *current* figure.

The dispersion relation is calculated at the bin centres (that is, the individual pixel information in a sqw input object is not used).

If the function that calculates dispersion relations produces more than one branch (i.e. the output of `dispreln`

is a cell array of arrays), then in the case of a single input dataset the output will be an array
of datasets, one for each branch. If the input is an array of datasets, then only the first dispersion branch will be returned, so there is one output dataset per input dataset.

Input:

`win`

Dataset that provides the axes and points for the calculation. If one of the plot axes is energy transfer, then the output dataset
will have dimensionality one less than the input dataset.

`dispreln`

Handle to function that calculates the dispersion relation w(Q). Must have form:
```
w = dispreln (qh,qk,ql,p)<code>
```

```
where,
```

```
<code>qh,qk,ql<code> Arrays containing the coordinates of a set of points in reciprocal lattice units
```

`<code>p`

Vector of parameters needed by dispersion function , e.g. [A,js,gam] as intensity, exchange, lifetime

`w`

Array of corresponding energies, or, if more than one dispersion relation, a cell array of arrays.

The more general form is:

`w = dispreln (qh,qk,ql,p,c1,c2,..)`

where,

`p`

Typically a vector of parameters that we might want to fit in a least-squares algorithm

`c1,c2,...`

Other constant parameters e.g. file name for look-up table

`p`

Arguments needed by the function. Most commonly, a vector of parameter values e.g. [A,js,gam] as intensity, exchange, lifetime. If a more general
set of parameters is required by the function, then package these into a cell array and pass that as pars. In the example above then pars = {p, c1, c2, ...}

The output is:

`wdisp`

Output dataset or array of datasets. Output is always dnd-type. The output dataset (or array of data sets) will retain only the Q axes, the
the signal array(s) will contain the values of energy along the Q axes, and the error array will contain the square of the spectral weight. If the function that calculates dispersion relations produces more than one branch, then in the case of a single input dataset the output will be an array of datasets, one for each branch. If the input is an array of datasets, then only the first dispersion branch will be returned, so there is one output dataset per input dataset.

`weight`

Mirror output: the signal is the spectral weight, and the error array contains the square of the frequency.

e.g. If `win`

is a 2D dataset with Q and E axes, then `wdisp`

is a 1D dataset with just the Q axis

## disp2sqw_eval

Similar to `sqw_eval`

, but takes as the input function a routine that calculates both the dispersion and the spectral weight, and only requires as its inputs h, k, l and some model parameters.

wout = disp2sqw_eval(win,dispreln,pars,fwhh,<Optional input parameters>)

`win`

is the input dataset (sqw or dnd)

`dispreln`

is a function of the form `[omega,s] = dispreln (qh,qk,ql,p)`

, or more generally `[w,s] = dispreln (qh,qk,ql,p,c1,c2,..)`

, where in addition to the coordinates `qh, qk, ql`

and model input parameters `p`

, some extra information contained in the data structures (cell arrays, vectors, structure arrays, etc) `c1, c2, ...`

is supplied. The outputs `omega`

and `s`

are the dispersion and spectral weight respectively. These are cell arrays of arrays if there is more than one branch of the dispersion.

`pars`

is the input parameters to the function. If this is just `p`

then `pars = p`

, but if extra parameters are required then `pars = {p, c1, c2, ...}`

, i.e. `pars`

is a cell array.

`fwhh`

is the full-width half-height of Gaussian broadening applied to dispersion relation.

The optional inputs are:

`'all'`

- Requests that the calculated sqw be returned over the whole of the domain of the input dataset. If not given, then the function will be returned only at those points of the dataset that contain data. Applies only to input with no pixel information - it is ignored if full sqw object.

`'ave'`

- Requests that the calculated sqw be computed for the average values of h,k,l of the pixels in a bin, not for each pixel individually. Reduces cost of expensive calculations. Applies only to the case of sqw object with pixel information - it is ignored if dnd type object.

The output is:

`wout`

- Output dataset or cell array of datasets

## dispersion_plot

Plot dispersion relation or array of dispersion relations along a path in reciprocal space. It can be called in the following ways, with or without outputs, as below:

dispersion_plot(rlp,dispreln,pars) dispersion_plot(lattice,rlp,dispreln,pars) dispersion_plot(...,'dispersion') % plot dispersion only dispersion_plot(...,'weight') % plot spectral weight only dispersion_plot(...,'labels',{'G','X',...}) % customised labels at the positions of the rlp dispersion_plot(...,'ndiv',n) % plot with number of points per interval other than the default [wdisp,weight]=dispersion_plot(...) % output arrays of IX_dataset_1d with dispersion and spectral weight [wdisp,weight]=dispersion_plot(...,'noplot') % output arrays without plotting

The inputs are as follows:

`lattice`

[optional] Lattice parameters [a,b,c,alpha,beta,gamma] (Angstrom, degrees). Default is [2*pi,2*pi,2*pi,90,90,90]

`rlp`

Array of reciprocal lattice points, e.g. [0,0,0; 0,0,1; 0,-1,1; 1,-1,1; 1,0,1; 1,0,0];

`dispreln`

Handle to function (e.g. `@my_function`

) that calculates the dispersion relation w(Q) and spectral weight, S(Q).
The most commonly used form is:

`[w,s] = dispreln (qh,qk,ql,p)`

where,

`qh,qk,ql`

Arrays containing the coordinates of a set of points in reciprocal lattice units

`p`

Vector of parameters needed by dispersion function e.g. [A,js,gam] as intensity, exchange, lifetime

`w`

Array of corresponding energies, or, if more than one dispersion relation, a cell array of arrays.

`s`

Array of spectral weights, or, if more than one dispersion relation, a cell array of arrays.

The more general form is:

`[w,s] = dispreln (qh,qk,ql,p,c1,c2,..)`

where,

`p`

Typically a vector of parameters that we might want to fit in a least-squares algorithm

`c1,c2,...`

Other constant parameters e.g. file name for look-up table.

`pars`

Arguments needed by the function. Most commonly, a vector of parameter values e.g. [A,js,gam] as intensity, exchange, lifetime. If a more general set of parameters is required by the function, then package these into a cell array and pass that as pars. In the exampleabove then pars = {p, c1, c2, ...}

The keyword options (which can be abbreviated to single letter) are:

`'dispersion'`

Only plot the dispersion relation(s). The default is to plot and/or return dispersion, and weight if available

`'weight'`

Only plot the spectral weight(s). The default is to plot and/or return dispersion, and weight if available

`'labels'`

Tick labels to place at the positions of the Q points in argument rlp. e.g. {'G','X','M','R'}. By default the labels are character representations of rlp, e.g. {0,0,0; 0.5,0,0; 0.5,0.5,0; 0.5,0.5,0.5} becomes {'0,0,0', '0.5,0,0', '0.5,0.5,0', '0.5,0.5,0.5'}

`'ndiv'`

Number of points into which to divide the interval between two r.l.p. (default=100)

`'noplot'`

Do not plot, just return the output IX_dataset_1d (see below)

The outputs are as follows

`wdisp`

Array of IX_dataset_1d containing dispersion, one per dispersion relation. The x-axis is the distance in Ang^-1 along the path described

`weight`

Array of IX_dataset_1d with corresponding spectral weight, one per dispersion relation

## disp2sqw_plot

Generate a plot of a dispersion relation (a subset of `dispersion_plot`

)

disp2sqw_plot(rlp,dispreln,pars,ebins,fwhh)

OR

weight=disp2sqw_plot(lattice,rlp,dispreln,pars,ebins,fwhh,'ndiv',n,'noplot')

`rlp`

is an array of reciprocal lattice points between which the dispersion relation will be calculated - e.g. `[0,0,0; 0,0,1; 0,-1,1; 1,-1,1; 1,0,1; 1,0,0]`

.

`dispreln`

is a handle to a function that calculates the dispersion relation w(Q) and spectral weight, s(Q). It must have the following form:

[w,sfact] = dispreln (qh,qk,ql,p)

where `qh,qk,ql,`

are arrays containing the coordinates of a set of points in reciprocal lattice units, `p`

is a vector of parameters needed by dispersion function. The output `w`

is an array of corresponding energies, or, if more than one dispersion relation is called, a cell array of arrays. `sfact`

is the correspondent to `w`

array of structure factors (see below).

`pars`

are the parameters `p`

mentioned above.

`ebins`

gives the energy grid on which the dispersion will be plotted. Is given by a vector of the form `[energy_lo,step,energy_hi]`

.

`fwhh`

is a scalar which gives the width of Gaussian broadening in energy applied to the dispersion relation - i.e. a very crude way of approximating the effect of resolution.

There are some additional optional inputs and outputs.

`'noplot'`

can be used when an output object is specified, and you do not wish to plot it.

`'ndiv'`

is a scalar specifying the number of points between adjacent q-points in the list `rlp`

.

The output `weight`

is a generic Herbert IX_dataset_2d object corresponding to the Q-E dispersion plot.

The image intensity, as the function of **Q** along the rlp path alonx x-axis and the energy transfer along y-axis is determined by the equation:

weight(energy)= sfact.*exp(-(w(Q,p)-energy).^2/(2*sig.^2))./(sig*sqrt(2*pi));

where

sig = fwhh/sqrt(log(256));

See example of script to plot dispersion here.