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 
24 #include "libmesh/transient_system.h"
25 
26 using namespace libMesh;
27 
29 
32 {
34 
35  params.addParam<MultiMooseEnum>("scope",
36  getScopes("all"),
37  "The types of object to output block coverage for, if nothing is "
38  "provided everything will be output.");
39 
40  params.addParam<NonlinearSystemName>(
41  "nl_sys", "nl0", "The nonlinear system that we should output information for.");
42 
43  params.addClassDescription(
44  "Debug output object for displaying information regarding block-restriction of objects.");
45  params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;
46  return params;
47 }
48 
50 BlockRestrictionDebugOutput::getScopes(std::string default_scopes)
51 {
52  return MultiMooseEnum("none all variables kernels auxvariables auxkernels materials userobjects",
53  default_scopes);
54 }
55 
57  : Output(parameters),
58  _scope(getParam<MultiMooseEnum>("scope")),
59  _nl(_problem_ptr->getNonlinearSystemBase(
60  _problem_ptr->nlSysNum(getParam<NonlinearSystemName>("nl_sys")))),
61  _sys(_nl.system())
62 {
63 }
64 
65 void
67 {
69 }
70 
71 void
73 {
74  // is there anything to do?
75  if (_scope.isValid() && _scope.contains("none"))
76  return;
77 
78  // Build output stream
79  std::stringstream out;
80 
81  auto printCategoryAndNames = [&out](std::string category, std::set<std::string> & names)
82  {
83  const auto n = names.size();
84  if (n > 0)
85  {
86  // print the category and number of items
87  out << " " << category << " (" << n << " " << ((n == 1) ? "item" : "items") << "): ";
88 
89  // If we would just use Moose::stringify(names), the indention is not right.
90  // So we are printing the names one by one and using ConsoleUtils::insertNewline.
91  std::streampos begin_string_pos = out.tellp();
92  std::streampos curr_string_pos = begin_string_pos;
93  unsigned int i = 0;
94  for (const auto & name : names)
95  {
96  out << Moose::stringify(name) << ((i++ < (n - 1)) ? ", " : "");
97  curr_string_pos = out.tellp();
98  ConsoleUtils::insertNewline(out, begin_string_pos, curr_string_pos);
99  }
100  out << '\n';
101  return true;
102  }
103  else
104  {
105  return false;
106  }
107  };
108 
109  // Reference to mesh for getting block names
111 
112  // set of all subdomains in the mesh
113  const std::set<SubdomainID> & mesh_subdomains = mesh.meshSubdomains();
114 
115  // get kernels via reference to the Kernel warehouse
116  const auto & kernel_warehouse = _nl.getKernelWarehouse();
117  const auto & kernels = kernel_warehouse.getObjects(/*tid = */ 0);
118 
119  // AuxSystem
120  const auto & auxSystem = _problem_ptr->getAuxiliarySystem();
121 
122  // Reference to the Material warehouse
123  const auto & material_warehouse = _problem_ptr->getMaterialWarehouse();
124 
125  // get all user objects
126  std::vector<UserObject *> userObjects;
128  .query()
129  .condition<AttribSystem>("UserObject")
130  .condition<AttribThread>(0)
131  .queryIntoUnsorted(userObjects);
132 
133  // do we have to check all object types?
134  const bool include_all = !_scope.isValid() || _scope.contains("all");
135 
136  // iterate all subdomains
137  for (const auto & subdomain_id : mesh_subdomains)
138  {
139  // get the corresponding subdomain name
140  const auto & subdomain_name = mesh.getSubdomainName(subdomain_id);
141 
142  out << "\n";
143  out << " Subdomain '" << subdomain_name << "' (id " << subdomain_id << "):\n";
144 
145  bool objectsFound = false;
146 
147  // Variables
148  if (include_all || _scope.contains("variables"))
149  {
150  std::set<std::string> names;
151  for (unsigned int var_num = 0; var_num < _sys.n_vars(); var_num++)
152  {
153  const auto & var_name = _sys.variable_name(var_num);
154  if (_problem_ptr->hasVariable(var_name))
155  {
156  const MooseVariableBase & var =
157  _problem_ptr->getVariable(/*tid = */ 0,
158  var_name,
161  if (var.hasBlocks(subdomain_id))
162  names.insert(var_name);
163  }
164  }
165  objectsFound = printCategoryAndNames("Variables", names) || objectsFound;
166  }
167 
168  // Kernels
169  if (include_all || _scope.contains("kernels"))
170  {
171  std::set<std::string> names;
172  for (const auto & kernel : kernels)
173  {
174  if (kernel->hasBlocks(subdomain_id))
175  names.insert(kernel->name());
176  }
177  objectsFound = printCategoryAndNames("Kernels", names) || objectsFound;
178  }
179 
180  // AuxVariables
181  if (include_all || _scope.contains("auxvariables"))
182  {
183  std::set<std::string> names;
184  const auto & sys = auxSystem.system();
185  for (unsigned int vg = 0; vg < sys.n_variable_groups(); vg++)
186  {
187  const VariableGroup & vg_description(sys.variable_group(vg));
188  for (unsigned int vn = 0; vn < vg_description.n_variables(); vn++)
189  {
190  if (vg_description.active_on_subdomain(subdomain_id))
191  names.insert(vg_description.name(vn));
192  }
193  }
194  objectsFound = printCategoryAndNames("AuxVariables", names) || objectsFound;
195  }
196 
197  // AuxKernels
198  if (include_all || _scope.contains("auxkernels"))
199  {
200 
201  {
202  const auto & wh = auxSystem.nodalAuxWarehouse();
203  std::set<std::string> names;
204  if (wh.hasActiveBlockObjects(subdomain_id))
205  {
206  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
207  for (auto & auxkernel : auxkernels)
208  names.insert(auxkernel->name());
209  }
210  objectsFound = printCategoryAndNames("AuxKernels[nodal]", names) || objectsFound;
211  }
212 
213  {
214  const auto & wh = auxSystem.nodalVectorAuxWarehouse();
215  std::set<std::string> names;
216  if (wh.hasActiveBlockObjects(subdomain_id))
217  {
218  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
219  for (auto & auxkernel : auxkernels)
220  names.insert(auxkernel->name());
221  }
222  objectsFound = printCategoryAndNames("AuxKernels[nodalVector]", names) || objectsFound;
223  }
224 
225  {
226  const auto & wh = auxSystem.nodalArrayAuxWarehouse();
227  std::set<std::string> names;
228  if (wh.hasActiveBlockObjects(subdomain_id))
229  {
230  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
231  for (auto & auxkernel : auxkernels)
232  names.insert(auxkernel->name());
233  }
234  objectsFound = printCategoryAndNames("AuxKernels[nodalArray]", names) || objectsFound;
235  }
236 
237  {
238  const auto & wh = auxSystem.elemAuxWarehouse();
239  std::set<std::string> names;
240  if (wh.hasActiveBlockObjects(subdomain_id))
241  {
242  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
243  for (auto & auxkernel : auxkernels)
244  names.insert(auxkernel->name());
245  }
246  objectsFound = printCategoryAndNames("AuxKernels[elemAux]", names) || objectsFound;
247  }
248 
249  {
250  const auto & wh = auxSystem.elemVectorAuxWarehouse();
251  std::set<std::string> names;
252  if (wh.hasActiveBlockObjects(subdomain_id))
253  {
254  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
255  for (auto & auxkernel : auxkernels)
256  names.insert(auxkernel->name());
257  }
258  objectsFound = printCategoryAndNames("AuxKernels[elemVector]", names) || objectsFound;
259  }
260 
261  {
262  const auto & wh = auxSystem.elemArrayAuxWarehouse();
263  std::set<std::string> names;
264  if (wh.hasActiveBlockObjects(subdomain_id))
265  {
266  const auto & auxkernels = wh.getActiveBlockObjects(subdomain_id);
267  for (auto & auxkernel : auxkernels)
268  names.insert(auxkernel->name());
269  }
270  objectsFound = printCategoryAndNames("AuxKernels[elemArray]", names) || objectsFound;
271  }
272  }
273 
274  // Materials
275  if (include_all || _scope.contains("materials"))
276  {
277  std::set<std::string> names;
278  if (material_warehouse.hasActiveBlockObjects(subdomain_id))
279  {
280  auto const objs = material_warehouse.getBlockObjects(subdomain_id);
281  for (const auto & mat : objs)
282  names.insert(mat->name());
283  }
284  objectsFound = printCategoryAndNames("Materials", names) || objectsFound;
285  }
286 
287  // UserObjects
288  if (include_all || _scope.contains("userobjects"))
289  {
290  std::set<std::string> names;
291  for (const auto & obj : userObjects)
292  if (BlockRestrictable * blockrestrictable_obj = dynamic_cast<BlockRestrictable *>(obj))
293  if (blockrestrictable_obj->hasBlocks(subdomain_id))
294  names.insert(obj->name());
295  objectsFound = printCategoryAndNames("UserObjects", names) || objectsFound;
296  }
297 
298  if (!objectsFound)
299  out << " (no objects found)\n";
300  }
301 
302  out << std::flush;
303 
304  // Write the stored string to the ConsoleUtils output objects
305  _console << "\n[DBG] Block-Restrictions (" << mesh_subdomains.size()
306  << " subdomains): showing active objects\n";
307  _console << std::setw(ConsoleUtils::console_field_width) << out.str() << std::endl;
308 }
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 std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
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::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:571
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:88
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:466
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:284
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
const libMesh::System & _sys
Reference to libMesh system.
const ExecFlagType EXEC_INITIAL
Definition: Moose.C:28