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 
15 //**********************************************************************
16 // container.cpp - C++ container optimization example
17 //**********************************************************************
18 
main(int argc,char ** argv)19 int main(int argc, char** argv)
20 {
21 
22   //******************************
23   // read the input from DAKOTA
24   //******************************
25   std::ifstream fin(argv[1]);
26   if (!fin) {
27     std::cerr << "\nError: failure opening " << argv[1] << std::endl;
28     exit(-1);
29   }
30   size_t i, num_vars, num_fns;
31   std::string vars_text, fns_text;
32 
33   // Get the parameter std::vector and ignore the labels
34   fin >> num_vars >> vars_text;
35   std::vector<double> x(num_vars);
36   for (i=0; i<num_vars; i++) {
37     fin >> x[i];
38     fin.ignore(256, '\n');
39   }
40 
41   // Get the ASV std::vector and ignore the labels
42   fin >> num_fns >> fns_text;
43   std::vector<int> ASV(num_fns);
44   for (i=0; i<num_fns; i++) {
45     fin >> ASV[i];
46     fin.ignore(256, '\n');
47   }
48 
49   if (num_vars != 2 || num_fns != 2) {
50     std::cerr << "Error: wrong number of inputs/outputs in container test function."
51          << std::endl;
52     exit(-1);
53   }
54 
55   //********************************************************
56   // compute the objective function and constraint values
57   // and write the response output for DAKOTA
58   //********************************************************
59   std::ofstream fout(argv[2]);
60   if (!fout) {
61     std::cerr << "\nError: failure creating " << argv[2] << std::endl;
62     exit(-1);
63   }
64   fout.precision(15); // 16 total digits
65   fout.setf(std::ios::scientific);
66   fout.setf(std::ios::right);
67 
68   double H = x[0], D = x[1];
69   const double PI = 3.14159265358979;
70   if (ASV[0] & 1) {
71     double fval = 0.644*PI*D*D + 1.04*PI*D*H;
72     fout << fval << " area\n";
73   }
74   if (ASV[1] & 1) {
75     double cval = 0.25*PI*H*D*D - 63.525;
76     fout << cval << " volume_constraint\n";
77   }
78 
79   fout.flush();
80   fout.close();
81   return 0;
82 }
83 
84