FEI Version of the Day
InputData.hpp
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 #ifndef _InputData_h_
00044 #define _InputData_h_
00045 
00046 
00047 #include <fei_macros.hpp>
00048 
00049 #include <vector>
00050 
00051 class ElemContribution {
00052  public:
00053   ElemContribution(){}
00054   ElemContribution(const ElemContribution& src)
00055     {
00056       rhsContributions = src.rhsContributions;
00057     }
00058 
00059   ~ElemContribution(){}
00060 
00061   bool operator==(const ElemContribution& rhs)
00062     {
00063       if (matrixContributions != rhs.matrixContributions) {
00064   cout << "matrixContributions don't match." << endl;
00065   return(false);
00066       }
00067 
00068       if ( rhsContributions != rhs.rhsContributions ) {
00069   cout << "rhsContributions don't match." << endl;
00070   return(false);
00071       }
00072 
00073       return(true);
00074     }
00075 
00076   bool operator!=(const ElemContribution& rhs)
00077     {
00078       return( !( *this == rhs) );
00079     }
00080 
00081   std::vector<double> matrixContributions;
00082   std::vector<double> rhsContributions;
00083 };
00084 
00085 class InputData {
00086  public:
00087   InputData(){}
00088   ~InputData()
00089     {
00090       for(int i=0; i<elemIDs.length(); i++) delete elemIDs[i];
00091     }
00092 
00093   std::vector<int> elemBlockIDs;
00094   std::vector<std::vector<int>*> elemIDs;
00095   std::vector<std::vector<ElemContribution>*> elemContributions;
00096 
00097   bool operator==(const InputData& rhs)
00098     {
00099       if (elemBlockIDs != rhs.elemBlockIDs) {
00100   cout << "elemBlockIDs don't match." << endl;
00101   return(false);
00102       }
00103 
00104       for(int i=0; i<elemIDs.length(); i++) {
00105   std::vector<ElemContribution>& elems = *(elemContributions[i]);
00106   std::vector<ElemContribution>& rhsElems = *(rhs.elemContributions[i]);
00107 
00108   for(int j=0; j<elemIDs[i]->length(); j++) {
00109     int id1 = (*(elemIDs[i]))[j];
00110     int id2 = (*(rhs.elemIDs[i]))[j];
00111 
00112     if ( id1 != id2 ) {
00113       cout << "elemIDs don't match. element-block " << elemBlockIDs[i]
00114      << ", elemID in position " << j << " is " << id1
00115      << ", doesn't match " << id2 << "." << endl;
00116       return(false);
00117     }
00118 
00119     if (elems[j] != rhsElems[j]) {
00120       cout << "element-block " << elemBlockIDs[i] << ", elemID " << id1
00121         << "'s element-contributions don't match." << endl;
00122       return(false);
00123     }
00124   }
00125       }
00126 
00127       return(true);
00128     }
00129 
00130   bool operator!=(const InputData& rhs)
00131     {
00132       return( !( (*this) == rhs) );
00133     }
00134 
00135   int addElemID(int elemBlockID, int elemID)
00136     {
00137       //add and elemBlockID/elemID pair to the internal arrays if not already
00138       //present.
00139 
00140       int err, insertPoint = -1;
00141       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00142       if (blkInd < 0) {
00143   err = elemBlockIDs.insert(elemBlockIDs.begin()+insertPoint, elemBlockID);
00144   err += elemIDs.insert(elemIDs.begin()+insertPoint, new std::vector<int>);
00145   err += elemContributions.insert(elemContributions.begin()+insertPoint, new std::vector<ElemContribution>);
00146   if (err != 0) return(err);
00147   blkInd = insertPoint;
00148       }
00149 
00150       std::vector<int>& IDs = *(elemIDs[blkInd]);
00151       std::vector<ElemContribution>& ec = *(elemContributions[blkInd]);
00152 
00153       err = IDs.insertSorted(elemID);      
00154       if (err == -2) return(err);
00155 
00156       ElemContribution dummy;
00157       if (err >= 0) err = ec.insert(ec.begin()+err, dummy);
00158       if (err == -2) return(err);
00159 
00160       return(0);
00161     }
00162 
00163   int addElemMatrix(int elemBlockID, int elemID, std::vector<double>& matrixData)
00164     {
00165       int insertPoint = -1;
00166       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00167       if (blkInd < 0) {
00168   cerr << " addElemMatrix ERROR, elemBlockID " << (int)elemBlockID
00169        << " not found" << endl;
00170   return(-1);
00171       }
00172 
00173       int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
00174       if (elemIdx < 0) {
00175   cerr << "addElemMatrix ERROR, elemID " << (int)elemID << " not found."
00176        <<endl;
00177   return(-1);
00178       }
00179 
00180       ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
00181 
00182       std::vector<double>& elemContrMatrix = elemContr.matrixContributions;
00183       int len = matrixData.length();
00184       int oldLen = elemContrMatrix.length();
00185       if (oldLen < len) {
00186   elemContrMatrix.resize(len);
00187   for(int i=oldLen; i<len; i++) elemContrMatrix[i] = 0.0;
00188       }
00189 
00190       for(int i=0; i<matrixData.length(); i++) {
00191   elemContrMatrix[i] += matrixData[i];
00192       }
00193 
00194       return(0);
00195     }
00196 
00197   int addElemRHS(int elemBlockID, int elemID, std::vector<double>& rhsData)
00198     {
00199       int insertPoint = -1;
00200       int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
00201       if (blkInd < 0) {
00202   cerr << " addElemRHS ERROR, elemBlockID " << (int)elemBlockID
00203        << " not found" << endl;
00204   return(-1);
00205       }
00206 
00207       int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
00208       if (elemIdx < 0) {
00209   cerr << "addElemRHS ERROR, elemID " << (int)elemID << " not found."<<endl;
00210   return(-1);
00211       }
00212 
00213       ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
00214 
00215       std::vector<double>& elemContrRHS = elemContr.rhsContributions;
00216       int len = rhsData.length();
00217       int oldLen = elemContrRHS.length();
00218       if (oldLen < len) {
00219   elemContrRHS.resize(len);
00220   for(int i=oldLen; i<len; i++) elemContrRHS[i] = 0.0;
00221       }
00222 
00223       for(int i=0; i<rhsData.length(); i++) {
00224   elemContrRHS[i] += rhsData[i];
00225       }
00226 
00227       return(0);
00228     }
00229 };
00230 
00231 #endif // _InputData_h_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends