source: altifloat/src/floater.h @ 123

Last change on this file since 123 was 123, checked in by kodalazian, 10 years ago

conflicts solved

File size: 11.2 KB
Line 
1#define UPDATE_BCK
2
3//constantes
4YREAL rdtflo=2*3600;
5//YREAL rdtflo=1;
6YREAL ur_dx=0;
7YREAL ur_dy=0;
8YREAL vr_dx=0;
9YREAL vr_dy=0;
10
11
12//Tableaux
13
14//Variable d'état
15YREAL umod[nlon][nlat][jptfl];
16YREAL vmod[nlon][nlat][jptfl];
17
18//Background
19YREAL ubck[nlon][nlat][jptfl];
20YREAL vbck[nlon][nlat][jptfl];
21
22//Trajectoires des flotteurs (en points de grille)
23YREAL piobs[jptfl][jpnfl];
24YREAL pjobs[jptfl][jpnfl];
25YREAL piret[jptfl][jpnfl];
26YREAL pjret[jptfl][jpnfl];
27short pmask[jptfl][jpnfl];
28
29//FILTER COEFFICIENTS
30#ifdef K_FILTER
31//YREAL c1;
32//YREAL c2,c3,c4,c5;
33//YREAL dtfil=26042000;
34YREAL dtfil=2.604166666666667e+07;
35
36#endif
37
38//Options du run incr士ental
39int nb_extiter=16;
40short inc_save=1;
41char dirsave[50]="../obs_float/";
42char savename[50]="optim25float-filtre15-inc";
43extern double Y3ddf1;
44//Declaration pour multirun
45void erase_lobs();
46short is_activ(int jpfl, int it);
47void read_obs(int argv, char *argc[]);
48extern int Yobs_insert_data (char *nmmod, int sortie, int iaxe, int jaxe, int kaxe,int pdt, YREAL val);
49void run_inc(int argv, char *argc[]);
50
51void appli_start(int argc, char *argv[]){
52#ifdef K_FILTER
53  //c1=0.5;
54  //c2=c3=c4=c5
55#endif
56
57  //Init des tableaux
58  for (int i=0;i<nlon;i++)
59    for (int j=0;j<nlat;j++)
60      for (int k=0;k<jptfl;k++) {
61        umod[i][j][k]=0;
62        vmod[i][j][k]=0;
63        ubck[i][j][k]=0;
64        vbck[i][j][k]=0;
65      }
66 for (int i=0;i<jptfl;i++)
67    for (int j=0;j<jpnfl;j++) {
68      piobs[i][j]=0;
69      pjobs[i][j]=0;
70      piret[i][j]=0;
71      pjret[i][j]=0; 
72      pmask[i][j]=0;
73      }
74
75char L1[50]="read_obs";
76char L2[50]="../obs_float/obs_float_test.dat";
77char *L[2];
78 L[0]=L1;
79 L[1]=L2;
80 read_obs(2,L);
81 //Test de isactiv
82 printf("isactiv(0,0):%d\n",is_activ(0,0));
83 printf("isactiv(1,0):%d\n",is_activ(1,0));
84 printf("isactiv(0,3):%d\n",is_activ(0,3));
85 printf("isactiv(1,3):%d\n",is_activ(1,3));
86 printf("isactiv(0,6):%d\n",is_activ(0,6));
87 printf("isactiv(1,6):%d\n",is_activ(1,6));
88
89 
90 // erase_lobs();
91}
92void before_it(int nit){}
93void cost_function(int pdt){}
94void adjust_target(){}
95void after_it(int nit){
96       
97        //printf("u_d(9,16)=%f %f %f %f\n",YS1_u_d(9,16),YS1_u_d(10,16),YS1_u_d(10,17),YS1_u_d(9,17));
98        }
99void forward_before(int ctrp){
100}
101void forward_after(int ctrp){
102}
103
104void backward_before(int ctrp){
105}
106
107void backward_after(int ctrp){
108        }
109
110short select_io(int indic, char *nmod, int sortie, int iaxe,
111                int jaxe, int kaxe, int pdt, YREAL *val){
112        //if(indic==YIO_LOADSTATE && !strcmp(nmod,"u")) {
113        //printf("s=%d t=%d Yi=%d Yj=%d val=%f\n",sortie,pdt,iaxe,jaxe,*val);   
114        //}
115       
116        return(1);
117}
118
119void clear_Yst_nodo(struct Yst_nodo *nobs, int lev, int max) {
120  /* lev est le niveau courant (racine=0)
121max est le niveau des feuilles soit
1224 si dimod=1
1235 si dimod=2
1246 si dimod=3
125dimod est défini au niveau 2 avec dimod=YTabMod[imod].dim
126
127
128  */
129  if (lev==max) {
130    //on nettoie une feuille (ni frere ni fils)
131    free(nobs);
132    nobs = NULL ;
133  }
134 
135  else {
136
137    if (lev==2) {
138      //on peut déterminer le niveau max
139      int dimod=YTabMod[nobs->iind].dim;
140      max=dimod+3;
141    }
142
143    if (nobs->fils!=NULL && nobs->frere!=NULL) {
144      clear_Yst_nodo(nobs->frere,lev+1,max);
145      nobs->frere=NULL;
146    }   
147    if (nobs->fils!=NULL) {
148      clear_Yst_nodo(nobs->fils,lev+1,max);
149      nobs->fils=NULL;
150    }
151    free(nobs);
152    nobs=NULL;
153  } // else (pas feuille)
154 
155
156}
157
158//in case we want to see how it s loading the data we do this above
159void erase_lobs() {
160  //Efface toute l'arboresence des obs (y compris les ébauches)
161  int itraj;
162  struct Yst_nodo *YRobs = NULL;
163  for (itraj=0;itraj<YNBTRAJ;itraj++) {
164    //Effacement des obs liées au modul imod
165    if (YTabMod[itraj].is_target || !YTabMod[itraj].is_cout) {
166      YRobs=YTabTraj[itraj].YRobs;
167      clear_Yst_nodo(YRobs,0,10);
168      YTabTraj[itraj].YRobs=NULL;
169  }
170
171  } //for itraj
172  for (int wi = 0; wi < YNBTRAJ; ++wi)
173    {
174      YTabTraj[wi].YRobs = (struct Yst_nodo*) malloc (sizeof (struct Yst_nodo)); /* init du Root des arborescences */
175      YTabTraj[wi].YRobs->frere = YTabTraj[wi].YRobs->fils = NULL;      /* d'observations pour chaque trajectoire */
176    }
177
178}
179void read_obs(int argv, char *argc[]) {
180  /* Read observation in a ascii file
181     format
182     Column 1 (%d) : dimension (1 : lon(Yi), 2 lat(Yj))
183     Column 2 (%d) : time step (from 1 to jptfl)
184     Column 3 (%d) : idfloat
185     Column 4 (%f) : grid point of the floater
186  */
187
188
189  if (argv!=2) {
190    printf("(read_obs) Error : read_obs with %d (1 nedded)\n",argv-1);
191    printf("No obs read\n");
192    return;
193  }
194  FILE *fid;
195  fid=fopen(argc[1],"r");
196  if (fid==NULL) {
197    printf("(read_obs) Error : unable to open %s\n",argc[1]);
198    return;
199  }
200  int i,it,id;
201  YREAL pos;
202  int status=4;
203  while(status==4) {
204    status=fscanf(fid,"%d %d %d %lf",&i,&it,&id,&pos);
205    if (status!=4) break;
206    pjobs[it-1][id-1]=pos;
207    status=fscanf(fid,"%d %d %d %lf",&i,&it,&id,&pos);
208    if (status!=4 && i!=2) {
209      printf("(read_obs) Error : unable to read obs in %s\n",argc[1]);
210      return;
211    }
212    piobs[it-1][id-1]=pos;
213    pmask[it-1][id-1]=1;
214  }
215  fclose(fid);
216}
217
218void update_uv(){
219  int j,k,t;
220  for (t=0;t<jtlag+1;t++)
221    for (j=0;j<nlon;j++)
222      for (k=0;k<nlat;k++) {
223        YS1_u(j,k,t)+=YS1_u_d(j,k);
224        YS1_v(j,k,t)+=YS1_v_d(j,k);
225        //printf("u=%f delu=%f \n",YS1_u(j,k),YS1_u_d(j,k));   
226
227}
228}
229
230short is_activ(int jpfl, int it) {
231  /* return 1 if float jpfl is active after it (stricly) */
232  it++;
233  while(it<jptfl && pmask[it][jpfl]==0)
234    it++;
235 return(it<jptfl && pmask[it][jpfl]==1);
236}
237
238void load_eb(int iti, int itn) {
239  /*Load background from ubck and vbck from time iti to itn*/
240  int j,k,it;
241  for (it=iti;it<itn;it++)
242    for (j=0;j<nlon;j++)
243      for (k=0;k<nlat;k++) {
244        YS1_u(j,k,it-iti)=ubck[j][k][it];
245        YS1_v(j,k,it-iti)=vbck[j][k][it];
246      }
247}
248
249void save_uv(int iti, int itn) {
250  /* save u,v fields in umod,vmod
251   and save piret,pjret*/
252  int j,k,it;
253  int Yifloat=0;
254  for (it=iti;it<itn;it++) {
255    Yifloat=0;
256    for (j=0;j<nlon;j++)
257      for (k=0;k<nlat;k++) {
258        umod[j][k][it]=YS1_u(j,k,it-iti);
259        vmod[j][k][it]=YS1_v(j,k,it-iti);
260#ifdef UPDATE_BCK
261        ubck[j][k][it]=YS1_u(j,k,it-iti);
262        vbck[j][k][it]=YS1_v(j,k,it-iti);
263#endif
264      }
265    for (j=0;j<jpnfl;j++) 
266      if (pmask[iti][j]==1 && is_activ(j,iti)>0 && it>iti) {
267        pjret[it][j]=YS1_r_float(Yifloat,it-iti);
268        piret[it][j]=YS2_r_float(Yifloat,it-iti);
269        Yifloat++;
270      }
271  }
272}
273
274void save_output_rfloat (int argc, char *argv[]) {
275  FILE *fid;
276  fid=fopen(argv[1],"w");
277  if (fid==NULL) {
278    printf("\nfailed to open %s",argv[1]);
279    exit(3);
280  }
281  int j,it;
282  for (j=0;j<jpnfl;j++)
283    for (it=0;it<jptfl;it++)
284     
285      fprintf(fid,"%d %d %f %f\n",it,j,piret[it][j],pjret[it][j]);
286       
287                                             
288  fclose(fid);
289
290}
291
292void save_output_uv (int argc, char *argv[]) {
293  FILE *fid;
294  fid=fopen(argv[1],"w");
295  if (fid==NULL) {
296    printf("\nfailed to open %s",argv[1]);
297    exit(3);
298  }
299  int j,k,it;
300    for (it=0;it<jptfl;it++)
301      for (j=0;j<nlon;j++)
302        for (k=0;k<nlat;k++) {
303          fprintf(fid,"%d %d %d %f %f\n",it,j,k,umod[j][k][it],vmod[j][k][it]);
304        }
305                                             
306  fclose(fid);
307
308}
309
310
311
312void load_init(int it) {
313  /*Load floater position at time it as init.
314  Only floats that have another position after it
315  Warning : nbre de flotteurs maxiums : nfloat */
316  int jfl;
317  int Yifloat=0;
318  for (jfl=0;jfl<jpnfl;jfl++) {
319    if (pmask[it][jfl]==1 && is_activ(jfl,it)>0) {
320      //Chargemente de l'init
321      YS1_r_float(Yifloat,0)=pjobs[it][jfl];
322      YS2_r_float(Yifloat,0)=piobs[it][jfl];
323       
324      //Chargement des obs
325      for (int itfl=it+1;itfl<jptfl;itfl++) {
326        if (pmask[itfl][jfl]==1) {
327          // Yobs_insert_data (char *nmmod, int sortie, int iaxe, int jaxe, int kaxe,int pdt, YREAL val)
328          Yobs_insert_data("r_cout_d",0,Yifloat,0,0,itfl-it,pjobs[itfl][jfl]);
329          Yobs_insert_data("r_cout_d",1,Yifloat,0,0,itfl-it,piobs[itfl][jfl]);
330        } //if pmask==1
331        } //for itfl
332     
333      Yifloat++; 
334    } //if pmask==1
335    } //for jfl
336 
337 
338  }
339
340void multirun() {
341  int niti=0; //Temps init
342  int nitn=jtlag;//Temps fin
343    while(nitn<jptfl) {
344     
345      //Effacement de toutes les obs précédentes
346      erase_lobs();
347
348      // Chargement de toutes les init et obs dans les bon pas de temps
349      load_init(niti);
350
351      // Réglage de l'ébauche
352      load_eb(niti,nitn);
353
354      // Run_inc
355      run_inc(0,NULL);
356
357      // Sauvegarde du résultat dans les tableaux
358      save_uv(niti,nitn);
359
360
361      niti=nitn;
362      nitn=niti+jtlag;
363    } //while nitn<jptfl
364}
365
366void erase_udvd() {
367  int j,k;
368  for (j=0;j<nlon;j++)
369        for (k=0;k<nlat;k++) {
370#ifdef K_FILTER
371          YS1_uc_d(j,k)=0;
372          YS1_vc_d(j,k)=0;
373#else     
374          YS1_u_d(j,k)=0;
375          YS1_v_d(j,k)=0;
376#endif
377}
378}
379//void load_aviso(int argc, char *argv[]){
380       
381//      }
382
383void run_inc(int argv, char *argc[]) {
384//Run the incremental optimisation
385//1 forward of the complete model
386//2 initialize du et dv to zero
387//3 minimize the incremental cost function
388//4 update u and v
389
390/*
391Options
392nb_extiter : number of extern loop (default=6)
393inc_save : save option
394        0  : no save
395        1 (default): save after each extern loop r_float,u and v
396dirsave : directory in case inc_save>0 (default="../obs_float/")
397savename : basename of save fils (default "optim")
398*/
399char filesave[100];
400int i;
401
402char sactiv[20]="activ";
403char sMD[20]="M";
404char sonly[20]="only";
405char *liste[3];
406liste[0]=sactiv;
407liste[1]=sMD;
408liste[2]=sonly;
409/*Check M1QN3 config*/
410 if (YNbItRun<=0)
411                {        printf("runm(2): number of run iteration not seted; use set_nbiter please\n");
412                         //return(0);
413                }
414        if (Y3ddf1<=0)
415                {       printf("runm(2): expected positive fcost decrease missed; use setm_ddf1\n");
416                         //return(0);
417                }
418        if (YioInsertObsCtr<0)
419                   printf("runm(2): warning : oh oh, run with no obs !!! \n");
420               
421        YTypeAdjust = ADJUST_M1QN3; //d'office avec M1QN3
422       
423//Savestate config
424 YioModulot = OFF; //On sauvegarde tous les pas de temps
425    YioWrite = ON;
426    YioRead= OFF;
427   // Yio_savestate(cdes[1], cdes[3], 0, cdes[7]);
428    YioState=0 ;//Save all states
429    YioBin=OFF; //ascii output
430    YioAscii=ON; //ascii output
431    YioAxes=ON; //save axe numbers
432
433for (i=0;i<nb_extiter;i++) {
434     strcpy(liste[1],"M");
435         Yactraj(3, liste);
436         Yset_modeltime(0);
437         before_it(1);
438         //printf("---forward(i=%d)---\n",i);
439         Yforward(-1, 0);
440         
441         if (inc_save==1 && i>0) {
442          //save rfloat
443          YioTime=ON;
444          sprintf(filesave,"%sr_float_%s_%d.dat",dirsave,savename,i);
445          Yio_savestate("r_float","i",0,filesave);
446         }
447             strcpy(liste[1],"D");
448         Yactraj(3,liste);
449         Yset_modeltime(0);
450         
451       
452        Y3run ('0');
453#ifdef K_FILTER
454        //compute values for u_d and v_d
455        strcpy(liste[1],"Tfil");
456        Yactraj(3,liste);
457        Yset_modeltime(0);
458        before_it(1);
459        Yforward(-1, 0);
460        strcpy(liste[1],"T_euler_d");
461        Yactraj(3,liste);
462        Yset_modeltime(0);
463        before_it(1);
464        Yforward(-1, 0);
465#endif
466
467
468    update_uv();
469    erase_udvd();
470
471    if (inc_save==1) {
472   
473   
474    //save u
475    YioTime=ON;
476    sprintf(filesave,"%su%s%d.dat",dirsave,savename,i+1);
477    Yio_savestate("u","ij",0,filesave);
478   
479    //save v
480    sprintf(filesave,"%sv%s%d.dat",dirsave,savename,i+1);
481    Yio_savestate("v","ij",0,filesave);
482   
483    }
484    }
485    /*End of the optimization, we run another time the model to compute the final value of r_float
486    */
487     strcpy(liste[1],"M");
488    Yactraj(3, liste);
489         Yset_modeltime(0);
490         before_it(1);
491         Yforward(-1, 0);
492 if (inc_save==1) {
493          //save rfloat
494          YioTime=ON;
495          sprintf(filesave,"%sr_float_%s_%d.dat",dirsave,savename,nb_extiter);
496          Yio_savestate("r_float","i",0,filesave);
497         }
498   
499}
500
501
Note: See TracBrowser for help on using the repository browser.