ASPECT
interface.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2022 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 
159  virtual
160  std::list<std::string>
161  required_other_postprocessors () const;
162 
184  virtual
185  void save (std::map<std::string, std::string> &status_strings) const;
186 
198  virtual
199  void load (const std::map<std::string, std::string> &status_strings);
200  };
201 
202 
203 
204 
205 
206 
216  template <int dim>
217  class Manager : public ::aspect::SimulatorAccess<dim>
218  {
219  public:
228  std::list<std::pair<std::string,std::string>>
229  execute (TableHandler &statistics);
230 
241  template <typename PostprocessorType>
243  PostprocessorType *
244  find_postprocessor () const;
245 
252  template <typename PostprocessorType>
253  bool
254  has_matching_postprocessor () const;
255 
264  template <typename PostprocessorType>
265  const PostprocessorType &
266  get_matching_postprocessor () const;
267 
272  static
273  void
274  declare_parameters (ParameterHandler &prm);
275 
281  void
282  parse_parameters (ParameterHandler &prm);
283 
288  template <class Archive>
289  void save (Archive &ar,
290  const unsigned int version) const;
291 
296  template <class Archive>
297  void load (Archive &ar,
298  const unsigned int version);
299 
300  BOOST_SERIALIZATION_SPLIT_MEMBER()
301 
302 
303 
320  static
321  void
322  register_postprocessor (const std::string &name,
323  const std::string &description,
324  void (*declare_parameters_function) (ParameterHandler &),
325  std::unique_ptr<Interface<dim>> (*factory_function) ());
326 
336  static
337  void
338  write_plugin_graph (std::ostream &output_stream);
339 
343  DeclException1 (ExcPostprocessorNameNotFound,
344  std::string,
345  << "Could not find entry <"
346  << arg1
347  << "> among the names of registered postprocessors.");
348  private:
353  std::vector<std::unique_ptr<Interface<dim>>> postprocessors;
354  };
355 
356 
357  /* -------------------------- inline and template functions ---------------------- */
358 
359  template <int dim>
360  template <class Archive>
361  void Manager<dim>::save (Archive &ar,
362  const unsigned int) const
363  {
364  // let all the postprocessors save their data in a map and then
365  // serialize that
366  std::map<std::string,std::string> saved_text;
367  for (const auto &p : postprocessors)
368  p->save (saved_text);
369 
370  ar &saved_text;
371  }
372 
373 
374  template <int dim>
375  template <class Archive>
376  void Manager<dim>::load (Archive &ar,
377  const unsigned int)
378  {
379  // get the map back out of the stream; then let the postprocessors
380  // that we currently have get their data from there. note that this
381  // may not be the same set of postprocessors we had when we saved
382  // their data
383  std::map<std::string,std::string> saved_text;
384  ar &saved_text;
385 
386  for (auto &p : postprocessors)
387  p->load (saved_text);
388  }
389 
390 
391 
392  template <int dim>
393  template <typename PostprocessorType>
394  inline
395  PostprocessorType *
397  {
398  for (auto &p : postprocessors)
399  if (PostprocessorType *x = dynamic_cast<PostprocessorType *> ( p.get()) )
400  return x;
401  return nullptr;
402  }
403 
404 
405 
406  template <int dim>
407  template <typename PostprocessorType>
408  inline
409  bool
411  {
412  for (const auto &p : postprocessors)
413  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
414  return true;
415 
416  return false;
417  }
418 
419 
420 
421  template <int dim>
422  template <typename PostprocessorType>
423  inline
424  const PostprocessorType &
426  {
427  AssertThrow(has_matching_postprocessor<PostprocessorType> (),
428  ExcMessage("You asked Postprocess::Manager::get_matching_postprocessor() for a "
429  "postprocessor of type <" + boost::core::demangle(typeid(PostprocessorType).name()) + "> "
430  "that could not be found in the current model. Activate this "
431  "postprocessor in the input file."));
432 
433  typename std::vector<std::unique_ptr<Interface<dim>>>::const_iterator postprocessor;
434  for (const auto &p : postprocessors)
435  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
436  return Plugins::get_plugin_as_type<PostprocessorType>(*p);
437 
438  // We will never get here, because we had the Assert above. Just to avoid warnings.
439  return Plugins::get_plugin_as_type<PostprocessorType>(*(*postprocessor));
440  }
441 
442 
450 #define ASPECT_REGISTER_POSTPROCESSOR(classname,name,description) \
451  template class classname<2>; \
452  template class classname<3>; \
453  namespace ASPECT_REGISTER_POSTPROCESSOR_ ## classname \
454  { \
455  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<2>,classname<2>> \
456  dummy_ ## classname ## _2d (&aspect::Postprocess::Manager<2>::register_postprocessor, \
457  name, description); \
458  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<3>,classname<3>> \
459  dummy_ ## classname ## _3d (&aspect::Postprocess::Manager<3>::register_postprocessor, \
460  name, description); \
461  }
462  }
463 }
464 
465 
466 #endif
StructuredDataLookup< dim > DEAL_II_DEPRECATED
std::vector< std::unique_ptr< Interface< dim > > > postprocessors
Definition: interface.h:353
void write_plugin_graph(std::ostream &output_stream)
void load(Archive &ar, const unsigned int version)
Definition: interface.h:376
void save(Archive &ar, const unsigned int version) const
Definition: interface.h:361
void declare_parameters(ParameterHandler &prm)
Definition: compat.h:88
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.")