MAST
function_evaluation.h
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2019 Manav Bhatia
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 
21 #ifndef __mast__function_evaluation_h__
22 #define __mast__function_evaluation_h__
23 
24 // C++ includes
25 #include <vector>
26 #include <iostream>
27 #include <iomanip>
28 #include <fstream>
29 
30 
31 // MAST includes
32 #include "base/mast_data_types.h"
33 #include "base/mast_config.h"
34 
35 
36 // libMesh includes
37 #include "libmesh/parallel_object.h"
38 
39 
40 namespace MAST {
41 
42  // Forward declerations
43  class OptimizationInterface;
44 
46  public libMesh::ParallelObject {
47 
48  public:
49 
50  FunctionEvaluation(const libMesh::Parallel::Communicator& comm_in):
51  libMesh::ParallelObject (comm_in),
52  _n_vars (0),
53  _n_eq (0),
54  _n_ineq (0),
55  _max_iters (0),
57  _tol (1.0e-6),
58  _output (nullptr),
59  _optimization_interface (nullptr)
60  { }
61 
62  virtual ~FunctionEvaluation() { }
63 
64 
66 
67 
68  unsigned int n_vars() const {
69  return _n_vars;
70  }
71 
72 
73  unsigned int n_eq() const {
74  return _n_eq;
75  }
76 
77 
78  unsigned int n_ineq() const{
79  return _n_ineq;
80  }
81 
82 
83  unsigned int max_iters() const{
84  return _max_iters;
85  }
86 
87 
88  unsigned int n_iters_relative_change() const {
89  return _n_rel_change_iters;
90  }
91 
92 
93  Real tolerance() const{
94  return _tol;
95  }
96 
97 
98  virtual void init_dvar(std::vector<Real>& x,
99  std::vector<Real>& xmin,
100  std::vector<Real>& xmax) = 0;
101 
106  virtual void evaluate(const std::vector<Real>& dvars,
107  Real& obj,
108  bool eval_obj_grad,
109  std::vector<Real>& obj_grad,
110  std::vector<Real>& fvals,
111  std::vector<bool>& eval_grads,
112  std::vector<Real>& grads) = 0;
113 
114 
124  void set_output_file(const std::string& nm) {
125 
126  if (!_output)
127  _output = new std::ofstream;
128 
129  _output->close();
130  _output->open(nm.c_str(), std::ofstream::out);
131  }
132 
133 
138  virtual void output(unsigned int iter,
139  const std::vector<Real>& x,
140  Real obj,
141  const std::vector<Real>& fval,
142  bool if_write_to_optim_file);
143 
144 
153  void initialize_dv_from_output_file(const std::string& nm,
154  const unsigned int iter,
155  std::vector<Real> &x);
156 
160  virtual bool verify_gradients(const std::vector<Real>& dvars);
161 
166  virtual void
167  parametric_line_study(const std::string& nm,
168  const unsigned int iter1,
169  const unsigned int iter2,
170  unsigned int divs);
171 
172 
173 #if MAST_ENABLE_SNOPT == 1
174  typedef void (*funobj) (int* mode,
175  int* n,
176  double* x,
177  double* f,
178  double* g,
179  int* nstate);
180 
181 
182  typedef void (*funcon) (int* mode,
183  int* ncnln,
184  int* n,
185  int* ldJ,
186  int* needc,
187  double* x,
188  double* c,
189  double* cJac,
190  int* nstate);
191 
196  virtual funobj
197  get_objective_evaluation_function() {
198 
199  // should not get here, if the derived method implements its
200  // specialized method
201  libmesh_assert(false);
202  return nullptr;
203  }
204 
205 
210  virtual funcon
211  get_constraint_evaluation_function() {
212 
213  // should not get here, if the derived method implements its
214  // specialized method
215  libmesh_assert(false);
216  return nullptr;
217  }
218 #endif
219 
224  void sanitize_parallel();
225 
231  virtual void _init_dvar_wrapper(std::vector<Real>& x,
232  std::vector<Real>& xmin,
233  std::vector<Real>& xmax);
234 
235 
242  virtual void _evaluate_wrapper(const std::vector<Real>& dvars,
243  Real& obj,
244  bool eval_obj_grad,
245  std::vector<Real>& obj_grad,
246  std::vector<Real>& fvals,
247  std::vector<bool>& eval_grads,
248  std::vector<Real>& grads);
249 
255  virtual void _output_wrapper(unsigned int iter,
256  const std::vector<Real>& x,
257  Real obj,
258  const std::vector<Real>& fval,
259  bool if_write_to_optim_file);
260 
261  protected:
262 
263  unsigned int _n_vars;
264 
265  unsigned int _n_eq;
266 
267  unsigned int _n_ineq;
268 
269  unsigned int _max_iters;
270 
271  unsigned int _n_rel_change_iters;
272 
274 
275  std::ofstream* _output;
276 
278  };
279 
280 
281 }
282 
283 #endif // __mast__function_evaluation_h__
284 
virtual void output(unsigned int iter, const std::vector< Real > &x, Real obj, const std::vector< Real > &fval, bool if_write_to_optim_file)
outputs the the current iterate to libMesh::out, and to the output file if it was set for this rank...
virtual void parametric_line_study(const std::string &nm, const unsigned int iter1, const unsigned int iter2, unsigned int divs)
computes a parametric evaluation along a line from iter1 to iter2 in file nm with divs runs between t...
void sanitize_parallel()
make sure that the analysis is setup consistently across all parallel processes
virtual void init_dvar(std::vector< Real > &x, std::vector< Real > &xmin, std::vector< Real > &xmax)=0
FunctionEvaluation(const libMesh::Parallel::Communicator &comm_in)
libMesh::Real Real
MAST::OptimizationInterface * _optimization_interface
unsigned int max_iters() const
virtual void evaluate(const std::vector< Real > &dvars, Real &obj, bool eval_obj_grad, std::vector< Real > &obj_grad, std::vector< Real > &fvals, std::vector< bool > &eval_grads, std::vector< Real > &grads)=0
grads(k): Derivative of f_i(x) with respect to x_j, where k = (j-1)*M + i.
unsigned int n_ineq() const
void set_output_file(const std::string &nm)
sets the output file and the function evaluation will write the optimization iterates to this file...
virtual void _evaluate_wrapper(const std::vector< Real > &dvars, Real &obj, bool eval_obj_grad, std::vector< Real > &obj_grad, std::vector< Real > &fvals, std::vector< bool > &eval_grads, std::vector< Real > &grads)
This serves as a wrapper around evaluate() and makes sure that the derived class&#39;s implementation is ...
void attach_optimization_interface(MAST::OptimizationInterface &opt)
Provides the basic interface API for classes the provide implement optimization problems.
unsigned int n_vars() const
void initialize_dv_from_output_file(const std::string &nm, const unsigned int iter, std::vector< Real > &x)
This reads and initializes the DV vector from a previous optimization history output file...
virtual void _output_wrapper(unsigned int iter, const std::vector< Real > &x, Real obj, const std::vector< Real > &fval, bool if_write_to_optim_file)
This serves as a wrapper around evaluate() and makes sure that the derived class&#39;s implementation is ...
virtual void _init_dvar_wrapper(std::vector< Real > &x, std::vector< Real > &xmin, std::vector< Real > &xmax)
This serves as a wrapper around init_dvar() and makes sure that the derived class&#39;s implementation pr...
unsigned int n_eq() const
unsigned int n_iters_relative_change() const
virtual bool verify_gradients(const std::vector< Real > &dvars)
verifies the gradients at the specified design point