LCOV - code coverage report
Current view: top level - src/actions - AddTallyAction.C (source / functions) Hit Total Coverage
Test: neams-th-coe/cardinal: be601f Lines: 58 65 89.2 %
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) 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             : #include "AddTallyAction.h"
      21             : 
      22             : #include "OpenMCCellAverageProblem.h"
      23             : #include "DelimitedFileReader.h"
      24             : 
      25             : registerMooseAction("CardinalApp", AddTallyAction, "add_tallies");
      26             : 
      27             : InputParameters
      28        2036 : AddTallyAction::validParams()
      29             : {
      30        2036 :   auto params = MooseObjectAction::validParams();
      31        2036 :   params.addClassDescription("Adds tally(s) for use in simulations containing an "
      32             :                              "OpenMCCellAverageProblem.");
      33             :   /**
      34             :    * These params are used to add multiple mesh tallies that use the same mesh but are
      35             :    * translated through the domain.
      36             :    */
      37        4072 :   params.addParam<std::vector<Point>>(
      38             :       "mesh_translations",
      39             :       "Coordinates to which each mesh template should be "
      40             :       "translated, if multiple unstructured meshes "
      41             :       "are desired. Units must match those used to define the [Mesh].");
      42        4072 :   params.addParam<std::vector<FileName>>(
      43             :       "mesh_translations_file",
      44             :       "File providing the coordinates to which each mesh "
      45             :       "template should be translated, if multiple "
      46             :       "unstructured meshes are desired. Units must match those used to define the [Mesh]");
      47             : 
      48        2036 :   return params;
      49           0 : }
      50             : 
      51        2036 : AddTallyAction::AddTallyAction(const InputParameters & parameters) : MooseObjectAction(parameters)
      52             : {
      53        2036 :   if (_type == "MeshTally")
      54             :   {
      55        1148 :     if (isParamValid("mesh_translations") && isParamValid("mesh_translations_file"))
      56           0 :       mooseError("Both 'mesh_translations' and 'mesh_translations_file' cannot be specified");
      57             : 
      58         364 :     fillMeshTranslations();
      59             :   }
      60             :   else
      61             :   {
      62        6688 :     if (isParamValid("mesh_translations") || isParamValid("mesh_translations_file"))
      63           0 :       mooseError("Mesh translations only apply to mesh-based tallies. 'mesh_translations' / "
      64             :                  "'mesh_translations_file' cannot be specified");
      65             :   }
      66        2034 : }
      67             : 
      68             : void
      69        1932 : AddTallyAction::act()
      70             : {
      71        1932 :   if (_current_task == "add_tallies")
      72             :   {
      73        1932 :     if (_type == "MeshTally")
      74        1059 :       for (unsigned int i = 0; i < _mesh_translations.size(); ++i)
      75         714 :         addMeshTally(i, _mesh_translations[i]);
      76             :     else
      77        1574 :       addTally();
      78             :   }
      79        1889 : }
      80             : 
      81             : void
      82         714 : AddTallyAction::addMeshTally(unsigned int instance, const Point & translation)
      83             : {
      84         714 :   auto openmc_problem = dynamic_cast<OpenMCCellAverageProblem *>(_problem.get());
      85             : 
      86         714 :   if (!openmc_problem)
      87           0 :     mooseError("The simulation must use an OpenMCCellAverageProblem when using the tally system!");
      88             : 
      89         714 :   std::string obj_name = _name;
      90         714 :   if (_mesh_translations.size() > 1)
      91             :   {
      92         534 :     obj_name += "_" + Moose::stringify(instance);
      93         534 :     _moose_object_pars.set<unsigned int>("instance") = instance;
      94         534 :     _moose_object_pars.set<Point>("mesh_translation") = translation * openmc_problem->scaling();
      95             :   }
      96             : 
      97         714 :   _moose_object_pars.set<OpenMCCellAverageProblem *>("_openmc_problem") = openmc_problem;
      98         714 :   openmc_problem->addTally(_type, obj_name, _moose_object_pars);
      99         701 : }
     100             : 
     101             : void
     102        1574 : AddTallyAction::addTally()
     103             : {
     104        1574 :   auto openmc_problem = dynamic_cast<OpenMCCellAverageProblem *>(_problem.get());
     105             : 
     106        1574 :   if (!openmc_problem)
     107           0 :     mooseError("The simulation must use an OpenMCCellAverageProblem when using the tally system!");
     108             : 
     109        1574 :   _moose_object_pars.set<OpenMCCellAverageProblem *>("_openmc_problem") = openmc_problem;
     110        1574 :   openmc_problem->addTally(_type, _name, _moose_object_pars);
     111        1544 : }
     112             : 
     113             : void
     114         364 : AddTallyAction::fillMeshTranslations()
     115             : {
     116         728 :   if (isParamValid("mesh_translations"))
     117             :   {
     118         630 :     _mesh_translations = getParam<std::vector<Point>>("mesh_translations");
     119         210 :     if (_mesh_translations.empty())
     120           0 :       mooseError("mesh_translations cannot be empty!");
     121             :   }
     122         308 :   else if (isParamValid("mesh_translations_file"))
     123             :   {
     124             :     std::vector<FileName> mesh_translations_file =
     125          54 :         getParam<std::vector<FileName>>("mesh_translations_file");
     126          18 :     if (mesh_translations_file.empty())
     127           0 :       mooseError("mesh_translations_file cannot be empty!");
     128             : 
     129          34 :     for (const auto & f : mesh_translations_file)
     130             :     {
     131          18 :       MooseUtils::DelimitedFileReader file(f, &_communicator);
     132             :       file.setFormatFlag(MooseUtils::DelimitedFileReader::FormatFlag::ROWS);
     133          18 :       file.read();
     134             : 
     135          18 :       const std::vector<std::vector<double>> & data = file.getData();
     136          18 :       readMeshTranslations(data);
     137          16 :     }
     138          16 :   }
     139             :   else
     140         136 :     _mesh_translations = {Point(0.0, 0.0, 0.0)};
     141         362 : }
     142             : 
     143             : void
     144          18 : AddTallyAction::readMeshTranslations(const std::vector<std::vector<double>> & data)
     145             : {
     146          66 :   for (const auto & d : data)
     147             :   {
     148          50 :     if (d.size() != OpenMCCellAverageProblem::DIMENSION)
     149           2 :       paramError("mesh_translations_file",
     150             :                  "All entries in 'mesh_translations_file' "
     151             :                  "must contain exactly ",
     152             :                  OpenMCCellAverageProblem::DIMENSION,
     153             :                  " coordinates.");
     154             : 
     155             :     // OpenMCCellAverageProblem::DIMENSION will always be 3
     156          48 :     _mesh_translations.push_back(Point(d[0], d[1], d[2]));
     157             :   }
     158          16 : }
     159             : #endif

Generated by: LCOV version 1.14