libMesh
equation_systems.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2026 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 // HP aCC needs these for some reason
30 #ifdef __HP_aCC
31 # include "libmesh/frequency_system.h"
32 # include "libmesh/transient_system.h"
33 # include "libmesh/newmark_system.h"
34 # include "libmesh/steady_system.h"
35 #endif
36 
37 // C++ includes
38 #include <cstddef>
39 #include <map>
40 #include <set>
41 #include <string>
42 #include <string_view>
43 #include <vector>
44 #include <memory>
45 
46 namespace libMesh
47 {
48 
49 // Forward Declarations
50 class Elem;
51 class MeshBase;
52 enum XdrMODE : int;
53 
68 class EquationSystems : public ReferenceCountedObject<EquationSystems>,
69  public ParallelObject
70 
71 {
72 public:
73 
77  enum ReadFlags { READ_HEADER = 1,
78  READ_DATA = 2,
83 
87  enum WriteFlags { WRITE_DATA = 1,
91 
96 
101  virtual ~EquationSystems ();
102 
106  virtual void clear ();
107 
111  virtual void init ();
112 
117  virtual void reinit ();
118 
123  virtual void reinit_mesh ();
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 (std::string_view name) const;
154 
161  template <typename T_sys>
162  const T_sys & get_system (std::string_view name) const;
163 
170  template <typename T_sys>
171  T_sys & get_system (std::string_view 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 (std::string_view name) const;
195 
199  System & get_system (std::string_view 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 (std::string_view system_type,
216  std::string_view name);
217 
221  template <typename T_sys>
222  T_sys & add_system (std::string_view name);
223 
228  unsigned int n_vars () const;
229 
234  std::size_t n_dofs () const;
235 
240  std::size_t n_active_dofs() const;
241 
250  virtual void solve ();
251 
260  virtual void adjoint_solve (const QoISet & qoi_indices = QoISet());
261 
270  virtual void sensitivity_solve (const ParameterVector & parameters);
271 
279  void build_variable_names (std::vector<std::string> & var_names,
280  const FEType * type=nullptr,
281  const std::set<std::string> * system_names=nullptr) const;
282 
291  void build_solution_vector (std::vector<Number> & soln,
292  std::string_view system_name,
293  std::string_view variable_name = "all_vars") const;
294 
306  void build_solution_vector (std::vector<Number> & soln,
307  const std::set<std::string> * system_names=nullptr,
308  bool add_sides=false) const;
309 
318  std::unique_ptr<NumericVector<Number>>
319  build_parallel_solution_vector(const std::set<std::string> * system_names=nullptr,
320  bool add_sides=false) const;
321 
326  void get_vars_active_subdomains(const std::vector<std::string> & names,
327  std::vector<std::set<subdomain_id_type>> & vars_active_subdomains) const;
328 
338  void build_elemental_solution_vector (std::vector<Number> & soln,
339  std::vector<std::string> & names) const;
340 
363  std::vector<std::pair<unsigned int, unsigned int>>
364  find_variable_numbers (std::vector<std::string> & names,
365  const FEType * type=nullptr,
366  const std::vector<FEType> * types=nullptr) const;
367 
386  std::unique_ptr<NumericVector<Number>>
387  build_parallel_elemental_solution_vector (std::vector<std::string> & names) const;
388 
406  (std::vector<Number> & soln,
407  const std::set<std::string> * system_names = nullptr,
408  const std::vector<std::string> * var_names = nullptr,
409  bool vertices_only = false,
410  bool add_sides = false) const;
411 
412  /*
413  * Returns true iff the given side of the given element is *never*
414  * added to output from that element, because it is considered to be
415  * redundant with respect to the same data added from the
416  * neighboring element sharing that side. This helper function is
417  * used with the add_sides option when building solution vectors
418  * here and when outputting solution vectors in MeshOutput
419  * (currently just Exodus) I/O.
420  */
421  static bool redundant_added_side(const Elem & elem, unsigned int side);
422 
423 
449  template <typename InValType = Number>
450  void read (std::string_view name,
451  const XdrMODE,
452  const unsigned int read_flags=(READ_HEADER | READ_DATA),
453  bool partition_agnostic = true);
454 
455  template <typename InValType = Number>
456  void read (std::string_view name,
457  const unsigned int read_flags=(READ_HEADER | READ_DATA),
458  bool partition_agnostic = true);
459 
460  template <typename InValType = Number>
461  void read (Xdr & io,
462  std::function<std::unique_ptr<Xdr>()> & local_io_functor,
463  const unsigned int read_flags=(READ_HEADER | READ_DATA),
464  bool partition_agnostic = true);
465 
490  void write (std::string_view name,
491  const XdrMODE,
492  const unsigned int write_flags=(WRITE_DATA),
493  bool partition_agnostic = true) const;
494 
495  void write (std::string_view name,
496  const unsigned int write_flags=(WRITE_DATA),
497  bool partition_agnostic = true) const;
498 
499  void write (std::ostream name,
500  const unsigned int write_flags=(WRITE_DATA),
501  bool partition_agnostic = true) const;
502 
503  void write (Xdr & io,
504  const unsigned int write_flags=(WRITE_DATA),
505  bool partition_agnostic = true,
506  Xdr * const local_io = nullptr) const;
507 
514  virtual bool compare (const EquationSystems & other_es,
515  const Real threshold,
516  const bool verbose) const;
517 
522  virtual std::string get_info() const;
523 
528  void print_info (std::ostream & os=libMesh::out) const;
529 
533  friend std::ostream & operator << (std::ostream & os,
534  const EquationSystems & es);
535 
539  const MeshBase & get_mesh() const;
540 
544  MeshBase & get_mesh();
545 
550  void allgather ();
551 
556 
560  void disable_refine_in_reinit() { this->_refine_in_reinit = false; }
561 
565  bool refine_in_reinit_flag() { return this->_refine_in_reinit; }
566 
573  bool reinit_solutions ();
574 
578  virtual void reinit_systems ();
579 
584 
585 
586 protected:
587 
588 
593 
597  std::map<std::string, std::unique_ptr<System>, std::less<>> _systems;
598 
604 
610 
611 private:
619 
624  void _remove_default_ghosting(unsigned int sys_num);
625 };
626 
627 
628 
629 // ------------------------------------------------------------
630 // EquationSystems inline methods
631 inline
633 {
634  return _mesh;
635 }
636 
637 
638 
639 inline
641 {
642  return _mesh;
643 }
644 
645 
646 inline
647 unsigned int EquationSystems::n_systems () const
648 {
649  return cast_int<unsigned int>(_systems.size());
650 }
651 
652 
653 
654 
655 template <typename T_sys>
656 inline
657 T_sys & EquationSystems::add_system (std::string_view name)
658 {
659  if (!_systems.count(name))
660  {
661  const unsigned int sys_num = this->n_systems();
662 
663  auto result = _systems.emplace
664  (name, std::make_unique<T_sys>(*this, std::string(name),
665  sys_num));
666 
668  this->_remove_default_ghosting(sys_num);
669 
670  // Tell all the \p DofObject entities to add a system.
672 
673  // Return reference to newly added item
674  auto it = result.first;
675  auto & sys_ptr = it->second;
676  return cast_ref<T_sys &>(*sys_ptr);
677  }
678  else
679  {
680  // We now allow redundant add_system calls, to make it
681  // easier to load data from files for user-derived system
682  // subclasses
683  return this->get_system<T_sys>(name);
684  }
685 }
686 
687 
688 
689 inline
690 bool EquationSystems::has_system (std::string_view name) const
691 {
692  if (_systems.find(name) == _systems.end())
693  return false;
694  return true;
695 }
696 
697 
698 
699 
700 template <typename T_sys>
701 inline
702 const T_sys & EquationSystems::get_system (const unsigned int num) const
703 {
704  libmesh_assert_less (num, this->n_systems());
705 
706  for (auto & pr : _systems)
707  {
708  const auto & sys_ptr = pr.second;
709  if (sys_ptr->number() == num)
710  return cast_ref<const T_sys &>(*sys_ptr);
711  }
712  // Error if we made it here
713  libmesh_error_msg("ERROR: no system number " << num << " found!");
714 }
715 
716 
717 
718 
719 template <typename T_sys>
720 inline
721 T_sys & EquationSystems::get_system (const unsigned int num)
722 {
723  libmesh_assert_less (num, this->n_systems());
724 
725  for (auto & pr : _systems)
726  {
727  auto & sys_ptr = pr.second;
728  if (sys_ptr->number() == num)
729  return cast_ref<T_sys &>(*sys_ptr);
730  }
731 
732  // Error if we made it here
733  libmesh_error_msg("ERROR: no system number " << num << " found!");
734 }
735 
736 
737 
738 
739 
740 
741 template <typename T_sys>
742 inline
743 const T_sys & EquationSystems::get_system (std::string_view name) const
744 {
745  auto pos = _systems.find(name);
746 
747  // Check for errors
748  libmesh_error_msg_if(pos == _systems.end(), "ERROR: no system named \"" << name << "\" found!");
749 
750  // Attempt dynamic cast
751  const auto & sys_ptr = pos->second;
752  return cast_ref<const T_sys &>(*sys_ptr);
753 }
754 
755 
756 
757 
758 
759 
760 template <typename T_sys>
761 inline
762 T_sys & EquationSystems::get_system (std::string_view name)
763 {
764  auto pos = _systems.find(name);
765 
766  // Check for errors
767  libmesh_error_msg_if(pos == _systems.end(), "ERROR: no system named " << name << " found!");
768 
769  // Attempt dynamic cast
770  auto & sys_ptr = pos->second;
771  return cast_ref<T_sys &>(*sys_ptr);
772 }
773 
774 
775 
776 
777 
778 
779 
780 inline
781 const System & EquationSystems::get_system (std::string_view name) const
782 {
783  return this->get_system<System>(name);
784 }
785 
786 
787 
788 inline
790 {
791  return this->get_system<System>(name);
792 }
793 
794 
795 
796 inline
797 const System & EquationSystems::get_system (const unsigned int num) const
798 {
799  return this->get_system<System>(num);
800 }
801 
802 
803 
804 inline
805 System & EquationSystems::get_system (const unsigned int num)
806 {
807  return this->get_system<System>(num);
808 }
809 
810 
811 } // namespace libMesh
812 
813 
814 #endif // LIBMESH_EQUATION_SYSTEMS_H
EquationSystems(MeshBase &mesh)
Constructor.
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
WriteFlags
Define enumeration to set properties in EquationSystems::write()
This is the EquationSystems class.
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
Fill the input vector var_names with the names of the variables for each system.
void write(std::string_view 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.
unsigned int n_systems() const
bool _enable_default_ghosting
Flag for whether to enable default ghosting on newly added Systems.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:74
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
bool has_system(std::string_view name) const
std::unique_ptr< NumericVector< Number > > build_parallel_elemental_solution_vector(std::vector< std::string > &names) const
Builds a parallel vector of CONSTANT MONOMIAL and/or components of CONSTANT MONOMIAL_VEC solution val...
std::size_t n_dofs() const
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
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, bool add_sides=false) const
Fill the input vector soln with solution values.
bool reinit_solutions()
Handle any mesh changes and project any solutions onto the updated mesh.
virtual void clear()
Restores the data structure to a pristine state.
virtual ~EquationSystems()
Destructor.
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
void disable_refine_in_reinit()
Calls to reinit() will not try to coarsen or refine the mesh.
virtual void enable_default_ghosting(bool enable)
Enable or disable default ghosting functors on the Mesh and on all Systems.
virtual void adjoint_solve(const QoISet &qoi_indices=QoISet())
Call adjoint_solve on all the individual equation systems.
friend std::ostream & operator<<(std::ostream &os, const EquationSystems &es)
Same as above, but allows you to also use stream syntax.
The libMesh namespace provides an interface to certain functionality in the library.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:80
virtual std::string get_info() const
MeshBase & _mesh
The mesh data structure.
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:35
void allgather()
Serializes a distributed mesh and its associated degree of freedom numbering for all systems...
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...
virtual void sensitivity_solve(const ParameterVector &parameters)
Call sensitivity_solve on all the individual equation systems.
virtual void reinit_systems()
Reinitialize all systems on the current mesh.
void enable_refine_in_reinit()
Calls to reinit() will also do two-step coarsen-then-refine.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
ReadFlags
Define enumeration to set properties in EquationSystems::read()
void read(std::string_view 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.
void build_solution_vector(std::vector< Number > &soln, std::string_view system_name, std::string_view variable_name="all_vars") const
Fill the input vector soln with the solution values for the system named name.
bool _refine_in_reinit
Flag for whether to call coarsen/refine in reinit().
virtual void reinit_mesh()
Handle the association of a completely new mesh with the EquationSystem and all the Systems assigned ...
This class implements reference counting.
An object whose state is distributed along a set of processors.
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
virtual void solve()
Call solve on all the individual equation systems.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::unique_ptr< NumericVector< Number > > build_parallel_solution_vector(const std::set< std::string > *system_names=nullptr, bool add_sides=false) const
A version of build_solution_vector which is appropriate for "parallel" output formats like Nemesis...
OStreamProxy out
void _remove_default_ghosting(unsigned int sys_num)
This just calls DofMap::remove_default_ghosting() but using a shim lets us forward-declare DofMap...
const MeshBase & get_mesh() const
std::size_t n_active_dofs() const
std::vector< std::pair< unsigned int, unsigned int > > find_variable_numbers(std::vector< std::string > &names, const FEType *type=nullptr, const std::vector< FEType > *types=nullptr) const
Finds system and variable numbers for any variables of &#39;type&#39; or of &#39;types&#39; corresponding to the entr...
Parameters parameters
Data structure holding arbitrary parameters.
virtual void init()
Initialize all the systems.
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
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...
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
void update()
Updates local values for all the systems.
void build_elemental_solution_vector(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for CONSTANT MONOMIALs and/or components of CONSTANT MONOMIAL_VECs.
std::map< std::string, std::unique_ptr< System >, std::less<> > _systems
Data structure holding the systems.
static bool redundant_added_side(const Elem &elem, unsigned int side)