Sandia Home Sandia Home
Main Page | Publications | Downloads | Configuration | Running the Code | Solver Parameters | FAQ | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

APPSPACK_Utils.cpp

Go to the documentation of this file.
00001 // $Id: APPSPACK_Utils.cpp,v 1.24 2006/06/30 01:42:08 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Utils.cpp,v $ 
00003 
00004 //@HEADER
00005 // ************************************************************************
00006 // 
00007 //          APPSPACK: Asynchronous Parallel Pattern Search
00008 //                 Copyright (2003) Sandia Corporation
00009 // 
00010 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00011 // license for use of this work by or on behalf of the U.S. Government.
00012 // 
00013 // This library is free software; you can redistribute it and/or modify
00014 // it under the terms of the GNU Lesser General Public License as
00015 // published by the Free Software Foundation; either version 2.1 of the
00016 // License, or (at your option) any later version.
00017 //  
00018 // This library is distributed in the hope that it will be useful, but
00019 // WITHOUT ANY WARRANTY; without even the implied warranty of
00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 // Lesser General Public License for more details.
00022 //                                                                                 
00023 // You should have received a copy of the GNU Lesser General Public
00024 // License along with this library; if not, write to the Free Software
00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00026 // USA.                                                                           .
00027 // 
00028 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov) 
00029 // 
00030 // ************************************************************************
00031 //@HEADER
00032 
00038 #include "APPSPACK_Utils.hpp"
00039 #include "APPSPACK_Common.hpp"
00040 #include "APPSPACK_Matrix.hpp"
00041 #include "APPSPACK_Float.hpp"
00042 
00043 #ifndef SNL_TFLOPS_ENV
00044 
00045 bool APPSPACK::getNextQuotedString(const string& line, string::size_type& pos, string& value)
00046 {
00047   // Initialize value
00048   value = "";
00049 
00050   // Compute the length of the line
00051   string::size_type linelength = line.length();
00052 
00053   // Find the location of the first quote
00054   string::size_type pos1 = line.find('"', pos); 
00055   
00056   // Check that the operation succeeded and that we're not at the end of the line
00057   if ((pos1 == string::npos) || (pos1 == linelength - 1))
00058   {
00059     pos = string::npos;
00060     return false;
00061   }
00062 
00063   // Advance to first character after the first quote
00064   pos1 = pos1 + 1;
00065 
00066   // Find the location of the second quote
00067   string::size_type pos2 = line.find('"', pos1); 
00068 
00069   // Check that the operation was successful
00070   if (pos2 == string::npos)
00071   {
00072     pos = string::npos;
00073     return false;
00074   }
00075 
00076   // Compute the length of the expression
00077   string::size_type length = pos2 - pos1;
00078 
00079   // Compute the final position
00080   pos = (pos2 == (linelength - 1)) ? string::npos : pos2 + 1;
00081 
00082   // Extract the substring
00083   value = line.substr(pos1, length);
00084 
00085   // Return true
00086   return true;
00087 }
00088 
00089 bool APPSPACK::getNextString(const string& line, string::size_type& pos, string& value)
00090 {
00091   // Initialize value
00092   value = "";
00093 
00094   // Compute the length of the line
00095   string::size_type linelength = line.length();
00096 
00097   // Find the location of the first non-space character
00098   string::size_type pos1 = line.find_first_not_of(' ', pos); 
00099   
00100   // Check that the operation succeeded 
00101   if (pos1 == string::npos) 
00102   {
00103     pos = string::npos;
00104     return false;
00105   }
00106 
00107   // Find the location of the next space, if any
00108   string::size_type pos2 = line.find(' ', pos1); 
00109 
00110   // Compute the length of the expression
00111   string::size_type length = (pos2 == string::npos) ? linelength - pos1 : pos2 - pos1;
00112 
00113   // Compute the final position
00114   pos = (pos2 == (linelength - 1)) ? string::npos : pos2 + 1;
00115 
00116   // Extract the substring
00117   value = line.substr(pos1, length);
00118 
00119   // Return true
00120   return true;
00121 }
00122 
00123 bool APPSPACK::getNextInt(const string& line, string::size_type& pos, int& value)
00124 {
00125   string field;
00126   if ((!getNextString(line,pos,field)) || (field.size() == 0))
00127     return false;
00128 
00129   return ((sscanf(field.c_str(), "%d", &value)) == 1);
00130 }
00131 
00132 bool APPSPACK::getNextDouble(const string& line, string::size_type& pos, double& value)
00133 {
00134   string field;
00135   if ((!getNextString(line,pos,field)) || (field.size() == 0))
00136     return false;
00137 
00138   return ((sscanf(field.c_str(), "%le", &value)) == 1);
00139 }
00140 
00141 bool APPSPACK::processTextInputFileLine(const string& line, 
00142                                         Parameter::List& params, 
00143                                         Parameter::List*& subPtr,
00144                                         ifstream &fin)
00145 {
00146   
00147   string::size_type pos;        // current position inside line
00148 
00149   string field;                 // name of parameter
00150 
00151   bool tmpbool;                 // for reading in a boolean
00152   int tmpint;                   // for reading in an int
00153   double tmpdouble;             // for reading in a double
00154   string tmpstring;             // for reading in a string
00155   Vector tmpvector;             // for reading in an APPSPACK::Vector
00156 
00157   string type;                  // parameter type 
00158 
00159   if (line.size() == 0)         // empty line - which is okay
00160   {
00161     return true;
00162   }
00163   else if (line[0] == '#')      // comment - which is okay
00164   {
00165     return true;
00166   }
00167   else if (line[0] == '@')      // sublist command
00168   {
00169     subPtr = &params;           // reset to the top of the list (no nesting!)
00170     
00171     pos = 0;
00172     
00173     while (pos != string::npos)
00174     {
00175       // reset to sublist pointer if this is opening a new sublist
00176       if ((getNextQuotedString(line, pos, field)) && (field.size() > 0))
00177         subPtr = &(subPtr->sublist(field));
00178     }
00179 
00180     return true;
00181   }
00182   else if (line[0] == '"')      // new style
00183   {
00184 
00185     // Get the name
00186     pos = 0;
00187     if ((!getNextQuotedString(line, pos, field)) || (field.empty()))
00188       return false;
00189     
00190     // Read in the type
00191     if (!getNextString(line, pos, type))
00192       return false;
00193 
00194     if (type == "int")
00195     {
00196       if (!getNextInt(line, pos, tmpint))
00197       {
00198         return false;
00199       }
00200       else
00201       {
00202         subPtr->setParameter(field, tmpint);
00203         return true;
00204       }
00205     }
00206     else if (type == "bool")
00207     {
00208       if (!getNextString(line, pos, tmpstring))
00209       {
00210         return false;
00211       }
00212       else
00213       {
00214         subPtr->setParameter(field, (tmpstring == "true") );
00215         return true;
00216       }
00217     }
00218     else if (type == "double")
00219     {
00220       if (!getNextDouble(line, pos, tmpdouble))
00221       {
00222         return false;
00223       }
00224       else
00225       {
00226         subPtr->setParameter(field, tmpdouble);
00227         return true;
00228       }
00229     }
00230     else if (type == "string")
00231     {
00232       if ((!getNextQuotedString(line, pos, tmpstring)) || (tmpstring.empty()))
00233       {
00234         return false;
00235       }
00236       else
00237       {
00238         subPtr->setParameter(field, tmpstring);
00239         return true;
00240       }
00241     }
00242     else if (type == "vector")
00243     {
00244       // get the size
00245       if (!getNextInt(line, pos, tmpint))
00246         return false;
00247       
00248       if (tmpint < 0)
00249         return false;
00250       
00251       tmpvector.resize(tmpint);
00252 
00253       // Split remainder of line.
00254       string vecline;
00255       vecline = line.substr(pos);
00256       vector<string> linesplit;
00257       tokenize(vecline, linesplit);
00258 
00259       // Check that there is sufficient data present.
00260       if (linesplit.size() != tmpint)
00261         return false;
00262 
00263       // Add in vector, checking for infinity denoted by DNE.
00264       for (int i = 0; i < tmpint; i ++)
00265       {
00266         if (linesplit[i] == "DNE")
00267           tmpvector[i] = dne();
00268         else if (sscanf(linesplit[i].c_str(), "%le", &tmpvector[i]) != 1)
00269           return false;
00270       }
00271     
00272       subPtr->setParameter(field, tmpvector);   
00273       return true;
00274     }
00275     else if (type == "matrix")
00276     {
00277       int nrows;
00278       int ncols;
00279 
00280       // get number of rows
00281       if (!getNextInt(line, pos, nrows))
00282         return false;
00283 
00284       // get number of columns
00285       if (!getNextInt(line, pos, ncols))
00286         return false;
00287       
00288       if ( (nrows <= 0) || (ncols <= 0) )
00289         return false;
00290 
00291       Matrix mat;
00292       tmpvector.resize(ncols);
00293   
00294       // Grab data for matrix.
00295       string matline;
00296       vector<string> linesplit;
00297       for (int i = 0; i < nrows; i++)
00298       {
00299         // Process row i of matrix.
00300         if (fin.eof())
00301         {
00302           return false;
00303         }
00304 
00305         getline(fin, matline);
00306 
00307         // Split line.
00308         tokenize(matline, linesplit);     
00309         if (linesplit.size() != ncols)
00310         {
00311           //cerr << "Problem with row " << i+1 << " of matrix \"" << field << "\": " << endl;
00312           //cerr << linesplit.size() << " entries present.  Should be " << ncols << "." << endl;
00313           return false;
00314         }
00315 
00316         // Copy into matrix.
00317         for (int j = 0; j < ncols; j++)
00318         {
00319           if (linesplit[i] == "DNE")
00320             tmpvector[i] = dne();
00321           else if ((sscanf(linesplit[j].c_str(), "%le", &tmpvector[j])) != 1)
00322             return false;
00323         }
00324         mat.addRow(tmpvector);
00325       }      
00326 
00327       subPtr->setParameter(field, mat); 
00328       return true;
00329     }
00330     else
00331     {
00332       return false;
00333     }
00334 
00335   } 
00336   else
00337   {
00338     return false;
00339   } 
00340 
00341   return false;
00342 }
00343 
00344 bool APPSPACK::parseTextInputFile(const string filename, Parameter::List& params)
00345 {
00346   // Open the input file
00347   ifstream fin;
00348   fin.open(filename.c_str());
00349 
00350   if (!fin) 
00351   {
00352     cout << "Error: Cannot find input file " << filename << endl;
00353     return false;
00354   }
00355 
00356   string line;                  // one line from input file
00357   Parameter::List* subPtr;      // sublist pointer
00358 
00359   subPtr = &params;
00360 
00361  while (!fin.eof())
00362   {
00363     getline(fin, line);
00364     if (!processTextInputFileLine(line, params, subPtr,fin))
00365     {
00366       cout << "Ignoring input file line: " << line << endl;
00367     }
00368 
00369   }
00370 
00371   fin.close();
00372 
00373   return true;
00374 }
00375 
00376 void APPSPACK::tokenize(const string& line, vector<string>& linesplit)
00377 {
00378   linesplit.resize(0);
00379   string buf; // Have a buffer string
00380   stringstream ss(line); // Insert the string into a stream
00381   while (ss >> buf)
00382     linesplit.push_back(buf);
00383 }
00384 
00385 #endif
00386 
00387 

 

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Fri Feb 16 10:33:35 2007 for APPSPACK 5.0.1 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2002