system.h

Go to the documentation of this file.
00001 // $Id: system.h 3546 2009-11-06 20:13:28Z roystgnr $
00002 
00003 // The libMesh Finite Element Library.
00004 // Copyright (C) 2002-2008 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00005   
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010   
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015   
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 
00021 
00022 #ifndef __system_h__
00023 #define __system_h__
00024 
00025 // C++ includes
00026 #include <vector>
00027 #include <set>
00028 
00029 // Local Includes
00030 #include "auto_ptr.h"
00031 #include "elem_range.h"
00032 #include "enum_xdr_mode.h"
00033 #include "fe_type.h"
00034 #include "libmesh_common.h"
00035 #include "qoi_set.h"
00036 #include "reference_counted_object.h"
00037 #include "system_norm.h"
00038 
00039 // Forward Declarations
00040 class System;
00041 class EquationSystems;
00042 class MeshBase;
00043 class Xdr;
00044 class DofMap;
00045 class Parameters;
00046 class ParameterVector;
00047 class Point;
00048 class SensitivityData;
00049 template <typename T> class NumericVector;
00050 template <typename T> class VectorValue;
00051 typedef VectorValue<Number> NumberVectorValue;
00052 typedef NumberVectorValue Gradient;
00053 
00066 // ------------------------------------------------------------
00067 // System class definition
00068 class System : public ReferenceCountedObject<System>
00069 {
00070 protected:
00071 
00077   System (EquationSystems& es,
00078           const std::string& name,
00079           const unsigned int number);
00080   
00081 public:
00082 
00083   
00087   virtual ~System ();
00088 
00092   typedef System sys_type;
00093 
00097   sys_type & system () { return *this; }
00098   
00103   virtual void clear ();
00104   
00109   void init ();
00110   
00117   virtual void reinit ();
00118   
00123   virtual void update ();
00124   
00131   virtual void assemble ();
00132 
00137   virtual void assemble_qoi
00138     (const QoISet &qoi_indices = QoISet());
00139  
00144   virtual void assemble_qoi_derivative 
00145     (const QoISet &qoi_indices = QoISet());
00146  
00158   virtual void assemble_residual_derivatives (const ParameterVector& parameters)
00159     { libmesh_not_implemented(); }
00160  
00164   virtual void solve () = 0;
00165   
00175   virtual std::pair<unsigned int, Real>
00176     sensitivity_solve (const ParameterVector& parameters)
00177       { libmesh_not_implemented(); }
00178   
00189   virtual std::pair<unsigned int, Real>
00190     weighted_sensitivity_solve (const ParameterVector& parameters,
00191                                 const ParameterVector& weights)
00192       { libmesh_not_implemented(); }
00193  
00204   virtual std::pair<unsigned int, Real>
00205     adjoint_solve (const QoISet& qoi_indices = QoISet())
00206       { libmesh_not_implemented(); }
00207   
00222   virtual std::pair<unsigned int, Real>
00223     weighted_sensitivity_adjoint_solve (const ParameterVector& parameters,
00224                                         const ParameterVector& weights,
00225                                         const QoISet& qoi_indices = QoISet())
00226       { libmesh_not_implemented(); }
00227  
00245   virtual void qoi_parameter_sensitivity (const QoISet& qoi_indices,
00246                                           const ParameterVector& parameters,
00247                                           SensitivityData& sensitivities);
00248   
00254   virtual void adjoint_qoi_parameter_sensitivity (const QoISet& qoi_indices,
00255                                                   const ParameterVector& parameters,
00256                                                   SensitivityData& sensitivities)
00257     { libmesh_not_implemented(); }
00258   
00264   virtual void forward_qoi_parameter_sensitivity (const QoISet& qoi_indices,
00265                                                   const ParameterVector& parameters,
00266                                                   SensitivityData& sensitivities)
00267     { libmesh_not_implemented(); }
00268   
00281   virtual void qoi_parameter_hessian_vector_product(const QoISet& qoi_indices,
00282                                                     const ParameterVector& parameters,
00283                                                     const ParameterVector& vector,
00284                                                     SensitivityData& product)
00285     { libmesh_not_implemented(); }
00286   
00292   virtual bool compare (const System& other_system, 
00293                         const Real threshold,
00294                         const bool verbose) const;
00295 
00299   const std::string & name () const;
00300 
00306   virtual std::string system_type () const { return "BasicSystem"; }
00307 
00311   void project_solution (Number fptr(const Point& p,
00312                                      const Parameters& parameters,
00313                                      const std::string& sys_name,
00314                                      const std::string& unknown_name),
00315                          Gradient gptr(const Point& p,
00316                                        const Parameters& parameters,
00317                                        const std::string& sys_name,
00318                                        const std::string& unknown_name),
00319                          Parameters& parameters) const;
00320 
00324   void project_vector (Number fptr(const Point& p,
00325                                    const Parameters& parameters,
00326                                    const std::string& sys_name,
00327                                    const std::string& unknown_name),
00328                        Gradient gptr(const Point& p,
00329                                      const Parameters& parameters,
00330                                      const std::string& sys_name,
00331                                      const std::string& unknown_name),
00332                        Parameters& parameters,
00333                        NumericVector<Number>& new_vector) const;
00334   
00338   unsigned int number () const;
00339   
00345   void update_global_solution (std::vector<Number>& global_soln) const;
00346 
00352   void update_global_solution (std::vector<Number>& global_soln,
00353                                const unsigned int dest_proc) const;
00354 
00358   const MeshBase & get_mesh() const;
00359   
00363   MeshBase & get_mesh();
00364   
00368   const DofMap & get_dof_map() const;
00369   
00373   DofMap & get_dof_map();
00374 
00378   const EquationSystems & get_equation_systems() const { return _equation_systems; }
00379 
00383   EquationSystems & get_equation_systems() { return _equation_systems; }
00384 
00389   bool active () const;
00390 
00394   void activate ();
00395 
00399   void deactivate ();
00400 
00404   typedef std::map<std::string, NumericVector<Number>* >::iterator       vectors_iterator;
00405   typedef std::map<std::string, NumericVector<Number>* >::const_iterator const_vectors_iterator;
00406 
00410   vectors_iterator vectors_begin ();
00411 
00415   const_vectors_iterator vectors_begin () const;
00416 
00420   vectors_iterator vectors_end ();
00421 
00425   const_vectors_iterator vectors_end () const;
00426 
00437   NumericVector<Number> & add_vector (const std::string& vec_name,
00438                                       const bool projections=true);
00439 
00445   bool& project_solution_on_reinit (void)
00446     { return _solution_projection; }
00447   
00452   bool have_vector (const std::string& vec_name) const;
00453   
00458   const NumericVector<Number> * request_vector (const std::string& vec_name) const;
00459   
00464   NumericVector<Number> * request_vector (const std::string& vec_name);
00465   
00471   const NumericVector<Number> * request_vector (const unsigned int vec_num) const;
00472 
00478   NumericVector<Number> * request_vector (const unsigned int vec_num);
00479 
00485   const NumericVector<Number> & get_vector (const std::string& vec_name) const;
00486 
00492   NumericVector<Number> & get_vector (const std::string& vec_name);
00493 
00499   const NumericVector<Number> & get_vector (const unsigned int vec_num) const;
00500 
00506   NumericVector<Number> & get_vector (const unsigned int vec_num);
00507 
00512   const std::string & vector_name (const unsigned int vec_num);
00513 
00519   NumericVector<Number> & add_adjoint_solution(unsigned int i=0);
00520 
00525   NumericVector<Number> & get_adjoint_solution(unsigned int i=0);
00526 
00531   const NumericVector<Number> & get_adjoint_solution(unsigned int i=0) const;
00532 
00538   NumericVector<Number> & add_sensitivity_solution(unsigned int i=0);
00539 
00544   NumericVector<Number> & get_sensitivity_solution(unsigned int i=0);
00545 
00550   const NumericVector<Number> & get_sensitivity_solution(unsigned int i=0) const;
00551 
00558   NumericVector<Number> & add_weighted_sensitivity_adjoint_solution(unsigned int i=0);
00559 
00565   NumericVector<Number> & get_weighted_sensitivity_adjoint_solution(unsigned int i=0);
00566 
00572   const NumericVector<Number> & get_weighted_sensitivity_adjoint_solution(unsigned int i=0) const;
00573 
00579   NumericVector<Number> & add_weighted_sensitivity_solution();
00580 
00585   NumericVector<Number> & get_weighted_sensitivity_solution();
00586 
00591   const NumericVector<Number> & get_weighted_sensitivity_solution() const;
00592 
00598   NumericVector<Number> & add_adjoint_rhs(unsigned int i=0);
00599 
00606   NumericVector<Number> & get_adjoint_rhs(unsigned int i=0);
00607 
00612   const NumericVector<Number> & get_adjoint_rhs(unsigned int i=0) const;
00613 
00619   NumericVector<Number> & add_sensitivity_rhs(unsigned int i=0);
00620 
00630   NumericVector<Number> & get_sensitivity_rhs(unsigned int i=0);
00631 
00636   const NumericVector<Number> & get_sensitivity_rhs(unsigned int i=0) const;
00637 
00643   unsigned int n_vectors () const;
00644 
00648   unsigned int n_vars() const;
00649 
00653   unsigned int n_dofs() const;
00654 
00659   unsigned int n_active_dofs() const;
00660 
00665   unsigned int n_constrained_dofs() const;
00666   
00671   unsigned int n_local_dofs() const;
00672 
00681   class Variable
00682   {
00683   public:
00684     
00690     Variable (const std::string &var_name,
00691               const unsigned int var_number,
00692               const FEType &var_type) :
00693       _name(var_name),
00694       _number(var_number),
00695       _type(var_type)
00696     {}
00697     
00702     Variable (const std::string &var_name,
00703               const unsigned int var_number,
00704               const FEType &var_type,
00705               const std::set<subdomain_id_type> &var_active_subdomains) :
00706       _name(var_name),
00707       _number(var_number),
00708       _type(var_type),
00709       _active_subdomains(var_active_subdomains)
00710     {}
00711     
00715     const std::string & name() const 
00716     { return _name; }
00717 
00721     unsigned int number() const 
00722     { return _number; }
00723 
00727     const FEType & type() const 
00728     { return _type; }
00729 
00736     bool active_on_subdomain (const subdomain_id_type sid) const
00737     { return (_active_subdomains.empty() || _active_subdomains.count(sid));  }
00738 
00744     bool implicitly_active () const
00745     { return _active_subdomains.empty(); }
00746     
00747   private:
00748     std::string             _name; 
00749     unsigned int            _number;
00750     FEType                  _type;
00751     std::set<subdomain_id_type> _active_subdomains;
00752   };
00753 
00758   unsigned int add_variable (const std::string& var,
00759                              const FEType& type,
00760                              const std::set<subdomain_id_type> * const active_subdomains = NULL);
00761 
00767   unsigned int add_variable (const std::string& var,
00768                              const Order order = FIRST,
00769                              const FEFamily = LAGRANGE,
00770                              const std::set<subdomain_id_type> * const active_subdomains = NULL);
00771 
00775   const Variable & variable (unsigned int var) const;
00776 
00780   bool has_variable(const std::string& var) const;
00781   
00785   const std::string & variable_name(const unsigned int i) const;
00786   
00791   unsigned short int variable_number (const std::string& var) const;
00792 
00796   const FEType & variable_type (const unsigned int i) const;
00797 
00801   const FEType & variable_type (const std::string& var) const;
00802 
00807   Real calculate_norm(NumericVector<Number>& v,
00808                       unsigned int var = 0,
00809                       FEMNormType norm_type = L2) const;
00810 
00815   Real calculate_norm(NumericVector<Number>& v,
00816                       const SystemNorm &norm) const;
00817 
00821   void read_header (Xdr& io,
00822                     const std::string &version,
00823                     const bool read_header=true,
00824                     const bool read_additional_data=true,
00825                     const bool read_legacy_format=false);
00826 
00830   void read_legacy_data (Xdr& io,
00831                          const bool read_additional_data=true);
00832 
00837   void read_serialized_data (Xdr& io,
00838                              const bool read_additional_data=true);
00839 
00846   void read_parallel_data (Xdr &io,
00847                            const bool read_additional_data);
00851   void write_header (Xdr& io,
00852                      const std::string &version,
00853                      const bool write_additional_data) const;
00854 
00859   void write_serialized_data (Xdr& io,
00860                               const bool write_additional_data = true) const;
00861 
00868   void write_parallel_data (Xdr &io,
00869                             const bool write_additional_data) const;
00870   
00875   std::string get_info () const;
00876 
00880   void attach_init_function (void fptr(EquationSystems& es,
00881                                        const std::string& name));
00882   
00887   void attach_assemble_function (void fptr(EquationSystems& es,
00888                                            const std::string& name));
00889   
00893   void attach_constraint_function (void fptr(EquationSystems& es,
00894                                              const std::string& name));
00895   
00900   void attach_QOI_function (void fptr(EquationSystems& es,
00901                                       const std::string& name,
00902                                       const QoISet& qoi_indices));
00903   
00909   void attach_QOI_derivative (void fptr(EquationSystems& es,
00910                                         const std::string& name,
00911                                         const QoISet& qoi_indices));
00912   
00917   virtual void user_initialization ();
00918   
00923   virtual void user_assembly ();
00924   
00929   virtual void user_constrain ();
00930 
00935   virtual void user_QOI (const QoISet& qoi_indices);
00936 
00941   virtual void user_QOI_derivative (const QoISet& qoi_indices);
00942 
00948   virtual void re_update ();
00949 
00953   virtual void restrict_vectors ();
00954 
00958   virtual void prolong_vectors ();
00959 
00979   bool assemble_before_solve;
00980 
00981 
00982   //--------------------------------------------------
00983   // The solution and solution access members
00984   
00989   Number current_solution (const unsigned int global_dof_number) const;
00990 
00994   AutoPtr<NumericVector<Number> > solution;
00995 
01006   AutoPtr<NumericVector<Number> > current_local_solution;
01007 
01014   std::vector<Number> qoi;
01015 
01020   void local_dof_indices (const unsigned int var,
01021                           std::set<unsigned int> & var_indices) const;
01022 
01027   void zero_variable (NumericVector<Number>& v, unsigned int var_num) const;
01028 
01029 protected:
01030 
01036   virtual void init_data ();
01037 
01042   void project_vector (NumericVector<Number>&) const;
01043 
01049   void project_vector (const NumericVector<Number>&,
01050                        NumericVector<Number>&) const;
01051   
01052 private:
01053 
01058   Real discrete_var_norm(NumericVector<Number>& v,
01059                          unsigned int var,
01060                          FEMNormType norm_type) const;
01061 
01067   template <typename iterator_type>
01068   unsigned int read_serialized_blocked_dof_objects (const unsigned int var,
01069                                                     const unsigned int n_objects,
01070                                                     const iterator_type begin,
01071                                                     const iterator_type end,
01072                                                     Xdr &io,
01073                                                     NumericVector<Number> &vec) const;
01074 
01079   void read_serialized_vector (Xdr& io,
01080                                NumericVector<Number> &vec);
01081 
01086   template <typename iterator_type>
01087   unsigned int write_serialized_blocked_dof_objects (const NumericVector<Number> &vec,
01088                                                      const unsigned int var,
01089                                                      const unsigned int n_objects,
01090                                                      const iterator_type begin,
01091                                                      const iterator_type end,
01092                                                      Xdr &io) const;
01093 
01098   void write_serialized_vector (Xdr& io,
01099                                 const NumericVector<Number> &vec) const;
01100 
01104   void (* _init_system) (EquationSystems& es,
01105                          const std::string& name);
01106   
01110   void (* _assemble_system) (EquationSystems& es,
01111                              const std::string& name);
01112 
01116   void (* _constrain_system) (EquationSystems& es, 
01117                               const std::string& name);
01118 
01122   void (* _qoi_evaluate) (EquationSystems& es, 
01123                           const std::string& name,
01124                           const QoISet& qoi_indices);
01125 
01129   void (* _qoi_evaluate_derivative) (EquationSystems& es, 
01130                                      const std::string& name,
01131                                      const QoISet& qoi_indices);
01132 
01137   AutoPtr<DofMap> _dof_map;
01138 
01143   EquationSystems& _equation_systems;
01144   
01149   MeshBase& _mesh;
01150   
01154   const std::string _sys_name;
01155 
01159   const unsigned int _sys_number;
01160   
01164   std::vector<System::Variable> _variables;
01165 
01170   std::map<std::string, unsigned short int> _variable_numbers;
01171 
01175   bool _active;
01176   
01183   std::map<std::string, NumericVector<Number>* > _vectors;
01184 
01189   std::map<std::string, bool> _vector_projections;
01190 
01196   bool _solution_projection;
01197 
01201   bool _can_add_vectors;
01202 
01208   bool _additional_data_written;
01209 
01210 
01216   class ProjectVector 
01217   {
01218   private:
01219     const System                &system;
01220     const NumericVector<Number> &old_vector;
01221     NumericVector<Number>       &new_vector;
01222 
01223   public:
01224     ProjectVector (const System &system_in,
01225                    const NumericVector<Number> &old_v_in,
01226                    NumericVector<Number> &new_v_in) :
01227     system(system_in),
01228     old_vector(old_v_in),
01229     new_vector(new_v_in)
01230     {}
01231     
01232     void operator()(const ConstElemRange &range) const;
01233   };
01234 
01235 
01245   class BuildProjectionList
01246   {
01247   private:
01248     const System              &system;
01249 
01250   public:
01251     BuildProjectionList (const System &system_in) :
01252     system(system_in)
01253     {}
01254 
01255     BuildProjectionList (BuildProjectionList &other, Threads::split) :
01256      system(other.system)
01257     {}
01258     
01259     void unique();
01260     void operator()(const ConstElemRange &range);
01261     void join (const BuildProjectionList &other);
01262     std::vector<unsigned int> send_list;
01263   };
01264 
01265 
01266 
01272   class ProjectSolution
01273   {
01274   private:
01275     const System                &system;
01276 
01277     Number (* fptr)(const Point& p,
01278                     const Parameters& parameters,
01279                     const std::string& sys_name,
01280                     const std::string& unknown_name);
01281 
01282     Gradient (* gptr)(const Point& p,
01283                       const Parameters& parameters,
01284                       const std::string& sys_name,
01285                       const std::string& unknown_name);
01286 
01287     Parameters &parameters;
01288     NumericVector<Number>       &new_vector;
01289 
01290   public:
01291     ProjectSolution (const System &system_in,
01292                      Number fptr_in(const Point& p,
01293                                     const Parameters& parameters,
01294                                     const std::string& sys_name,
01295                                     const std::string& unknown_name),
01296                      Gradient gptr_in(const Point& p,
01297                                       const Parameters& parameters,
01298                                       const std::string& sys_name,
01299                                       const std::string& unknown_name),
01300                      Parameters &parameters_in,
01301                      NumericVector<Number> &new_v_in) :
01302     system(system_in),
01303     fptr(fptr_in),
01304     gptr(gptr_in),
01305     parameters(parameters_in),
01306     new_vector(new_v_in)
01307     {}
01308     
01309     void operator()(const ConstElemRange &range) const;
01310   };
01311 };
01312 
01313 
01314 
01315 // ------------------------------------------------------------
01316 // System inline methods
01317 inline
01318 const std::string & System::name() const
01319 {
01320   return _sys_name;
01321 }
01322 
01323 
01324 
01325 inline
01326 unsigned int System::number() const
01327 {
01328   return _sys_number;
01329 }
01330 
01331 
01332 
01333 inline
01334 const MeshBase & System::get_mesh() const
01335 {
01336   return _mesh;
01337 }
01338 
01339 
01340 
01341 inline
01342 MeshBase & System::get_mesh()
01343 {
01344   return _mesh;
01345 }
01346 
01347 
01348 
01349 inline
01350 const DofMap & System::get_dof_map() const
01351 {
01352   return *_dof_map;
01353 }
01354 
01355 
01356 
01357 inline
01358 DofMap & System::get_dof_map()
01359 {
01360   return *_dof_map;
01361 }
01362 
01363 
01364 
01365 inline
01366 bool System::active() const
01367 {
01368   return _active;  
01369 }
01370 
01371 
01372 
01373 inline
01374 void System::activate ()
01375 {
01376   _active = true;
01377 }
01378 
01379 
01380 
01381 inline
01382 void System::deactivate ()
01383 {
01384   _active = false;
01385 }
01386 
01387 
01388 
01389 inline
01390 unsigned int System::n_vars() const
01391 {
01392   return _variables.size();
01393 }
01394 
01395 
01396 
01397 inline
01398 const System::Variable & System::variable (const unsigned int i) const
01399 {
01400   libmesh_assert (i < _variables.size());
01401 
01402   return _variables[i];
01403 }
01404 
01405 
01406 
01407 inline
01408 const std::string & System::variable_name (const unsigned int i) const
01409 {
01410   libmesh_assert (i < _variables.size());
01411 
01412   return _variables[i].name();
01413 }
01414 
01415 
01416 
01417 inline
01418 const FEType & System::variable_type (const unsigned int i) const
01419 {
01420   libmesh_assert (i < _variables.size());
01421   
01422   return _variables[i].type();
01423 }
01424 
01425 
01426 
01427 inline
01428 const FEType & System::variable_type (const std::string& var) const
01429 {
01430   return _variables[this->variable_number(var)].type();
01431 }
01432 
01433 
01434 
01435 inline
01436 unsigned int System::n_active_dofs() const
01437 {
01438   return this->n_dofs() - this->n_constrained_dofs();
01439 }
01440 
01441 
01442 
01443 inline
01444 bool System::have_vector (const std::string& vec_name) const
01445 {
01446   return (_vectors.count(vec_name));
01447 }
01448 
01449 
01450 
01451 inline
01452 unsigned int System::n_vectors () const
01453 {
01454   return _vectors.size(); 
01455 }
01456 
01457 inline
01458 System::vectors_iterator System::vectors_begin ()
01459 {
01460   return _vectors.begin();
01461 }
01462 
01463 inline
01464 System::const_vectors_iterator System::vectors_begin () const
01465 {
01466   return _vectors.begin();
01467 }
01468 
01469 inline
01470 System::vectors_iterator System::vectors_end ()
01471 {
01472   return _vectors.end();
01473 }
01474 
01475 inline
01476 System::const_vectors_iterator System::vectors_end () const
01477 {
01478   return _vectors.end();
01479 }
01480 
01481 
01482 
01483 #endif // #define __system_h__

Site Created By: libMesh Developers
Last modified: November 08 2009 22:16:26.

Hosted By:
SourceForge.net Logo