FEI Version of the Day
fei_DofMapper.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 
00044 #ifndef _fei_DofMapper_hpp_
00045 #define _fei_DofMapper_hpp_
00046 
00047 #include <fei_macros.hpp>
00048 
00049 #include <set>
00050 #include <map>
00051 #include <sstream>
00052 #include <stdexcept>
00053 
00054 #include <fei_Dof.hpp>
00055 
00056 namespace fei {
00057 
00076 template<class LocalOrdinal, class GlobalOrdinal, class DofOrder=less_rank_id_field<LocalOrdinal, GlobalOrdinal> >
00077 class DofMapper {
00078  public:
00080   DofMapper()
00081   : m_dof_idx(), m_idx_dof(), m_maps_are_valid(false), m_field_sizes() {}
00082 
00084   ~DofMapper() {}
00085 
00086   void addDOF(LocalOrdinal rank, GlobalOrdinal id, LocalOrdinal field)
00087   {
00088   if (m_field_sizes.find(field) == m_field_sizes.end()) m_field_sizes.insert(std::make_pair(field,1));
00089     //m_maps_are_valid is false when a new Dof is inserted.
00090     m_maps_are_valid =
00091       m_dof_idx.insert(std::make_pair(Dof<LocalOrdinal,GlobalOrdinal>(rank, id, field), 0)).second;
00092   }
00093 
00098   void setFieldSize(LocalOrdinal field, LocalOrdinal field_size);
00099 
00100   LocalOrdinal getFieldSize(LocalOrdinal field) const;
00101 
00102   GlobalOrdinal getGlobalIndex(LocalOrdinal rank, GlobalOrdinal id, LocalOrdinal field) const;
00103 
00104   std::pair<const Dof<LocalOrdinal,GlobalOrdinal>*,LocalOrdinal> getDof(GlobalOrdinal global_index) const;
00105 
00106   bool maps_are_valid() const { return m_maps_are_valid; }
00107   void set_maps_are_valid(bool flag) { m_maps_are_valid = flag; }
00108 
00109   typedef typename std::map<Dof<LocalOrdinal,GlobalOrdinal>,GlobalOrdinal,DofOrder> DofMap;
00110 
00111   typename DofMap::const_iterator begin_dof() const
00112   { return m_dof_idx.begin(); }
00113 
00114   typename DofMap::const_iterator end_dof() const
00115   { return m_dof_idx.end(); }
00116 
00117   typename DofMap::iterator begin_dof()
00118   { return m_dof_idx.begin(); }
00119 
00120   typename DofMap::iterator end_dof()
00121   { return m_dof_idx.end(); }
00122 
00123   typedef typename std::map<GlobalOrdinal,const Dof<LocalOrdinal,GlobalOrdinal>*> IdxMap;
00124 
00125   typename IdxMap::const_iterator begin_idx() const
00126   { return m_idx_dof.begin(); }
00127 
00128   typename IdxMap::const_iterator end_idx() const
00129   { return m_idx_dof.end(); }
00130 
00131   typename IdxMap::iterator begin_idx()
00132   { return m_idx_dof.begin(); }
00133 
00134   typename IdxMap::iterator end_idx()
00135   { return m_idx_dof.end(); }
00136 
00137   const DofMap& get_dof_idx_map() const {return m_dof_idx;}
00138   DofMap& get_dof_idx_map() {return m_dof_idx;}
00139 
00140   const IdxMap& get_idx_dof_map() const {return m_idx_dof;}
00141   IdxMap& get_idx_dof_map() {return m_idx_dof;}
00142 
00143   typedef typename std::map<LocalOrdinal,LocalOrdinal> FieldSizeMap;
00144   const FieldSizeMap& getFieldSizeMap() const {return m_field_sizes;}
00145 
00146  private:
00147   std::map<Dof<LocalOrdinal, GlobalOrdinal>, GlobalOrdinal, DofOrder > m_dof_idx;
00148 
00149   std::map<GlobalOrdinal, const Dof<LocalOrdinal, GlobalOrdinal>*> m_idx_dof;
00150   bool m_maps_are_valid;
00151 
00152   std::map<LocalOrdinal,LocalOrdinal> m_field_sizes;
00153 
00154   DofMapper(const DofMapper<LocalOrdinal,GlobalOrdinal>& src);
00155   DofMapper& operator=(const DofMapper<LocalOrdinal,GlobalOrdinal>& src);
00156 };//class DofMapper
00157 
00158 template<class LocalOrdinal,class GlobalOrdinal,class DofOrder>
00159 void DofMapper<LocalOrdinal,GlobalOrdinal,DofOrder>::setFieldSize(LocalOrdinal field, LocalOrdinal field_size)
00160 {
00161   typename FieldSizeMap::iterator f_iter = m_field_sizes.find(field);
00162   if (f_iter == m_field_sizes.end()) {
00163     m_field_sizes.insert(std::make_pair(field, field_size));
00164   }
00165   else {
00166     //field already present, resetting field_size:
00167     f_iter->second = field_size;
00168   }
00169 }
00170 
00171 template<class LocalOrdinal,class GlobalOrdinal,class DofOrder>
00172 LocalOrdinal DofMapper<LocalOrdinal,GlobalOrdinal,DofOrder>::getFieldSize(LocalOrdinal field) const
00173 {
00174   typename FieldSizeMap::const_iterator f_iter = m_field_sizes.find(field);
00175   if (f_iter == m_field_sizes.end()) {
00176     std::ostringstream os;
00177     os << "fei::DofMapper::getFieldSize ERROR, field=="
00178       << field << " not found";
00179     std::string str = os.str();
00180     throw std::runtime_error(str);
00181   }
00182   return f_iter->second;
00183 }
00184 
00185 template<class LocalOrdinal,class GlobalOrdinal,class DofOrder>
00186 GlobalOrdinal DofMapper<LocalOrdinal,GlobalOrdinal,DofOrder>::getGlobalIndex(LocalOrdinal rank, GlobalOrdinal id, LocalOrdinal field) const
00187 {
00188   typename DofMap::const_iterator iter = m_dof_idx.find(Dof<LocalOrdinal,GlobalOrdinal>(rank,id,field));
00189   if (iter == m_dof_idx.end()) {
00190     std::ostringstream osstr;
00191     osstr << "fei::DofMapper::getGlobalIndex ERROR, dof("
00192         << rank << "," << id << "," << field << ") not found.";
00193     std::string str = osstr.str();
00194     throw std::runtime_error(str);
00195   }
00196 
00197   return iter->second;
00198 }
00199 
00200 template<class LocalOrdinal,class GlobalOrdinal,class DofOrder>
00201 std::pair<const Dof<LocalOrdinal,GlobalOrdinal>*,LocalOrdinal>
00202 DofMapper<LocalOrdinal,GlobalOrdinal,DofOrder>::getDof(GlobalOrdinal global_index) const
00203 {
00204   typename IdxMap::const_iterator iter = m_idx_dof.lower_bound(global_index);
00205   if (iter == m_idx_dof.begin()) {
00206   if (iter->first == global_index) {
00207     return std::make_pair(iter->second, global_index);
00208   }
00209   else {
00210       std::ostringstream osstr;
00211       osstr << "fei::DofMapper::getDof ERROR, dof not found for global_index=="
00212         << global_index;
00213       std::string str = osstr.str();
00214       throw std::runtime_error(str);
00215   }
00216   }
00217   else if (iter != m_idx_dof.end() && iter->first == global_index) {
00218     //return pair(dof,component-of-field)
00219   return std::make_pair(iter->second, 0);
00220   }
00221 
00222   bool last_dof = iter == m_idx_dof.end();
00223   --iter;
00224   //return pair(dof,component-of-field)
00225   LocalOrdinal component = global_index - iter->first;
00226   bool check_range_of_component = last_dof && !m_field_sizes.empty();
00227   if (check_range_of_component) {
00228   typename std::map<LocalOrdinal,LocalOrdinal>::const_iterator f_iter = m_field_sizes.find(iter->second->field());
00229   if (f_iter == m_field_sizes.end() || f_iter->second <= component) {
00230     std::ostringstream os;
00231     os << "fei::DofMapper::getDof ERROR2, dof not found for global_index=="
00232       << global_index;
00233     std::string str = os.str();
00234     throw std::runtime_error(str);
00235   }
00236   }
00237 
00238   return std::make_pair(iter->second, component);
00239 }
00240 
00241 }//namespace fei
00242 
00243 #endif
00244 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends