FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
test_EqnBuffer.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include <fei_macros.hpp>
10 
11 #include <test_utils/test_EqnBuffer.hpp>
12 #include <cmath>
13 #include <fei_CSVec.hpp>
14 #include <fei_EqnBuffer.hpp>
15 
16 #undef fei_file
17 #define fei_file "test_EqnBuffer.cpp"
18 #include <fei_ErrMacros.hpp>
19 
20 test_EqnBuffer::test_EqnBuffer(MPI_Comm comm)
21  : tester(comm)
22 {
23 }
24 
25 test_EqnBuffer::~test_EqnBuffer()
26 {
27 }
28 
29 int test_EqnBuffer::runtests()
30 {
31  CHK_ERR( test1() );
32  CHK_ERR( test2() );
33  CHK_ERR( test3() );
34  CHK_ERR( test4() );
35  return(0);
36 }
37 
38 int test_EqnBuffer::test1()
39 {
40  FEI_COUT << "testing EqnBuffer...";
41 
42  EqnBuffer eqns;
43 
44  std::vector<double> eqnCoefs(3);
45  std::vector<int> colIndices(3);
46 
47  eqnCoefs[0] = 0.5; colIndices[0] = 2;
48  eqnCoefs[1] = 0.5; colIndices[1] = 4;
49  eqnCoefs[2] = 0.5; colIndices[2] = 6;
50 
51  CHK_ERR( eqns.addIndices(1, &colIndices[0], eqnCoefs.size()) );
52 
53  CHK_ERR( eqns.addEqn(1, &eqnCoefs[0], &colIndices[0],
54  eqnCoefs.size(), false) );
55 
56  eqnCoefs[0] = 0.5; colIndices[0] = 1;
57  eqnCoefs[1] = 0.5; colIndices[1] = 3;
58  eqnCoefs[2] = 0.5; colIndices[2] = 5;
59 
60  CHK_ERR( eqns.addEqn(7, &eqnCoefs[0], &colIndices[0],
61  eqnCoefs.size(), true) );
62 
63  eqnCoefs[0] = 0.25; colIndices[0] = 2;
64  eqnCoefs[1] = 0.25; colIndices[1] = 3;
65  eqnCoefs[2] = 0.5; colIndices[2] = 6;
66 
67  CHK_ERR( eqns.addEqn(8, &eqnCoefs[0], &colIndices[0],
68  eqnCoefs.size(), false) );
69 
70  eqns.setNumRHSs(1);
71 
72  double coef = 0.0;
73  CHK_ERR( eqns.getCoef(7, 3, coef) );
74  if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
75 
76  CHK_ERR( eqns.removeIndex(7,3) );
77  int err = eqns.getCoef(7, 3, coef);
78  if (err != -1) ERReturn(-1);
79 
80  CHK_ERR( eqns.getCoefAndRemoveIndex(1, 6, coef) );
81  if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
82 
83  err = eqns.getCoef(1, 6, coef);
84  if (err != -1) ERReturn(-1);
85 
86 
87  std::vector<int>& eqnNumbers = eqns.eqnNumbers();
88  std::vector<fei::CSVec*>& rows = eqns.eqns();
89 
90  EqnBuffer* eqnsCopy = eqns.deepCopy();
91 
92  std::vector<int>& eqnNumbersCopy = eqnsCopy->eqnNumbers();
93 
94  if (eqnNumbersCopy != eqnNumbers) {
95  ERReturn(-1);
96  }
97 
98  CHK_ERR( eqnsCopy->addRHS(1, 0, 1.0, true) );
99  CHK_ERR( eqnsCopy->addEqns(eqns, true) );
100 
101  std::vector<double> tempCoefs;
102  std::vector<int> tempIndices;
103 
104  int levelsOfCoupling = 0;
105  bool finished = false;
106  while(!finished) {
107  bool foundCoupling = false;
108  for(size_t i=0; i<eqnNumbers.size(); i++) {
109  int rowIndex = eqns.isInIndices(eqnNumbers[i]);
110 
111  while(rowIndex >= 0) {
112  foundCoupling = true;
113  coef = 0.0;
114  CHK_ERR( eqns.getCoefAndRemoveIndex( eqnNumbers[rowIndex], eqnNumbers[i],
115  coef) );
116 
117  std::vector<int>& indicesRef = rows[i]->indices();
118  std::vector<double>& coefsRef = rows[i]->coefs();
119 
120  int len = indicesRef.size();
121  tempCoefs.resize(len);
122  tempIndices.resize(len);
123 
124  double* tempCoefsPtr = &tempCoefs[0];
125  int* tempIndicesPtr = &tempIndices[0];
126  double* coefsPtr = &coefsRef[0];
127  int* indicesPtr = &indicesRef[0];
128 
129  for(int j=0; j<len; ++j) {
130  tempIndicesPtr[j] = indicesPtr[j];
131  tempCoefsPtr[j] = coef*coefsPtr[j];
132  }
133 
134  CHK_ERR( eqns.addEqn(eqnNumbers[rowIndex], tempCoefsPtr, tempIndicesPtr,
135  len, true) );
136 
137  rowIndex = eqns.isInIndices(eqnNumbers[i]);
138  }
139  }
140  if (foundCoupling) ++levelsOfCoupling;
141  else finished = true;
142  }
143 
144  if (levelsOfCoupling != 1) {
145  return(-1);
146  }
147 
148  eqns.resetCoefs();
149 
150  delete eqnsCopy;
151 
152  FEI_COUT << "ok"<<FEI_ENDL;
153 
154  return(0);
155 }
156 
157 int test_EqnBuffer::test2()
158 {
159 
160  return(0);
161 }
162 
163 int test_EqnBuffer::test3()
164 {
165  return(0);
166 }
167 
168 int test_EqnBuffer::test4()
169 {
170  return(0);
171 }
int isInIndices(int eqn)
int addEqn(int eqnNumber, const double *coefs, const int *indices, int len, bool accumulate, bool create_indices_union=false)
std::vector< fei::CSVec * > & eqns()
void setNumRHSs(int n)
std::vector< int > & eqnNumbers()
int addRHS(int eqnNumber, int rhsIndex, double value, bool accumulate=true)
EqnBuffer * deepCopy()
int getCoefAndRemoveIndex(int eqnNumber, int colIndex, double &coef)
int addEqns(EqnBuffer &inputEqns, bool accumulate)
int addIndices(int eqnNumber, const int *indices, int len)
int removeIndex(int eqnNumber, int colIndex)
int getCoef(int eqnNumber, int colIndex, double &coef)
void resetCoefs()