www.mooseframework.org
DOFMapOutput.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 registerMooseObjectAliased("MooseApp", DOFMapOutput, "DOFMap");
27 
28 template <>
31 {
32  // Get the parameters from the base class
34  params.addClassDescription("Output degree-of-freedom (DOF) map.");
35 
36  // Screen and file output toggles
37  params.addParam<bool>("output_screen", false, "Output to the screen");
38  params.addParam<bool>("output_file", true, "Output to the file");
39  params.addParam<std::string>("system_name", "nl0", "System to output");
40 
41  // By default this only executes on the initial timestep
42  params.set<ExecFlagEnum>("execute_on", true) = EXEC_INITIAL;
43 
44  return params;
45 }
46 
48  : FileOutput(parameters),
49  _write_file(getParam<bool>("output_file")),
50  _write_screen(getParam<bool>("output_screen")),
51  _system_name(getParam<std::string>("system_name")),
52  _mesh(_problem_ptr->mesh())
53 {
54 }
55 
56 std::string
58 {
59  if (_file_num > 0)
60  return _file_base + "_" + Moose::stringify(_file_num) + ".json";
61  else
62  return _file_base + ".json";
63 }
64 
65 std::string
66 DOFMapOutput::demangle(const std::string & name)
67 {
68 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
69  return libMesh::demangle(name.c_str());
70 #else
71  // at least remove leading digits
72  std::string demangled(name);
73  while (demangled.length() && demangled[0] >= '0' && demangled[0] <= '9')
74  demangled.erase(0, 1);
75 
76  return demangled;
77 #endif
78 }
79 
80 void
82 {
83  // Create the stream
84  std::ofstream output;
85 
86  // Open the file and write contents of file output stream and close the file
87  output.open(filename().c_str(), std::ios::trunc);
88  if (output.fail())
89  mooseError("Unable to open file ", filename());
90 
91  output << _file_output_stream.str();
92  output.close();
93 
94  // Clear the file output stream
95  _file_output_stream.str("");
96  _file_num++;
97 }
98 
99 template <typename T>
100 std::string
101 DOFMapOutput::join(const T & begin, const T & end, const char * const delim)
102 {
103  std::ostringstream os;
104  for (T it = begin; it != end; ++it)
105  os << (it != begin ? delim : "") << *it;
106  return os.str();
107 }
108 
109 void
111 {
112  // Don't build this information if nothing is to be written
113  if (!_write_screen && !_write_file)
114  return;
115 
116  std::stringstream oss;
117 
118  // Get the DOF Map through the equation system
119  const System & sys = _problem_ptr->es().get_system(_system_name); // TransientNonlinearImplicit
120  const DofMap & dof_map = sys.get_dof_map();
121 
122  // fetch the KernelWarehouse through the NonlinearSystem
124  auto & kernels = _nl.getKernelWarehouse();
125 
126  // get a set of all subdomains
127  const std::set<SubdomainID> & subdomains = _mesh.meshSubdomains();
128 
129  bool first = true;
130  oss << "{\"ndof\": " << sys.n_dofs() << ", \"demangled\": ";
131 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
132  oss << "true";
133 #else
134  oss << "false";
135 #endif
136  oss << ", \"vars\": [";
137  for (unsigned int vg = 0; vg < dof_map.n_variable_groups(); ++vg)
138  {
139  const VariableGroup & vg_description(dof_map.variable_group(vg));
140  for (unsigned int vn = 0; vn < vg_description.n_variables(); ++vn)
141  {
142  unsigned int var = vg_description.number(vn);
143 
144  if (!first)
145  oss << ", ";
146  first = false;
147 
148  oss << "{\"name\": \"" << vg_description.name(vn) << "\", \"subdomains\": [";
149  for (std::set<SubdomainID>::const_iterator sd = subdomains.begin(); sd != subdomains.end();
150  ++sd)
151  {
152  oss << (sd != subdomains.begin() ? ", " : "") << "{\"id\": " << *sd << ", \"kernels\": [";
153 
154  // if this variable has active kernels output them
155  if (kernels.hasActiveVariableBlockObjects(var, *sd))
156  {
157  const auto & active_kernels = kernels.getActiveVariableBlockObjects(var, *sd);
158  for (unsigned i = 0; i < active_kernels.size(); ++i)
159  {
160  KernelBase & kb = *(active_kernels[i].get());
161  oss << (i > 0 ? ", " : "") << "{\"name\": \"" << kb.name() << "\", \"type\": \""
162  << demangle(typeid(kb).name()) << "\"}";
163  }
164  }
165  oss << "], \"dofs\": [";
166 
167  // get the list of unique DOFs for this variable
168  // Start by looking at local DOFs
169  std::set<dof_id_type> dofs;
170  ConstElemRange * active_local_elems = _mesh.getActiveLocalElementRange();
171  for (const auto & elem : *active_local_elems)
172  {
173  if (elem->subdomain_id() == *sd)
174  {
175  std::vector<dof_id_type> di;
176  dof_map.dof_indices(elem, di, var);
177  dofs.insert(di.begin(), di.end());
178  }
179  }
180 
181  // Then collect DOFs from other processors. On a distributed
182  // mesh they may know about DOFs we can't even see.
183  _communicator.set_union(dofs);
184 
185  oss << join(dofs.begin(), dofs.end(), ", ") << "]}";
186  }
187  oss << "]}";
188  }
189  }
190  oss << "]}\n";
191 
192  // Write the message to file stream
193  if (_write_file)
194  _file_output_stream << oss.str() << std::endl;
195 
196  // Write message to the screen
197  if (_write_screen)
198  _console << oss.str() << std::flush;
199 
200  // Write the actual file
201  if (_write_file)
203 }
ConstElemRange * getActiveLocalElementRange()
Return pointers to range objects for various types of ranges (local nodes, boundary elems...
Definition: MooseMesh.C:737
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
void output(const ExecFlagType &type) override
Write the DOF mapt.
Definition: DOFMapOutput.C:110
NonlinearSystemBase & getNonlinearSystemBase()
std::stringstream _file_output_stream
Stream for storing information to be written to a file.
Definition: DOFMapOutput.h:74
registerMooseObjectAliased("MooseApp", DOFMapOutput, "DOFMap")
InputParameters validParams< DOFMapOutput >()
Definition: DOFMapOutput.C:30
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.
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:81
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
virtual EquationSystems & es() override
Nonlinear system to be solved.
This is the common base class for the two main kernel types implemented in MOOSE, EigenKernel and Ker...
Definition: KernelBase.h:44
MooseMesh & _mesh
Reference to the mesh object.
Definition: DOFMapOutput.h:80
DofMap & dof_map
bool _write_screen
Flag for controlling outputting console information to screen.
Definition: DOFMapOutput.h:71
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
An output object for writing the DOF map of the system in a machine parsable format.
Definition: DOFMapOutput.h:25
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:57
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
DOFMapOutput(const InputParameters &parameters)
Definition: DOFMapOutput.C:47
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
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 option 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:84
InputParameters validParams< FileOutput >()
Definition: FileOutput.C:23
std::string _system_name
The name of the system to extract DOF information.
Definition: DOFMapOutput.h:77
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:81
An outputter with filename support.
Definition: FileOutput.h:26
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:101
std::string demangle(const std::string &name)
Try to demangle type name.
Definition: DOFMapOutput.C:66
const std::set< SubdomainID > & meshSubdomains() const
Returns a read-only reference to the set of subdomains currently present in the Mesh.
Definition: MooseMesh.C:2316
bool _write_file
Flag for controlling outputting console information to a file.
Definition: DOFMapOutput.h:68
const ExecFlagType EXEC_INITIAL