QNANO
Tao_Minimizer.h
1 #ifndef QNANO_NEW_TAO_MINIMIZER_DEFINED_H
2 #define QNANO_NEW_TAO_MINIMIZER_DEFINED_H
3 
4 
5 #include "tools/Parameter_Pointer_Function.h"
6 #include "slepc/SLEPc_Register_Parameter_Map.h"
7 
9 public:
10  Tao tao;
11  Parameter_Map *param_map;
12 
13 static PetscErrorCode Objective_Function(Tao tao,Vec v,PetscReal* result,void* param){
14  PetscErrorCode ierr;
16 
17  const double *cptr;
18  ierr=VecGetArrayRead(v,(const PetscScalar **)&cptr); CHKERRQ(ierr);
19  *result=(PetscReal) fct->f(cptr);
20  ierr=VecRestoreArrayRead(v,(const PetscScalar **) &cptr); CHKERRQ(ierr);
21  return ierr;
22 }
23 
24  PetscErrorCode Set_Initial_Vector(Parameter_Pointer_Function &ppf){
25  PetscErrorCode ierr;
26  Vec initialvec;
27  ierr=VecCreate(PETSC_COMM_WORLD,&initialvec);CHKERRQ(ierr);
28  ierr=VecSetSizes(initialvec,PETSC_DECIDE,ppf.get_DIM());CHKERRQ(ierr);
29  ierr=VecSetFromOptions(initialvec);CHKERRQ(ierr);
30 
31  double *ptr;
32  ierr=VecGetArray(initialvec,(PetscScalar **)&ptr);CHKERRQ(ierr);
33  ppf.get_params(ptr);
34 
35  ierr=VecRestoreArray(initialvec,(PetscScalar **)&ptr);CHKERRQ(ierr);
36  ierr=TaoSetInitialVector(tao,initialvec);CHKERRQ(ierr);
37  ierr=VecDestroy(&initialvec);CHKERRQ(ierr);
38  return ierr;
39 }
40  void minimize(Parameter_Pointer_Function &ppf){
41  Set_Initial_Vector(ppf);
42  TaoSetObjectiveRoutine(tao, Tao_Minimizer::Objective_Function, (void*)&ppf);
43 
44  TaoSolve(tao);
45  }
46  Tao_Minimizer(Parameter_Map &param_map_){
47  param_map=&param_map_;
48  PetscInitialize(param_map->p_args,param_map->p_argv,(char*)0,0);
49  TaoCreate(PETSC_COMM_WORLD,&tao);
50  TaoSetType(tao, TAONM);
51  TaoSetFromOptions(tao);
52  PetscViewer monviewer;
53  PetscViewerASCIIOpen(PETSC_COMM_WORLD,"stdout",&monviewer);
54  TaoSetMonitor(tao,TaoMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);
55 
56  }
57  ~Tao_Minimizer(){
58  TaoDestroy(&tao);
59  SLEPc_Register_Parameter_Map(*param_map);
60  PetscFinalize();
61  }
62 };
63 
64 
65 #endif
Definition: Tao_Minimizer.h:8
Definition: Parameter_Map.h:12
virtual void get_params(double *d_start) const
copy Parameters int the specified double vector
Definition: Parameter_Pointer_Function.h:19
Definition: Parameter_Pointer_Function.h:7
virtual size_t get_DIM() const
get dimension
Definition: Parameter_Pointer_Function.h:12