Skip to content

Adding support for NCrystal materials in OpenMC - #2222

Merged
paulromano merged 47 commits into
openmc-dev:developfrom
highness-eu:mixed_ncrystal
Jan 12, 2023
Merged

paulromano merged 47 commits into
openmc-dev:developfrom
highness-eu:mixed_ncrystal

Conversation

@marquezj

Copy link
Copy Markdown
Contributor

After some discussion with @paulromano we cleaned up this version of OpenMC with support for NCrystal. NCrystal is a software library that provides low energy neutron physics to Monte Carlo codes. It was initially designed with Geant4 in mind, but it currently has interfaces with many other codes, and it can be called from Python, C and C++. NCrystal is developed mainly at the European Spallation Source. You can read more about NCrystal in these papers:

X.-X. Cai and T. Kittelmann, NCrystal: A library for thermal neutron
transport, Computer Physics Communications 246 (2020) 106851,
https://doi.org/10.1016/j.cpc.2019.07.015

T. Kittelmann and X.-X. Cai, Elastic neutron scattering models
for NCrystal, Computer Physics Communications 267 (2021) 108082,
https://doi.org/10.1016/j.cpc.2021.108082

X.-X. Cai, T. Kittelmann, et. al., "Rejection-based sampling of inelastic
neutron scattering", Journal of Computational Physics 380 (2019) 400-407,
https://doi.org/10.1016/j.jcp.2018.11.043

or watch the presentation from @tkittel, at ND2022:

https://indico.frib.msu.edu/event/52/contributions/954/

We have used NCrystal together with NJOY to generate a large library of new materials in ACE format (the NJOY-NCrystal library), but calling NCrystal directly allows to introduce in OpenMC additional physics that is not supported by the ACE format.

In particular, NCrystal allows to generate and sample on the fly scattering kernels for solid polycrystalline materials at any temperature. This is done at initialization, based on a human readable description stored in text form in the NCMAT format. For existing ENDF-6 scattering kernels, it allows the direct sampling of S(alpha, beta), which allows for continuous angular distributions (which are still not supported by the ACE format). NCrystal also support oriented materials (single crystals, highly-oriented pyrolytic graphite) and has plug-in support for other physics, currently being used to include small angle neutron scattering and inelastic magnetic scattering. All this would make OpenMC more useful to the neutron scattering community, while adding advanced nuclear data capabilities for the reactor community.

This pull request is planned as an optional feature that could be activated with cmake, using the OPENMC_USE_NCRYSTAL flag after installing and initializing NCrystal. A new method is added to the Material class in Python, to create materials from NCrystal, which takes the composition and density, creates the OpenMC material and associates it with NCrystal.

After reading the xml files, an NCrystal object is created in memory, which provides thermal scattering cross sections and scattering events (for the calculation of the scattering events, the direction of the neutron is a parameter to be able to consider oriented materials). When the thermal scattering cross section is computed, the nuclear elastic scattering component is removed, similarly to what is done in the standard S(alpha, beta) treatment.

There a couple of open topics that we could not decide, and are marked with "fixme" in the code. For instance, NCrystal uses a cache that could be implemented in OpenMC to reduce the computation time. Also, new tags for NCrystal events could be added for tallies. But, overall, we think it is ready for merging upstream.

@tkittel

tkittel commented Sep 14, 2022

Copy link
Copy Markdown
Contributor

I can add to what @marquezj wrote, that since last week NCrystal is also available on conda-forge (since @paulromano suggested this during our meeting).

@paulromano

Copy link
Copy Markdown
Contributor

Thanks for the PR @marquezj! Really excited to see this. Just wanted to give you a heads up I probably won't get to reviewing this until early next week.

Comment thread CMakeLists.txt Outdated
Comment thread src/physics.cpp Outdated
@tkittel

tkittel commented Sep 16, 2022

Copy link
Copy Markdown
Contributor

@paulromano I added comments concerning the two non-obvious fixmes in the PR. Let us know if you have any specific wishes concerning this (or anything else), and we will try to update the PR.

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marquezj @tkittel Thanks once again for this PR. This change really isn't very intrusive at all and I think with a little restructuring, we might be able to make it even cleaner. Here is my first round of comments, mostly just about naming/style conventions. I haven't had a chance yet to actually try this out but plan on doing so next week.

@ameliajo I was wondering if you might be interested in taking a look at this PR too as our resident thermal scattering expert 😄

Comment thread CMakeLists.txt Outdated
Comment thread CMakeLists.txt Outdated
Comment thread include/openmc/material.h Outdated
Comment thread include/openmc/material.h Outdated
Comment thread include/openmc/physics.h Outdated
Comment thread src/physics.cpp Outdated
Comment thread src/material.cpp Outdated
Comment thread src/physics.cpp Outdated
Comment thread src/physics.cpp Outdated
Comment thread src/physics.cpp Outdated
@tkittel

tkittel commented Sep 26, 2022

Copy link
Copy Markdown
Contributor

So @paulromano, unless I overlooked something I believe @marquezj took care of all the changes requested so far (including applying clang-format).

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @marquezj and @tkittel for the updates on this PR. In addition to the comments below, the other thing this PR needs in order to be considered for merging is tests/documentation. For testing, take a look at existing examples in the tests/regression_tests folder. At a minimum, we should have a test that utilizes this new capability and ensures that we get the expected answer. Along with that, we'll need some updates to the CI environment so that we can build against NCrystal -- you can look at tools/ci/gha-install-njoy.sh for an example of how we install NJOY. Adding something similar for NCrystal should be straightforward, and (correct me if I'm wrong) the build time for NCrystal shouldn't be prohibitive to add to CI.

