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 "TallyRelativeError.h" 22 : #include "UserErrorChecking.h" 23 : #include "xtensor/xview.hpp" 24 : 25 : registerMooseObject("CardinalApp", TallyRelativeError); 26 : 27 : registerMooseObjectRenamed("CardinalApp", 28 : FissionTallyRelativeError, 29 : "03/01/2023 24:00", 30 : TallyRelativeError); 31 : 32 : InputParameters 33 1090 : TallyRelativeError::validParams() 34 : { 35 1090 : InputParameters params = GeneralPostprocessor::validParams(); 36 1090 : params += OpenMCBase::validParams(); 37 2180 : params.addParam<MooseEnum>("value_type", 38 2180 : getOperationEnum(), 39 : "Whether to give the maximum or minimum tally relative error"); 40 : 41 2180 : params.addParam<MooseEnum>("tally_score", 42 2180 : getSingleTallyScoreEnum(), 43 : "Score to report the relative error. If there is just a single score, " 44 : "this defaults to that value"); 45 1090 : params.addClassDescription("Maximum/minimum tally relative error"); 46 1090 : return params; 47 0 : } 48 : 49 350 : TallyRelativeError::TallyRelativeError(const InputParameters & parameters) 50 : : GeneralPostprocessor(parameters), 51 : OpenMCBase(this, parameters), 52 698 : _type(getParam<MooseEnum>("value_type").getEnum<operation::OperationEnum>()) 53 : { 54 696 : if (isParamValid("tally_score")) 55 : { 56 236 : const auto & tally_score = getParam<MooseEnum>("tally_score"); 57 236 : _score = _openmc_problem->enumToTallyScore(tally_score); 58 : 59 236 : if (!_openmc_problem->hasScore(_score)) 60 4 : paramError( 61 : "tally_score", 62 2 : "To extract the relative error of the '" + std::string(tally_score) + 63 : "' score, it must be included in one of the [Tallies] added in your input file!"); 64 : } 65 : else 66 : { 67 112 : if (_openmc_problem->getTallyScores().size() != 1 && !isParamValid("tally_score")) 68 0 : paramError("tally_score", 69 : "When multiple scores have been added by tally objects, you must specify a score " 70 : "from which the relative error will be extracted."); 71 : 72 224 : for (const auto & s : _openmc_problem->getTallyScores()) 73 112 : _console << s << std::endl; 74 : 75 112 : _score = _openmc_problem->getTallyScores()[0]; 76 : } 77 346 : } 78 : 79 : Real 80 438 : TallyRelativeError::getValue() const 81 : { 82 : Real post_processor_value; 83 438 : switch (_type) 84 : { 85 322 : case operation::max: 86 322 : post_processor_value = std::numeric_limits<Real>::min(); 87 322 : break; 88 58 : case operation::min: 89 58 : post_processor_value = std::numeric_limits<Real>::max(); 90 58 : break; 91 58 : case operation::average: 92 58 : post_processor_value = 0.0; 93 58 : break; 94 0 : default: 95 0 : mooseError("Unhandled OperationEnum!"); 96 : } 97 : 98 : unsigned int num_values = 0; 99 940 : for (const auto tally : _openmc_problem->getTalliesByScore(_score)) 100 : { 101 502 : const auto t = tally->getWrappedTally(); 102 502 : auto sum = xt::view(t->results_, 103 502 : xt::all(), 104 502 : tally->scoreIndex(_score), 105 502 : static_cast<int>(openmc::TallyResult::SUM)); 106 : auto sum_sq = xt::view(t->results_, 107 502 : xt::all(), 108 502 : tally->scoreIndex(_score), 109 502 : static_cast<int>(openmc::TallyResult::SUM_SQ)); 110 : 111 502 : auto rel_err = _openmc_problem->relativeError(sum, sum_sq, t->n_realizations_); 112 833864 : for (int i = 0; i < t->n_filter_bins(); ++i) 113 : { 114 : // tallies without any scores to them will have zero error, which doesn't really make 115 : // sense to compare against 116 833362 : if (MooseUtils::absoluteFuzzyEqual(sum(i), 0)) 117 81670 : continue; 118 : 119 751692 : switch (_type) 120 : { 121 294036 : case operation::max: 122 294036 : post_processor_value = std::max(post_processor_value, rel_err[i]); 123 294036 : break; 124 228828 : case operation::min: 125 228828 : post_processor_value = std::min(post_processor_value, rel_err[i]); 126 228828 : break; 127 228828 : case operation::average: 128 228828 : post_processor_value += rel_err[i]; 129 228828 : num_values++; 130 228828 : break; 131 0 : default: 132 0 : mooseError("Unhandled OperationEnum!"); 133 : } 134 : } 135 502 : } 136 : 137 438 : if (_type == operation::average) 138 58 : post_processor_value /= num_values; 139 : 140 438 : return post_processor_value; 141 : } 142 : 143 : #endif