|
|
|
|||
|
About McXtrace Documentation |
5.4 Using simulation front-endsMcXtrace includes a number of front-end programs that extend the functionality of the simulations. A front-end program is an interface between the user and the simulations, running the simulations and presenting the output in various ways to the user. The list of available McXtrace front-end programs is (see mxdoc --tools, or run any tool with -h for its specific help): 1 McXtrace Tools 2 mcxtrace Main instrument compiler 3 mcxtrace-pygen Alternative code generator: McStasScript Python model 4 mxrun Instrument build and execution utility 5 mcxtrace-jupylab Opens a generated instrument model in JupyterLab 6 mxgui Graphical User Interface instrument builder 7 mxdoc Component library documentation generator/viewer 8 mxplot Simulation result viewer (default: pyqtgraph backend) 9 mxplot-html Simulation result viewer, in a web browser (D3.js) 10 mxplotdiff-html Difference plot between two simulation results, in a browser 11 mxcoplot-html Overlay (co-plot) of two simulation results’ 1D monitors 12 mxdisplay Instrument geometry viewer (default: pyqtgraph backend) 13 mxtest Self-test / benchmark of the current McXtrace installation 14 mxviewtest Viewer for mxtest results, with mxplotdiff-html comparisons 15 Each tool also exists in dedicated *-matplotlib, *-pyqtgraph, *-html, 16 *-webgl or *-webgl-classic variants where applicable, see sections below. 17 DOC: Please visit https://www.mcxtrace.org
5.4.1 The graphical user interface (mxgui)The front-end mxgui provides a graphical user interface that interfaces the various parts of the McXtrace package. It may be started with the single command 1 mxgui The mxgui program may optionally be given the name of the instrument file to use. Dependencies: As of McXtrace 3.x, mxgui is a pure Python/Qt application (PyQt), bundled with the standard conda-forge McXtrace package; no separate Perl, Tk, or PGPLOT installation is required. The menusWhen the front-end is started the main window is opened (see figure 5.1). This window displays the output from compiling and running simulations, and contains a few menus and buttons for easy navigation. The main purpose of the front-end is to edit and compile instrument definitions, run the simulations, and visualize the results. The File menu has the following features:
The Simulation menu has the following features:
The New from Template menu (known as “X-ray Site” in older McXtrace releases)
contains a list of template and example instruments as found in the McXtrace library,
sorted by category/facility (e.g. Templates, ILL, ISIS, ESS, Union_demos, …). When
selecting one of these, a local copy of the instrument description is transferred to
the active directory (so that users have modification rights) and loaded. One may
then view its source (Edit) and use it directly for simulations/trace (3D View).
The Tools menu gathers minor tools.
The Help menu has the following features, through use of mxdoc and a web browser. To customize the used web browser, set the BROWSER environment variable. If BROWSER is not set, mxgui uses netscape/mozilla/firefox on Unix/Linux and the default browser on Windows.
The run dialogThe run dialog is used to run simulations. It allows the entry of instrument parameters as well as the specifications of options for running the simulation (see section 5.3 for details). It also allows to run the mxdisplay (section 5.4.5) and mxplot (section 5.4.6) front-ends together with the simulation. The meaning of the different fields is as follows:
Most of the settings on the run dialog can be saved for your next McXtrace run using ’Save configuration’ in the File menu. Before running the simulation, the instrument definition is automatically compiled if it is newer than the generated C file (or if the C file is newer than the executable). The executable is assumed to have a .out suffix in the filename. NB: If components are changed, automatic compilation is not performed. Instead, use the File/Compile menu item in mxgui. The editor windowThe editor window provides a simple editor for creating and modifying instrument definitions. Apart from the usual editor functions, the “Insert” menu provides some functions that aid in the construction of the instrument definitions:
5.4.2 Running simulations on the commandline (mxrun)The mxrun front-end provides a convenient command-line interface for running simulations with the same automatic compilation features available in the mxgui front-end. It also provides a facility for running a series of simulations while varying an input parameter. The command 1mxrun sim args ... 2 will compile the instrument definition sim.instr (if necessary) into an executable simulation sim.out. It will then run sim.out, passing the argument list args The possible arguments are the same as those accepted by the simulations themselves as described in section 5.3, with the following extensions:
The -h option will list valid options; see also Table 5.1 for the complete, current option list.
5.4.3 Interactive notebooks (mcxtrace-jupylab)The mcxtrace-jupylab front-end offers an interactive, notebook-based alternative to editing an .instr file directly and running it via mxgui/mxrun. Given an instrument file: 1mcxtrace-jupylab name.instr it first runs mcxtrace-pygen (see above) to translate the instrument into an equivalent McStasScript Python model, writes that model into a notebook (name_generated.ipynb) built from a ready-made template (imports, and cells calling show_diagram(), show_instrument(), backengine() and the plotting helpers are already filled in), and opens it in JupyterLab1 . From there the instrument can be inspected, modified and run cell by cell using McStasScript’s own Python API, which is useful for exploratory work and for combining a simulation with other Python-based analysis in the same notebook. Note that this generates a McStasScript model once, as a starting point; it is not kept in sync with later edits to the original .instr file, nor vice versa. See the McStasScript documentation [Mcs] for the Python API itself. 5.4.4 GPU acceleration via OpenACCMcXtrace 3.x supports GPU-accelerated simulations through the OpenACC framework, enabling substantial speed-ups over CPU-only execution on compatible NVIDIA hardware. Requirements
Running a GPU-accelerated simulationPass the --openacc flag to mxrun: mxrun --openacc MyInstrument.instr -n 1e9 The OpenACC execution can be tuned with --vecsize (vector length), --numgangs (number of “gangs”), --gpu_innerloop (maximum number of particles per GPU kernel run; if smaller than the requested x-ray count, the kernel is repeated) and --funnel (funnelled simulation flow, e.g. for mixed CPU/GPU execution), see Table 5.2 and Table 5.1. Combined multi-core + GPU execution. It is also possible to use both CPU cores and the GPU simultaneously. Enable this by adding the following to the OACCFLAGS field of your mccode_config.json: "OACCFLAGS": "-fast -Minfo=accel -acc=gpu,multicore -gpu=managed -DOPENACC -DMULTICORE"
Further informationGPU terminology specific to McStas/McXtrace 3 and detailed debugging tips are documented on the McCode wiki:
5.4.5 Graphical display of simulations (mxdisplay)The front-end mxdisplay is a graphical visualization tool, very useful for debugging. It presents a schematic drawing of the instrument definition, showing the position of the components and the paths of the simulated x-rays through the instrument. It is thus very useful for debugging a simulation, for example to spot components in the wrong position or to find out where x-rays are getting lost. To use the mxdisplay front-end with a simulation, run it as follows: 1mxdisplay sim args ... 2 where sim is the name of either the instrument source sim.instr or the simulation program sim.out generated by mcxtrace, and args … are the normal command line arguments for the simulation, as explained above. The -h option will list valid options. The drawing back-end may be selected by invoking one of the dedicated front-ends directly:
For instance, calling 1mxdisplay-webgl ./Template_1Slit_Diff.out SLITW=5e-6 2 will open a WebGL view of the instrument in a browser tab. The mxdisplay front-end can also be run from the mxgui front-end. The (default, PyQtGraph) front-end accepts the following options, amongst others – run mxdisplay --help for the complete, current list:
All variants run mxrun --trace underneath, so an instrument parameter may be given as name=value just as for mxrun. In the PyQtGraph viewer, q quits, p saves a PNG image, s saves an SVG image (not on Windows), space or F5 shows the next ray, a click on a sub-plot zooms in on it (right-click to leave the zoomed view), and h or F1 lists the components. A legacy Matlab/Octave variant, mxdisplay-matlab, also exists under tools/matlab (it falls back to the Python mxdisplay when neither Matlab nor Octave is found). When debugging trajectories through a long or complex instrument, combining --inspect with a modest --ncount typically gives the clearest, fastest picture of what is happening around a particular component.
5.4.6 Plotting the results of a simulation (mxplot)The front-end mxplot is a program that produces plots of all the monitors in a simulation, and it is thus useful to get a quick overview of the simulation results. In the simplest case, the front-end is run simply by typing 1 mxplot 2 This will plot any simulation data stored in the current directory, which is where simulations store their results by default. If the --dir option has been used (see section 5.3), the name of the file or directory should be passed to mxplot, e.g. “mxplot dir ” or “mxplot file ”. It is also possible to plot one single text (not binary) data file from a given monitor, passing its name to mxplot. As with mxdisplay (section 5.4.5), the drawing back-end may be selected by invoking the dedicated front-end directly: mxplot-pyqtgraph (the default), mxplot-matplotlib, or mxplot-html (renders an interactive, self-contained web page using D3.js, see section 5.3.2), or by setting the MCXTRACE_FORMAT environment variable. The back-ends share the simulation file or directory argument, and have a few options of their own. mxplot-html accepts -n/--nobrowse (do not open a browser), -l/--log (log scale), --autosize (expand the plots to the window size), -o/--output FILE (output .html file) and -W/-H (width and height of the plot frames). mxplot-matplotlib accepts --log, --backend NAME, --format FMT together with --output FILE (save as pdf, png, eps, svg, … without opening a window) and --html (save as HTML via mpld3; Linux only), and mxplot-pyqtgraph accepts --invcanvas (white instead of black canvas). Both the matplotlib and PyQtGraph variants also take -t/--test, which only runs the data loader and prints the structure of the plot. A legacy Matlab/Octave/iFit variant, mxplot-matlab, exists under tools/matlab; it picks whichever of Matlab, iFit and Octave is available (falling back to the Python mxplot), and can export each monitor with -png, -jpg, -fig, -eps or -pdf. All plotters read the plain-text McXtrace/McCode data format (section 5.5); NeXus files are not currently read back by the plotters. The mxplot front-end can also be run from the mxgui front-end. The initial display shows plots for each detector in the simulation. Comparing simulation results: mxplotdiff and mxcoplot. Two companion tools share the loading and monitor-matching code of mxplot to compare simulation results (output directories, or single monitor files), matching monitors by output filename. Each exists in the same three rendering variants as mxplot (-html, -matplotlib and -pyqtgraph); the browser-based HTML variants are described here:
mxplotdiff-html accepts -A LABEL_A/-B LABEL_B to control the labels used for each input, and both tools accept -o OUTPUT to control the output directory name, -n/--nobrowse, -l/--log and --autosize; run either with --help for the complete option list. The matplotlib and PyQtGraph variants take the same dataset arguments and labelling options, plus the output options of the corresponding mxplot back-end. mxplotdiff-html and mxcoplot-html are also used internally by mxviewtest (section 5.4) to compare test runs against a reference.
5.4.7 Creating and viewing the library, component/instrument help and Manuals (mxdoc)McXtrace provides an easy way to generate automatically an HTML help page about a given component or instrument, or the whole McXtrace library. 1mxdoc 2mxdoc searchterm 3mxdoc file.comp The first example generates an index.html catalog file using the available components and instruments (both locally, and in the McXtrace library), and browses it using the BROWSER environment variable (e.g. firefox, chromium, …). Alternatively, if a search term or a file.comp/file.instr path is given, mxdoc will search within the library and open documentation for all matching entries. Additional options include --install/-i (regenerate the local installation-wide index), --dir=DIR/-d (add search results from a given directory), and --verbose/-v (print a parsing log). HTML is always written; --md/-M and --tex/-T also emit Markdown respectively LaTeX documentation files (--all-formats/-A for all three) — the LaTeX snippets of the component library that make up the Component Manual are produced this way. With --in-repo the output is placed next to the source files instead of in the installed documentation directory (and no master page is made), and --outdir=DIR/-o selects the output directory (in-repo use only). The options --manual/-m, --comps/-c and --web/-w will open the User Manual (this document), the Component Manual, and the McXtrace web site, respectively, all requiring BROWSER to be defined. Finally, the --help option will display the command help, as usual. See section 6.7 for more details about the McDoc usage and header format. mxdoc is a pure Python tool, part of the standard McXtrace conda-forge package. Online wiki. In addition to this manual, extensive and frequently updated documentation is maintained on the McCode GitHub wiki [Wik]: The wiki includes end-user guides, component development howtos, GPU acceleration information, and migration guides from McXtrace 2.x to 3.x.
5.4.8 Self-testing the installation (mxtest, mxviewtest)McXtrace ships with a Python self-test/benchmark harness, replacing the earlier mxrun --test command. mxtest compiles and runs a selection (or all) of the example instruments found under the current installation(s), and stores each run’s results (including any test-defined %Example reference values) in a timestamped output folder. Each test compiles the instrument, displays it with a single particle, runs it, and compares the result with the target value recorded in the instrument header. Useful options include --ncount (default \(10^6\)), --seed (default 1000; 0 randomizes), --mpi, --no-mpi, --openacc and --nexus (forwarded to mxrun for every tested instrument), --lint (only run the C linter, no simulations), --config=LABEL (test only a specific McCode installation/config), --instr=REGEX (test only matching instrument names), --comp=COMP[,COMP...] (test only instruments using any of the given components), --local=DIR (test instruments from DIR instead of the installation), --limit=N (test only the first N instruments per version), --skipnontest (skip instruments without an %Example line), --strict (fail instruments without %Example line(s) immediately) and --permissive (exit with status 0 even if tests fail), --testdir=DIR and --suffix=SUFFIX (where the results are written), --compilemax, --runmax and --displaymax (time limits in seconds) and --noplots (do not generate the overview plots); run mxtest --help for the full list. mxviewtest then generates an HTML report of one or more mxtest runs found in a given folder (the current directory by default). When more than one run is present (e.g. a reference run and a run on a different platform, GPU vs. CPU, or a different McXtrace branch), it takes the oldest subfolder as the reference column (override with --reflabel=LABEL), and additionally runs mxplotdiff-html and mxcoplot-html (section 5.4.6) between each other column’s monitor output and the reference’s, adding a DIFF [vs ref] link next to each such row in the report. These comparisons are cached on disk and generated in parallel (--diffworkers, each limited to --diffmax seconds), so re-running mxviewtest is fast; use --nodiff to skip generating them entirely, or --diff-errors-only to only diff rows that already show a discrepancy (by default every valid row is diffed). --nobrowse keeps the finished report from being opened in a browser. |
||||
| |||||