FEI Version of the Day
test_EqnBuffer.cpp
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include <fei_macros.hpp>
00010 
00011 #include <test_utils/test_EqnBuffer.hpp>
00012 #include <cmath>
00013 #include <fei_CSVec.hpp>
00014 #include <fei_EqnBuffer.hpp>
00015 
00016 #undef fei_file
00017 #define fei_file "test_EqnBuffer.cpp"
00018 #include <fei_ErrMacros.hpp>
00019 
00020 test_EqnBuffer::test_EqnBuffer(MPI_Comm comm)
00021  : tester(comm)
00022 {
00023 }
00024 
00025 test_EqnBuffer::~test_EqnBuffer()
00026 {
00027 }
00028 
00029 int test_EqnBuffer::runtests()
00030 {
00031   CHK_ERR( test1() );
00032   CHK_ERR( test2() );
00033   CHK_ERR( test3() );
00034   CHK_ERR( test4() );
00035   return(0);
00036 }
00037 
00038 int test_EqnBuffer::test1()
00039 {
00040   FEI_COUT << "testing EqnBuffer...";
00041 
00042   EqnBuffer eqns;
00043 
00044   std::vector<double> eqnCoefs(3);
00045   std::vector<int> colIndices(3);
00046 
00047   eqnCoefs[0] = 0.5;  colIndices[0] = 2;
00048   eqnCoefs[1] = 0.5;  colIndices[1] = 4;
00049   eqnCoefs[2] = 0.5;   colIndices[2] = 6;
00050 
00051   CHK_ERR( eqns.addIndices(1, &colIndices[0], eqnCoefs.size()) );
00052 
00053   CHK_ERR( eqns.addEqn(1, &eqnCoefs[0], &colIndices[0],
00054            eqnCoefs.size(), false) );
00055 
00056   eqnCoefs[0] = 0.5;  colIndices[0] = 1;
00057   eqnCoefs[1] = 0.5;  colIndices[1] = 3;
00058   eqnCoefs[2] = 0.5;   colIndices[2] = 5;
00059 
00060   CHK_ERR( eqns.addEqn(7, &eqnCoefs[0], &colIndices[0],
00061            eqnCoefs.size(), true) );
00062 
00063   eqnCoefs[0] = 0.25;  colIndices[0] = 2;
00064   eqnCoefs[1] = 0.25;  colIndices[1] = 3;
00065   eqnCoefs[2] = 0.5;   colIndices[2] = 6;
00066 
00067   CHK_ERR( eqns.addEqn(8, &eqnCoefs[0], &colIndices[0],
00068            eqnCoefs.size(), false) );
00069 
00070   eqns.setNumRHSs(1);
00071 
00072   double coef = 0.0;
00073   CHK_ERR( eqns.getCoef(7, 3, coef) );
00074   if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
00075 
00076   CHK_ERR( eqns.removeIndex(7,3) );
00077   int err = eqns.getCoef(7, 3, coef);
00078   if (err != -1) ERReturn(-1);
00079 
00080   CHK_ERR( eqns.getCoefAndRemoveIndex(1, 6, coef) );
00081   if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
00082 
00083   err = eqns.getCoef(1, 6, coef);
00084   if (err != -1) ERReturn(-1);
00085 
00086 
00087   std::vector<int>& eqnNumbers = eqns.eqnNumbers();
00088   std::vector<fei::CSVec*>& rows = eqns.eqns();
00089 
00090   EqnBuffer* eqnsCopy = eqns.deepCopy();
00091 
00092   std::vector<int>& eqnNumbersCopy = eqnsCopy->eqnNumbers();
00093 
00094   if (eqnNumbersCopy != eqnNumbers) {
00095     ERReturn(-1);
00096   }
00097 
00098   CHK_ERR( eqnsCopy->addRHS(1, 0, 1.0, true) );
00099   CHK_ERR( eqnsCopy->addEqns(eqns, true) );
00100 
00101   std::vector<double> tempCoefs;
00102   std::vector<int> tempIndices;
00103 
00104   int levelsOfCoupling = 0;
00105   bool finished = false;
00106   while(!finished) {
00107     bool foundCoupling = false;
00108     for(size_t i=0; i<eqnNumbers.size(); i++) {
00109       int rowIndex = eqns.isInIndices(eqnNumbers[i]);
00110 
00111       while(rowIndex >= 0) {
00112   foundCoupling = true;
00113   coef = 0.0;
00114   CHK_ERR( eqns.getCoefAndRemoveIndex( eqnNumbers[rowIndex], eqnNumbers[i],
00115                coef) );
00116 
00117   std::vector<int>& indicesRef = rows[i]->indices();
00118   std::vector<double>& coefsRef = rows[i]->coefs();
00119 
00120   int len = indicesRef.size();
00121   tempCoefs.resize(len);
00122   tempIndices.resize(len);
00123 
00124   double* tempCoefsPtr = &tempCoefs[0];
00125   int* tempIndicesPtr = &tempIndices[0];
00126   double* coefsPtr = &coefsRef[0];
00127   int* indicesPtr = &indicesRef[0];
00128 
00129   for(int j=0; j<len; ++j) {
00130     tempIndicesPtr[j] = indicesPtr[j];
00131     tempCoefsPtr[j] = coef*coefsPtr[j];
00132   }
00133 
00134   CHK_ERR( eqns.addEqn(eqnNumbers[rowIndex], tempCoefsPtr, tempIndicesPtr,
00135            len, true) );
00136 
00137   rowIndex = eqns.isInIndices(eqnNumbers[i]);
00138       }
00139     }
00140     if (foundCoupling) ++levelsOfCoupling;
00141     else finished = true;
00142   }
00143 
00144   if (levelsOfCoupling != 1) {
00145     return(-1);
00146   }
00147 
00148   eqns.resetCoefs();
00149 
00150   delete eqnsCopy;
00151 
00152   FEI_COUT << "ok"<<FEI_ENDL;
00153 
00154   return(0);
00155 }
00156 
00157 int test_EqnBuffer::test2()
00158 {
00159 
00160   return(0);
00161 }
00162 
00163 int test_EqnBuffer::test3()
00164 {
00165   return(0);
00166 }
00167 
00168 int test_EqnBuffer::test4()
00169 {
00170   return(0);
00171 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends