Add MCViNE sample kernels as Union processes (and standalone samples) - #2716
Fahima-Islam wants to merge 5 commits into
Conversation
Add the MCViNE entry to enum process, a pointer_to_a_MCViNE_physics_storage_struct member to union data_transfer_union, and the two dispatch cases in physics_my and physics_scattering (guarded by PROCESS_MCVINE_DETECTOR). All MCViNE kernel processes share this one type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
mcvine-lib: ports of the MCViNE sample kernels (mccomponents/lib/kernels/sample and mcvine/acc SANS2D_ongrid), a run-time expression evaluator, table and grid readers, DOS / Debye-Waller helpers, MCViNE IDF phonon readers, sample shapes and the MCViNE HomogeneousNeutronScatterer transport. mcvine-union-lib: glue between the kernels and the Union framework. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
MCViNE_<kernel>_process (Union) and MCViNE_<kernel> (standalone) for ConstantEnergyTransfer, ConstantQE, ConstantvQE, E_Q, Broadened_E_Q, LorentzianBroadened_E_Q, E_vQ, SQ, SvQ, SQE, DGSSXRes, SANS2D_ongrid, Phonon_IncoherentElastic, Phonon_IncoherentInelastic, Phonon_CoherentInelastic_PolyXtal and Phonon_CoherentInelastic_SingleXtal. Documentation in contrib/doc/MCViNE_kernels.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
One Tests_samples/Test_MCViNE_<kernel> instrument per kernel, running the Union process (comp_select=1) or the standalone component (comp_select=2) on the same sample, with an %Example line for each. Test inputs in data/MCViNE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YUUwywmxwaAvj7JoVQJ9y2
|
@Fahima-Islam this looks absolutely great, good job! :-) The compilation issue for mcstas-antlr is intermittent and will be fixed via an incoming PR. |
|
Wow, great job! Really like how the code is organized such that duplication is avoided across the stand alone samples and Union processes. This offers a lot of exciting new capabilities! The run times are also faster than I feared for the advanced inelastic capabilities. You mention that duplicated efforts such as 2D grid (Q,E) are not ported, and of course its extra work to do that, but in general we welcome having several independent ways to calculate the same thing for a good cross check. |
|
@Fahima-Islam absolutely nice stuff indeed. :-) Just a word of warning - the Windows platform will require a bit more work it seems, but Claude might help you with that. (Pick the right artefact from https://github.com/mccode-dev/McCode/actions/runs/36691645550?pr=2716 and investigate the failing new MCVINE instrument Most often the issues are along the lines of
|
|
@Fahima-Islam I have dug out the Windows results for you It looks like a number of them indeed are things like replacements of I will boot up my Windows later to see if I can get a hold of some of this... |
rpcndr.h (pulled in by windows.h) has '#define small char', so 'int i, small;' in mcvine_S_Phonon_IncoherentInelastic became 'int i, char;' under MSVC and broke the build of every instrument that includes mcvine-lib.c. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
@willend thanks a lot for digging out the Windows results! All 16 5f6df3c renames it to Let's see what the Windows CI says now. |
|
@Fahima-Islam great! |
|
Seems to do he trick indeed @Fahima-Islam - snipped from the Windows compile log: FYI we are also in the midst of restructuring parts of |
|
@willend thanks for confirming! All CI has now finished:
On the Union restructuring: no problem with waiting. Once |
|
@mads-bertelsen thank you! I'm glad the code structure and the run times work for you. Good point about independent implementations as cross-checks. I left those kernels out to keep this PR focused, but they would be straightforward to add in a separate follow-up PR using the same This kind of work has also become much faster with AI-assisted ("vibe") coding: this PR, porting 16 kernels from MCViNE's C++, the Union glue and the test instruments, came together far quicker than it would have by hand. Most of my time went into checking the physics and validating against MCViNE and the references. So a follow-up with the overlapping kernels is a realistic next step rather than a long project, and having two independent implementations side by side is exactly the kind of cross-check that makes the fast route trustworthy. I'd start that after this PR is merged and the Union restructuring has settled, so it is built on the new Union interface. Does that sound good? |
|
@Fahima-Islam I've recently seen intermittent failures on As a side-note both of I think I will simply add an issue for that to later perform a separate investigation. |







Free-form text area
This PR adds 16 sample scattering kernels from MCViNE (https://github.com/mcvine/mcvine,
mccomponents/lib/kernels/sample, plusSANS2D\_ongridfrom https://github.com/mcvine/acc). McStas has no equivalent for any of them. Each kernel is available in two forms:contrib/MCViNE\_<kernel>\_process.comp(geometry, containers, absorption and multiple scattering by Union), andcontrib/MCViNE\_<kernel>.comp(box / (hollow) cylinder / sphere, same transport as MCViNE's HomogeneousNeutronScatterer).Both forms call the same kernel code in
share/mcvine-lib.c.Kernels added
Isotropic\_Sqw-format grid, optional final-energy focusingMCViNE takes functions such as E(Q) and S(Q) as strings evaluated at run time (fparser). The same is supported here through a small built-in expression evaluator, e.g.
E\_Q="20\*sin(Q\*1.5)^2".Kernels not added, because McStas already covers them: isotropic scattering (
Incoherent), gridded S(Q,E) (Isotropic\_Sqw), powder and single-crystal diffraction (PowderN,Single\_crystal), SANS spheres, and multiphonon (IncoherentPhonon\_process, NCrystal). MCViNE's EPSC diffraction kernel is unfinished upstream and is not ported.Runtime-library change (Union core, 12 added lines). Union dispatches physics with a
switchonenum process. This PR registers one new process type,MCViNE:share/union-lib.c: adds an enum entry and adata\_transfer\_unionmembershare/union-suffix.c: adds twocase MCViNE:blocks guarded byPROCESS\_MCVINE\_DETECTOR, the same pattern as the other processesAll 16 kernels share this type. Its storage struct holds pointers to the kernel and to its sampling function, so further MCViNE kernels need no more core changes. Existing processes are untouched. The processes are
NOACC(CPU only).New files
share/mcvine-lib.h/.c– kernel physics, expression evaluator, table/grid readers (files found throughOpen\_File), DOS and Debye–Waller helpers, MCViNE IDF phonon readers, shapes and standalone transportshare/mcvine-union-lib.h/.c– Union gluecontrib/MCViNE\_\*\_process.comp(16),contrib/MCViNE\_\*.comp(16)contrib/doc/MCViNE\_kernels.md– overview, input formats, differences from MCViNE, validationexamples/Tests\_samples/Test\_MCViNE\_\*/(16 test instruments)data/MCViNE/– small test inputs (toy fcc phonon IDF set and atoms file, Debye DOS, S(q,w) grid, SANS map; ~0.6 MB)Where the port differs from MCViNE (documented in
contrib/doc/MCViNE\_kernels.md):unbiased=1option: available on four kernels whose MCViNE sampling loops are slightly biased; the default reproduces MCViNE.Tests
Test\_MCViNE\_<kernel>instrument runs the same sample as the Union process (comp\_select=1) or as the standalone component (comp\_select=2), with the same geometry, absorption and multiple scattering. It has an%Exampleline for each form.mctestresults: all 32%Examplecases pass inmctest --no-mpiat 100 % of their recorded values. The Union and standalone values of each kernel agree within about 2σ (0.3–1 % for most kernels; SQE, SANS2D and SingleXtal have larger statistical errors).Physics validation (scripts attached): analytic integrals, McStas
Incoherent, and independent Python calculations. Examples:Incoherentwithin 0.3 % for single scattering, and an analog MC within 0.2 % for double scattering.Documentation:
mcdocrenders the parameter tables of all 32 components.Formatting:
mccode-clangformathas been applied.Linting: the shared library is clean under
gcc -std=c99 -Wall -Wextraandcppcheck --enable=warning,portability,performance.---
Declaration of use of AI-tools
Please add a checkmark here if you used AI-tools during the work for this contribution
Furter, please describe how / where and for what the tools were used:
Claude (Anthropic, Claude Code) was used. It compared the MCViNE and McStas code bases to find the missing kernels, ported the MCViNE C++ kernels to C (
share/mcvine-lib.c), and wrote the Union glue and the 12-line Union core registration.I reviewed the ported physics against the MCViNE sources and the validation results.
---
Development OS / boundary conditions
Developed and tested on Linux (Ubuntu 24.04, gcc 13, x86_64) with McStas built from
main@ d3ab8cf, usingmcrun/mctestfrom the same build without MPI. No extra dependencies are needed. Two test instruments are slow at the default 1e6 neutrons in Union mode:Test\_MCViNE\_Phonon\_CoherentInelastic\_SingleXtal(about 150 s) andTest\_MCViNE\_E\_vQ(about 90 s).---
PR Checklist for contributing to McStas/McXtrace
My contribution includes a new component file
sigma\_\*,Vc,p\_transmit,target\_index/focus\_r, Unionpacking\_factor/interact\_fraction)mcdocutility and rendered a reasonable documentation page for the component (screenshots attached)mccode-clangformattool to apply the standard McCode component indentation schemecontribcomponent categoryMy contribution includes a new instrument file
mcdocutility and rendered a reasonable documentation page for the instrument%Example:line to describe expected behaviour (two per instrument)mcrun --c-lint"linter" and followed advice to remove most / all warnings that are raised (the new library code was linted with cppcheck and is clean)exampleshierarchy (examples/Tests\_samples/Test\_MCViNE\_\*/)data/MCViNE/folder.My work touches / adds to the runtime lib code (.c,.h etc in multiple locations
MCViNEenum entry, union member and two dispatch cases🤖 Generated with Claude Code