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
25
28{
30 params.addClassDescription("Single purpose problem object that does not run the given input but "
31 "allows deconstructing actions into their series of underlying Moose "
32 "objects and variables.");
33 params.addParam<std::string>(
34 "dump_path", "all", "Syntax path of the action of which to dump the generated syntax");
35 params.addParam<bool>(
36 "include_all_user_specified_params",
37 true,
38 "Whether to include all parameters that have been specified by a user in the dump, even if "
39 "they match the default value of the parameter in the Factory");
40
41 // Change the default because any complex solve or executioners needs the problem to perform its
42 // setup duties (all the calls in initialSetup()) which are skipped by the DumpObjectsProblem
43 params.addParam<bool>(
44 "solve",
45 false,
46 "Whether to attempt to solve the Problem. This will only cause additional outputs of the "
47 "objects and their parameters. This is unlikely to succeed with more complex executioners.");
48 return params;
49}
50
52 : FEProblemBase(parameters),
53 _include_all_user_specified_params(getParam<bool>("include_all_user_specified_params"))
54{
55 // Make dummy systems based on parameters passed
56 _solver_systems.resize(0);
57 for (const auto i : index_range(_nl_sys_names))
58 {
59 const auto & sys_name = _nl_sys_names[i];
60 const auto & new_sys = std::make_shared<DumpObjectsNonlinearSystem>(*this, sys_name);
61 _solver_systems.push_back(new_sys);
62 _nl[i] = new_sys;
63 }
64 for (const auto i : index_range(_linear_sys_names))
65 {
66 const auto & sys_name = _linear_sys_names[i];
67 const auto & new_sys = std::make_shared<DumpObjectsLinearSystem>(*this, sys_name);
68 _solver_systems.push_back(new_sys);
69 _linear_systems[i] = new_sys;
70 }
71 _aux = std::make_shared<AuxiliarySystem>(*this, "aux0");
72
73 // Create a dummy assembly for the systems at hand
75
76 // Create extra vectors and matrices if any
78
79 // Create extra solution vectors if any
81
82 // Add an action to call printObjects at the end of the action/tasks phase
83 // NOTE: We previously relied on problem.solve() but some executioners (SIMPLE in NavierStokes) do
84 // not support this
85 auto action_params = _app.getActionFactory().getValidParams("DumpObjectsAction");
86 action_params.applyParameters(parameters);
87 auto dump_objects_action =
88 _app.getActionFactory().create("DumpObjectsAction", "dump_objects", action_params);
89 _app.actionWarehouse().addActionBlock(dump_objects_action);
90}
91
92std::string
94 const InputParameters & parameters)
95{
96 auto factory_params = stringifyParameters(_factory.getValidParams(type));
97 auto specified_params = stringifyParameters(parameters);
98
99 std::string param_text;
100 for (auto & value_pair : specified_params)
101 {
102 // parameter name
103 const auto & param_name = value_pair.first;
104 const auto & param_value = value_pair.second;
105
106 // determine whether to include the parameter
107 auto factory_it = factory_params.find(param_name);
108 bool include_param = (factory_it->second != param_value);
110 include_param = include_param || parameters.isParamSetByUser(param_name);
111 if (factory_it == factory_params.end() || include_param)
112 param_text += " " + param_name + " = " + param_value + '\n';
113 }
114
115 return param_text;
116}
117
118void
119DumpObjectsProblem::dumpObjectHelper(const std::string & system,
120 const std::string & type,
121 const std::string & name,
122 const InputParameters & parameters)
123{
125 auto param_text = deduceNecessaryParameters(type, parameters);
126
127 // clang-format off
128 _generated_syntax[path][system] +=
129 " [" + name + "]\n"
130 + " type = " + type + '\n'
131 + param_text
132 + " []\n";
133 // clang-format on
134}
135
136void
137DumpObjectsProblem::dumpVariableHelper(const std::string & system,
138 const std::string & var_name,
139 FEFamily family,
140 Order order,
141 Real scale_factor,
142 const std::set<SubdomainID> * const active_subdomains)
143{
145 std::string param_text;
146
147 if (active_subdomains)
148 {
149 std::string blocks;
150 for (auto & subdomain_id : *active_subdomains)
151 {
152 auto subdomain_name = _mesh.getMesh().subdomain_name(subdomain_id);
153 if (subdomain_name == "")
154 subdomain_name = std::to_string(subdomain_id);
155
156 if (!blocks.empty())
157 blocks += ' ';
158
159 blocks += subdomain_name;
160 }
161
162 if (active_subdomains->size() > 1)
163 blocks = "'" + blocks + "'";
164
165 param_text += " blocks = " + blocks + '\n';
166 }
167
168 if (family != LAGRANGE)
169 param_text += " family = " + libMesh::Utility::enum_to_string<FEFamily>(family) + '\n';
170 if (order != FIRST)
171 param_text += " order = " + libMesh::Utility::enum_to_string<Order>(order) + '\n';
172 if (scale_factor != 1.0)
173 param_text += " scale = " + std::to_string(scale_factor);
174
175 // clang-format off
176 _generated_syntax[path][system] +=
177 " [" + var_name + "]\n"
178 + param_text
179 + " []\n";
180 // clang-format on
181}
182
183void
185{
186 const auto path = getParam<std::string>("dump_path");
187 if (path != "all")
189 else
191}
192
193void
195{
196 auto pathit = _generated_syntax.find(path);
197 if (pathit == _generated_syntax.end())
198 return;
199
200 Moose::out << "**START DUMP DATA**\n";
201 for (const auto & system_pair : pathit->second)
202 Moose::out << '[' << system_pair.first << "]\n" << system_pair.second << "[]\n\n";
203 Moose::out << "**END DUMP DATA**\n";
204 Moose::out << std::flush;
205}
206
207void
209{
210 Moose::out << "**START DUMP DATA**\n";
211 for (const auto & path : _generated_syntax)
212 for (const auto & system_pair : path.second)
213 Moose::out << '[' << system_pair.first << "]\n" << system_pair.second << "[]\n\n";
214 Moose::out << "**END DUMP DATA**\n";
215 Moose::out << std::flush;
216}
217
218std::map<std::string, std::string>
220{
221 std::map<std::string, std::string> parameter_map;
222
223 std::string syntax;
224 if (parameters.isParamValid("parser_syntax"))
225 syntax = parameters.get<std::string>("parser_syntax");
226
227 for (auto & value_pair : parameters)
228 {
229 // parameter name
230 const auto & param_name = value_pair.first;
231
232 if (!parameters.isPrivate(param_name) && parameters.isParamValid(param_name))
233 {
234 if (param_name == "control_tags")
235 {
236 // deal with the control tags. The current parser_syntax is automatically added to this. So
237 // we can remove the parameter if that's all there is in it
238 }
239 else
240 {
241 // special treatment for some types
242
243 // parameter value
244 std::string param_value;
245 if (parameters.have_parameter<bool>(param_name))
246 {
247 const bool & b = parameters.get<bool>(param_name);
248 param_value = b ? "true" : "false";
249 }
250 else
251 {
252 std::stringstream ss;
253 value_pair.second->print(ss);
254 param_value = ss.str();
255 }
256
257 // delete trailing space
258 if (!param_value.empty() && param_value.back() == ' ')
259 param_value.pop_back();
260
261 // add quotes if the parameter contains spaces or is empty
262 if (param_value.find_first_of(" ") != std::string::npos || param_value.length() == 0)
263 param_value = "'" + param_value + "'";
264
265 parameter_map[param_name] = param_value;
266 }
267 }
268 }
269
270 return parameter_map;
271}
272
273void
275{
276 TIME_SECTION("initializingFunctions", 5, "Initializing Functions");
277 mooseAssert(libMesh::n_threads() == 1, "We should only use one thread for dumping objects");
278
279 // Call the initialSetup methods for functions
280 // We need to do that at least for the parsed functions that can be used as parameters
281 // in the input file
282 // Note that we are not planning to use the functions, which is why we are not re-initing scalar
283 // variables
285}
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()
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition Factory.C:68
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:3557
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.
unsigned int n_threads()