FEI Version of the Day
test_AztecWrappers.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 <fei_mpi.h>
00046 
00047 #include <test_utils/test_AztecWrappers.hpp>
00048 
00049 #include <fei_ArrayUtils.hpp>
00050 #include <test_utils/LibraryFactory.hpp>
00051 
00052 #ifdef HAVE_FEI_AZTECOO
00053 #include <fei_Aztec_Map.hpp>
00054 #include <fei_Aztec_LSVector.hpp>
00055 #include <fei_AztecDMSR_Matrix.hpp>
00056 #endif
00057 
00058 #undef fei_file
00059 #define fei_file "test_AztecWrappers.cpp"
00060 
00061 #include <fei_ErrMacros.hpp>
00062 
00063 #ifdef HAVE_FEI_AZTECOO
00064 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00065         std::vector<std::vector<int> >& colIndices,
00066         std::vector<std::vector<double> >& values);
00067 
00068 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00069         std::vector<std::vector<int> >& colIndices,
00070         std::vector<std::vector<double> >& values, bool sumInto);
00071 #endif
00072 
00073 test_AztecWrappers::test_AztecWrappers(MPI_Comm comm)
00074  : tester(comm)
00075 {
00076 }
00077 
00078 test_AztecWrappers::~test_AztecWrappers()
00079 {
00080 }
00081 
00082 int test_AztecWrappers::runtests()
00083 {
00084   if (numProcs_ < 2) {
00085     CHK_ERR( serialtest1() );
00086   }
00087 
00088   CHK_ERR( test1() );
00089   CHK_ERR( test2() );
00090   CHK_ERR( test3() );
00091   CHK_ERR( test4() );
00092   return(0);
00093 }
00094 
00095 int test_AztecWrappers::serialtest1()
00096 {
00097   return(0);
00098 }
00099 
00100 int test_AztecWrappers::test1()
00101 {
00102 #ifdef HAVE_FEI_AZTECOO
00103   int localSize = 3, globalSize = localSize*numProcs_;
00104   int localOffset = localSize*localProc_;
00105   int i;
00106 
00107   std::vector<int> update(localSize);
00108   for(i=0; i<localSize; i++) update[i] = localOffset+i;
00109 
00110   fei::SharedPtr<fei_trilinos::Aztec_Map> map(
00111     new fei_trilinos::Aztec_Map(globalSize, localSize, &update[0], localOffset, comm_));
00112 
00113   fei_trilinos::AztecDMSR_Matrix* matrix = new fei_trilinos::AztecDMSR_Matrix(map);
00114 
00115   std::vector<int> elemrows(localSize);
00116   std::vector<int> elemcols(globalSize);
00117   double** elemcoefs = new double*[localSize];
00118   for(int j=0; j<globalSize; ++j) elemcols[j] = j;
00119   for(i=0; i<localSize; ++i) {
00120     elemrows[i] = localOffset+i;
00121     elemcoefs[i] = new double[globalSize];
00122     for(int j=0; j<globalSize; ++j) {
00123       elemcoefs[i][j] = (double)(localOffset+i+j);
00124     }
00125   }
00126   
00127   std::vector<std::vector<int> > colIndices(localSize);
00128   std::vector<std::vector<double> > values(localSize);
00129   std::vector<int> rowLengths(localSize);
00130   std::vector<int*> colPtrs(localSize);
00131   int nnzeros = 0;
00132 
00133   for(i=0; i<localSize; i++) {
00134     int diagEntry = 0;
00135     int row = i+localOffset;
00136     for(int j=0; j<globalSize; j++) {
00137       int col = j;
00138       if (col == row) diagEntry = 1;
00139       colIndices[i].push_back(col);
00140       values[i].push_back((double)(row+col));
00141     }
00142     rowLengths[i] = colIndices[i].size() - diagEntry;
00143     nnzeros += rowLengths[i] + 1;
00144     colPtrs[i] = &(colIndices[i][0]);
00145   }
00146 
00147   matrix->allocate( &rowLengths[0] );
00148 
00149   if (!(matrix->isAllocated())) {
00150     ERReturn(-1);
00151   }
00152 
00153   if (matrix->getNumNonZeros() != nnzeros) {
00154     ERReturn(-1);
00155   }
00156 
00157   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
00158 
00159   int* rowinds = &elemrows[0];
00160   int* colinds = &elemcols[0];
00161 
00162   CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
00163 
00164   for(i=0; i<localSize; ++i) {
00165     for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
00166   }
00167 
00168   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00169 
00170   for(i=0; i<localSize; ++i) {
00171     for(int j=0; j<globalSize; ++j) values[i][j] /= 2.0;
00172   }
00173 
00174   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
00175 
00176   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00177 
00178   if (matrix->writeToFile("A_Az_notFilled.mtx") != true) {
00179     ERReturn(-1);
00180   }
00181 
00182   if (matrix->readFromFile("A_Az_notFilled.mtx") != true) {
00183     ERReturn(-1);
00184   }
00185 
00186   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00187 
00188   matrix->fillComplete();
00189 
00190   if (!(matrix->isFilled())) {
00191     ERReturn(-1);
00192   }
00193 
00194   if (matrix->writeToFile("A_Az_filled.mtx") != true) {
00195     ERReturn(-1);
00196   }
00197 
00198   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00199 
00200   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, false) );
00201 
00202   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00203 
00204   matrix->put(0.0);
00205 
00206   CHK_ERR( fill_DMSR(*matrix, localOffset, colIndices, values, true) );
00207 
00208   CHK_ERR( matrix->sumIntoRow(localSize, rowinds, globalSize, colinds, elemcoefs) );
00209 
00210   for(i=0; i<localSize; ++i) {
00211     for(int j=0; j<globalSize; ++j) values[i][j] *= 2.0;
00212     delete [] elemcoefs[i];
00213   }
00214   delete [] elemcoefs;
00215 
00216   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00217 
00218   if (matrix->writeToFile("A_Az_filled2.mtx") != true) {
00219     ERReturn(-1);
00220   }
00221 
00222   if (matrix->readFromFile("A_Az_filled2.mtx") != true) {
00223     ERReturn(-1);
00224   }
00225 
00226   CHK_ERR( compare_DMSR_contents(*matrix, localOffset, colIndices, values) );
00227 
00228   delete matrix;
00229 #endif
00230   return(0);
00231 }
00232 
00233 int test_AztecWrappers::test2()
00234 {
00235   return(0);
00236 }
00237 
00238 int test_AztecWrappers::test3()
00239 {
00240   return(0);
00241 }
00242 
00243 int test_AztecWrappers::test4()
00244 {
00245   return(0);
00246 }
00247 
00248 #ifdef HAVE_FEI_AZTECOO
00249 //==============================================================================
00250 int compare_DMSR_contents(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00251         std::vector<std::vector<int> >& colIndices,
00252         std::vector<std::vector<double> >& values)
00253 {
00254   int localSize = colIndices.size();
00255 
00256   for(int i=0; i<localSize; i++) {
00257     int row = i+localOffset;
00258     int rowLen = matrix.rowLength(row);
00259     if (rowLen == 0) ERReturn(-1);
00260     int* tempInd = new int[rowLen];
00261     double* tempVal = new double[rowLen];
00262     std::vector<int> sortedInd;
00263     std::vector<double> sortedVal;
00264 
00265     int tmpLen = rowLen;
00266     matrix.getRow(row, tmpLen, tempVal, tempInd);
00267     if (tmpLen != rowLen) ERReturn(-1);
00268 
00269     for(int j=0; j<tmpLen; j++) {
00270       int offset = fei::sortedListInsert(tempInd[j], sortedInd);
00271       if (offset < 0) ERReturn(-1);
00272       sortedVal.insert(sortedVal.begin()+offset, tempVal[j]);
00273     }
00274 
00275     delete [] tempInd;
00276     delete [] tempVal;
00277 
00278     std::vector<int>& colInds = colIndices[i];
00279     if (sortedInd != colInds) {
00280       ERReturn(-1);
00281     }
00282     std::vector<double>& vals = values[i];
00283     if (sortedVal != vals) {
00284       ERReturn(-1);
00285     }
00286   }
00287 
00288   return(0);
00289 }
00290 
00291 //==============================================================================
00292 int fill_DMSR(fei_trilinos::AztecDMSR_Matrix& matrix, int localOffset,
00293         std::vector<std::vector<int> >& colIndices,
00294         std::vector<std::vector<double> >& values, bool sumInto)
00295 {
00296   int localSize = colIndices.size();
00297 
00298   for(int i=0; i<localSize; i++) {
00299     int row = localOffset + i;
00300     int rowLen = values[i].size();
00301 
00302     if (sumInto) {
00303       CHK_ERR( matrix.sumIntoRow(row, rowLen,
00304          &(values[i][0]), &(colIndices[i][0])) );
00305     }
00306     else {
00307       CHK_ERR( matrix.putRow(row, rowLen,
00308            &(values[i][0]), &(colIndices[i][0])) );
00309     }
00310   }
00311 
00312   return(0);
00313 }
00314 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends