1 /*  _______________________________________________________________________
2 
3     DAKOTA: Design Analysis Kit for Optimization and Terascale Applications
4     Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Dakota directory.
7     _______________________________________________________________________ */
8 
9 #include <cstdlib>
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 #include <string>
14 #include <cmath>
15 
16 
main(int argc,char ** argv)17 int main(int argc, char** argv)
18 {
19 
20   std::ifstream fin(argv[1]);
21   if (!fin) {
22     std::cerr << "\nError: failure opening " << argv[1] << std::endl;
23     exit(-1);
24   }
25   size_t i, num_vars, num_fns;
26   std::string vars_text, fns_text;
27 
28   // Get the parameter std::vector and ignore the labels
29   fin >> num_vars >> vars_text;
30   std::vector<double> x(num_vars);
31   for (i=0; i<num_vars; i++) {
32     fin >> x[i];
33     fin.ignore(256, '\n');
34   }
35 
36   // Get the ASV std::vector and ignore the labels
37   fin >> num_fns >> fns_text;
38   std::vector<int> ASV(num_fns);
39   for (i=0; i<num_fns; i++) {
40     fin >> ASV[i];
41     fin.ignore(256, '\n');
42   }
43 
44   if (num_vars != 2 || num_fns != 1) {
45     std::cerr << "Error: wrong number of inputs/outputs in log_ratio test function."
46          << std::endl;
47     exit(-1);
48   }
49 
50   // Compute the results and output them directly to argv[2] (the NO_FILTER
51   // option is used).  Response tags are now optional; output them for ease
52   // of results readability.
53   std::ofstream fout(argv[2]);
54   if (!fout) {
55     std::cerr << "\nError: failure creating " << argv[2] << std::endl;
56     exit(-1);
57   }
58   fout.precision(15); // 16 total digits
59   fout.setf(std::ios::scientific);
60   fout.setf(std::ios::right);
61 
62   // **** f:
63   if (ASV[0] & 1)
64     fout << "                     " << x[0]/x[1] << " f\n";
65 
66   // **** df/dx:
67   if (ASV[0] & 2)
68     fout << "[ " << 1./x[1] << ' ' << -x[0]/(x[1]*x[1]) << " ]\n";
69 
70   // **** d^2f/dx^2: (full Newton opt., SORM integration)
71   if (ASV[0] & 4)
72     fout << "[[ 0.0 " << -1./(x[1]*x[1]) << ' ' << -1./(x[1]*x[1]) << ' '
73          << 2.*x[0]/pow(x[1],3) << " ]]\n";
74 
75   fout.flush();
76   fout.close();
77   return 0;
78 }
79