forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MooseApp.C
915 lines (754 loc) · 33.1 KB
/
MooseApp.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/
// MOOSE includes
#include "MooseApp.h"
#include "AppFactory.h"
#include "MooseSyntax.h"
#include "MooseInit.h"
#include "Executioner.h"
#include "InputFileFormatter.h"
#include "YAMLFormatter.h"
#include "PetscSupport.h"
#include "Conversion.h"
#include "CommandLine.h"
#include "InfixIterator.h"
#include "MultiApp.h"
#include "MeshModifier.h"
#include "DependencyResolver.h"
#include "MooseUtils.h"
// Regular expression includes
#include "pcrecpp.h"
// libMesh includes
#include "libmesh/mesh_refinement.h"
#include "libmesh/string_to_enum.h"
// System include for dynamic library methods
#include <dlfcn.h>
#define QUOTE(macro) stringifyName(macro)
template<>
InputParameters validParams<MooseApp>()
{
InputParameters params;
params.addCommandLineParam<std::string>("input_file", "-i <input_file>", "Specify an input file");
params.addCommandLineParam<std::string>("mesh_only", "--mesh-only", "Setup and Output the input mesh only.");
params.addCommandLineParam<bool>("show_input", "--show-input", false, "Shows the parsed input file before running the simulation.");
params.addCommandLineParam<bool>("show_outputs", "--show-outputs", false, "Shows the output execution time information.");
params.addCommandLineParam<bool>("no_color", "--no-color", false, "Disable coloring of all Console outputs.");
params.addCommandLineParam<bool>("help", "-h --help", false, "Displays CLI usage statement.");
params.addCommandLineParam<std::string>("dump", "--dump [search_string]", "Shows a dump of available input file syntax.");
params.addCommandLineParam<std::string>("yaml", "--yaml", "Dumps input file syntax in YAML format.");
params.addCommandLineParam<bool>("syntax", "--syntax", false, "Dumps the associated Action syntax paths ONLY");
params.addCommandLineParam<bool>("check_input", "--check-input", false, "Check the input file (i.e. requires -i <filename>) and quit.");
params.addCommandLineParam<bool>("list_constructed_objects", "--list-constructed-objects", false, "List all moose object type names constructed by the master app factory.");
params.addCommandLineParam<unsigned int>("n_threads", "--n-threads=<n>", 1, "Runs the specified number of threads per process");
params.addCommandLineParam<bool>("warn_unused", "-w --warn-unused", false, "Warn about unused input file options");
params.addCommandLineParam<bool>("error_unused", "-e --error-unused", false, "Error when encountering unused input file options");
params.addCommandLineParam<bool>("error_override", "-o --error-override", false, "Error when encountering overridden or parameters supplied multiple times");
params.addCommandLineParam<bool>("parallel_mesh", "--parallel-mesh", false, "The libMesh Mesh underlying MooseMesh should always be a ParallelMesh");
params.addCommandLineParam<unsigned int>("refinements", "-r <n>", 0, "Specify additional initial uniform refinements for automatic scaling");
params.addCommandLineParam<std::string>("recover", "--recover [file_base]", "Continue the calculation. If file_base is omitted then the most recent recovery file will be utilized");
params.addCommandLineParam<bool>("half_transient", "--half-transient", false, "When true the simulation will only run half of its specified transient (ie half the timesteps). This is useful for testing recovery and restart");
// No default on these two options, they must not both be valid
params.addCommandLineParam<bool>("trap_fpe", "--trap-fpe", "Enable Floating Point Exception handling in critical sections of code. This is enabled automatically in DEBUG mode");
params.addCommandLineParam<bool>("no_trap_fpe", "--no-trap-fpe", "Disable Floating Point Exception handling in critical sections of code when using DEBUG mode.");
params.addCommandLineParam<bool>("error", "--error", false, "Turn all warnings into errors");
params.addCommandLineParam<bool>("timing", "-t --timing", false, "Enable all performance logging for timing purposes. This will disable all screen output of performance logs for all Console objects.");
// Legacy Flags
params.addParam<bool>("use_legacy_uo_aux_computation", true, "Set to true to have MOOSE recompute *all* AuxKernel types every time *any* UserObject type is executed.\nThis behavoir is non-intuitive and will be removed late fall 2014, The default is controlled through MooseApp");
params.addParam<bool>("use_legacy_uo_initialization", true, "Set to true to have MOOSE compute all UserObjects and Postprocessors during the initial setup phase of the problem recompute *all* AuxKernel types every time *any* UserObject type is executed.\nThis behavoir is non-intuitive and will be removed late fall 2014, The default is controlled through MooseApp");
params.addParam<bool>("use_legacy_output_syntax", true, "Set to true when using 'output_on' instead of 'execute_on' output syntax");
// Options ignored by MOOSE but picked up by libMesh, these are here so that they are displayed in the application help
params.addCommandLineParam<bool>("keep_cout", "--keep-cout", false, "Keep standard output from all processors when running in parallel");
params.addCommandLineParam<bool>("redirect_stdout", "--redirect-stdout", false, "Keep standard output from all processors when running in parallel");
params.addPrivateParam<std::string>("_type");
params.addPrivateParam<int>("_argc");
params.addPrivateParam<char**>("_argv");
params.addPrivateParam<MooseSharedPointer<CommandLine> >("_command_line");
params.addPrivateParam<MooseSharedPointer<Parallel::Communicator> >("_comm");
return params;
}
// Free function for removing cli flags
bool isFlag(const std::string s)
{
return s.length() && s[0] == '-';
}
MooseApp::MooseApp(InputParameters parameters) :
ConsoleStreamInterface(*this),
ParallelObject(*parameters.get<MooseSharedPointer<Parallel::Communicator> >("_comm")), // Can't call getParam() before pars is set
_name(parameters.get<std::string>("name")),
_pars(parameters),
_comm(getParam<MooseSharedPointer<Parallel::Communicator> >("_comm")),
_output_position_set(false),
_start_time_set(false),
_start_time(0.0),
_global_time_offset(0.0),
_input_parameter_warehouse(new InputParameterWarehouse()),
_action_factory(*this),
_action_warehouse(*this, _syntax, _action_factory),
_parser(*this, _action_warehouse),
_use_nonlinear(true),
_enable_unused_check(WARN_UNUSED),
_factory(*this),
_error_overridden(false),
_ready_to_exit(false),
_initial_from_file(false),
_parallel_mesh_on_command_line(false),
_recover(false),
_restart(false),
_half_transient(false),
_legacy_uo_aux_computation_default(getParam<bool>("use_legacy_uo_aux_computation")),
_legacy_uo_initialization_default(getParam<bool>("use_legacy_uo_initialization")),
_check_input(getParam<bool>("check_input")),
_restartable_data(libMesh::n_threads()),
_multiapp_level(0),
_use_legacy_output_syntax(getParam<bool>("use_legacy_output_syntax"))
{
if (isParamValid("_argc") && isParamValid("_argv"))
{
int argc = getParam<int>("_argc");
char ** argv = getParam<char**>("_argv");
_sys_info = MooseSharedPointer<SystemInfo>(new SystemInfo(argc, argv));
}
if (isParamValid("_command_line"))
_command_line = getParam<MooseSharedPointer<CommandLine> >("_command_line");
else
mooseError("Valid CommandLine object required");
}
MooseApp::~MooseApp()
{
_action_warehouse.clear();
_executioner.reset();
delete _input_parameter_warehouse;
#ifdef LIBMESH_HAVE_DLOPEN
// Close any open dynamic libraries
for (std::map<std::pair<std::string, std::string>, void *>::iterator it = _lib_handles.begin(); it != _lib_handles.end(); ++it)
dlclose(it->second);
#endif
}
void
MooseApp::setupOptions()
{
// Print the header, this is as early as possible
std::string hdr = header();
if (multiAppLevel() > 0)
MooseUtils::indentMessage(_name, hdr);
Moose::out << hdr << std::endl;
if (getParam<bool>("error_unused"))
setCheckUnusedFlag(true);
else if (getParam<bool>("warn_unused"))
setCheckUnusedFlag(false);
if (getParam<bool>("error_override"))
setErrorOverridden();
_parallel_mesh_on_command_line = getParam<bool>("parallel_mesh");
_half_transient = getParam<bool>("half_transient");
_pars.set<bool>("timing") = getParam<bool>("timing");
if (isParamValid("trap_fpe") && isParamValid("no_trap_fpe"))
mooseError("Cannot use both \"--trap-fpe\" and \"--no-trap-fpe\" flags.");
if (isParamValid("trap_fpe"))
Moose::_trap_fpe = true;
else if (isParamValid("no_trap_fpe"))
Moose::_trap_fpe = false;
Moose::_warnings_are_errors = getParam<bool>("error");
Moose::_color_console = !getParam<bool>("no_color");
if (getParam<bool>("help"))
{
_command_line->printUsage();
_ready_to_exit = true;
}
else if (isParamValid("dump"))
{
_parser.initSyntaxFormatter(Parser::INPUT_FILE, true);
// Get command line argument following --dump on command line
std::string dump_following_arg = getParam<std::string>("dump");
// If the argument following --dump is non-existent or begins with
// a dash, call buildFullTree() with an empty string, otherwise
// pass the argument following --dump.
if (dump_following_arg.empty() || (dump_following_arg.find('-') == 0))
_parser.buildFullTree("");
else
_parser.buildFullTree(dump_following_arg);
_ready_to_exit = true;
}
else if (isParamValid("yaml"))
{
_parser.initSyntaxFormatter(Parser::YAML, true);
// Get command line argument following --yaml on command line
std::string yaml_following_arg = getParam<std::string>("yaml");
// If the argument following --yaml is non-existent or begins with
// a dash, call buildFullTree() with an empty string, otherwise
// pass the argument following --yaml.
if (yaml_following_arg.empty() || (yaml_following_arg.find('-') == 0))
_parser.buildFullTree("");
else
_parser.buildFullTree(yaml_following_arg);
_ready_to_exit = true;
}
else if (getParam<bool>("syntax"))
{
std::multimap<std::string, Syntax::ActionInfo> syntax = _syntax.getAssociatedActions();
Moose::out << "**START SYNTAX DATA**\n";
for (std::multimap<std::string, Syntax::ActionInfo>::iterator it = syntax.begin(); it != syntax.end(); ++it)
Moose::out << it->first << "\n";
Moose::out << "**END SYNTAX DATA**\n" << std::endl;
_ready_to_exit = true;
}
else if (_input_filename != "" || isParamValid("input_file")) // They already specified an input filename
{
if (_input_filename == "")
_input_filename = getParam<std::string>("input_file");
if (isParamValid("recover"))
{
// We need to set the flag manually here since the recover parameter is a string type (takes an optional filename)
_recover = true;
// Get command line argument following --recover on command line
std::string recover_following_arg = getParam<std::string>("recover");
// If the argument following --recover is non-existent or begins with
// a dash then we are going to eventually find the newest recovery file to use
if (!(recover_following_arg.empty() || (recover_following_arg.find('-') == 0)))
_recover_base = recover_following_arg;
}
_parser.parse(_input_filename);
_action_warehouse.build();
}
else
{
if (_check_input)
mooseError("You specified --check-input, but did not provide an input file. Add -i <inputfile> to your command line.");
_command_line->printUsage();
_ready_to_exit = true;
}
}
void
MooseApp::setInputFileName(std::string input_filename)
{
_input_filename = input_filename;
}
std::string
MooseApp::getOutputFileBase()
{
return _output_file_base;
}
void
MooseApp::runInputFile()
{
std::string mesh_file_name;
if (isParamValid("mesh_only"))
{
meshOnly(getParam<std::string>("mesh_only"));
_ready_to_exit = true;
}
// If ready to exit has been set, then just return
if (_ready_to_exit)
return;
_action_warehouse.executeAllActions();
if (getParam<bool>("list_constructed_objects"))
{
// TODO: ask multiapps for their constructed objects
std::vector<std::string> obj_list = _factory.getConstructedObjects();
Moose::out << "**START OBJECT DATA**\n";
for (unsigned int i = 0; i < obj_list.size(); ++i)
Moose::out << obj_list[i] << "\n";
Moose::out << "**END OBJECT DATA**\n" << std::endl;
_ready_to_exit = true;
return;
}
bool error_unused = getParam<bool>("error_unused") || _enable_unused_check == ERROR_UNUSED;
bool warn_unused = getParam<bool>("warn_unused") || _enable_unused_check == WARN_UNUSED;
if (error_unused || warn_unused)
{
// Check the input file parameters
std::vector<std::string> all_vars = _parser.getPotHandle()->get_variable_names();
_parser.checkUnidentifiedParams(all_vars, error_unused, true);
// Check the CLI parameters
all_vars = _command_line->getPot()->get_variable_names();
// Remove flags, they aren't "input" parameters
all_vars.erase( std::remove_if(all_vars.begin(), all_vars.end(), isFlag), all_vars.end() );
MooseSharedPointer<FEProblem> fe_problem= _action_warehouse.problem();
if (fe_problem.get() && name() == "main")
{
// Make sure that multiapp overrides were processed properly
int last = all_vars.size() - 1; // last is allowed to go negative
for (int i = 0; i <= last; /* no increment */) // i is an int because last is an int
{
std::string multi_app, variable;
int app_num;
/**
* Command line parameters that contain a colon are assumed to apply to MultiApps
* (e.g. MultiApp_name[num]:fully_qualified_parameter)
*
* Note: Two separate regexs are used since the digit part is optional. Attempting
* to have an optional capture into a non-string type will cause pcrecpp to report
* false. Capturing into a string an converting is more work than just using two
* regexs to begin with.
*/
if (pcrecpp::RE("(.*?)" // Match the MultiApp name
"(\\d+)" // MultiApp number (leave off to apply to all MultiApps with this name)
":" // the colon delimiter
"(.*)" // the variable override that applies to the MultiApp
).FullMatch(all_vars[i], &multi_app, &app_num, &variable) &&
fe_problem->hasMultiApp(multi_app) && // Make sure the MultiApp exists
// Finally make sure the number is in range (if provided)
static_cast<unsigned int>(app_num) < fe_problem->getMultiApp(multi_app)->numGlobalApps())
// delete the current item by copying the last item to this position and decrementing the vector end position
all_vars[i] = all_vars[last--];
else if (pcrecpp::RE("(.*?)" // Same as above without the MultiApp number
":"
"(.*)"
).FullMatch(all_vars[i], &multi_app, &variable) &&
fe_problem->hasMultiApp(multi_app)) // Make sure the MultiApp exists but no need to check numbers
// delete (see comment above)
all_vars[i] = all_vars[last--];
// TODO: check to see if globals are unused
else if (all_vars[i].find(":") == 0)
all_vars[i] = all_vars[last--];
else
// only increment if we didn't "delete", otherwise we'll need to revisit the current index due to copy
++i;
}
mooseAssert(last + 1 >= 0, "index \"last\" is negative");
// Remove the deleted items
all_vars.resize(last+1);
_parser.checkUnidentifiedParams(all_vars, error_unused, false);
}
}
if (getParam<bool>("error_override") || _error_overridden)
_parser.checkOverriddenParams(true);
else
_parser.checkOverriddenParams(false);
}
void
MooseApp::executeExecutioner()
{
// If ready to exit has been set, then just return
if (_ready_to_exit)
return;
// run the simulation
if (_executioner)
{
#ifdef LIBMESH_HAVE_PETSC
Moose::PetscSupport::petscSetupOutput(_command_line.get());
#endif
_executioner->init();
if (_check_input)
{
// Output to stderr, so it is easier for peacock to get the result
Moose::err << "Syntax OK" << std::endl;
return;
}
_executioner->execute();
}
else
mooseError("No executioner was specified (go fix your input file)");
}
bool
MooseApp::isRecovering() const
{
// We never "recover" a sub-app... they just get their state "pushed" to them from above
return _recover;
}
bool
MooseApp::isRestarting() const
{
// We never "restart" a sub-app... they just get their state "pushed" to them from above
return _restart;
}
bool
MooseApp::hasRecoverFileBase()
{
return !_recover_base.empty();
}
void
MooseApp::meshOnly(std::string mesh_file_name)
{
/**
* These actions should be the minimum set necessary to generate and output
* a Mesh.
*/
_action_warehouse.executeActionsWithAction("set_global_params");
_action_warehouse.executeActionsWithAction("setup_mesh");
_action_warehouse.executeActionsWithAction("setup_partitioner");
_action_warehouse.executeActionsWithAction("init_mesh");
_action_warehouse.executeActionsWithAction("prepare_mesh");
_action_warehouse.executeActionsWithAction("add_mesh_modifier");
_action_warehouse.executeActionsWithAction("execute_mesh_modifiers");
_action_warehouse.executeActionsWithAction("uniform_refine_mesh");
_action_warehouse.executeActionsWithAction("setup_mesh_complete");
MooseSharedPointer<MooseMesh> & mesh = _action_warehouse.mesh();
// If no argument specified or if the argument following --mesh-only starts
// with a dash, try to build an output filename based on the input mesh filename.
if (mesh_file_name.empty() || (mesh_file_name.find('-') == 0))
{
mesh_file_name = _parser.getFileName();
size_t pos = mesh_file_name.find_last_of('.');
// Default to writing out an ExodusII mesh base on the input filename.
mesh_file_name = mesh_file_name.substr(0, pos) + "_in.e";
}
// If we're writing an Exodus file, write the Mesh using its logical
// element dimension rather than the spatial dimension, unless it's
// a 1D Mesh. One reason to prefer this approach is that sidesets
// are displayed incorrectly for 2D triangular elements in both
// Paraview and Cubit if num_dim==3 in the Exodus file. We do the
// same thing in MOOSE's Exodus Output object, so we are mimicking
// that behavior here.
if (mesh_file_name.find(".e") + 2 == mesh_file_name.size())
{
ExodusII_IO exio(mesh->getMesh());
if (mesh->getMesh().mesh_dimension() != 1)
exio.use_mesh_dimension_instead_of_spatial_dimension(true);
exio.write(mesh_file_name);
}
else
{
// Just write the file using the name requested by the user.
mesh->getMesh().write(mesh_file_name);
}
}
void
MooseApp::registerRecoverableData(std::string name)
{
_recoverable_data.insert(name);
}
MooseSharedPointer<Backup>
MooseApp::backup()
{
FEProblem & fe_problem = static_cast<FEProblem &>(_executioner->problem());
RestartableDataIO rdio(fe_problem);
return rdio.createBackup();
}
void
MooseApp::restore(MooseSharedPointer<Backup> backup)
{
FEProblem & fe_problem = static_cast<FEProblem &>(_executioner->problem());
RestartableDataIO rdio(fe_problem);
rdio.restoreBackup(backup);
}
void
MooseApp::setCheckUnusedFlag(bool warn_is_error)
{
_enable_unused_check = warn_is_error ? ERROR_UNUSED : WARN_UNUSED;
}
void
MooseApp::disableCheckUnusedFlag()
{
_enable_unused_check = OFF;
}
void
MooseApp::setErrorOverridden()
{
_error_overridden = true;
}
bool &
MooseApp::legacyUoAuxComputationDefault()
{
return _legacy_uo_aux_computation_default;
}
bool &
MooseApp::legacyUoInitializationDefault()
{
return _legacy_uo_initialization_default;
}
void
MooseApp::run()
{
setupOptions();
runInputFile();
executeExecutioner();
}
void
MooseApp::setOutputPosition(Point p)
{
_output_position_set = true;
_output_position = p;
_output_warehouse.meshChanged();
if (_executioner.get() != NULL)
_executioner->parentOutputPositionChanged();
}
std::string
MooseApp::getFileName(bool stripLeadingPath) const
{
return _parser.getFileName(stripLeadingPath);
}
OutputWarehouse &
MooseApp::getOutputWarehouse()
{
return _output_warehouse;
}
std::string
MooseApp::appNameToLibName(const std::string & app_name) const
{
std::string library_name(app_name);
// Strip off the App part (should always be the last 3 letters of the name)
size_t pos = library_name.find("App");
if (pos != library_name.length() - 3)
mooseError("Invalid application name: " << library_name);
library_name.erase(pos);
// Now get rid of the camel case, prepend lib, and append the method and suffix
return std::string("lib") + MooseUtils::camelCaseToUnderscore(library_name) + '-' + QUOTE(METHOD) + ".la";
}
std::string
MooseApp::libNameToAppName(const std::string & library_name) const
{
std::string app_name(library_name);
// Strip off the leading "lib" and trailing ".la"
if (pcrecpp::RE("lib(.+?)(?:-\\w+)?\\.la").Replace("\\1", &app_name) == 0)
mooseError("Invalid library name: " << app_name);
return MooseUtils::underscoreToCamelCase(app_name, true);
}
void
MooseApp::registerRestartableData(std::string name, RestartableDataValue * data, THREAD_ID tid)
{
std::map<std::string, RestartableDataValue *> & restartable_data = _restartable_data[tid];
if (restartable_data.find(name) != restartable_data.end())
mooseError("Attempted to declare restartable twice with the same name: " << name);
restartable_data[name] = data;
}
void
MooseApp::dynamicAppRegistration(const std::string & app_name, std::string library_path)
{
Parameters params;
params.set<std::string>("app_name") = app_name;
params.set<RegistrationType>("reg_type") = APPLICATION;
params.set<std::string>("registration_method") = app_name + "__registerApps";
params.set<std::string>("library_path") = library_path;
dynamicRegistration(params);
// At this point the application should be registered so check it
if (!AppFactory::instance().isRegistered(app_name))
{
std::ostringstream oss;
std::set<std::string> paths = getLoadedLibraryPaths();
oss << "Unable to locate library for \"" << app_name << "\".\nWe attempted to locate the library \"" << appNameToLibName(app_name)
<< "\" in the following paths:\n\t";
std::copy(paths.begin(), paths.end(), infix_ostream_iterator<std::string>(oss, "\n\t"));
oss << "\n\nMake sure you have compiled the library and either set the \"library_path\" variable "
<< "in your input file or exported \"MOOSE_LIBRARY_PATH\".\n"
<< "Compiled in debug mode to see the list of libraries checked for dynamic loading methods.";
mooseError(oss.str());
}
}
void
MooseApp::dynamicObjectRegistration(const std::string & app_name, Factory * factory, std::string library_path)
{
Parameters params;
params.set<std::string>("app_name") = app_name;
params.set<RegistrationType>("reg_type") = OBJECT;
params.set<std::string>("registration_method") = app_name + "__registerObjects";
params.set<std::string>("library_path") = library_path;
params.set<Factory *>("factory") = factory;
dynamicRegistration(params);
}
void
MooseApp::dynamicSyntaxAssociation(const std::string & app_name, Syntax * syntax, ActionFactory * action_factory, std::string library_path)
{
Parameters params;
params.set<std::string>("app_name") = app_name;
params.set<RegistrationType>("reg_type") = SYNTAX;
params.set<std::string>("registration_method") = app_name + "__associateSyntax";
params.set<std::string>("library_path") = library_path;
params.set<Syntax *>("syntax") = syntax;
params.set<ActionFactory *>("action_factory") = action_factory;
dynamicRegistration(params);
}
void
MooseApp::dynamicRegistration(const Parameters & params)
{
// first convert the app name to a library name
std::string library_name = appNameToLibName(params.get<std::string>("app_name"));
// Create a vector of paths that we can search inside for libraries
std::vector<std::string> paths;
std::string library_path = params.get<std::string>("library_path");
if (library_path != "")
MooseUtils::tokenize(library_path, paths, 1, ":");
char * moose_lib_path_env = std::getenv("MOOSE_LIBRARY_PATH");
if (moose_lib_path_env)
{
std::string moose_lib_path(moose_lib_path_env);
std::vector<std::string> tmp_paths;
MooseUtils::tokenize(moose_lib_path, tmp_paths, 1, ":");
// merge the two vectors together (all possible search paths)
paths.insert(paths.end(), tmp_paths.begin(), tmp_paths.end());
}
// Attempt to dynamically load the library
for (std::vector<std::string>::const_iterator path_it = paths.begin(); path_it != paths.end(); ++path_it)
if (MooseUtils::checkFileReadable(*path_it + '/' + library_name, false, false))
loadLibraryAndDependencies(*path_it + '/' + library_name, params);
else
mooseWarning("Unable to open library file \"" << *path_it + '/' + library_name << "\". Double check for spelling errors.");
}
void
MooseApp::loadLibraryAndDependencies(const std::string & library_filename, const Parameters & params)
{
std::string line;
std::string dl_lib_filename;
// This RE looks for absolute path libtool filenames (i.e. begins with a slash and ends with a .la)
pcrecpp::RE re_deps("(/\\S*\\.la)");
std::ifstream handle(library_filename.c_str());
if (handle.is_open())
{
while (std::getline(handle, line))
{
// Look for the system dependent dynamic library filename to open
if (line.find("dlname=") != std::string::npos)
// Magic numbers are computed from length of this string "dlname=' and line minus that string plus quotes"
dl_lib_filename = line.substr(8, line.size()-9);
if (line.find("dependency_libs=") != std::string::npos)
{
pcrecpp::StringPiece input(line);
pcrecpp::StringPiece depend_library;
while (re_deps.FindAndConsume(&input, &depend_library))
// Recurse here to load dependent libraries in depth-first order
loadLibraryAndDependencies(depend_library.as_string(), params);
// There's only one line in the .la file containing the dependency libs so break after finding it
break;
}
}
handle.close();
}
std::string registration_method_name = params.get<std::string>("registration_method");
// Time to load the library, First see if we've already loaded this particular dynamic library
if (_lib_handles.find(std::make_pair(library_filename, registration_method_name)) == _lib_handles.end() && // make sure we haven't already loaded this library
dl_lib_filename != "") // AND make sure we have a library name (we won't for static linkage)
{
std::pair<std::string, std::string> lib_name_parts = MooseUtils::splitFileName(library_filename);
// Assemble the actual filename using the base path of the *.la file and the dl_lib_filename
std::string dl_lib_full_path = lib_name_parts.first + '/' + dl_lib_filename;
#ifdef LIBMESH_HAVE_DLOPEN
void * handle = dlopen(dl_lib_full_path.c_str(), RTLD_LAZY);
#else
void * handle = NULL;
#endif
if (!handle)
mooseError("Cannot open library: " << dl_lib_full_path.c_str() << "\n");
// get the pointer to the method in the library. The dlsym()
// function returns a null pointer if the symbol cannot be found,
// we also explicitly set the pointer to NULL if dlsym is not
// available.
#ifdef LIBMESH_HAVE_DLOPEN
void * registration_method = dlsym(handle, registration_method_name.c_str());
#else
void * registration_method = NULL;
#endif
if (!registration_method)
{
// We found a dynamic library that doesn't have a dynamic
// registration method in it. This shouldn't be an error, so
// we'll just move on.
#ifdef DEBUG
mooseWarning("Unable to find extern \"C\" method \"" << registration_method_name \
<< "\" in library: " << dl_lib_full_path << ".\n" \
<< "This doesn't necessarily indicate an error condition unless you believe that the method should exist in that library.\n");
#endif
#ifdef LIBMESH_HAVE_DLOPEN
dlclose(handle);
#endif
}
else // registration_method is valid!
{
// TODO: Look into cleaning this up
switch (params.get<RegistrationType>("reg_type"))
{
case APPLICATION:
{
typedef void (*register_app_t)();
register_app_t *reg_ptr = reinterpret_cast<register_app_t *>( ®istration_method );
(*reg_ptr)();
break;
}
case OBJECT:
{
typedef void (*register_app_t)(Factory *);
register_app_t *reg_ptr = reinterpret_cast<register_app_t *>( ®istration_method );
(*reg_ptr)(params.get<Factory *>("factory"));
break;
}
case SYNTAX:
{
typedef void (*register_app_t)(Syntax *, ActionFactory *);
register_app_t *reg_ptr = reinterpret_cast<register_app_t *>( ®istration_method );
(*reg_ptr)(params.get<Syntax *>("syntax"), params.get<ActionFactory *>("action_factory"));
break;
}
default:
mooseError("Unhandled RegistrationType");
}
// Store the handle so we can close it later
_lib_handles.insert(std::make_pair(std::make_pair(library_filename, registration_method_name), handle));
}
}
}
std::set<std::string>
MooseApp::getLoadedLibraryPaths() const
{
// Return the paths but not the open file handles
std::set<std::string> paths;
for (std::map<std::pair<std::string, std::string>, void *>::const_iterator it = _lib_handles.begin(); it != _lib_handles.end(); ++it)
paths.insert(it->first.first);
return paths;
}
InputParameterWarehouse &
MooseApp::getInputParameterWarehouse()
{
return *_input_parameter_warehouse;
}
std::string
MooseApp::header() const
{
return std::string("");
}
void
MooseApp::addMeshModifier(const std::string & modifier_name, const std::string & name, InputParameters parameters)
{
MooseSharedPointer<MeshModifier> mesh_modifier = MooseSharedNamespace::static_pointer_cast<MeshModifier>(_factory.create(modifier_name, name, parameters));
_mesh_modifiers.insert(std::make_pair(MooseUtils::shortName(name), mesh_modifier));
}
void
MooseApp::executeMeshModifiers()
{
DependencyResolver<MooseSharedPointer<MeshModifier> > resolver;
// Add all of the dependencies into the resolver and sort them
for (std::map<std::string, MooseSharedPointer<MeshModifier> >::const_iterator it = _mesh_modifiers.begin(); it != _mesh_modifiers.end(); ++it)
{
// Make sure an item with no dependencies comes out too!
resolver.addItem(it->second);
std::vector<std::string> & modifiers = it->second->getDependencies();
for (std::vector<std::string>::const_iterator depend_name_it = modifiers.begin(); depend_name_it != modifiers.end(); ++depend_name_it)
{
std::map<std::string, MooseSharedPointer<MeshModifier> >::const_iterator depend_it = _mesh_modifiers.find(*depend_name_it);
if (depend_it == _mesh_modifiers.end())
mooseError("The MeshModifier \"" << *depend_name_it << "\" was not created, did you make a spelling mistake or forget to include it in your input file?");
resolver.insertDependency(it->second, depend_it->second);
}
}
const std::vector<MooseSharedPointer<MeshModifier> > & ordered_modifiers = resolver.getSortedValues();
if (ordered_modifiers.size())
{
MooseMesh * mesh = _action_warehouse.mesh().get();
MooseMesh * displaced_mesh = _action_warehouse.displacedMesh().get();
// Run the MeshModifiers in the proper order
for (std::vector<MooseSharedPointer<MeshModifier> >::const_iterator it = ordered_modifiers.begin(); it != ordered_modifiers.end(); ++it)
{
MeshModifier * modifier_ptr = it->get();
modifier_ptr->setMeshPointer(mesh);
modifier_ptr->modify();
if (displaced_mesh)
{
modifier_ptr->setMeshPointer(displaced_mesh);
modifier_ptr->modify();
}
}
mesh->prepared(false);
if (displaced_mesh)
displaced_mesh->prepared(false);
}
// Clear the modifiers, they are not used again during the simulation
_mesh_modifiers.clear();
}