www.mooseframework.org
DumpObjectsProblem.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 #include "DumpObjectsProblem.h"
12 #include "AuxiliarySystem.h"
13 #include <sstream>
14 
15 #include "libmesh/string_to_enum.h"
16 
18 
21 {
23  params.addClassDescription("Single purpose problem object that does not run the given input but "
24  "allows deconstructing actions into their series of underlying Moose "
25  "objects and variables.");
26  params.addRequiredParam<std::string>(
27  "dump_path", "Syntax path of the action of which to dump the generated syntax");
28  return params;
29 }
30 
32  : FEProblemBase(parameters), _nl_sys(std::make_shared<DumpObjectsNonlinearSystem>(*this, "nl0"))
33 {
34  _nl[0] = _nl_sys;
35  _aux = std::make_shared<AuxiliarySystem>(*this, "aux0");
37 
38  // Create extra vectors and matrices if any
40 
41  // Create extra solution vectors if any
43 }
44 
45 std::string
47  const InputParameters & parameters)
48 {
49  auto factory_params = stringifyParameters(_factory.getValidParams(type));
50  auto specified_params = stringifyParameters(parameters);
51 
52  std::string param_text;
53  for (auto & value_pair : specified_params)
54  {
55  // parameter name
56  const auto & param_name = value_pair.first;
57  const auto & param_value = value_pair.second;
58 
59  auto factory_it = factory_params.find(param_name);
60  if (factory_it == factory_params.end() || factory_it->second != param_value)
61  param_text += " " + param_name + " = " + param_value + '\n';
62  }
63 
64  return param_text;
65 }
66 
67 void
68 DumpObjectsProblem::dumpObjectHelper(const std::string & system,
69  const std::string & type,
70  const std::string & name,
71  const InputParameters & parameters)
72 {
74  auto param_text = deduceNecessaryParameters(type, parameters);
75 
76  // clang-format off
77  _generated_syntax[path][system] +=
78  " [./" + name + "]\n"
79  + " type = " + type + '\n'
80  + param_text
81  + " [../]\n";
82  // clang-format on
83 }
84 
85 void
86 DumpObjectsProblem::dumpVariableHelper(const std::string & system,
87  const std::string & var_name,
88  FEFamily family,
89  Order order,
90  Real scale_factor,
91  const std::set<SubdomainID> * const active_subdomains)
92 {
94  std::string param_text;
95 
96  if (active_subdomains)
97  {
98  std::string blocks;
99  for (auto & subdomain_id : *active_subdomains)
100  {
101  auto subdomain_name = _mesh.getMesh().subdomain_name(subdomain_id);
102  if (subdomain_name == "")
103  subdomain_name = std::to_string(subdomain_id);
104 
105  if (!blocks.empty())
106  blocks += ' ';
107 
108  blocks += subdomain_name;
109  }
110 
111  if (active_subdomains->size() > 1)
112  blocks = "'" + blocks + "'";
113 
114  param_text += " blocks = " + blocks + '\n';
115  }
116 
117  if (family != LAGRANGE)
118  param_text += " family = " + libMesh::Utility::enum_to_string<FEFamily>(family) + '\n';
119  if (order != FIRST)
120  param_text += " order = " + libMesh::Utility::enum_to_string<Order>(order) + '\n';
121  if (scale_factor != 1.0)
122  param_text += " scale = " + std::to_string(scale_factor);
123 
124  // clang-format off
125  _generated_syntax[path][system] +=
126  " [./" + var_name + "]\n"
127  + param_text
128  + " [../]\n";
129  // clang-format on
130 }
131 
132 void
134 {
135  dumpGeneratedSyntax(getParam<std::string>("dump_path"));
136 }
137 
138 void
140 {
141  auto pathit = _generated_syntax.find(path);
142  if (pathit == _generated_syntax.end())
143  return;
144 
145  Moose::out << "**START DUMP DATA**\n";
146  for (const auto & system_pair : pathit->second)
147  Moose::out << '[' << system_pair.first << "]\n" << system_pair.second << "[]\n\n";
148  Moose::out << "**END DUMP DATA**\n";
149  Moose::out << std::flush;
150 }
151 
152 std::map<std::string, std::string>
154 {
155  std::map<std::string, std::string> parameter_map;
156 
157  std::string syntax;
158  if (parameters.isParamValid("parser_syntax"))
159  syntax = parameters.get<std::string>("parser_syntax");
160 
161  for (auto & value_pair : parameters)
162  {
163  // parameter name
164  const auto & param_name = value_pair.first;
165 
166  if (!parameters.isPrivate(param_name) && parameters.isParamValid(param_name))
167  {
168  if (param_name == "control_tags")
169  {
170  // deal with the control tags. The current parser_syntax is automatically added to this. So
171  // we can remove the parameter if that's all there is in it
172  }
173  else
174  {
175  // special treatment for some types
176 
177  // parameter value
178  std::string param_value;
179  if (parameters.have_parameter<bool>(param_name))
180  {
181  const bool & b = parameters.get<bool>(param_name);
182  param_value = b ? "true" : "false";
183  }
184  else
185  {
186  std::stringstream ss;
187  value_pair.second->print(ss);
188  param_value = ss.str();
189  }
190 
191  // delete trailing space
192  if (!param_value.empty() && param_value.back() == ' ')
193  param_value.pop_back();
194 
195  // add quotes if the parameter contains spaces or is empty
196  if (param_value.find_first_of(" ") != std::string::npos || param_value.length() == 0)
197  param_value = "'" + param_value + "'";
198 
199  parameter_map[param_name] = param_value;
200  }
201  }
202  }
203 
204  return parameter_map;
205 }
LAGRANGE
Order
Factory & _factory
The Factory for building objects.
Definition: SubProblem.h:972
static InputParameters validParams()
std::shared_ptr< DumpObjectsNonlinearSystem > _nl_sys
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
Specialization of SubProblem for dumping generated objects as input file syntax.
FIRST
std::string getCurrentActionName() const
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:67
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
bool isPrivate(const std::string &name) const
Returns a Boolean indicating whether the specified parameter is private or not.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
virtual void newAssemblyArray(std::vector< std::shared_ptr< NonlinearSystemBase >> &nl)
void dumpObjectHelper(const std::string &system, const std::string &type, const std::string &name, const InputParameters &parameters)
std::vector< std::shared_ptr< NonlinearSystemBase > > _nl
The nonlinear systems.
Nonlinear system for dumping objects.
void createTagSolutions()
Create extra tagged solution vectors.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3198
void createTagVectors()
Create extra tagged vectors and matrices.
virtual void solve(unsigned int nl_sys_num) override
output data in solve
std::shared_ptr< AuxiliarySystem > _aux
The auxiliary system.
MooseMesh & _mesh
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:50
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:206
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
std::map< std::string, std::string > stringifyParameters(const InputParameters &parameters)
create a string map form parameter names to stringified parameter values
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
std::map< std::string, std::map< std::string, std::string > > _generated_syntax
store input syntax to build objects generated by a specific action
registerMooseObject("MooseApp", DumpObjectsProblem)
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...
const InputParameters & parameters() const
Get the parameters of the object.
static InputParameters validParams()
void dumpGeneratedSyntax(const std::string path)
output input blocks for a given action path
FEFamily
std::string deduceNecessaryParameters(const std::string &type, const InputParameters &parameters)
build a text snippet of the minimal set of parameters that need to be specified
DumpObjectsProblem(const InputParameters &parameters)
void dumpVariableHelper(const std::string &system, const std::string &var_name, FEFamily family, Order order, Real scale_factor, const std::set< SubdomainID > *const active_subdomains)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.