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
20using namespace libMesh;
21
23 const std::string & exodus_mesh,
24 const std::string var_name)
25 : _communicator(MPI_COMM_SELF), _mesh(_communicator), _var_name(var_name)
26{
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}
70
71std::vector<Real>
72ReadExodusMeshVars::getParameterValues(const unsigned int time_step) const
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 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
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
OStreamProxy out(std::cout)