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       bool zeroSharedRows = false;
00114       fei::SharedPtr<fei::Matrix> matptr;
00115       matptr.reset(new fei::Matrix_Impl<FiniteElementData>(feData_,
00116             matrixGraph_, localsize, zeroSharedRows));
00117       return(matptr);
00118     }
00119 
00122     virtual fei::SharedPtr<fei::LinearSystem> createLinearSystem()
00123       {
00124   fei::SharedPtr<fei::LinearSystem> lsptr;
00125   if (matrixGraph_.get() == NULL) return(lsptr);
00126 
00127   if (setStructure() != 0) return(lsptr);
00128 
00129   snl_fei::LinearSystem_FEData*
00130     linsysfed = new LinearSystem_FEData(feData_,
00131                 matrixGraph_);
00132   linsysfed->setLookup(lookup_);
00133   fei::SharedPtr<fei::LinearSystem> linsysptr(linsysfed);
00134   return(linsysptr);
00135       }
00136 
00138     virtual void setMatrixGraph(fei::SharedPtr<fei::MatrixGraph> matrixGraph)
00139     {
00140       matrixGraph_ = matrixGraph;
00141     }
00142 
00143   private:
00144     int setStructure()
00145     {
00146       if (matrixGraph_.get() == NULL) ERReturn(-1);
00147       if (setStructure_ == true) return(0);
00148 
00149       lookup_ = new fei::Lookup_Impl(matrixGraph_, nodeIDType_);
00150 
00151       CHK_ERR( feData_->setLookup(*lookup_) );
00152 
00153       fei::SharedPtr<fei::VectorSpace> vspace = matrixGraph_->getRowSpace();
00154 
00155       int numLocalNodes = vspace->getNumOwnedAndSharedIDs(nodeIDType_);
00156 
00157       int numElemBlocks = matrixGraph_->getNumConnectivityBlocks();
00158 
00159       int nodeType = 0;
00160       snl_fei::RecordCollection* nodeRecords = NULL;
00161       vspace->getRecordCollection(nodeType, nodeRecords);
00162 
00163       int* intData = new int[numElemBlocks*3];
00164       int* numElemsPerBlock =       intData;
00165       int* numNodesPerElem =        intData+numElemBlocks;
00166       int* elemMatrixSizePerBlock = intData+2*numElemBlocks;
00167       int i;
00168 
00169       std::vector<int> elemBlockIDs;
00170       CHK_ERR( matrixGraph_->getConnectivityBlockIDs( elemBlockIDs) );
00171 
00172       for(i=0; i<numElemBlocks; ++i) {
00173         const fei::ConnectivityBlock* cblock =
00174           matrixGraph_->getConnectivityBlock(elemBlockIDs[i]);
00175         if (cblock==NULL) return(-1);
00176         numElemsPerBlock[i] = cblock->getConnectivityIDs().size();
00177         numNodesPerElem[i] = cblock->getRowPattern()->getNumIDs();
00178         elemMatrixSizePerBlock[i] = cblock->getRowPattern()->getNumIndices();
00179       }
00180 
00181       int numSharedNodes = 0;
00182       CHK_ERR( vspace->getNumSharedIDs(nodeIDType_, numSharedNodes) );
00183 
00184       int numLagrangeConstraints = matrixGraph_->getLocalNumLagrangeConstraints();
00185 
00186       CHK_ERR( feData_->describeStructure(numElemBlocks,
00187             numElemsPerBlock,
00188             numNodesPerElem,
00189             elemMatrixSizePerBlock,
00190             numLocalNodes,
00191             numSharedNodes,
00192             numLagrangeConstraints) );
00193 
00194       std::map<int,fei::ConnectivityBlock*>::const_iterator
00195         cdb_iter = matrixGraph_->getConnectivityBlocks().begin();
00196 
00197       std::vector<int> nodeNumbers, numDofPerNode, dof_ids;
00198       int total_num_dof = 0;
00199       for(i=0; i<numElemBlocks; ++i, ++cdb_iter) {
00200         fei::ConnectivityBlock* cblock = (*cdb_iter).second;
00201         fei::Pattern* pattern = cblock->getRowPattern();
00202 
00203         int numConnectedNodes = pattern->getNumIDs();
00204         nodeNumbers.resize(numConnectedNodes);
00205         numDofPerNode.resize(numConnectedNodes);
00206         int* nodeNumPtr = &nodeNumbers[0];
00207         int* numDofPtr = &numDofPerNode[0];
00208 
00209         //For the calls to FiniteElementData::setConnectivity, we're going to
00210         //need a list of num-dof-per-node. So construct that now.
00211         const int* numFieldsPerID = pattern->getNumFieldsPerID();
00212         const int* fieldIDs = pattern->getFieldIDs();
00213 
00214         int foffset = 0;
00215         for(int ii=0; ii<numConnectedNodes; ++ii) {
00216           int dof = 0;
00217           for(int f=0; f<numFieldsPerID[ii]; ++f) {
00218             dof += vspace->getFieldSize(fieldIDs[foffset++]);
00219           }
00220           numDofPtr[ii] = dof;
00221           total_num_dof += dof;
00222         }
00223 
00224         dof_ids.resize(total_num_dof, 0);
00225         int* dof_ids_ptr = &dof_ids[0];
00226 
00228         //Next we'll loop over the connectivity-lists in this block,
00229         //making a call to FiniteElementData::setConnectivity for each one.
00230 
00231         int numElems = cblock->getConnectivityIDs().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