FEI Version of the Day
test_EqnBuffer.cpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 #include <fei_macros.hpp>
00044 
00045 #include <test_utils/test_EqnBuffer.hpp>
00046 #include <cmath>
00047 #include <fei_CSVec.hpp>
00048 #include <fei_EqnBuffer.hpp>
00049 
00050 #undef fei_file
00051 #define fei_file "test_EqnBuffer.cpp"
00052 #include <fei_ErrMacros.hpp>
00053 
00054 test_EqnBuffer::test_EqnBuffer(MPI_Comm comm)
00055  : tester(comm)
00056 {
00057 }
00058 
00059 test_EqnBuffer::~test_EqnBuffer()
00060 {
00061 }
00062 
00063 int test_EqnBuffer::runtests()
00064 {
00065   CHK_ERR( test1() );
00066   CHK_ERR( test2() );
00067   CHK_ERR( test3() );
00068   CHK_ERR( test4() );
00069   return(0);
00070 }
00071 
00072 int test_EqnBuffer::test1()
00073 {
00074   FEI_COUT << "testing EqnBuffer...";
00075 
00076   EqnBuffer eqns;
00077 
00078   std::vector<double> eqnCoefs(3);
00079   std::vector<int> colIndices(3);
00080 
00081   eqnCoefs[0] = 0.5;  colIndices[0] = 2;
00082   eqnCoefs[1] = 0.5;  colIndices[1] = 4;
00083   eqnCoefs[2] = 0.5;   colIndices[2] = 6;
00084 
00085   CHK_ERR( eqns.addIndices(1, &colIndices[0], eqnCoefs.size()) );
00086 
00087   CHK_ERR( eqns.addEqn(1, &eqnCoefs[0], &colIndices[0],
00088            eqnCoefs.size(), false) );
00089 
00090   eqnCoefs[0] = 0.5;  colIndices[0] = 1;
00091   eqnCoefs[1] = 0.5;  colIndices[1] = 3;
00092   eqnCoefs[2] = 0.5;   colIndices[2] = 5;
00093 
00094   CHK_ERR( eqns.addEqn(7, &eqnCoefs[0], &colIndices[0],
00095            eqnCoefs.size(), true) );
00096 
00097   eqnCoefs[0] = 0.25;  colIndices[0] = 2;
00098   eqnCoefs[1] = 0.25;  colIndices[1] = 3;
00099   eqnCoefs[2] = 0.5;   colIndices[2] = 6;
00100 
00101   CHK_ERR( eqns.addEqn(8, &eqnCoefs[0], &colIndices[0],
00102            eqnCoefs.size(), false) );
00103 
00104   eqns.setNumRHSs(1);
00105 
00106   double coef = 0.0;
00107   CHK_ERR( eqns.getCoef(7, 3, coef) );
00108   if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
00109 
00110   CHK_ERR( eqns.removeIndex(7,3) );
00111   int err = eqns.getCoef(7, 3, coef);
00112   if (err != -1) ERReturn(-1);
00113 
00114   CHK_ERR( eqns.getCoefAndRemoveIndex(1, 6, coef) );
00115   if (std::abs(coef - 0.5) > 1.e-49) ERReturn(-1);
00116 
00117   err = eqns.getCoef(1, 6, coef);
00118   if (err != -1) ERReturn(-1);
00119 
00120 
00121   std::vector<int>& eqnNumbers = eqns.eqnNumbers();
00122   std::vector<fei::CSVec*>& rows = eqns.eqns();
00123 
00124   EqnBuffer* eqnsCopy = eqns.deepCopy();
00125 
00126   std::vector<int>& eqnNumbersCopy = eqnsCopy->eqnNumbers();
00127 
00128   if (eqnNumbersCopy != eqnNumbers) {
00129     ERReturn(-1);
00130   }
00131 
00132   CHK_ERR( eqnsCopy->addRHS(1, 0, 1.0, true) );
00133   CHK_ERR( eqnsCopy->addEqns(eqns, true) );
00134 
00135   std::vector<double> tempCoefs;
00136   std::vector<int> tempIndices;
00137 
00138   int levelsOfCoupling = 0;
00139   bool finished = false;
00140   while(!finished) {
00141     bool foundCoupling = false;
00142     for(size_t i=0; i<eqnNumbers.size(); i++) {
00143       int rowIndex = eqns.isInIndices(eqnNumbers[i]);
00144 
00145       while(rowIndex >= 0) {
00146   foundCoupling = true;
00147   coef = 0.0;
00148   CHK_ERR( eqns.getCoefAndRemoveIndex( eqnNumbers[rowIndex], eqnNumbers[i],
00149                coef) );
00150 
00151   std::vector<int>& indicesRef = rows[i]->indices();
00152   std::vector<double>& coefsRef = rows[i]->coefs();
00153 
00154   int len = indicesRef.size();
00155   tempCoefs.resize(len);
00156   tempIndices.resize(len);
00157 
00158   double* tempCoefsPtr = &tempCoefs[0];
00159   int* tempIndicesPtr = &tempIndices[0];
00160   double* coefsPtr = &coefsRef[0];
00161   int* indicesPtr = &indicesRef[0];
00162 
00163   for(int j=0; j<len; ++j) {
00164     tempIndicesPtr[j] = indicesPtr[j];
00165     tempCoefsPtr[j] = coef*coefsPtr[j];
00166   }
00167 
00168   CHK_ERR( eqns.addEqn(eqnNumbers[rowIndex], tempCoefsPtr, tempIndicesPtr,
00169            len, true) );
00170 
00171   rowIndex = eqns.isInIndices(eqnNumbers[i]);
00172       }
00173     }
00174     if (foundCoupling) ++levelsOfCoupling;
00175     else finished = true;
00176   }
00177 
00178   if (levelsOfCoupling != 1) {
00179     return(-1);
00180   }
00181 
00182   eqns.resetCoefs();
00183 
00184   delete eqnsCopy;
00185 
00186   FEI_COUT << "ok"<<FEI_ENDL;
00187 
00188   return(0);
00189 }
00190 
00191 int test_EqnBuffer::test2()
00192 {
00193 
00194   return(0);
00195 }
00196 
00197 int test_EqnBuffer::test3()
00198 {
00199   return(0);
00200 }
00201 
00202 int test_EqnBuffer::test4()
00203 {
00204   return(0);
00205 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends