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

Last change on this file since 456 was 456, checked in by ymipsl, 10 years ago

Longitude and latitude value are now initialized to 0 in order to avoid to write uninitialized value in the output netcdf file.

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