FEI Version of the Day
fei_FieldMask.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 
00044 #include "fei_macros.hpp"
00045 
00046 #include <string>
00047 #include <exception>
00048 #include <stdexcept>
00049 
00050 #include "fei_ArrayUtils.hpp"
00051 #include "fei_FieldMask.hpp"
00052 
00053 
00054 fei::FieldMask::FieldMask()
00055   : maskID_(0),
00056     fieldIDs_(),
00057     fieldSizes_(),
00058     fieldEqnOffsets_(),
00059     numFields_(0),
00060     numIndices_(1)
00061 {
00062 }
00063 
00064 fei::FieldMask::FieldMask(const FieldMask& fm)
00065   : maskID_(fm.maskID_),
00066     fieldIDs_(fm.fieldIDs_),
00067     fieldSizes_(fm.fieldSizes_),
00068     fieldEqnOffsets_(fm.fieldEqnOffsets_)
00069 {
00070   numFields_ = fieldIDs_.size();
00071   numIndices_ = fm.numIndices_;
00072 }
00073 
00074 fei::FieldMask::FieldMask(int numFields,
00075             const int* fieldIDs,
00076             const int* fieldSizes)
00077   : maskID_(0),
00078     fieldIDs_(0, 4),
00079     fieldSizes_(0, 4),
00080     fieldEqnOffsets_(0, 4)
00081 {
00082   for(int i=0; i<numFields; ++i) {
00083     addField(fieldIDs[i], fieldSizes[i]);
00084   }
00085 }
00086 
00087 fei::FieldMask::~FieldMask()
00088 {
00089 }
00090 
00091 void fei::FieldMask::getFieldEqnOffset(int fieldID,
00092              int& offset) const
00093 {
00094   int idindex = 0;
00095   if (numFields_ < 2) {
00096     if (numFields_ < 1) {
00097       offset = 0;
00098       return;
00099     }
00100 
00101     if (fieldIDs_[0] != fieldID) {
00102       throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00103     }
00104   }
00105   else {
00106     idindex = -1;
00107     for(size_t i=0; i<fieldIDs_.size(); ++i) {
00108       if (fieldIDs_[i] == fieldID) {
00109         idindex = i; break;
00110       }
00111     }
00112   }
00113 
00114   if (idindex < 0) {
00115     throw std::runtime_error("fei::FieldMask::getFieldEqnOffset: fieldID not found");
00116   }
00117 
00118   offset = fieldEqnOffsets_[idindex];
00119 }
00120 
00121 void fei::FieldMask::addField(int fieldID, int fieldSize)
00122 {
00123   if (fieldID < 0) {
00124     throw std::runtime_error("fei::FieldMask ERROR, fieldID should be >= 0.");
00125   }
00126 
00127   int insertPoint = -1;
00128   int idindex = fei::binarySearch(fieldID, fieldIDs_, insertPoint);
00129   if (idindex >= 0) {
00130     for(unsigned i=idindex+1; i<fieldEqnOffsets_.size(); ++i) {
00131       fieldEqnOffsets_[i] += fieldSize;
00132     }
00133   }
00134   else {
00135     fieldIDs_.insert(fieldIDs_.begin()+insertPoint, fieldID);
00136 
00137     fieldSizes_.insert(fieldSizes_.begin()+insertPoint, fieldSize);
00138 
00139     fieldEqnOffsets_.push_back(1);
00140 
00141     int eqnOffset = 0;
00142     numIndices_ = 0;
00143     for(unsigned i=0; i<fieldIDs_.size(); ++i) {
00144       fieldEqnOffsets_[i] = eqnOffset;
00145       eqnOffset += fieldSizes_[i];
00146       numIndices_ += fieldSizes_[i];
00147     }
00148 
00149     numFields_ = fieldIDs_.size();
00150   }
00151 
00152   maskID_ = calculateMaskID();
00153 }
00154 
00155 int fei::FieldMask::calculateMaskID()
00156 {
00157   return( calculateMaskID(fieldIDs_.size(), &fieldIDs_[0]));
00158 }
00159 
00160 int fei::FieldMask::calculateMaskID(int numFields, const int* fieldIDs)
00161 {
00162   int maskID = 0;
00163   for(int i=0; i<numFields; ++i) {
00164     maskID += (fieldIDs[i]+1) +(i+1)*1000;
00165   }
00166 
00167   return(maskID);
00168 }
00169 
00170 int fei::FieldMask::calculateMaskID(const FieldMask& fm, int fieldID)
00171 {
00172 //  if (fm.hasFieldID(fieldID)) return fm.maskID_;
00173   return( fm.maskID_ + (fieldID+1) + (fm.numFields_+1)*1000 );
00174 }
00175 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends