22 #ifndef _aspect_plugins_h 23 #define _aspect_plugins_h 27 #include <deal.II/base/utilities.h> 28 #include <deal.II/base/parameter_handler.h> 29 #include <deal.II/fe/component_mask.h> 31 #include <boost/core/demangle.hpp> 40 #include <type_traits> 69 template <
typename TestType,
typename PluginType,
70 typename =
typename std::enable_if_t<std::is_base_of<PluginType,TestType>::value>>
75 return (dynamic_cast<const TestType *> (&
object) !=
nullptr);
95 template <
typename TestType,
typename PluginType,
96 typename =
typename std::enable_if_t<std::is_base_of<PluginType,TestType>::value>>
101 AssertThrow(plugin_type_matches<TestType>(
object),
102 ExcMessage(
"You have requested to convert a plugin of type <" 103 + boost::core::demangle(
typeid(PluginType).name())
105 + boost::core::demangle(
typeid(TestType).name()) +
106 ">, but this cast cannot be performed."));
111 return *
dynamic_cast<TestType *
> (&object);
186 parse_parameters (ParameterHandler &prm);
209 template <
typename InterfaceType>
234 template <
typename PluginType,
235 typename =
typename std::enable_if_t<std::is_base_of<InterfaceType,PluginType>::value>>
237 has_matching_active_plugin ()
const;
253 template <
typename PluginType,
254 typename =
typename std::enable_if_t<std::is_base_of<InterfaceType,PluginType>::value>>
256 get_matching_active_plugin ()
const;
263 const std::list<std::unique_ptr<InterfaceType>> &
264 get_active_plugins ()
const;
272 const std::vector<std::string> &
273 get_active_plugin_names ()
const;
291 template <
typename InterfaceType>
296 #ifdef DEAL_II_HAVE_CXX17 297 if (std::uncaught_exceptions() == 0)
299 if (std::uncaught_exception() ==
false)
302 Assert (plugin_names.size() == plugin_objects.size(), ExcInternalError());
307 template <
typename InterfaceType>
311 for (
const auto &p : plugin_objects)
324 catch (std::exception &exc)
326 std::cerr << std::endl << std::endl
327 <<
"----------------------------------------------------" 329 std::cerr <<
"Exception on MPI process <" 330 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
331 <<
"> while running plugin <" 333 <<
">: " << std::endl
334 << exc.what() << std::endl
335 <<
"Aborting!" << std::endl
336 <<
"----------------------------------------------------" 340 MPI_Abort (MPI_COMM_WORLD, 1);
344 std::cerr << std::endl << std::endl
345 <<
"----------------------------------------------------" 347 std::cerr <<
"Exception on MPI process <" 348 << ::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)
349 <<
"> while running plugin <" 351 <<
">: " << std::endl;
352 std::cerr <<
"Unknown exception!" << std::endl
353 <<
"Aborting!" << std::endl
354 <<
"----------------------------------------------------" 358 MPI_Abort (MPI_COMM_WORLD, 1);
364 template <
typename InterfaceType>
365 template <
typename PluginType,
typename>
370 for (
const auto &p : plugin_objects)
371 if (Plugins::plugin_type_matches<PluginType>(*p))
377 template <
typename InterfaceType>
378 template <
typename PluginType,
typename>
383 AssertThrow(has_matching_active_plugin<PluginType> (),
384 ExcMessage(
"You asked the object managing a collection of plugins for a " 385 "plugin object of type <" + boost::core::demangle(
typeid(PluginType).name()) +
"> " 386 "that could not be found in the current model. You need to " 387 "activate this plugin in the input file for it to be " 390 for (
const auto &p : plugin_objects)
391 if (Plugins::plugin_type_matches<PluginType>(*p))
392 return Plugins::get_plugin_as_type<PluginType>(*p);
395 return Plugins::get_plugin_as_type<PluginType>(**(plugin_objects.begin()));
400 template <
typename InterfaceType>
401 const std::list<std::unique_ptr<InterfaceType>> &
404 return plugin_objects;
409 template <
typename InterfaceType>
410 const std::vector<std::string> &
436 template <
typename InterfaceClass,
447 void ( *)(ParameterHandler &),
448 std::unique_ptr<InterfaceClass> ( *)()),
450 const char *description)
452 register_function (name,
465 return std::make_unique<ModelClass>();
474 template <
typename InterfaceClass>
489 = std::tuple<std::string,
491 void ( *) (ParameterHandler &),
492 std::unique_ptr<InterfaceClass>( *) ()>;
519 void register_plugin (
const std::string &name,
520 const std::string &description,
521 void (*declare_parameters_function) (ParameterHandler &),
522 std::unique_ptr<InterfaceClass> (*factory_function) ());
532 std::string get_pattern_of_names ();
542 std::string get_description_string ();
560 std::unique_ptr<InterfaceClass>
561 create_plugin (
const std::string &name,
562 const std::string &documentation);
575 std::unique_ptr<InterfaceClass>
576 create_plugin (
const std::string &name,
577 const std::string &documentation,
578 ParameterHandler &prm);
604 std::ostream &output_stream,
605 const std::string &attachment_point =
"Simulator");
612 <<
"Can't create a plugin of name <" << arg1
613 <<
"> because such a plugin hasn't been declared.");
619 template <
typename InterfaceClass>
625 if (plugins !=
nullptr)
632 template <
typename InterfaceClass>
636 const std::string &description,
637 void (*declare_parameters_function) (ParameterHandler &),
638 std::unique_ptr<InterfaceClass> (*factory_function) ())
642 if (plugins ==
nullptr)
643 plugins =
new std::list<PluginInfo>();
648 for (
const auto &p : *plugins)
650 Assert (std::get<0>(p) != name,
651 ExcMessage (
"A plugin with name <" + name +
"> has " 652 "already been registered!"));
658 plugins->emplace_back (name,
660 declare_parameters_function,
666 template <
typename InterfaceClass>
671 Assert (plugins !=
nullptr,
672 ExcMessage (
"No plugins registered!?"));
676 std::set<std::string> names;
677 for (
typename std::list<PluginInfo>::const_iterator
678 p = plugins->begin();
679 p != plugins->end(); ++p)
680 names.insert (std::get<0>(*p));
683 std::string pattern_of_names;
684 for (
const auto &name : names)
686 if (pattern_of_names.size() > 0)
687 pattern_of_names +=
"|";
688 pattern_of_names += name;
691 return pattern_of_names;
696 template <
typename InterfaceClass>
701 std::string description;
705 std::map<std::string,std::string> names_and_descriptions;
706 for (
typename std::list<PluginInfo>::const_iterator
707 p = plugins->begin();
708 p != plugins->end(); ++p)
709 names_and_descriptions[std::get<0>(*p)] = std::get<1>(*p);
712 std::map<std::string,std::string>::const_iterator
713 p = names_and_descriptions.begin();
720 description += p->first;
721 description +=
"': ";
722 description += p->second;
729 if (p != names_and_descriptions.end())
730 description +=
"\n\n";
741 template <
typename InterfaceClass>
746 Assert (plugins !=
nullptr,
747 ExcMessage (
"No postprocessors registered!?"));
749 for (
typename std::list<PluginInfo>::const_iterator
750 p = plugins->begin();
751 p != plugins->end(); ++p)
752 (std::get<2>(*p))(prm);
757 template <
typename InterfaceClass>
758 std::unique_ptr<InterfaceClass>
761 const std::string &documentation)
764 Assert (plugins !=
nullptr,
765 ExcMessage (
"No postprocessors registered!?"));
766 AssertThrow (name !=
"unspecified",
767 ExcMessage(std::string(
"A plugin must have a name!\n\n" 768 "This function was asked to create a plugin but no name for the " 769 "plugin was provided. This may be due to the fact that you did not " 770 "explicitly specify a name for this plugin in your input file and " 771 "ASPECT does not provide a default for this kind of plugin, for " 772 "example because no generally useful plugin exists. An example " 773 "is that there is no default geometry: You need to explicitly " 774 "provide one in the input file, and it seems like you have not " 776 "To find out which kind of plugin this function tries to create, " 777 "take a look at the backtrace of this error message.\n\n" 778 "The place that called this function also provided as " 779 "additional information this:\n\n" 781 + documentation +
">"));
783 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
784 p != plugins->end(); ++p)
785 if (std::get<0>(*p) == name)
787 std::unique_ptr<InterfaceClass> i = std::get<3>(*p)();
791 AssertThrow (
false, ExcUnknownPlugin(name));
797 template <
typename InterfaceClass>
798 std::unique_ptr<InterfaceClass>
801 const std::string &documentation,
802 ParameterHandler &prm)
804 std::unique_ptr<InterfaceClass> i = create_plugin(name, documentation);
805 i->parse_parameters (prm);
811 template <
typename InterfaceClass>
815 std::ostream &output_stream,
816 const std::string &attachment_point)
824 output_stream << std::string(
typeid(InterfaceClass).name())
826 << plugin_system_name
827 <<
"\", height=.8,width=.8,shape=\"rect\",fillcolor=\"lightgreen\"]" 839 std::map<std::string, typename std::list<PluginInfo>::const_iterator>
841 for (
typename std::list<PluginInfo>::const_iterator p = plugins->begin();
842 p != plugins->end(); ++p)
843 plugin_map[std::get<0>(*p)] = p;
846 for (
typename std::map<std::string,
typename std::list<PluginInfo>::const_iterator>::const_iterator
847 p = plugin_map.begin();
848 p != plugin_map.end(); ++p)
854 const std::vector<std::string> plugin_label_parts
855 = ::Utilities::break_text_into_lines(p->first, 15);
856 Assert (plugin_label_parts.size()>0, ExcInternalError());
857 std::string plugin_name = plugin_label_parts[0];
858 for (
unsigned int i=1; i<plugin_label_parts.size(); ++i)
859 plugin_name +=
"\\n" + plugin_label_parts[i];
864 std::unique_ptr<InterfaceClass> instance (create_plugin (p->first,
""));
865 const std::string node_name =
typeid(*instance).name();
868 output_stream << node_name
871 <<
"\", height=.8,width=.8,shape=\"circle\",fillcolor=\"lightblue\"];" 876 output_stream << node_name
878 << std::string(
typeid(InterfaceClass).name())
879 <<
" [len=3, weight=50]" 889 output_stream <<
"SimulatorAccess" 892 <<
" [style=\"dotted\", arrowhead=\"empty\", constraint=false, color=\"gray\", len=20, weight=0.1];" 899 output_stream << std::string(
typeid(InterfaceClass).name())
902 <<
" [len=15, weight=50]" 908 output_stream << std::endl;
const std::vector< std::string > & get_active_plugin_names() const
const PluginType & get_matching_active_plugin() const
std::list< std::unique_ptr< InterfaceType > > plugin_objects
void write_plugin_graph(std::ostream &output_stream)
TestType & get_plugin_as_type(PluginType &object)
RegisterHelper(void(*register_function)(const std::string &, const std::string &, void(*)(ParameterHandler &), std::unique_ptr< InterfaceClass >(*)()), const char *name, const char *description)
static std::list< PluginInfo > * plugins
std::tuple< std::string, std::string, void(*)(ParameterHandler &), std::unique_ptr< InterfaceClass >(*)()> PluginInfo
void declare_parameters(ParameterHandler &prm)
static std::unique_ptr< InterfaceClass > factory()
bool has_matching_active_plugin() const
bool plugin_type_matches(const PluginType &object)
const std::list< std::unique_ptr< InterfaceType > > & get_active_plugins() const
std::vector< std::string > plugin_names
DeclException1(ProbabilityFunctionNegative, Point< dim >,<< "Your probability density function in the particle generator " "returned a negative probability density for the following position: "<< arg1<< ". Please check your function expression.")