test_FEI_Implementation.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 
00010 #include <fei_macros.hpp>
00011 #include <fei_sstream.hpp>
00012 
00013 #include <test_utils/test_FEI_Implementation.hpp>
00014 
00015 #include <FEI_Implementation.hpp>
00016 #ifdef HAVE_FEI_AZTECOO
00017 #include <fei_Aztec_LinSysCore.hpp>
00018 #endif
00019 #include <test_utils/FEData.hpp>
00020 #include <fei_LibraryWrapper.hpp>
00021 
00022 #include <test_utils/testData.hpp>
00023 
00024 #include <fei_Filter.hpp>
00025 
00026 #undef fei_file
00027 #define fei_file "test_FEI_Implementation.cpp"
00028 #include <fei_ErrMacros.hpp>
00029 
00030 test_FEI_Implementation::test_FEI_Implementation(MPI_Comm comm)
00031  : tester(comm)
00032 {
00033 }
00034 
00035 test_FEI_Implementation::~test_FEI_Implementation()
00036 {
00037 }
00038 
00039 int test_FEI_Implementation::runtests()
00040 {
00041   if (numProcs_<2) {
00042     CHK_ERR( serialtest1() );
00043   }
00044 
00045   CHK_ERR( test1() );
00046   CHK_ERR( test2() );
00047   CHK_ERR( test3() );
00048   CHK_ERR( test4() );
00049   return(0);
00050 }
00051 
00052 int test_FEI_Implementation::serialtest1()
00053 {
00054   int i, j, n = 3;
00055   double** coefs = new double*[n];
00056   double** copy = new double*[n];
00057   double** correct = new double*[n];
00058 
00059   for(i=0; i<n; ++i) {
00060     coefs[i] = new double[n];
00061     copy[i] = new double[n];
00062     correct[i] = new double[n];
00063     for(j=0; j<n; ++j) {
00064       coefs[i][j] = (double)(i+j);
00065       correct[i][j] = (double)(i+j);
00066     }
00067   }
00068 
00069   Filter::copyStiffness(coefs, n, FEI_DENSE_ROW, copy);
00070   CHK_ERR( compareCoefs(n, correct, copy) );
00071 
00072   for(i=0; i<n; ++i) {
00073     for(j=0; j<n; ++j) coefs[i][j] = 0.0;
00074     for(j=i; j<n; ++j) {
00075       coefs[i][j-i] = (double)(i+j);
00076     }
00077   }
00078 
00079   Filter::copyStiffness(coefs, n, FEI_UPPER_SYMM_ROW, copy);
00080   CHK_ERR( compareCoefs(n, correct, copy) );
00081 
00082   for(i=0; i<n; ++i) {
00083     for(j=0; j<n; ++j) coefs[i][j] = 0.0;
00084     for(j=0; j<n; ++j) {
00085       if (j>i) continue;
00086       coefs[i][j] = (double)(i+j);
00087     }
00088   }
00089 
00090   Filter::copyStiffness(coefs, n, FEI_LOWER_SYMM_ROW, copy);
00091   CHK_ERR( compareCoefs(n, correct, copy) );
00092 
00093   for(i=0; i<n; ++i) {
00094     delete [] coefs[i];
00095     delete [] copy[i];
00096     delete [] correct[i];
00097   }
00098 
00099   delete [] coefs;
00100   delete [] copy;
00101   delete [] correct;
00102 
00103   return(0);
00104 }
00105 
00106 int test_FEI_Implementation::compareCoefs(int n,
00107             const double*const* coefs1,
00108             const double*const* coefs2)
00109 {
00110   for(int i=0; i<n; ++i) {
00111     for(int j=0; j<n; ++j) {
00112       double diff = coefs1[i][j] - coefs2[i][j];
00113       if (diff > 1.e-20 || diff < -1.e-20) {
00114   return(-1);
00115       }
00116     }
00117   }
00118 
00119   return(0);
00120 }
00121 
00122 int test_FEI_Implementation::test1()
00123 {
00124 #ifdef HAVE_FEI_AZTECOO
00125   fei::SharedPtr<testData> testdata(new testData(localProc_, numProcs_));
00126 
00127   fei::SharedPtr<LinearSystemCore> linSys(new fei_trilinos::Aztec_LinSysCore(comm_));
00128   fei::SharedPtr<LibraryWrapper> wrapper(new LibraryWrapper(linSys));
00129 
00130   fei::SharedPtr<FEI_Implementation>
00131     fei(new FEI_Implementation(wrapper, comm_, 0));
00132 
00133   int numParams = 2;
00134   char** params = new char*[numParams];
00135   params[0] = new char[128];
00136   strcpy(params[0], "name test1");
00137   if (path_.empty()) {
00138     params[1] = new char[32];
00139     strcpy(params[1], "debugOutput .");
00140   }
00141   else {
00142     FEI_OSTRINGSTREAM osstr;
00143     osstr << "debugOutput " << path_;
00144     std::string str = osstr.str();
00145     
00146     params[1] = new char[str.size()+1];
00147     strcpy(params[1], str.c_str());
00148   }
00149 
00150   //call the parameters function a couple of times to test the fei's internal
00151   //method for merging string lists when parameters is called more than once.
00152   CHK_ERR( fei->parameters(1, &params[0]) );
00153   CHK_ERR( fei->parameters(1, &params[1]) );
00154   CHK_ERR( fei->parameters(2, params) );
00155 
00156   delete [] params[0];
00157   delete [] params[1];
00158   delete [] params;
00159 
00160   CHK_ERR( fei->setIDLists(1, &(testdata->ids[0]),
00161          1, &(testdata->ids[0])) );
00162 
00163   CHK_ERR( fei->initFields(testdata->fieldIDs.size(),
00164         &(testdata->fieldSizes[0]),
00165         &(testdata->fieldIDs[0])) );
00166 
00167   unsigned numNodesPerElem = testdata->ids.size();
00168   std::vector<int> numFieldsPerNode(numNodesPerElem, 1);
00169   std::vector<int*>nodalFieldIDs(numNodesPerElem, &(testdata->fieldIDs[0]));
00170 
00171   CHK_ERR( fei->initElemBlock(0, //blockID
00172            1, //numElements
00173            numNodesPerElem,
00174            &numFieldsPerNode[0],
00175            &nodalFieldIDs[0],
00176            0, //numElemDofFieldsPerElement
00177            NULL, //elemDofFieldIDs
00178            0)); //interleaveStrategy
00179 
00180   CHK_ERR( fei->initElem(0, //blockID
00181             0, //elemID
00182             &(testdata->ids[0])) );
00183 
00184   std::vector<int*> sharingProcs2D(testdata->sharedIDs.size());
00185   int offset = 0;
00186   for(unsigned i=0; i<testdata->numSharingProcsPerID.size(); ++i) {
00187     sharingProcs2D[i] = &(testdata->sharingProcs[offset]);
00188     offset += testdata->numSharingProcsPerID[i];
00189   }
00190 
00191   if (testdata->sharedIDs.size() > 0) {
00192     CHK_ERR( fei->initSharedNodes(testdata->sharedIDs.size(),
00193       testdata->sharedIDs.size() ? &(testdata->sharedIDs[0]) : 0,
00194       testdata->numSharingProcsPerID.size() ? &(testdata->numSharingProcsPerID[0]) : 0,
00195       &sharingProcs2D[0]) );
00196   }
00197 
00198   CHK_ERR( fei->initComplete() );
00199 
00200   std::vector<double> rhsData(testdata->ids.size(), 1.0);
00201 
00202   double one = 1.0;
00203   CHK_ERR( fei->setMatScalars(1, &(testdata->ids[0]), &one) );
00204   CHK_ERR( fei->setRHSScalars(1, &(testdata->ids[0]), &one) );
00205 
00206   CHK_ERR( fei->setCurrentMatrix(testdata->ids[0]) );
00207   CHK_ERR( fei->setCurrentRHS(testdata->ids[0]) );
00208 
00209   CHK_ERR( fei->putIntoRHS(FEI_NODE, testdata->fieldIDs[0],
00210          testdata->ids.size(),
00211          &(testdata->ids[0]),
00212          &(rhsData[0])) );
00213 
00214   int numBCNodes = 2;
00215   GlobalID* BCNodeIDs = &(testdata->ids[0]);
00216   int BCFieldID = testdata->fieldIDs[0];
00217   double* values = new double[numBCNodes];
00218   int* offsetsIntoField = new int[numBCNodes];
00219   for(int ii=0; ii<numBCNodes; ++ii) {
00220     values[ii] = 1.0;
00221     offsetsIntoField[ii] = 0;
00222   }
00223 
00224   CHK_ERR( fei->loadNodeBCs(numBCNodes, BCNodeIDs, BCFieldID,
00225           offsetsIntoField, values) );
00226 
00227   delete [] offsetsIntoField;
00228   delete [] values;
00229 
00230   CHK_ERR( fei->loadComplete() );
00231 
00232   int numActiveNodes = 0;
00233   CHK_ERR( fei->getNumLocalNodes(numActiveNodes) );
00234   if (numActiveNodes != (int)testdata->ids.size()) {
00235     ERReturn(-1);
00236   }
00237 
00238   GlobalID* localNodes = new GlobalID[numActiveNodes];
00239   CHK_ERR( fei->getLocalNodeIDList(numActiveNodes, localNodes, numActiveNodes) );
00240 
00241   int totalFieldSize = 0;
00242   for(int ii=0; ii<(int)testdata->fieldSizes.size(); ++ii) {
00243     totalFieldSize += testdata->fieldSizes[ii];
00244   }
00245 
00246   double* soln = new double[numActiveNodes*totalFieldSize];
00247   int* offsets = new int[numActiveNodes+1];
00248 
00249   CHK_ERR( fei->getNodalSolution(numActiveNodes, localNodes,
00250          offsets, soln) );
00251   delete [] offsets;
00252   delete [] soln;
00253   delete [] localNodes;
00254 
00255   CHK_ERR( fei->resetInitialGuess() );
00256 
00257   int fieldSize = 0;
00258   CHK_ERR( fei->getFieldSize(testdata->fieldIDs[0], fieldSize) );
00259 
00260   double initTime, loadTime, solveTime, solnReturnTime;
00261   CHK_ERR( fei->cumulative_cpu_times(initTime, loadTime, solveTime,
00262               solnReturnTime) );
00263 #endif
00264   return(0);
00265 }
00266 
00267 int test_FEI_Implementation::test2()
00268 {
00269   fei::SharedPtr<testData> testdata(new testData(localProc_, numProcs_));
00270 
00271   fei::SharedPtr<FiniteElementData> fedata(new FEData(comm_));
00272   fei::SharedPtr<LibraryWrapper> wrapper(new LibraryWrapper(fedata));
00273   fei::SharedPtr<FEI_Implementation>
00274     fei(new FEI_Implementation(wrapper, comm_, 0));
00275 
00276   int numParams = 2;
00277   char** params = new char*[numParams];
00278   params[0] = new char[128];
00279   strcpy(params[0], "name test1");
00280   if (path_.empty()) {
00281     params[1] = new char[32];
00282     strcpy(params[1], "debugOutput .");
00283   }
00284   else {
00285     FEI_OSTRINGSTREAM osstr;
00286     osstr << "debugOutput " << path_;
00287     std::string str = osstr.str();
00288     
00289     params[1] = new char[str.size()+1];
00290     strcpy(params[1], str.c_str());
00291   }
00292 
00293 
00294   //call the parameters function a couple of times to test the fei's internal
00295   //method for merging string lists when parameters is called more than once.
00296   CHK_ERR( fei->parameters(1, &params[0]) );
00297   CHK_ERR( fei->parameters(1, &params[1]) );
00298   CHK_ERR( fei->parameters(2, params) );
00299 
00300   delete [] params[0];
00301   delete [] params[1];
00302   delete [] params;
00303 
00304   CHK_ERR( fei->setIDLists(1, &(testdata->ids[0]),
00305          1, &(testdata->ids[0])) );
00306 
00307   CHK_ERR( fei->initFields(testdata->fieldIDs.size(),
00308         &(testdata->fieldSizes[0]),
00309         &(testdata->fieldIDs[0])) );
00310 
00311   unsigned numNodesPerElem = testdata->ids.size();
00312   std::vector<int> numFieldsPerNode(numNodesPerElem, 1);
00313   std::vector<int*>nodalFieldIDs(numNodesPerElem, &(testdata->fieldIDs[0]));
00314 
00315   CHK_ERR( fei->initElemBlock(0, //blockID
00316             1, //numElements
00317             numNodesPerElem,
00318             &numFieldsPerNode[0],
00319             &nodalFieldIDs[0],
00320             0, //numElemDofFieldsPerElement
00321             NULL, //elemDofFieldIDs
00322             0)); //interleaveStrategy
00323 
00324   CHK_ERR( fei->initElem(0, //blockID
00325             0, //elemID
00326             &(testdata->ids[0])) );
00327 
00328   std::vector<int*> sharingProcs2D(testdata->sharedIDs.size());
00329   int offset = 0;
00330   for(unsigned i=0; i<testdata->numSharingProcsPerID.size(); ++i) {
00331     sharingProcs2D[i] = &(testdata->sharingProcs[offset]);
00332     offset += testdata->numSharingProcsPerID[i];
00333   }
00334 
00335   if (testdata->sharedIDs.size() > 0) {
00336     CHK_ERR( fei->initSharedNodes(testdata->sharedIDs.size(),
00337       testdata->sharedIDs.size() ? &(testdata->sharedIDs[0]) : 0,
00338       testdata->numSharingProcsPerID.size() ? &(testdata->numSharingProcsPerID[0]) : 0,
00339       &sharingProcs2D[0]) );
00340   }
00341 
00342   CHK_ERR( fei->initComplete() );
00343 
00344   int numBlkActNodes = 0;
00345   CHK_ERR( fei->getNumBlockActNodes(0, numBlkActNodes) );
00346 
00347   std::vector<double> rhsData(testdata->ids.size(), 1.0);
00348 
00349   double one = 1.0;
00350   CHK_ERR( fei->setMatScalars(1, &(testdata->ids[0]), &one) );
00351   CHK_ERR( fei->setRHSScalars(1, &(testdata->ids[0]), &one) );
00352 
00353   CHK_ERR( fei->setCurrentMatrix(testdata->ids[0]) );
00354   CHK_ERR( fei->setCurrentRHS(testdata->ids[0]) );
00355 
00356   CHK_ERR( fei->putIntoRHS(FEI_NODE, testdata->fieldIDs[0],
00357          testdata->ids.size(),
00358          &(testdata->ids[0]),
00359          &(rhsData[0])) );
00360 
00361   int numBCNodes = 2;
00362   GlobalID* BCNodeIDs = &(testdata->ids[0]);
00363   int BCFieldID = testdata->fieldIDs[0];
00364   double* values = new double[numBCNodes];
00365   int* offsetsIntoField = new int[numBCNodes];
00366   for(int ii=0; ii<numBCNodes; ++ii) {
00367     values[ii] = 1.0;
00368     offsetsIntoField[ii] = 0;
00369   }
00370 
00371   CHK_ERR( fei->loadNodeBCs(numBCNodes, BCNodeIDs, BCFieldID,
00372           offsetsIntoField, values) );
00373 
00374   delete [] offsetsIntoField;
00375   delete [] values;
00376 
00377   CHK_ERR( fei->loadComplete() );
00378 
00379   int numActiveNodes = 0;
00380   CHK_ERR( fei->getNumLocalNodes(numActiveNodes) );
00381   if (numActiveNodes != (int)testdata->ids.size()) {
00382     ERReturn(-1);
00383   }
00384 
00385   GlobalID* localNodes = new GlobalID[numActiveNodes];
00386   CHK_ERR( fei->getLocalNodeIDList(numActiveNodes, localNodes, numActiveNodes) );
00387 
00388   int totalFieldSize = 0;
00389   for(int ii=0; ii<(int)testdata->fieldSizes.size(); ++ii) {
00390     totalFieldSize += testdata->fieldSizes[ii];
00391   }
00392 
00393   double* soln = new double[numActiveNodes*totalFieldSize];
00394   int* offsets = new int[numActiveNodes+1];
00395 
00396   CHK_ERR( fei->getNodalSolution(numActiveNodes, localNodes,
00397          offsets, soln) );
00398   delete [] offsets;
00399   delete [] soln;
00400   delete [] localNodes;
00401 
00402   CHK_ERR( fei->resetInitialGuess() );
00403 
00404   int fieldSize = 0;
00405   CHK_ERR( fei->getFieldSize(testdata->fieldIDs[0], fieldSize) );
00406 
00407   double initTime, loadTime, solveTime, solnReturnTime;
00408   CHK_ERR( fei->cumulative_cpu_times(initTime, loadTime, solveTime,
00409               solnReturnTime) );
00410 
00411   return(0);
00412 }
00413 
00414 int test_FEI_Implementation::test3()
00415 {
00416   return(0);
00417 }
00418 
00419 int test_FEI_Implementation::test4()
00420 {
00421   return(0);
00422 }

Generated on Wed May 12 21:30:42 2010 for FEI by  doxygen 1.4.7