https://mooseframework.inl.gov
DOFMapOutput.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 // MOOSE includes
11 #include "DOFMapOutput.h"
12 #include "FEProblem.h"
13 #include "KernelBase.h"
14 #include "MooseApp.h"
15 #include "Moose.h"
16 #include "Conversion.h"
17 #include "MooseMesh.h"
18 #include "NonlinearSystem.h"
19 
20 #include "libmesh/fe.h"
21 
22 // compiler includes (for type demangling)
23 #include <cxxabi.h>
24 #include <fstream>
25 
26 using namespace libMesh;
27 
28 registerMooseObjectAliased("MooseApp", DOFMapOutput, "DOFMap");
29 
32 {
33  // Get the parameters from the base class
35  params.addClassDescription("Output degree-of-freedom (DOF) map.");
36 
37  // Screen and file output toggles
38  params.addParam<bool>("output_screen", false, "Output to the screen");
39  params.addParam<bool>("output_file", true, "Output to the file");
40  params.addParam<std::string>("system_name", "nl0", "System to output");
41 
42  // By default this only executes on the initial timestep
43  params.set<ExecFlagEnum>("execute_on", true) = EXEC_INITIAL;
44 
45  params.addParam<NonlinearSystemName>(
46  "nl_sys", "nl0", "The nonlinear system that we should output information for.");
47  return params;
48 }
49 
51  : FileOutput(parameters),
52  _write_file(getParam<bool>("output_file")),
53  _write_screen(getParam<bool>("output_screen")),
54  _system_name(getParam<std::string>("system_name")),
55  _mesh(_problem_ptr->mesh()),
56  _nl_sys_num(_problem_ptr->nlSysNum(getParam<NonlinearSystemName>("nl_sys")))
57 {
58 }
59 
60 std::string
62 {
63  if (_file_num > 0)
64  return _file_base + "_" + Moose::stringify(_file_num) + ".json";
65  else
66  return _file_base + ".json";
67 }
68 
69 std::string
70 DOFMapOutput::demangle(const std::string & name)
71 {
72 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
73  return libMesh::demangle(name.c_str());
74 #else
75  // at least remove leading digits
76  std::string demangled(name);
77  while (demangled.length() && demangled[0] >= '0' && demangled[0] <= '9')
78  demangled.erase(0, 1);
79 
80  return demangled;
81 #endif
82 }
83 
84 void
86 {
87  // Create the stream
88  std::ofstream output;
89 
90  // Open the file and write contents of file output stream and close the file
91  output.open(filename().c_str(), std::ios::trunc);
92  if (output.fail())
93  mooseError("Unable to open file ", filename());
94 
95  output << _file_output_stream.str();
96  output.close();
97 
98  // Clear the file output stream
99  _file_output_stream.str("");
100  _file_num++;
101 }
102 
103 template <typename T>
104 std::string
105 DOFMapOutput::join(const T & begin, const T & end, const char * const delim)
106 {
107  std::ostringstream os;
108  for (T it = begin; it != end; ++it)
109  os << (it != begin ? delim : "") << *it;
110  return os.str();
111 }
112 
113 void
115 {
116  // Don't build this information if nothing is to be written
117  if (!_write_screen && !_write_file)
118  return;
119 
120  std::stringstream oss;
121 
122  // Get the DOF Map through the equation system
123  const System & sys = _problem_ptr->es().get_system(_system_name); // TransientNonlinearImplicit
124  const DofMap & dof_map = sys.get_dof_map();
125 
126  // fetch the KernelWarehouse through the NonlinearSystem
128  auto & kernels = _nl.getKernelWarehouse();
129 
130  // get a set of all subdomains
131  const std::set<SubdomainID> & subdomains = _mesh.meshSubdomains();
132 
133  bool first = true;
134  oss << "{\"ndof\": " << sys.n_dofs() << ", \"demangled\": ";
135 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
136  oss << "true";
137 #else
138  oss << "false";
139 #endif
140  oss << ", \"vars\": [";
141  for (unsigned int vg = 0; vg < dof_map.n_variable_groups(); ++vg)
142  {
143  const VariableGroup & vg_description(dof_map.variable_group(vg));
144  for (unsigned int vn = 0; vn < vg_description.n_variables(); ++vn)
145  {
146  unsigned int var = vg_description.number(vn);
147 
148  if (!first)
149  oss << ", ";
150  first = false;
151 
152  oss << "{\"name\": \"" << vg_description.name(vn) << "\", \"subdomains\": [";
153  for (std::set<SubdomainID>::const_iterator sd = subdomains.begin(); sd != subdomains.end();
154  ++sd)
155  {
156  oss << (sd != subdomains.begin() ? ", " : "") << "{\"id\": " << *sd << ", \"kernels\": [";
157 
158  // if this variable has active kernels output them
159  if (kernels.hasActiveVariableBlockObjects(var, *sd))
160  {
161  const auto & active_kernels = kernels.getActiveVariableBlockObjects(var, *sd);
162  for (unsigned i = 0; i < active_kernels.size(); ++i)
163  {
164  KernelBase & kb = *(active_kernels[i].get());
165  oss << (i > 0 ? ", " : "") << "{\"name\": \"" << kb.name() << "\", \"type\": \""
166  << demangle(typeid(kb).name()) << "\"}";
167  }
168  }
169  oss << "], \"dofs\": [";
170 
171  // get the list of unique DOFs for this variable
172  // Start by looking at local DOFs
173  std::set<dof_id_type> dofs;
174  ConstElemRange * active_local_elems = _mesh.getActiveLocalElementRange();
175  for (const auto & elem : *active_local_elems)
176  {
177  if (elem->subdomain_id() == *sd)
178  {
179  std::vector<dof_id_type> di;
180  dof_map.dof_indices(elem, di, var);
181  dofs.insert(di.begin(), di.end());
182  }
183  }
184 
185  // Then collect DOFs from other processors. On a distributed
186  // mesh they may know about DOFs we can't even see.
187  _communicator.set_union(dofs);
188 
189  oss << join(dofs.begin(), dofs.end(), ", ") << "]}";
190  }
191  oss << "]}";
192  }
193  }
194  oss << "]}" << std::endl;
195 
196  // Write the message to file stream
197  if (_write_file)
198  _file_output_stream << oss.str() << std::endl;
199 
200  // Write message to the screen
201  if (_write_screen)
202  _console << oss.str() << std::flush;
203 
204  // Write the actual file
205  if (_write_file)
207 }
unsigned int n_variable_groups() const
static InputParameters validParams()
Definition: DOFMapOutput.C:31
libMesh::ConstElemRange * getActiveLocalElementRange()
Return pointers to range objects for various types of ranges (local nodes, boundary elems...
Definition: MooseMesh.C:1235
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
std::stringstream _file_output_stream
Stream for storing information to be written to a file.
Definition: DOFMapOutput.h:71
registerMooseObjectAliased("MooseApp", DOFMapOutput, "DOFMap")
MooseObjectTagWarehouse< KernelBase > & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:89
const unsigned int _nl_sys_num
The nonlinear system number we should output degree of freedom information for.
Definition: DOFMapOutput.h:80
const Parallel::Communicator & _communicator
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
const T_sys & get_system(std::string_view name) const
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
dof_id_type n_dofs() const
Nonlinear system to be solved.
This is the common base class for the three main kernel types implemented in MOOSE, Kernel, VectorKernel and ArrayKernel.
Definition: KernelBase.h:23
MooseMesh & _mesh
Reference to the mesh object.
Definition: DOFMapOutput.h:77
virtual libMesh::EquationSystems & es() override
bool _write_screen
Flag for controlling outputting console information to screen.
Definition: DOFMapOutput.h:68
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:185
static InputParameters validParams()
Definition: FileOutput.C:24
void output() override
Write the DOF mapt.
Definition: DOFMapOutput.C:114
const VariableGroup & variable_group(const unsigned int c) const
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
An output object for writing the DOF map of the system in a machine parsable format.
Definition: DOFMapOutput.h:20
virtual std::string filename() override
Creates the output file name Appends the user-supplied &#39;file_base&#39; input parameter with a &#39;...
Definition: DOFMapOutput.C:61
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
std::string demangle(const char *name)
DOFMapOutput(const InputParameters &parameters)
Definition: DOFMapOutput.C:50
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
unsigned int number(unsigned int v) const
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
unsigned int & _file_num
A file number counter, initialized to 0 (this must be controlled by the child class, see Exodus)
Definition: FileOutput.h:80
std::string _system_name
The name of the system to extract DOF information.
Definition: DOFMapOutput.h:74
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
void writeStreamToFile(bool append=true)
Write the file stream to the file.
Definition: DOFMapOutput.C:85
An outputter with filename support.
Definition: FileOutput.h:20
const DofMap & get_dof_map() const
std::string join(const T &begin, const T &end, const char *const delim)
A helper method for joining items with a delimeter.
Definition: DOFMapOutput.C:105
std::string demangle(const std::string &name)
Try to demangle type name.
Definition: DOFMapOutput.C:70
const std::set< SubdomainID > & meshSubdomains() const
Returns a read-only reference to the set of subdomains currently present in the Mesh.
Definition: MooseMesh.C:3166
bool _write_file
Flag for controlling outputting console information to a file.
Definition: DOFMapOutput.h:65
void set_union(T &data, const unsigned int root_id) const
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28