[mcstas-users] McStasScript + optimizers

Peter Kjær Willendrup pkwi at dtu.dk
Tue Mar 31 12:47:16 CEST 2026


Dear Peter,

Sorry for overlooking your email this long…

On Thu Jul 17 12:08:23 CEST 2025 104pet104 at gmail.com wrote:

In the good old times of MATLAB + iFit one could choose a great variety of optimization methods to use on mcstas instrument. Do we have smth similar with McStasScript? Or maybe there are some common Python packages, which you recommend to use to perform optimizations, eg. genetic or swarm?

There are indeed two layers/options to work with optimisers:

1) From the relevant section of mcrun --help: (to use from McStasScript, apply instr.settings(... custom_flags='--optimize’) etc.)

    --optimize          Optimize instrument variable parameters to maximize
                        monitors
    --optimize-maxiter=optimize_maxiter
                        Maximum number of optimization iterations to perform.
                        Default=1000
    --optimize-tol=optimize_tol
                        Tolerance for optimization termination. When optimize-
                        tol is specified, the selected optimization algorithm
                        sets some relevant solver-specific tolerance(s) equal
                        to optimize-tol
    --optimize-method=optimize_method
                        Optimization solver in ['powell', 'nelder-mead', 'cg',
                        'bfgs', 'newton-cg', 'l-bfgs-b', 'tnc', 'cobyla',
                        'slsqp', 'trust-constr', 'dogleg', 'trust-ncg',
                        'trust-exact', 'trust-krylov'] (default: powell) You
                        can use your custom method method(fun, x0, args,
                        **kwargs, **options). Please refer to scipy
                        documentation for proper use of it: https://docs.scipy
                        .org/doc/scipy/reference/generated/scipy.optimize.mini
                        mize.html?highlight=minimize
    --optimize-eval=optimize_eval
                        Optimization expression to evaluate for each detector
                        "d" structure. You may combine: "d.intensity" The
                        detector intensity; "d.error"     The detector
                        intensity uncertainty; "d.values"    An array with
                        [intensity, error, counts]; "d.X0 d.Y0"   Center of
                        signal (1st moment); "d.dX d.dY"   Width  of signal
                        (2nd moment). Default is "d.intensity". Examples are:
                        "d.intensity/d.dX" and "d.intensity/d.dX/d.dY"
    --optimize-minimize
                        Choose to minimize the monitors instead of maximize
    --optimize-monitor=optimize_monitor
                        Name of a single monitor to optimize (default is to
                        use all)

2) I believe that McStasScript has some built-in features also, but couldn’t at first glance find them on the doc page or via the source code.

Also, it must be possible to use “generic” python optimisers “around” McStasScript, in the sense that monitor data can be extracted as (numpy?) arrays.

Maybe Mads(cc:) has specific hints / an example notebook?

Best,
Peter

Peter Kjær Willendrup
Forskningsingeniør, Specialkonsulent

DTU Physics

[image001.gif]


Technical University of Denmark


[image002.gif]


Department of Physics
Fysikvej
Building 307
DK-2800 Kongens Lyngby

Main office at
ESS DMSC
Building 305
DK-2800 Kongens Lyngby

Direct +45 2125 4612
Mobil +45 2125 4612
Fax +45 4593 2399
pkwi at fysik.dtu.dk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman2.mcstas.org/pipermail/mcstas-users/attachments/20260331/5162e4da/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 58 bytes
Desc: image001.gif
URL: <http://mailman2.mcstas.org/pipermail/mcstas-users/attachments/20260331/5162e4da/attachment-0002.gif>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.gif
Type: image/gif
Size: 1055 bytes
Desc: image002.gif
URL: <http://mailman2.mcstas.org/pipermail/mcstas-users/attachments/20260331/5162e4da/attachment-0003.gif>


More information about the mcstas-users mailing list