www.mooseframework.org
DOFMapOutput.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "DOFMapOutput.h"
17 #include "FEProblem.h"
18 #include "KernelBase.h"
19 #include "MooseApp.h"
20 #include "Moose.h"
21 #include "Conversion.h"
22 #include "MooseMesh.h"
23 #include "NonlinearSystem.h"
24 
25 #include "libmesh/fe.h"
26 
27 // compiler includes (for type demangling)
28 #include <cxxabi.h>
29 #include <fstream>
30 
31 template <>
34 {
35  // Get the parameters from the base class
37 
38  // Screen and file output toggles
39  params.addParam<bool>("output_screen", false, "Output to the screen");
40  params.addParam<bool>("output_file", true, "Output to the file");
41  params.addParam<std::string>("system_name", "nl0", "System to output");
42 
43  // By default this only executes on the initial timestep
44  params.set<MultiMooseEnum>("execute_on") = "initial";
45 
46  return params;
47 }
48 
50  : FileOutput(parameters),
51  _write_file(getParam<bool>("output_file")),
52  _write_screen(getParam<bool>("output_screen")),
53  _system_name(getParam<std::string>("system_name")),
54  _mesh(_problem_ptr->mesh())
55 {
56 }
57 
58 std::string
60 {
61  if (_file_num > 0)
62  return _file_base + "_" + Moose::stringify(_file_num) + ".json";
63  else
64  return _file_base + ".json";
65 }
66 
67 std::string
68 DOFMapOutput::demangle(const std::string & name)
69 {
70 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
71  return libMesh::demangle(name.c_str());
72 #else
73  // at least remove leading digits
74  std::string demangled(name);
75  while (demangled.length() && demangled[0] >= '0' && demangled[0] <= '9')
76  demangled.erase(0, 1);
77 
78  return demangled;
79 #endif
80 }
81 
82 void
84 {
85  // Create the stream
86  std::ofstream output;
87 
88  // Open the file and write contents of file output stream and close the file
89  output.open(filename().c_str(), std::ios::trunc);
90  output << _file_output_stream.str();
91  output.close();
92 
93  // Clear the file output stream
94  _file_output_stream.str("");
95  _file_num++;
96 }
97 
98 template <typename T>
99 std::string
100 DOFMapOutput::join(const T & begin, const T & end, const char * const delim)
101 {
102  std::ostringstream os;
103  for (T it = begin; it != end; ++it)
104  os << (it != begin ? delim : "") << *it;
105  return os.str();
106 }
107 
108 void
110 {
111  // Don't build this information if nothing is to be written
112  if (!_write_screen && !_write_file)
113  return;
114 
115  std::stringstream oss;
116 
117  // Get the DOF Map through the equation system
118  const System & sys = _problem_ptr->es().get_system(_system_name); // TransientNonlinearImplicit
119  const DofMap & dof_map = sys.get_dof_map();
120 
121  // fetch the KernelWarehouse through the NonlinearSystem
123  const KernelWarehouse & kernels = _nl.getKernelWarehouse();
124 
125  // get a set of all subdomains
126  const std::set<SubdomainID> & subdomains = _mesh.meshSubdomains();
127 
128  bool first = true;
129  oss << "{\"ndof\": " << sys.n_dofs() << ", \"demangled\": ";
130 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
131  oss << "true";
132 #else
133  oss << "false";
134 #endif
135  oss << ", \"vars\": [";
136  for (unsigned int vg = 0; vg < dof_map.n_variable_groups(); ++vg)
137  {
138  const VariableGroup & vg_description(dof_map.variable_group(vg));
139  for (unsigned int vn = 0; vn < vg_description.n_variables(); ++vn)
140  {
141  unsigned int var = vg_description.number(vn);
142 
143  if (!first)
144  oss << ", ";
145  first = false;
146 
147  oss << "{\"name\": \"" << vg_description.name(vn) << "\", \"subdomains\": [";
148  for (std::set<SubdomainID>::const_iterator sd = subdomains.begin(); sd != subdomains.end();
149  ++sd)
150  {
151  oss << (sd != subdomains.begin() ? ", " : "") << "{\"id\": " << *sd << ", \"kernels\": [";
152 
153  // if this variable has active kernels output them
154  if (kernels.hasActiveVariableBlockObjects(var, *sd))
155  {
156  const auto & active_kernels = kernels.getActiveVariableBlockObjects(var, *sd);
157  for (unsigned i = 0; i < active_kernels.size(); ++i)
158  {
159  KernelBase & kb = *(active_kernels[i].get());
160  oss << (i > 0 ? ", " : "") << "{\"name\": \"" << kb.name() << "\", \"type\": \""
161  << demangle(typeid(kb).name()) << "\"}";
162  }
163  }
164  oss << "], \"dofs\": [";
165 
166  // get the list of unique DOFs for this variable
167  // Start by looking at local DOFs
168  std::set<dof_id_type> dofs;
169  ConstElemRange * active_local_elems = _mesh.getActiveLocalElementRange();
170  for (const auto & elem : *active_local_elems)
171  {
172  if (elem->subdomain_id() == *sd)
173  {
174  std::vector<dof_id_type> di;
175  dof_map.dof_indices(elem, di, var);
176  dofs.insert(di.begin(), di.end());
177  }
178  }
179 
180  // Then collect DOFs from other processors. On a distributed
181  // mesh they may know about DOFs we can't even see.
182  _communicator.set_union(dofs);
183 
184  oss << join(dofs.begin(), dofs.end(), ", ") << "]}";
185  }
186  oss << "]}";
187  }
188  }
189  oss << "]}\n";
190 
191  // Write the message to file stream
192  if (_write_file)
193  _file_output_stream << oss.str() << std::endl;
194 
195  // Write message to the screen
196  if (_write_screen)
197  _console << oss.str() << std::flush;
198 
199  // Write the actual file
200  if (_write_file)
202 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
ConstElemRange * getActiveLocalElementRange()
Return pointers to range objects for various types of ranges (local nodes, boundary elems...
Definition: MooseMesh.C:724
const std::vector< std::shared_ptr< KernelBase > > & getActiveVariableBlockObjects(unsigned int variable_id, SubdomainID block_id, THREAD_ID tid=0) const
void output(const ExecFlagType &type) override
Write the DOF mapt.
Definition: DOFMapOutput.C:109
NonlinearSystemBase & getNonlinearSystemBase()
std::stringstream _file_output_stream
Stream for storing information to be written to a file.
Definition: DOFMapOutput.h:80
const std::set< SubdomainID > & meshSubdomains() const
Returns a read-only reference to the set of subdomains currently present in the Mesh.
Definition: MooseMesh.C:2157
InputParameters validParams< DOFMapOutput >()
Definition: DOFMapOutput.C:33
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:87
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:47
MooseMesh & _mesh
Reference to the mesh object.
Definition: DOFMapOutput.h:86
DofMap & dof_map
bool _write_screen
Flag for controlling outputting console information to screen.
Definition: DOFMapOutput.h:77
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:171
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:59
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:66
Holds kernels and provides some services.
DOFMapOutput(const InputParameters &parameters)
Definition: DOFMapOutput.C:49
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:90
InputParameters validParams< FileOutput >()
Definition: FileOutput.C:28
std::string _system_name
The name of the system to extract DOF information.
Definition: DOFMapOutput.h:83
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
bool hasActiveVariableBlockObjects(unsigned int variable_id, SubdomainID block_id, THREAD_ID tid=0) const
Methods for checking/getting variable kernels for a variable and SubdomainID.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
void writeStreamToFile(bool append=true)
Write the file stream to the file.
Definition: DOFMapOutput.C:83
An outputter with filename support.
Definition: FileOutput.h:32
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:100
std::string demangle(const std::string &name)
Try to demangle type name.
Definition: DOFMapOutput.C:68
const KernelWarehouse & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
bool _write_file
Flag for controlling outputting console information to a file.
Definition: DOFMapOutput.h:74