source: XIOS/trunk/src/node/domain.cpp @ 449

Last change on this file since 449 was 449, checked in by ymipsl, 11 years ago

Add unstructured grid management.

YM

File size: 31.7 KB
Line 
1#include "domain.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6
7#include "xmlioserver_spl.hpp"
8#include "event_client.hpp"
9#include "event_server.hpp"
10#include "buffer_in.hpp"
11#include "message.hpp"
12#include "type.hpp"
13#include "context.hpp"
14#include "context_client.hpp"
15#include "array_new.hpp"
16
17namespace xios {
18   
19   /// ////////////////////// Définitions ////////////////////// ///
20
21   CDomain::CDomain(void)
22      : CObjectTemplate<CDomain>(), CDomainAttributes()
23      , isChecked(false),  relFiles()
24   { /* Ne rien faire de plus */ }
25
26   CDomain::CDomain(const StdString & id)
27      : CObjectTemplate<CDomain>(id), CDomainAttributes()
28      , isChecked(false), relFiles()
29         { /* Ne rien faire de plus */ }
30
31   CDomain::~CDomain(void)
32   { 
33   }
34
35   ///---------------------------------------------------------------
36
37   const std::set<StdString> & CDomain::getRelFiles(void) const
38   {
39      return (this->relFiles); 
40   }
41
42   //----------------------------------------------------------------
43   
44   bool CDomain::hasZoom(void) const
45   {
46      return ((this->zoom_ni.getValue() != this->ni_glo.getValue()) && 
47              (this->zoom_nj.getValue() != this->nj_glo.getValue()));
48   }
49   
50   //----------------------------------------------------------------
51   
52   bool CDomain::isEmpty(void) const
53   {
54      return ((this->zoom_ni_srv == 0) || 
55              (this->zoom_nj_srv == 0));
56   }
57
58   //----------------------------------------------------------------
59
60   bool CDomain::IsWritten(const StdString & filename) const
61   {
62      return (this->relFiles.find(filename) != this->relFiles.end());
63   }
64
65   //----------------------------------------------------------------
66
67   void CDomain::addRelFile(const StdString & filename)
68   {
69      this->relFiles.insert(filename);
70   }
71
72   //----------------------------------------------------------------
73
74   StdString CDomain::GetName(void)   { return (StdString("domain")); }
75   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
76   ENodeType CDomain::GetType(void)   { return (eDomain); }
77
78   //----------------------------------------------------------------
79
80   void CDomain::checkGlobalDomain(void)
81   {
82      if (!type.isEmpty() && type==type_attr::unstructured)
83      {
84         if (ni_glo.isEmpty() || ni_glo <= 0 )
85         {
86            ERROR("CDomain::checkAttributes(void)",
87               << "[ Id = " << this->getId() << " ] "
88               << "The global domain is badly defined,"
89               << " check the \'ni_glo\'  value !") 
90         }
91         nj_glo=ni_glo ;
92         ni_glo=1 ;
93         if (!ni.isEmpty()) nj=ni ;
94         if (!ibegin.isEmpty()) jbegin=ibegin ;
95         if (!iend.isEmpty()) jend=iend ;
96         ni=1 ;
97         ibegin=1 ;
98         iend=1 ;
99      }
100      else if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
101          (nj_glo.isEmpty() || nj_glo.getValue() <= 0 ))
102      {
103         ERROR("CDomain::checkAttributes(void)",
104               << "[ Id = " << this->getId() << " ] "
105               << "The global domain is badly defined,"
106               << " check the \'ni_glo\' et \'nj_glo\' values !") 
107      }
108   }
109
110   //----------------------------------------------------------------
111
112   void CDomain::checkLocalIDomain(void)
113   {
114      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
115         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
116
117      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
118         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
119
120      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
121         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
122
123      else if (!ibegin.isEmpty() && !iend.isEmpty() && !ni.isEmpty() )
124      {
125         if (iend.getValue() != ibegin.getValue() + ni.getValue() - 1)
126           ERROR("CDomain::checkAttributes(void)",
127                 << "The domain si wrong defined,"
128                 << " iend is different of (ibegin + ni - 1) !") ;
129      }
130      else
131      {
132         ERROR("CDomain::checkAttributes(void)",
133               << "The domain is wrong defined,"
134               << " 2 value from \'iend\', \'ibegin\', \'ni\'  must be defined !") ;
135      }
136
137
138      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
139          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
140          {
141                  cout<<ni_glo<<"  "<<ni<<"  "<<ibegin<<"  "<<iend<<endl;
142
143         ERROR("CDomain::checkAttributes(void)",
144               << "[ Id = " << this->getId() << " ] "
145               << "Local domain is wrong defined,"
146               << " Check the value : ni, ni_glo, ibegin, iend") ;
147        }
148   }
149
150   //----------------------------------------------------------------
151
152   void CDomain::checkLocalJDomain(void)
153   {
154      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
155         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
156
157      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
158         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
159
160      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
161         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
162
163      else if (!jbegin.isEmpty() && !jend.isEmpty() && !nj.isEmpty() )
164      {
165          if  (jend.getValue() != jbegin.getValue() + nj.getValue() - 1)
166             ERROR("CDomain::checkAttributes(void)",
167                 << "The domain is wrong defined,"
168                 << " iend is different of (jbegin + nj - 1) !") ;
169      }
170      else
171      {
172         ERROR("CDomain::checkAttributes(void)",
173               << "The domain is wrong defined,"
174               << " 2 values from  jend, jbegin, nj  must be defined !") ;
175      }
176
177      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
178          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
179         ERROR("CDomain::checkAttributes(void)",
180               << "Domain is wrong defined,"
181               << " Check the values : nj, nj_glo, jbegin, jend") ;
182               
183     ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
184     jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
185   }
186
187   //----------------------------------------------------------------
188
189   void CDomain::checkMask(void)
190   {
191      using namespace std;
192     
193      int ibegin_mask = 0,
194          jbegin_mask = 0,
195          iend_mask = iend.getValue() - ibegin.getValue(),
196          jend_mask = jend.getValue() - jbegin.getValue();
197     
198      if (!zoom_ibegin.isEmpty())
199      {
200         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
201         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
202         
203         ibegin_mask = max (ibegin.getValue(), zoom_ibegin.getValue());
204         jbegin_mask = max (jbegin.getValue(), zoom_jbegin.getValue());
205         iend_mask   = min (iend.getValue(), zoom_iend);
206         jend_mask   = min (jend.getValue(), zoom_jend);
207                 
208         ibegin_mask -= ibegin.getValue();
209         jbegin_mask -= jbegin.getValue();
210         iend_mask   -= ibegin.getValue();
211         jend_mask   -= jbegin.getValue();
212      }
213     
214
215      if (!mask.isEmpty())
216      {
217         unsigned int niu = ni.getValue(), nju = nj.getValue();
218         if ((mask.extent(0) != niu) ||
219             (mask.extent(1) != nju))
220            ERROR("CDomain::checkAttributes(void)",
221                  <<"the mask has not the same size than the local domain"<<endl
222                   <<"Local size is "<<niu<<"x"<<nju<<endl
223                  <<"Mask size is "<<mask.extent(0)<<"x"<<mask.extent(1));                 
224         for (int i = 0; i < ni.getValue(); i++)
225         {
226            for (int j = 0; j < nj.getValue(); j++)
227            {
228               if (i < ibegin_mask && i > iend_mask &&
229                   j < jbegin_mask && j > jend_mask )
230                     mask(i,j) = false;
231            }
232         }
233      }
234      else // (!mask.hasValue())
235      { // Si aucun masque n'est défini,
236        // on en crée un nouveau qui valide l'intégralité du domaine.
237         mask.resize(ni,nj) ;
238         for (int i = 0; i < ni.getValue(); i++)
239         {
240            for (int j = 0; j < nj.getValue(); j++)
241            {
242               if (i >= ibegin_mask && i <= iend_mask &&
243                   j >= jbegin_mask && j <= jend_mask )
244                     mask(i,j) = true;
245               else  mask(i,j) = false;
246            }
247         }
248      }
249   }
250
251
252   //----------------------------------------------------------------
253
254   void CDomain::checkDomainData(void)
255   {     
256      if (!data_dim.isEmpty() &&
257         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
258      {
259         ERROR("CDomain::checkAttributes(void)",
260               << "Data dimension incompatible (must be 1 or 2) !") ;
261      }
262      else if (data_dim.isEmpty())
263      {
264         ERROR("CDomain::checkAttributes(void)",
265               << "Data dimension undefined !") ;
266      }
267
268      if (data_ibegin.isEmpty())
269         data_ibegin.setValue(0) ;
270      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
271         data_jbegin.setValue(0) ;
272
273      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
274      {
275         ERROR("CDomain::checkAttributes(void)",
276               << "Data dimension is negative (data_ni).") ;
277      }
278      else if (data_ni.isEmpty())
279      {
280         data_ni.setValue((data_dim.getValue() == 1)
281                           ? (ni.getValue() * nj.getValue())
282                           : ni.getValue());
283      }
284
285      if (data_dim.getValue() == 2)
286      {
287         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
288         {
289            ERROR("CDomain::checkAttributes(void)",
290                  << "Data dimension is negative (data_nj).") ;
291         }
292         else if (data_nj.isEmpty())
293            data_nj.setValue(nj.getValue()) ;
294      }
295
296   }
297
298   //----------------------------------------------------------------
299
300   void CDomain::checkCompression(void)
301   {
302      if (!data_i_index.isEmpty())
303      {
304         int ssize = data_i_index.numElements();
305         if (!data_n_index.isEmpty() &&
306            (data_n_index.getValue() != ssize))
307         {
308            ERROR("CDomain::checkAttributes(void)",
309                  <<"Dimension data_i_index incompatible with data_n_index.") ;
310         }
311         else if (data_n_index.isEmpty())
312            data_n_index.setValue(ssize) ;
313
314         if (data_dim.getValue() == 2)
315         {
316            if (!data_j_index.isEmpty() &&
317               (data_j_index.numElements() != data_i_index.numElements()))
318            {
319               ERROR("CDomain::checkAttributes(void)",
320                     <<"Dimension data_j_index incompatible with data_i_index.") ;
321            }
322            else if (data_j_index.isEmpty())
323            {
324               ERROR("CDomain::checkAttributes(void)",
325                     <<"data_j_index must be defined !") ;
326            }
327         }
328      }
329      else
330      {
331         if (!data_n_index.isEmpty() ||
332            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
333            ERROR("CDomain::checkAttributes(void)", << "data_i_index undefined") ;
334      }
335
336      if (data_n_index.isEmpty())
337      { // -> bloc re-vérifié OK
338         if (data_dim.getValue() == 1)
339         {
340            const int dni = data_ni.getValue();
341            data_i_index.resize(dni) ;
342            data_n_index.setValue(dni);
343            for (int i = 0; i < dni; i++) data_i_index(i) = i+1 ;
344         }
345         else   // (data_dim == 2)
346         {
347            const int dni = data_ni.getValue() * data_nj.getValue();
348            data_i_index.resize(dni) ;
349            data_j_index.resize(dni) ;
350           
351            data_n_index.setValue(dni);
352           
353            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
354            {
355               for(int i = 0; i < data_ni.getValue(); i++, count++)
356               {
357                  data_i_index(count) = i+1 ;
358                  data_j_index(count) = j+1 ;
359               }
360            }
361         }
362      }
363   }
364
365   //----------------------------------------------------------------
366   
367   void CDomain::completeLonLatClient(void)
368   {
369      int i,j,k ;
370      CArray<double,1> lonvalue_temp(ni*nj) ;
371      CArray<double,1> latvalue_temp(ni*nj) ;
372      CArray<double,2> bounds_lon_temp(nvertex,ni*nj) ;
373      CArray<double,2> bounds_lat_temp(nvertex,ni*nj) ;
374     
375      if (type.isEmpty())
376      {
377        if ( lonvalue.numElements() == ni*nj && latvalue.numElements() == ni*nj ) 
378        {
379          type.setValue(type_attr::curvilinear) ;
380          isCurvilinear=true ;
381        }
382        else if ( lonvalue.numElements() == ni && latvalue.numElements() == nj ) 
383        {
384          type.setValue(type_attr::regular) ;
385          isCurvilinear=false ;
386        }
387        else ERROR("void CDomain::completeLonLatClient(void)",<<"the grid is nor curvilinear, nor cartesian, because the size of longitude and latitude array is not coherent with the domain size"<<endl
388                                                              <<"lonvalue size = " << lonvalue.numElements() << "different of ni or ni*nj"<<endl
389                                                              <<"latvalue size = " << latvalue.numElements() << "different of nj or ni*nj" ) ;
390      }
391      if (type==type_attr::curvilinear || type==type_attr::unstructured)
392      {
393        lonvalue_temp=lonvalue ;
394        latvalue_temp=latvalue ;
395        if (hasBounds) bounds_lon_temp=bounds_lon ;
396        if (hasBounds) bounds_lat_temp=bounds_lat ;
397      }
398      else
399      {
400        for(j=0;j<nj;j++)
401          for(i=0;i<ni;i++) 
402          {
403            k=j*ni+i ;
404            lonvalue_temp(k)=lonvalue(i) ;
405            latvalue_temp(k)=latvalue(j) ;
406            if (hasBounds)
407            {
408              for(int n=0;n<nvertex;n++) 
409              {
410                bounds_lon_temp(n,k)=bounds_lon(n,i) ;
411                bounds_lat_temp(n,k)=bounds_lat(n,j) ;
412              }
413            }
414          }
415      }
416         
417      StdSize dm = zoom_ni_client * zoom_nj_client;
418
419      lonvalue.resize(dm);
420      latvalue.resize(dm);
421         
422      for (int i = 0; i < zoom_ni_client; i++)
423      {
424        for (int j = 0; j < zoom_nj_client; j++)
425        {
426//          lonvalue(i + j * zoom_ni_client) = lonvalue_temp( (i + zoom_ibegin_client -1) + (j + zoom_jbegin_client -1)*ni );
427//          latvalue(i + j * zoom_ni_client) = latvalue_temp( (i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni );
428          lonvalue(i + j * zoom_ni_client) = lonvalue_temp( (i + zoom_ibegin_client-ibegin) + (j + zoom_jbegin_client -jbegin)*ni ); 
429          latvalue(i + j * zoom_ni_client) = latvalue_temp( (i + zoom_ibegin_client - ibegin)+(j + zoom_jbegin_client - jbegin)*ni );
430          if (hasBounds)
431          {
432            for(int n=0;n<nvertex;n++) 
433            {
434//              bounds_lon(n,i + j * zoom_ni_client) = bounds_lon_temp( n, (i + zoom_ibegin_client -1) + (j + zoom_jbegin_client -1)*ni );
435//              bounds_lat(n,i + j * zoom_ni_client) = bounds_lat_temp( n, (i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni );
436              bounds_lon(n,i + j * zoom_ni_client) = bounds_lon_temp( n, (i + zoom_ibegin_client - ibegin) + (j + zoom_jbegin_client -jbegin)*ni ); 
437              bounds_lat(n,i + j * zoom_ni_client) = bounds_lat_temp( n, (i + zoom_ibegin_client - ibegin)+(j + zoom_jbegin_client -jbegin)*ni );
438            }
439          }
440        }
441      }
442    }
443 
444
445   //----------------------------------------------------------------
446
447   void CDomain::checkZoom(void)
448   {
449      // Résolution et vérification des données globales de zoom.
450      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
451          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
452      {
453         if (this->zoom_ni.isEmpty()     || this->zoom_nj.isEmpty() ||
454             this->zoom_ibegin.isEmpty() || this->zoom_jbegin.isEmpty())
455         {
456            ERROR("CDomain::checkZoom(void)",
457                  <<"if one of zoom attributes is defined then all zoom attributes must be defined") ;
458         }
459         else
460         {
461            int zoom_iend = zoom_ibegin + zoom_ni - 1;
462            int zoom_jend = zoom_jbegin + zoom_nj - 1;
463               
464            if (zoom_ibegin < 1  || zoom_jbegin < 1 || zoom_iend > ni_glo || zoom_jend > nj_glo)
465               ERROR("CDomain::checkZoom(void)",
466                     << "Zoom is wrong defined,"
467                     << " Check the values : zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
468         }
469      }
470      else
471      {
472         zoom_ni = ni_glo; 
473         zoom_nj = nj_glo;
474         zoom_ibegin = 1;
475         zoom_jbegin = 1;
476      }
477     
478      // compute client zoom indices
479
480      int zoom_iend=zoom_ibegin+zoom_ni-1 ;
481      zoom_ibegin_client = ibegin_client > zoom_ibegin ? ibegin_client : zoom_ibegin ;
482      zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
483      zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
484      if (zoom_ni_client<0) zoom_ni_client=0 ;
485
486   
487      int zoom_jend=zoom_jbegin+zoom_nj-1 ;
488      zoom_jbegin_client = jbegin_client > zoom_jbegin ? jbegin_client : zoom_jbegin ;
489      zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
490      zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
491      if (zoom_nj_client<0) zoom_nj_client=0 ;
492     
493      cout << zoom_ibegin<<"  "<<zoom_ni <<"  "<< zoom_jbegin<<"  "<<zoom_nj<<endl ;
494      cout << zoom_ibegin_client<<"  "<<zoom_ni_client <<"  "<< zoom_jbegin_client<<"  "<<zoom_nj_client<<endl ;
495      cout << ibegin_client<<"  "<<iend_client <<"  "<< jbegin_client<<"  "<<jend_client<<endl ;
496/*   
497      {
498         int zoom_iend = zoom_ibegin + zoom_ni - 1;
499         int zoom_jend = zoom_jbegin + zoom_nj - 1;
500         
501         if ( zoom_ibegin > iend || zoom_iend < ibegin )
502         {
503            zoom_ni_loc = 0 ;
504            zoom_ibegin_loc = zoom_ibegin ;
505         }
506         else
507         {
508            int zoom_ibegin_loc_ = (zoom_ibegin > ibegin) ? zoom_ibegin : ibegin ;
509            int zoom_iend_loc_  = (zoom_iend < iend) ? zoom_iend : iend ;
510            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
511           
512            zoom_ni_loc = zoom_ni_loc_;
513            zoom_ibegin_loc = zoom_ibegin_loc_-ibegin.getValue()+1;
514         }
515         
516         if ( zoom_jbegin > jend || zoom_jend < jbegin )
517         {
518            zoom_nj_loc = 0 ;
519            zoom_jbegin_loc = zoom_jbegin ;
520         }
521         else
522         {
523            int zoom_jbegin_loc_ = ( zoom_jbegin > jbegin ) ? zoom_jbegin : jbegin ;
524            int zoom_jend_loc_  = (zoom_jend < jend) ? zoom_jend : jend ;
525            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
526           
527            zoom_nj_loc = zoom_nj_loc_;
528            zoom_jbegin_loc = zoom_jbegin_loc_-jbegin.getValue()+1;
529         }
530      }
531*/
532   }
533   
534   void CDomain::checkBounds(void)
535   {
536     if (!nvertex.isEmpty() && !bounds_lon.isEmpty() && !bounds_lat.isEmpty())
537     {
538       hasBounds=true ;
539       
540     }
541     else 
542     {
543       hasBounds=false;
544       nvertex=0 ;
545     }
546   }
547           
548   //----------------------------------------------------------------
549
550   void CDomain::checkAttributes(void)
551   {
552      if (this->isChecked) return;
553      CContext* context=CContext::getCurrent() ;
554
555      this->checkGlobalDomain();
556      this->checkLocalIDomain();
557      this->checkLocalJDomain();
558     
559      this->checkZoom();
560      this->checkBounds();
561     
562      if (context->hasClient)
563      { // CÃŽté client uniquement
564         this->checkMask();
565         this->checkDomainData();
566         this->checkCompression();
567         this->completeLonLatClient();
568      }
569      else
570      { // CÃŽté serveur uniquement
571//         if (!this->isEmpty())
572// ne sert plus //   this->completeLonLatServer();
573      }
574   
575      if (context->hasClient)
576      {
577        computeConnectedServer() ;
578        sendServerAttribut() ;
579        sendLonLat() ;
580      }
581     
582      this->isChecked = true;
583   }
584   
585  void CDomain::sendServerAttribut(void)
586  {
587    int ni_srv=ni_glo.getValue() ;
588    int ibegin_srv=1 ;
589    int iend_srv=ni_glo.getValue() ;
590     
591    int nj_srv ;
592    int jbegin_srv ;
593    int jend_srv ;
594   
595    CContext* context=CContext::getCurrent() ;
596    CContextClient* client=context->client ;
597    int nbServer=client->serverSize ;
598    int serverRank=client->getServerLeader() ;
599   
600    jend_srv=0 ;
601    for(int i=0;i<=serverRank;i++)
602    {
603      jbegin_srv=jend_srv+1 ;
604      nj_srv=nj_glo.getValue()/nbServer ;
605      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
606      jend_srv=jbegin_srv+nj_srv-1 ;
607    }
608   
609     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
610     if (client->isServerLeader())
611     {
612       CMessage msg ;
613       msg<<this->getId() ;
614       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
615       event.push(client->getServerLeader(),1,msg) ;
616       client->sendEvent(event) ;
617     }
618     else client->sendEvent(event) ;
619  }
620 
621  void CDomain::computeConnectedServer(void)
622  {
623    int ib,ie,in;
624    int jb,je,jn ;
625   
626    int ni_srv=ni_glo.getValue() ;
627    int ibegin_srv=1 ;
628    int iend_srv=ni_glo.getValue() ;
629     
630    int nj_serv,jbegin_srv, jend_srv ;
631    int zoom_ibegin_srv,zoom_iend_srv,zoom_ni_srv ;
632    int zoom_jbegin_srv,zoom_jend_srv,zoom_nj_srv ;
633   
634    ibegin_client=ibegin.getValue() ; iend_client=iend.getValue() ; ni_client=ni.getValue() ;
635    jbegin_client=jbegin.getValue() ; jend_client=jend.getValue() ; nj_client=nj.getValue() ;
636     
637    CContext* context = CContext::getCurrent() ;
638    CContextClient* client=context->client ;
639    int nbServer=client->serverSize ;
640
641/*   
642    // compute client zoom indices
643    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
644    zoom_ibegin_client = ibegin_client > zoom_ibegin.getValue() ? ibegin_client : zoom_ibegin.getValue() ;
645    zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
646    zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
647    if (zoom_ni_client<0) zoom_ni_client=0 ;
648
649    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
650    zoom_jbegin_client = jbegin_client > zoom_jbegin.getValue() ? jbegin_client : zoom_jbegin.getValue() ;
651    zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
652    zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
653    if (zoom_nj_client<0) zoom_nj_client=0 ;
654*/ 
655    // find how much client are connected to a server
656    int zoom_iend=zoom_ibegin+zoom_ni-1 ;
657    int zoom_jend=zoom_jbegin+zoom_nj-1 ;
658   
659    jend_srv=0 ;
660    for(int ns=0;ns<nbServer;ns++)
661    {
662      jbegin_srv=jend_srv+1 ;
663      nj_srv=nj_glo.getValue()/nbServer ;
664      if (ns<nj_glo.getValue()%nbServer) nj_srv++ ;
665      jend_srv=jbegin_srv+nj_srv-1 ;
666     
667      ib = ibegin_client>ibegin_srv ? ibegin_client : ibegin_srv ;
668      ie=  iend_client< iend_srv? iend_client : iend_srv ;
669      in=ie-ib+1 ; 
670      if (in<0) in=0 ;
671     
672      jb= jbegin_client>jbegin_srv ? jbegin_client : jbegin_srv ;
673      je= jend_client<jend_srv ? jend_client : jend_srv ;
674      jn=je-jb+1 ;
675      if (jn<0) jn=0 ;
676       
677      if (in>0 && jn>0)
678      {
679        zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
680        zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
681        zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
682        if (zoom_ni_srv<0) zoom_ni_srv=0 ;
683     
684        zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
685        zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
686        zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
687        if (zoom_nj_srv<0) zoom_nj_srv=0 ;
688 
689        if (zoom_ni_srv>0 && zoom_nj_srv>0 && zoom_ni_client>0 && zoom_nj_client>0)
690        {
691          ib = zoom_ibegin_client>zoom_ibegin_srv ? zoom_ibegin_client : zoom_ibegin_srv ;
692          ie=zoom_iend_client<zoom_iend_srv?zoom_iend_client:zoom_iend_srv ;
693          in=ie-ib+1 ;
694          if (in<0) in=0 ;
695       
696          jb=zoom_jbegin_client>zoom_jbegin_srv?zoom_jbegin_client:zoom_jbegin_srv ;
697          je=zoom_jend_client<zoom_jend_srv?zoom_jend_client:zoom_jend_srv ;
698          jn=je-jb+1 ;
699          if (jn<0) jn=0 ;
700        }
701        else
702        {
703          ib=1 ; ie=0 ; in=0 ;
704          jb=1 ; je=0 ; jn=0 ;
705        }
706       
707//          if (in>0 && jn>0)
708//          {
709            connectedServer.push_back(ns) ;
710            ib_srv.push_back(ib) ;
711            ie_srv.push_back(ie) ;
712            in_srv.push_back(in) ;
713            jb_srv.push_back(jb) ;
714            je_srv.push_back(je) ;
715            jn_srv.push_back(jn) ;
716//           }
717       
718      }
719    }
720    int nbConnectedServer=connectedServer.size() ;
721    int* recvCount=new int[client->clientSize] ;
722    int* displ=new int[client->clientSize] ;
723    int* sendBuff=new int[nbConnectedServer] ;
724    valarray<int> nbClient(0,client->serverSize) ;
725   
726    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
727   
728    // get connected server for everybody
729    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
730   
731    displ[0]=0 ;
732    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
733    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
734    int* recvBuff=new int[recvSize] ;
735 
736   
737    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
738    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
739   
740    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
741   
742    delete [] recvCount ;
743    delete [] displ ;
744    delete [] sendBuff ;
745    delete [] recvBuff ;
746  }
747 
748  void CDomain::sendLonLat(void)
749  {
750    CContext* context = CContext::getCurrent() ;
751    CContextClient* client=context->client ;
752    // send lon lat for each connected server
753    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
754   
755    int ib,ie,in ;
756    int jb,je,jn ;
757 
758    list<shared_ptr<CMessage> > list_msg ;   
759    list< CArray<int,1>* > list_indi,list_indj ;
760    list< CArray<double,1>* >list_lon,list_lat ;
761    list< CArray<double,2>* >list_boundslon,list_boundslat ;
762
763    for(int ns=0;ns<connectedServer.size();ns++)
764    {
765      ib=ib_srv[ns] ; ie=ie_srv[ns] ; in=in_srv[ns] ;
766      jb=jb_srv[ns] ; je=je_srv[ns] ; jn=jn_srv[ns] ;
767     
768      CArray<int,1> indi(in*jn) ;
769      CArray<int,1> indj(in*jn) ;
770      CArray<double,1> lon(in*jn) ;
771      CArray<double,1> lat(in*jn) ;
772      CArray<double,2> boundslon(nvertex,in*jn) ;
773      CArray<double,2> boundslat(nvertex,in*jn) ;
774
775         
776      int ind_client,ind_loc ;
777     
778      for(int j=jb;j<=je;j++)
779        for(int i=ib;i<=ie;i++)
780        {
781          ind_client=(i-zoom_ibegin_client)+(j-zoom_jbegin_client)*zoom_ni_client ;
782          ind_loc=(i-ib)+(j-jb)*in ;
783          lon(ind_loc)=lonvalue(ind_client) ;
784          lat(ind_loc)=latvalue(ind_client) ;
785          if (hasBounds)
786          {
787            for(int n=0;n<nvertex;n++)
788            {
789              boundslon(n,ind_loc)=bounds_lon(n,ind_client);
790              boundslat(n,ind_loc)=bounds_lat(n,ind_client);
791            }
792          }
793          indi(ind_loc)=i ;
794          indj(ind_loc)=j ;
795        }
796     
797      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
798      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
799      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
800      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
801      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
802      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
803
804      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
805
806      *list_msg.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
807      *list_msg.back()<<isCurvilinear ;
808      *list_msg.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back()<<*list_lat.back() ;
809      if (hasBounds) *list_msg.back()<<*list_boundslon.back()<<*list_boundslat.back();
810      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
811    }
812
813    client->sendEvent(event) ;
814   
815   
816    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
817    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
818    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
819    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
820    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
821    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;
822   
823  }
824 
825  bool CDomain::dispatchEvent(CEventServer& event)
826   {
827     
828      if (SuperClass::dispatchEvent(event)) return true ;
829      else
830      {
831        switch(event.type)
832        {
833           case EVENT_ID_SERVER_ATTRIBUT :
834             recvServerAttribut(event) ;
835             return true ;
836             break ;
837           case EVENT_ID_LON_LAT :
838             recvLonLat(event) ;
839             return true ;
840             break ;
841           default :
842             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
843                    <<"Unknown Event") ;
844           return false ;
845         }
846      }
847   }
848   
849  void CDomain::recvServerAttribut(CEventServer& event)
850  {
851    CBufferIn* buffer=event.subEvents.begin()->buffer;
852    string domainId ;
853    *buffer>>domainId ;
854    get(domainId)->recvServerAttribut(*buffer) ;
855  }
856 
857  void CDomain::recvServerAttribut(CBufferIn& buffer)
858  {
859    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
860    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
861
862     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
863   
864    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
865    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
866    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
867     
868    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
869    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
870    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
871
872    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
873    {
874      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
875      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
876    }
877   
878    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
879    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
880    if (hasBounds) bounds_lon_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
881    if (hasBounds) bounds_lat_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
882  }
883   
884  void CDomain::recvLonLat(CEventServer& event)
885  {
886    list<CEventServer::SSubEvent>::iterator it ;
887    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
888    {
889      CBufferIn* buffer=it->buffer;
890      string domainId ;
891      *buffer>>domainId ;
892      get(domainId)->recvLonLat(*buffer) ;
893    }
894  }
895 
896  void CDomain::recvLonLat(CBufferIn& buffer)
897  {
898    CArray<int,1> indi ;
899    CArray<int,1> indj ;
900    CArray<double,1> lon ;
901    CArray<double,1> lat ;
902    CArray<double,2> boundslon ;
903    CArray<double,2> boundslat ;
904
905    int type_int ;
906    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon>>lat ;
907    if (hasBounds) buffer>>boundslon>>boundslat ;
908    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
909
910    int i,j,ind_srv ;
911
912    for(int ind=0;ind<indi.numElements();ind++)
913    {
914      i=indi(ind) ; j=indj(ind) ;
915      ind_srv=(i-zoom_ibegin_srv)+(j-zoom_jbegin_srv)*zoom_ni_srv ;
916      lonvalue_srv(ind_srv)=lon(ind) ;
917      latvalue_srv(ind_srv)=lat(ind) ;
918      if (hasBounds) 
919      {
920        for(int n=0;n<nvertex;n++) 
921        {
922          bounds_lon_srv(n,ind_srv)=boundslon(n,ind) ;
923          bounds_lat_srv(n,ind_srv)=boundslat(n,ind) ;
924        }
925      }
926    }
927  }
928   //----------------------------------------------------------------
929   
930   
931   
932   ///---------------------------------------------------------------
933
934} // namespace xios
Note: See TracBrowser for help on using the repository browser.