FEI Version of the Day
snl_fei_Broker_FEData.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 _snl_fei_Broker_FEData_hpp_
00045 #define _snl_fei_Broker_FEData_hpp_
00046 
00047 #include <fei_macros.hpp>
00048 #include <fei_mpi.h>
00049 #include <snl_fei_Broker.hpp>
00050 #include <fei_FiniteElementData.hpp>
00051 #include <fei_VectorSpace.hpp>
00052 #include <fei_MatrixGraph.hpp>
00053 #include <fei_Matrix_Impl.hpp>
00054 #include <fei_Pattern.hpp>
00055 #include <fei_Vector_Impl.hpp>
00056 #include <fei_ConnectivityBlock.hpp>
00057 #include <snl_fei_LinearSystem_FEData.hpp>
00058 #include <fei_Lookup_Impl.hpp>
00059 
00060 #undef fei_file
00061 #define fei_file "snl_fei_Broker_FEData.hpp"
00062 #include <fei_ErrMacros.hpp>
00063 
00064 namespace snl_fei {
00065 
00069   class Broker_FEData : public snl_fei::Broker {
00070   public:
00072     Broker_FEData(fei::SharedPtr<FiniteElementData> feData,
00073       fei::SharedPtr<fei::MatrixGraph> matrixGraph,
00074       int nodeIDType);
00075 
00077     virtual ~Broker_FEData();
00078 
00088     virtual fei::SharedPtr<fei::Vector> createVector(bool isSolutionVector=false)
00089       {
00090   fei::SharedPtr<fei::Vector> vptr;
00091   if (matrixGraph_.get() == NULL) return(vptr);
00092 
00093   if (setStructure() != 0) return(vptr);
00094 
00095         int localsize = matrixGraph_->getRowSpace()->getNumIndices_Owned();
00096   fei::SharedPtr<fei::Vector> vecptr;
00097         vecptr.reset(new fei::Vector_Impl<FiniteElementData>(matrixGraph_->getRowSpace(),
00098                                                    feData_.get(), localsize,
00099                                                     isSolutionVector));
00100   return(vecptr);
00101       }
00102 
00105     virtual fei::SharedPtr<fei::Matrix> createMatrix()
00106       {
00107   fei::SharedPtr<fei::Matrix> mptr;
00108   if (matrixGraph_.get() == NULL) return(mptr);
00109 
00110   if (setStructure() != 0) return(mptr);
00111         int localsize = matrixGraph_->getRowSpace()->getNumIndices_Owned();
00112 
00113   fei::SharedPtr<fei::Matrix> matptr;
00114         matptr.reset(new fei::Matrix_Impl<FiniteElementData>(feData_,
00115                  matrixGraph_, localsize));
00116   return(matptr);
00117       }
00118 
00121     virtual fei::SharedPtr<fei::LinearSystem> createLinearSystem()
00122       {
00123   fei::SharedPtr<fei::LinearSystem> lsptr;
00124   if (matrixGraph_.get() == NULL) return(lsptr);
00125 
00126   if (setStructure() != 0) return(lsptr);
00127 
00128   snl_fei::LinearSystem_FEData*
00129     linsysfed = new LinearSystem_FEData(feData_,
00130                 matrixGraph_);
00131   linsysfed->setLookup(lookup_);
00132   fei::SharedPtr<fei::LinearSystem> linsysptr(linsysfed);
00133   return(linsysptr);
00134       }
00135 
00137     virtual void setMatrixGraph(fei::SharedPtr<fei::MatrixGraph> matrixGraph)
00138     {
00139       matrixGraph_ = matrixGraph;
00140     }
00141 
00142   private:
00143     int setStructure()
00144     {
00145       if (matrixGraph_.get() == NULL) ERReturn(-1);
00146       if (setStructure_ == true) return(0);
00147 
00148       lookup_ = new fei::Lookup_Impl(matrixGraph_, nodeIDType_);
00149 
00150       CHK_ERR( feData_->setLookup(*lookup_) );
00151 
00152       fei::SharedPtr<fei::VectorSpace> vspace = matrixGraph_->getRowSpace();
00153 
00154       int numLocalNodes = vspace->getNumOwnedAndSharedIDs(nodeIDType_);
00155 
00156       int numElemBlocks = matrixGraph_->getNumConnectivityBlocks();
00157 
00158       int nodeType = 0;
00159       snl_fei::RecordCollection* nodeRecords = NULL;
00160       vspace->getRecordCollection(nodeType, nodeRecords);
00161 
00162       int* intData = new int[numElemBlocks*3];
00163       int* numElemsPerBlock =       intData;
00164       int* numNodesPerElem =        intData+numElemBlocks;
00165       int* elemMatrixSizePerBlock = intData+2*numElemBlocks;
00166       int i;
00167 
00168       std::vector<int> elemBlockIDs;
00169       CHK_ERR( matrixGraph_->getConnectivityBlockIDs( elemBlockIDs) );
00170 
00171       for(i=0; i<numElemBlocks; ++i) {
00172         const fei::ConnectivityBlock* cblock =
00173           matrixGraph_->getConnectivityBlock(elemBlockIDs[i]);
00174         if (cblock==NULL) return(-1);
00175         numElemsPerBlock[i] = cblock->getNativeConnectivityIDs().size();
00176         numNodesPerElem[i] = cblock->getRowPattern()->getNumIDs();
00177         elemMatrixSizePerBlock[i] = cblock->getRowPattern()->getNumIndices();
00178       }
00179 
00180       int numSharedNodes = 0;
00181       CHK_ERR( vspace->getNumSharedIDs(nodeIDType_, numSharedNodes) );
00182 
00183       int numLagrangeConstraints = matrixGraph_->getLocalNumLagrangeConstraints();
00184 
00185       CHK_ERR( feData_->describeStructure(numElemBlocks,
00186             numElemsPerBlock,
00187             numNodesPerElem,
00188             elemMatrixSizePerBlock,
00189             numLocalNodes,
00190             numSharedNodes,
00191             numLagrangeConstraints) );
00192 
00193       std::map<int,fei::ConnectivityBlock*>::const_iterator
00194         cdb_iter = matrixGraph_->getConnectivityBlocks().begin();
00195 
00196       std::vector<int> nodeNumbers, numDofPerNode, dof_ids;
00197       int total_num_dof = 0;
00198       for(i=0; i<numElemBlocks; ++i, ++cdb_iter) {
00199         fei::ConnectivityBlock* cblock = (*cdb_iter).second;
00200         fei::Pattern* pattern = cblock->getRowPattern();
00201 
00202         int numConnectedNodes = pattern->getNumIDs();
00203         nodeNumbers.resize(numConnectedNodes);
00204         numDofPerNode.resize(numConnectedNodes);
00205         int* nodeNumPtr = &nodeNumbers[0];
00206         int* numDofPtr = &numDofPerNode[0];
00207 
00208         //For the calls to FiniteElementData::setConnectivity, we're going to
00209         //need a list of num-dof-per-node. So construct that now.
00210         const int* numFieldsPerID = pattern->getNumFieldsPerID();
00211         const int* fieldIDs = pattern->getFieldIDs();
00212 
00213         int foffset = 0;
00214         for(int ii=0; ii<numConnectedNodes; ++ii) {
00215           int dof = 0;
00216           for(int f=0; f<numFieldsPerID[ii]; ++f) {
00217             dof += vspace->getFieldSize(fieldIDs[foffset++]);
00218           }
00219           numDofPtr[ii] = dof;
00220           total_num_dof += dof;
00221         }
00222 
00223         dof_ids.resize(total_num_dof, 0);
00224         int* dof_ids_ptr = &dof_ids[0];
00225 
00227         //Next we'll loop over the connectivity-lists in this block,
00228         //making a call to FiniteElementData::setConnectivity for each one.
00229 
00230         const fei::IndexType<int,int>& elemIDs = cblock->getNativeConnectivityIDs();
00231         int numElems = elemIDs.size();
00232         int* nodes = &(cblock->getRowConnectivities()[0]);
00233 
00234         int offset = 0;
00235         for(int elem=0; elem<numElems; ++elem) {
00236           for(int n=0; n<numConnectedNodes; ++n) {
00237             fei::Record<int>* node = nodeRecords->getRecordWithLocalID(nodes[offset++]);
00238             nodeNumPtr[n] = node->getNumber();
00239           }
00240 
00241           CHK_ERR( feData_->setConnectivity(elemBlockIDs[i], elem,
00242                 numConnectedNodes,
00243                 nodeNumPtr, numDofPtr, dof_ids_ptr));
00244         }//end for(...numElems...)
00245       }//end for(...numElemBlocks...)
00246 
00247       delete [] intData;
00248 
00249       setStructure_ = true;
00250       return(0);
00251     }
00252 
00253     fei::SharedPtr<FiniteElementData> feData_;
00254     fei::SharedPtr<fei::MatrixGraph> matrixGraph_;
00255 
00256     int nodeIDType_;
00257     bool setStructure_;
00258     bool setMatrixMatrixGraph_;
00259 
00260     fei::Lookup_Impl* lookup_;
00261   };//class Broker_FEData
00262 }//namespace snl_fei
00263 
00264 
00265 #endif // _snl_fei_Broker_FEData_hpp_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends