OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
constrained_matrix.h
Go to the documentation of this file.
1 //---------------------------------------------------------------------------
2 // $Id: constrained_matrix.h 1348 2013-08-16 00:45:07Z secanell $
3 //
4 // Copyright (C) 2006, 2007, 2009 by Guido Kanschat
5 //
6 // This file is subject to QPL and may not be distributed
7 // without copyright and license information. Please refer
8 // to the file deal.II/doc/license.html for the text and
9 // further information on this license.
10 //
11 //---------------------------------------------------------------------------
12 
13 #ifndef __APPFRAME_CONSTRAINED_MATRIX_H
14 #define __APPFRAME_CONSTRAINED_MATRIX_H
15 
16 #include <boost/shared_ptr.hpp>
17 #include <lac/pointer_matrix.h>
18 #include <lac/constraint_matrix.h>
19 
20 
21 template <class VECTOR>
23  public Subscriptor
24 {
25  public:
26  template <class MATRIX>
27  void initialize(const MATRIX& matrix, ConstraintMatrix& constraints, bool reverse);
28  void clear();
29 
30  void vmult(VECTOR& dst, const VECTOR& src) const;
31  void vmult_add(VECTOR& dst, const VECTOR& src) const;
32  void Tvmult(VECTOR& dst, const VECTOR& src) const;
33  void Tvmult_add(VECTOR& dst, const VECTOR& src) const;
34 
35  boost::shared_ptr<PointerMatrixBase<VECTOR> > matrix;
36  SmartPointer<ConstraintMatrix> constraints;
37  bool reverse;
38 };
39 
40 
41 
42 template <class VECTOR>
43 template <class MATRIX>
44 void
45 ConstrainedMatrix<VECTOR>::initialize(const MATRIX& m, ConstraintMatrix& c, bool r)
46 {
47  matrix = boost::shared_ptr<PointerMatrixBase<VECTOR> > (new_pointer_matrix_base(m, VECTOR()));
48  constraints = &c;
49  reverse = r;
50 }
51 
52 
53 
54 template <class VECTOR>
55 void
57 {
58  matrix = 0;
59  constraints = 0;
60 }
61 
62 
63 
64 template <class VECTOR>
65 void
66 ConstrainedMatrix<VECTOR>::vmult(VECTOR& dst, const VECTOR& src) const
67 {
68  GrowingVectorMemory<VECTOR> mem;
69  VECTOR* tmp = mem.alloc();
70  tmp->reinit(src, true);
71  *tmp = src;
72 
73  if (reverse)
74  constraints->condense(*tmp);
75  else
76  constraints->distribute(*tmp);
77 
78  matrix->vmult(dst, *tmp);
79 
80  if (reverse)
81  constraints->distribute(dst);
82  else
83  constraints->condense(dst);
84 
85  mem.free(tmp);
86 }
87 
88 
89 
90 template <class VECTOR>
91 void
92 ConstrainedMatrix<VECTOR>::vmult_add(VECTOR& dst, const VECTOR& src) const
93 {
94  GrowingVectorMemory<VECTOR> mem;
95  VECTOR* tmp = mem.alloc();
96  VECTOR* tmp2 = mem.alloc();
97  tmp->reinit(src, true);
98  *tmp = src;
99 
100  if (reverse)
101  constraints->condense(*tmp);
102  else
103  constraints->distribute(*tmp);
104 
105  matrix->vmult(*tmp2, *tmp);
106 
107  if(reverse)
108  constraints->distribute(*tmp2);
109  else
110  constraints->condense(*tmp2);
111 
112  dst.add(1., *tmp2);
113  mem.free(tmp2);
114  mem.free(tmp);
115 }
116 
117 
118 
119 template <class VECTOR>
120 void
121 ConstrainedMatrix<VECTOR>::Tvmult(VECTOR& dst, const VECTOR& src) const
122 {
123  GrowingVectorMemory<VECTOR> mem;
124  VECTOR* tmp = mem.alloc();
125  tmp->reinit(src, true);
126  *tmp = src;
127 // constraints->distribute(*tmp);
128  matrix->Tvmult(dst, *tmp);
129  constraints->condense(dst);
130  mem.free(tmp);
131 }
132 
133 
134 
135 template <class VECTOR>
136 void
137 ConstrainedMatrix<VECTOR>::Tvmult_add(VECTOR& dst, const VECTOR& src) const
138 {
139  GrowingVectorMemory<VECTOR> mem;
140  VECTOR* tmp = mem.alloc();
141  VECTOR* tmp2 = mem.alloc();
142  tmp->reinit(src, true);
143  *tmp = src;
144 // constraints->distribute(*tmp);
145  matrix->Tvmult(*tmp2, *tmp);
146  constraints->condense(*tmp2);
147  dst.add(1., *tmp2);
148  mem.free(tmp2);
149  mem.free(tmp);
150 }
151 
152 #endif