https://mooseframework.inl.gov
BlockRestrictionDebugOutput.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
12 #include "FEProblem.h"
13 #include "Material.h"
14 #include "ConsoleUtils.h"
15 #include "MooseMesh.h"
16 #include "MooseObjectName.h"
17 #include "NonlinearSystemBase.h"
18 #include "MooseVariableBase.h"
19 #include "BlockRestrictable.h"
20 #include "KernelBase.h"
21 #include "AuxiliarySystem.h"
22 #include "AuxKernel.h"
23 #include "UserObject.h"
24 
25 #include "libmesh/transient_system.h"
26 
27 using namespace libMesh;
28 
30 
33 {
35 
36  params.addParam<MultiMooseEnum>("scope",
37  getScopes("all"),
38  "The types of object to output block coverage for, if nothing is "
39  "provided everything will be output.");
40 
41  params.addParam<NonlinearSystemName>(
42  "nl_sys", "nl0", "The nonlinear system that we should output information for.");
43 
44  params.addClassDescription(
45  "Debug output object for displaying information regarding block-restriction of objects.");
46  params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;
47  return params;
48 }
49 
51 BlockRestrictionDebugOutput::getScopes(std::string default_scopes)
52 {
53  return MultiMooseEnum("none all variables kernels auxvariables auxkernels materials userobjects",
54  default_scopes);
55 }
56 
58  : Output(parameters),
59  _scope(getParam<MultiMooseEnum>("scope")),
60  _nl(_problem_ptr->getNonlinearSystemBase(
61  _problem_ptr->nlSysNum(getParam<NonlinearSystemName>("nl_sys")))),
62  _sys(_nl.system())
63 {
64 }
65 
66 void
68 {
70 }
71 
72 void
74 {
75  // is there anything to do?
76  if (_scope.isValid() && _scope.contains("none"))
77  return;
78 
79  // Build output stream
80  std::stringstream out;
81 
82  auto printCategoryAndNames = [&out](std::string category, std::set<std::string> & names)
83  {
84  const auto n = names.size();
85  if (n > 0)
86  {
87  // print the category and number of items
88  out << " " << category << " (" << n << " " << ((n == 1) ? "item" : "items") << "): ";
89 
90  // If we would just use Moose::stringify(names), the indention is not right.
91  // So we are printing the names one by one and using ConsoleUtils::insertNewline.
92  std::streampos begin_string_pos = out.tellp();
93  std::streampos curr_string_pos = begin_string_pos;
94  unsigned int i = 0;
95  for (const auto & name : names)
96  {
97  out << Moose::stringify(name) << ((i++ < (n - 1)) ? ", " : "");
98  curr_string_pos = out.tellp();
99  ConsoleUtils::insertNewline(out, begin_string_pos, curr_string_pos);
100  }
101  out << '\n';
102  return true;
103  }
104  else
105  {
106  return false;
107  }
108  };
109 
110  // Reference to mesh for getting block names
112 
113  // set of all subdomains in the mesh
114  const std::set<SubdomainID> & mesh_subdomains = mesh.meshSubdomains();
115 
116  // get kernels via reference to the Kernel warehouse
117  const auto & kernel_warehouse = _nl.getKernelWarehouse();
118  const auto & kernels = kernel_warehouse.getObjects(/*tid = */ 0);
119 
120  // AuxSystem
121  const auto & auxSystem = _problem_ptr->getAuxiliarySystem();
122 
123  // Reference to the Material warehouse
124  const auto & material_warehouse = _problem_ptr->getMaterialWarehouse();
125 
126  // get all user objects
127  std::vector<UserObject *> userObjects;
129  .query()
130  .condition<AttribSystem>("UserObject")
131  .condition<AttribThread>(0)
132  .queryIntoUnsorted(userObjects);
133 
134  // do we have to check all object types?
135  const bool include_all = !_scope.isValid() || _scope.contains("all");
136 
137  // iterate all subdomains
138  for (const auto & subdomain_id : mesh_subdomains)
139  {
140  // get the corresponding subdomain name
141  const auto & subdomain_name = mesh.getSubdomainName(subdomain_id);
142 
143  out << "\n";
144  out << " Subdomain '" << subdomain_name << "' (id " << subdomain_id << "):\n";
145 
146  bool objectsFound = false;
147 
148  // Variables
149  if (include_all || _scope.contains("variables"))
150  {
151  std::set<std::string> names;
152  for (unsigned int var_num = 0; var_num < _sys.n_vars(); var_num++)
153  {
154  const auto & var_name = _sys.variable_name(var_num);
155  if (_problem_ptr->hasVariable(var_name))
156  {
157  const MooseVariableBase & var =
158  _problem_ptr->getVariable(/*tid = */ 0,
159  var_name,
162  if (var.hasBlocks(subdomain_id))
163  names.insert(var_name);
164  }
165  }
166  objectsFound = printCategoryAndNames("Variables", names) || objectsFound;
167  }
168 
169  // Kernels
170  if (include_all || _scope.contains("kernels"))
171  {
172  std::set<std::string> names;
173  for (const auto & kernel : kernels)
174  {
175  if (kernel->hasBlocks(subdomain_id))
176  names.insert(kernel->name());
177  }
178  objectsFound = printCategoryAndNames("Kernels", names) || objectsFound;
179  }
180 
181  // AuxVariables
182  if (include_all || _scope.contains("auxvariables"))
183  {
184  std::set<std::string> names;
185  const auto & sys = auxSystem.system();
186  for (unsigned int vg = 0; vg < sys.n_variable_groups(); vg++)
187  {
188  const VariableGroup & vg_description(sys.variable_group(vg));
189  for (unsigned int vn = 0; vn < vg_description.n_variables(); vn++)
190  {
191  if (vg_description.active_on_subdomain(subdomain_id))
192  names.insert(vg_description.name(vn));
193  }
194  }
195  objectsFound = printCategoryAndNames("AuxVariables", names) || objectsFound;
196  }
197 
198  // AuxKernels
199  if (include_all || _scope.contains("auxkernels"))
200  {
201 
202  {
203  const auto & wh = auxSystem.nodalAuxWarehouse();
204  std::set<std::string> names;
205  if (wh.hasActiveBlockObjects(subdomain_id))
206  {
207  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
208  for (auto & auxkernel : auxkernels)
209  names.insert(auxkernel->name());
210  }
211  objectsFound = printCategoryAndNames("AuxKernels[nodal]", names) || objectsFound;
212  }
213 
214  {
215  const auto & wh = auxSystem.nodalVectorAuxWarehouse();
216  std::set<std::string> names;
217  if (wh.hasActiveBlockObjects(subdomain_id))
218  {
219  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
220  for (auto & auxkernel : auxkernels)
221  names.insert(auxkernel->name());
222  }
223  objectsFound = printCategoryAndNames("AuxKernels[nodalVector]", names) || objectsFound;
224  }
225 
226  {
227  const auto & wh = auxSystem.nodalArrayAuxWarehouse();
228  std::set<std::string> names;
229  if (wh.hasActiveBlockObjects(subdomain_id))
230  {
231  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
232  for (auto & auxkernel : auxkernels)
233  names.insert(auxkernel->name());
234  }
235  objectsFound = printCategoryAndNames("AuxKernels[nodalArray]", names) || objectsFound;
236  }
237 
238  {
239  const auto & wh = auxSystem.elemAuxWarehouse();
240  std::set<std::string> names;
241  if (wh.hasActiveBlockObjects(subdomain_id))
242  {
243  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
244  for (auto & auxkernel : auxkernels)
245  names.insert(auxkernel->name());
246  }
247  objectsFound = printCategoryAndNames("AuxKernels[elemAux]", names) || objectsFound;
248  }
249 
250  {
251  const auto & wh = auxSystem.elemVectorAuxWarehouse();
252  std::set<std::string> names;
253  if (wh.hasActiveBlockObjects(subdomain_id))
254  {
255  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
256  for (auto & auxkernel : auxkernels)
257  names.insert(auxkernel->name());
258  }
259  objectsFound = printCategoryAndNames("AuxKernels[elemVector]", names) || objectsFound;
260  }
261 
262  {
263  const auto & wh = auxSystem.elemArrayAuxWarehouse();
264  std::set<std::string> names;
265  if (wh.hasActiveBlockObjects(subdomain_id))
266  {
267  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
268  for (auto & auxkernel : auxkernels)
269  names.insert(auxkernel->name());
270  }
271  objectsFound = printCategoryAndNames("AuxKernels[elemArray]", names) || objectsFound;
272  }
273  }
274 
275  // Materials
276  if (include_all || _scope.contains("materials"))
277  {
278  std::set<std::string> names;
279  if (material_warehouse.hasActiveBlockObjects(subdomain_id))
280  {
281  auto const objs = material_warehouse.getBlockObjects(subdomain_id);
282  for (const auto & mat : objs)
283  names.insert(mat->name());
284  }
285  objectsFound = printCategoryAndNames("Materials", names) || objectsFound;
286  }
287 
288  // UserObjects
289  if (include_all || _scope.contains("userobjects"))
290  {
291  std::set<std::string> names;
292  for (const auto & obj : userObjects)
293  if (BlockRestrictable * blockrestrictable_obj = dynamic_cast<BlockRestrictable *>(obj))
294  if (blockrestrictable_obj->hasBlocks(subdomain_id))
295  names.insert(obj->name());
296  objectsFound = printCategoryAndNames("UserObjects", names) || objectsFound;
297  }
298 
299  if (!objectsFound)
300  out << " (no objects found)\n";
301  }
302 
303  out << std::flush;
304 
305  // Write the stored string to the ConsoleUtils output objects
306  _console << "\n[DBG] Block-Restrictions (" << mesh_subdomains.size()
307  << " subdomains): showing active objects\n";
308  _console << std::setw(ConsoleUtils::console_field_width) << out.str() << std::endl;
309 }
virtual bool hasVariable(const std::string &var_name) const override
Whether or not this problem has the variable.
BlockRestrictionDebugOutput(const InputParameters &parameters)
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
std::streampos tellp()
void printBlockRestrictionMap() const
Prints block-restriction information.
virtual bool isValid() const override
IsValid.
virtual void output() override
Perform the debugging output.
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...
A class for producing various debug related outputs.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
const MaterialWarehouse & getMaterialWarehouse() const
const NonlinearSystemBase & _nl
Reference to MOOSE&#39;s nonlinear system.
virtual const MooseVariableFieldBase & getVariable(const THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) const override
Returns the variable reference for requested variable which must be of the expected_var_type (Nonline...
static const unsigned int console_field_width
Width used for printing simulation information.
Definition: ConsoleUtils.h:30
Based class for output objects.
Definition: Output.h:43
bool contains(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
const std::vector< std::shared_ptr< T > > & getObjects(THREAD_ID tid=0) const
Retrieve complete vector to the all/block/boundary restricted objects for a given thread...
TheWarehouse & theWarehouse() const
const MultiMooseEnum & _scope
multi-enum of object types to show the block-restriction for
void insertNewline(std::stringstream &oss, std::streampos &begin, std::streampos &curr)
Helper function function for stringstream formatting.
Definition: ConsoleUtils.C:572
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:185
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
const std::string & variable_name(const unsigned int i) const
static InputParameters validParams()
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
AuxiliarySystem & getAuxiliarySystem()
registerMooseObject("MooseApp", BlockRestrictionDebugOutput)
virtual void insert(libMesh::NumericVector< libMesh::Number > &vector)=0
Insert the currently cached degree of freedom values into the provided vector.
OStreamProxy out
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:467
static MultiMooseEnum getScopes(std::string default_scopes="")
Get the supported scopes of output (e.g., variables, etc.)
An interface that restricts an object to subdomains via the &#39;blocks&#39; input parameter.
virtual MooseMesh & mesh() override
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...
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
Definition: TheWarehouse.h:285
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...
bool hasBlocks(const SubdomainName &name) const
Test if the supplied block name is valid for this object.
unsigned int n_vars() const
static InputParameters validParams()
Definition: Output.C:32
Base variable class.
const libMesh::System & _sys
Reference to libMesh system.
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:30