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

example3.cpp

Go to the documentation of this file.
00001 /* $Id: example3.cpp,v 1.2 2006/06/30 01:07:26 tgkolda Exp $ */
00002 /* $Source: /space/CVS-Acro/acro/packages/appspack/appspack/examples/example3.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 
00081 #include <iostream>
00082 #include <fstream>
00083 #include <vector>
00084 
00085 using std::vector;
00086 using std::ifstream;
00087 using std::ofstream;
00088 using std::cout;
00089 using std::endl;
00090 
00092 double feval(const vector<double>& x)
00093 {
00094   double f1 = (x[0] * x[0]) + (x[1] * x[1]) + (x[2] * x[2]);
00095 
00096   double f2 = 3 - (x[0] * x[0]) + ((x[1]-2) * (x[1]-2)) + (x[2] * x[2]);
00097 
00098   if (f2 > 0)
00099     f2 = 0;
00100   else
00101     f2 = f2 * f2;
00102 
00103   double f3 = 3 - ((x[0]-2) * (x[0]-2)) + (x[1] * x[1]) + (x[2] * x[2]);
00104 
00105   if (f3 > 0)
00106     f3 = 0;
00107   else
00108     f3 = f3 * f3;
00109 
00110   double f = f1 + f2 + f3;
00111   return(f);
00112 
00113 } // end feval 
00114 
00116 int main(int argc, char* argv[]) 
00117 {
00118 
00119   int i;                        // counter 
00120   vector<double> x;             // x-vector 
00121   double y;                     // solution of f(x) 
00122   ifstream fin;                 // input file
00123   ofstream fout;                // output file
00124 
00125   // Check input arguments 
00126 
00127   if (argc < 2) 
00128   {
00129     cout << "usage: " << argv[0] << " <input file> <output file>" << endl;
00130     return -1;
00131   }
00132 
00133   // Open input file 
00134 
00135   fin.open(argv[1]);
00136   if (!fin)
00137   {
00138     cout << argv[0] << " - Error opening input file " << argv[1] << endl;
00139     return -1;
00140   }
00141 
00142   // Read size of x 
00143 
00144   int n;
00145   fin >> n;
00146 
00147   // Allocate memory for x 
00148 
00149   x.resize(n);
00150 
00151   // Read x 
00152 
00153   for (i = 0; i < n; i ++)
00154     fin >> x[i];
00155   
00156   // Close input file 
00157 
00158   fin.close();
00159 
00160   // Open output file 
00161 
00162   fout.open(argv[2]);
00163   if (!fout)
00164   {
00165     cout << argv[0] << " - Error opening output file " << argv[2] << endl;
00166     return -1;
00167   }
00168 
00169   // Evaluate function at x 
00170 
00171   y = feval(x);
00172 
00173   // Write function value to output file 
00174 
00175   fout << y;
00176 
00177   // Close output file 
00178 
00179   fout.close();
00180 
00181   // Exit 
00182 
00183   return 0;
00184 }
00185 

 

© 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