https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ReadExodusMeshVars.C
Go to the documentation of this file.
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
21 const std::string & exodus_mesh,
22 const std::string var_name)
23 : _communicator(MPI_COMM_SELF), _mesh(_communicator), _var_name(var_name)
24{
27 _exodusII_io = std::make_unique<ExodusII_IO>(_mesh);
28 _exodusII_io->read(exodus_mesh);
29 _mesh.read(exodus_mesh);
30 // Create system to store parameter values
31 _eq = std::make_unique<libMesh::EquationSystems>(_mesh);
32 _sys = &_eq->add_system<ExplicitSystem>("_reading_exodus_mesh_var");
33
34 // Make Exodus vars for equation system
35 const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
36 const std::vector<std::string> & all_elemental(_exodusII_io->get_elem_var_names());
37
38 std::string nodal_variable;
39 std::string elemental_variable;
40
41 if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
42 nodal_variable = _var_name;
43 if (std::find(all_elemental.begin(), all_elemental.end(), _var_name) != all_elemental.end())
44 elemental_variable = _var_name;
45
46 // Add the parameter variable to system
47 // All parameters in a group will be the same type, only one of these loops will do anything
48 // If it tries to add the wrong type, will throw a libmesh error
49 if (!nodal_variable.empty())
50 _sys->add_variable(_var_name, param_type);
51 if (!elemental_variable.empty())
52 _sys->add_variable(_var_name, param_type);
53 // Initialize the equations systems
54 _eq->init();
55
56 if (nodal_variable.empty() && elemental_variable.empty())
57 {
58 std::string out("\n Parameter Requested: " + var_name);
59 out += "\n Exodus Nodal Variables: ";
60 for (const auto & var : all_nodal)
61 out += var + " ";
62 out += "\n Exodus Elemental Variables: ";
63 for (const auto & var : all_elemental)
64 out += var + " ";
65 mooseError("Exodus file did not contain the parameter name being intitialized.", out);
66 }
67}
68
69std::vector<Real>
70ReadExodusMeshVars::getParameterValues(const unsigned int time_step) const
71{
72 // get the exodus variable and put it into the equation system.
73 unsigned int step_to_read = _exodusII_io->get_num_time_steps();
74 if (time_step <= step_to_read)
75 step_to_read = time_step;
76 else if (time_step != std::numeric_limits<unsigned int>::max())
77 mooseError("Invalid value passed as \"time_step\". Expected a valid integer "
78 "less than ",
79 _exodusII_io->get_num_time_steps(),
80 ", received ",
81 time_step);
82
83 // Store Exodus variable on the equation system.
84 // This will give it the same order as other functions reading variables from the same exodus mesh
85 const std::vector<std::string> & all_nodal(_exodusII_io->get_nodal_var_names());
86 const std::vector<std::string> & all_elemental(_exodusII_io->get_elem_var_names());
87 // determine what kind of variable you are trying to read from mesh
88 if (std::find(all_nodal.begin(), all_nodal.end(), _var_name) != all_nodal.end())
89 _exodusII_io->copy_nodal_solution(*_sys, _var_name, _var_name, step_to_read);
90 else if (std::find(all_elemental.begin(), all_elemental.end(), _var_name) != all_elemental.end())
91 _exodusII_io->copy_elemental_solution(*_sys, _var_name, _var_name, step_to_read);
92
93 // Update the equations systems
94 _sys->update();
95
96 // Now read the Exodus variable from the equation system into a vector for the reporter
97 const unsigned short int var_id = _sys->variable_number(_var_name);
98 std::set<dof_id_type> var_indices;
99 _sys->local_dof_indices(var_id, var_indices); // Everything is local so this is fine
100 std::vector<dof_id_type> var_indices_vector(var_indices.begin(), var_indices.end());
101
102 std::vector<Real> parameter_values;
103 _sys->solution->localize(parameter_values, var_indices_vector);
104 return parameter_values;
105}
void mooseError(Args &&... args)
std::unique_ptr< libMesh::EquationSystems > _eq
ReadExodusMeshVars(const libMesh::FEType &param_type, const std::string &exodus_mesh, const std::string var_name)
libMesh::ReplicatedMesh _mesh
std::unique_ptr< libMesh::ExodusII_IO > _exodusII_io
std::vector< Real > getParameterValues(const unsigned int timestep) const
Initializes parameter data and sets bounds in the main optmiization application getParameterValues is...
const std::string _var_name
variable name read from Exodus mesh
libMesh::System * _sys
void allow_renumbering(bool allow)
void prepare_for_use(const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
void local_dof_indices(const unsigned int var, std::set< dof_id_type > &var_indices) const
std::unique_ptr< NumericVector< Number > > solution
virtual void update()
unsigned int variable_number(std::string_view var) const
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false) override