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 //- Class:        PythonInterface
10 
11 //- Description:  Derived interface class for performing analysis via
12 //-               linked Python API, permitting single initialization /
13 //-               finalize of Python for the whole set of function evaluations.
14 //- Owner:        Brian Adams
15 //- Version: $Id$
16 #ifndef PYTHON_INTERFACE_H
17 #define PYTHON_INTERFACE_H
18 
19 #include "DirectApplicInterface.hpp"
20 
21 // The following to forward declare, but avoid clash with include
22 #ifndef PyObject_HEAD
23 struct _object;
24 typedef _object PyObject;
25 #endif
26 
27 namespace Dakota {
28 
29 /** Specialization of DirectApplicInterface to link to Python analysis
30     drivers.  Includes convenience functions to map data to/from Python */
31 class PythonInterface: public DirectApplicInterface
32 {
33 
34 public:
35 
36   PythonInterface(const ProblemDescDB& problem_db); ///< constructor
37   ~PythonInterface();                               ///< destructor
38 
39 protected:
40 
41   /// execute an analysis code portion of a direct evaluation invocation
42   virtual int derived_map_ac(const String& ac_name);
43 
44   /// direct interface to Python via API, BMA 07/02/07
45   int python_run(const String& ac_name);
46 
47   /// whether the user requested numpy data structures in the input file
48   bool userNumpyFlag;
49   /// true if this class created the interpreter instance
50   bool ownPython;
51 
52   /// convert arrays of integer types to Python list or numpy array
53   template<class ArrayT, class Size>
54   bool python_convert_int(const ArrayT& src, Size size, PyObject** dst);
55   /// convert RealVector to Python list or numpy array
56   bool python_convert(const RealVector& src, PyObject** dst);
57   /// convert RealVector + IntVector + RealVector to Python mixed list
58   /// or numpy double array
59   bool python_convert(const RealVector& c_src, const IntVector& di_src,
60 		      const RealVector& dr_src, PyObject** dst);
61   /// convert labels
62   template<class StringArrayT>
63   bool python_convert_strlist(const StringArrayT& src, PyObject** dst);
64   /// convert all labels to single list
65   bool python_convert(const StringMultiArray& c_src,
66 		      const StringMultiArray& di_src,
67 		      const StringMultiArray& dr_src, PyObject** dst);
68   /// convert python [list of int or float] or [numpy array of double] to
69   /// RealVector (for fns)
70   bool python_convert(PyObject *pyv, RealVector& rv, const int& dim);
71   /// convert python [list of int or float] or [numpy array of double] to
72   /// double[], for use as helper in converting gradients
73   bool python_convert(PyObject *pyv, double *rv, const int& dim);
74   /// convert python [list of lists of int or float] or [numpy array of dbl]
75   /// to RealMatrix (for gradients)
76   bool python_convert(PyObject *pym, RealMatrix &rm);
77   /// convert python [list of lists of int or float] or [numpy array of dbl]
78   /// to RealMatrix (used as helper in Hessian conversion)
79   bool python_convert(PyObject *pym, RealSymMatrix &rm);
80   /// convert python [list of lists of lists of int or float] or
81   /// [numpy array of double] to RealSymMatrixArray (for Hessians)
82   bool python_convert(PyObject *pyma, RealSymMatrixArray &rma);
83 
84 };
85 
86 } // namespace Dakota
87 
88 #endif  // PYTHON_INTERFACE_H
89