MAST
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
function_evaluation.h
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
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  _iter (0),
53  _n_vars (0),
54  _n_eq (0),
55  _n_ineq (0),
56  _max_iters (0),
58  _tol (1.0e-6),
59  _output (nullptr),
60  _optimization_interface (nullptr)
61  { }
62 
63  virtual ~FunctionEvaluation() { }
64 
65 
67 
68 
69  unsigned int n_vars() const {
70  return _n_vars;
71  }
72 
73 
74  unsigned int n_eq() const {
75  return _n_eq;
76  }
77 
78 
79  unsigned int n_ineq() const{
80  return _n_ineq;
81  }
82 
83 
84  unsigned int max_iters() const{
85  return _max_iters;
86  }
87 
88 
89  unsigned int n_iters_relative_change() const {
90  return _n_rel_change_iters;
91  }
92 
93 
94  Real tolerance() const{
95  return _tol;
96  }
97 
98 
99  virtual void init_dvar(std::vector<Real>& x,
100  std::vector<Real>& xmin,
101  std::vector<Real>& xmax) = 0;
102 
107  virtual void evaluate(const std::vector<Real>& dvars,
108  Real& obj,
109  bool eval_obj_grad,
110  std::vector<Real>& obj_grad,
111  std::vector<Real>& fvals,
112  std::vector<bool>& eval_grads,
113  std::vector<Real>& grads) = 0;
114 
115 
125  void set_output_file(const std::string& nm) {
126 
127  if (!_output)
128  _output = new std::ofstream;
129 
130  _output->close();
131  _output->open(nm.c_str(), std::ofstream::out);
132  }
133 
134 
139  virtual void output(unsigned int iter,
140  const std::vector<Real>& x,
141  Real obj,
142  const std::vector<Real>& fval,
143  bool if_write_to_optim_file);
144 
145 
154  void initialize_dv_from_output_file(const std::string& nm,
155  const unsigned int iter,
156  std::vector<Real> &x);
157 
161  virtual bool verify_gradients(const std::vector<Real>& dvars);
162 
167  virtual void
168  parametric_line_study(const std::string& nm,
169  const unsigned int iter1,
170  const unsigned int iter2,
171  unsigned int divs);
172 
173 
174 #if MAST_ENABLE_SNOPT == 1
175  typedef void (*funobj) (int* mode,
176  int* n,
177  double* x,
178  double* f,
179  double* g,
180  int* nstate);
181 
182 
183  typedef void (*funcon) (int* mode,
184  int* ncnln,
185  int* n,
186  int* ldJ,
187  int* needc,
188  double* x,
189  double* c,
190  double* cJac,
191  int* nstate);
192 
197  virtual funobj
198  get_objective_evaluation_function() {
199 
200  // should not get here, if the derived method implements its
201  // specialized method
202  libmesh_assert(false);
203  return nullptr;
204  }
205 
206 
211  virtual funcon
212  get_constraint_evaluation_function() {
213 
214  // should not get here, if the derived method implements its
215  // specialized method
216  libmesh_assert(false);
217  return nullptr;
218  }
219 #endif
220 
225  void sanitize_parallel();
226 
232  virtual void _init_dvar_wrapper(std::vector<Real>& x,
233  std::vector<Real>& xmin,
234  std::vector<Real>& xmax);
235 
236 
243  virtual void _evaluate_wrapper(const std::vector<Real>& dvars,
244  Real& obj,
245  bool eval_obj_grad,
246  std::vector<Real>& obj_grad,
247  std::vector<Real>& fvals,
248  std::vector<bool>& eval_grads,
249  std::vector<Real>& grads);
250 
256  virtual void _output_wrapper(unsigned int iter,
257  const std::vector<Real>& x,
258  Real obj,
259  const std::vector<Real>& fval,
260  bool if_write_to_optim_file);
261 
262  protected:
263 
264  unsigned int _iter;
265 
266  unsigned int _n_vars;
267 
268  unsigned int _n_eq;
269 
270  unsigned int _n_ineq;
271 
272  unsigned int _max_iters;
273 
274  unsigned int _n_rel_change_iters;
275 
277 
278  std::ofstream* _output;
279 
281  };
282 
283 
284 }
285 
286 #endif // __mast__function_evaluation_h__
287 
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...
unsigned int n_eq() const
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
unsigned int max_iters() const
unsigned int n_vars() const
FunctionEvaluation(const libMesh::Parallel::Communicator &comm_in)
libMesh::Real Real
MAST::OptimizationInterface * _optimization_interface
unsigned int n_ineq() 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.
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.
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...
unsigned int n_iters_relative_change() const
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...
virtual bool verify_gradients(const std::vector< Real > &dvars)
verifies the gradients at the specified design point