# Can I stop the calculation midway

**URL:** <https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969>\
**Category:** User Support\
**Created:** [May 3, 2023, 5:08am UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969 "2023-05-03T05:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingyue](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/kingyue/32/507_2.png) [@kingyue](https://openmc.discourse.group/u/kingyue)\
**Post date:** [May 3, 2023, 5:08am UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969/1 "2023-05-03T05:08:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [May 3, 2023, 7:35am UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969/2 "2023-05-03T07:35:33Z")

</div>

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

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

```

[https://docs.openmc.org/en/stable/pythonapi/generated/openmc.Settings.html](https://docs.openmc.org/en/stable/pythonapi/generated/openmc.Settings.html)

---

<div class="post-metadata">

**Author:** ![kingyue](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/kingyue/32/507_2.png) [@kingyue](https://openmc.discourse.group/u/kingyue)\
**Post date:** [May 4, 2023, 3:52am UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969/3 "2023-05-04T03:52:53Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [May 4, 2023, 7:12am UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969/4 "2023-05-04T07:12:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [May 5, 2023, 4:14pm UTC](https://openmc.discourse.group/t/can-i-stop-the-calculation-midway/2969/5 "2023-05-05T16:14:22Z")

</div>

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](https://docs.openmc.org/en/stable/pythonapi/capi.html)

```python
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()

```
