Main Page | Namespace List | Class Hierarchy | Alphabetical List | 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.17.2.2 2005/06/29 17:49:35 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
    00041
    00042 #ifndef SNL_TFLOPS_ENV
    00043
    00044 bool APPSPACK::getNextQuotedString(const string& line, string::size_type& pos, string& value)
    00045 {
    00046   // Initialize value
    00047   value = "";
    00048
    00049   // Compute the length of the line
    00050   string::size_type linelength = line.length();
    00051
    00052   // Find the location of the first quote
    00053   string::size_type pos1 = line.find('"', pos);
    00054
    00055   // Check that the operation succeeded and that we're not at the end of the line
    00056   if ((pos1 == string::npos) || (pos1 == linelength - 1))
    00057   {
    00058     pos = string::npos;
    00059     return false;
    00060   }
    00061
    00062   // Advance to first character after the first quote
    00063   pos1 = pos1 + 1;
    00064
    00065   // Find the location of the second quote
    00066   string::size_type pos2 = line.find('"', pos1);
    00067
    00068   // Check that the operation was successful
    00069   if (pos2 == string::npos)
    00070   {
    00071     pos = string::npos;
    00072     return false;
    00073   }
    00074
    00075   // Compute the length of the expression
    00076   string::size_type length = pos2 - pos1;
    00077
    00078   // Compute the final position
    00079   pos = (pos2 == (linelength - 1)) ? string::npos : pos2 + 1;
    00080
    00081   // Extract the substring
    00082   value = line.substr(pos1, length);
    00083
    00084   // Return true
    00085   return true;
    00086 }
    00087
    00088 bool APPSPACK::getNextString(const string& line, string::size_type& pos, string& value)
    00089 {
    00090   // Initialize value
    00091   value = "";
    00092
    00093   // Compute the length of the line
    00094   string::size_type linelength = line.length();
    00095
    00096   // Find the location of the first non-space character
    00097   string::size_type pos1 = line.find_first_not_of(' ', pos);
    00098
    00099   // Check that the operation succeeded
    00100   if (pos1 == string::npos)
    00101   {
    00102     pos = string::npos;
    00103     return false;
    00104   }
    00105
    00106   // Find the location of the next space, if any
    00107   string::size_type pos2 = line.find(' ', pos1);
    00108
    00109   // Compute the length of the expression
    00110   string::size_type length = (pos2 == string::npos) ? linelength - pos1 : pos2 - pos1;
    00111
    00112   // Compute the final position
    00113   pos = (pos2 == (linelength - 1)) ? string::npos : pos2 + 1;
    00114
    00115   // Extract the substring
    00116   value = line.substr(pos1, length);
    00117
    00118   // Return true
    00119   return true;
    00120 }
    00121
    00122 bool APPSPACK::getNextInt(const string& line, string::size_type& pos, int& value)
    00123 {
    00124   string field;
    00125   if ((!getNextString(line,pos,field)) || (field.size() == 0))
    00126     return false;
    00127
    00128   return ((sscanf(field.c_str(), "%d", &value)) == 1);
    00129 }
    00130
    00131 bool APPSPACK::getNextDouble(const string& line, string::size_type& pos, double& value)
    00132 {
    00133   string field;
    00134   if ((!getNextString(line,pos,field)) || (field.size() == 0))
    00135     return false;
    00136
    00137   return ((sscanf(field.c_str(), "%le", &value)) == 1);
    00138 }
    00139
    00140 bool APPSPACK::processTextInputFileLine(const string& line, Parameter::List& params, Parameter::List*& subPtr)
    00141 {
    00142
    00143   string::size_type pos;        // current position inside line
    00144
    00145   string field;                 // name of parameter
    00146
    00147   bool tmpbool;                 // for reading in a boolean
    00148   int tmpint;                   // for reading in an int
    00149   double tmpdouble;             // for reading in a double
    00150   string tmpstring;             // for reading in a string
    00151   Value tmpvalue;               // for reading in an APPSPACK::Value
    00152   Vector tmpvector;             // for reading in an APPSPACK::Vector
    00153
    00154   string type;                  // parameter type (used in new style)
    00155
    00156   static int n = -1;                    // size of vector (used in old style)
    00157   char c;                       // used to read in '=' character in old style
    00158
    00159   if (line.size() == 0)         // empty line
    00160   {
    00161     return false;
    00162   }
    00163   else if (line[0] == '#')      // comment
    00164   {
    00165     return false;
    00166   }
    00167   else if (line[0] == '@')      // sublist command
    00168   {
    00169     subPtr = &params;
    00170
    00171     pos = 0;
    00172
    00173     while (pos != string::npos)
    00174     {
    00175       if ((getNextQuotedString(line, pos, field)) && (field.size() > 0))
    00176         subPtr = &(subPtr->sublist(field));
    00177     }
    00178
    00179     return true;
    00180   }
    00181   else if (line[0] == '"')      // new style
    00182   {
    00183
    00184     // Get the name
    00185     pos = 0;
    00186     if ((!getNextQuotedString(line, pos, field)) || (field.empty()))
    00187       return false;
    00188
    00189     // Read in the type
    00190     if (!getNextString(line, pos, type))
    00191       return false;
    00192
    00193     if (type == "int")
    00194     {
    00195       if (!getNextInt(line, pos, tmpint))
    00196       {
    00197         return false;
    00198       }
    00199       else
    00200       {
    00201         subPtr->setParameter(field, tmpint);
    00202         return true;
    00203       }
    00204     }
    00205     else if (type == "bool")
    00206     {
    00207       if (!getNextString(line, pos, tmpstring))
    00208       {
    00209         return false;
    00210       }
    00211       else
    00212       {
    00213         subPtr->setParameter(field, (tmpstring == "true") );
    00214         return true;
    00215       }
    00216     }
    00217     else if (type == "double")
    00218     {
    00219       if (!getNextDouble(line, pos, tmpdouble))
    00220       {
    00221         return false;
    00222       }
    00223       else
    00224       {
    00225         subPtr->setParameter(field, tmpdouble);
    00226         return true;
    00227       }
    00228     }
    00229     else if (type == "string")
    00230     {
    00231       if ((!getNextQuotedString(line, pos, tmpstring)) || (tmpstring.empty()))
    00232       {
    00233         return false;
    00234       }
    00235       else
    00236       {
    00237         subPtr->setParameter(field, tmpstring);
    00238         return true;
    00239       }
    00240     }
    00241     else if (type == "vector")
    00242     {
    00243       // get the size
    00244       if (!getNextInt(line, pos, tmpint))
    00245         return false;
    00246
    00247       if (tmpint < 0)
    00248         return false;
    00249
    00250       tmpvector.resize(tmpint);
    00251       for (int i = 0; i < tmpint; i ++)
    00252       {
    00253         if (!getNextDouble(line, pos, tmpvector[i]))
    00254           return false;
    00255       }
    00256
    00257       subPtr->setParameter(field, tmpvector);
    00258       return true;
    00259     }
    00260     else
    00261       return false;
    00262
    00263   } // end new style
    00264
    00265   else                          // old style
    00266   {
    00267     istringstream linein(line);
    00268     linein >> field;
    00269
    00270     if ((field == "n_parameters") || (field == "continuous_design"))
    00271     {
    00272       linein >> c;
    00273       linein >> n;
    00274     }
    00275
    00276     else if ((field == "initial_point") || (field == "cdv_initial_point"))
    00277     {
    00278       if (n == -1)
    00279       {
    00280         cout << "Error: initial_point specified before n_parameters " << endl;
    00281         return false;
    00282       }
    00283
    00284       linein >> c;
    00285       tmpvector.resize(n);
    00286       for (int i = 0; i < n; i ++)
    00287         linein >> tmpvector[i];
    00288       params.sublist("Solver").setParameter("Initial X", tmpvector);
    00289     }
    00290
    00291     else if (field == "initial_f")
    00292     {
    00293       linein >> c;
    00294       linein >> tmpdouble;
    00295       tmpvalue.setValueTo(true, tmpdouble);
    00296       params.sublist("Solver").setParameter("Initial F", tmpvalue);
    00297     }
    00298
    00299     else if ((field == "lower_bounds") || (field == "cdv_lower_bounds"))
    00300     {
    00301       if (n == -1)
    00302       {
    00303         cout << "Error:lower_bounds specified before n_parameters " << endl;
    00304         return false;
    00305       }
    00306
    00307       linein >> c;
    00308       tmpvector.resize(n);
    00309       for (int i = 0; i < n; i ++)
    00310         linein >> tmpvector[i];
    00311       params.sublist("Bounds").setParameter("Lower", tmpvector);
    00312
    00313     }
    00314
    00315     else if ((field == "upper_bounds") || (field == "cdv_upper_bounds"))
    00316     {
    00317       if (n == -1)
    00318       {
    00319         cout << "Error:upper_bounds specified before n_parameters " << endl;
    00320         return false;
    00321       }
    00322
    00323       linein >> c;
    00324       tmpvector.resize(n);
    00325       for (int i = 0; i < n; i ++)
    00326         linein >> tmpvector[i];
    00327       params.sublist("Bounds").setParameter("Upper", tmpvector);
    00328
    00329     }
    00330
    00331     else if (field == "executable")
    00332     {
    00333       linein >> c;
    00334       linein >> tmpstring;
    00335       params.sublist("Evaluator").setParameter("Executable Name", tmpstring);
    00336     }
    00337
    00338     else if (field == "params_prefix")
    00339     {
    00340       linein >> c;
    00341       linein >> tmpstring;
    00342       params.sublist("Evaluator").setParameter("Input Prefix", tmpstring);
    00343     }
    00344
    00345     else if (field == "result_prefix")
    00346     {
    00347       linein >> c;
    00348       linein >> tmpstring;
    00349       params.sublist("Evaluator").setParameter("Output Prefix", tmpstring);
    00350     }
    00351
    00352     else
    00353     {
    00354       cout << "Ignoring unrecognized field." << field << "\n";
    00355     }
    00356
    00357   } /* old style */
    00358
    00359   return true;
    00360 }
    00361
    00362 bool APPSPACK::parseTextInputFile(const string filename, Parameter::List& params)
    00363 {
    00364   // Open the input file
    00365   ifstream fin;
    00366   fin.open(filename.c_str());
    00367
    00368   if (!fin)
    00369   {
    00370     cout << "Error: Cannot find input file " << filename << endl;
    00371     return false;
    00372   }
    00373
    00374   string line;                  // one line from input file
    00375   Parameter::List* subPtr;      // sublist pointer
    00376
    00377   subPtr = &params;
    00378
    00379   while (!fin.eof())
    00380   {
    00381     getline(fin, line);
    00382     if (!processTextInputFileLine(line, params, subPtr))
    00383     {
    00384       // cout << "Ignoring input file line: " << line << endl;
    00385     }
    00386
    00387   }
    00388
    00389   fin.close();
    00390
    00391   return true;
    00392 }
    00393
    00394
    00395 #endif
    00396
    00397 APPSPACK::Vector APPSPACK::createZeroVector(int n)
    00398 {
    00399   Vector z;
    00400   z.resize(n);
    00401   for (int i = 0; i < n ; i ++)
    00402     z[i] = 0;
    00403   return z;
    00404 }
    00405

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Wed Dec 14 18:41:05 2005 for APPSPACK 4.0.2 by 1.3.8 written by Dimitri van Heesch, © 1997-2002