https://mooseframework.inl.gov
Public Member Functions | Private Attributes | List of all members
ReadExodusMeshVars Class Reference

Utility function to read a single variable off an Exodus mesh for optimization problem This class will read initial conditions and bounds from an exodus file and make sure they have the same order in the parameter reporter between the main and subapp when using ParameterMeshOptimization. More...

#include <ReadExodusMeshVars.h>

Public Member Functions

 ReadExodusMeshVars (const libMesh::FEType &param_type, const std::string &exodus_mesh, const std::string var_name)
 
std::vector< RealgetParameterValues (const unsigned int timestep) const
 Initializes parameter data and sets bounds in the main optmiization application getParameterValues is only used by ParameterMeshOptimization. More...
 

Private Attributes

libMesh::Parallel::Communicator _communicator
 
libMesh::ReplicatedMesh _mesh
 
std::unique_ptr< libMesh::EquationSystems_eq
 
libMesh::System_sys
 
std::unique_ptr< libMesh::ExodusII_IO_exodusII_io
 
const std::string _var_name
 variable name read from Exodus mesh More...
 

Detailed Description

Utility function to read a single variable off an Exodus mesh for optimization problem This class will read initial conditions and bounds from an exodus file and make sure they have the same order in the parameter reporter between the main and subapp when using ParameterMeshOptimization.

Definition at line 30 of file ReadExodusMeshVars.h.

Constructor & Destructor Documentation

◆ ReadExodusMeshVars()

ReadExodusMeshVars::ReadExodusMeshVars ( const libMesh::FEType param_type,
const std::string &  exodus_mesh,
const std::string  var_name 
)

Definition at line 22 of file ReadExodusMeshVars.C.

25  : _communicator(MPI_COMM_SELF), _mesh(_communicator), _var_name(var_name)
26 {
27  _mesh.allow_renumbering(false);
29  _exodusII_io = std::make_unique<ExodusII_IO>(_mesh);
30  _exodusII_io->read(exodus_mesh);
31  _mesh.read(exodus_mesh);
32  // Create system to store parameter values
33  _eq = std::make_unique<EquationSystems>(_mesh);
34  _sys = &_eq->add_system<ExplicitSystem>("_reading_exodus_mesh_var");
35 
36  // Make Exodus vars for equation system
37  const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
38  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  if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
44  nodal_variable = _var_name;
45  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  if (!nodal_variable.empty())
52  _sys->add_variable(_var_name, param_type);
53  if (!elemental_variable.empty())
54  _sys->add_variable(_var_name, param_type);
55  // Initialize the equations systems
56  _eq->init();
57 
58  if (nodal_variable.empty() && elemental_variable.empty())
59  {
60  std::string out("\n Parameter Requested: " + var_name);
61  out += "\n Exodus Nodal Variables: ";
62  for (const auto & var : all_nodal)
63  out += var + " ";
64  out += "\n Exodus Elemental Variables: ";
65  for (const auto & var : all_elemental)
66  out += var + " ";
67  mooseError("Exodus file did not contain the parameter name being intitialized.", out);
68  }
69 }
libMesh::ReplicatedMesh _mesh
void allow_renumbering(bool allow)
void mooseError(Args &&... args)
void prepare_for_use(const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
const std::string _var_name
variable name read from Exodus mesh
libMesh::System * _sys
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
std::unique_ptr< libMesh::EquationSystems > _eq
OStreamProxy out
std::unique_ptr< libMesh::ExodusII_IO > _exodusII_io
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false) override
libMesh::Parallel::Communicator _communicator

Member Function Documentation

◆ getParameterValues()

std::vector< Real > ReadExodusMeshVars::getParameterValues ( const unsigned int  timestep) const

Initializes parameter data and sets bounds in the main optmiization application getParameterValues is only used by ParameterMeshOptimization.

Parameters
timesteptimestep to read variable off mesh
Returns
vector of variables read off mesh at timestep

Definition at line 72 of file ReadExodusMeshVars.C.

Referenced by ParameterMeshOptimization::parseExodusData().

73 {
74  // get the exodus variable and put it into the equation system.
75  unsigned int step_to_read = _exodusII_io->get_num_time_steps();
76  if (time_step <= step_to_read)
77  step_to_read = time_step;
78  else if (time_step != std::numeric_limits<unsigned int>::max())
79  mooseError("Invalid value passed as \"time_step\". Expected a valid integer "
80  "less than ",
81  _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  const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
88  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  if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
91  _exodusII_io->copy_nodal_solution(*_sys, _var_name, _var_name, step_to_read);
92  else if (std::find(all_elemental.begin(), all_elemental.end(), _var_name) != all_elemental.end())
93  _exodusII_io->copy_elemental_solution(*_sys, _var_name, _var_name, step_to_read);
94 
95  // Update the equations systems
96  _sys->update();
97 
98  // Now read the Exodus variable from the equation system into a vector for the reporter
99  const unsigned short int var_id = _sys->variable_number(_var_name);
100  std::set<dof_id_type> var_indices;
101  _sys->local_dof_indices(var_id, var_indices); // Everything is local so this is fine
102  std::vector<dof_id_type> var_indices_vector(var_indices.begin(), var_indices.end());
103 
104  std::vector<Real> parameter_values;
105  _sys->solution->localize(parameter_values, var_indices_vector);
106  return parameter_values;
107 }
void local_dof_indices(const unsigned int var, std::set< dof_id_type > &var_indices) const
void mooseError(Args &&... args)
unsigned int variable_number(std::string_view var) const
const std::string _var_name
variable name read from Exodus mesh
libMesh::System * _sys
std::unique_ptr< NumericVector< Number > > solution
virtual void update()
std::unique_ptr< libMesh::ExodusII_IO > _exodusII_io

Member Data Documentation

◆ _communicator

libMesh::Parallel::Communicator ReadExodusMeshVars::_communicator
private

Definition at line 45 of file ReadExodusMeshVars.h.

◆ _eq

std::unique_ptr<libMesh::EquationSystems> ReadExodusMeshVars::_eq
private

Definition at line 47 of file ReadExodusMeshVars.h.

Referenced by ReadExodusMeshVars().

◆ _exodusII_io

std::unique_ptr<libMesh::ExodusII_IO> ReadExodusMeshVars::_exodusII_io
private

Definition at line 49 of file ReadExodusMeshVars.h.

Referenced by getParameterValues(), and ReadExodusMeshVars().

◆ _mesh

libMesh::ReplicatedMesh ReadExodusMeshVars::_mesh
private

Definition at line 46 of file ReadExodusMeshVars.h.

Referenced by ReadExodusMeshVars().

◆ _sys

libMesh::System* ReadExodusMeshVars::_sys
private

Definition at line 48 of file ReadExodusMeshVars.h.

Referenced by getParameterValues(), and ReadExodusMeshVars().

◆ _var_name

const std::string ReadExodusMeshVars::_var_name
private

variable name read from Exodus mesh

Definition at line 52 of file ReadExodusMeshVars.h.

Referenced by getParameterValues(), and ReadExodusMeshVars().


The documentation for this class was generated from the following files: