LCOV - code coverage report
Current view: top level - include/outputs - OutputWarehouse.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 35 40 87.5 %
Date: 2025-07-17 01:28:37 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          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             : // MOOSE includes
      13             : #include "Output.h"
      14             : #include "PerfGraphInterface.h"
      15             : 
      16             : // System includes
      17             : #include <atomic>
      18             : 
      19             : // Forward declarations
      20             : class FEProblemBase;
      21             : class InputParameters;
      22             : 
      23             : /**
      24             :  * Class for storing and utilizing output objects
      25             :  */
      26             : class OutputWarehouse : protected PerfGraphInterface
      27             : {
      28             : public:
      29             :   /**
      30             :    * Class constructor
      31             :    */
      32             :   OutputWarehouse(MooseApp & app);
      33             : 
      34             :   /*
      35             :    * Class destructor
      36             :    * The OutputWarehouse deletes all output objects passed in via addOutput
      37             :    */
      38             :   virtual ~OutputWarehouse();
      39             : 
      40             :   /**
      41             :    * Adds an existing output object to the warehouse
      42             :    * @param output Pointer to the output object
      43             :    * It is the responsibility of the OutputWarehouse to delete the output objects
      44             :    * add using this method
      45             :    */
      46             :   void addOutput(std::shared_ptr<Output> output);
      47             : 
      48             :   /**
      49             :    * Get a complete set of all output object names
      50             :    * @return A set of output names for each output object
      51             :    *
      52             :    * Note, if this method is called prior to the creation of outputs in AddOutputAction it will
      53             :    * create the proxy list of names from the action system. The main use is for the OutputInterface,
      54             :    * specifically, when used with Postprocessors in the UserObjects block of the input file.
      55             :    * UserObjects are created prior to Outputs objects, but OutputInterface needs the list
      56             :    * of output names to operate correctly.
      57             :    *
      58             :    */
      59             :   const std::set<OutputName> & getOutputNames();
      60             : 
      61             :   /**
      62             :    * Returns true if the output object exists
      63             :    * @param name The name of the output object for which to test for existence within the warehouse
      64             :    */
      65             :   bool hasOutput(const std::string & name) const;
      66             : 
      67             :   /**
      68             :    * Returns true if the output object exists, and it supports material property output
      69             :    * @param name The name of the output object for which to test for existence within the warehouse
      70             :    */
      71             :   bool hasMaterialPropertyOutput(const std::string & name) const;
      72             : 
      73             :   /**
      74             :    * Calls the meshChanged method for every output object
      75             :    */
      76             :   void meshChanged();
      77             : 
      78             :   /**
      79             :    * Return the list of hidden variables for the given output name
      80             :    * @param output_name The name of the output object for which the variables should be returned
      81             :    * @param hide The set of variables to hide which is built by this method
      82             :    *
      83             :    * Objects inheriting from the OutputInterface have the ability to control the output of variables
      84             :    * associated with the objects (i.e., Marker elemental variable). This method returns a list
      85             :    * of variables that should be hidden for the supplied object name due to the 'outputs' parameter
      86             :    * being set by the object(s).
      87             :    *
      88             :    * This method is used by Output::initOutputList to populate the correct hide lists for the
      89             :    * output object, it is not intended for general use.
      90             :    */
      91             :   void buildInterfaceHideVariables(const std::string & output_name, std::set<std::string> & hide);
      92             : 
      93             :   /**
      94             :    * Calls the setFileNumber method for every FileOutput output object
      95             :    */
      96             :   void setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset = 0);
      97             : 
      98             :   /**
      99             :    * Extracts the file numbers from the output objects
     100             :    * @return Map of file numbers for the output objects
     101             :    */
     102             :   std::map<std::string, unsigned int> getFileNumbers();
     103             : 
     104             :   /**
     105             :    * Stores the common InputParameters object
     106             :    * @param params_ptr A pointer to the common parameters object to be stored
     107             :    *
     108             :    * @see CommonOutputAction
     109             :    */
     110             :   void setCommonParameters(const InputParameters * params_ptr);
     111             : 
     112             :   /**
     113             :    * Get a reference to the common output parameters
     114             :    * @return Pointer to the common InputParameters object
     115             :    */
     116             :   const InputParameters * getCommonParameters() const;
     117             : 
     118             :   /**
     119             :    * Return the sync times for all objects
     120             :    */
     121             :   std::set<Real> & getSyncTimes();
     122             : 
     123             :   /**
     124             :    * Test that the output names exist
     125             :    * @param names A vector of names to check
     126             :    * @param supports_material_output Optional parameter to check if all output objects associated
     127             :    *       with 'names' must support material property output
     128             :    *
     129             :    * This method will produce an error if any of the supplied names do not exist in
     130             :    * the warehouse. Reserved names are not considered.
     131             :    */
     132             :   void checkOutputs(const std::set<OutputName> & names,
     133             :                     const bool supports_material_output = false);
     134             : 
     135             :   /**
     136             :    * Returns all output names that support material output
     137             :    * @return A set of all output names that support material output
     138             :    */
     139             :   std::set<OutputName> getAllMaterialPropertyOutputNames() const;
     140             : 
     141             :   /**
     142             :    * Return an Output object by name
     143             :    * @tparam T The Out put object type to return
     144             :    * @param name The name of the output object
     145             :    * @return A pointer to the output object
     146             :    */
     147             :   template <typename T>
     148             :   T * getOutput(const OutputName & name);
     149             : 
     150             :   /**
     151             :    * Return a vector of objects by names
     152             :    * @tparam T The Output object type to return
     153             :    * @param names A vector of names of the output object
     154             :    * @return A pointer to the output object
     155             :    */
     156             :   template <typename T>
     157             :   std::vector<T *> getOutputs(const std::vector<OutputName> & names);
     158             : 
     159             :   /**
     160             :    * Return a vector of objects of a given type
     161             :    * @tparam T The Output object type to return
     162             :    * @return A pointer to the output object
     163             :    */
     164             :   template <typename T>
     165             :   std::vector<T *> getOutputs() const;
     166             : 
     167             :   /**
     168             :    * Return a list of output objects with a given type
     169             :    * @tparam T The output object type
     170             :    * @return A vector of names
     171             :    */
     172             :   template <typename T>
     173             :   std::vector<OutputName> getOutputNames();
     174             : 
     175             :   /**
     176             :    * Return a set of reserved output names
     177             :    * @return A std::set of reserved names
     178             :    */
     179             :   const std::set<std::string> & getReservedNames() const;
     180             : 
     181             :   /**
     182             :    * Test if the given name is reserved
     183             :    * @param name The name to test
     184             :    * @return True if the name is reserved
     185             :    */
     186             :   bool isReservedName(const std::string & name);
     187             : 
     188             :   /**
     189             :    * Send current output buffer to Console output objects
     190             :    */
     191             :   void mooseConsole();
     192             : 
     193             :   /**
     194             :    * Send a buffer to Console output objects
     195             :    */
     196             :   void mooseConsole(std::ostringstream & buffer);
     197             : 
     198             :   /**
     199             :    * The buffered messages stream for Console objects
     200             :    * @return Reference to the stream storing cached messages from calls to _console
     201             :    */
     202       56742 :   std::ostringstream & consoleBuffer() { return _console_buffer; }
     203             : 
     204             :   /**
     205             :    * Set if the outputs to Console before its construction are to be buffered or to screen directly
     206             :    * @param buffer Ture to buffer
     207             :    */
     208             :   void bufferConsoleOutputsBeforeConstruction(bool buffer)
     209             :   {
     210             :     _buffer_action_console_outputs = buffer;
     211             :   }
     212             : 
     213             :   /// Reset the output system
     214             :   void reset();
     215             : 
     216             :   /**
     217             :    * Calls the timestepSetup function for each of the output objects
     218             :    * @see FEProblemBase::solve()
     219             :    * timestepSetup is too early for solver setup where we try to hook up monitor,
     220             :    * change prefix, etc. SLEPc solver does not exist yet when we call timestepSetup.
     221             :    * Moved this method from the private region to the public region so we can call
     222             :    * this function from EigenProblem with no need to make EigenProblem as friend
     223             :    * of OutputWarehouse.
     224             :    */
     225             :   void solveSetup();
     226             : 
     227             :   /// The number of times something has been printed
     228   126822666 :   unsigned long long int numPrinted() const { return _num_printed; }
     229             : 
     230             : private:
     231             :   /**
     232             :    * Calls the outputStep method for each output object
     233             :    * @param type The type execution flag (see Moose.h)
     234             :    *
     235             :    * This is private, users should utilize FEProblemBase::outputStep()
     236             :    */
     237             :   void outputStep(ExecFlagType type);
     238             : 
     239             :   ///@{
     240             :   /**
     241             :    * Ability to enable/disable output calls
     242             :    * This is private, users should utilize FEProblemBase::allowOutput()
     243             :    * @see FEProblemBase::allowOutput()
     244             :    */
     245             :   void allowOutput(bool state);
     246             :   template <typename T>
     247             :   void allowOutput(bool state);
     248             :   ///@}
     249             : 
     250             :   /**
     251             :    * Indicates that the next call to outputStep should be forced
     252             :    * This is private, users should utilize FEProblemBase::forceOutput()
     253             :    * @see FEProblemBase::forceOutput()
     254             :    */
     255             :   void forceOutput();
     256             : 
     257             :   /**
     258             :    * We are using std::shared_ptr to handle the cleanup of the pointers at the end of execution.
     259             :    * This is necessary since several warehouses might be sharing a single instance of a MooseObject.
     260             :    */
     261             :   std::vector<std::shared_ptr<Output>> _all_ptrs;
     262             : 
     263             :   /**
     264             :    * Adds the file name to the map of filenames being output with an associated object
     265             :    * The main function of this object is to test that the same output file
     266             :    * does not already exist in another object to protect against output files overwriting each other
     267             :    *
     268             :    * @param obj_name Name of an FileOutput object
     269             :    * @param filename Name of an output file (extracted from filename() method of the objects)
     270             :    */
     271             :   void addOutputFilename(const OutputName & obj_name, const OutFileBase & filename);
     272             : 
     273             :   /**
     274             :    * Calls the initialSetup function for each of the output objects
     275             :    * @see FEProblemBase::initialSetup()
     276             :    */
     277             :   void initialSetup();
     278             : 
     279             :   /**
     280             :    * Calls the timestepSetup function for each of the output objects
     281             :    * @see FEProblemBase::timestepSetup()
     282             :    */
     283             :   void timestepSetup();
     284             : 
     285             :   /**
     286             :    * Calls the setup function for each of the output objects
     287             :    * @see FEProblemBase::customSetup(const ExecFlagType & exec_type)
     288             :    */
     289             :   void customSetup(const ExecFlagType & exec_type);
     290             : 
     291             :   /**
     292             :    * Calls the jacobianSetup function for each of the output objects
     293             :    * @see FEProblemBase::computeJacobian
     294             :    */
     295             :   void jacobianSetup();
     296             : 
     297             :   /**
     298             :    * Calls the residualSetup function for each of the output objects
     299             :    * @see FEProblemBase::computeResidualTyp
     300             :    */
     301             :   void residualSetup();
     302             : 
     303             :   /**
     304             :    * Calls the subdomainSetup function for each of the output objects
     305             :    * @see FEProblemBase::setupSubdomain
     306             :    */
     307             :   void subdomainSetup();
     308             : 
     309             :   /**
     310             :    * Insert variable names for hiding via the OutoutInterface
     311             :    * @param output_name The name of the output object on which the variable is to be hidden
     312             :    * @param variable_names The names of the variables to be hidden
     313             :    *
     314             :    * This is a private method used by the OutputInterface system, it is not intended for any
     315             :    * other purpose.
     316             :    */
     317             :   void addInterfaceHideVariables(const std::string & output_name,
     318             :                                  const std::set<std::string> & variable_names);
     319             : 
     320             :   /**
     321             :    * Sets the execution flag type
     322             :    *
     323             :    * This is a private method used by FEProblemBase, it is not intended for any other purpose
     324             :    */
     325             :   void setOutputExecutionType(ExecFlagType type);
     326             : 
     327             :   /**
     328             :    * If content exists in the buffer, write it.
     329             :    * This is used by Console to make sure PETSc related output does not dump
     330             :    * before buffered content. It is private because people shouldn't be messing with it.
     331             :    */
     332             :   void flushConsoleBuffer();
     333             : 
     334             :   /**
     335             :    * Resets the file base for all FileOutput objects
     336             :    */
     337             :   void resetFileBase();
     338             : 
     339             :   /// MooseApp
     340             :   MooseApp & _app;
     341             : 
     342             :   /// All instances of objects (raw pointers)
     343             :   std::vector<Output *> _all_objects;
     344             : 
     345             :   /// True to buffer console outputs in actions
     346             :   bool _buffer_action_console_outputs;
     347             : 
     348             :   /// A map of the output pointers
     349             :   std::map<OutputName, Output *> _object_map;
     350             : 
     351             :   /// A set of output names
     352             :   std::set<OutputName> _object_names;
     353             : 
     354             :   /// List of object names
     355             :   std::map<OutputName, std::set<OutFileBase>> _file_base_map;
     356             : 
     357             :   /// Pointer to the common InputParameters (@see CommonOutputAction)
     358             :   const InputParameters * _common_params_ptr;
     359             : 
     360             :   /// Sync times for all objects
     361             :   std::set<Real> _sync_times;
     362             : 
     363             :   /// Input file name for this output object
     364             :   std::string _input_file_name;
     365             : 
     366             :   /// Map of output name and AuxVariable names to be output (used by auto Material output)
     367             :   std::map<OutputName, std::set<AuxVariableName>> _material_output_map;
     368             : 
     369             :   /// List of all variable created by auto material output
     370             :   std::set<AuxVariableName> _all_material_output_variables;
     371             : 
     372             :   /// List of reserved names
     373             :   std::set<std::string> _reserved;
     374             : 
     375             :   /// The stream for holding messages passed to _console prior to Output object construction
     376             :   std::ostringstream _console_buffer;
     377             : 
     378             :   /// Storage for variables to hide as prescribed by the object via the OutputInterface
     379             :   std::map<std::string, std::set<std::string>> _interface_map;
     380             : 
     381             :   /// The current output execution flag
     382             :   ExecFlagType _output_exec_flag;
     383             : 
     384             :   /// Flag indicating that next call to outputStep is forced
     385             :   bool _force_output;
     386             : 
     387             :   /// Whether or not the last thing output by mooseConsole had a newline as the last character
     388             :   bool _last_message_ended_in_newline;
     389             : 
     390             :   /// What the last buffer was that was printed
     391             :   const std::ostringstream * _last_buffer;
     392             : 
     393             :   /// Number of times the stream has been printed to
     394             :   std::atomic<unsigned long long int> _num_printed;
     395             : 
     396             :   // Allow complete access:
     397             :   // FEProblemBase for calling initial, timestepSetup, outputStep, etc. methods
     398             :   friend class FEProblemBase;
     399             : 
     400             :   // MaterialOutputAction for calling addInterfaceHideVariables
     401             :   friend class MaterialOutputAction;
     402             : 
     403             :   // OutputInterface for calling addInterfaceHideVariables
     404             :   friend class OutputInterface;
     405             : 
     406             :   // Console for calling flushConsoleBuffer()
     407             :   friend class PetscOutputInterface;
     408             : 
     409             :   // MooseApp for resetFileBase()
     410             :   friend class MooseApp;
     411             : };
     412             : 
     413             : template <typename T>
     414             : T *
     415          21 : OutputWarehouse::getOutput(const OutputName & name)
     416             : {
     417             :   // Check that the object exists
     418          21 :   if (!hasOutput(name))
     419           0 :     mooseError("An output object with the name '", name, "' does not exist.");
     420             : 
     421             :   // Attempt to cast the object to the correct type
     422          21 :   T * output = dynamic_cast<T *>(_object_map[name]);
     423             : 
     424             :   // Error if the cast fails
     425          21 :   if (output == NULL)
     426           0 :     mooseError("An output object with the name '", name, "' for the specified type does not exist");
     427             : 
     428             :   // Return the object
     429          21 :   return output;
     430             : }
     431             : 
     432             : template <typename T>
     433             : std::vector<T *>
     434           7 : OutputWarehouse::getOutputs(const std::vector<OutputName> & names)
     435             : {
     436             :   // The vector to output
     437           7 :   std::vector<T *> outputs;
     438             : 
     439             :   // Populate the vector
     440          21 :   for (std::vector<OutputName>::const_iterator it = names.begin(); it != names.end(); ++it)
     441          14 :     outputs.push_back(getOutput<T>(*it));
     442             : 
     443             :   // Return the objects
     444           7 :   return outputs;
     445           0 : }
     446             : 
     447             : template <typename T>
     448             : std::vector<T *>
     449     5851703 : OutputWarehouse::getOutputs() const
     450             : {
     451             :   // The vector to output
     452     5851703 :   std::vector<T *> outputs;
     453             : 
     454             :   // Populate the vector
     455     5851703 :   for (std::map<OutputName, Output *>::const_iterator it = _object_map.begin();
     456    33280246 :        it != _object_map.end();
     457    27428543 :        ++it)
     458             :   {
     459    27428543 :     T * output = dynamic_cast<T *>(it->second);
     460    27428543 :     if (output != NULL)
     461     5754906 :       outputs.push_back(output);
     462             :   }
     463             : 
     464             :   // Return the objects
     465     5851703 :   return outputs;
     466           0 : }
     467             : 
     468             : template <typename T>
     469             : std::vector<OutputName>
     470           7 : OutputWarehouse::getOutputNames()
     471             : {
     472             :   // The output vector
     473           7 :   std::vector<OutputName> names;
     474             : 
     475             :   // Loop through the objects and store the name if the type cast succeeds
     476           7 :   for (std::map<OutputName, Output *>::const_iterator it = _object_map.begin();
     477          49 :        it != _object_map.end();
     478          42 :        ++it)
     479             :   {
     480          42 :     T * output = dynamic_cast<T *>(it->second);
     481          42 :     if (output != NULL)
     482          14 :       names.push_back(it->first);
     483             :   }
     484             : 
     485             :   // Return the names
     486           7 :   return names;
     487           0 : }
     488             : 
     489             : template <typename T>
     490             : void
     491        1572 : OutputWarehouse::allowOutput(bool state)
     492             : {
     493        1572 :   std::vector<T *> outputs = getOutputs<T>();
     494        3144 :   for (typename std::vector<T *>::iterator it = outputs.begin(); it != outputs.end(); ++it)
     495        1572 :     (*it)->allowOutput(state);
     496        1572 : }

Generated by: LCOV version 1.14