OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
optimization_block_matrix_application.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 //
3 // FCST: Fuel Cell Simulation Toolbox
4 //
5 // Copyright (C) 2006-2013 by Energy Systems Design Laboratory, University of Alberta
6 //
7 // This software is distributed under the MIT License
8 // For more information, see the README file in /doc/LICENSE
9 //
10 // - Class: optimization_block_matrix_application.h
11 // - Description: This is a base class for all applications that provide optimization information
12 // - Developers: Marc Secanell, University of Alberta
13 // - $Id: optimization_block_matrix_application.h 2605 2014-08-15 03:36:44Z secanell $
14 //
15 // ----------------------------------------------------------------------------
16 
17 #ifndef _FUEL_CELL_APPLICATION_CORE_OPTIMIZATION_BLOCK_MATRIX_APPLICATION_H_
18 #define _FUEL_CELL_APPLICATION_CORE_OPTIMIZATION_BLOCK_MATRIX_APPLICATION_H_
19 
20 #include <base/parameter_handler.h>
21 #include <base/convergence_table.h>
22 #include <lac/vector.h>
23 #include <lac/block_vector.h>
24 #include <grid/grid_refinement.h>
25 #include <grid/grid_tools.h>
26 #include <grid/persistent_tria.h>
27 #include <dofs/dof_handler.h>
28 #include <dofs/dof_renumbering.h>
29 #include <fe/fe.h>
30 #include <fe/fe_values.h>
31 #include <numerics/solution_transfer.h>
32 #include <boost/shared_ptr.hpp>
33 
34 #include "dof_application.h"
35 #include "matrix_block.h"
37 
38 #include <iostream>
39 #include <fstream>
40 #include <sstream>
41 #include <vector>
42 
43 namespace FuelCell
44 {
45 namespace ApplicationCore
46 {
61  template <int dim>
64  {
65  public:
73 
88  bool triangulation_only);
89 
101  OptimizationBlockMatrixApplication(boost::shared_ptr<FuelCell::ApplicationCore::ApplicationData> data = boost::shared_ptr<FuelCell::ApplicationCore::ApplicationData>());
102 
103 
106 
113  virtual void declare_parameters(ParameterHandler& param);
114 
117  virtual void initialize(ParameterHandler& param);
118 
125  virtual void responses (std::vector<double>& f,
126  const FuelCell::ApplicationCore::FEVectors& vectors);
131  virtual void check_responses();
132 
138  virtual void cell_responses (std::vector<double>& resp,
139  const typename DoFApplication<dim>::CellInfo& info,
141 
147  virtual void bdry_responses (std::vector<double>& resp,
148  const typename DoFApplication<dim>::FaceInfo& info,
150 
155  virtual void global_responses (std::vector<double>& resp,
157 
159  virtual void print_responses (std::vector<double>& resp);
160 
161 
174  virtual void dresponses_dl (std::vector<std::vector<double> >& df_dl,
181  virtual void cell_dresponses_dl(std::vector<std::vector<double> >& cell_df_dl,
182  const typename DoFApplication<dim>::CellInfo& info,
189  virtual void global_dresponses_dl(std::vector<std::vector<double> >& df_dl,
206  virtual void dresponses_du(std::vector<FuelCell::ApplicationCore::FEVector>& dst,
208 
213  virtual void cell_dresponses_du(std::vector<FuelCell::ApplicationCore::FEVector>& df_du,
214  const typename DoFApplication<dim>::CellInfo& info,
215  std::vector<std::vector<double> >& src);
216 
222  virtual void global_dresponses_du(std::vector<FuelCell::ApplicationCore::FEVector>& df_du,
224 
240  virtual void dresidual_dlambda(std::vector<FuelCell::ApplicationCore::FEVector>& dst,
246  virtual void cell_dresidual_dlambda(std::vector<FuelCell::ApplicationCore::FEVector>& cell_vector,
247  const typename DoFApplication<dim>::CellInfo& cell,
248  std::vector<std::vector<double> >& src);
249 
261  void solve_direct (std::vector<std::vector<double> >& df_dl,
263 
275  void solve_adjoint (std::vector<std::vector<double> >& df_dl,
280  unsigned int get_n_resp() const;
284  unsigned int get_n_dvar() const;
288  inline std::vector<std::string> get_name_dvar() const
289  {
290  return name_design_var;
291 
292  }
293 
297  inline std::vector<std::string> get_name_responses() const
298  {
299  return name_responses;
300 
301  }
302 
306  const std::vector<std::string> get_all_responses_names() const
307  {
308  return all_response_names;
309  }
313  /*void enter_data_scalar(std::string name,
314  * const double& scalar)
315  { *
316  this->get_data()->enter(name, scalar);
317  }*/
318 
323  {
324  ParameterHandler prm;
325  this->declare_parameters(prm);
326  std::ofstream file;
327  file.open("Default_parameter_file.prm");
328  prm.print_parameters (file, ParameterHandler::Text);
329  file.close();
330  }
331 
334  {return output_coarse_solution;}
335 
341  void set_optimization_parameters(unsigned int &n_dvar, unsigned int &n_resp,
342  std::vector<std::string> &name_design_var, std::vector<std::string> &name_responses);
343 
347  void set_output_variables(std::vector<std::string> &dakota_name_responses)
348  {
349  name_responses = dakota_name_responses;
350  };
351 
366  const unsigned int& refinement_cycle,
367  std::vector< ConvergenceTable >& convergence_tables) const
368  {
369  const std::type_info& info = typeid(*this);
370  FcstUtilities::log << "Pure function " << __FUNCTION__ << " called in Class " << info.name() << std::endl;
371 
372  }
373 
374  protected:
379  void print_dresponses_dl(std::vector<std::vector<double> > pdf_pdl)
380  {
381  for (unsigned int i=0; i<n_resp; ++i)
382  for (unsigned int j=0; j<n_dvar; ++j)
383  FcstUtilities::log<<"Df"<<i<<"/Dl"<<j<<" is equal to "<<pdf_pdl[i][j]<<std::endl;
384  }
385 
390  void print_dresponses_du(std::vector<FuelCell::ApplicationCore::FEVector> df_du)
391  {
392  // Write partial df_du:
393 
394  for (unsigned int r=0; r<n_resp; ++r)
395  {
396  // Output Du block by block
397  unsigned int n_blocks = df_du[r].n_blocks();
398  unsigned int size = df_du[r].size();
399  unsigned int comp_per_block = size/n_blocks;
400  for (unsigned int i=0; i<n_blocks; ++i)
401  for (unsigned int j=0; j<comp_per_block; ++j)
402  {
403  FcstUtilities::log<<"Element "<<j<<" in block "<<i<<" in STEP (Du) is "<<df_du[r].block(i)(j)<<std::endl;
404  }
405  FcstUtilities::log<<"NORM OF Df"<<r<<"/Du = "<<df_du[r].l2_norm()<<std::endl;
406  }
407  }
408 
415  const std::string extend_filename(const std::string& , const int precision = 3) const;
416 
422  typedef void (*CELL_Dvalues) (std::vector<FuelCell::ApplicationCore::FEVector>& ,
423  const typename DoFApplication<dim>::CellInfo& ,
424  std::vector<std::vector<double> >& );
425 
435  void dfunction (std::vector<FuelCell::ApplicationCore::FEVector>& dst,
437  bool dfunctional_du,
438  bool dresidual_dlambda);
439 
444  void set_all_response_names();
445 
449  unsigned int n_dvar;
453  unsigned int n_obj;
457  unsigned int n_resp;
461  std::vector<std::string> name_design_var;
466  std::vector<std::string> name_responses;
467 
472  std::vector<std::string> name_output_var;
473 
476 
479 
484 
488  std::vector <std::string> all_response_names;
489  };
490 
491 }
492 }
493 
494 #endif
void solve_adjoint(std::vector< std::vector< double > > &df_dl, const FuelCell::ApplicationCore::FEVector &sol)
Solver in order to obtain the analytical sensitivities for the given objective and constraints using ...
virtual void dresidual_dlambda(std::vector< FuelCell::ApplicationCore::FEVector > &dst, const FuelCell::ApplicationCore::FEVectors &src)
Loop over all cells and assemble the vector that is used to solve the sensitivity equations by using...
void dfunction(std::vector< FuelCell::ApplicationCore::FEVector > &dst, const FuelCell::ApplicationCore::FEVectors &src, bool dfunctional_du, bool dresidual_dlambda)
This is an auxiliary function called by dresidual_dlambda and dfunctional_du.
virtual void dresponses_du(std::vector< FuelCell::ApplicationCore::FEVector > &dst, const FuelCell::ApplicationCore::FEVectors &src)
Loop over all cells and assemble the vector that is used to solve the sensitivity equations by using...
void solve_direct(std::vector< std::vector< double > > &df_dl, const FuelCell::ApplicationCore::FEVectors &sol)
Solver in order to obtain the analytical sensitivities for the given objective and constraints using ...
unsigned int n_obj
Number of objective functions.
Definition: optimization_block_matrix_application.h:453
void set_output_variables(std::vector< std::string > &dakota_name_responses)
Definition: optimization_block_matrix_application.h:347
virtual void dresponses_dl(std::vector< std::vector< double > > &df_dl, const FuelCell::ApplicationCore::FEVectors &src)
Post-processing.
Application handling matrices and assembling linear systems of equations.
Definition: block_matrix_application.h:83
virtual void global_dresponses_du(std::vector< FuelCell::ApplicationCore::FEVector > &df_du, const FuelCell::ApplicationCore::FEVector &src)
This class is used to evaluate the sensitivities of all responses that do not require looping over ce...
void(* CELL_Dvalues)(std::vector< FuelCell::ApplicationCore::FEVector > &, const typename DoFApplication< dim >::CellInfo &, std::vector< std::vector< double > > &)
Definition of a pointer to a function that is called in a loop over cells to compute the derivatives ...
Definition: optimization_block_matrix_application.h:422
virtual void cell_dresponses_du(std::vector< FuelCell::ApplicationCore::FEVector > &df_du, const typename DoFApplication< dim >::CellInfo &info, std::vector< std::vector< double > > &src)
Integration of local bilinear form.
virtual void global_dresponses_dl(std::vector< std::vector< double > > &df_dl, const FuelCell::ApplicationCore::FEVector &src)
This class is used to evaluate the sensitivities of all responses that do not require looping over ce...
std::vector< std::string > name_responses
Member that stores the name of the responses, i.e.
Definition: optimization_block_matrix_application.h:466
OptimizationBlockMatrixApplication(FuelCell::ApplicationCore::DoFApplication< dim > &, bool triangulation_only)
Constructor for an object, owning its own mesh and dof handler.
virtual void initialize(ParameterHandler &param)
Initialize application.
This class is created for the objects handed to the mesh loops.
Definition: mesh_loop_info_objects.h:625
std::vector< std::string > all_response_names
Variable that holds a list of response names.
Definition: optimization_block_matrix_application.h:488
std::vector< std::string > name_design_var
Member that stores the name of the design variables.
Definition: optimization_block_matrix_application.h:461
unsigned int get_n_resp() const
Member function that returns the number of responses.
void set_optimization_parameters(unsigned int &n_dvar, unsigned int &n_resp, std::vector< std::string > &name_design_var, std::vector< std::string > &name_responses)
This routine assigns the value of n_dvar, n_resp, name_design_var and name_responses to the internal ...
virtual void print_responses(std::vector< double > &resp)
This function is used to print the responses to screen (FcstUtilities::log)
virtual void cell_dresidual_dlambda(std::vector< FuelCell::ApplicationCore::FEVector > &cell_vector, const typename DoFApplication< dim >::CellInfo &cell, std::vector< std::vector< double > > &src)
Integration of local bilinear form.
virtual void compute_L1_L2_error_and_convergence_rate(const FuelCell::ApplicationCore::FEVector &solution, const unsigned int &refinement_cycle, std::vector< ConvergenceTable > &convergence_tables) const
If the exact or analytical solution is available, then this function computes L1 and L2 norms of the ...
Definition: optimization_block_matrix_application.h:365
bool output_coarse_solution
Decision variable for whether the solution is to be output for transfer.
Definition: optimization_block_matrix_application.h:475
bool get_bool_transfer_solution()
Function to see if we are transferring a solution on a refined grid to the initial coarse grid...
Definition: optimization_block_matrix_application.h:333
FCSTLogStream log
Object used to output data to file and, if file attached recorded to a file as well.
virtual void responses(std::vector< double > &f, const FuelCell::ApplicationCore::FEVectors &vectors)
Post-processing.
void print_dresponses_du(std::vector< FuelCell::ApplicationCore::FEVector > df_du)
Auxiliary routine to print the values of df_du This routine should be called once df_du is assembled...
Definition: optimization_block_matrix_application.h:390
void print_default_parameter_file()
Print the default parameter handler file.
Definition: optimization_block_matrix_application.h:322
bool boundary_responses
true, if boundary responses are supposed to be computed.
Definition: optimization_block_matrix_application.h:483
std::vector< std::string > name_output_var
Member that stores the name of the output variables, These names will be written to name_responses if...
Definition: optimization_block_matrix_application.h:472
virtual void check_responses()
This class is called by responses to make sure that all responses requested are implemented in either...
virtual void global_responses(std::vector< double > &resp, const FuelCell::ApplicationCore::FEVector &sol)
This class is used to evaluate all responses that do not require looping over cells.
~OptimizationBlockMatrixApplication()
Destructor.
Definition: optimization_block_matrix_application.h:105
virtual void declare_parameters(ParameterHandler &param)
Declare all parameters that are needed for:
unsigned int get_n_dvar() const
Member function that returns the number of design variables.
virtual void cell_responses(std::vector< double > &resp, const typename DoFApplication< dim >::CellInfo &info, const FuelCell::ApplicationCore::FEVector &sol)
This class is used to evaluate all the functionals that require looping over cells.
const std::vector< std::string > get_all_responses_names() const
Function used to return all possible response names in OpenFCST.
Definition: optimization_block_matrix_application.h:306
Objects of this kind are used to notify interior applications of changes provoked by an outer loop...
Definition: event.h:57
std::vector< std::string > get_name_responses() const
Member function that returns the name of the responses.
Definition: optimization_block_matrix_application.h:297
unsigned int n_dvar
Number of design variables.
Definition: optimization_block_matrix_application.h:449
std::vector< std::string > get_name_dvar() const
Member function that returns the name of the design variables.
Definition: optimization_block_matrix_application.h:288
BlockVector< double > FEVector
The vector class used by applications.
Definition: application_data.h:39
The data type used in function calls of Application.
Definition: fe_vectors.h:59
void set_all_response_names()
Initialize the data member all_response_names with all the available Responses in OpenFCST...
Application handling matrices and assembling the linear system to solve the sensitivity equations...
Definition: optimization_block_matrix_application.h:62
bool optimization
Decision variable for whether the application is being used for optimization.
Definition: optimization_block_matrix_application.h:478
void print_dresponses_dl(std::vector< std::vector< double > > pdf_pdl)
Auxiliary routine to print the values df_dl This routine should be called once df_df is assembled...
Definition: optimization_block_matrix_application.h:379
Base class for all linear applications, i.e., all applications requiring Triangulation and DoFHandler...
Definition: dof_application.h:122
boost::shared_ptr< ApplicationData > data
Object for auxiliary data.
Definition: application_base.h:342
virtual void cell_dresponses_dl(std::vector< std::vector< double > > &cell_df_dl, const typename DoFApplication< dim >::CellInfo &info, const FuelCell::ApplicationCore::FEVector &src)
This class is used to evaluate the derivative of all the functionals that require looping over cells ...
static const FuelCell::ApplicationCore::Event sensitivity_analysis
The Event set by OptimizationMatrixApplication if if we want to compute the sensitivities and a new m...
Definition: optimization_block_matrix_application.h:72
const std::string extend_filename(const std::string &, const int precision=3) const
Member function used in order to extend the name of a file with the design variable name and value...
unsigned int n_resp
Number of responses = n_obj + n_con.
Definition: optimization_block_matrix_application.h:457
virtual void bdry_responses(std::vector< double > &resp, const typename DoFApplication< dim >::FaceInfo &info, const FuelCell::ApplicationCore::FEVector &sol)
This class is used to evaluate all the functionals that require looping over boundaries.