openmc icon indicating copy to clipboard operation
openmc copied to clipboard

Using openmc.lib.statepoint_write throws a HDF5 error when trying to write source_bank in fixed source calculations

Open ecasglez opened this issue 9 months ago • 1 comments

Bug Description

I am running a fixed source calculation using openmc.lib to control the simulation such as the one in this simple example:


import openmc
import openmc.lib

all_materials = openmc.Materials()

mat_nitrogen = openmc.Material(name='Air')
mat_nitrogen.add_nuclide('N14', 1.0, 'wo')
mat_nitrogen.set_density('g/cm3', 1.205E-50)
mat_nitrogen.temperature = 293

all_materials.append(mat_nitrogen)


#######################
# Surface definitions #
#######################

surf_sphe_chamber = openmc.Sphere(r=1000, boundary_type='vacuum')


####################
# Cells definition #
####################

cell_chamber = openmc.Cell(name='Sphere surrounding the source')
cell_chamber.region = -surf_sphe_chamber
cell_chamber.fill = mat_nitrogen

#############################
# Final geometry definition #
#############################

root_universe = openmc.Universe(cells=[cell_chamber])
geom = openmc.Geometry()
geom.root_universe = root_universe

###########################
# Fixed source definition #
###########################

src = openmc.IndependentSource()
src.space = openmc.stats.Point((0,0,0))

########################
# Calculation settings #
########################

settings = openmc.Settings()
settings.source = src
settings.batches = 10
settings.particles = 10000
settings.run_mode = 'fixed source'
settings.temperature = {'method': 'interpolation'}

###########
# Tallies #
###########

tallies = openmc.Tallies()

t1 = openmc.Tally()
cell_filter = openmc.CellFilter(cell_chamber)
t1.filters = [cell_filter]
t1.scores = ['flux']
tallies.append(t1)


##############
# Run OpenMC #
##############

model = openmc.Model(geom, all_materials, settings, tallies)

model.export_to_xml()  # this is necessary as openmc.lib loads up the model.xml file

with openmc.lib.run_in_memory():
    openmc.lib.run()
    # THE FOLLOWING LINE FAILS
    openmc.lib.statepoint_write(filename='statepoint_simulation.h5')

The simulation runs well, and writes the normal statepoint file properly. However, if I request the code to write the statepoint using openmc.lib.statepoint_write (see last line of the example), the following error arises:

 Creating state point statepoint_simulation.h5...
HDF5-DIAG: Error detected in HDF5 (1.10.10) thread 1:
  #000: ../../../src/H5Dio.c line 316 in H5Dwrite(): can't write data
    major: Dataset
    minor: Write failed
  #001: ../../../src/H5Dio.c line 713 in H5D__write(): no output buffer
    major: Invalid arguments to routine
    minor: Bad value

I have gone through file state_point.cpp, and it is related to calling function write_source_bank at:

  // Write the source bank if desired
  if (write_source_) {
    if (mpi::master || parallel)
      file_id = file_open(filename_, 'a', true);
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
    if (mpi::master || parallel)
      file_close(file_id);
  }

When writing the normal statepoint automatically after executing the code, variable write_source_ is false. However, when I manually generate the statepoint, write_source_ is true, going into function write_source_bank.

In this function, it is executing the following line:

      // Write data to hyperslab
      H5Dwrite(
        dset, banktype, memspace, dspace, H5P_DEFAULT, source_bank.data());

I think the problem is in source_data. It seems empty, thus there is nothing to write:

(gdb) p source_bank.data()
$1 = (openmc::SourceSite *) 0x0
(gdb) p source_bank
$2 = {data_ = 0x0, size_ = 0}

Does it make sense to write source_bank in fixed source calculations?

Steps to Reproduce

Run the input file included before

Environment

I have tried OpenMC 0.15.0 and 0.15.2 compiled from sources in a Ubuntu Linux distribution

ecasglez avatar Apr 21 '25 11:04 ecasglez

I just found that openmc.lib.statepoint_write has an argument to control this:

openmc.lib.statepoint_write(filename='statepoint_simulation.h5', write_source=False)

The default value of write_source is True. I think a user-friendly warning should be included in OpenMC when this happens other than an HDF5 error message, or set this to False by default for fixed_source calculations only.

I have prepared the following patch that sets this to false when using fixed_source calculations, and keeps the usual behavior for eiganvalue calculations:

diff --git a/src/state_point.cpp b/src/state_point.cpp
index 3b822715c..d82773919 100644
--- a/src/state_point.cpp
+++ b/src/state_point.cpp
@@ -58,7 +58,10 @@ extern "C" int openmc_statepoint_write(const char* filename, bool* write_source)
   }

   // Determine whether or not to write the source bank
-  bool write_source_ = write_source ? *write_source : true;
+  bool write_source_ = false;
+  if (settings::run_mode == RunMode::EIGENVALUE) {
+    bool write_source_ = write_source ? *write_source : true;
+  }

   // Write message
   write_message("Creating state point " + filename_ + "...", 5);

I can submit a pull request if you think this is the right approach. The documentation should also be updated to reflect this new behavior

ecasglez avatar Apr 22 '25 10:04 ecasglez