source: altifloat/src/floater.h @ 142

Last change on this file since 142 was 142, checked in by jbrlod, 9 years ago

minor bug on assim_t

File size: 15.0 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//Mode Forward ou assim;
13typedef enum {ASSIM,FORW} assim_t;
14
15assim_t tassim = ASSIM;
16
17//Tableaux
18
19//Variable d'état
20YREAL umod[nlon][nlat][jptfl];
21YREAL vmod[nlon][nlat][jptfl];
22
23//Background
24YREAL ubck[nlon][nlat][jptfl];
25YREAL vbck[nlon][nlat][jptfl];
26int mask[nlon][nlat];
27
28//Trajectoires des flotteurs (en points de grille)
29YREAL piobs[jptfl][jpnfl];
30YREAL pjobs[jptfl][jpnfl];
31YREAL piret[jptfl][jpnfl];
32YREAL pjret[jptfl][jpnfl];
33short pmask[jptfl][jpnfl];
34
35//FILTER COEFFICIENTS
36#ifdef K_FILTER
37//YREAL c1;
38//YREAL c2,c3,c4,c5;
39//YREAL dtfil=26042000;
40YREAL dtfil=2.604166666666667e+07;
41
42#endif
43
44//Options du run incr士ental
45int nb_extiter=4;
46short inc_save=0;
47char dirsave[50]="../obs_float/";
48char savename[50]="optim25float-filtre15-inc";
49extern double Y3ddf1;
50//Declaration pour multirun
51void erase_lobs();
52short is_activ(int jpfl, int it);
53void read_obs(int argv, char *argc[]);
54extern int Yobs_insert_data (char *nmmod, int sortie, int iaxe, int jaxe, int kaxe,int pdt, YREAL val);
55void run_inc(int argv, char *argc[]);
56
57void appli_start(int argc, char *argv[]){
58#ifdef K_FILTER
59  //c1=0.5;
60  //c2=c3=c4=c5
61#endif
62
63  //Init des tableaux
64  for (int i=0;i<nlon;i++)
65    for (int j=0;j<nlat;j++) {
66        mask[i][j]=1;   
67      for (int k=0;k<jptfl;k++) {
68        umod[i][j][k]=0;
69        vmod[i][j][k]=0;
70        ubck[i][j][k]=0;
71        vbck[i][j][k]=0;
72      }
73}
74 for (int i=0;i<jptfl;i++)
75    for (int j=0;j<jpnfl;j++) {
76      piobs[i][j]=0;
77      pjobs[i][j]=0;
78      piret[i][j]=0;
79      pjret[i][j]=0; 
80      pmask[i][j]=0;
81      }
82
83char L1[50]="read_obs";
84char L2[50]="../obs_float/obs_float_test.dat";
85char *L[2];
86 L[0]=L1;
87 L[1]=L2;
88 read_obs(2,L);
89 //Test de isactiv
90 //printf("isactiv(0,0):%d\n",is_activ(0,0));
91 //printf("isactiv(1,0):%d\n",is_activ(1,0));
92 //printf("isactiv(0,3):%d\n",is_activ(0,3));
93 //printf("isactiv(1,3):%d\n",is_activ(1,3));
94 //printf("isactiv(0,6):%d\n",is_activ(0,6));
95 //printf("isactiv(1,6):%d\n",is_activ(1,6));
96
97 
98 // erase_lobs();
99}
100void before_it(int nit){}
101void cost_function(int pdt){}
102void adjust_target(){}
103void after_it(int nit){
104       
105        //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));
106        }
107void forward_before(int ctrp){
108}
109void forward_after(int ctrp){
110
111}
112
113void backward_before(int ctrp){
114}
115
116void backward_after(int ctrp){
117        }
118
119short select_io(int indic, char *nmod, int sortie, int iaxe,
120                int jaxe, int kaxe, int pdt, YREAL *val){
121        //if(indic==YIO_LOADSTATE && !strcmp(nmod,"u")) {
122        //printf("s=%d t=%d Yi=%d Yj=%d val=%f\n",sortie,pdt,iaxe,jaxe,*val);   
123        //}
124       
125        return(1);
126}
127
128void clear_Yst_nodo(struct Yst_nodo *nobs, int lev, int max) {
129  /* lev est le niveau courant (racine=0)
130max est le niveau des feuilles soit
1314 si dimod=1
1325 si dimod=2
1336 si dimod=3
134dimod est défini au niveau 2 avec dimod=YTabMod[imod].dim
135
136
137  */
138  if (lev==max) {
139    //on nettoie une feuille (ni frere ni fils)
140    free(nobs);
141    nobs = NULL ;
142  }
143 
144  else {
145
146    if (lev==2) {
147      //on peut déterminer le niveau max
148      int dimod=YTabMod[nobs->iind].dim;
149      max=dimod+3;
150    }
151
152    if (nobs->fils!=NULL && nobs->frere!=NULL) {
153      clear_Yst_nodo(nobs->frere,lev+1,max);
154      nobs->frere=NULL;
155    }   
156    if (nobs->fils!=NULL) {
157      clear_Yst_nodo(nobs->fils,lev+1,max);
158      nobs->fils=NULL;
159    }
160    free(nobs);
161    nobs=NULL;
162  } // else (pas feuille)
163 
164
165}
166
167//in case we want to see how it s loading the data we do this above
168void erase_lobs() {
169  //Efface toute l'arboresence des obs (y compris les ébauches)
170  int itraj;
171  struct Yst_nodo *YRobs = NULL;
172  for (itraj=0;itraj<YNBTRAJ;itraj++) {
173    //Effacement des obs liées au modul imod
174    if (YTabMod[itraj].is_target || !YTabMod[itraj].is_cout) {
175      YRobs=YTabTraj[itraj].YRobs;
176      clear_Yst_nodo(YRobs,0,10);
177      YTabTraj[itraj].YRobs=NULL;
178  }
179
180  } //for itraj
181  for (int wi = 0; wi < YNBTRAJ; ++wi)
182    {
183      YTabTraj[wi].YRobs = (struct Yst_nodo*) malloc (sizeof (struct Yst_nodo)); /* init du Root des arborescences */
184      YTabTraj[wi].YRobs->frere = YTabTraj[wi].YRobs->fils = NULL;      /* d'observations pour chaque trajectoire */
185    }
186
187}
188
189void read_mask(int argv, char *argc[]) {
190char fname[50];
191if (argv==1) {
192        sprintf(fname,"../obs_float/mask.dat");
193}
194if (argv==2) {
195        sprintf(fname,argc[1]);
196}
197if (argv>2)
198 {
199        fprintf(stderr,"(read_mask) Error %d : bad number of arguments\n",argv);
200        return;
201}
202FILE *fid;
203fid=fopen(fname,"r");
204if (fid==NULL) {
205   fprintf(stderr,"(read_mask) Error : unable to open %s\n",fname);
206   return;
207 }
208 int ilon,ilat,status;
209float val;
210for (ilat=0;ilat<nlat;ilat++)
211        for (ilon=0;ilon<nlon;ilon++) {
212        status=fscanf(fid,"%f",&val);
213        mask[ilon][ilat]=(int)val;
214if (status!=1) {
215        fprintf(stderr,"(read_mask) unable to read mask value (lon=%d,lat=%d)\n",ilon,ilat);
216        fclose(fid);   
217        return;
218}
219}
220fclose(fid);
221
222}
223
224void read_bck (int argv, char *argc[]) {
225/* Read background in a ascii file
226   format
227   Column 1 (%d) : time step (from 1 to jptfl)
228   Column 2 (%d) : lon (Yi) (from 1 to nlon)
229   Column 3 (%d) : lat (Yj) (from 1 to nlat)
230   Column 4 (%f) : u value
231   Column 5 (%f) : v value
232*/
233
234if (argv!=2) {
235  fprintf(stderr,"(read_bck) Error : read_bck with %d (1 nedded)\n",argv-1);
236  fprintf(stderr,"No background read\n");
237  return;
238 }
239 FILE *fid;
240 fid=fopen(argc[1],"r");
241 if (fid==NULL) {
242   fprintf(stderr,"(read_bck) Error : unable to open %s\n",argc[1]);
243   return;
244 }
245 
246 int it,ilon,ilat,count;
247 count=0;
248 YREAL uval,vval;
249 int status=5;
250 while (status==5) {
251   status=fscanf(fid,"%d %d %d %lf %lf",&it,&ilon,&ilat,&uval,&vval);
252   if (status!=5) break;
253   if (ilon>nlon || ilon<=0 || ilat>nlat || ilat<=0 || it<=0 || it>jptfl) {
254     fprintf(stderr,"(read_bck) Error : bad coordinate values in %s (ilon=%d, ilat=%d, it=%d)\n",argc[1],ilon,ilat,it);
255     fclose(fid);
256     return;
257   }
258   ubck[ilon-1][ilat-1][it-1]=uval;
259   vbck[ilon-1][ilat-1][it-1]=vval;
260   count++;
261 }
262 if (count%(nlon*nlat)!=0) 
263   fprintf(stderr,"(read_bck) Warning : only %d data was loaded(nlon*nlat=%d)\n",count,nlon*nlat);
264 fclose(fid);
265 fprintf(stdout,"(read_bck) %d was loaded as background\n",count);
266 
267}
268 
269void read_obs(int argv, char *argc[]) {
270  /* Read observation in a ascii file
271     format
272     Column 1 (%d) : dimension (1 : lon(Yi), 2 lat(Yj))
273     Column 2 (%d) : time step (from 1 to jptfl)
274     Column 3 (%d) : idfloat
275     Column 4 (%f) : grid point of the floater
276  */
277
278
279  if (argv!=2) {
280    printf("(read_obs) Error : read_obs with %d (1 nedded)\n",argv-1);
281    printf("No obs read\n");
282    return;
283  }
284  FILE *fid;
285  int count=0;
286  fid=fopen(argc[1],"r");
287  if (fid==NULL) {
288    printf("(read_obs) Error : unable to open %s\n",argc[1]);
289    return;
290  }
291  int i,it,id;
292  YREAL pos;
293  int status=4;
294  while(status==4) {
295    status=fscanf(fid,"%d %d %d %lf",&i,&it,&id,&pos);
296    if (status!=4) break;
297    pjobs[it-1][id-1]=pos;
298    status=fscanf(fid,"%d %d %d %lf",&i,&it,&id,&pos);
299    if (status!=4 && i!=2) {
300      printf("(read_obs) Error : unable to read obs in %s\n",argc[1]);
301      return;
302    }
303    count++;
304    piobs[it-1][id-1]=pos;
305    pmask[it-1][id-1]=1;
306  }
307  fclose(fid);
308  fprintf(stdout,"(read_obs) %d data were loaded as observations\n",count);
309}
310
311void update_uv(){
312  int j,k,t;
313  for (t=0;t<jtlag+1;t++)
314    for (j=0;j<nlon;j++)
315      for (k=0;k<nlat;k++) {
316        YS1_u(j,k,t)+=YS1_u_d(j,k);
317        YS1_v(j,k,t)+=YS1_v_d(j,k);
318        //printf("u=%f delu=%f \n",YS1_u(j,k),YS1_u_d(j,k));   
319
320}
321}
322
323short is_activ(int jpfl, int it) {
324  /* return 1 if float jpfl is active after it (stricly) */
325
326  //If forward mode, dont test the flaot ater it
327  if (tassim==FORW)
328    return 1;
329 
330  int jptend=it+jtlag+1; 
331  it++;
332 
333  while(it<jptend && pmask[it][jpfl]==0)
334    it++;
335  return(it<jptend && pmask[it][jpfl]==1);
336}
337
338void load_eb(int iti, int itn) {
339  /*Load background from ubck and vbck from time iti to itn*/
340  int j,k,it;
341  for (it=iti;it<itn;it++)
342    for (j=0;j<nlon;j++)
343      for (k=0;k<nlat;k++) {
344        YS1_u(j,k,it-iti)=ubck[j][k][it];
345        YS1_v(j,k,it-iti)=vbck[j][k][it];
346#ifdef K_FILTER
347        YS1_uc_d(j,k)=0;
348        YS1_vc_d(j,k)=0;
349 // Yobs_insert_data (char *nmmod, int sortie, int iaxe, int jaxe, int kaxe,int pdt, YREAL val)
350
351          Yobs_insert_data("uc_d",0,j,k,0,-1,0);
352          Yobs_insert_data("vc_d",0,j,k,0,-1,0);
353#else
354          Yobs_insert_data("u_d",0,j,k,0,-1,0);
355          Yobs_insert_data("v_d",0,j,k,0,-1,0);
356
357#endif
358
359         
360
361       
362      }
363}
364
365void save_uv(int iti, int itn) {
366  /* save u,v fields in umod,vmod
367   and save piret,pjret*/
368  int j,k,it;
369  int Yifloat=0;
370  for (it=iti;it<itn;it++) {
371    Yifloat=0;
372    for (j=0;j<nlon;j++)
373      for (k=0;k<nlat;k++) {
374        umod[j][k][it]=YS1_u(j,k,it-iti);
375        vmod[j][k][it]=YS1_v(j,k,it-iti);
376#ifdef UPDATE_BCK
377        ubck[j][k][it]=YS1_u(j,k,it-iti);
378        vbck[j][k][it]=YS1_v(j,k,it-iti);
379#endif
380      }
381    for (j=0;j<jpnfl;j++) 
382      if (pmask[iti][j]==1 && is_activ(j,iti)>0 && it>iti) {
383        pjret[it][j]=YS1_r_float(Yifloat,it-iti);
384        piret[it][j]=YS2_r_float(Yifloat,it-iti);
385        Yifloat++;
386      }
387  }
388}
389
390void save_output_rfloat (int argc, char *argv[]) {
391  FILE *fid;
392  fid=fopen(argv[1],"w");
393  if (fid==NULL) {
394    printf("\nfailed to open %s",argv[1]);
395    exit(3);
396  }
397  int j,it;
398  for (j=0;j<jpnfl;j++)
399    for (it=0;it<jptfl;it++)
400     
401      fprintf(fid,"%d %d %f %f\n",it,j,piret[it][j],pjret[it][j]);
402       
403                                             
404  fclose(fid);
405
406}
407
408void save_output_uv (int argc, char *argv[]) {
409  FILE *fid;
410  fid=fopen(argv[1],"w");
411  if (fid==NULL) {
412    printf("\nfailed to open %s",argv[1]);
413    exit(3);
414  }
415  int j,k,it;
416    for (it=0;it<jptfl;it++)
417      for (j=0;j<nlon;j++)
418        for (k=0;k<nlat;k++) {
419          fprintf(fid,"%d %d %d %f %f\n",it,j,k,umod[j][k][it],vmod[j][k][it]);
420        }
421                                             
422  fclose(fid);
423
424}
425
426void save_output_uinter (int argc, char *argv[]) {
427  FILE *fid;
428  fid=fopen(argv[1],"w");
429  if (fid==NULL) {
430    printf("\nfailed to open %s",argv[1]);
431    exit(3);
432  }
433  int j,it;
434  for (j=0;j<jpnfl;j++)
435    for (it=0;it<jptfl;it++)
436     
437      fprintf(fid,"%d %d %f %f\n",it,j,piret[it][j],pjret[it][j]);
438       
439                                             
440  fclose(fid);
441
442}
443
444void load_init(int it) {
445  /*Load floater position at time it as init.
446  Only floats that have another position after it
447  Warning : nbre de flotteurs maxiums : nfloat */
448  int jfl;
449  int Yifloat=0;
450int nfobs=0;
451int jptend=it+1+jtlag;
452  for (jfl=0;jfl<jpnfl;jfl++) {
453    nfobs=0;
454    if (pmask[it][jfl]==1 && is_activ(jfl,it)>0) {
455      //Chargemente de l'init
456      YS1_r_float(Yifloat,0)=pjobs[it][jfl];
457      YS2_r_float(Yifloat,0)=piobs[it][jfl];
458      //Chargement des obs
459      for (int itfl=it+1;itfl<jptend;itfl++) {
460        if (pmask[itfl][jfl]==1) {
461          // Yobs_insert_data (char *nmmod, int sortie, int iaxe, int jaxe, int kaxe,int pdt, YREAL val)
462          Yobs_insert_data("r_cout_d",0,Yifloat,0,0,itfl-it,pjobs[itfl][jfl]);
463          Yobs_insert_data("r_cout_d",1,Yifloat,0,0,itfl-it,piobs[itfl][jfl]);
464        nfobs++;
465        } //if pmask==1
466        } //for itfl
467     
468      Yifloat++; 
469      printf("--Float nr %d : %d obs loaded\n",Yifloat,nfobs); 
470    } //if pmask==1
471
472    } //for jfl
473 
474 
475  }
476
477void myforward() {
478  int niti=0; //Temps init
479  int nitn=jptfl-1; //Temps fin
480  char sactiv[20]="activ";
481  char sMD[20]="M";
482  char sonly[20]="only";
483  char *liste[3];
484  liste[0]=sactiv;
485  liste[1]=sMD;
486  liste[2]=sonly;
487 
488  //Mode forward
489  tassim=FORW;
490
491  //Effacement des obs potentielles
492  erase_lobs();
493 
494  // Chargement de toutes les init et obs dans les bon pas de temps
495  load_init(niti);
496 
497  //Réglage de l'ébauche
498   load_eb(niti,nitn);
499 
500   //Forward
501   strcpy(liste[1],"M");
502   Yactraj(3, liste);
503   Yset_modeltime(0);
504   before_it(1);
505
506   Yforward(-1, 0);
507
508   //Save outputs
509   save_uv(niti,nitn);
510
511  //On se remet au mode ASSIM par défaut
512  tassim=ASSIM;
513}
514
515void multirun() {
516  int niti=0; //Temps init
517  int nitn=jtlag;//Temps fin
518    while(nitn<jptfl) {
519     
520      //Effacement de toutes les obs précédentes
521      erase_lobs();
522
523      // Chargement de toutes les init et obs dans les bon pas de temps
524      load_init(niti);
525
526      // Réglage de l'ébauche
527      load_eb(niti,nitn);
528
529      // Run_inc
530      run_inc(0,NULL);
531
532      // Sauvegarde du résultat dans les tableaux
533      save_uv(niti,nitn);
534
535
536      niti=nitn;
537      nitn=niti+jtlag;
538    } //while nitn<jptfl
539}
540
541void erase_udvd() {
542  int j,k;
543  for (j=0;j<nlon;j++)
544        for (k=0;k<nlat;k++) {
545#ifdef K_FILTER
546          YS1_uc_d(j,k)=0;
547          YS1_vc_d(j,k)=0;
548#else     
549          YS1_u_d(j,k)=0;
550          YS1_v_d(j,k)=0;
551#endif
552}
553}
554//void load_aviso(int argc, char *argv[]){
555       
556//      }
557
558void run_inc(int argv, char *argc[]) {
559//Run the incremental optimisation
560//1 forward of the complete model
561//2 initialize du et dv to zero
562//3 minimize the incremental cost function
563//4 update u and v
564
565/*
566Options
567nb_extiter : number of extern loop (default=6)
568inc_save : save option
569        0  : no save
570        1 (default): save after each extern loop r_float,u and v
571dirsave : directory in case inc_save>0 (default="../obs_float/")
572savename : basename of save fils (default "optim")
573*/
574char filesave[100];
575int i;
576
577char sactiv[20]="activ";
578char sMD[20]="M";
579char sonly[20]="only";
580char *liste[3];
581liste[0]=sactiv;
582liste[1]=sMD;
583liste[2]=sonly;
584/*Check M1QN3 config*/
585 if (YNbItRun<=0)
586                {        printf("runm(2): number of run iteration not seted; use set_nbiter please\n");
587                         //return(0);
588                }
589        if (Y3ddf1<=0)
590                {       printf("runm(2): expected positive fcost decrease missed; use setm_ddf1\n");
591                         //return(0);
592                }
593        if (YioInsertObsCtr<0)
594                   printf("runm(2): warning : oh oh, run with no obs !!! \n");
595               
596        YTypeAdjust = ADJUST_M1QN3; //d'office avec M1QN3
597       
598//Savestate config
599 YioModulot = OFF; //On sauvegarde tous les pas de temps
600    YioWrite = ON;
601    YioRead= OFF;
602   // Yio_savestate(cdes[1], cdes[3], 0, cdes[7]);
603    YioState=0 ;//Save all states
604    YioBin=OFF; //ascii output
605    YioAscii=ON; //ascii output
606    YioAxes=ON; //save axe numbers
607printf("Run with %d ext_iter\n",nb_extiter);
608for (i=0;i<nb_extiter;i++) {
609printf("Ext loop %d/%d\n",i+1,nb_extiter);
610     strcpy(liste[1],"M");
611         Yactraj(3, liste);
612         Yset_modeltime(0);
613         before_it(1);
614         //printf("---forward(i=%d)---\n",i);
615         Yforward(-1, 0);
616         
617         if (inc_save==1 && i>0) {
618          //save rfloat
619          YioTime=ON;
620          sprintf(filesave,"%sr_float_%s_%d.dat",dirsave,savename,i);
621          Yio_savestate("r_float","i",0,filesave);
622         }
623             strcpy(liste[1],"D");
624         Yactraj(3,liste);
625         Yset_modeltime(0);
626         
627       
628        Y3run ('0');
629#ifdef K_FILTER
630        //compute values for u_d and v_d
631        strcpy(liste[1],"Tfil");
632        Yactraj(3,liste);
633        Yset_modeltime(0);
634        before_it(1);
635        Yforward(-1, 0);
636        strcpy(liste[1],"T_euler_d");
637        Yactraj(3,liste);
638        Yset_modeltime(0);
639        before_it(1);
640        Yforward(-1, 0);
641#endif
642
643
644    update_uv();
645    erase_udvd();
646
647    if (inc_save==1) {
648   
649   
650    //save u
651    YioTime=ON;
652    sprintf(filesave,"%su%s%d.dat",dirsave,savename,i+1);
653    Yio_savestate("u","ij",0,filesave);
654   
655    //save v
656    sprintf(filesave,"%sv%s%d.dat",dirsave,savename,i+1);
657    Yio_savestate("v","ij",0,filesave);
658   
659    }
660    }
661    /*End of the optimization, we run another time the model to compute the final value of r_float
662    */
663     strcpy(liste[1],"M");
664    Yactraj(3, liste);
665         Yset_modeltime(0);
666         before_it(1);
667         Yforward(-1, 0);
668 if (inc_save==1) {
669          //save rfloat
670          YioTime=ON;
671          sprintf(filesave,"%sr_float_%s_%d.dat",dirsave,savename,nb_extiter);
672          Yio_savestate("r_float","i",0,filesave);
673         }
674}
675
676
Note: See TracBrowser for help on using the repository browser.