Memosa-FVM  0.2
KSConnectivity.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _KSCONNECTIVITY_H_
6 #define _KSCONNECTIVITY_H_
7 
8 #include "Array.h"
9 
10 template<class T>
12 {
13  public:
14 
16  typedef Array<T> TArray;
17  typedef pair<IntArray*,TArray*> CouplingPair;
18  typedef vector<CouplingPair*> SelfToOther;
19  typedef vector<CouplingPair*> SelfToSelf;
20 
21  KSConnectivity(const int selfLength, const int otherLength):
22  _selfSite(selfLength),
23  _otherSite(otherLength),
28  _selfNNZ(0),
29  _otherNNZ(0)
30  {}
31 
33  {
36  }
37 
40  void addCountSelf(const int index, const int count)
41  {
42  _SelfToSelfConn.addCount(index, count);
43  _selfNNZ+=count;
44  }
45  void addCountOther(const int index, const int count)
46  {
47  _SelfToOtherConn.addCount(index, count);
48  _otherNNZ+=count;
49  }
51  {
55  }
57  {
61  }
62  void addSelf(const int i, const int j, const T val)
64  void addOther(const int i, const int j, const T val)
68  int getSelfCount(const int i) {return _SelfToSelfConn.getCount(i);}
69  int getOtherCount(const int i) {return _SelfToOtherConn.getCount(i);}
77  int getSelfNNZ() {return _selfNNZ;}
78  int getOtherNNZ() {return _otherNNZ;}
79 
80  void multiplySelf(const TArray& x, TArray& b, const T scale) const
81  {//b=this*x
82  const int Arows=_selfSite.getSelfCount();
83  b.zero();
84  if(Arows==x.getLength() && Arows==b.getLength())
85  {
86  const IntArray& row=_SelfToSelfConn.getRow();
87  const IntArray& col=_SelfToSelfConn.getCol();
88  for(int i=0;i<Arows;i++)
89  {
90  for(int pos=row[i];pos<row[i+1];pos++)
91  b[i]+=(x[col[pos]]*_SelfToSelfCoeffs[pos])/scale;
92  b[i]*=scale;
93  }
94  }
95  else
96  throw CException("Matrix size does not agree with vectors!");
97  }
98 
99  void multiplyOther(const TArray& x, TArray& b, const T scale) const
100  {//b=this*x
101  const int Arows=_otherSite.getSelfCount();
102  b.zero();
103  if(Arows==x.getLength() && Arows==b.getLength())
104  {
105  for(int i=0;i<Arows;i++)
106  {
107  const IntArray& row=_SelfToOtherConn.getRow();
108  const IntArray& col=_SelfToOtherConn.getCol();
109  for(int pos=row[i];pos<row[i+1];pos++)
110  b[i]+=(x[col[pos]]*_SelfToOtherCoeffs[pos])/scale;
111  b[i]*=scale;
112  }
113  }
114  else
115  throw CException("Matrix size does not agree with vectors!");
116  }
117 
120 
122  {
123  //Copy SelfToSelf connections first
124 
125  initSelfCount();
126  int Arows=from.getSelfSize();
127  for(int i=0;i<Arows;i++)
128  addCountSelf(i,from.getSelfCount(i));
129 
130  finishCountSelf();
131 
132  for(int i=0;i<Arows;i++)
133  {
134  const IntArray& row=from.getSelfRow();
135  const IntArray& col=from.getSelfCol();
136  const TArray& coeff=from.getSelfCoeffs();
137  for(int pos=row[i];pos<row[i+1];pos++)
138  {
139  const T fromCoeff=coeff[pos];
140  const int j=col[pos];
141  addSelf(i,j,fromCoeff);
142  }
143  }
144 
145  finishAddSelf();
146 
147  //Copy SelfToOther connections now
148 
149  initOtherCount();
150  for(int i=0;i<Arows;i++)
151  addCountOther(i,from.getOtherCount(i));
152 
154 
155  for(int i=0;i<Arows;i++)
156  {
157  const IntArray& row=from.getOtherRow();
158  const IntArray& col=from.getOtherCol();
159  const TArray& coeff=from.getOtherCoeffs();
160  for(int pos=row[i];pos<row[i+1];pos++)
161  {
162  const T fromCoeff=coeff[pos];
163  const int j=col[pos];
164  addOther(i,j,fromCoeff);
165  }
166  }
167 
168  finishAddOther();
169 
170  }
171 
172  void multiplySelf(const T x)
173  {
174  for(int i=0;i<_SelfToSelfCoeffs.getLength();i++)
175  _SelfToSelfCoeffs[i]*=x;
176  }
177 
178  void multiplyOther(const T x)
179  {
180  for(int i=0;i<_SelfToOtherCoeffs.getLength();i++)
181  _SelfToOtherCoeffs[i]*=x;
182  }
183 
185  {
186  const int selfSize=getSelfSize();
187  if(selfSize==added.getSelfSize())
188  {
190  myCopy.copyFrom(*this);
191  //start the counting
192  initSelfCount();
193  for(int i=0;i<selfSize;i++)
194  {
195  TArray myExpand(1);
196  myCopy.expandMySelfSelf(i, myExpand);
197 
198  TArray addExpand(1);
199  added.expandMySelfSelf(i, addExpand);
200 
201  myExpand+=addExpand;
202 
203  int newSize(0);
204  for(int j=0;j<selfSize;j++)
205  {
206  if(fabs(myExpand[j])>0.)
207  newSize++;
208  }
209  addCountSelf(i,newSize);
210  }
211  finishCountSelf();
212 
213  //put in values
214  for(int i=0;i<selfSize;i++)
215  {
216  TArray myExpand(1);
217  myCopy.expandMySelfSelf(i, myExpand);
218 
219  TArray addExpand(1);
220  added.expandMySelfSelf(i, addExpand);
221 
222  myExpand+=addExpand;
223 
224  for(int j=0;j<selfSize;j++)
225  {
226  if(fabs(myExpand[j])>0.)
227  addSelf(i,j,myExpand[j]);
228  }
229  }
230  finishAddSelf();
231 
232  }
233  else
234  throw CException("addToSelf: Rows not the same size!");
235  }
236 
238  {
239  const int selfSize=getSelfSize();
240  if(selfSize==added.getSelfSize())
241  {
242  if(getOtherSize()==added.getOtherSize())
243  {
245  myCopy.copyFrom(*this);
246  //start the counting
247  initOtherCount();
248  for(int i=0;i<selfSize;i++)
249  {
250  TArray myExpand(1);
251  myCopy.expandMySelfOther(i, myExpand);
252 
253  TArray addExpand(1);
254  added.expandMySelfOther(i, addExpand);
255 
256  myExpand+=addExpand;
257 
258  int newSize(0);
259  for(int j=0;j<getOtherSize();j++)
260  {
261  if(fabs(myExpand[j])>0.)
262  newSize++;
263  }
264  addCountOther(i,newSize);
265  }
267 
268  //put in values
269  for(int i=0;i<selfSize;i++)
270  {
271  TArray myExpand(1);
272  myCopy.expandMySelfOther(i, myExpand);
273 
274  TArray addExpand(1);
275  added.expandMySelfOther(i, addExpand);
276 
277  myExpand+=addExpand;
278 
279  for(int j=0;j<getOtherSize();j++)
280  {
281  if(fabs(myExpand[j])>0.)
282  addOther(i,j,myExpand[j]);
283  }
284  }
285  finishAddOther();
286  }
287  else
288  throw CException("addToOther: Columns not the same size!");
289  }
290  else
291  throw CException("addToOther: Rows not the same size!");
292  }
293 
294  void expandMySelfSelf(const int i, TArray& ExpCoeff)
295  {
296  const int mysize=getSelfSize();
297  ExpCoeff.resize(mysize);
298  ExpCoeff.zero();
299  const IntArray& row=_SelfToSelfConn.getRow();
300  const IntArray& col=_SelfToSelfConn.getCol();
301 
302  for(int pos=row[i];pos<row[i+1];pos++)
303  {
304  const int j=col[pos];
305  ExpCoeff[j]=_SelfToSelfCoeffs[pos];
306  }
307 
308  }
309 
310  void expandMySelfOther(const int i, TArray& ExpCoeff)
311  {
312  const int mysize=getOtherSize();
313  ExpCoeff.resize(mysize);
314  ExpCoeff.zero();
315  const IntArray& row=_SelfToOtherConn.getRow();
316  const IntArray& col=_SelfToOtherConn.getCol();
317 
318  for(int pos=row[i];pos<row[i+1];pos++)
319  {
320  const int j=col[pos];
321  ExpCoeff[j]=_SelfToOtherCoeffs[pos];
322  }
323 
324  }
325 
326  private:
327 
335  int _selfNNZ;
337 
338 };
339 
340 #endif
const Array< int > & getCol() const
int getCount(const int i) const
void multiplySelf(const T x)
virtual void zero()
Definition: Array.h:281
const Array< int > & getRow() const
int getSelfCount() const
Definition: StorageSite.h:40
void addCountOther(const int index, const int count)
vector< CouplingPair * > SelfToOther
void multiplyOther(const T x)
const TArray & getSelfCoeffs()
void addSelf(const int i, const int j, const T val)
StorageSite _otherSite
TArray _SelfToOtherCoeffs
CRConnectivity _SelfToOtherConn
Array< int > IntArray
KSConnectivity(const int selfLength, const int otherLength)
vector< CouplingPair * > SelfToSelf
void finishCountSelf()
const IntArray & getOtherRow()
void emptyConnections()
pair< IntArray *, TArray * > CouplingPair
CRConnectivity _SelfToSelfConn
TArray _SelfToSelfCoeffs
void initOtherCount()
void addCountSelf(const int index, const int count)
void initSelfCount()
void copyFrom(KSConnectivity &from)
const IntArray & getOtherCol()
void multiplyOther(const TArray &x, TArray &b, const T scale) const
void resize(const int newLength)
Definition: Array.h:56
const IntArray & getSelfCol()
int getOtherCount(const int i)
void addToOther(KSConnectivity &added)
void finishAddOther()
Tangent fabs(const Tangent &a)
Definition: Tangent.h:312
StorageSite _selfSite
int add(const int index, const int val)
void finishAddSelf()
void finishCountOther()
int getSelfCount(const int i)
void multiplySelf(const TArray &x, TArray &b, const T scale) const
TArray & getNonConstOtherCoeffs()
void addOther(const int i, const int j, const T val)
void expandMySelfSelf(const int i, TArray &ExpCoeff)
void addCount(const int index, const int count)
const TArray & getOtherCoeffs()
void addToSelf(KSConnectivity &added)
const IntArray & getSelfRow()
Array< T > TArray
void expandMySelfOther(const int i, TArray &ExpCoeff)
int getLength() const
Definition: Array.h:87