LCOV - code coverage report
Current view: top level - src/utils - ReadExodusMeshVars.C (source / functions) Hit Total Coverage
Test: idaholab/moose optimization: #31730 (e8b711) with base e0c998 Lines: 45 45 100.0 %
Date: 2025-10-29 16:52:42 Functions: 2 2 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             : #include "MooseError.h"
      11             : #include "ReadExodusMeshVars.h"
      12             : 
      13             : #include "libmesh/dof_map.h"
      14             : #include "libmesh/explicit_system.h"
      15             : #include "libmesh/fe_compute_data.h"
      16             : #include "libmesh/libmesh_common.h"
      17             : #include "libmesh/numeric_vector.h"
      18             : #include <memory>
      19             : 
      20             : using namespace libMesh;
      21             : 
      22         211 : ReadExodusMeshVars::ReadExodusMeshVars(const FEType & param_type,
      23             :                                        const std::string & exodus_mesh,
      24         211 :                                        const std::string var_name)
      25         211 :   : _communicator(MPI_COMM_SELF), _mesh(_communicator), _var_name(var_name)
      26             : {
      27             :   _mesh.allow_renumbering(false);
      28         211 :   _mesh.prepare_for_use();
      29         422 :   _exodusII_io = std::make_unique<ExodusII_IO>(_mesh);
      30         211 :   _exodusII_io->read(exodus_mesh);
      31         211 :   _mesh.read(exodus_mesh);
      32             :   // Create system to store parameter values
      33         422 :   _eq = std::make_unique<EquationSystems>(_mesh);
      34         211 :   _sys = &_eq->add_system<ExplicitSystem>("_reading_exodus_mesh_var");
      35             : 
      36             :   // Make Exodus vars for equation system
      37         211 :   const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
      38         211 :   const std::vector<std::string> & all_elemental(_exodusII_io->get_elem_var_names());
      39             : 
      40             :   std::string nodal_variable;
      41             :   std::string elemental_variable;
      42             : 
      43         211 :   if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
      44             :     nodal_variable = _var_name;
      45         211 :   if (std::find(all_elemental.begin(), all_elemental.end(), _var_name) != all_elemental.end())
      46             :     elemental_variable = _var_name;
      47             : 
      48             :   // Add the parameter variable to system
      49             :   // All parameters in a group will be the same type, only one of these loops will do anything
      50             :   // If it tries to add the wrong type, will throw a libmesh error
      51         211 :   if (!nodal_variable.empty())
      52         164 :     _sys->add_variable(_var_name, param_type);
      53         211 :   if (!elemental_variable.empty())
      54          45 :     _sys->add_variable(_var_name, param_type);
      55             :   // Initialize the equations systems
      56         211 :   _eq->init();
      57             : 
      58         211 :   if (nodal_variable.empty() && elemental_variable.empty())
      59             :   {
      60           2 :     std::string out("\n  Parameter Requested: " + var_name);
      61             :     out += "\n  Exodus Nodal Variables: ";
      62          14 :     for (const auto & var : all_nodal)
      63          24 :       out += var + " ";
      64             :     out += "\n  Exodus Elemental Variables: ";
      65           8 :     for (const auto & var : all_elemental)
      66          12 :       out += var + " ";
      67           2 :     mooseError("Exodus file did not contain the parameter name being intitialized.", out);
      68             :   }
      69         209 : }
      70             : 
      71             : std::vector<Real>
      72         375 : ReadExodusMeshVars::getParameterValues(const unsigned int time_step) const
      73             : {
      74             :   // get the exodus variable and put it into the equation system.
      75         375 :   unsigned int step_to_read = _exodusII_io->get_num_time_steps();
      76         375 :   if (time_step <= step_to_read)
      77             :     step_to_read = time_step;
      78         119 :   else if (time_step != std::numeric_limits<unsigned int>::max())
      79           2 :     mooseError("Invalid value passed as \"time_step\". Expected a valid integer "
      80             :                "less than ",
      81           2 :                _exodusII_io->get_num_time_steps(),
      82             :                ", received ",
      83             :                time_step);
      84             : 
      85             :   // Store Exodus variable on the equation system.
      86             :   // This will give it the same order as other functions reading variables from the same exodus mesh
      87         373 :   const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
      88         373 :   const std::vector<std::string> & all_elemental(_exodusII_io->get_elem_var_names());
      89             :   // determine what kind of variable you are trying to read from mesh
      90         373 :   if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
      91         548 :     _exodusII_io->copy_nodal_solution(*_sys, _var_name, _var_name, step_to_read);
      92          99 :   else if (std::find(all_elemental.begin(), all_elemental.end(), _var_name) != all_elemental.end())
      93         198 :     _exodusII_io->copy_elemental_solution(*_sys, _var_name, _var_name, step_to_read);
      94             : 
      95             :   // Update the equations systems
      96         373 :   _sys->update();
      97             : 
      98             :   // Now read the Exodus variable from the equation system into a vector for the reporter
      99         373 :   const unsigned short int var_id = _sys->variable_number(_var_name);
     100             :   std::set<dof_id_type> var_indices;
     101         373 :   _sys->local_dof_indices(var_id, var_indices); // Everything is local so this is fine
     102         373 :   std::vector<dof_id_type> var_indices_vector(var_indices.begin(), var_indices.end());
     103             : 
     104             :   std::vector<Real> parameter_values;
     105         373 :   _sys->solution->localize(parameter_values, var_indices_vector);
     106         373 :   return parameter_values;
     107         373 : }

Generated by: LCOV version 1.14