https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
27
30{
31 // Get the parameters from the base class
33 params.addClassDescription("Output degree-of-freedom (DOF) map.");
34
35 // Screen and file output toggles
36 params.addParam<bool>("output_screen", false, "Output to the screen");
37 params.addParam<bool>("output_file", true, "Output to the file");
38 params.addParam<std::string>("system_name", "nl0", "System to output");
39
40 // By default this only executes on the initial timestep
41 params.set<ExecFlagEnum>("execute_on", true) = EXEC_INITIAL;
42
43 params.addParam<NonlinearSystemName>(
44 "nl_sys", "nl0", "The nonlinear system that we should output information for.");
45 return params;
46}
47
49 : FileOutput(parameters),
50 _write_file(getParam<bool>("output_file")),
51 _write_screen(getParam<bool>("output_screen")),
52 _system_name(getParam<std::string>("system_name")),
53 _mesh(_problem_ptr->mesh()),
54 _nl_sys_num(_problem_ptr->nlSysNum(getParam<NonlinearSystemName>("nl_sys")))
55{
56}
57
58std::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
67std::string
68DOFMapOutput::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
82void
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 if (output.fail())
91 mooseError("Unable to open file ", filename());
92
94 output.close();
95
96 // Clear the file output stream
97 _file_output_stream.str("");
98 _file_num++;
99}
100
101template <typename T>
102std::string
103DOFMapOutput::join(const T & begin, const T & end, const char * const delim)
104{
105 std::ostringstream os;
106 for (T it = begin; it != end; ++it)
107 os << (it != begin ? delim : "") << *it;
108 return os.str();
109}
110
111void
113{
114 // Don't build this information if nothing is to be written
115 if (!_write_screen && !_write_file)
116 return;
117
118 std::stringstream oss;
119
120 // Get the DOF Map through the equation system
121 const System & sys = _problem_ptr->es().get_system(_system_name); // TransientNonlinearImplicit
122 const DofMap & dof_map = sys.get_dof_map();
123
124 // fetch the KernelWarehouse through the NonlinearSystem
126 auto & kernels = _nl.getKernelWarehouse();
127
128 // get a set of all subdomains
129 const std::set<SubdomainID> & subdomains = _mesh.meshSubdomains();
130
131 bool first = true;
132 oss << "{\"ndof\": " << sys.n_dofs() << ", \"demangled\": ";
133#if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE)
134 oss << "true";
135#else
136 oss << "false";
137#endif
138 oss << ", \"vars\": [";
139 for (unsigned int vg = 0; vg < dof_map.n_variable_groups(); ++vg)
140 {
141 const libMesh::VariableGroup & vg_description(dof_map.variable_group(vg));
142 for (unsigned int vn = 0; vn < vg_description.n_variables(); ++vn)
143 {
144 unsigned int var = vg_description.number(vn);
145
146 if (!first)
147 oss << ", ";
148 first = false;
149
150 oss << "{\"name\": \"" << vg_description.name(vn) << "\", \"subdomains\": [";
151 for (std::set<SubdomainID>::const_iterator sd = subdomains.begin(); sd != subdomains.end();
152 ++sd)
153 {
154 oss << (sd != subdomains.begin() ? ", " : "") << "{\"id\": " << *sd << ", \"kernels\": [";
155
156 // if this variable has active kernels output them
157 if (kernels.hasActiveVariableBlockObjects(var, *sd))
158 {
159 const auto & active_kernels = kernels.getActiveVariableBlockObjects(var, *sd);
160 for (unsigned i = 0; i < active_kernels.size(); ++i)
161 {
162 KernelBase & kb = *(active_kernels[i].get());
163 oss << (i > 0 ? ", " : "") << "{\"name\": \"" << kb.name() << "\", \"type\": \""
164 << demangle(typeid(kb).name()) << "\"}";
165 }
166 }
167 oss << "], \"dofs\": [";
168
169 // get the list of unique DOFs for this variable
170 // Start by looking at local DOFs
171 std::set<dof_id_type> dofs;
172 const ConstElemRange * active_local_elems = _mesh.getActiveLocalElementRange();
173 for (const auto & elem : *active_local_elems)
174 {
175 if (elem->subdomain_id() == *sd)
176 {
177 std::vector<dof_id_type> di;
178 dof_map.dof_indices(elem, di, var);
179 dofs.insert(di.begin(), di.end());
180 }
181 }
182
183 // Then collect DOFs from other processors. On a distributed
184 // mesh they may know about DOFs we can't even see.
186
187 oss << join(dofs.begin(), dofs.end(), ", ") << "]}";
188 }
189 oss << "]}";
190 }
191 }
192 oss << "]}" << std::endl;
193
194 // Write the message to file stream
195 if (_write_file)
196 _file_output_stream << oss.str() << std::endl;
197
198 // Write message to the screen
199 if (_write_screen)
200 _console << oss.str() << std::flush;
201
202 // Write the actual file
203 if (_write_file)
205}
registerMooseObjectAliased("MooseApp", DOFMapOutput, "DOFMap")
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
An output object for writing the DOF map of the system in a machine parsable format.
bool _write_file
Flag for controlling outputting console information to a file.
std::string join(const T &begin, const T &end, const char *const delim)
A helper method for joining items with a delimeter.
std::stringstream _file_output_stream
Stream for storing information to be written to a file.
static InputParameters validParams()
virtual std::string filename() override
Creates the output file name Appends the user-supplied 'file_base' input parameter with a '....
const unsigned int _nl_sys_num
The nonlinear system number we should output degree of freedom information for.
void output() override
Write the DOF mapt.
void writeStreamToFile(bool append=true)
Write the file stream to the file.
DOFMapOutput(const InputParameters &parameters)
std::string demangle(const std::string &name)
Try to demangle type name.
MooseMesh & _mesh
Reference to the mesh object.
bool _write_screen
Flag for controlling outputting console information to screen.
std::string _system_name
The name of the system to extract DOF information.
A MultiMooseEnum object to hold "execute_on" flags.
virtual libMesh::EquationSystems & es() override
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
An outputter with filename support.
Definition FileOutput.h:21
static InputParameters validParams()
Definition FileOutput.C:24
std::string _file_base
The base filename from the input paramaters.
Definition FileOutput.h:89
unsigned int & _file_num
A file number counter, initialized to 0 (this must be controlled by the child class,...
Definition FileOutput.h:80
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
This is the common base class for the three main kernel types implemented in MOOSE,...
Definition KernelBase.h:29
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
const std::set< SubdomainID > & meshSubdomains() const
Returns a read-only reference to the set of subdomains currently present in the Mesh.
Definition MooseMesh.C:3280
const libMesh::ConstElemRange * getActiveLocalElementRange()
Return pointers to range objects for various types of ranges (local nodes, boundary elems,...
Definition MooseMesh.C:1245
Nonlinear system to be solved.
MooseObjectTagWarehouse< KernelBase > & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition Output.h:185
void set_union(T &data, const unsigned int root_id) const
const T_sys & get_system(std::string_view name) const
const Parallel::Communicator & _communicator
unsigned int n_variables() const
const std::string & name(unsigned int v) const
unsigned int number(unsigned int v) const
MeshBase & mesh
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64
std::string demangle(const char *name)