I was going to ask for some example inputs using this so I can test it out, but I think the addition of a regression test will serve that purpose.

For documentation, the only thing that's absolutely required for now is to update the section of our user's guide mentioning optional dependencies.

Comment thread include/openmc/physics.h Outdated
//==============================================================================

class NCrystalRNGWrapper : public NCrystal::RNGStream {
uint64_t* openmc_seed_;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be in the private: section?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it technically is since the default access level of classes is private :-)

But we can certainly make an explicit private: section if you prefer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yes, we generally follow the C++ core guidelines when possible:
NL.16: Use a conventional class member declaration order

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was done now.

Comment thread CMakeLists.txt
@tkittel

tkittel commented Oct 19, 2022

Copy link
Copy Markdown
Contributor

FYI we haven't forgotten this, but some other work came in the way. We hope to spend some time on this on Friday.

@tkittel

tkittel commented Oct 20, 2022

Copy link
Copy Markdown
Contributor

I just pushed a few updates, mainly to use a new py API method I have just added in NCrystal 3.4.1, which helps getting the basic material composition in Python even for complicated (multiphase/enriched/...) materials. In the rare and somewhat academic case of materials where the same element now appears both as a natural element and as a explicit isotope, we are now using the openmc natural abundancies to break apart the natural element. This is like what we have been doing all along in Geant4 (with the C++ API), where we instead of course were asking Geant4 to provide the natural abundancies.

I also added material_id and name optional parameters on the Material.from_ncrystal method, which then simply pass these two parameters on to the Material constructor.

@tkittel

tkittel commented Oct 20, 2022

Copy link
Copy Markdown
Contributor

Outstanding issues are tests, documentation and CI as requested. We will have a look at those tomorrow.

@tkittel

tkittel commented Oct 24, 2022 •

Copy link
Copy Markdown
Contributor

@paulromano I added a tools/ci/gha-install-ncrystal.sh script now (can most likely be improved a bit in the future). Should we also update the gha-install.sh and gha-install.py files? Also, will it be possible to enable the workflows on the current PR, so we can try to get the new tests for NCrystal to work?

Update: I guess there will be a few more files to update, like perhaps gha-script.sh, ci.yml, ...). Do you prefer to take care of that, or should we try?

@marquezj
marquezj requested a review from paulromano November 29, 2022 12:08
@tkittel

tkittel commented Dec 1, 2022

Copy link
Copy Markdown
Contributor

@paulromano : Is it normal that some of the checks do not start? All the started ones have now passed.
Are there any remaining things we need to look at, or are you happy to merge this now?

Cheers,
Thomas

@paulromano

Copy link
Copy Markdown
Contributor

@tkittel No, you don't need to worry about that. GitHub is expecting a set of test configurations that does not include the new ncrystal option, so that's why those extra ones are shown. Once this is merged, we'll just change the list of expected test configurations to include the ones with ncrystal included.

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tkittel and @marquezj for the updates! It's looking much better than where it started. Here's another round of comments. I think we're getting pretty close at this point.

Comment thread CMakeLists.txt Outdated
Comment thread docs/source/usersguide/materials.rst Outdated
Comment thread docs/source/usersguide/materials.rst Outdated
Comment thread docs/source/usersguide/materials.rst Outdated
Comment thread docs/source/usersguide/materials.rst Outdated
Comment thread tests/regression_tests/ncrystal/test.py Outdated
Comment thread tests/regression_tests/ncrystal/test.py Outdated
Comment thread tests/regression_tests/ncrystal/test.py Outdated
Comment thread tools/ci/gha-install.py Outdated
Comment thread tools/ci/gha-script.sh Outdated
@tkittel

tkittel commented Dec 7, 2022

Copy link
Copy Markdown
Contributor

Thanks @paulromano , all changes committed as requested.

Anything else? Let me know if you want me to do a rebase of the branch on main now, or do any squashing or whatever :-)

@marquezj
marquezj requested a review from paulromano December 10, 2022 09:55
@tkittel

tkittel commented Jan 6, 2023

Copy link
Copy Markdown
Contributor

Hi @paulromano

Despite the status saying "1 change requested", I think we did everything. Let us know if you think more is needed 😄

Cheers,
Thomas

@paulromano

Copy link
Copy Markdown
Contributor

@tkittel Apologies this fell off my radar -- will take another look soon!

Merge develop branch to resolve conflicts
@tkittel
tkittel requested a review from pshriwise as a code owner January 10, 2023 07:52
@paulromano

Copy link
Copy Markdown
Contributor

@tkittel One final question -- should ncrystal_max_energy be configurable? Right now it defaults to 5 eV and it doesn't appear that there's any way to change this.

@tkittel

tkittel commented Jan 10, 2023

Copy link
Copy Markdown
Contributor

I would postpone that for another time, since there is a common issue here with e.g. the Geant4 hooks. Once we eventually look into it, it might anyway be that we go for another solution altogether for this threshold (e.g. a per-material threshold rather than a global one).

@paulromano

Copy link
Copy Markdown
Contributor

Ok, in that case let's change it to be a global constant as opposed to a global variable in the settings:: namespace. I'll send a quick PR to your branch updating that momentarily.

Change ncrystal_max_energy to be a global constant

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited to have this merged in -- thanks @tkittel @marquezj for this great contribution!

@paulromano
paulromano merged commit a62bb50 into openmc-dev:develop Jan 12, 2023
@tkittel

tkittel commented Jan 12, 2023

Copy link
Copy Markdown
Contributor

Very exciting to us as well, thanks a lot @paulromano for the support and shepherding! 😄

apingegno pushed a commit to apingegno/openmc that referenced this pull request May 7, 2026
Adding support for NCrystal materials in OpenMC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants