LCOV - code coverage report
Current view: top level - src/userobjects - OpenMCTallyEditor.C (source / functions) Hit Total Coverage
Test: neams-th-coe/cardinal: 93e9c4 Lines: 63 66 95.5 %
Date: 2026-07-16 12:06:10 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /********************************************************************/
       2             : /*                  SOFTWARE COPYRIGHT NOTIFICATION                 */
       3             : /*                             Cardinal                             */
       4             : /*                                                                  */
       5             : /*                  (c) 2024 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 "OpenMCTallyEditor.h"
      22             : #include "openmc/tallies/tally.h"
      23             : 
      24             : registerMooseObject("CardinalApp", OpenMCTallyEditor);
      25             : 
      26             : InputParameters
      27         126 : OpenMCTallyEditor::validParams()
      28             : {
      29         126 :   InputParameters params = GeneralUserObject::validParams();
      30         126 :   params += OpenMCBase::validParams();
      31         252 :   params.addParam<bool>("create_tally", false, "Whether to create the tally if it doesn't exist");
      32         252 :   params.addRequiredParam<int32_t>("tally_id", "The ID of the tally to modify");
      33         252 :   params.addRequiredParam<std::vector<std::string>>("scores", "The scores to apply in the tally");
      34         252 :   params.addRequiredParam<std::vector<std::string>>("nuclides",
      35             :                                                     "The nuclides to apply in the tally");
      36         252 :   params.addRequiredParam<std::vector<std::string>>("filter_ids",
      37             :                                                     "The filter IDs to apply in the tally");
      38         378 :   params.addParam<bool>("multiply_density",
      39         252 :                         true,
      40             :                         "Whether to multiply the tally by the current material's atom density");
      41         252 :   params.declareControllable("scores");
      42         252 :   params.declareControllable("nuclides");
      43         252 :   params.declareControllable("filter_ids");
      44         252 :   params.declareControllable("multiply_density");
      45         126 :   params.addClassDescription("A UserObject for creating and managing OpenMC tallies");
      46         126 :   return params;
      47           0 : }
      48             : 
      49          62 : OpenMCTallyEditor::OpenMCTallyEditor(const InputParameters & parameters)
      50             :   : GeneralUserObject(parameters),
      51             :     OpenMCBase(this, parameters),
      52         124 :     _tally_id(getParam<int32_t>("tally_id"))
      53             : {
      54          62 :   if (_openmc_problem->runRandomRay())
      55           2 :     mooseError("OpenMCTallyEditor is not supported when running the random ray solver!");
      56             : 
      57         120 :   bool create_tally = getParam<bool>("create_tally");
      58             : 
      59          60 :   if (create_tally)
      60             :   {
      61          44 :     if (tallyExists())
      62           4 :       paramWarning("tally_id",
      63           4 :                    "Tally " + std::to_string(_tally_id) +
      64             :                        " already exists in the OpenMC model and will be overriden"
      65             :                        " by this UserObject");
      66             :     else
      67             :     {
      68          42 :       _console << "Creating tally " << _tally_id << std::endl;
      69          42 :       openmc::Tally::create(_tally_id);
      70             :     }
      71             :   }
      72          60 : }
      73             : 
      74             : bool
      75         146 : OpenMCTallyEditor::tallyExists() const
      76             : {
      77         146 :   return openmc::model::tally_map.find(_tally_id) != openmc::model::tally_map.end();
      78             : }
      79             : 
      80             : int32_t
      81         100 : OpenMCTallyEditor::tallyIndex() const
      82             : {
      83         100 :   return openmc::model::tally_map.at(_tally_id);
      84             : }
      85             : 
      86             : void
      87         102 : OpenMCTallyEditor::execute()
      88             : {
      89         102 :   if (!tallyExists())
      90           2 :     paramError("tally_id",
      91           2 :                "Tally " + std::to_string(_tally_id) + " does not exist in the OpenMC model");
      92             : 
      93         100 :   openmc::Tally * tally = openmc::model::tallies[tallyIndex()].get();
      94             : 
      95         300 :   std::vector<std::string> scores = getParam<std::vector<std::string>>("scores");
      96         100 :   if (scores.size() > 0)
      97             :   {
      98             :     try
      99             :     {
     100          82 :       tally->set_scores(scores);
     101             :     }
     102           2 :     catch (const std::exception & e)
     103             :     {
     104           2 :       std::string s = e.what();
     105           2 :       paramError("scores", "In attempting to set tally scores, OpenMC reported:\n\n" + s);
     106           0 :     }
     107             :   }
     108             : 
     109         294 :   std::vector<std::string> nuclides = getParam<std::vector<std::string>>("nuclides");
     110          98 :   if (nuclides.size() > 0)
     111             :   {
     112             :     try
     113             :     {
     114          34 :       tally->set_nuclides(nuclides);
     115             :     }
     116           2 :     catch (const std::exception & e)
     117             :     {
     118           2 :       std::string s = e.what();
     119           2 :       paramError("nuclides", "In attempting to set tally nuclides, OpenMC reported:\n\n" + s);
     120           0 :     }
     121             :   }
     122             : 
     123         288 :   std::vector<std::string> filter_ids = getParam<std::vector<std::string>>("filter_ids");
     124          96 :   if (filter_ids.size() > 0)
     125             :   {
     126          48 :     tally->set_filters({});
     127          96 :     for (const auto & fid : filter_ids)
     128             :     {
     129          48 :       int32_t filter_index = openmc::model::filter_map.at(std::stoi(fid));
     130          48 :       tally->add_filter(openmc::model::tally_filters[filter_index].get());
     131             :     }
     132             :   }
     133             : 
     134         192 :   tally->set_multiply_density(getParam<bool>("multiply_density"));
     135          96 : }
     136             : 
     137             : void
     138           2 : OpenMCTallyEditor::duplicateTallyError(const int32_t & id) const
     139             : {
     140           2 :   paramError("tally_id",
     141           2 :              "Tally ID (" + std::to_string(id) + ") found in multiple OpenMCTallyEditors");
     142             : }
     143             : 
     144             : void
     145           2 : OpenMCTallyEditor::mappedTallyError(const int32_t & id) const
     146             : {
     147           2 :   paramError(
     148             :       "tally_id",
     149           2 :       "Tally ID " + std::to_string(id) +
     150             :           " is a tally which Cardinal has automatically created and is controlling from the "
     151             :           "Problem/Tallies block. OpenMCTallyEditor cannot be used for these types of tallies.");
     152             : }
     153             : 
     154             : #endif

Generated by: LCOV version 1.14