libMesh
equation_systems.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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/enum_xdr_mode.h"
28 #include "libmesh/parallel_object.h"
29 
30 // HP aCC needs these for some reason
31 #ifdef __HP_aCC
32 # include "libmesh/frequency_system.h"
33 # include "libmesh/transient_system.h"
34 # include "libmesh/newmark_system.h"
35 # include "libmesh/steady_system.h"
36 #endif
37 
38 // C++ includes
39 #include <cstddef>
40 #include <map>
41 #include <set>
42 #include <string>
43 #include <vector>
44 
45 namespace libMesh
46 {
47 
48 // Forward Declarations
49 class Elem;
50 class MeshBase;
51 
66 class EquationSystems : public ReferenceCountedObject<EquationSystems>,
67  public ParallelObject
68 
69 {
70 public:
71 
75  enum ReadFlags { READ_HEADER = 1,
76  READ_DATA = 2,
81 
85  enum WriteFlags { WRITE_DATA = 1,
89 
94 
99  virtual ~EquationSystems ();
100 
104  virtual void clear ();
105 
109  virtual void init ();
110 
114  virtual void reinit ();
115 
119  void update ();
120 
124  unsigned int n_systems() const;
125 
130  bool has_system (const std::string & name) const;
131 
138  template <typename T_sys>
139  const T_sys & get_system (const std::string & name) const;
140 
147  template <typename T_sys>
148  T_sys & get_system (const std::string & name);
149 
156  template <typename T_sys>
157  const T_sys & get_system (const unsigned int num) const;
158 
165  template <typename T_sys>
166  T_sys & get_system (const unsigned int num);
167 
171  const System & get_system (const std::string & name) const;
172 
176  System & get_system (const std::string & name);
177 
181  const System & get_system (const unsigned int num) const;
182 
186  System & get_system (const unsigned int num);
187 
192  virtual System & add_system (const std::string & system_type,
193  const std::string & name);
194 
198  template <typename T_sys>
199  T_sys & add_system (const std::string & name);
200 
209 #ifdef LIBMESH_ENABLE_DEPRECATED
210  void delete_system (const std::string & name);
211 #endif
212 
217  unsigned int n_vars () const;
218 
223  std::size_t n_dofs () const;
224 
229  std::size_t n_active_dofs() const;
230 
239  virtual void solve ();
240 
249  virtual void adjoint_solve (const QoISet & qoi_indices = QoISet());
250 
259  virtual void sensitivity_solve (const ParameterVector & parameters);
260 
268  void build_variable_names (std::vector<std::string> & var_names,
269  const FEType * type=libmesh_nullptr,
270  const std::set<std::string> * system_names=libmesh_nullptr) const;
271 
280  void build_solution_vector (std::vector<Number> & soln,
281  const std::string & system_name,
282  const std::string & variable_name = "all_vars") const;
283 
291  void build_solution_vector (std::vector<Number> & soln,
292  const std::set<std::string> * system_names=libmesh_nullptr) const;
293 
303  build_parallel_solution_vector(const std::set<std::string> * system_names=libmesh_nullptr) const;
304 
310  void get_solution( std::vector<Number> & soln,
311  std::vector<std::string> & names) const;
312 
320  void build_discontinuous_solution_vector (std::vector<Number> & soln,
321  const std::set<std::string> * system_names=libmesh_nullptr) const;
322 
348  template <typename InValType>
349  void read (const std::string & name,
350  const XdrMODE,
351  const unsigned int read_flags=(READ_HEADER | READ_DATA),
352  bool partition_agnostic = true);
353 
354  void read (const std::string & name,
355  const XdrMODE mode,
356  const unsigned int read_flags=(READ_HEADER | READ_DATA),
357  bool partition_agnostic = true)
358  { read<Number>(name, mode, read_flags, partition_agnostic); }
359 
360  template <typename InValType>
361  void read (const std::string & name,
362  const unsigned int read_flags=(READ_HEADER | READ_DATA),
363  bool partition_agnostic = true);
364 
365  void read (const std::string & name,
366  const unsigned int read_flags=(READ_HEADER | READ_DATA),
367  bool partition_agnostic = true)
368  { read<Number>(name, read_flags, partition_agnostic); }
369 
370 
395  void write (const std::string & name,
396  const XdrMODE,
397  const unsigned int write_flags=(WRITE_DATA),
398  bool partition_agnostic = true) const;
399 
400  void write (const std::string & name,
401  const unsigned int write_flags=(WRITE_DATA),
402  bool partition_agnostic = true) const;
403 
410  virtual bool compare (const EquationSystems & other_es,
411  const Real threshold,
412  const bool verbose) const;
413 
418  virtual std::string get_info() const;
419 
424  void print_info (std::ostream & os=libMesh::out) const;
425 
429  friend std::ostream & operator << (std::ostream & os,
430  const EquationSystems & es);
431 
435  const MeshBase & get_mesh() const;
436 
440  MeshBase & get_mesh();
441 
446  void allgather ();
447 
452 
456  void disable_refine_in_reinit() { this->_refine_in_reinit = false; }
457 
461  bool refine_in_reinit_flag() { return this->_refine_in_reinit; }
462 
463 
468 
469 
470 protected:
471 
472 
477 
481  std::map<std::string, System *> _systems;
482 
486  typedef std::map<std::string, System *>::iterator system_iterator;
487 
491  typedef std::map<std::string, System *>::const_iterator const_system_iterator;
492 
498 
499 private:
500 
515  template <typename InValType>
516  void _read_impl (const std::string & name,
517  const XdrMODE,
518  const unsigned int read_flags,
519  bool partition_agnostic = true);
520 
528 };
529 
530 
531 
532 // ------------------------------------------------------------
533 // EquationSystems inline methods
534 inline
536 {
537  return _mesh;
538 }
539 
540 
541 
542 inline
544 {
545  return _mesh;
546 }
547 
548 
549 inline
550 unsigned int EquationSystems::n_systems () const
551 {
552  return cast_int<unsigned int>(_systems.size());
553 }
554 
555 
556 
557 
558 template <typename T_sys>
559 inline
560 T_sys & EquationSystems::add_system (const std::string & name)
561 {
562  T_sys * ptr = libmesh_nullptr;
563 
564  if (!_systems.count(name))
565  {
566  ptr = new T_sys(*this, name, this->n_systems());
567 
568  _systems.insert (std::make_pair(name, ptr));
569 
570  // Tell all the \p DofObject entities to add a system.
572  }
573  else
574  {
575  // We now allow redundant add_system calls, to make it
576  // easier to load data from files for user-derived system
577  // subclasses
578  ptr = &(this->get_system<T_sys>(name));
579  }
580 
581  // Return a dynamically casted reference to the newly added System.
582  return *ptr;
583 }
584 
585 
586 
587 inline
588 bool EquationSystems::has_system (const std::string & name) const
589 {
590  if (_systems.find(name) == _systems.end())
591  return false;
592  return true;
593 }
594 
595 
596 
597 
598 template <typename T_sys>
599 inline
600 const T_sys & EquationSystems::get_system (const unsigned int num) const
601 {
602  libmesh_assert_less (num, this->n_systems());
603 
604 
605  const_system_iterator pos = _systems.begin();
606  const const_system_iterator end = _systems.end();
607 
608  for (; pos != end; ++pos)
609  if (pos->second->number() == num)
610  break;
611 
612  // Check for errors
613  if (pos == end)
614  libmesh_error_msg("ERROR: no system number " << num << " found!");
615 
616  // Attempt dynamic cast
617  return *cast_ptr<T_sys *>(pos->second);
618 }
619 
620 
621 
622 
623 template <typename T_sys>
624 inline
625 T_sys & EquationSystems::get_system (const unsigned int num)
626 {
627  libmesh_assert_less (num, this->n_systems());
628 
629  const_system_iterator pos = _systems.begin();
630  const const_system_iterator end = _systems.end();
631 
632  for (; pos != end; ++pos)
633  if (pos->second->number() == num)
634  break;
635 
636  // Check for errors
637  if (pos == end)
638  libmesh_error_msg("ERROR: no system number " << num << " found!");
639 
640  // Attempt dynamic cast
641  return *cast_ptr<T_sys *>(pos->second);
642 }
643 
644 
645 
646 
647 
648 
649 template <typename T_sys>
650 inline
651 const T_sys & EquationSystems::get_system (const std::string & name) const
652 {
653  const_system_iterator pos = _systems.find(name);
654 
655  // Check for errors
656  if (pos == _systems.end())
657  libmesh_error_msg("ERROR: no system named \"" << name << "\" found!");
658 
659  // Attempt dynamic cast
660  return *cast_ptr<T_sys *>(pos->second);
661 }
662 
663 
664 
665 
666 
667 
668 template <typename T_sys>
669 inline
670 T_sys & EquationSystems::get_system (const std::string & name)
671 {
672  system_iterator pos = _systems.find(name);
673 
674  // Check for errors
675  if (pos == _systems.end())
676  libmesh_error_msg("ERROR: no system named " << name << " found!");
677 
678  // Attempt dynamic cast
679  return *cast_ptr<T_sys *>(pos->second);
680 }
681 
682 
683 
684 
685 
686 
687 
688 inline
689 const System & EquationSystems::get_system (const std::string & name) const
690 {
691  return this->get_system<System>(name);
692 }
693 
694 
695 
696 inline
697 System & EquationSystems::get_system (const std::string & name)
698 {
699  return this->get_system<System>(name);
700 }
701 
702 
703 
704 inline
705 const System & EquationSystems::get_system (const unsigned int num) const
706 {
707  return this->get_system<System>(num);
708 }
709 
710 
711 
712 inline
713 System & EquationSystems::get_system (const unsigned int num)
714 {
715  return this->get_system<System>(num);
716 }
717 
718 
719 } // namespace libMesh
720 
721 
722 #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:39
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:178
WriteFlags
Define enumeration to set properties in EquationSystems::write()
This is the EquationSystems class.
bool has_system(const std::string &name) const
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.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:63
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
virtual void clear()
Restores the data structure to a pristine state.
virtual ~EquationSystems()
Destructor.
MeshBase & mesh
void disable_refine_in_reinit()
Calls to reinit() will not try to coarsen or refine the mesh.
const class libmesh_nullptr_t libmesh_nullptr
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end...
virtual void adjoint_solve(const QoISet &qoi_indices=QoISet())
Call adjoint_solve on all the individual equation systems.
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.
friend std::ostream & operator<<(std::ostream &os, const EquationSystems &es)
Same as above, but allows you to also use stream syntax.
UniquePtr< NumericVector< Number > > build_parallel_solution_vector(const std::set< std::string > *system_names=libmesh_nullptr) const
A version of build_solution_vector which is appropriate for "parallel" output formats like Nemesis...
The libMesh namespace provides an interface to certain functionality in the library.
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.
void build_discontinuous_solution_vector(std::vector< Number > &soln, const std::set< std::string > *system_names=libmesh_nullptr) const
Fill the input vector soln with solution values.
virtual std::string get_info() const
This is the MeshBase class.
Definition: mesh_base.h:68
MeshBase & _mesh
The mesh data structure.
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
void allgather()
Serializes a distributed mesh and its associated degree of freedom numbering for all systems...
virtual void sensitivity_solve(const ParameterVector &parameters)
Call sensitivity_solve on all the individual equation systems.
std::map< std::string, System * >::const_iterator const_system_iterator
Typedef for constant system iterators.
void read(const std::string &name, const XdrMODE mode, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
void enable_refine_in_reinit()
Calls to reinit() will also do two-step coarsen-then-refine.
XdrMODE
Defines an enum for read/write mode in Xdr format.
Definition: enum_xdr_mode.h:32
std::map< std::string, System * >::iterator system_iterator
Typedef for system iterators.
virtual bool compare(const EquationSystems &other_es, const Real threshold, const bool verbose) const
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
This is the base class for classes which contain information related to any physical process that mig...
Definition: system.h:76
virtual void reinit()
Reinitialize all the systems.
void delete_system(const std::string &name)
Remove the system named name from the systems array.
ReadFlags
Define enumeration to set properties in EquationSystems::read()
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.
bool _refine_in_reinit
Flag for whether to call coarsen/refine in reinit().
This class implements reference counting.
This class forms the base class for all other classes that are expected to be implemented in parallel...
void read(const std::string &name, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
unsigned int n_systems() const
virtual void solve()
Call solve on all the individual equation systems.
std::size_t n_dofs() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
OStreamProxy out
void build_variable_names(std::vector< std::string > &var_names, const FEType *type=libmesh_nullptr, const std::set< std::string > *system_names=libmesh_nullptr) const
Fill the input vector var_names with the names of the variables for each system.
Parameters parameters
Data structure holding arbitrary parameters.
unsigned int n_vars() const
const MeshBase & get_mesh() const
const T_sys & get_system(const std::string &name) const
virtual void init()
Initialize all the systems.
std::size_t n_active_dofs() const
void _read_impl(const std::string &name, const XdrMODE, const unsigned int read_flags, bool partition_agnostic=true)
Actual read implementation.
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 update()
Updates local values for all the systems.
void get_solution(std::vector< Number > &soln, std::vector< std::string > &names) const
Retrieve the solution data for CONSTANT MONOMIALs.
std::map< std::string, System * > _systems
Data structure holding the systems.