libMesh
equation_systems.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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"
27 #include "libmesh/parallel_object.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
40 # include "libmesh/frequency_system.h"
41 # include "libmesh/transient_system.h"
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 
361  std::vector<std::pair<unsigned int, unsigned int>>
362  find_variable_numbers (std::vector<std::string> & names,
363  const FEType * type=nullptr) const;
364 
381  std::unique_ptr<NumericVector<Number>>
382  build_parallel_elemental_solution_vector (std::vector<std::string> & names) const;
383 
396  (std::vector<Number> & soln,
397  const std::set<std::string> * system_names = nullptr,
398  const std::vector<std::string> * var_names = nullptr,
399  bool vertices_only = false) const;
400 
426  template <typename InValType>
427  void read (const std::string & name,
428  const XdrMODE,
429  const unsigned int read_flags=(READ_HEADER | READ_DATA),
430  bool partition_agnostic = true);
431 
432  void read (const std::string & name,
433  const XdrMODE mode,
434  const unsigned int read_flags=(READ_HEADER | READ_DATA),
435  bool partition_agnostic = true)
436  { read<Number>(name, mode, read_flags, partition_agnostic); }
437 
438  template <typename InValType>
439  void read (const std::string & name,
440  const unsigned int read_flags=(READ_HEADER | READ_DATA),
441  bool partition_agnostic = true);
442 
443  void read (const std::string & name,
444  const unsigned int read_flags=(READ_HEADER | READ_DATA),
445  bool partition_agnostic = true)
446  { read<Number>(name, read_flags, partition_agnostic); }
447 
448 
473  void write (const std::string & name,
474  const XdrMODE,
475  const unsigned int write_flags=(WRITE_DATA),
476  bool partition_agnostic = true) const;
477 
478  void write (const std::string & name,
479  const unsigned int write_flags=(WRITE_DATA),
480  bool partition_agnostic = true) const;
481 
488  virtual bool compare (const EquationSystems & other_es,
489  const Real threshold,
490  const bool verbose) const;
491 
496  virtual std::string get_info() const;
497 
502  void print_info (std::ostream & os=libMesh::out) const;
503 
507  friend std::ostream & operator << (std::ostream & os,
508  const EquationSystems & es);
509 
513  const MeshBase & get_mesh() const;
514 
518  MeshBase & get_mesh();
519 
524  void allgather ();
525 
530 
534  void disable_refine_in_reinit() { this->_refine_in_reinit = false; }
535 
539  bool refine_in_reinit_flag() { return this->_refine_in_reinit; }
540 
547  bool reinit_solutions ();
548 
552  virtual void reinit_systems ();
553 
558 
559 
560 protected:
561 
562 
567 
571  std::map<std::string, System *> _systems;
572 
576  typedef std::map<std::string, System *>::iterator system_iterator;
577 
581  typedef std::map<std::string, System *>::const_iterator const_system_iterator;
582 
588 
594 
595 private:
596 
611  template <typename InValType>
612  void _read_impl (const std::string & name,
613  const XdrMODE,
614  const unsigned int read_flags,
615  bool partition_agnostic = true);
616 
624 
629  void _remove_default_ghosting(unsigned int sys_num);
630 };
631 
632 
633 
634 // ------------------------------------------------------------
635 // EquationSystems inline methods
636 inline
638 {
639  return _mesh;
640 }
641 
642 
643 
644 inline
646 {
647  return _mesh;
648 }
649 
650 
651 inline
652 unsigned int EquationSystems::n_systems () const
653 {
654  return cast_int<unsigned int>(_systems.size());
655 }
656 
657 
658 
659 
660 template <typename T_sys>
661 inline
662 T_sys & EquationSystems::add_system (const std::string & name)
663 {
664  T_sys * ptr = nullptr;
665 
666  if (!_systems.count(name))
667  {
668  const unsigned int sys_num = this->n_systems();
669  ptr = new T_sys(*this, name, sys_num);
670 
671  _systems.insert (std::make_pair(name, ptr));
672 
674  this->_remove_default_ghosting(sys_num);
675 
676  // Tell all the \p DofObject entities to add a system.
678  }
679  else
680  {
681  // We now allow redundant add_system calls, to make it
682  // easier to load data from files for user-derived system
683  // subclasses
684  ptr = &(this->get_system<T_sys>(name));
685  }
686 
687  // Return a dynamically casted reference to the newly added System.
688  return *ptr;
689 }
690 
691 
692 
693 inline
694 bool EquationSystems::has_system (const std::string & name) const
695 {
696  if (_systems.find(name) == _systems.end())
697  return false;
698  return true;
699 }
700 
701 
702 
703 
704 template <typename T_sys>
705 inline
706 const T_sys & EquationSystems::get_system (const unsigned int num) const
707 {
708  libmesh_assert_less (num, this->n_systems());
709 
710 
711  const_system_iterator pos = _systems.begin();
712  const const_system_iterator end = _systems.end();
713 
714  for (; pos != end; ++pos)
715  if (pos->second->number() == num)
716  break;
717 
718  // Check for errors
719  if (pos == end)
720  libmesh_error_msg("ERROR: no system number " << num << " found!");
721 
722  // Attempt dynamic cast
723  return *cast_ptr<T_sys *>(pos->second);
724 }
725 
726 
727 
728 
729 template <typename T_sys>
730 inline
731 T_sys & EquationSystems::get_system (const unsigned int num)
732 {
733  libmesh_assert_less (num, this->n_systems());
734 
735  const_system_iterator pos = _systems.begin();
736  const const_system_iterator end = _systems.end();
737 
738  for (; pos != end; ++pos)
739  if (pos->second->number() == num)
740  break;
741 
742  // Check for errors
743  if (pos == end)
744  libmesh_error_msg("ERROR: no system number " << num << " found!");
745 
746  // Attempt dynamic cast
747  return *cast_ptr<T_sys *>(pos->second);
748 }
749 
750 
751 
752 
753 
754 
755 template <typename T_sys>
756 inline
757 const T_sys & EquationSystems::get_system (const std::string & name) const
758 {
759  const_system_iterator pos = _systems.find(name);
760 
761  // Check for errors
762  if (pos == _systems.end())
763  libmesh_error_msg("ERROR: no system named \"" << name << "\" found!");
764 
765  // Attempt dynamic cast
766  return *cast_ptr<T_sys *>(pos->second);
767 }
768 
769 
770 
771 
772 
773 
774 template <typename T_sys>
775 inline
776 T_sys & EquationSystems::get_system (const std::string & name)
777 {
778  system_iterator pos = _systems.find(name);
779 
780  // Check for errors
781  if (pos == _systems.end())
782  libmesh_error_msg("ERROR: no system named " << name << " found!");
783 
784  // Attempt dynamic cast
785  return *cast_ptr<T_sys *>(pos->second);
786 }
787 
788 
789 
790 
791 
792 
793 
794 inline
795 const System & EquationSystems::get_system (const std::string & name) const
796 {
797  return this->get_system<System>(name);
798 }
799 
800 
801 
802 inline
803 System & EquationSystems::get_system (const std::string & name)
804 {
805  return this->get_system<System>(name);
806 }
807 
808 
809 
810 inline
811 const System & EquationSystems::get_system (const unsigned int num) const
812 {
813  return this->get_system<System>(num);
814 }
815 
816 
817 
818 inline
819 System & EquationSystems::get_system (const unsigned int num)
820 {
821  return this->get_system<System>(num);
822 }
823 
824 
825 } // namespace libMesh
826 
827 
828 #endif // LIBMESH_EQUATION_SYSTEMS_H
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::EquationSystems::solve
virtual void solve()
Call solve on all the individual equation systems.
Definition: equation_systems.C:446
libMesh::EquationSystems::build_elemental_solution_vector
void build_elemental_solution_vector(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for CONSTANT MONOMIALs.
Definition: equation_systems.C:883
libMesh::EquationSystems::WRITE_PARALLEL_FILES
Definition: equation_systems.h:95
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh::EquationSystems::get_mesh
const MeshBase & get_mesh() const
Definition: equation_systems.h:637
libMesh::EquationSystems::WRITE_DATA
Definition: equation_systems.h:93
libMesh::EquationSystems::READ_ADDITIONAL_DATA
Definition: equation_systems.h:85
libMesh::EquationSystems::allgather
void allgather()
Serializes a distributed mesh and its associated degree of freedom numbering for all systems.
Definition: equation_systems.C:274
libMesh::ReferenceCountedObject
This class implements reference counting.
Definition: reference_counted_object.h:65
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::EquationSystems::get_system
const T_sys & get_system(const std::string &name) const
Definition: equation_systems.h:757
libMesh::EquationSystems::read
void read(const std::string &name, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Definition: equation_systems.h:443
libMesh::EquationSystems::build_solution_vector
void build_solution_vector(std::vector< Number > &soln, const std::string &system_name, const std::string &variable_name="all_vars") const
Fill the input vector soln with the solution values for the system named name.
Definition: equation_systems.C:578
end
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end.
Definition: variant_filter_iterator.h:343
libMesh::EquationSystems::_read_impl
void _read_impl(const std::string &name, const XdrMODE, const unsigned int read_flags, bool partition_agnostic=true)
Actual read implementation.
Definition: equation_systems_io.C:143
libMesh::EquationSystems::sensitivity_solve
virtual void sensitivity_solve(const ParameterVector &parameters)
Call sensitivity_solve on all the individual equation systems.
Definition: equation_systems.C:456
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::EquationSystems::operator<<
friend std::ostream & operator<<(std::ostream &os, const EquationSystems &es)
Same as above, but allows you to also use stream syntax.
Definition: equation_systems.C:1322
libMesh::EquationSystems::EquationSystems
EquationSystems(MeshBase &mesh)
Constructor.
Definition: equation_systems.C:55
libMesh::EquationSystems::has_system
bool has_system(const std::string &name) const
Definition: equation_systems.h:694
libMesh::EquationSystems::_remove_default_ghosting
void _remove_default_ghosting(unsigned int sys_num)
This just calls DofMap::remove_default_ghosting() but using a shim lets us forward-declare DofMap.
Definition: equation_systems.C:1387
libMesh::EquationSystems::refine_in_reinit_flag
bool refine_in_reinit_flag()
Definition: equation_systems.h:539
libMesh::EquationSystems::find_variable_numbers
std::vector< std::pair< unsigned int, unsigned int > > find_variable_numbers(std::vector< std::string > &names, const FEType *type=nullptr) const
Finds system and variable numbers for any variables of type corresponding to the entries in the input...
Definition: equation_systems.C:904
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::EquationSystems::n_active_dofs
std::size_t n_active_dofs() const
Definition: equation_systems.C:1362
libMesh::XdrMODE
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
libMesh::QoISet
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
libMesh::EquationSystems::reinit_systems
virtual void reinit_systems()
Reinitialize all systems on the current mesh.
Definition: equation_systems.C:266
libMesh::EquationSystems::build_variable_names
void build_variable_names(std::vector< std::string > &var_names, const FEType *type=nullptr, const std::set< std::string > *system_names=nullptr) const
Fill the input vector var_names with the names of the variables for each system.
Definition: equation_systems.C:476
libMesh::EquationSystems::reinit_solutions
bool reinit_solutions()
Handle any mesh changes and project any solutions onto the updated mesh.
Definition: equation_systems.C:133
libMesh::EquationSystems::const_system_iterator
std::map< std::string, System * >::const_iterator const_system_iterator
Typedef for constant system iterators.
Definition: equation_systems.h:581
libMesh::EquationSystems::update
void update()
Updates local values for all the systems.
Definition: equation_systems.C:334
libMesh::EquationSystems::READ_DATA
Definition: equation_systems.h:84
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
libMesh::EquationSystems::n_vars
unsigned int n_vars() const
Definition: equation_systems.C:1331
libMesh::ParameterVector
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Definition: parameter_vector.h:44
libMesh::EquationSystems::_enable_default_ghosting
bool _enable_default_ghosting
Flag for whether to enable default ghosting on newly added Systems.
Definition: equation_systems.h:593
libMesh::EquationSystems::system_iterator
std::map< std::string, System * >::iterator system_iterator
Typedef for system iterators.
Definition: equation_systems.h:576
libMesh::EquationSystems::enable_default_ghosting
virtual void enable_default_ghosting(bool enable)
Enable or disable default ghosting functors on the Mesh and on all Systems.
Definition: equation_systems.C:312
libMesh::EquationSystems::n_systems
unsigned int n_systems() const
Definition: equation_systems.h:652
libMesh::EquationSystems::ReadFlags
ReadFlags
Define enumeration to set properties in EquationSystems::read()
Definition: equation_systems.h:83
libMesh::EquationSystems::_mesh
MeshBase & _mesh
The mesh data structure.
Definition: equation_systems.h:566
libMesh::EquationSystems::n_dofs
std::size_t n_dofs() const
Definition: equation_systems.C:1346
libMesh::EquationSystems::read
void read(const std::string &name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
Definition: equation_systems_io.C:90
libMesh::EquationSystems::print_info
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
Definition: equation_systems.C:1314
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::EquationSystems::disable_refine_in_reinit
void disable_refine_in_reinit()
Calls to reinit() will not try to coarsen or refine the mesh.
Definition: equation_systems.h:534
libMesh::EquationSystems::WRITE_SERIAL_FILES
Definition: equation_systems.h:96
libMesh::EquationSystems::reinit
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
Definition: equation_systems.C:121
libMesh::EquationSystems::compare
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
Definition: equation_systems.C:1217
libMesh::EquationSystems::_systems
std::map< std::string, System * > _systems
Data structure holding the systems.
Definition: equation_systems.h:571
libMesh::FEType
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:178
libMesh::EquationSystems::get_info
virtual std::string get_info() const
Definition: equation_systems.C:1278
libMesh::EquationSystems::build_discontinuous_solution_vector
void build_discontinuous_solution_vector(std::vector< Number > &soln, const std::set< std::string > *system_names=nullptr, const std::vector< std::string > *var_names=nullptr, bool vertices_only=false) const
Fill the input vector soln with solution values.
Definition: equation_systems.C:1049
libMesh::EquationSystems::clear
virtual void clear()
Restores the data structure to a pristine state.
Definition: equation_systems.C:75
libMesh::EquationSystems::read
void read(const std::string &name, const XdrMODE mode, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Definition: equation_systems.h:432
libMesh::EquationSystems::_refine_in_reinit
bool _refine_in_reinit
Flag for whether to call coarsen/refine in reinit().
Definition: equation_systems.h:587
libMesh::EquationSystems::write
void write(const std::string &name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
Write the systems to disk using the XDR data format.
Definition: equation_systems_io.C:378
libMesh::EquationSystems::TRY_READ_IFEMS
Definition: equation_systems.h:87
libMesh::EquationSystems::adjoint_solve
virtual void adjoint_solve(const QoISet &qoi_indices=QoISet())
Call adjoint_solve on all the individual equation systems.
Definition: equation_systems.C:466
libMesh::EquationSystems::~EquationSystems
virtual ~EquationSystems()
Destructor.
Definition: equation_systems.C:68
libMesh::EquationSystems::WRITE_ADDITIONAL_DATA
Definition: equation_systems.h:94
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::ParallelObject
An object whose state is distributed along a set of processors.
Definition: parallel_object.h:55
libMesh::EquationSystems::build_parallel_elemental_solution_vector
std::unique_ptr< NumericVector< Number > > build_parallel_elemental_solution_vector(std::vector< std::string > &names) const
Builds a parallel vector of CONSTANT MONOMIAL solution values corresponding to the entries in the inp...
Definition: equation_systems.C:957
libMesh::EquationSystems::build_parallel_solution_vector
std::unique_ptr< NumericVector< Number > > build_parallel_solution_vector(const std::set< std::string > *system_names=nullptr) const
A version of build_solution_vector which is appropriate for "parallel" output formats like Nemesis.
Definition: equation_systems.C:590
libMesh::EquationSystems::delete_system
void delete_system(const std::string &name)
Remove the system named name from the systems array.
Definition: equation_systems.C:431
libMesh::out
OStreamProxy out
libMesh::EquationSystems::get_solution
void get_solution(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for CONSTANT MONOMIALs.
Definition: equation_systems.C:873
libMesh::EquationSystems::_add_system_to_nodes_and_elems
void _add_system_to_nodes_and_elems()
This function is used in the implementation of add_system, it loops over the nodes and elements of th...
Definition: equation_systems.C:1376
int
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
libMesh::EquationSystems::READ_HEADER
Definition: equation_systems.h:83
libMesh::Parameters
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:59
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::EquationSystems::WriteFlags
WriteFlags
Define enumeration to set properties in EquationSystems::write()
Definition: equation_systems.h:93
libMesh::EquationSystems::get_vars_active_subdomains
void get_vars_active_subdomains(const std::vector< std::string > &names, std::vector< std::set< subdomain_id_type >> &vars_active_subdomains) const
Retrieve vars_active_subdomains, which indicates the active subdomains for each variable in names.
Definition: equation_systems.C:841
libMesh::EquationSystems::READ_LEGACY_FORMAT
Definition: equation_systems.h:86
libMesh::EquationSystems::enable_refine_in_reinit
void enable_refine_in_reinit()
Calls to reinit() will also do two-step coarsen-then-refine.
Definition: equation_systems.h:529
libMesh::EquationSystems::READ_BASIC_ONLY
Definition: equation_systems.h:88
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557