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

Generated by: LCOV version 1.14