Ice Sheet System Model  4.18
Code documentation
M1qn3.cpp
Go to the documentation of this file.
1 
5 #include "./M1qn3.h"
6 
7 #ifdef _HAVE_M1QN3_
8 /*m1qn3 prototypes {{{*/
9 extern "C" void *ctonbe_; // DIS mode : Conversion
10 extern "C" void *ctcabe_; // DIS mode : Conversion
11 extern "C" void *euclid_; // Scalar product
12 typedef void (*SimulFunc) (long* indic,long* n, double* x, double* pf,double* g,long [],float [],void* dzs);
13 extern "C" void m1qn3_ (void f(long* indic,long* n, double* x, double* pf,double* g,long [],float [],void* dzs),
14  void **, void **, void **,
15  long *, double [], double *, double [], double*, double *,
16  double *, char [], long *, long *, long *, long *, long *, long *, long [], double [], long *,
17  long *, long *, long [], float [],void* );
18 /*Cost function prototype*/
19 void fakesimul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs);
20 typedef struct {
21  int priorn;
22  int counter;
23  double* Gs;
24  double* Xs;
25  double* Js;
26 } Data;
27 /*}}}*/
28 void M1qn3Usage(void){/*{{{*/
29  _printf0_(" usage:\n");
30  _printf0_(" X=M1qn3(Xs,Gs);\n");
31  _printf0_(" where:\n");
32  _printf0_(" Xs are the X values (m x n, where m is the number of independents, n the number of evaluations previously carried out on X)\n");
33  _printf0_(" Gs are the G (gradient) values (m x n, where m is the number of independents, n the number of evaluations previously carried out on X,G)\n");
34  _printf0_(" X - the new direction.\n");
35  _printf0_("\n");
36 }/*}}}*/
37 #endif
38 WRAPPER(M1qn3_python){
39 
40  /*Boot module: */
41  MODULEBOOT();
42 
43 #ifdef _HAVE_M1QN3_
44  /*input: */
45  double* Xs=NULL;
46  double* Gs=NULL;
47  double* Js=NULL;
48  int intn;
49  int priorn;
50  Data data_struct;
51 
52  /* output: */
53  double* X_out = NULL;
54 
55  /*intermediary: */
56  double f;
57  double dxmin=0.01;
58  double gttol=0.0001;
59  long omode;
60 
61  /*checks on arguments on the matlab side: */
62  if(nlhs!=NLHS || nrhs!=3){
63  M1qn3Usage();
64  _error_("M1qn3 usage error");
65  }
66 
67  /*Input datasets: */
68  FetchData(&Xs,&intn,&priorn,XHANDLE);
69  FetchData(&Gs,&intn,&priorn,GHANDLE);
70  FetchData(&Js,&priorn,JHANDLE);
71 
72  /*Initialize M1QN3 parameters*/
73  SimulFunc costfuncion = &fakesimul; /*Cost function address*/
74  void** prosca = &euclid_; /*Dot product function (euclid is the default)*/
75  char normtype[] = "dfn"; /*Norm type: dfn = scalar product defined by prosca*/
76  long izs[5]; /*Arrays used by m1qn3 subroutines*/
77  long iz[5]; /*Integer m1qn3 working array of size 5*/
78  float rzs[1]; /*Arrays used by m1qn3 subroutines*/
79  long impres = 0; /*verbosity level*/
80  long imode[3] = {0}; /*scaling and starting mode, 0 by default*/
81  long indic = 4; /*compute f and g*/
82  long reverse = 0; /*reverse or direct mode*/
83  long io = 6; /*Channel number for the output*/
84 
85  /*Optimization criterions*/
86  long niter = long(intn); /*Maximum number of iterations*/
87  long nsim = long(intn); /*Maximum number of function calls*/
88 
89  /*Get problem dimension and initialize X, G and f: */
90  long n = long(intn);
91  IssmPDouble* G = xNew<IssmPDouble>(n); for (int i=0;i<n;i++)G[i]=Gs[i*priorn+0];
92  IssmPDouble* X = xNew<IssmPDouble>(n); for (int i=0;i<n;i++)X[i]=Xs[i*priorn+0];
93  f = Js[0];
94 
95  /*Allocate m1qn3 working arrays (see doc)*/
96  long m = 100;
97  long ndz = 4*n+m*(2*n+1);
98  double* dz = xNew<double>(ndz);
99  double f1=f;
100 
101  /*Initialize: */
102  data_struct.priorn=priorn;
103  data_struct.counter=0;
104  data_struct.Gs=Gs;
105  data_struct.Js=Js;
106  data_struct.Xs=Xs;
107 
108  m1qn3_(costfuncion,prosca,&ctonbe_,&ctcabe_,
109  &n,X,&f,G,&dxmin,&f1,
110  &gttol,normtype,&impres,&io,imode,&omode,&niter,&nsim,iz,dz,&ndz,
111  &reverse,&indic,izs,rzs,(void*)&data_struct);
112 
113  switch(int(omode)){
114  case 0: /* _printf0_(" Stop requested\n");*/ break;
115  case 1: _printf0_(" Convergence reached (gradient satisfies stopping criterion)\n"); break;
116  case 2: _printf0_(" Bad initialization\n"); break;
117  case 3: _printf0_(" Line search failure\n"); break;
118  case 4: _printf0_(" Maximum number of iterations exceeded\n");break;
119  case 5: _printf0_(" Maximum number of function calls exceeded\n"); break;
120  case 6: _printf0_(" stopped on dxmin during line search\n"); break;
121  case 7: _printf0_(" <g,d> > 0 or <y,s> <0\n"); break;
122  default: _printf0_(" Unknown end condition\n");
123  }
124 
125  /*build output: */
126  X_out=xNew<IssmPDouble>(n);
127  for(int i=0;i<n;i++)X_out[i]=X[i];
128 
129  /*Write data: */
130  WriteData(XOUT,X_out,n);
131 
132  /*end module: */
133  xDelete<double>(Xs);
134  xDelete<double>(Gs);
135  xDelete<double>(Js);
136  xDelete<double>(X_out);
137  xDelete<double>(G);
138  xDelete<double>(X);
139  #else
140  _error_("m1qn3 is not installed");
141  #endif
142  MODULEEND();
143 
144 }
145 
146 
147 #ifdef _HAVE_M1QN3_
148 void fakesimul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs){
149 
150  Data* ds=(Data*)dzs;
151  double* Xs=ds->Xs;
152  double* Gs=ds->Gs;
153  double* Js=ds->Js;
154 
155  /*Are we done? : */
156  if(ds->counter+1==ds->priorn){
157  *indic=0;
158  return;
159  }
160  else{
161  ds->counter++;
162  *pf=Js[ds->counter];
163  for(int i=0;i<*n;i++) X[i]=Xs[ds->priorn*i+ds->counter];
164  for(int i=0;i<*n;i++) G[i]=Gs[ds->priorn*i+ds->counter];
165  }
166 }
167 #endif
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
WriteData
void WriteData(IssmPDouble **pmatrix, int *pnel, int *matrix, int M, int N)
Definition: WriteJavascriptData.cpp:16
FetchData
void FetchData(char **pstring, char *stringin)
Definition: FetchJavascriptData.cpp:16
M1qn3.h
: prototype for Data Interpolation mex module.
NLHS
#define NLHS
Definition: BamgConvertMesh.h:50
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
IssmPDouble
IssmDouble IssmPDouble
Definition: types.h:38
WRAPPER
WRAPPER(M1qn3_python)
Definition: M1qn3.cpp:38