Can I stop the calculation midway

For example, I set 100 batches. Can I make OpenMC stop and write results to file when it have only ran 50 batches?

Yep you can specify particular batches using the settings.statepoint attribute

It accepts a dictionary and one of the keys can be batches and the value should be a list of batches

my_settings = openmc.Settings()
my_settings.stationpoint = {'batches': [50]}

https://docs.openmc.org/en/stable/pythonapi/generated/openmc.Settings.html

Thanks. I did use this attribute. But sometimes I want to stop at a point which I didn’t set in settings.xml. It seems not available.
PS: I found we should add the final batch in this attribute or OpenMC won’t record it after finishing the whole calculation.

my_settings = openmc.Settings()
my_settings.stationpoint = {'batches': [50,100]}
1 Like

Nice find. Perhaps the openmc lib part of the code is helpful here. It allows you to simulate batch by batch and check conditions in-between batches.

I can post and example soon

1 Like

There are lots of handy functions like next_batch and openmc.lib.tallies. This example below runs two batches with a statepoint write in between
https://docs.openmc.org/en/stable/pythonapi/capi.html

import openmc.lib

model.export_to_xml()

with openmc.lib.run_in_memory():

    openmc.lib.reset()
    
    # first run
    openmc.lib.run()

    # write the 1st statepoint
    openmc.lib.statepoint_write()

    # second run
    openmc.lib.run()