Repository: openmc-dev/openmc

Issue:
Tally with cell filter assumes the cells tallied to are mutually exclusive
If a tally is created with a cell filter, the bins (i.e. cells) are assumed to be mutually exclusive. For instance, we can modify the `examples/xml/lattice/simple/tallies.xml` file for a 4x4 lattice of pin cells to only have the following tally:

```
<tally id="1">
  <filter type="cell" bins="1 101" />
  <scores>total</scores>
</tally>
```

Where cell `1` is the cell filled with the lattice and cell `101` is a the fuel region of one of the pins. The `tallies.out` file would be:

```
============================>     TALLY 1     <============================

Cell 1
  Total Material
    Total Reaction Rate                  1.09346        +/- 6.47323E-03
Cell 101
  Total Material
    Total Reaction Rate                  0.0            +/- 0.0
```

The tallies over the two cells can be put into separate tallies as follows:

```
<tally id="1">
  <filter type="cell" bins="1" />
  <scores>total</scores>
</tally>
<tally id="2">
  <filter type="cell" bins="101" />
  <scores>total</scores>
</tally> 
```

Yielding the desired results:

```
============================>     TALLY 1     <============================

Cell 1
  Total Material
    Total Reaction Rate                  1.09346        +/- 6.47323E-03

============================>     TALLY 2     <============================

Cell 101
  Total Material
    Total Reaction Rate                  4.99603E-02    +/- 4.27083E-04
```

The openmc API will merge tallies with cell filters assuming that the cells are mutually exclusive so this issue should be addressed in both the Fortran code and Python API.

As far as I can see, the documentation does not indicate that bins for the cell filter must be mutually exclusive. Fixing this issue seems like it might be difficult and waiting for the tally module to be refactored could be the best option.



Discussion:
I agree that a fix for this might wait until the tally module has been
refactored...whenever that will be. To adjust the Python API to check for
this would require a large extension of the geometric capabilities in the
API; in particular, detection of overlapping cells in this case. Althoigh
this might be desirable, I think that a more realistic solution is to embed
the error checking in the back end.
On Oct 23, 2015 12:56 PM, "sam" notifications@github.com wrote:

> If a tally is created with a cell filter, the bins (i.e. cells) are
> assumed to be mutually exclusive. For instance, we can modify the
> examples/xml/lattice/simple/tallies.xml file for a 4x4 lattice of pin
> cells to only have the following tally:
> 
> <tally id="1">
>   <filter type="cell" bins="1 101" />
>   <scores>total</scores>
> </tally>
> 
> Where cell 1 is the cell filled with the lattice and cell 101 is a the
> fuel region of one of the pins. The tallies.out file would be:
> 
> ============================>     TALLY 1     <============================
> 
> Cell 1
>   Total Material
>     Total Reaction Rate                  1.09346        +/- 6.47323E-03
> Cell 101
>   Total Material
>     Total Reaction Rate                  0.0            +/- 0.0
> 
> The tallies over the two cells can be put into separate tallies as follows:
> 
> <tally id="1">
>   <filter type="cell" bins="1" />
>   <scores>total</scores>
> </tally>
> <tally id="2">
>   <filter type="cell" bins="101" />
>   <scores>total</scores>
> </tally>
> 
> Yielding the desired results:
> 
> ============================>     TALLY 1     <============================
> 
> Cell 1
>   Total Material
>     Total Reaction Rate                  1.09346        +/- 6.47323E-03
> 
> ============================>     TALLY 2     <============================
> 
> Cell 101
>   Total Material
>     Total Reaction Rate                  4.99603E-02    +/- 4.27083E-04
> 
> The openmc API will merge tallies with cell filters assuming that the
> cells are mutually exclusive so this issue should be addressed in both the
> Fortran code and Python API.
> 
> As far as I can see, the documentation does not indicate that bins for the
> cell filter must be mutually exclusive. Fixing this issue seems like it
> might be difficult and waiting for the tally module to be refactored could
> be the best option.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/mit-crpg/openmc/issues/484.

Yeah, this is going to take an algorithmic change that might require data structure changes as well, so I also agree that it makes sense to do this as part of a refactor.


