InputData.hpp

00001 #ifndef _InputData_h_
00002 #define _InputData_h_
00003 
00004 /*--------------------------------------------------------------------*/
00005 /*    Copyright 2005 Sandia Corporation.                              */
00006 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00007 /*    non-exclusive license for use of this work by or on behalf      */
00008 /*    of the U.S. Government.  Export of this program may require     */
00009 /*    a license from the United States Government.                    */
00010 /*--------------------------------------------------------------------*/
00011 
00012 #include <fei_macros.hpp>
00013 
00014 #include <vector>
00015 
00016 class ElemContribution {
00017  public:
00018   ElemContribution(){}
00019   ElemContribution(const ElemContribution& src)
00020     {
00021       rhsContributions = src.rhsContributions;
00022     }
00023 
00024   ~ElemContribution(){}
00025 
00026   bool operator==(const ElemContribution& rhs)
00027     {
00028       if (matrixContributions != rhs.matrixContributions) {
00029   cout << "matrixContributions don't match." << endl;
00030   return(false);
00031       }
00032 
00033       if ( rhsContributions != rhs.rhsContributions ) {
00034   cout << "rhsContributions don't match." << endl;
00035   return(false);
00036       }
00037 
00038       return(true);
00039     }
00040 
00041   bool operator!=(const ElemContribution& rhs)
00042     {
00043       return( !( *this == rhs) );
00044     }
00045 
00046   std::vector<double> matrixContributions;
00047   std::vector<double> rhsContributions;
00048 };
00049 
00050 class InputData {
00051  public:
00052   InputData(){}
00053   ~InputData()
00054     {
00055       for(int i=0; i<elemIDs.length(); i++) delete elemIDs[i];
00056     }
00057 
00058   std::vector<int> elemBlockIDs;
00059   std::vector<std::vector<int>*> elemIDs;
00060   std::vector<std::vector<ElemContribution>*> elemContributions;
00061 
00062   bool operator==(const InputData& rhs)
00063     {
00064       if (elemBlockIDs != rhs.elemBlockIDs) {
00065   cout << "elemBlockIDs don't match." << endl;
00066   return(false);
00067       }
00068 
00069       for(int i=0; i<elemIDs.length(); i++) {
00070   std::vector<ElemContribution>& elems = *(elemContributions[i]);
00071   std::vector<ElemContribution>& rhsElems = *(rhs.elemContributions[i]);
00072 
00073   for(int j=0; j<elemIDs[i]->length(); j++) {
00074     int id1 = (*(elemIDs[i]))[j];
00075     int id2 = (*(rhs.elemIDs[i]))[j];
00076 
00077     if ( id1 != id2 ) {
00078       cout << "elemIDs don't match. element-block " << elemBlockIDs[i]
00079      << ", elemID in position " << j << " is " << id1
00080      << ", doesn't match " << id2 << "." << endl;
00081       return(false);
00082     }
00083 
00084     if (elems[j] != rhsElems[j]) {
00085       cout << "element-block " << elemBlockIDs[i] << ", elemID " << id1
00086         << "'s element-contributions don't match." << endl;
00087       return(false);
00088     }
00089   }
00090       }
00091 
00092       return(true);
00093     }
00094 
00095   bool operator!=(const InputData& rhs)
00096     {
00097       return( !( (*this) == rhs) );
00098     }
00099 
00100   int addElemID(int elemBlockID, int elemID)
00101     {
00102       //add and elemBlockID/elemID pair to the internal arrays if not already
00103       //present.
00104 
00105       int err, insertPoint = -1;
00106       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00107       if (blkInd < 0) {
00108   err = elemBlockIDs.insert(elemBlockIDs.begin()+insertPoint, elemBlockID);
00109   err += elemIDs.insert(elemIDs.begin()+insertPoint, new std::vector<int>);
00110   err += elemContributions.insert(elemContributions.begin()+insertPoint, new std::vector<ElemContribution>);
00111   if (err != 0) return(err);
00112   blkInd = insertPoint;
00113       }
00114 
00115       std::vector<int>& IDs = *(elemIDs[blkInd]);
00116       std::vector<ElemContribution>& ec = *(elemContributions[blkInd]);
00117 
00118       err = IDs.insertSorted(elemID);      
00119       if (err == -2) return(err);
00120 
00121       ElemContribution dummy;
00122       if (err >= 0) err = ec.insert(ec.begin()+err, dummy);
00123       if (err == -2) return(err);
00124 
00125       return(0);
00126     }
00127 
00128   int addElemMatrix(int elemBlockID, int elemID, std::vector<double>& matrixData)
00129     {
00130       int insertPoint = -1;
00131       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00132       if (blkInd < 0) {
00133   cerr << " addElemMatrix ERROR, elemBlockID " << (int)elemBlockID
00134        << " not found" << endl;
00135   return(-1);
00136       }
00137 
00138       int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
00139       if (elemIdx < 0) {
00140   cerr << "addElemMatrix ERROR, elemID " << (int)elemID << " not found."
00141        <<endl;
00142   return(-1);
00143       }
00144 
00145       ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
00146 
00147       std::vector<double>& elemContrMatrix = elemContr.matrixContributions;
00148       int len = matrixData.length();
00149       int oldLen = elemContrMatrix.length();
00150       if (oldLen < len) {
00151   elemContrMatrix.resize(len);
00152   for(int i=oldLen; i<len; i++) elemContrMatrix[i] = 0.0;
00153       }
00154 
00155       for(int i=0; i<matrixData.length(); i++) {
00156   elemContrMatrix[i] += matrixData[i];
00157       }
00158 
00159       return(0);
00160     }
00161 
00162   int addElemRHS(int elemBlockID, int elemID, std::vector<double>& rhsData)
00163     {
00164       int insertPoint = -1;
00165       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00166       if (blkInd < 0) {
00167   cerr << " addElemRHS ERROR, elemBlockID " << (int)elemBlockID
00168        << " not found" << endl;
00169   return(-1);
00170       }
00171 
00172       int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
00173       if (elemIdx < 0) {
00174   cerr << "addElemRHS ERROR, elemID " << (int)elemID << " not found."<<endl;
00175   return(-1);
00176       }
00177 
00178       ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
00179 
00180       std::vector<double>& elemContrRHS = elemContr.rhsContributions;
00181       int len = rhsData.length();
00182       int oldLen = elemContrRHS.length();
00183       if (oldLen < len) {
00184   elemContrRHS.resize(len);
00185   for(int i=oldLen; i<len; i++) elemContrRHS[i] = 0.0;
00186       }
00187 
00188       for(int i=0; i<rhsData.length(); i++) {
00189   elemContrRHS[i] += rhsData[i];
00190       }
00191 
00192       return(0);
00193     }
00194 };
00195 
00196 #endif // _InputData_h_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:24 2011 for FEI by  doxygen 1.6.3