$extrastylesheet
equation_systems.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_EQUATION_SYSTEMS_H
00021 #define LIBMESH_EQUATION_SYSTEMS_H
00022 
00023 // Local Includes
00024 #include "libmesh/libmesh_common.h"
00025 #include "libmesh/parameters.h"
00026 #include "libmesh/system.h"
00027 #include "libmesh/enum_xdr_mode.h"
00028 #include "libmesh/parallel_object.h"
00029 
00030 // HP aCC needs these for some reason
00031 #ifdef __HP_aCC
00032 # include "libmesh/frequency_system.h"
00033 # include "libmesh/transient_system.h"
00034 # include "libmesh/newmark_system.h"
00035 # include "libmesh/steady_system.h"
00036 #endif
00037 
00038 // C++ includes
00039 #include <cstddef>
00040 #include <map>
00041 #include <set>
00042 #include <string>
00043 #include <vector>
00044 
00045 namespace libMesh
00046 {
00047 
00048 // Forward Declarations
00049 class MeshData;
00050 class Elem;
00051 class MeshBase;
00052 
00066 // ------------------------------------------------------------
00067 // EquationSystems class definition
00068 class EquationSystems : public ReferenceCountedObject<EquationSystems>,
00069                         public ParallelObject
00070 
00071 {
00072 public:
00073 
00077   enum ReadFlags { READ_HEADER           = 1,
00078                    READ_DATA             = 2,
00079                    READ_ADDITIONAL_DATA  = 4,
00080                    READ_LEGACY_FORMAT    = 8,
00081                    TRY_READ_IFEMS        = 16,
00082                    READ_BASIC_ONLY       = 32 };
00083 
00087   enum WriteFlags { WRITE_DATA             = 1,
00088                     WRITE_ADDITIONAL_DATA  = 2,
00089                     WRITE_PARALLEL_FILES   = 4,
00090                     WRITE_SERIAL_FILES     = 8 };
00091 
00095   EquationSystems (MeshBase& mesh, MeshData* mesh_data=NULL);
00096 
00101   virtual ~EquationSystems ();
00102 
00106   virtual void clear ();
00107 
00111   virtual void init ();
00112 
00116   virtual void reinit ();
00117 
00121   void update ();
00122 
00126   unsigned int n_systems() const;
00127 
00132   bool has_system (const std::string& name) const;
00133 
00140   template <typename T_sys>
00141   const T_sys & get_system (const std::string& name) const;
00142 
00149   template <typename T_sys>
00150   T_sys & get_system (const std::string& name);
00151 
00158   template <typename T_sys>
00159   const T_sys & get_system (const unsigned int num) const;
00160 
00167   template <typename T_sys>
00168   T_sys & get_system (const unsigned int num);
00169 
00173   const System & get_system (const std::string& name) const;
00174 
00178   System & get_system (const std::string& name);
00179 
00183   const System & get_system (const unsigned int num) const;
00184 
00188   System & get_system (const unsigned int num);
00189 
00194   virtual System & add_system (const std::string& system_type,
00195                                const std::string& name);
00196 
00200   template <typename T_sys>
00201   T_sys & add_system (const std::string& name);
00202 
00208   void delete_system (const std::string& name);
00209 
00214   unsigned int n_vars () const;
00215 
00220   std::size_t n_dofs () const;
00221 
00226   std::size_t n_active_dofs() const;
00227 
00236   virtual void solve ();
00237 
00246   virtual void adjoint_solve (const QoISet& qoi_indices = QoISet());
00247 
00256   virtual void sensitivity_solve (const ParameterVector& parameters);
00257 
00265   void build_variable_names (std::vector<std::string>& var_names,
00266                              const FEType *type=NULL,
00267                              const std::set<std::string>* system_names=NULL) const;
00268 
00275   void build_solution_vector (std::vector<Number>& soln,
00276                               const std::string& system_name,
00277                               const std::string& variable_name = "all_vars") const;
00278 
00286   void build_solution_vector (std::vector<Number>& soln,
00287                               const std::set<std::string>* system_names=NULL) const;
00288 
00294   void get_solution( std::vector<Number> & soln,
00295                      std::vector<std::string> & names) const;
00296 
00304   void build_discontinuous_solution_vector (std::vector<Number>& soln,
00305                                             const std::set<std::string>* system_names=NULL) const;
00306 
00329   template <typename InValType>
00330   void read (const std::string& name,
00331              const XdrMODE,
00332              const unsigned int read_flags=(READ_HEADER | READ_DATA),
00333              bool partition_agnostic = true);
00334 
00335   void read (const std::string& name,
00336              const XdrMODE mode,
00337              const unsigned int read_flags=(READ_HEADER | READ_DATA),
00338              bool partition_agnostic = true)
00339   { read<Number>(name, mode, read_flags, partition_agnostic); }
00340 
00341   template <typename InValType>
00342   void read (const std::string& name,
00343              const unsigned int read_flags=(READ_HEADER | READ_DATA),
00344              bool partition_agnostic = true);
00345 
00346   void read (const std::string& name,
00347              const unsigned int read_flags=(READ_HEADER | READ_DATA),
00348              bool partition_agnostic = true)
00349   { read<Number>(name, read_flags, partition_agnostic); }
00350 
00351 
00373   void write (const std::string& name,
00374               const XdrMODE,
00375               const unsigned int write_flags=(WRITE_DATA),
00376               bool partition_agnostic = true) const;
00377 
00378   void write (const std::string& name,
00379               const unsigned int write_flags=(WRITE_DATA),
00380               bool partition_agnostic = true) const;
00381 
00388   virtual bool compare (const EquationSystems& other_es,
00389                         const Real threshold,
00390                         const bool verbose) const;
00391 
00396   virtual std::string get_info() const;
00397 
00402   void print_info (std::ostream& os=libMesh::out) const;
00403 
00407   friend std::ostream& operator << (std::ostream& os, const EquationSystems& es);
00408 
00412   const MeshBase & get_mesh() const;
00413 
00417   MeshBase & get_mesh();
00418 
00423   bool has_mesh_data() const;
00424 
00428   const MeshData & get_mesh_data() const;
00429 
00433   MeshData & get_mesh_data();
00434 
00439   void allgather ();
00440 
00441 
00445   Parameters parameters;
00446 
00447 
00448 protected:
00449 
00450 
00454   MeshBase& _mesh;
00455 
00460   MeshData* _mesh_data;
00461 
00465   std::map<std::string, System*> _systems;
00466 
00470   typedef std::map<std::string, System*>::iterator       system_iterator;
00471 
00475   typedef std::map<std::string, System*>::const_iterator const_system_iterator;
00476 
00477 private:
00478 
00489   template <typename InValType>
00490   void _read_impl (const std::string& name,
00491                    const XdrMODE,
00492                    const unsigned int read_flags,
00493                    bool partition_agnostic = true);
00494 
00501   void _add_system_to_nodes_and_elems();
00502 };
00503 
00504 
00505 
00506 // ------------------------------------------------------------
00507 // EquationSystems inline methods
00508 inline
00509 const MeshBase & EquationSystems::get_mesh () const
00510 {
00511   return _mesh;
00512 }
00513 
00514 
00515 
00516 inline
00517 MeshBase & EquationSystems::get_mesh ()
00518 {
00519   return _mesh;
00520 }
00521 
00522 
00523 inline
00524 const MeshData & EquationSystems::get_mesh_data () const
00525 {
00526   libmesh_assert(_mesh_data);
00527   return *_mesh_data;
00528 }
00529 
00530 
00531 inline
00532 MeshData & EquationSystems::get_mesh_data ()
00533 {
00534   libmesh_assert(_mesh_data);
00535   return *_mesh_data;
00536 }
00537 
00538 inline
00539 bool EquationSystems::has_mesh_data () const
00540 {
00541   return (_mesh_data!=NULL);
00542 }
00543 
00544 
00545 inline
00546 unsigned int EquationSystems::n_systems () const
00547 {
00548   return cast_int<unsigned int>(_systems.size());
00549 }
00550 
00551 
00552 
00553 
00554 template <typename T_sys>
00555 inline
00556 T_sys & EquationSystems::add_system (const std::string& name)
00557 {
00558   T_sys* ptr = NULL;
00559 
00560   if (!_systems.count(name))
00561     {
00562       ptr = new T_sys(*this, name, this->n_systems());
00563 
00564       _systems.insert (std::make_pair(name, ptr));
00565 
00566       // Tell all the \p DofObject entities to add a system.
00567       this->_add_system_to_nodes_and_elems();
00568     }
00569   else
00570     {
00571       // We now allow redundant add_system calls, to make it
00572       // easier to load data from files for user-derived system
00573       // subclasses
00574       ptr = &(this->get_system<T_sys>(name));
00575     }
00576 
00577   // Return a dynamically casted reference to the newly added System.
00578   return *ptr;
00579 }
00580 
00581 
00582 
00583 inline
00584 bool EquationSystems::has_system (const std::string& name) const
00585 {
00586   if (_systems.find(name) == _systems.end())
00587     return false;
00588   return true;
00589 }
00590 
00591 
00592 
00593 
00594 template <typename T_sys>
00595 inline
00596 const T_sys & EquationSystems::get_system (const unsigned int num) const
00597 {
00598   libmesh_assert_less (num, this->n_systems());
00599 
00600 
00601   const_system_iterator       pos = _systems.begin();
00602   const const_system_iterator end = _systems.end();
00603 
00604   for (; pos != end; ++pos)
00605     if (pos->second->number() == num)
00606       break;
00607 
00608   // Check for errors
00609   if (pos == end)
00610     libmesh_error_msg("ERROR: no system number " << num << " found!");
00611 
00612   // Attempt dynamic cast
00613   return *cast_ptr<T_sys*>(pos->second);
00614 }
00615 
00616 
00617 
00618 
00619 template <typename T_sys>
00620 inline
00621 T_sys & EquationSystems::get_system (const unsigned int num)
00622 {
00623   libmesh_assert_less (num, this->n_systems());
00624 
00625   const_system_iterator       pos = _systems.begin();
00626   const const_system_iterator end = _systems.end();
00627 
00628   for (; pos != end; ++pos)
00629     if (pos->second->number() == num)
00630       break;
00631 
00632   // Check for errors
00633   if (pos == end)
00634     libmesh_error_msg("ERROR: no system number " << num << " found!");
00635 
00636   // Attempt dynamic cast
00637   return *cast_ptr<T_sys*>(pos->second);
00638 }
00639 
00640 
00641 
00642 
00643 
00644 
00645 template <typename T_sys>
00646 inline
00647 const T_sys & EquationSystems::get_system (const std::string& name) const
00648 {
00649   const_system_iterator pos = _systems.find(name);
00650 
00651   // Check for errors
00652   if (pos == _systems.end())
00653     libmesh_error_msg("ERROR: no system named \"" << name << "\" found!");
00654 
00655   // Attempt dynamic cast
00656   return *cast_ptr<T_sys*>(pos->second);
00657 }
00658 
00659 
00660 
00661 
00662 
00663 
00664 template <typename T_sys>
00665 inline
00666 T_sys & EquationSystems::get_system (const std::string& name)
00667 {
00668   system_iterator pos = _systems.find(name);
00669 
00670   // Check for errors
00671   if (pos == _systems.end())
00672     libmesh_error_msg("ERROR: no system named " << name << " found!");
00673 
00674   // Attempt dynamic cast
00675   return *cast_ptr<T_sys*>(pos->second);
00676 }
00677 
00678 
00679 
00680 
00681 
00682 
00683 
00684 inline
00685 const System & EquationSystems::get_system (const std::string& name) const
00686 {
00687   return this->get_system<System>(name);
00688 }
00689 
00690 
00691 
00692 inline
00693 System & EquationSystems::get_system (const std::string& name)
00694 {
00695   return this->get_system<System>(name);
00696 }
00697 
00698 
00699 
00700 inline
00701 const System & EquationSystems::get_system (const unsigned int num) const
00702 {
00703   return this->get_system<System>(num);
00704 }
00705 
00706 
00707 
00708 inline
00709 System & EquationSystems::get_system (const unsigned int num)
00710 {
00711   return this->get_system<System>(num);
00712 }
00713 
00714 
00715 } // namespace libMesh
00716 
00717 
00718 #endif // LIBMESH_EQUATION_SYSTEMS_H