Resolving patch:
diff --git a/openmc/filter.py b/openmc/filter.py
--- a/openmc/filter.py
+++ b/openmc/filter.py
@@ -162,6 +162,11 @@ def bins(self, bins):
         if not isinstance(bins, Iterable):
             bins = [bins]
 
+        # If the bin is 0D numpy array, promote to 1D
+        elif isinstance(bins, np.ndarray):
+            if bins.shape == ():
+                bins.shape = (1,)
+
         # If the bins are in a collection, convert it to a list
         else:
             bins = list(bins)


Test patch:
diff --git a/tests/test_score_current/results_true.dat b/tests/test_score_current/results_true.dat
--- a/tests/test_score_current/results_true.dat
+++ b/tests/test_score_current/results_true.dat
@@ -1 +1 @@
-a9310752363eb059ff40f16ac9716b41ccab6ec6607d29f498069318745e485d18d784264304cc2586865bd58cef7587203cc22a1d485c58ddd63c14c0defdb9
\ No newline at end of file
+bafab1921a12146abb2bb29603b52b9cc28a5a950a7a6bb1e3f012c05891c310fad643760d4f148b04d0fef3d1f3e141d146e3a278d81cc6fc8187c37717c5e7
\ No newline at end of file
diff --git a/tests/test_tallies/inputs_true.dat b/tests/test_tallies/inputs_true.dat
--- a/tests/test_tallies/inputs_true.dat
+++ b/tests/test_tallies/inputs_true.dat
@@ -1 +1 @@
-ca47172a42f6c13b244a763c990cbe4811662708ee03307d810a4542ee34bb5db7cc29d66aea313dad95b9f38a4ff7943ded527cfd0c7c8825372fec40cfc0d0
\ No newline at end of file
+930af242a043f2676a000dbc5a2db6b148edcb31ed8c87dbaa35a8efb37a3be8cff30cdf4dc03f9c5c7eb4021f7e4c3327e64681cdd8fd8722c95c69db850227
\ No newline at end of file
diff --git a/tests/test_tallies/results_true.dat b/tests/test_tallies/results_true.dat
--- a/tests/test_tallies/results_true.dat
+++ b/tests/test_tallies/results_true.dat
@@ -1 +1 @@
-7d085e38f331083a8c3f7814eb6025f9457a1a8e3899e7d9a65af1ba92ea75c0182fcc605d4eadd3eb7edfc0949df688d39a3f45683e10cbdcfb6d7954f34129
\ No newline at end of file
+a51db2a4efc681805f85968e04411dc33beee0532c202f5179b9a82880ab60a75e53fa9141c81045ea1d2842372f2d8da900326f09382ea61dd80a3c9b43bba1
\ No newline at end of file
diff --git a/tests/test_tallies/test_tallies.py b/tests/test_tallies/test_tallies.py
--- a/tests/test_tallies/test_tallies.py
+++ b/tests/test_tallies/test_tallies.py
@@ -113,10 +113,11 @@ def _build_inputs(self):
         polar_tally4.estimator = 'tracklength'
 
         universe_tally = Tally()
-        universe_tally.filters = [Filter(type='universe', bins=(1, 2, 3, 4))]
+        universe_tally.filters = [
+             Filter(type='universe', bins=(1, 2, 3, 4, 6, 8))]
         universe_tally.scores = ['total']
 
-        cell_filter = Filter(type='cell', bins=(10, 21, 22, 23))
+        cell_filter = Filter(type='cell', bins=(10, 21, 22, 23, 60))
         score_tallies = [Tally(), Tally(), Tally()]
         for t in score_tallies:
             t.filters = [cell_filter]
@@ -128,7 +129,7 @@ def _build_inputs(self):
         score_tallies[1].estimator = 'analog'
         score_tallies[2].estimator = 'collision'
 
-        cell_filter2 = Filter(type='cell', bins=(21, 22, 23, 27, 28, 29))
+        cell_filter2 = Filter(type='cell', bins=(21, 22, 23, 27, 28, 29, 60))
         flux_tallies = [Tally() for i in range(4)]
         for t in flux_tallies:
             t.filters = [cell_filter2]