equation_systems.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_EQUATION_SYSTEMS_H
21 #define LIBMESH_EQUATION_SYSTEMS_H
22 
23 // Local Includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/parameters.h"
26 #include "libmesh/system.h"
28 
29 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
30 namespace libMesh
31 {
32 enum XdrMODE : int;
33 }
34 #else
35 #include "libmesh/enum_xdr_mode.h"
36 #endif
37 
38 // HP aCC needs these for some reason
39 #ifdef __HP_aCC
42 # include "libmesh/newmark_system.h"
43 # include "libmesh/steady_system.h"
44 #endif
45 
46 // C++ includes
47 #include <cstddef>
48 #include <map>
49 #include <set>
50 #include <string>
51 #include <vector>
52 
53 namespace libMesh
54 {
55 
56 // Forward Declarations
57 class Elem;
58 class MeshBase;
59 
74 class EquationSystems : public ReferenceCountedObject<EquationSystems>,
75  public ParallelObject
76 
77 {
78 public:
79 
83  enum ReadFlags { READ_HEADER = 1,
84  READ_DATA = 2,
89 
93  enum WriteFlags { WRITE_DATA = 1,
97 
102 
107  virtual ~EquationSystems ();
108 
112  virtual void clear ();
113 
117  virtual void init ();
118 
123  virtual void reinit ();
124 
137  virtual void enable_default_ghosting (bool enable);
138 
142  void update ();
143 
147  unsigned int n_systems() const;
148 
153  bool has_system (const std::string & name) const;
154 
161  template <typename T_sys>
162  const T_sys & get_system (const std::string & name) const;
163 
170  template <typename T_sys>
171  T_sys & get_system (const std::string & name);
172 
179  template <typename T_sys>
180  const T_sys & get_system (const unsigned int num) const;
181 
188  template <typename T_sys>
189  T_sys & get_system (const unsigned int num);
190 
194  const System & get_system (const std::string & name) const;
195 
199  System & get_system (const std::string & name);
200 
204  const System & get_system (const unsigned int num) const;
205 
209  System & get_system (const unsigned int num);
210 
215  virtual System & add_system (const std::string & system_type,
216  const std::string & name);
217 
221  template <typename T_sys>
222  T_sys & add_system (const std::string & name);
223 
232 #ifdef LIBMESH_ENABLE_DEPRECATED
233  void delete_system (const std::string & name);
234 #endif
235 
240  unsigned int n_vars () const;
241 
246  std::size_t n_dofs () const;
247 
252  std::size_t n_active_dofs() const;
253 
262  virtual void solve ();
263 
272  virtual void adjoint_solve (const QoISet & qoi_indices = QoISet());
273 
282  virtual void sensitivity_solve (const ParameterVector & parameters);
283 
291  void build_variable_names (std::vector<std::string> & var_names,
292  const FEType * type=nullptr,
293  const std::set<std::string> * system_names=nullptr) const;
294 
303  void build_solution_vector (std::vector<Number> & soln,
304  const std::string & system_name,
305  const std::string & variable_name = "all_vars") const;
306 
314  void build_solution_vector (std::vector<Number> & soln,
315  const std::set<std::string> * system_names=nullptr) const;
316 
325  std::unique_ptr<NumericVector<Number>>
326  build_parallel_solution_vector(const std::set<std::string> * system_names=nullptr) const;
327 
332  void get_vars_active_subdomains(const std::vector<std::string> & names,
333  std::vector<std::set<subdomain_id_type>> & vars_active_subdomains) const;
334 
343  void get_solution (std::vector<Number> & soln,
344  std::vector<std::string> & names) const;
345 
354  void build_elemental_solution_vector (std::vector<Number> & soln,
355  std::vector<std::string> & names) const;
356 
373  std::unique_ptr<NumericVector<Number>>
374  build_parallel_elemental_solution_vector (std::vector<std::string> & names) const;
375 
383  void build_discontinuous_solution_vector (std::vector<Number> & soln,
384  const std::set<std::string> * system_names=nullptr) const;
385 
411  template <typename InValType>
412  void read (const std::string & name,
413  const XdrMODE,
414  const unsigned int read_flags=(READ_HEADER | READ_DATA),
415  bool partition_agnostic = true);
416 
417  void read (const std::string & name,
418  const XdrMODE mode,
419  const unsigned int read_flags=(READ_HEADER | READ_DATA),
420  bool partition_agnostic = true)
421  { read<Number>(name, mode, read_flags, partition_agnostic); }
422 
423  template <typename InValType>
424  void read (const std::string & name,
425  const unsigned int read_flags=(READ_HEADER | READ_DATA),
426  bool partition_agnostic = true);
427 
428  void read (const std::string & name,
429  const unsigned int read_flags=(READ_HEADER | READ_DATA),
430  bool partition_agnostic = true)
431  { read<Number>(name, read_flags, partition_agnostic); }
432 
433 
458  void write (const std::string & name,
459  const XdrMODE,
460  const unsigned int write_flags=(WRITE_DATA),
461  bool partition_agnostic = true) const;
462 
463  void write (const std::string & name,
464  const unsigned int write_flags=(WRITE_DATA),
465  bool partition_agnostic = true) const;
466 
473  virtual bool compare (const EquationSystems & other_es,
474  const Real threshold,
475  const bool verbose) const;
476 
481  virtual std::string get_info() const;
482 
487  void print_info (std::ostream & os=libMesh::out) const;
488 
492  friend std::ostream & operator << (std::ostream & os,
493  const EquationSystems & es);
494 
498  const MeshBase & get_mesh() const;
499 
503  MeshBase & get_mesh();
504 
509  void allgather ();
510 
515 
519  void disable_refine_in_reinit() { this->_refine_in_reinit = false; }
520 
524  bool refine_in_reinit_flag() { return this->_refine_in_reinit; }
525 
532  bool reinit_solutions ();
533 
537  virtual void reinit_systems ();
538 
543 
544 
545 protected:
546 
547 
552 
556  std::map<std::string, System *> _systems;
557 
561  typedef std::map<std::string, System *>::iterator system_iterator;
562 
566  typedef std::map<std::string, System *>::const_iterator const_system_iterator;
567 
573 
579 
580 private:
581 
596  template <typename InValType>
597  void _read_impl (const std::string & name,
598  const XdrMODE,
599  const unsigned int read_flags,
600  bool partition_agnostic = true);
601 
609 
614  void _remove_default_ghosting(unsigned int sys_num);
615 };
616 
617 
618 
619 // ------------------------------------------------------------
620 // EquationSystems inline methods
621 inline
623 {
624  return _mesh;
625 }
626 
627 
628 
629 inline
631 {
632  return _mesh;
633 }
634 
635 
636 inline
637 unsigned int EquationSystems::n_systems () const
638 {
639  return cast_int<unsigned int>(_systems.size());
640 }
641 
642 
643 
644 
645 template <typename T_sys>
646 inline
647 T_sys & EquationSystems::add_system (const std::string & name)
648 {
649  T_sys * ptr = nullptr;
650 
651  if (!_systems.count(name))
652  {
653  const unsigned int sys_num = this->n_systems();
654  ptr = new T_sys(*this, name, sys_num);
655 
656  _systems.insert (std::make_pair(name, ptr));
657 
659  this->_remove_default_ghosting(sys_num);
660 
661  // Tell all the \p DofObject entities to add a system.
663  }
664  else
665  {
666  // We now allow redundant add_system calls, to make it
667  // easier to load data from files for user-derived system
668  // subclasses
669  ptr = &(this->get_system<T_sys>(name));
670  }
671 
672  // Return a dynamically casted reference to the newly added System.
673  return *ptr;
674 }
675 
676 
677 
678 inline
679 bool EquationSystems::has_system (const std::string & name) const
680 {
681  if (_systems.find(name) == _systems.end())
682  return false;
683  return true;
684 }
685 
686 
687 
688 
689 template <typename T_sys>
690 inline
691 const T_sys & EquationSystems::get_system (const unsigned int num) const
692 {
693  libmesh_assert_less (num, this->n_systems());
694 
695 
696  const_system_iterator pos = _systems.begin();
697  const const_system_iterator end = _systems.end();
698 
699  for (; pos != end; ++pos)
700  if (pos->second->number() == num)
701  break;
702 
703  // Check for errors
704  if (pos == end)
705  libmesh_error_msg("ERROR: no system number " << num << " found!");
706 
707  // Attempt dynamic cast
708  return *cast_ptr<T_sys *>(pos->second);
709 }
710 
711 
712 
713 
714 template <typename T_sys>
715 inline
716 T_sys & EquationSystems::get_system (const unsigned int num)
717 {
718  libmesh_assert_less (num, this->n_systems());
719 
720  const_system_iterator pos = _systems.begin();
721  const const_system_iterator end = _systems.end();
722 
723  for (; pos != end; ++pos)
724  if (pos->second->number() == num)
725  break;
726 
727  // Check for errors
728  if (pos == end)
729  libmesh_error_msg("ERROR: no system number " << num << " found!");
730 
731  // Attempt dynamic cast
732  return *cast_ptr<T_sys *>(pos->second);
733 }
734 
735 
736 
737 
738 
739 
740 template <typename T_sys>
741 inline
742 const T_sys & EquationSystems::get_system (const std::string & name) const
743 {
744  const_system_iterator pos = _systems.find(name);
745 
746  // Check for errors
747  if (pos == _systems.end())
748  libmesh_error_msg("ERROR: no system named \"" << name << "\" found!");
749 
750  // Attempt dynamic cast
751  return *cast_ptr<T_sys *>(pos->second);
752 }
753 
754 
755 
756 
757 
758 
759 template <typename T_sys>
760 inline
761 T_sys & EquationSystems::get_system (const std::string & name)
762 {
763  system_iterator pos = _systems.find(name);
764 
765  // Check for errors
766  if (pos == _systems.end())
767  libmesh_error_msg("ERROR: no system named " << name << " found!");
768 
769  // Attempt dynamic cast
770  return *cast_ptr<T_sys *>(pos->second);
771 }
772 
773 
774 
775 
776 
777 
778 
779 inline
780 const System & EquationSystems::get_system (const std::string & name) const
781 {
782  return this->get_system<System>(name);
783 }
784 
785 
786 
787 inline
788 System & EquationSystems::get_system (const std::string & name)
789 {
790  return this->get_system<System>(name);
791 }
792 
793 
794 
795 inline
796 const System & EquationSystems::get_system (const unsigned int num) const
797 {
798  return this->get_system<System>(num);
799 }
800 
801 
802 
803 inline
804 System & EquationSystems::get_system (const unsigned int num)
805 {
806  return this->get_system<System>(num);
807 }
808 
809 
810 } // namespace libMesh
811 
812 
813 #endif // LIBMESH_EQUATION_SYSTEMS_H
EquationSystems(MeshBase &mesh)
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
Manages the family, order, etc. parameters for a given FE.
Definition: fe_type.h:179
Manages multiples systems of equations.
unsigned int n_vars() const
void build_variable_names(std::vector< std::string > &var_names, const FEType *type=nullptr, const std::set< std::string > *system_names=nullptr) const
unsigned int n_systems() const
Specifies parameters for parameter sensitivity calculations.
std::unique_ptr< NumericVector< Number > > build_parallel_elemental_solution_vector(std::vector< std::string > &names) const
std::size_t n_dofs() const
Used to specify quantities of interest in a simulation.
Definition: qoi_set.h:45
const T_sys & get_system(const std::string &name) const
void print_info(std::ostream &os=libMesh::out) const
MeshBase & mesh
std::unique_ptr< NumericVector< Number > > build_parallel_solution_vector(const std::set< std::string > *system_names=nullptr) const
virtual void enable_default_ghosting(bool enable)
IterBase * end
virtual void adjoint_solve(const QoISet &qoi_indices=QoISet())
void read(const std::string &name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
friend std::ostream & operator<<(std::ostream &os, const EquationSystems &es)
Base class for Mesh.
Definition: mesh_base.h:77
virtual std::string get_info() const
void get_vars_active_subdomains(const std::vector< std::string > &names, std::vector< std::set< subdomain_id_type >> &vars_active_subdomains) const
virtual void sensitivity_solve(const ParameterVector &parameters)
std::map< std::string, System * >::const_iterator const_system_iterator
void read(const std::string &name, const XdrMODE mode, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
void write(const std::string &name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
std::map< std::string, System * >::iterator system_iterator
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:92
void delete_system(const std::string &name)
virtual System & add_system(const std::string &system_type, const std::string &name)
An object whose state is distributed along a set of processors.
void read(const std::string &name, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
void get_solution(std::vector< Number > &soln, std::vector< std::string > &names) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void build_solution_vector(std::vector< Number > &soln, const std::string &system_name, const std::string &variable_name="all_vars") const
void _remove_default_ghosting(unsigned int sys_num)
const MeshBase & get_mesh() const
std::size_t n_active_dofs() const
bool has_system(const std::string &name) const
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
void _read_impl(const std::string &name, const XdrMODE, const unsigned int read_flags, bool partition_agnostic=true)
OStreamProxy out(std::cout)
void build_discontinuous_solution_vector(std::vector< Number > &soln, const std::set< std::string > *system_names=nullptr) const
void build_elemental_solution_vector(std::vector< Number > &soln, std::vector< std::string > &names) const
std::map< std::string, System * > _systems