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 // $Id: optimization_block_matrix_application.h 1354 2013-08-17 00:01:22Z secanell $
3 //
4 // Copyright (C) 2006 by Marc Secanell
5 //
6 // This file is subject to QPL and may not be distributed
7 // without copyright and license information. Please refer
8 // to the file deal.II/doc/license.html for the text and
9 // further information on this license.
10 //
11 //---------------------------------------------------------------------------
12 
13 #ifndef __appframe__optimization_block_application_h
14 #define __appframe__optimization_block_application_h
15 
16 #include <base/parameter_handler.h>
17 #include <lac/vector.h>
18 #include <lac/block_vector.h>
19 #include <grid/grid_refinement.h>
20 #include <grid/grid_tools.h>
21 #include <grid/persistent_tria.h>
22 #include <dofs/dof_handler.h>
23 #include <dofs/dof_renumbering.h>
24 #include <fe/fe.h>
25 #include <fe/fe_values.h>
26 #include <numerics/solution_transfer.h>
27 #include <boost/shared_ptr.hpp>
28 //#include <numerics/mesh_worker.h>
29 //#include <numerics/mesh_worker_loop.h>
30 
31 #include <appframe/base.h>
33 #include <appframe/matrix_base.h>
37 
38 #include <iostream>
39 #include <fstream>
40 #include <sstream>
41 #include <vector>
42 
43 namespace AppFrame
44 {
45 
50  template<int dim> class OptimizationBlockMatrixApplication;
51 
66  template <int dim>
67  class LocalResponse :
68  public MeshWorker::Assembler::Functional<double>,
70  {
71  private:
72  SmartPointer<OptimizationBlockMatrixApplication<dim> > app;
73 
74  public:
75 
80  unsigned int m)
81  {
82  app = a;
84  }
85 
89  void cell(const typename AppFrame::DoFApplication<dim>::CellInfo& cell_info);
90 
95 
99  void face(const typename AppFrame::DoFApplication<dim>::FaceInfo& face1,
100  const typename AppFrame::DoFApplication<dim>::FaceInfo& face2);
101  };
102 
113  template <int dim>
116  {
117  public:
136  OptimizationBlockMatrixApplication(boost::shared_ptr<AppFrame::ApplicationData> data =
137  boost::shared_ptr<AppFrame::ApplicationData>());
138 
151  bool triangulation_only);
152 
155 
162  virtual void declare_parameters(ParameterHandler& param);
163 
170  virtual void set_parameters(const std::vector<std::string>& name_dvar,
171  const std::vector<double>& value_dvar,
172  ParameterHandler& param);
173 
176  virtual void initialize(ParameterHandler& param);
177 
181  virtual void init_solution(AppFrame::FEVector& src) = 0;
182 
189  virtual void responses (std::vector<double>& f,
190  const AppFrame::FEVectors& vectors);
195  virtual void check_responses();
196 
202  virtual void cell_responses (std::vector<double>& resp,
203  const typename DoFApplication<dim>::CellInfo& info,
204  const AppFrame::FEVector& sol);
205 
210  virtual void global_responses (std::vector<double>& resp,
211  const AppFrame::FEVector& sol);
212 
214  virtual void print_responses (std::vector<double>& resp);
215 
216 
229  virtual void dresponses_dl (std::vector<std::vector<double> >& df_dl,
230  const AppFrame::FEVectors& src);
236  virtual void cell_dresponses_dl(std::vector<std::vector<double> >& cell_df_dl,
237  const typename DoFApplication<dim>::CellInfo& info,
238  const AppFrame::FEVector& src);
244  virtual void global_dresponses_dl(std::vector<std::vector<double> >& df_dl,
245  const AppFrame::FEVector& src);
261  virtual void dresponses_du(std::vector<AppFrame::FEVector>& dst,
262  const AppFrame::FEVectors& src);
263 
268  virtual void cell_dresponses_du(std::vector<AppFrame::FEVector>& df_du,
269  const typename DoFApplication<dim>::CellInfo& info,
270  std::vector<std::vector<double> >& src);
271 
277  virtual void global_dresponses_du(std::vector<AppFrame::FEVector>& df_du,
278  const AppFrame::FEVector& src);
279 
295  virtual void dresidual_dlambda(std::vector<AppFrame::FEVector>& dst,
296  const AppFrame::FEVectors& src);
301  virtual void cell_dresidual_dlambda(std::vector<AppFrame::FEVector>& cell_vector,
302  const typename DoFApplication<dim>::CellInfo& cell,
303  std::vector<std::vector<double> >& src);
304 
316  void solve_direct (std::vector<std::vector<double> >& df_dl,
317  const AppFrame::FEVectors& sol);
318 
330  void solve_adjoint (std::vector<std::vector<double> >& df_dl,
331  const AppFrame::FEVector& sol);
335  unsigned int get_n_resp() const;
339  unsigned int get_n_dvar() const;
343  inline std::vector<std::string> get_name_dvar() const
344  { return name_design_var;}
345 
349  inline std::vector<std::string> get_name_responses() const
350  { return name_responses;}
351 
355  /*void enter_data_scalar(std::string name,
356  const double& scalar)
357  {
358  this->get_data()->enter(name, scalar);
359  }*/
360 
365  {
366  ParameterHandler prm;
367  this->declare_parameters(prm);
368  std::ofstream file;
369  file.open("Default_parameter_file.prm");
370  prm.print_parameters (file, ParameterHandler::Text);
371  file.close();
372  }
373 
376  {return output_coarse_solution;}
377 
378  virtual void set_read_initial_solution(bool &read)
379  {
381  }
382 
388  void set_optimization_parameters(unsigned int &n_dvar, unsigned int &n_resp,
389  std::vector<std::string> &name_design_var, std::vector<std::string> &name_responses);
390 
394  void set_output_variables(std::vector<std::string> &dakota_name_responses)
395  {
396  name_responses = dakota_name_responses;
397  };
398 
399 
400  protected:
405  void print_dresponses_dl(std::vector<std::vector<double> > pdf_pdl)
406  {
407  for (unsigned int i=0; i<n_resp; ++i)
408  for (unsigned int j=0; j<n_dvar; ++j)
409  std::cout<<"Df"<<i<<"/Dl"<<j<<" is equal to "<<pdf_pdl[i][j]<<std::endl;
410  }
411 
416  void print_dresponses_du(std::vector<AppFrame::FEVector> df_du)
417  {
418  // Write partial df_du:
419 
420  for (unsigned int r=0; r<n_resp; ++r)
421  {
422  // Output Du block by block
423  unsigned int n_blocks = df_du[r].n_blocks();
424  unsigned int size = df_du[r].size();
425  unsigned int comp_per_block = size/n_blocks;
426  for (unsigned int i=0; i<n_blocks; ++i)
427  for (unsigned int j=0; j<comp_per_block; ++j)
428  {
429  std::cout<<"Element "<<j<<" in block "<<i<<" in STEP (Du) is "<<df_du[r].block(i)(j)<<std::endl;
430  }
431  std::cout<<"NORM OF Df"<<r<<"/Du = "<<df_du[r].l2_norm()<<std::endl;
432  }
433  }
434 
440  typedef void (*CELL_Dvalues) (std::vector<AppFrame::FEVector>& ,
441  const typename DoFApplication<dim>::CellInfo& ,
442  std::vector<std::vector<double> >& );
443 
453  void dfunction (std::vector<AppFrame::FEVector>& dst,
454  const AppFrame::FEVectors& src,
455  bool dfunctional_du,
456  bool dresidual_dlambda);
457 
458 
462  unsigned int n_dvar;
466  unsigned int n_obj;
470  unsigned int n_resp;
474  std::vector<std::string> name_design_var;
479  std::vector<std::string> name_responses;
480 
485  std::vector<std::string> name_output_var;
486 
489 
492 
495  };
496 
497 }
498 
499 #endif