FEI Version of the Day
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   int nodeType = 0;
00136   snl_fei::RecordCollection* nodeRecords = NULL;
00137   matrixGraph.getRowSpace()->getRecordCollection(nodeType, nodeRecords);
00138 
00139   for(; iter != iter_end; ++iter) {
00140     fei::ConnectivityBlock* cblk = iter->second;
00141 
00142     //if not symmetric, then assume it's not an element-block.
00143     if (!cblk->isSymmetric()) continue;
00144 
00145     fei::Pattern* pattern = cblk->getRowPattern();
00146 
00147     int ne = cblk->getConnectivityIDs().size();
00148     int nn = pattern->getNumIDs();
00149     std::vector<int>& cblk_nodes = cblk->getRowConnectivities();
00150     for(unsigned i=0; i<cblk_nodes.size(); ++i) {
00151       nodes[node_offset++] = nodeRecords->getRecordWithLocalID(cblk_nodes[i])->getID();
00152     }
00153 
00154     for(int i=0; i<ne; ++i) {
00155       elem_offsets[elem_counter++] = elem_offset;
00156       elem_offset += nn;
00157     }
00158   }
00159 
00160   elem_offsets[elem_counter] = elem_offset;
00161 }
00162 
00163 //----------------------------------------------------------------------------
00164 void fei::utils::char_ptrs_to_strings(int numStrings,
00165                                      const char*const* charstrings,
00166                                      std::vector<std::string>& stdstrings)
00167 {
00168   stdstrings.resize(0);
00169   for(int i=0; i<numStrings; ++i) {
00170     if (charstrings[i] != NULL) {
00171       std::string tempstr(charstrings[i]);
00172       stdstrings.push_back(tempstr);
00173     }
00174   }
00175 }
00176 
00177 //----------------------------------------------------------------------------
00178 void fei::utils::strings_to_char_ptrs(std::vector<std::string>& stdstrings,
00179                                       int& numStrings,
00180                                       const char**& charPtrs)
00181 {
00182   numStrings = stdstrings.size();
00183   charPtrs = numStrings > 0 ? new const char*[numStrings] : NULL;
00184 
00185   for(int i=0; i<numStrings; ++i) {
00186     charPtrs[i] = stdstrings[i].c_str();
00187   }
00188 }
00189 
00190 //----------------------------------------------------------------------------
00191 void fei::utils::parse_strings(std::vector<std::string>& stdstrings,
00192                               const char* separator_string,
00193                               fei::ParameterSet& paramset)
00194 {
00195   std::vector<std::string>::iterator
00196     s_iter = stdstrings.begin(),
00197     s_end = stdstrings.end();
00198 
00199   int intval = 0;
00200   double doubleval = 0.0;
00201   const char* charstring_key = NULL;
00202   const char* charstring_val = NULL;
00203   int key_len = 0;
00204   int val_len = 0;
00205 
00206   for(; s_iter != s_end; ++s_iter) {
00207     snl_fei::separate_string((*s_iter).c_str(), separator_string,
00208                     charstring_key, key_len, charstring_val, val_len);
00209     if (key_len == 0) {
00210       continue;
00211     }
00212 
00213     std::string keystr(charstring_key, key_len);
00214 
00215     if (val_len == 0) {
00216       fei::Param vparam(keystr.c_str(), true);
00217       paramset.add(vparam, false);
00218       continue;
00219     }
00220 
00221     std::string valstr(charstring_val);
00222 
00223     if (valstr == "true" || valstr == "True" || valstr == "TRUE") {
00224       fei::Param bparam(keystr.c_str(), true);
00225       paramset.add(bparam, false);
00226       continue;
00227     }
00228 
00229     if (valstr == "false" || valstr == "False" || valstr == "FALSE") {
00230       fei::Param bparam(keystr.c_str(), false);
00231       paramset.add(bparam, false);
00232       continue;
00233     }
00234 
00235     FEI_ISTRINGSTREAM isstr(valstr);
00236 
00237     //Does charstring_val contain a floating-point value?
00238     //If so, we'll store it as a double.
00239     std::string::size_type i = valstr.find(".");
00240     std::string::size_type valstrsize = valstr.size();
00241 
00242     if (i < valstrsize) {
00243       isstr >> doubleval;
00244       if (!isstr.fail()) {
00245         fei::Param dparam(keystr.c_str(), doubleval);
00246         paramset.add(dparam, false);
00247         continue;
00248       }
00249       isstr.clear();
00250     }
00251 
00252     //Does charstring_val contain an int?
00253     isstr >> intval;
00254     if (!isstr.fail()) {
00255       fei::Param iparam(keystr.c_str(), intval);
00256       paramset.add(iparam, false);
00257       continue;
00258     }
00259     isstr.clear();
00260 
00261     //If charstring_val doesn't contain an int or a real, we'll just store
00262     //it as a string.
00263     fei::Param sparam(keystr.c_str(), charstring_val);
00264     paramset.add(sparam, false);
00265   }
00266 }
00267 
00268 //----------------------------------------------------------------------------
00269 void
00270 fei::utils::convert_ParameterSet_to_strings(const fei::ParameterSet* paramset,
00271                                         std::vector<std::string>& paramStrings)
00272 {
00273   paramStrings.resize(0);
00274 
00275   fei::ParameterSet::const_iterator
00276     iter = paramset->begin(),
00277     iter_end = paramset->end();
00278 
00279   for(; iter != iter_end; ++iter) {
00280     const fei::Param& param = *iter;
00281     fei::Param::ParamType ptype = param.getType();
00282 
00283     FEI_OSTRINGSTREAM osstr;
00284     osstr << param.getName();
00285 
00286     switch(ptype) {
00287     case fei::Param::STRING:
00288       osstr << " " << param.getStringValue();
00289       break;
00290     case fei::Param::DOUBLE:
00291       osstr << " " << param.getDoubleValue();
00292       break;
00293     case fei::Param::INT:
00294       osstr << " " << param.getIntValue();
00295       break;
00296     case fei::Param::VOID:
00297       break;
00298     case fei::Param::BOOL:
00299       if (param.getBoolValue()) osstr << " true";
00300       else osstr << " false";
00301       break;
00302     default:
00303       break;
00304     }
00305 
00306     paramStrings.push_back(osstr.str());
00307   }
00308 }
00309 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends