fei_utils.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 #include "fei_fstream.hpp"
00009 
00010 #include "fei_utils.hpp"
00011 #include "snl_fei_Utils.hpp"
00012 #include "fei_MatrixReducer.hpp"
00013 #include "fei_Matrix_Impl.hpp"
00014 #include "fei_LinearSystemCore.hpp"
00015 #include "fei_ParameterSet.hpp"
00016 
00017 #include "fei_version.h"
00018 
00019 #ifdef FEI_HAVE_TIME_H
00020 #include <time.h>
00021 #endif
00022 
00023 //----------------------------------------------------------------------------
00024 const char* fei_VERSION::version()
00025 {
00026   static int times_called = 0;
00027 
00028   static std::string static_fei_version_string;
00029 
00030   if (times_called == 0) {
00031     FEI_OSTRINGSTREAM osstr;
00032 
00033     osstr << FEI_MAJOR_VERSION << "."
00034           << FEI_MINOR_VERSION << "."
00035           << FEI_PATCH_VERSION;
00036 
00037     static_fei_version_string = osstr.str();
00038 
00039     times_called = 1;
00040   }
00041 
00042   return( static_fei_version_string.c_str() );
00043 }
00044 
00045 //----------------------------------------------------------------------------
00046 double fei::utils::cpu_time()
00047 {
00048   double cpu_seconds = 0.0;
00049 
00050 #ifdef FEI_HAVE_TIME_H
00051   cpu_seconds = (1.0*clock())/CLOCKS_PER_SEC;
00052 #endif
00053 
00054   return(cpu_seconds);
00055 }
00056 
00057 //----------------------------------------------------------------------------
00058 fei::OutputLevel fei::utils::string_to_output_level(const std::string& str)
00059 {
00060   if (str == "STATS" || str == "fei::STATS") {
00061     return fei::STATS;
00062   }
00063   else if (str == "BRIEF_LOGS" || str == "fei::BRIEF_LOGS") {
00064     return fei::BRIEF_LOGS;
00065   }
00066   else if (str == "MATRIX_FILES" || str == "fei::MATRIX_FILES") {
00067     return fei::MATRIX_FILES;
00068   }
00069   else if (str == "FULL_LOGS" || str == "fei::FULL_LOGS") {
00070     return fei::FULL_LOGS;
00071   }
00072   else if (str == "ALL" || str == "fei::ALL") {
00073     return fei::ALL;
00074   }
00075 
00076   return fei::NONE;
00077 }
00078 
00079 //----------------------------------------------------------------------------
00080 LinearSystemCore*
00081 fei::utils::get_LinearSystemCore(fei::Matrix* matrix)
00082 {
00083   fei::Matrix* matptr = matrix;
00084   fei::MatrixReducer* matred = dynamic_cast<fei::MatrixReducer*>(matptr);
00085   if (matred != NULL) matptr = matred->getTargetMatrix().get();
00086 
00087   fei::Matrix_Impl<LinearSystemCore>* mat_lsc =
00088     dynamic_cast<fei::Matrix_Impl<LinearSystemCore>*>(matptr);
00089 
00090   if (mat_lsc != NULL) {
00091     return mat_lsc->getMatrix().get();
00092   }
00093 
00094   return(NULL);
00095 }
00096 
00097 //----------------------------------------------------------------------------
00098 void fei::utils::getConnectivityArrays(fei::MatrixGraph& matrixGraph,
00099                                        std::vector<int>& nodes,
00100                                        std::vector<int>& elem_offsets)
00101 {
00102   //first, iterate over the connectivity-blocks and count how many nodes
00103   //and elements there are.
00104   int num_elems = 0;
00105   int num_nodes = 0;
00106 
00107   std::map<int,fei::ConnectivityBlock*>& cBlocks =
00108     matrixGraph.getConnectivityBlocks();
00109   std::map<int,fei::ConnectivityBlock*>::iterator
00110     iter = cBlocks.begin(),
00111     iter_end = cBlocks.end();
00112 
00113   for(; iter != iter_end; ++iter) {
00114     fei::ConnectivityBlock* cblk = iter->second;
00115 
00116     //if not symmetric, then assume it's not an element-block.
00117     if (!cblk->isSymmetric()) continue;
00118 
00119     num_elems += cblk->getConnectivityIDs().size();
00120 
00121     fei::Pattern* pattern = cblk->getRowPattern();
00122     num_nodes += num_elems*pattern->getNumIDs();
00123   }
00124 
00125   nodes.resize(num_nodes);
00126   elem_offsets.resize(num_elems+1);
00127 
00128   iter = cBlocks.begin(),
00129   iter_end = cBlocks.end();
00130 
00131   int node_offset = 0;
00132   int elem_offset = 0;
00133   int elem_counter = 0;
00134 
00135   for(; iter != iter_end; ++iter) {
00136     fei::ConnectivityBlock* cblk = iter->second;
00137 
00138     //if not symmetric, then assume it's not an element-block.
00139     if (!cblk->isSymmetric()) continue;
00140 
00141     fei::Pattern* pattern = cblk->getRowPattern();
00142 
00143     int ne = cblk->getConnectivityIDs().size();
00144     int nn = pattern->getNumIDs();
00145     std::vector<fei::Record*>& cblk_nodes = cblk->getRowConnectivities();
00146     for(unsigned i=0; i<cblk_nodes.size(); ++i) {
00147       nodes[node_offset++] = cblk_nodes[i]->getID();
00148     }
00149 
00150     for(int i=0; i<ne; ++i) {
00151       elem_offsets[elem_counter++] = elem_offset;
00152       elem_offset += nn;
00153     }
00154   }
00155 
00156   elem_offsets[elem_counter] = elem_offset;
00157 }
00158 
00159 //----------------------------------------------------------------------------
00160 void fei::utils::char_ptrs_to_strings(int numStrings,
00161                                      const char*const* charstrings,
00162                                      std::vector<std::string>& stdstrings)
00163 {
00164   stdstrings.resize(0);
00165   for(int i=0; i<numStrings; ++i) {
00166     if (charstrings[i] != NULL) {
00167       std::string tempstr(charstrings[i]);
00168       stdstrings.push_back(tempstr);
00169     }
00170   }
00171 }
00172 
00173 //----------------------------------------------------------------------------
00174 void fei::utils::strings_to_char_ptrs(std::vector<std::string>& stdstrings,
00175                                       int& numStrings,
00176                                       const char**& charPtrs)
00177 {
00178   numStrings = stdstrings.size();
00179   charPtrs = numStrings > 0 ? new const char*[numStrings] : NULL;
00180 
00181   for(int i=0; i<numStrings; ++i) {
00182     charPtrs[i] = stdstrings[i].c_str();
00183   }
00184 }
00185 
00186 //----------------------------------------------------------------------------
00187 void fei::utils::parse_strings(std::vector<std::string>& stdstrings,
00188                               const char* separator_string,
00189                               fei::ParameterSet& paramset)
00190 {
00191   std::vector<std::string>::iterator
00192     s_iter = stdstrings.begin(),
00193     s_end = stdstrings.end();
00194 
00195   int intval = 0;
00196   double doubleval = 0.0;
00197   const char* charstring_key = NULL;
00198   const char* charstring_val = NULL;
00199   int key_len = 0;
00200   int val_len = 0;
00201 
00202   for(; s_iter != s_end; ++s_iter) {
00203     snl_fei::separate_string((*s_iter).c_str(), separator_string,
00204                     charstring_key, key_len, charstring_val, val_len);
00205     if (key_len == 0) {
00206       continue;
00207     }
00208 
00209     std::string keystr(charstring_key, key_len);
00210 
00211     if (val_len == 0) {
00212       fei::Param vparam(keystr.c_str(), true);
00213       paramset.add(vparam, false);
00214       continue;
00215     }
00216 
00217     std::string valstr(charstring_val);
00218 
00219     if (valstr == "true" || valstr == "True" || valstr == "TRUE") {
00220       fei::Param bparam(keystr.c_str(), true);
00221       paramset.add(bparam, false);
00222       continue;
00223     }
00224 
00225     if (valstr == "false" || valstr == "False" || valstr == "FALSE") {
00226       fei::Param bparam(keystr.c_str(), false);
00227       paramset.add(bparam, false);
00228       continue;
00229     }
00230 
00231     FEI_ISTRINGSTREAM isstr(valstr);
00232 
00233     //Does charstring_val contain a floating-point value?
00234     //If so, we'll store it as a double.
00235     std::string::size_type i = valstr.find(".");
00236     std::string::size_type valstrsize = valstr.size();
00237 
00238     if (i < valstrsize) {
00239       isstr >> doubleval;
00240       if (!isstr.fail()) {
00241         fei::Param dparam(keystr.c_str(), doubleval);
00242         paramset.add(dparam, false);
00243         continue;
00244       }
00245       isstr.clear();
00246     }
00247 
00248     //Does charstring_val contain an int?
00249     isstr >> intval;
00250     if (!isstr.fail()) {
00251       fei::Param iparam(keystr.c_str(), intval);
00252       paramset.add(iparam, false);
00253       continue;
00254     }
00255     isstr.clear();
00256 
00257     //If charstring_val doesn't contain an int or a real, we'll just store
00258     //it as a string.
00259     fei::Param sparam(keystr.c_str(), charstring_val);
00260     paramset.add(sparam, false);
00261   }
00262 }
00263 
00264 //----------------------------------------------------------------------------
00265 void
00266 fei::utils::convert_ParameterSet_to_strings(const fei::ParameterSet* paramset,
00267                                         std::vector<std::string>& paramStrings)
00268 {
00269   paramStrings.resize(0);
00270 
00271   fei::ParameterSet::const_iterator
00272     iter = paramset->begin(),
00273     iter_end = paramset->end();
00274 
00275   for(; iter != iter_end; ++iter) {
00276     const fei::Param& param = *iter;
00277     fei::Param::ParamType ptype = param.getType();
00278 
00279     FEI_OSTRINGSTREAM osstr;
00280     osstr << param.getName();
00281 
00282     switch(ptype) {
00283     case fei::Param::STRING:
00284       osstr << " " << param.getStringValue();
00285       break;
00286     case fei::Param::DOUBLE:
00287       osstr << " " << param.getDoubleValue();
00288       break;
00289     case fei::Param::INT:
00290       osstr << " " << param.getIntValue();
00291       break;
00292     case fei::Param::VOID:
00293       break;
00294     case fei::Param::BOOL:
00295       if (param.getBoolValue()) osstr << " true";
00296       else osstr << " false";
00297       break;
00298     default:
00299       break;
00300     }
00301 
00302     paramStrings.push_back(osstr.str());
00303   }
00304 }
00305 

Generated on Wed May 12 21:30:41 2010 for FEI by  doxygen 1.4.7