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

example1.c

Go to the documentation of this file.
00001 /* $Id: example1.c,v 1.4 2006/06/30 01:07:26 tgkolda Exp $ */
00002 /* $Source: /space/CVS-Acro/acro/packages/appspack/appspack/examples/example1.c,v $ */
00003 
00004 /* @HEADER */
00005 /*
00006 ************************************************************************
00007 
00008          APPSPACK: Asynchronous Parallel Pattern Search
00009                 Copyright (2003) Sandia Corporation
00010 
00011 Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00012 license for use of this work by or on behalf of the U.S. Government.
00013 
00014 This library is free software; you can redistribute it and/or modify
00015 it under the terms of the GNU Lesser General Public License as
00016 published by the Free Software Foundation; either version 2.1 of the
00017 License, or (at your option) any later version.
00018  
00019 This library is distributed in the hope that it will be useful, but
00020 WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022 Lesser General Public License for more details.
00023                                                                                 
00024 You should have received a copy of the GNU Lesser General Public
00025 License along with this library; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00027 USA.                                                                           .
00028 
00029 Questions? Contact Tammy Kolda (tgkolda@sandia.gov) 
00030 
00031 ************************************************************************
00032 */
00033 /* @HEADER */
00034 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 
00062 double example1_feval(int n, double *x) 
00063 {
00064   int i;
00065   double f = 0;
00066 
00067   for (i = 0; i < n; i ++)
00068     f += (i + 1) * x[i] * x[i];
00069 
00070   return(f);
00071 
00072 } /* end feval */
00073 
00075 int main(int argc, char* argv[]) {
00076 
00077   int i;                        /* counter */
00078   int n;                        /* number of dimensions */
00079   double* x;                    /* n-dimensional vector */
00080   double y;                     /* solution of f(x) */
00081   FILE* fp;                     /* file pointer */
00082 
00083 
00084   /* Check input arguments */
00085   if (argc < 2) {
00086     fprintf (stderr, "usage: %s <input file> <output file>\n", argv[0]);
00087     return -1;
00088   }
00089 
00090   /* Open input file */
00091 
00092   if ((fp = fopen(argv[1], "r")) == NULL) {
00093     fprintf(stderr, "%s - Error opening input file %s.\n", argv[0], argv[1]);
00094     return -1;
00095   }
00096 
00097   /* Read size of x */
00098 
00099   if ((fscanf(fp, "%d", &n)) != 1) {
00100     fprintf(stderr, "%s - Error reading n.\n", argv[0]);
00101     return -1;
00102   }
00103 
00104   /* Allocate memory for x */
00105 
00106   if ((x = malloc(n * sizeof(double))) == NULL) {
00107     fprintf(stderr, "%s - Error allocating space for x.\n", argv[0]);
00108     return -1;
00109   }
00110 
00111   /* Read x */
00112 
00113   for (i = 0; i < n; i ++)
00114     if ((fscanf(fp, "%le", &x[i])) != 1) {
00115       fprintf(stderr, "%s - Error reading x[%d].\n", argv[0], i);
00116       return -1;
00117     }
00118   
00119   /* Close input file */
00120 
00121   fclose(fp);
00122 
00123   /* Evaluate function at x */
00124 
00125   y = example1_feval(n, x);
00126 
00127   /* Open output file */
00128 
00129   if ((fp = fopen(argv[2], "w")) == NULL) {
00130     fprintf(stderr, "%s - Error opening output file.\n", argv[0]);
00131     return -1;
00132   }
00133 
00134   /* Write function value to output file */
00135   
00136   fprintf(fp, "%e\n", y);
00137 
00138   /* Close output file */
00139 
00140   fclose(fp);
00141 
00142   /* Release memory */
00143 
00144   free(x);
00145 
00146   /* Exit */
00147 
00148   return 0;
00149 }
00150 

 

© 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