Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "InputParameters.h" 13 : #include "MooseObject.h" 14 : 15 : /** 16 : * Interface class for producing errors, warnings, or just quiet NaNs 17 : * 18 : * For some objects it is desirable to continue running despite generation of 19 : * NaN(s). This class provides an interface for choosing whether to throw an 20 : * error, a warning, or nothing at all, just using a quiet NaN. 21 : */ 22 : class NaNInterface 23 : { 24 : public: 25 : static InputParameters validParams(); 26 : 27 : NaNInterface(const MooseObject * moose_object); 28 : 29 : protected: 30 : enum NaNMessage 31 : { 32 : NAN_MESSAGE_NONE = 0, 33 : NAN_MESSAGE_WARNING = 1, 34 : NAN_MESSAGE_EXCEPTION = 2, 35 : NAN_MESSAGE_ERROR = 3 36 : }; 37 : 38 : const MooseObject * const _moose_object; 39 : 40 : /// Raise mooseWarning or mooseError? 41 : const enum NaNMessage _emit_on_nan; 42 : 43 : /** 44 : * Throws an error or returns a NaN with or without a warning, with a default message 45 : */ 46 122 : Real getNaN() const { return getNaN("A NaN was produced."); } 47 : 48 : /** 49 : * Throws an error or returns NaNs with or without a warning, with a default message 50 : * 51 : * @param[in] n Vector size 52 : */ 53 : std::vector<Real> getNaNVector(const unsigned int & n) const 54 : { 55 0 : return getNaNVector(n, "A NaN was produced."); 56 : } 57 : 58 : /** 59 : * Throws an error or returns a NaN with or without a warning 60 : */ 61 : template <typename... Args> 62 122 : Real getNaN(Args &&... args) const 63 : { 64 122 : switch (_emit_on_nan) 65 : { 66 20 : case (NAN_MESSAGE_WARNING): 67 20 : mooseWarning(_moose_object->name(), ": ", std::forward<Args>(args)...); 68 : break; 69 82 : case (NAN_MESSAGE_EXCEPTION): 70 82 : mooseException(_moose_object->name(), ": ", std::forward<Args>(args)...); 71 : break; 72 0 : case (NAN_MESSAGE_ERROR): 73 0 : mooseError(_moose_object->name(), ": ", std::forward<Args>(args)...); 74 : break; 75 : default: 76 : break; 77 : } 78 : // return a quiet NaN 79 40 : return std::nan(""); 80 : } 81 : 82 : /** 83 : * Throws an error or returns NaNs with or without a warning 84 : * 85 : * @param[in] n Vector size 86 : */ 87 : template <typename... Args> 88 0 : std::vector<Real> getNaNVector(const unsigned int & n, Args &&... args) const 89 : { 90 0 : switch (_emit_on_nan) 91 : { 92 0 : case (NAN_MESSAGE_WARNING): 93 0 : mooseWarning(_moose_object->name(), ": ", std::forward<Args>(args)...); 94 : break; 95 0 : case (NAN_MESSAGE_EXCEPTION): 96 0 : mooseException(_moose_object->name(), ": ", std::forward<Args>(args)...); 97 : break; 98 0 : case (NAN_MESSAGE_ERROR): 99 0 : mooseError(_moose_object->name(), ": ", std::forward<Args>(args)...); 100 : break; 101 : default: 102 : break; 103 : } 104 : // return quiet NaNs 105 0 : return std::vector<Real>(n, std::nan("")); 106 : } 107 : };