libMesh
Loading...
Searching...
No Matches
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 <functional>
40#include <map>
41#include <set>
42#include <string>
43#include <string_view>
44#include <vector>
45#include <memory>
46
47namespace libMesh
48{
49
50// Forward Declarations
51class Elem;
52class MeshBase;
53enum XdrMODE : int;
54
69class EquationSystems : public ReferenceCountedObject<EquationSystems>,
70 public ParallelObject
71
72{
73public:
74
84
92
97
103
107 virtual void clear ();
108
112 virtual void init ();
113
118 virtual void reinit ();
119
124 virtual void reinit_mesh ();
125
138 virtual void enable_default_ghosting (bool enable);
139
143 void update ();
144
148 unsigned int n_systems() const;
149
154 bool has_system (std::string_view name) const;
155
162 template <typename T_sys>
163 const T_sys & get_system (std::string_view name) const;
164
171 template <typename T_sys>
172 T_sys & get_system (std::string_view name);
173
180 template <typename T_sys>
181 const T_sys & get_system (const unsigned int num) const;
182
189 template <typename T_sys>
190 T_sys & get_system (const unsigned int num);
191
195 const System & get_system (std::string_view name) const;
196
200 System & get_system (std::string_view name);
201
205 const System & get_system (const unsigned int num) const;
206
210 System & get_system (const unsigned int num);
211
216 virtual System & add_system (std::string_view system_type,
217 std::string_view name);
218
222 template <typename T_sys>
223 T_sys & add_system (std::string_view name);
224
229 unsigned int n_vars () const;
230
235 std::size_t n_dofs () const;
236
241 std::size_t n_active_dofs() const;
242
251 virtual void solve ();
252
261 virtual void adjoint_solve (const QoISet & qoi_indices = QoISet());
262
271 virtual void sensitivity_solve (const ParameterVector & parameters);
272
280 void build_variable_names (std::vector<std::string> & var_names,
281 const FEType * type=nullptr,
282 const std::set<std::string> * system_names=nullptr) const;
283
290 static bool is_elemental_data_fe_type (const FEType & type);
291
299 (std::vector<std::string> & var_names,
300 const std::set<std::string> * system_names=nullptr) const;
301
310 void build_solution_vector (std::vector<Number> & soln,
311 std::string_view system_name,
312 std::string_view variable_name = "all_vars") const;
313
325 void build_solution_vector (std::vector<Number> & soln,
326 const std::set<std::string> * system_names=nullptr,
327 bool add_sides=false) const;
328
337 std::unique_ptr<NumericVector<Number>>
338 build_parallel_solution_vector(const std::set<std::string> * system_names=nullptr,
339 bool add_sides=false) const;
340
345 void get_vars_active_subdomains(const std::vector<std::string> & names,
346 std::vector<std::set<subdomain_id_type>> & vars_active_subdomains) const;
347
356 void build_elemental_solution_vector (std::vector<Number> & soln,
357 std::vector<std::string> & names) const;
358
381 std::vector<std::pair<unsigned int, unsigned int>>
382 find_variable_numbers (std::vector<std::string> & names,
383 const FEType * type=nullptr,
384 const std::vector<FEType> * types=nullptr) const;
385
391 std::vector<std::pair<unsigned int, unsigned int>>
392 find_elemental_data_variable_numbers (std::vector<std::string> & names) const;
393
411 std::unique_ptr<NumericVector<Number>>
412 build_parallel_elemental_solution_vector (std::vector<std::string> & names) const;
413
431 (std::vector<Number> & soln,
432 const std::set<std::string> * system_names = nullptr,
433 const std::vector<std::string> * var_names = nullptr,
434 bool vertices_only = false,
435 bool add_sides = false) const;
436
437 /*
438 * Returns true iff the given side of the given element is *never*
439 * added to output from that element, because it is considered to be
440 * redundant with respect to the same data added from the
441 * neighboring element sharing that side. This helper function is
442 * used with the add_sides option when building solution vectors
443 * here and when outputting solution vectors in MeshOutput
444 * (currently just Exodus) I/O.
445 */
446 static bool redundant_added_side(const Elem & elem, unsigned int side);
447
448
474 template <typename InValType = Number>
475 void read (std::string_view name,
476 const XdrMODE,
477 const unsigned int read_flags=(READ_HEADER | READ_DATA),
478 bool partition_agnostic = true);
479
480 template <typename InValType = Number>
481 void read (std::string_view name,
482 const unsigned int read_flags=(READ_HEADER | READ_DATA),
483 bool partition_agnostic = true);
484
485 template <typename InValType = Number>
486 void read (Xdr & io,
487 std::function<std::unique_ptr<Xdr>()> & local_io_functor,
488 const unsigned int read_flags=(READ_HEADER | READ_DATA),
489 bool partition_agnostic = true);
490
515 void write (std::string_view name,
516 const XdrMODE,
517 const unsigned int write_flags=(WRITE_DATA),
518 bool partition_agnostic = true) const;
519
520 void write (std::string_view name,
521 const unsigned int write_flags=(WRITE_DATA),
522 bool partition_agnostic = true) const;
523
524 void write (std::ostream name,
525 const unsigned int write_flags=(WRITE_DATA),
526 bool partition_agnostic = true) const;
527
528 void write (Xdr & io,
529 const unsigned int write_flags=(WRITE_DATA),
530 bool partition_agnostic = true,
531 Xdr * const local_io = nullptr) const;
532
539 virtual bool compare (const EquationSystems & other_es,
540 const Real threshold,
541 const bool verbose) const;
542
547 virtual std::string get_info() const;
548
553 void print_info (std::ostream & os=libMesh::out) const;
554
558 friend std::ostream & operator << (std::ostream & os,
559 const EquationSystems & es);
560
564 const MeshBase & get_mesh() const;
565
569 MeshBase & get_mesh();
570
575 void allgather ();
576
581
586
591
598 bool reinit_solutions ();
599
603 virtual void reinit_systems ();
604
609
610
611protected:
612
613
618
622 std::map<std::string, std::unique_ptr<System>, std::less<>> _systems;
623
629
635
636private:
640 std::vector<std::pair<unsigned int, unsigned int>>
642 (std::vector<std::string> & names,
643 const std::function<bool(const FEType &)> & type_filter) const;
644
652
657 void _remove_default_ghosting(unsigned int sys_num);
658};
659
660
661
662// ------------------------------------------------------------
663// EquationSystems inline methods
664inline
666{
667 return _mesh;
668}
669
670
671
672inline
674{
675 return _mesh;
676}
677
678
679inline
680unsigned int EquationSystems::n_systems () const
681{
682 return cast_int<unsigned int>(_systems.size());
683}
684
685
686
687
688template <typename T_sys>
689inline
690T_sys & EquationSystems::add_system (std::string_view name)
691{
692 if (!_systems.count(name))
693 {
694 const unsigned int sys_num = this->n_systems();
695
696 auto result = _systems.emplace
697 (name, std::make_unique<T_sys>(*this, std::string(name),
698 sys_num));
699
701 this->_remove_default_ghosting(sys_num);
702
703 // Tell all the \p DofObject entities to add a system.
705
706 // Return reference to newly added item
707 auto it = result.first;
708 auto & sys_ptr = it->second;
709 return cast_ref<T_sys &>(*sys_ptr);
710 }
711 else
712 {
713 // We now allow redundant add_system calls, to make it
714 // easier to load data from files for user-derived system
715 // subclasses
716 return this->get_system<T_sys>(name);
717 }
718}
719
720
721
722inline
723bool EquationSystems::has_system (std::string_view name) const
724{
725 if (_systems.find(name) == _systems.end())
726 return false;
727 return true;
728}
729
730
731
732
733template <typename T_sys>
734inline
735const T_sys & EquationSystems::get_system (const unsigned int num) const
736{
737 libmesh_assert_less (num, this->n_systems());
738
739 for (auto & pr : _systems)
740 {
741 const auto & sys_ptr = pr.second;
742 if (sys_ptr->number() == num)
743 return cast_ref<const T_sys &>(*sys_ptr);
744 }
745 // Error if we made it here
746 libmesh_error_msg("ERROR: no system number " << num << " found!");
747}
748
749
750
751
752template <typename T_sys>
753inline
754T_sys & EquationSystems::get_system (const unsigned int num)
755{
756 libmesh_assert_less (num, this->n_systems());
757
758 for (auto & pr : _systems)
759 {
760 auto & sys_ptr = pr.second;
761 if (sys_ptr->number() == num)
762 return cast_ref<T_sys &>(*sys_ptr);
763 }
764
765 // Error if we made it here
766 libmesh_error_msg("ERROR: no system number " << num << " found!");
767}
768
769
770
771
772
773
774template <typename T_sys>
775inline
776const T_sys & EquationSystems::get_system (std::string_view name) const
777{
778 auto pos = _systems.find(name);
779
780 // Check for errors
781 libmesh_error_msg_if(pos == _systems.end(), "ERROR: no system named \"" << name << "\" found!");
782
783 // Attempt dynamic cast
784 const auto & sys_ptr = pos->second;
785 return cast_ref<const T_sys &>(*sys_ptr);
786}
787
788
789
790
791
792
793template <typename T_sys>
794inline
795T_sys & EquationSystems::get_system (std::string_view name)
796{
797 auto pos = _systems.find(name);
798
799 // Check for errors
800 libmesh_error_msg_if(pos == _systems.end(), "ERROR: no system named " << name << " found!");
801
802 // Attempt dynamic cast
803 auto & sys_ptr = pos->second;
804 return cast_ref<T_sys &>(*sys_ptr);
805}
806
807
808
809
810
811
812
813inline
814const System & EquationSystems::get_system (std::string_view name) const
815{
816 return this->get_system<System>(name);
817}
818
819
820
821inline
822System & EquationSystems::get_system (std::string_view name)
823{
824 return this->get_system<System>(name);
825}
826
827
828
829inline
830const System & EquationSystems::get_system (const unsigned int num) const
831{
832 return this->get_system<System>(num);
833}
834
835
836
837inline
838System & EquationSystems::get_system (const unsigned int num)
839{
840 return this->get_system<System>(num);
841}
842
843
844} // namespace libMesh
845
846
847#endif // LIBMESH_EQUATION_SYSTEMS_H
void ErrorVector unsigned int
This is the base class from which all geometric element types are derived.
Definition elem.h:96
This is the EquationSystems class.
std::vector< std::pair< unsigned int, unsigned int > > find_elemental_data_variable_numbers(std::vector< std::string > &names) const
Finds system and variable numbers for variables that can be represented as elemental data.
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
static bool is_elemental_data_fe_type(const FEType &type)
std::vector< std::pair< unsigned int, unsigned int > > find_variable_numbers_by_predicate(std::vector< std::string > &names, const std::function< bool(const FEType &)> &type_filter) const
Implementation detail for find_variable_numbers() variants.
virtual std::string get_info() const
void enable_refine_in_reinit()
Calls to reinit() will also do two-step coarsen-then-refine.
std::unique_ptr< NumericVector< Number > > build_parallel_elemental_solution_vector(std::vector< std::string > &names) const
Builds a parallel vector of elemental data solution values corresponding to the entries in the input ...
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
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.
friend std::ostream & operator<<(std::ostream &os, const EquationSystems &es)
Same as above, but allows you to also use stream syntax.
virtual ~EquationSystems()
Destructor.
virtual void sensitivity_solve(const ParameterVector &parameters)
Call sensitivity_solve on all the individual equation systems.
void _remove_default_ghosting(unsigned int sys_num)
This just calls DofMap::remove_default_ghosting() but using a shim lets us forward-declare DofMap.
std::size_t n_active_dofs() const
void update()
Updates local values for all the systems.
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 'type' or of 'types' corresponding to the entr...
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.
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 build_elemental_solution_vector(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for elemental data variables.
unsigned int n_systems() const
static bool redundant_added_side(const Elem &elem, unsigned int side)
unsigned int n_vars() const
WriteFlags
Define enumeration to set properties in EquationSystems::write()
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.
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.
const MeshBase & get_mesh() const
Parameters parameters
Data structure holding arbitrary parameters.
void build_elemental_data_variable_names(std::vector< std::string > &var_names, const std::set< std::string > *system_names=nullptr) const
Filter var_names to names of variables that can be represented as elemental data.
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 has_system(std::string_view name) const
bool _refine_in_reinit
Flag for whether to call coarsen/refine in reinit().
void write(std::ostream name, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
ReadFlags
Define enumeration to set properties in EquationSystems::read()
MeshBase & _mesh
The mesh data structure.
bool reinit_solutions()
Handle any mesh changes and project any solutions onto the updated mesh.
virtual void init()
Initialize all the systems.
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.
virtual void reinit_mesh()
Handle the association of a completely new mesh with the EquationSystem and all the Systems assigned ...
virtual void adjoint_solve(const QoISet &qoi_indices=QoISet())
Call adjoint_solve on all the individual equation systems.
virtual void reinit_systems()
Reinitialize all systems on the current mesh.
virtual void clear()
Restores the data structure to a pristine state.
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...
const T_sys & get_system(std::string_view name) const
virtual void enable_default_ghosting(bool enable)
Enable or disable default ghosting functors on the Mesh and on all Systems.
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.
void disable_refine_in_reinit()
Calls to reinit() will not try to coarsen or refine the mesh.
std::size_t n_dofs() const
virtual void solve()
Call solve on all the individual equation systems.
bool _enable_default_ghosting
Flag for whether to enable default ghosting on newly added Systems.
std::map< std::string, std::unique_ptr< System >, std::less<> > _systems
Data structure holding the systems.
void allgather()
Serializes a distributed mesh and its associated degree of freedom numbering for all systems.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
This is the MeshBase class.
Definition mesh_base.h:81
An object whose state is distributed along a set of processors.
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition qoi_set.h:46
This class implements reference counting.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition xdr_cxx.h:68
MeshBase & mesh
The libMesh namespace provides an interface to certain functionality in the library.
OStreamProxy out
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
XdrMODE
Defines an enum for read/write mode in Xdr format.