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   // This application program reads and writes parameter and response data
21   // directly so no input/output filters are needed.
22   std::ifstream fin(argv[1]);
23   if (!fin) {
24     std::cerr << "\nError: failure opening " << argv[1] << std::endl;
25     exit(-1);
26   }
27   size_t i, num_vars, num_fns, num_deriv_vars;
28   std::string vars_text, fns_text, dvv_text;
29 
30   // Get the parameter std::vector and ignore the labels
31   fin >> num_vars >> vars_text;
32   std::vector<double> x(num_vars);
33   for (i=0; i<num_vars; i++) {
34     fin >> x[i];
35     fin.ignore(256, '\n');
36   }
37 
38   // Get the ASV std::vector and ignore the labels
39   fin >> num_fns >> fns_text;
40   std::vector<int> ASV(num_fns);
41   for (i=0; i<num_fns; i++) {
42     fin >> ASV[i];
43     fin.ignore(256, '\n');
44   }
45 
46   // Get the DVV std::vector and ignore the labels
47   fin >> num_deriv_vars >> dvv_text;
48   std::vector<int> DVV(num_deriv_vars);
49   for (i=0; i<num_deriv_vars; i++) {
50     fin >> DVV[i];
51     fin.ignore(256, '\n');
52   }
53 
54   if (num_vars != 2) {
55     std::cerr << "Wrong number of variables for this problem\n";
56     exit(-1);
57   }
58   if (num_fns != 1) {
59     std::cerr << "Wrong number of functions for this problem\n";
60     exit(-1);
61   }
62 
63   std::ofstream fout(argv[2]);
64   if (!fout) {
65     std::cerr << "\nError: failure creating " << argv[2] << std::endl;
66     exit(-1);
67   }
68   fout.precision(15); // 16 total digits
69   fout.setf(std::ios::scientific);
70   fout.setf(std::ios::right);
71 
72   // f = (x2 + 0.5)^4 / (x1 + 0.5)^2
73   // See Storlie et al. SAND2008-6570
74 
75   // **** f:
76   fout << "                     "
77        << pow((x[1] + 0.5), 4.0) / pow((x[0] + 0.5), 2.0) << " f\n";
78 
79   // **** df/dx:
80   if (ASV[0] & 2) {
81     fout << "[ ";
82     for (i=0; i<num_deriv_vars; i++) {
83       int var_index = DVV[i] - 1;
84       switch (var_index) {
85       case 0: // x1
86 	fout << -2.*pow(x[1] + 0.5, 4.0)/pow(x[0] + 0.5, 3.0) << ' ';
87 	break;
88       case 1: // x2
89 	fout <<  4.*pow(x[1] + 0.5, 3.0)/pow(x[0] + 0.5, 2.0) << ' ';
90 	break;
91       }
92     }
93     fout << "]\n";
94   }
95 
96   fout.flush();
97   fout.close();
98   return 0;
99 }
100