Line data Source code
1 : /********************************************************************/ 2 : /* SOFTWARE COPYRIGHT NOTIFICATION */ 3 : /* Cardinal */ 4 : /* */ 5 : /* (c) 2021 UChicago Argonne, LLC */ 6 : /* ALL RIGHTS RESERVED */ 7 : /* */ 8 : /* Prepared by UChicago Argonne, LLC */ 9 : /* Under Contract No. DE-AC02-06CH11357 */ 10 : /* With the U. S. Department of Energy */ 11 : /* */ 12 : /* Prepared by Battelle Energy Alliance, LLC */ 13 : /* Under Contract No. DE-AC07-05ID14517 */ 14 : /* With the U. S. Department of Energy */ 15 : /* */ 16 : /* See LICENSE for full restrictions */ 17 : /********************************************************************/ 18 : 19 : #ifdef ENABLE_OPENMC_COUPLING 20 : 21 : #include "OpenMCCellMaterialFill.h" 22 : #include "UserErrorChecking.h" 23 : #include "openmc/cell.h" 24 : 25 : registerMooseObject("CardinalApp", OpenMCCellMaterialFill); 26 : 27 : InputParameters 28 76 : OpenMCCellMaterialFill::validParams() 29 : { 30 76 : auto params = ModelModifiersBase::validParams(); 31 152 : params.addRequiredParam<int32_t>("cell_id", "Cell ID to modify"); 32 152 : params.addParam<std::vector<int32_t>>( 33 : "material_ids", 34 : "IDs of the materials to fill into the cell; the length of this array can be one material ID " 35 : "that will be applied to all instances. Otherwise, it must match the number " 36 : "of instances of the cell"); 37 152 : params.addParam<std::string>("material_ids_file", 38 : "Filename containing material ids to assign to the cell instances. " 39 : "Must be an HDF5 (*.h5) file."); 40 76 : params.addClassDescription("Modifies the cell fill within an OpenMC cell"); 41 76 : return params; 42 0 : } 43 : 44 42 : OpenMCCellMaterialFill::OpenMCCellMaterialFill(const InputParameters & parameters) 45 84 : : ModelModifiersBase(parameters), _cell_id(getParam<int32_t>("cell_id")) 46 : { 47 : // check that only one of material_ids and material_ids_file is specified 48 42 : const bool material_ids_valid = isParamValid("material_ids"); 49 42 : const bool material_ids_file_valid = isParamValid("material_ids_file"); 50 42 : if (material_ids_valid == material_ids_file_valid) 51 2 : mooseError("Only one of 'material_ids' or 'material_ids_file' can be provided; you have " 52 : "specified both or none."); 53 : 54 : // get the cell requested 55 40 : _cell_index = -1; 56 40 : int err = openmc_get_cell_index(_cell_id, &_cell_index); 57 40 : if (err) 58 2 : paramError("cell_id", 59 2 : "Cell ID " + std::to_string(_cell_id) + " was not found in the OpenMC model!"); 60 : 61 38 : auto n_cell_instances = openmc::model::cells[_cell_index]->n_instances(); 62 38 : if (n_cell_instances == 0) 63 2 : paramError("cell_id", 64 2 : "Cell ID " + std::to_string(_cell_id) + 65 : " has zero instances so its material fill cannot be modified. This cell is " 66 : "probably corresponding to a cell in a lattice.outer universe. If you want to " 67 : "change the material here, you will need to either (i) widen your lattice to " 68 : "have universes covering all of the space you want to change the material or " 69 : "(ii) represent this region of space as a conventional cell.\n\nFor more " 70 : "information, see: https://github.com/openmc-dev/openmc/issues/551."); 71 : 72 : std::vector<int32_t> material_ids; 73 : // check that the length of the materials matches the number of instances 74 36 : if (material_ids_valid) 75 84 : material_ids = getParam<std::vector<int32_t>>("material_ids"); 76 : else 77 24 : extractMaterialIdsFromFile(getParam<std::string>("material_ids_file"), material_ids); 78 : 79 : auto n_mats = std::size(material_ids); 80 36 : if (n_mats != 1 && (n_cell_instances != n_mats)) 81 2 : paramError("material_ids", 82 2 : "'material_ids' (length " + std::to_string(n_mats) + 83 : ") must either be of length 1 or have a length equal to the number of instances " 84 2 : "of cell " + 85 4 : std::to_string(_cell_id) + " (" + std::to_string(n_cell_instances) + ")"); 86 : 87 : // check that each provided material exists in the model 88 34 : int32_t material_index = -1; 89 118 : for (const auto & m : material_ids) 90 : { 91 86 : err = openmc_get_material_index(m, &material_index); 92 86 : if (err) 93 2 : paramError("material_ids", 94 2 : "Material with ID " + std::to_string(m) + " was not found in the OpenMC model!"); 95 84 : _material_indices.push_back(material_index); 96 : } 97 : 98 32 : if (n_mats == 1) 99 : { 100 : // make _material_indices equal in length to the number of cell instances 101 8 : _material_indices.assign(n_cell_instances, material_index); 102 : } 103 32 : } 104 : 105 : void 106 8 : OpenMCCellMaterialFill::extractMaterialIdsFromFile(std::string filename, 107 : std::vector<int32_t> & material_ids) 108 : { 109 : // confirm that filename specified exists 110 8 : MooseUtils::checkFileReadable(filename); 111 : 112 : // extract the material_fill_ids attribute from the file 113 8 : hid_t file = openmc::file_open(filename, 'r'); 114 8 : openmc::read_dataset(file, "material_fill_ids", material_ids); 115 8 : openmc::file_close(file); 116 8 : } 117 : 118 : void 119 32 : OpenMCCellMaterialFill::modifyOpenMCModel() const 120 : { 121 : const auto n_mats = std::size(_material_indices); 122 32 : std::string m = n_mats > 1 ? "materials" : "material"; 123 32 : _console << "Modifying " << m << " in cell " << _cell_id << "..." << std::endl; 124 : 125 32 : int err = openmc_cell_set_fill(_cell_index, 0, n_mats, _material_indices.data()); 126 64 : catchOpenMCError(err, "attempting to set a material fill in cell " + std::to_string(_cell_id)); 127 32 : } 128 : 129 : #endif