source: altifloat/src/floater.h @ 113

Last change on this file since 113 was 113, checked in by jbrlod, 10 years ago

small corrections

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