ASPECT
interface.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2023 by the authors of the ASPECT code.
3 
4  This file is part of ASPECT.
5 
6  ASPECT is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10 
11  ASPECT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with ASPECT; see the file LICENSE. If not see
18  <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _aspect_postprocess_interface_h
23 #define _aspect_postprocess_interface_h
24 
25 #include <aspect/global.h>
26 #include <aspect/plugins.h>
28 
29 #include <memory>
30 #include <deal.II/base/table_handler.h>
31 #include <deal.II/base/parameter_handler.h>
32 
33 #include <boost/serialization/split_member.hpp>
34 #include <boost/core/demangle.hpp>
35 
36 #include <typeinfo>
37 
38 
39 namespace aspect
40 {
41  using namespace dealii;
42 
43  template <int dim> class Simulator;
44  template <int dim> class SimulatorAccess;
45 
46 
53  namespace Postprocess
54  {
55 
70  template <int dim>
71  class Interface
72  {
73  public:
78  virtual ~Interface () = default;
79 
83  virtual void initialize ();
84 
89  virtual void update ();
90 
108  virtual
109  std::pair<std::string,std::string>
110  execute (TableHandler &statistics) = 0;
111 
125  static
126  void
127  declare_parameters (ParameterHandler &prm);
128 
135  virtual
136  void
137  parse_parameters (ParameterHandler &prm);
138 
160  virtual
161  std::list<std::string>
162  required_other_postprocessors () const;
163 
185  virtual
186  void save (std::map<std::string, std::string> &status_strings) const;
187 
199  virtual
200  void load (const std::map<std::string, std::string> &status_strings);
201  };
202 
203 
204 
205 
206 
207 
217  template <int dim>
218  class Manager : public ::aspect::SimulatorAccess<dim>
219  {
220  public:
229  std::list<std::pair<std::string,std::string>>
230  execute (TableHandler &statistics);
231 
241  template <typename PostprocessorType,
242  typename = typename std::enable_if_t<std::is_base_of<Interface<dim>,PostprocessorType>::value>>
243  bool
244  has_matching_postprocessor () const;
245 
257  template <typename PostprocessorType,
258  typename = typename std::enable_if_t<std::is_base_of<Interface<dim>,PostprocessorType>::value>>
259  const PostprocessorType &
260  get_matching_postprocessor () const;
261 
266  static
267  void
268  declare_parameters (ParameterHandler &prm);
269 
275  void
276  parse_parameters (ParameterHandler &prm);
277 
282  template <class Archive>
283  void save (Archive &ar,
284  const unsigned int version) const;
285 
290  template <class Archive>
291  void load (Archive &ar,
292  const unsigned int version);
293 
294  BOOST_SERIALIZATION_SPLIT_MEMBER()
295 
296 
297 
314  static
315  void
316  register_postprocessor (const std::string &name,
317  const std::string &description,
318  void (*declare_parameters_function) (ParameterHandler &),
319  std::unique_ptr<Interface<dim>> (*factory_function) ());
320 
330  static
331  void
332  write_plugin_graph (std::ostream &output_stream);
333 
337  DeclException1 (ExcPostprocessorNameNotFound,
338  std::string,
339  << "Could not find entry <"
340  << arg1
341  << "> among the names of registered postprocessors.");
342  private:
347  std::vector<std::unique_ptr<Interface<dim>>> postprocessors;
348  };
349 
350 
351  /* -------------------------- inline and template functions ---------------------- */
352 
353  template <int dim>
354  template <class Archive>
355  void Manager<dim>::save (Archive &ar,
356  const unsigned int) const
357  {
358  // let all the postprocessors save their data in a map and then
359  // serialize that
360  std::map<std::string,std::string> saved_text;
361  for (const auto &p : postprocessors)
362  p->save (saved_text);
363 
364  ar &saved_text;
365  }
366 
367 
368  template <int dim>
369  template <class Archive>
370  void Manager<dim>::load (Archive &ar,
371  const unsigned int)
372  {
373  // get the map back out of the stream; then let the postprocessors
374  // that we currently have get their data from there. note that this
375  // may not be the same set of postprocessors we had when we saved
376  // their data
377  std::map<std::string,std::string> saved_text;
378  ar &saved_text;
379 
380  for (auto &p : postprocessors)
381  p->load (saved_text);
382  }
383 
384 
385 
386  template <int dim>
387  template <typename PostprocessorType, typename>
388  inline
389  bool
391  {
392  for (const auto &p : postprocessors)
393  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
394  return true;
395 
396  return false;
397  }
398 
399 
400 
401  template <int dim>
402  template <typename PostprocessorType, typename>
403  inline
404  const PostprocessorType &
406  {
407  AssertThrow(has_matching_postprocessor<PostprocessorType> (),
408  ExcMessage("You asked Postprocess::Manager::get_matching_postprocessor() for a "
409  "postprocessor of type <" + boost::core::demangle(typeid(PostprocessorType).name()) + "> "
410  "that could not be found in the current model. Activate this "
411  "postprocessor in the input file."));
412 
413  typename std::vector<std::unique_ptr<Interface<dim>>>::const_iterator postprocessor;
414  for (const auto &p : postprocessors)
415  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
416  return Plugins::get_plugin_as_type<PostprocessorType>(*p);
417 
418  // We will never get here, because we had the Assert above. Just to avoid warnings.
419  return Plugins::get_plugin_as_type<PostprocessorType>(*(*postprocessor));
420  }
421 
422 
430 #define ASPECT_REGISTER_POSTPROCESSOR(classname,name,description) \
431  template class classname<2>; \
432  template class classname<3>; \
433  namespace ASPECT_REGISTER_POSTPROCESSOR_ ## classname \
434  { \
435  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<2>,classname<2>> \
436  dummy_ ## classname ## _2d (&aspect::Postprocess::Manager<2>::register_postprocessor, \
437  name, description); \
438  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<3>,classname<3>> \
439  dummy_ ## classname ## _3d (&aspect::Postprocess::Manager<3>::register_postprocessor, \
440  name, description); \
441  }
442  }
443 }
444 
445 
446 #endif
std::vector< std::unique_ptr< Interface< dim > > > postprocessors
Definition: interface.h:347
void write_plugin_graph(std::ostream &output_stream)
void load(Archive &ar, const unsigned int version)
Definition: interface.h:370
void save(Archive &ar, const unsigned int version) const
Definition: interface.h:355
void declare_parameters(ParameterHandler &prm)
Definition: compat.h:42
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.")