00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __system_h__
00023 #define __system_h__
00024
00025
00026 #include <vector>
00027 #include <set>
00028
00029
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
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
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
00163 virtual void solve () = 0;
00164
00174 virtual std::pair<unsigned int, Real>
00175 sensitivity_solve (const ParameterVector& parameters);
00176
00187 virtual std::pair<unsigned int, Real>
00188 weighted_sensitivity_solve (const ParameterVector& parameters,
00189 const ParameterVector& weights);
00190
00201 virtual std::pair<unsigned int, Real>
00202 adjoint_solve (const QoISet& qoi_indices = QoISet());
00203
00218 virtual std::pair<unsigned int, Real>
00219 weighted_sensitivity_adjoint_solve (const ParameterVector& parameters,
00220 const ParameterVector& weights,
00221 const QoISet& qoi_indices = QoISet());
00222
00240 virtual void qoi_parameter_sensitivity (const QoISet& qoi_indices,
00241 const ParameterVector& parameters,
00242 SensitivityData& sensitivities);
00243
00249 virtual void adjoint_qoi_parameter_sensitivity (const QoISet& qoi_indices,
00250 const ParameterVector& parameters,
00251 SensitivityData& sensitivities);
00252
00258 virtual void forward_qoi_parameter_sensitivity (const QoISet& qoi_indices,
00259 const ParameterVector& parameters,
00260 SensitivityData& sensitivities);
00261
00272 virtual void qoi_parameter_hessian(const QoISet& qoi_indices,
00273 const ParameterVector& parameters,
00274 SensitivityData& hessian);
00275
00288 virtual void qoi_parameter_hessian_vector_product(const QoISet& qoi_indices,
00289 const ParameterVector& parameters,
00290 const ParameterVector& vector,
00291 SensitivityData& product);
00292
00298 virtual bool compare (const System& other_system,
00299 const Real threshold,
00300 const bool verbose) const;
00301
00305 const std::string & name () const;
00306
00312 virtual std::string system_type () const { return "BasicSystem"; }
00313
00317 void project_solution (Number fptr(const Point& p,
00318 const Parameters& parameters,
00319 const std::string& sys_name,
00320 const std::string& unknown_name),
00321 Gradient gptr(const Point& p,
00322 const Parameters& parameters,
00323 const std::string& sys_name,
00324 const std::string& unknown_name),
00325 Parameters& parameters) const;
00326
00330 void project_vector (Number fptr(const Point& p,
00331 const Parameters& parameters,
00332 const std::string& sys_name,
00333 const std::string& unknown_name),
00334 Gradient gptr(const Point& p,
00335 const Parameters& parameters,
00336 const std::string& sys_name,
00337 const std::string& unknown_name),
00338 Parameters& parameters,
00339 NumericVector<Number>& new_vector) const;
00340
00344 unsigned int number () const;
00345
00351 void update_global_solution (std::vector<Number>& global_soln) const;
00352
00358 void update_global_solution (std::vector<Number>& global_soln,
00359 const unsigned int dest_proc) const;
00360
00364 const MeshBase & get_mesh() const;
00365
00369 MeshBase & get_mesh();
00370
00374 const DofMap & get_dof_map() const;
00375
00379 DofMap & get_dof_map();
00380
00384 const EquationSystems & get_equation_systems() const { return _equation_systems; }
00385
00389 EquationSystems & get_equation_systems() { return _equation_systems; }
00390
00395 bool active () const;
00396
00400 void activate ();
00401
00405 void deactivate ();
00406
00410 typedef std::map<std::string, NumericVector<Number>* >::iterator vectors_iterator;
00411 typedef std::map<std::string, NumericVector<Number>* >::const_iterator const_vectors_iterator;
00412
00416 vectors_iterator vectors_begin ();
00417
00421 const_vectors_iterator vectors_begin () const;
00422
00426 vectors_iterator vectors_end ();
00427
00431 const_vectors_iterator vectors_end () const;
00432
00443 NumericVector<Number> & add_vector (const std::string& vec_name,
00444 const bool projections=true);
00445
00451 bool& project_solution_on_reinit (void)
00452 { return _solution_projection; }
00453
00458 bool have_vector (const std::string& vec_name) const;
00459
00464 const NumericVector<Number> * request_vector (const std::string& vec_name) const;
00465
00470 NumericVector<Number> * request_vector (const std::string& vec_name);
00471
00477 const NumericVector<Number> * request_vector (const unsigned int vec_num) const;
00478
00484 NumericVector<Number> * request_vector (const unsigned int vec_num);
00485
00491 const NumericVector<Number> & get_vector (const std::string& vec_name) const;
00492
00498 NumericVector<Number> & get_vector (const std::string& vec_name);
00499
00505 const NumericVector<Number> & get_vector (const unsigned int vec_num) const;
00506
00512 NumericVector<Number> & get_vector (const unsigned int vec_num);
00513
00518 const std::string & vector_name (const unsigned int vec_num);
00519
00525 NumericVector<Number> & add_adjoint_solution(unsigned int i=0);
00526
00531 NumericVector<Number> & get_adjoint_solution(unsigned int i=0);
00532
00537 const NumericVector<Number> & get_adjoint_solution(unsigned int i=0) const;
00538
00544 NumericVector<Number> & add_sensitivity_solution(unsigned int i=0);
00545
00550 NumericVector<Number> & get_sensitivity_solution(unsigned int i=0);
00551
00556 const NumericVector<Number> & get_sensitivity_solution(unsigned int i=0) const;
00557
00564 NumericVector<Number> & add_weighted_sensitivity_adjoint_solution(unsigned int i=0);
00565
00571 NumericVector<Number> & get_weighted_sensitivity_adjoint_solution(unsigned int i=0);
00572
00578 const NumericVector<Number> & get_weighted_sensitivity_adjoint_solution(unsigned int i=0) const;
00579
00585 NumericVector<Number> & add_weighted_sensitivity_solution();
00586
00591 NumericVector<Number> & get_weighted_sensitivity_solution();
00592
00597 const NumericVector<Number> & get_weighted_sensitivity_solution() const;
00598
00604 NumericVector<Number> & add_adjoint_rhs(unsigned int i=0);
00605
00612 NumericVector<Number> & get_adjoint_rhs(unsigned int i=0);
00613
00618 const NumericVector<Number> & get_adjoint_rhs(unsigned int i=0) const;
00619
00625 NumericVector<Number> & add_sensitivity_rhs(unsigned int i=0);
00626
00636 NumericVector<Number> & get_sensitivity_rhs(unsigned int i=0);
00637
00642 const NumericVector<Number> & get_sensitivity_rhs(unsigned int i=0) const;
00643
00649 unsigned int n_vectors () const;
00650
00654 unsigned int n_vars() const;
00655
00659 unsigned int n_dofs() const;
00660
00665 unsigned int n_active_dofs() const;
00666
00671 unsigned int n_constrained_dofs() const;
00672
00677 unsigned int n_local_dofs() const;
00678
00687 class Variable
00688 {
00689 public:
00690
00696 Variable (const std::string &var_name,
00697 const unsigned int var_number,
00698 const FEType &var_type) :
00699 _name(var_name),
00700 _number(var_number),
00701 _type(var_type)
00702 {}
00703
00708 Variable (const std::string &var_name,
00709 const unsigned int var_number,
00710 const FEType &var_type,
00711 const std::set<subdomain_id_type> &var_active_subdomains) :
00712 _name(var_name),
00713 _number(var_number),
00714 _type(var_type),
00715 _active_subdomains(var_active_subdomains)
00716 {}
00717
00721 const std::string & name() const
00722 { return _name; }
00723
00727 unsigned int number() const
00728 { return _number; }
00729
00733 const FEType & type() const
00734 { return _type; }
00735
00742 bool active_on_subdomain (const subdomain_id_type sid) const
00743 { return (_active_subdomains.empty() || _active_subdomains.count(sid)); }
00744
00750 bool implicitly_active () const
00751 { return _active_subdomains.empty(); }
00752
00753 private:
00754 std::string _name;
00755 unsigned int _number;
00756 FEType _type;
00757 std::set<subdomain_id_type> _active_subdomains;
00758 };
00759
00764 unsigned int add_variable (const std::string& var,
00765 const FEType& type,
00766 const std::set<subdomain_id_type> * const active_subdomains = NULL);
00767
00773 unsigned int add_variable (const std::string& var,
00774 const Order order = FIRST,
00775 const FEFamily = LAGRANGE,
00776 const std::set<subdomain_id_type> * const active_subdomains = NULL);
00777
00781 const Variable & variable (unsigned int var) const;
00782
00786 bool has_variable(const std::string& var) const;
00787
00791 const std::string & variable_name(const unsigned int i) const;
00792
00797 unsigned short int variable_number (const std::string& var) const;
00798
00802 const FEType & variable_type (const unsigned int i) const;
00803
00807 const FEType & variable_type (const std::string& var) const;
00808
00813 Real calculate_norm(NumericVector<Number>& v,
00814 unsigned int var = 0,
00815 FEMNormType norm_type = L2) const;
00816
00821 Real calculate_norm(NumericVector<Number>& v,
00822 const SystemNorm &norm) const;
00823
00827 void read_header (Xdr& io,
00828 const std::string &version,
00829 const bool read_header=true,
00830 const bool read_additional_data=true,
00831 const bool read_legacy_format=false);
00832
00836 void read_legacy_data (Xdr& io,
00837 const bool read_additional_data=true);
00838
00843 void read_serialized_data (Xdr& io,
00844 const bool read_additional_data=true);
00845
00852 void read_parallel_data (Xdr &io,
00853 const bool read_additional_data);
00857 void write_header (Xdr& io,
00858 const std::string &version,
00859 const bool write_additional_data) const;
00860
00865 void write_serialized_data (Xdr& io,
00866 const bool write_additional_data = true) const;
00867
00874 void write_parallel_data (Xdr &io,
00875 const bool write_additional_data) const;
00876
00881 std::string get_info () const;
00882
00886 void attach_init_function (void fptr(EquationSystems& es,
00887 const std::string& name));
00888
00893 void attach_assemble_function (void fptr(EquationSystems& es,
00894 const std::string& name));
00895
00899 void attach_constraint_function (void fptr(EquationSystems& es,
00900 const std::string& name));
00901
00906 void attach_QOI_function (void fptr(EquationSystems& es,
00907 const std::string& name,
00908 const QoISet& qoi_indices));
00909
00915 void attach_QOI_derivative (void fptr(EquationSystems& es,
00916 const std::string& name,
00917 const QoISet& qoi_indices));
00918
00923 virtual void user_initialization ();
00924
00929 virtual void user_assembly ();
00930
00935 virtual void user_constrain ();
00936
00941 virtual void user_QOI (const QoISet& qoi_indices);
00942
00947 virtual void user_QOI_derivative (const QoISet& qoi_indices);
00948
00954 virtual void re_update ();
00955
00959 virtual void restrict_vectors ();
00960
00964 virtual void prolong_vectors ();
00965
00985 bool assemble_before_solve;
00986
00987
00988
00989
00990
00995 Number current_solution (const unsigned int global_dof_number) const;
00996
01000 AutoPtr<NumericVector<Number> > solution;
01001
01012 AutoPtr<NumericVector<Number> > current_local_solution;
01013
01020 std::vector<Number> qoi;
01021
01026 void local_dof_indices (const unsigned int var,
01027 std::set<unsigned int> & var_indices) const;
01028
01033 void zero_variable (NumericVector<Number>& v, unsigned int var_num) const;
01034
01035 protected:
01036
01042 virtual void init_data ();
01043
01048 void project_vector (NumericVector<Number>&) const;
01049
01055 void project_vector (const NumericVector<Number>&,
01056 NumericVector<Number>&) const;
01057
01058 private:
01059
01064 Real discrete_var_norm(NumericVector<Number>& v,
01065 unsigned int var,
01066 FEMNormType norm_type) const;
01067
01073 template <typename iterator_type>
01074 unsigned int read_serialized_blocked_dof_objects (const unsigned int var,
01075 const unsigned int n_objects,
01076 const iterator_type begin,
01077 const iterator_type end,
01078 Xdr &io,
01079 NumericVector<Number> &vec) const;
01080
01085 void read_serialized_vector (Xdr& io,
01086 NumericVector<Number> &vec);
01087
01092 template <typename iterator_type>
01093 unsigned int write_serialized_blocked_dof_objects (const NumericVector<Number> &vec,
01094 const unsigned int var,
01095 const unsigned int n_objects,
01096 const iterator_type begin,
01097 const iterator_type end,
01098 Xdr &io) const;
01099
01104 void write_serialized_vector (Xdr& io,
01105 const NumericVector<Number> &vec) const;
01106
01110 void (* _init_system) (EquationSystems& es,
01111 const std::string& name);
01112
01116 void (* _assemble_system) (EquationSystems& es,
01117 const std::string& name);
01118
01122 void (* _constrain_system) (EquationSystems& es,
01123 const std::string& name);
01124
01128 void (* _qoi_evaluate) (EquationSystems& es,
01129 const std::string& name,
01130 const QoISet& qoi_indices);
01131
01135 void (* _qoi_evaluate_derivative) (EquationSystems& es,
01136 const std::string& name,
01137 const QoISet& qoi_indices);
01138
01143 AutoPtr<DofMap> _dof_map;
01144
01149 EquationSystems& _equation_systems;
01150
01155 MeshBase& _mesh;
01156
01160 const std::string _sys_name;
01161
01165 const unsigned int _sys_number;
01166
01170 std::vector<System::Variable> _variables;
01171
01176 std::map<std::string, unsigned short int> _variable_numbers;
01177
01181 bool _active;
01182
01189 std::map<std::string, NumericVector<Number>* > _vectors;
01190
01195 std::map<std::string, bool> _vector_projections;
01196
01202 bool _solution_projection;
01203
01207 bool _can_add_vectors;
01208
01214 bool _additional_data_written;
01215
01216
01222 class ProjectVector
01223 {
01224 private:
01225 const System &system;
01226 const NumericVector<Number> &old_vector;
01227 NumericVector<Number> &new_vector;
01228
01229 public:
01230 ProjectVector (const System &system_in,
01231 const NumericVector<Number> &old_v_in,
01232 NumericVector<Number> &new_v_in) :
01233 system(system_in),
01234 old_vector(old_v_in),
01235 new_vector(new_v_in)
01236 {}
01237
01238 void operator()(const ConstElemRange &range) const;
01239 };
01240
01241
01251 class BuildProjectionList
01252 {
01253 private:
01254 const System &system;
01255
01256 public:
01257 BuildProjectionList (const System &system_in) :
01258 system(system_in)
01259 {}
01260
01261 BuildProjectionList (BuildProjectionList &other, Threads::split) :
01262 system(other.system)
01263 {}
01264
01265 void unique();
01266 void operator()(const ConstElemRange &range);
01267 void join (const BuildProjectionList &other);
01268 std::vector<unsigned int> send_list;
01269 };
01270
01271
01272
01278 class ProjectSolution
01279 {
01280 private:
01281 const System &system;
01282
01283 Number (* fptr)(const Point& p,
01284 const Parameters& parameters,
01285 const std::string& sys_name,
01286 const std::string& unknown_name);
01287
01288 Gradient (* gptr)(const Point& p,
01289 const Parameters& parameters,
01290 const std::string& sys_name,
01291 const std::string& unknown_name);
01292
01293 Parameters ¶meters;
01294 NumericVector<Number> &new_vector;
01295
01296 public:
01297 ProjectSolution (const System &system_in,
01298 Number fptr_in(const Point& p,
01299 const Parameters& parameters,
01300 const std::string& sys_name,
01301 const std::string& unknown_name),
01302 Gradient gptr_in(const Point& p,
01303 const Parameters& parameters,
01304 const std::string& sys_name,
01305 const std::string& unknown_name),
01306 Parameters ¶meters_in,
01307 NumericVector<Number> &new_v_in) :
01308 system(system_in),
01309 fptr(fptr_in),
01310 gptr(gptr_in),
01311 parameters(parameters_in),
01312 new_vector(new_v_in)
01313 {}
01314
01315 void operator()(const ConstElemRange &range) const;
01316 };
01317 };
01318
01319
01320
01321
01322
01323 inline
01324 const std::string & System::name() const
01325 {
01326 return _sys_name;
01327 }
01328
01329
01330
01331 inline
01332 unsigned int System::number() const
01333 {
01334 return _sys_number;
01335 }
01336
01337
01338
01339 inline
01340 const MeshBase & System::get_mesh() const
01341 {
01342 return _mesh;
01343 }
01344
01345
01346
01347 inline
01348 MeshBase & System::get_mesh()
01349 {
01350 return _mesh;
01351 }
01352
01353
01354
01355 inline
01356 const DofMap & System::get_dof_map() const
01357 {
01358 return *_dof_map;
01359 }
01360
01361
01362
01363 inline
01364 DofMap & System::get_dof_map()
01365 {
01366 return *_dof_map;
01367 }
01368
01369
01370
01371 inline
01372 bool System::active() const
01373 {
01374 return _active;
01375 }
01376
01377
01378
01379 inline
01380 void System::activate ()
01381 {
01382 _active = true;
01383 }
01384
01385
01386
01387 inline
01388 void System::deactivate ()
01389 {
01390 _active = false;
01391 }
01392
01393
01394
01395 inline
01396 unsigned int System::n_vars() const
01397 {
01398 return _variables.size();
01399 }
01400
01401
01402
01403 inline
01404 const System::Variable & System::variable (const unsigned int i) const
01405 {
01406 libmesh_assert (i < _variables.size());
01407
01408 return _variables[i];
01409 }
01410
01411
01412
01413 inline
01414 const std::string & System::variable_name (const unsigned int i) const
01415 {
01416 libmesh_assert (i < _variables.size());
01417
01418 return _variables[i].name();
01419 }
01420
01421
01422
01423 inline
01424 const FEType & System::variable_type (const unsigned int i) const
01425 {
01426 libmesh_assert (i < _variables.size());
01427
01428 return _variables[i].type();
01429 }
01430
01431
01432
01433 inline
01434 const FEType & System::variable_type (const std::string& var) const
01435 {
01436 return _variables[this->variable_number(var)].type();
01437 }
01438
01439
01440
01441 inline
01442 unsigned int System::n_active_dofs() const
01443 {
01444 return this->n_dofs() - this->n_constrained_dofs();
01445 }
01446
01447
01448
01449 inline
01450 bool System::have_vector (const std::string& vec_name) const
01451 {
01452 return (_vectors.count(vec_name));
01453 }
01454
01455
01456
01457 inline
01458 unsigned int System::n_vectors () const
01459 {
01460 return _vectors.size();
01461 }
01462
01463 inline
01464 System::vectors_iterator System::vectors_begin ()
01465 {
01466 return _vectors.begin();
01467 }
01468
01469 inline
01470 System::const_vectors_iterator System::vectors_begin () const
01471 {
01472 return _vectors.begin();
01473 }
01474
01475 inline
01476 System::vectors_iterator System::vectors_end ()
01477 {
01478 return _vectors.end();
01479 }
01480
01481 inline
01482 System::const_vectors_iterator System::vectors_end () const
01483 {
01484 return _vectors.end();
01485 }
01486
01487 inline
01488 void System::assemble_residual_derivatives (const ParameterVector&)
01489 {
01490 libmesh_not_implemented();
01491 }
01492
01493 inline
01494 std::pair<unsigned int, Real>
01495 System::sensitivity_solve (const ParameterVector&)
01496 {
01497 libmesh_not_implemented();
01498 }
01499
01500 inline
01501 std::pair<unsigned int, Real>
01502 System::weighted_sensitivity_solve (const ParameterVector&,
01503 const ParameterVector&)
01504 {
01505 libmesh_not_implemented();
01506 }
01507
01508 inline
01509 std::pair<unsigned int, Real>
01510 System::adjoint_solve (const QoISet&)
01511 {
01512 libmesh_not_implemented();
01513 }
01514
01515 inline
01516 std::pair<unsigned int, Real>
01517 System::weighted_sensitivity_adjoint_solve (const ParameterVector&,
01518 const ParameterVector&,
01519 const QoISet&)
01520 {
01521 libmesh_not_implemented();
01522 }
01523
01524 inline
01525 void
01526 System::adjoint_qoi_parameter_sensitivity (const QoISet&,
01527 const ParameterVector&,
01528 SensitivityData&)
01529 {
01530 libmesh_not_implemented();
01531 }
01532
01533 inline
01534 void
01535 System::forward_qoi_parameter_sensitivity (const QoISet&,
01536 const ParameterVector&,
01537 SensitivityData&)
01538 {
01539 libmesh_not_implemented();
01540 }
01541
01542 inline
01543 void
01544 System::qoi_parameter_hessian(const QoISet&,
01545 const ParameterVector&,
01546 SensitivityData&)
01547 {
01548 libmesh_not_implemented();
01549 }
01550
01551 inline
01552 void
01553 System::qoi_parameter_hessian_vector_product(const QoISet&,
01554 const ParameterVector&,
01555 const ParameterVector&,
01556 SensitivityData&)
01557 {
01558 libmesh_not_implemented();
01559 }
01560
01561 #endif // #define __system_h__