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 #include <thread>
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, j, k, num_vars, num_fns, num_deriv_vars;
26   std::string vars_text, fns_text, dvv_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   // Get the DVV std::vector and ignore the labels
45   fin >> num_deriv_vars >> dvv_text;
46   std::vector<int> DVV(num_deriv_vars);
47   for (i=0; i<num_deriv_vars; i++) {
48     fin >> DVV[i];
49     fin.ignore(256, '\n');
50   }
51 
52   //std::this_thread::sleep_for(std::chrono::seconds(1));
53 
54   // Compute the results and output them directly to argv[2] (the NO_FILTER
55   // option is used).  Response tags are now optional; output them for ease
56   // of results readability.
57   std::ofstream fout(argv[2]);
58   if (!fout) {
59     std::cerr << "\nError: failure creating " << argv[2] << std::endl;
60     exit(-1);
61   }
62   fout.precision(15); // 16 total digits
63   fout.setf(std::ios::scientific);
64   fout.setf(std::ios::right);
65 
66   // text_book1 calculates active f data & outputs 0's for active c1 & c2 data
67 
68   // **** f:
69   for (i=0; i<num_fns; i++) {
70     if (i==0) {
71       if (ASV[0] & 1) {
72         double value = 0.;
73         for (j=0; j<num_vars; j++)
74           value += pow(x[j]-1., 4.);
75         fout << "                     " << value << " fn" << i << '\n';
76       }
77     }
78     else if (ASV[i] & 1)
79       fout << "                     0.0 fn" << i << '\n';
80   }
81 
82   // **** df/dx:
83   for (i=0; i<num_fns; i++) {
84     if (i==0) {
85       if (ASV[0] & 2) {
86         fout << "[ ";
87 	for (j=0; j<num_deriv_vars; j++)
88 	  fout << 4.*pow(x[DVV[j]-1] - 1., 3) << ' ';
89         fout << "]\n";
90       }
91     }
92     else if (ASV[i] & 2) {
93       fout << "[ ";
94       for (j=0; j<num_deriv_vars; j++)
95         fout << "0. ";
96       fout << "]\n";
97     }
98   }
99 
100   // **** d^2f/dx^2: (full Newton unconstrained opt.)
101   for (i=0; i<num_fns; i++) {
102     if (i==0) {
103       if (ASV[0] & 4) {
104         fout << "[[ ";
105         for (j=0; j<num_deriv_vars; j++)
106           for (k=0; k<num_deriv_vars; k++)
107             if (j==k)
108 	      fout << 12.*pow(x[DVV[j]-1] - 1., 2) << ' ';
109             else
110               fout << "0. ";
111         fout << "]]\n";
112       }
113     }
114     else if (ASV[i] & 4) {
115       fout << "[[ ";
116       for (j=0; j<num_deriv_vars; j++)
117         for (k=0; k<num_deriv_vars; k++)
118           fout << "0. ";
119       fout << "]]\n";
120     }
121   }
122 
123   fout.flush();
124   fout.close();
125   return 0;
126 }
127