https://mooseframework.inl.gov
Loading...
Searching...
No Matches
DumpObjectsProblem.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#include "DumpObjectsProblem.h"
11#include "DumpObjectsAction.h"
14#include "AuxiliarySystem.h"
15#include "InputParameters.h"
16#include "NonlinearSystem.h"
17#include "LinearSystem.h"
18#include "SolverSystem.h"
19#include "Function.h"
20#include <sstream>
21
22#include "libmesh/string_to_enum.h"
23
24using namespace libMesh;
25
27
30{
32 params.addClassDescription("Single purpose problem object that does not run the given input but "
33 "allows deconstructing actions into their series of underlying Moose "
34 "objects and variables.");
35 params.addParam<std::string>(
36 "dump_path", "all", "Syntax path of the action of which to dump the generated syntax");
37 params.addParam<bool>(
38 "include_all_user_specified_params",
39 true,
40 "Whether to include all parameters that have been specified by a user in the dump, even if "
41 "they match the default value of the parameter in the Factory");
42
43 // Change the default because any complex solve or executioners needs the problem to perform its
44 // setup duties (all the calls in initialSetup()) which are skipped by the DumpObjectsProblem
45 params.addParam<bool>(
46 "solve",
47 false,
48 "Whether to attempt to solve the Problem. This will only cause additional outputs of the "
49 "objects and their parameters. This is unlikely to succeed with more complex executioners.");
50 return params;
51}
52
54 : FEProblemBase(parameters),
55 _include_all_user_specified_params(getParam<bool>("include_all_user_specified_params"))
56{
57 // Make dummy systems based on parameters passed
58 _solver_systems.resize(0);
59 for (const auto i : index_range(_nl_sys_names))
60 {
61 const auto & sys_name = _nl_sys_names[i];
62 const auto & new_sys = std::make_shared<DumpObjectsNonlinearSystem>(*this, sys_name);
63 _solver_systems.push_back(new_sys);
64 _nl[i] = new_sys;
65 }
66 for (const auto i : index_range(_linear_sys_names))
67 {
68 const auto & sys_name = _linear_sys_names[i];
69 const auto & new_sys = std::make_shared<DumpObjectsLinearSystem>(*this, sys_name);
70 _solver_systems.push_back(new_sys);
71 _linear_systems[i] = new_sys;
72 }
73 _aux = std::make_shared<AuxiliarySystem>(*this, "aux0");
74
75 // Create a dummy assembly for the systems at hand
77
78 // Create extra vectors and matrices if any
80
81 // Create extra solution vectors if any
83
84 // Add an action to call printObjects at the end of the action/tasks phase
85 // NOTE: We previously relied on problem.solve() but some executioners (SIMPLE in NavierStokes) do
86 // not support this
87 auto action_params = _app.getActionFactory().getValidParams("DumpObjectsAction");
88 action_params.applyParameters(parameters);
89 auto dump_objects_action =
90 _app.getActionFactory().create("DumpObjectsAction", "dump_objects", action_params);
91 _app.actionWarehouse().addActionBlock(dump_objects_action);
92}
93
94std::string
96 const InputParameters & parameters)
97{
98 auto factory_params = stringifyParameters(_factory.getValidParams(type));
99 auto specified_params = stringifyParameters(parameters);
100
101 std::string param_text;
102 for (auto & value_pair : specified_params)
103 {
104 // parameter name
105 const auto & param_name = value_pair.first;
106 const auto & param_value = value_pair.second;
107
108 // determine whether to include the parameter
109 auto factory_it = factory_params.find(param_name);
110 bool include_param = (factory_it->second != param_value);
112 include_param = include_param || parameters.isParamSetByUser(param_name);
113 if (factory_it == factory_params.end() || include_param)
114 param_text += " " + param_name + " = " + param_value + '\n';
115 }
116
117 return param_text;
118}
119
120void
121DumpObjectsProblem::dumpObjectHelper(const std::string & system,
122 const std::string & type,
123 const std::string & name,
124 const InputParameters & parameters)
125{
127 auto param_text = deduceNecessaryParameters(type, parameters);
128
129 // clang-format off
130 _generated_syntax[path][system] +=
131 " [" + name + "]\n"
132 + " type = " + type + '\n'
133 + param_text
134 + " []\n";
135 // clang-format on
136}
137
138void
139DumpObjectsProblem::dumpVariableHelper(const std::string & system,
140 const std::string & var_name,
141 FEFamily family,
142 Order order,
143 Real scale_factor,
144 const std::set<SubdomainID> * const active_subdomains)
145{
147 std::string param_text;
148
149 if (active_subdomains)
150 {
151 std::string blocks;
152 for (auto & subdomain_id : *active_subdomains)
153 {
154 auto subdomain_name = _mesh.getMesh().subdomain_name(subdomain_id);
155 if (subdomain_name == "")
156 subdomain_name = std::to_string(subdomain_id);
157
158 if (!blocks.empty())
159 blocks += ' ';
160
161 blocks += subdomain_name;
162 }
163
164 if (active_subdomains->size() > 1)
165 blocks = "'" + blocks + "'";
166
167 param_text += " blocks = " + blocks + '\n';
168 }
169
170 if (family != LAGRANGE)
171 param_text += " family = " + libMesh::Utility::enum_to_string<FEFamily>(family) + '\n';
172 if (order != FIRST)
173 param_text += " order = " + libMesh::Utility::enum_to_string<Order>(order) + '\n';
174 if (scale_factor != 1.0)
175 param_text += " scale = " + std::to_string(scale_factor);
176
177 // clang-format off
178 _generated_syntax[path][system] +=
179 " [" + var_name + "]\n"
180 + param_text
181 + " []\n";
182 // clang-format on
183}
184
185void
187{
188 const auto path = getParam<std::string>("dump_path");
189 if (path != "all")
191 else
193}
194
195void
197{
198 auto pathit = _generated_syntax.find(path);
199 if (pathit == _generated_syntax.end())
200 return;
201
202 Moose::out << "**START DUMP DATA**\n";
203 for (const auto & system_pair : pathit->second)
204 Moose::out << '[' << system_pair.first << "]\n" << system_pair.second << "[]\n\n";
205 Moose::out << "**END DUMP DATA**\n";
206 Moose::out << std::flush;
207}
208
209void
211{
212 Moose::out << "**START DUMP DATA**\n";
213 for (const auto & path : _generated_syntax)
214 for (const auto & system_pair : path.second)
215 Moose::out << '[' << system_pair.first << "]\n" << system_pair.second << "[]\n\n";
216 Moose::out << "**END DUMP DATA**\n";
217 Moose::out << std::flush;
218}
219
220std::map<std::string, std::string>
222{
223 std::map<std::string, std::string> parameter_map;
224
225 std::string syntax;
226 if (parameters.isParamValid("parser_syntax"))
227 syntax = parameters.get<std::string>("parser_syntax");
228
229 for (auto & value_pair : parameters)
230 {
231 // parameter name
232 const auto & param_name = value_pair.first;
233
234 if (!parameters.isPrivate(param_name) && parameters.isParamValid(param_name))
235 {
236 if (param_name == "control_tags")
237 {
238 // deal with the control tags. The current parser_syntax is automatically added to this. So
239 // we can remove the parameter if that's all there is in it
240 }
241 else
242 {
243 // special treatment for some types
244
245 // parameter value
246 std::string param_value;
247 if (parameters.have_parameter<bool>(param_name))
248 {
249 const bool & b = parameters.get<bool>(param_name);
250 param_value = b ? "true" : "false";
251 }
252 else
253 {
254 std::stringstream ss;
255 value_pair.second->print(ss);
256 param_value = ss.str();
257 }
258
259 // delete trailing space
260 if (!param_value.empty() && param_value.back() == ' ')
261 param_value.pop_back();
262
263 // add quotes if the parameter contains spaces or is empty
264 if (param_value.find_first_of(" ") != std::string::npos || param_value.length() == 0)
265 param_value = "'" + param_value + "'";
266
267 parameter_map[param_name] = param_value;
268 }
269 }
270 }
271
272 return parameter_map;
273}
274
275void
277{
278 TIME_SECTION("initializingFunctions", 5, "Initializing Functions");
279 mooseAssert(libMesh::n_threads() == 1, "We should only use one thread for dumping objects");
280
281 // Call the initialSetup methods for functions
282 // We need to do that at least for the parsed functions that can be used as parameters
283 // in the input file
284 // Note that we are not planning to use the functions, which is why we are not re-initing scalar
285 // variables
287}
registerMooseObject("MooseApp", DumpObjectsProblem)
char ** blocks
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters &parameters)
InputParameters getValidParams(const std::string &name)
std::string getCurrentActionName() const
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
Specialization of SubProblem for dumping generated objects as input file syntax.
std::map< std::string, std::string > stringifyParameters(const InputParameters &parameters)
create a string map form parameter names to stringified parameter values
DumpObjectsProblem(const InputParameters &parameters)
void dumpAllGeneratedSyntax() const
output input blocks for all paths
const bool _include_all_user_specified_params
Whether to include all user-specified parameters in the dump or only parameters that differ from the ...
void dumpVariableHelper(const std::string &system, const std::string &var_name, libMesh::FEFamily family, Order order, Real scale_factor, const std::set< SubdomainID > *const active_subdomains)
virtual void initialSetup() override
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
void dumpObjectHelper(const std::string &system, const std::string &type, const std::string &name, const InputParameters &parameters)
static InputParameters validParams()
std::map< std::string, std::map< std::string, std::string > > _generated_syntax
store input syntax to build objects generated by a specific action
void dumpGeneratedSyntax(const std::string path)
output input blocks for a given action path
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::shared_ptr< AuxiliarySystem > _aux
The auxiliary system.
void createTagVectors()
Create extra tagged vectors and matrices.
MooseMesh & _mesh
std::vector< std::shared_ptr< LinearSystem > > _linear_systems
The vector of linear systems.
virtual void newAssemblyArray(std::vector< std::shared_ptr< SolverSystem > > &solver_systems)
const std::vector< LinearSystemName > _linear_sys_names
The linear system names.
std::vector< std::shared_ptr< NonlinearSystemBase > > _nl
The nonlinear systems.
void createTagSolutions()
Create extra tagged solution vectors.
MooseObjectWarehouse< Function > _functions
functions
std::vector< std::shared_ptr< SolverSystem > > _solver_systems
Combined container to base pointer of every solver system.
const std::vector< NonlinearSystemName > _nl_sys_names
The nonlinear system names.
static InputParameters validParams()
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.
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
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.
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.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
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.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
void applyParameters(const InputParameters &common, const std::vector< std::string > &exclude={}, const bool allow_private=false)
Method for applying common parameters.
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition MooseApp.h:217
ActionFactory & getActionFactory()
Retrieve a writable reference to the ActionFactory associated with this App.
Definition MooseApp.h:412
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3549
virtual void initialSetup(THREAD_ID tid=0) const
Convenience methods for calling object setup methods.
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
Factory & _factory
The Factory for building objects.
std::string & subdomain_name(subdomain_id_type id)
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
auto index_range(const T &sizable)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
unsigned int n_threads()