ASPECT
interface.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2019 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
79  ~Interface ();
80 
84  virtual void initialize ();
85 
90  virtual void update ();
91 
109  virtual
110  std::pair<std::string,std::string>
111  execute (TableHandler &statistics) = 0;
112 
126  static
127  void
129 
136  virtual
137  void
138  parse_parameters (ParameterHandler &prm);
139 
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 
242  template <typename PostprocessorType>
243  DEAL_II_DEPRECATED
244  PostprocessorType *
245  find_postprocessor () const;
246 
253  template <typename PostprocessorType>
254  bool
255  has_matching_postprocessor () const;
256 
265  template <typename PostprocessorType>
266  const PostprocessorType &
267  get_matching_postprocessor () const;
268 
273  static
274  void
276 
282  void
283  parse_parameters (ParameterHandler &prm);
284 
289  template <class Archive>
290  void save (Archive &ar,
291  const unsigned int version) const;
292 
297  template <class Archive>
298  void load (Archive &ar,
299  const unsigned int version);
300 
301  BOOST_SERIALIZATION_SPLIT_MEMBER()
302 
303 
304 
321  static
322  void
323  register_postprocessor (const std::string &name,
324  const std::string &description,
325  void (*declare_parameters_function) (ParameterHandler &),
326  Interface<dim> *(*factory_function) ());
327 
337  static
338  void
339  write_plugin_graph (std::ostream &output_stream);
340 
344  DeclException1 (ExcPostprocessorNameNotFound,
345  std::string,
346  << "Could not find entry <"
347  << arg1
348  << "> among the names of registered postprocessors.");
349  private:
354  std::vector<std::unique_ptr<Interface<dim> > > postprocessors;
355  };
356 
357 
358  /* -------------------------- inline and template functions ---------------------- */
359 
360  template <int dim>
361  template <class Archive>
362  void Manager<dim>::save (Archive &ar,
363  const unsigned int) const
364  {
365  // let all the postprocessors save their data in a map and then
366  // serialize that
367  std::map<std::string,std::string> saved_text;
368  for (const auto &p : postprocessors)
369  p->save (saved_text);
370 
371  ar &saved_text;
372  }
373 
374 
375  template <int dim>
376  template <class Archive>
377  void Manager<dim>::load (Archive &ar,
378  const unsigned int)
379  {
380  // get the map back out of the stream; then let the postprocessors
381  // that we currently have get their data from there. note that this
382  // may not be the same set of postprocessors we had when we saved
383  // their data
384  std::map<std::string,std::string> saved_text;
385  ar &saved_text;
386 
387  for (auto &p : postprocessors)
388  p->load (saved_text);
389  }
390 
391 
392 
393  template <int dim>
394  template <typename PostprocessorType>
395  inline
396  PostprocessorType *
398  {
399  for (auto &p : postprocessors)
400  if (PostprocessorType *x = dynamic_cast<PostprocessorType *> ( p.get()) )
401  return x;
402  return nullptr;
403  }
404 
405 
406 
407  template <int dim>
408  template <typename PostprocessorType>
409  inline
410  bool
412  {
413  for (const auto &p : postprocessors)
414  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
415  return true;
416 
417  return false;
418  }
419 
420 
421 
422  template <int dim>
423  template <typename PostprocessorType>
424  inline
425  const PostprocessorType &
427  {
428  AssertThrow(has_matching_postprocessor<PostprocessorType> (),
429  ExcMessage("You asked Postprocess::Manager::get_matching_postprocessor() for a "
430  "postprocessor of type <" + boost::core::demangle(typeid(PostprocessorType).name()) + "> "
431  "that could not be found in the current model. Activate this "
432  "postprocessor in the input file."));
433 
434  typename std::vector<std::unique_ptr<Interface<dim> > >::const_iterator postprocessor;
435  for (const auto &p : postprocessors)
436  if (Plugins::plugin_type_matches<PostprocessorType>(*p))
437  return Plugins::get_plugin_as_type<PostprocessorType>(*p);
438 
439  // We will never get here, because we had the Assert above. Just to avoid warnings.
440  return Plugins::get_plugin_as_type<PostprocessorType>(*(*postprocessor));
441  }
442 
443 
451 #define ASPECT_REGISTER_POSTPROCESSOR(classname,name,description) \
452  template class classname<2>; \
453  template class classname<3>; \
454  namespace ASPECT_REGISTER_POSTPROCESSOR_ ## classname \
455  { \
456  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<2>,classname<2> > \
457  dummy_ ## classname ## _2d (&aspect::Postprocess::Manager<2>::register_postprocessor, \
458  name, description); \
459  aspect::internal::Plugins::RegisterHelper<aspect::Postprocess::Interface<3>,classname<3> > \
460  dummy_ ## classname ## _3d (&aspect::Postprocess::Manager<3>::register_postprocessor, \
461  name, description); \
462  }
463  }
464 }
465 
466 
467 #endif
std::vector< std::unique_ptr< Interface< dim > > > postprocessors
Definition: interface.h:354
#define AssertThrow(cond, exc)
void write_plugin_graph(std::ostream &output_stream)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
void load(Archive &ar, const unsigned int version)
Definition: interface.h:377
Definition: compat.h:37
void save(Archive &ar, const unsigned int version) const
Definition: interface.h:362
void initialize(const Triangulation< dim, spacedim > &triangulation, const MappingQGeneric< dim, spacedim > &mapping)
void declare_parameters(ParameterHandler &prm)