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

Last change on this file since 369 was 369, checked in by ymipsl, 12 years ago

Major Update

  • redesign Type and attribute manipulation
  • add enumerate type and attribute
  • use blitz class array instead of boost class array

YM

File size: 35.6 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      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
25      , ibegin_zoom_sub(), jbegin_zoom_sub(), ni_zoom_sub(), nj_zoom_sub()
26      , lonvalue_sub(), latvalue_sub()
27   { /* Ne rien faire de plus */ }
28
29   CDomain::CDomain(const StdString & id)
30      : CObjectTemplate<CDomain>(id), CDomainAttributes()
31      , isChecked(false), relFiles()
32      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
33      , ibegin_zoom_sub(), jbegin_zoom_sub(),ni_zoom_sub(), nj_zoom_sub()
34      , lonvalue_sub(), latvalue_sub()
35   { /* Ne rien faire de plus */ }
36
37   CDomain::~CDomain(void)
38   { 
39     vector<CArray<double,1>* >::iterator it;
40     for(it=lonvalue_sub.begin();it<lonvalue_sub.end();it++) delete *it;
41     for(it=latvalue_sub.begin();it<latvalue_sub.end();it++) delete *it;
42   }
43
44   ///---------------------------------------------------------------
45
46   const std::set<StdString> & CDomain::getRelFiles(void) const
47   {
48      return (this->relFiles);
49   }
50
51   //----------------------------------------------------------------
52   
53   bool CDomain::hasZoom(void) const
54   {
55      return ((this->zoom_ni.getValue() != this->ni_glo.getValue()) && 
56              (this->zoom_nj.getValue() != this->nj_glo.getValue()));
57   }
58   
59   //----------------------------------------------------------------
60   
61   bool CDomain::isEmpty(void) const
62   {
63      return ((this->zoom_ni_srv == 0) || 
64              (this->zoom_nj_srv == 0));
65   }
66
67   //----------------------------------------------------------------
68
69   bool CDomain::IsWritten(const StdString & filename) const
70   {
71      return (this->relFiles.find(filename) != this->relFiles.end());
72   }
73
74   //----------------------------------------------------------------
75
76   void CDomain::addRelFile(const StdString & filename)
77   {
78      this->relFiles.insert(filename);
79   }
80
81   //----------------------------------------------------------------
82/*
83   void CDomain::fromBinary(StdIStream & is)
84   {
85      SuperClass::fromBinary(is);
86     
87      if ( !this->ibegin.isEmpty()   &&
88           !this->jbegin.isEmpty()   &&
89           !this->iend.isEmpty()     &&
90           !this->jend.isEmpty()     &&
91           !this->latvalue.isEmpty() &&
92           !this->lonvalue.isEmpty())
93      {
94     
95         this->ibegin_sub.push_back(this->ibegin.getValue());
96         this->jbegin_sub.push_back(this->jbegin.getValue());
97         this->iend_sub.push_back(this->iend.getValue());
98         this->jend_sub.push_back(this->jend.getValue());
99         
100         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
101         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
102         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
103         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
104     
105         this->latvalue_sub.push_back(this->latvalue.getValue());
106         this->lonvalue_sub.push_back(this->lonvalue.getValue());
107      }
108     
109#define CLEAR_ATT(name_)\
110      SuperClassAttribute::operator[](#name_)->reset()
111
112         CLEAR_ATT(mask);
113         CLEAR_ATT(data_n_index);
114         CLEAR_ATT(data_i_index);
115         CLEAR_ATT(data_j_index);
116         
117         CLEAR_ATT(data_ni);
118         CLEAR_ATT(data_nj);
119         CLEAR_ATT(data_ibegin);
120         CLEAR_ATT(data_jbegin);
121         
122         CLEAR_ATT(ni);
123         CLEAR_ATT(nj);
124         
125#undef CLEAR_ATT
126
127      if ( !this->ibegin.isEmpty()   &&
128           !this->jbegin.isEmpty()   &&
129           !this->iend.isEmpty()     &&
130           !this->jend.isEmpty()     &&
131           !this->latvalue.isEmpty() &&
132           !this->lonvalue.isEmpty())
133      {
134
135         this->ibegin.setValue(*std::min_element(this->ibegin_sub.begin(),this->ibegin_sub.end()));
136         this->jbegin.setValue(*std::min_element(this->jbegin_sub.begin(),this->jbegin_sub.end()));
137         this->iend.setValue(*std::max_element(this->iend_sub.begin(),this->iend_sub.end()));
138         this->jend.setValue(*std::max_element(this->jend_sub.begin(),this->jend_sub.end()));
139      }
140   }
141*/
142   //----------------------------------------------------------------
143
144   StdString CDomain::GetName(void)   { return (StdString("domain")); }
145   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
146   ENodeType CDomain::GetType(void)   { return (eDomain); }
147
148   //----------------------------------------------------------------
149
150   void CDomain::checkGlobalDomain(void)
151   {
152      if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
153          (nj_glo.isEmpty() || nj_glo.getValue() <= 0 ))
154      {
155         ERROR("CDomain::checkAttributes(void)",
156               << "[ Id = " << this->getId() << " ] "
157               << "Le domaine global est mal défini,"
158               << " vérifiez les valeurs de \'ni_glo\' et \'nj_glo\' !") 
159      }
160   }
161
162   //----------------------------------------------------------------
163
164   void CDomain::checkLocalIDomain(void)
165   {
166      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
167         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
168
169      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
170         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
171
172      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
173         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
174
175      else if (!ibegin.isEmpty() && !iend.isEmpty() &&
176               !ni.isEmpty() && (iend.getValue() != ibegin.getValue() + ni.getValue() - 1))
177      {
178         ERROR("CDomain::checkAttributes(void)",
179               << "Le domaine est mal défini,"
180               << " iend est différent de (ibegin + ni - 1) !") ;
181      }
182      else
183      {
184         ERROR("CDomain::checkAttributes(void)",
185               << "Le domaine est mal défini,"
186               << " deux valeurs au moins parmis iend, ibegin, ni doivent être définies !") ;
187      }
188
189
190      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
191          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
192         ERROR("CDomain::checkAttributes(void)",
193               << "[ Id = " << this->getId() << " ] "
194               << "Domaine local mal défini,"
195               << " vérifiez les valeurs ni, ni_glo, ibegin, iend") ;
196
197   }
198
199   //----------------------------------------------------------------
200
201   void CDomain::checkLocalJDomain(void)
202   {
203      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
204         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
205
206      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
207         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
208
209      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
210         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
211
212      else if (!jbegin.isEmpty() && !jend.isEmpty() && !nj.isEmpty() &&
213               (jend.getValue() != jbegin.getValue() + nj.getValue() - 1))
214      {
215         ERROR("CDomain::checkAttributes(void)",
216               << "Le domaine est mal défini,"
217               << " iend est différent de (jbegin + nj - 1) !") ;
218      }
219      else
220      {
221         ERROR("CDomain::checkAttributes(void)",
222               << "Le domaine est mal défini,"
223               << " deux valeurs au moins parmis jend, jbegin, nj doivent être définies !") ;
224      }
225
226      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
227          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
228         ERROR("CDomain::checkAttributes(void)",
229               << "Domaine local mal défini,"
230               << " vérifiez les valeurs nj, nj_glo, jbegin, jend") ;
231   }
232
233   //----------------------------------------------------------------
234
235   void CDomain::checkMask(void)
236   {
237      using namespace std;
238     
239      int ibegin_mask = 0,
240          jbegin_mask = 0,
241          iend_mask = iend.getValue() - ibegin.getValue(),
242          jend_mask = jend.getValue() - jbegin.getValue();
243     
244      if (!zoom_ibegin.isEmpty())
245      {
246         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
247         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
248         
249         ibegin_mask = max (ibegin.getValue(), zoom_ibegin.getValue());
250         jbegin_mask = max (jbegin.getValue(), zoom_jbegin.getValue());
251         iend_mask   = min (iend.getValue(), zoom_iend);
252         jend_mask   = min (jend.getValue(), zoom_jend);
253                 
254         ibegin_mask -= ibegin.getValue();
255         jbegin_mask -= jbegin.getValue();
256         iend_mask   -= ibegin.getValue();
257         jend_mask   -= jbegin.getValue();
258      }
259     
260
261      if (!mask.isEmpty())
262      {
263         unsigned int niu = ni.getValue(), nju = nj.getValue();
264         if ((mask.extent(0) != niu) ||
265             (mask.extent(1) != nju))
266            ERROR("CDomain::checkAttributes(void)",
267                  <<"Le masque n'a pas la même taille que le domaine local") ;
268                 
269         for (int i = 0; i < ni.getValue(); i++)
270         {
271            for (int j = 0; j < nj.getValue(); j++)
272            {
273               if (i < ibegin_mask && i > iend_mask &&
274                   j < jbegin_mask && j > jend_mask )
275                     mask(i,j) = false;
276            }
277         }
278      }
279      else // (!mask.hasValue())
280      { // Si aucun masque n'est défini,
281        // on en crée un nouveau qui valide l'intégralité du domaine.
282         mask.resize(ni,nj) ;
283         for (int i = 0; i < ni.getValue(); i++)
284         {
285            for (int j = 0; j < nj.getValue(); j++)
286            {
287               if (i >= ibegin_mask && i <= iend_mask &&
288                   j >= jbegin_mask && j <= jend_mask )
289                     mask(i,j) = true;
290               else  mask(i,j) = false;
291            }
292         }
293      }
294   }
295
296
297   //----------------------------------------------------------------
298
299   void CDomain::checkDomainData(void)
300   {     
301      if (!data_dim.isEmpty() &&
302         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
303      {
304         ERROR("CDomain::checkAttributes(void)",
305               << "Dimension des données non comptatible (doit être 1 ou 2) !") ;
306      }
307      else if (data_dim.isEmpty())
308      {
309         ERROR("CDomain::checkAttributes(void)",
310               << "Dimension des données non définie !") ;
311      }
312
313      if (data_ibegin.isEmpty())
314         data_ibegin.setValue(0) ;
315      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
316         data_jbegin.setValue(0) ;
317
318      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
319      {
320         ERROR("CDomain::checkAttributes(void)",
321               << "Dimension des données négative (data_ni).") ;
322      }
323      else if (data_ni.isEmpty())
324      {
325         data_ni.setValue((data_dim.getValue() == 1)
326                           ? (ni.getValue() * nj.getValue())
327                           : ni.getValue());
328      }
329
330      if (data_dim.getValue() == 2)
331      {
332         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
333         {
334            ERROR("CDomain::checkAttributes(void)",
335                  << "Dimension des données négative (data_nj).") ;
336         }
337         else if (data_nj.isEmpty())
338            data_nj.setValue(nj.getValue()) ;
339      }
340
341   }
342
343   //----------------------------------------------------------------
344
345   void CDomain::checkCompression(void)
346   {
347      if (!data_i_index.isEmpty())
348      {
349         int ssize = data_i_index.numElements();
350         if (!data_n_index.isEmpty() &&
351            (data_n_index.getValue() != ssize))
352         {
353            ERROR("CDomain::checkAttributes(void)",
354                  <<"Dimension data_i_index incompatible avec data_n_index.") ;
355         }
356         else if (data_n_index.isEmpty())
357            data_n_index.setValue(ssize) ;
358
359         if (data_dim.getValue() == 2)
360         {
361            if (!data_j_index.isEmpty() &&
362               (data_j_index.numElements() != data_i_index.numElements()))
363            {
364               ERROR("CDomain::checkAttributes(void)",
365                     <<"Dimension data_j_index incompatible avec data_i_index.") ;
366            }
367            else if (data_j_index.isEmpty())
368            {
369               ERROR("CDomain::checkAttributes(void)",
370                     <<"La donnée data_j_index doit être renseignée !") ;
371            }
372         }
373      }
374      else
375      {
376         if (!data_n_index.isEmpty() ||
377            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
378            ERROR("CDomain::checkAttributes(void)", << "data_i_index non défini") ;
379      }
380
381      if (data_n_index.isEmpty())
382      { // -> bloc re-vérifié OK
383         if (data_dim.getValue() == 1)
384         {
385            const int dni = data_ni.getValue();
386            data_i_index.resize(dni) ;
387            data_n_index.setValue(dni);
388            for (int i = 0; i < dni; i++) data_i_index(i) = i+1 ;
389         }
390         else   // (data_dim == 2)
391         {
392            const int dni = data_ni.getValue() * data_nj.getValue();
393            data_i_index.resize(dni) ;
394            data_j_index.resize(dni) ;
395           
396            data_n_index.setValue(dni);
397           
398            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
399            {
400               for(int i = 0; i < data_ni.getValue(); i++, count++)
401               {
402                  data_i_index(count) = i+1 ;
403                  data_j_index(count) = j+1 ;
404               }
405            }
406         }
407      }
408   }
409
410   //----------------------------------------------------------------
411   
412   void CDomain::completeLonLatClient(void)
413   {
414      CArray<double,1> lonvalue_temp ;
415      CArray<double,1> latvalue_temp ;
416     
417      const int zoom_ibegin_client  = zoom_ibegin_loc.getValue(),
418                zoom_jbegin_client  = zoom_jbegin_loc.getValue(),
419                zoom_ni_client      = zoom_ni_loc.getValue(),
420                zoom_nj_client      = zoom_nj_loc.getValue();
421               
422      if (this->data_dim.getValue() == 2)
423      {
424         StdSize dm = zoom_ni_client * zoom_nj_client;
425
426         lonvalue_temp.resize(dm);
427         latvalue_temp.resize(dm);
428         
429         for (int i = 0; i < zoom_ni_client; i++)
430         {
431            for (int j = 0; j < zoom_nj_client; j++)
432            {
433               lonvalue_temp(i + j * zoom_ni_client) = lonvalue( (i + zoom_ibegin_client -1) + (j + zoom_jbegin_client -1)*ni.getValue() ); 
434               latvalue_temp(i + j * zoom_ni_client) = latvalue( (i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue() );
435            }
436         }
437         lonvalue.reference(lonvalue_temp.copy()) ;
438         latvalue.reference(latvalue_temp.copy()) ;
439         cout<<lonvalue<<endl ;
440         cout<<latvalue<<endl ;
441      }
442      else
443      {
444         lonvalue_temp.resize(zoom_ni_client);
445         latvalue_temp.resize(zoom_nj_client);
446
447// Attention ici à revoir, probablement faux         
448         for (int i = zoom_ibegin_client - 1; i < (zoom_ni_client - zoom_ibegin_client + 1); i++)
449         {
450            lonvalue_temp(i) = lonvalue(i); 
451         }
452         
453         for (int j = zoom_jbegin_client - 1; j < (zoom_nj_client - zoom_jbegin_client + 1); j++)
454         {
455            latvalue_temp(j) = latvalue(j);
456         }
457         
458         lonvalue.reference(lonvalue_temp.copy()) ;
459         latvalue.reference(latvalue_temp.copy()) ;
460      } 
461   }
462 
463   //----------------------------------------------------------------
464     
465   void CDomain::completeLonLatServer(void)
466   {
467      CArray<double,1> lonvalue_temp ;
468      CArray<double,1> latvalue_temp ;
469     
470      const int ibegin_serv     = ibegin.getValue(),
471                jbegin_serv     = jbegin.getValue(),
472                zoom_ni_serv    = zoom_ni_loc.getValue(),
473                zoom_nj_serv    = zoom_nj_loc.getValue(),
474                ibegin_zoom_srv = zoom_ibegin_loc.getValue(),
475                jbegin_zoom_srv = zoom_jbegin_loc.getValue();
476                     
477                       
478      if (this->data_dim.getValue() == 2)
479      {
480         StdSize dm = zoom_ni_serv * zoom_nj_serv;     
481         
482         lonvalue_temp.resize(dm);
483         latvalue_temp.resize(dm);
484         
485         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
486         {
487            CArray<double,1> lonvalue_loc(*(lonvalue_sub[k])) ;
488            CArray<double,1> latvalue_loc (*(latvalue_sub[k]));
489           
490            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
491                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k],
492                      ibegin_cl = ibegin_sub[k] ,
493                      jbegin_cl = jbegin_sub[k] ,
494                      ni_cl = iend_sub[k] - ibegin_sub[k] + 1;
495                     
496            for (int i = 0; i < zoom_ni_cl; i++)
497            {
498               for (int j = 0; j < zoom_nj_cl; j++)
499               {
500                  int ii = i + (ibegin_cl-1) - (ibegin_serv - 1) + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1);
501                  int jj = j + (jbegin_cl-1) - (jbegin_serv - 1) + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1);
502                  lonvalue_temp(ii + jj * zoom_ni_serv) = lonvalue_loc(i + j * zoom_ni_cl);
503                  latvalue_temp(ii + jj * zoom_ni_serv) = latvalue_loc(i + j * zoom_ni_cl);
504               }
505            }
506         }
507         
508         lonvalue.reference(lonvalue_temp.copy()) ;
509         latvalue.reference(latvalue_temp.copy()) ;
510      }
511      else
512      {
513         lonvalue_temp.resize(zoom_ni_serv);
514         latvalue_temp.resize(zoom_nj_serv);
515         
516         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
517         {
518            CArray<double,1> lonvalue_loc(*(lonvalue_sub[k]));
519            CArray<double,1> latvalue_loc(*(latvalue_sub[k]));
520           
521            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
522                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k];
523                     
524            for (int i = 0; i < zoom_ni_cl; i++)
525              lonvalue_temp(i /*- (ibegin_serv - 1)*/ + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1)) = lonvalue_loc(i);
526               
527            for (int j = 0; j < zoom_nj_cl; j++)
528              latvalue_temp(j /*- (jbegin_serv - 1)*/ + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1)) = latvalue_loc(j);
529         }       
530         lonvalue.reference(lonvalue_temp.copy()) ;
531         latvalue.reference(latvalue_temp.copy()) ;
532      }
533   }
534
535   //----------------------------------------------------------------
536
537   void CDomain::checkZoom(void)
538   {
539      // Résolution et vérification des données globales de zoom.
540      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
541          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
542      {
543         if (this->zoom_ni.isEmpty()     && this->zoom_nj.isEmpty() &&
544             this->zoom_ibegin.isEmpty() && this->zoom_jbegin.isEmpty())
545         {
546            ERROR("CDomain::checkZoom(void)",
547                  <<"Les attributs définissant un zoom doivent tous être définis") ;
548         }
549         else
550         {
551            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
552            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
553               
554            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
555                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
556               ERROR("CDomain::checkZoom(void)",
557                     << "Zoom mal défini,"
558                     << " vérifiez les valeurs zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
559         }
560      }
561      else
562      {
563         this->zoom_ni.setValue(this->ni_glo.getValue()); 
564         this->zoom_nj.setValue(this->nj_glo.getValue());
565         this->zoom_ibegin.setValue(1);
566         this->zoom_jbegin.setValue(1);
567      }
568      // Résolution des données locales de zoom.
569      {
570         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
571         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
572         
573         if ((zoom_ibegin.getValue() > iend.getValue()) || 
574             (zoom_iend < ibegin.getValue()))
575         {
576            zoom_ni_loc.setValue(0);
577            zoom_ibegin_loc.setValue(zoom_ibegin.getValue());
578         }
579         else
580         {
581            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
582                                 ? zoom_ibegin.getValue()
583                                 : ibegin.getValue();
584            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
585                                 ? zoom_iend
586                                 : iend.getValue();
587            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
588           
589            zoom_ni_loc.setValue(zoom_ni_loc_);
590            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
591         }
592         
593         if ((zoom_jbegin.getValue() > jend.getValue()) || 
594             (zoom_jend < jbegin.getValue()))
595         {
596            zoom_nj_loc.setValue(0);
597            zoom_jbegin_loc.setValue(zoom_jbegin.getValue());
598         }
599         else
600         {
601            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
602                                 ? zoom_jbegin.getValue()
603                                 : jbegin.getValue();
604            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
605                                 ? zoom_jend
606                                 : jend.getValue();
607            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
608           
609            zoom_nj_loc.setValue(zoom_nj_loc_);
610            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
611         }
612      }
613   }
614
615   //----------------------------------------------------------------
616
617   void CDomain::checkAttributes(void)
618   {
619      if (this->isChecked) return;
620      CContext* context=CContext::getCurrent() ;
621
622      this->checkGlobalDomain();
623      this->checkLocalIDomain();
624      this->checkLocalJDomain();
625     
626      this->checkZoom();
627     
628      if (context->hasClient)
629      { // CÃŽté client uniquement
630         this->checkMask();
631         this->checkDomainData();
632         this->checkCompression();
633         
634         this->ibegin_sub.push_back(this->ibegin.getValue());
635         this->jbegin_sub.push_back(this->jbegin.getValue());
636         this->iend_sub.push_back(this->iend.getValue());
637         this->jend_sub.push_back(this->jend.getValue()); 
638
639         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
640         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
641         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
642         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
643     
644         this->latvalue_sub.push_back(new CArray<double,1>(latvalue.copy()));
645         this->lonvalue_sub.push_back(new CArray<double,1>(lonvalue.copy())); 
646
647
648//         if (!this->isEmpty())
649//         {
650            this->completeLonLatClient();
651//         }
652         this->completeMask();
653
654      }
655      else
656      { // CÃŽté serveur uniquement
657//         if (!this->isEmpty())
658// ne sert plus //   this->completeLonLatServer();
659      }
660   
661      if (context->hasClient)
662      {
663        computeConnectedServer() ;
664        sendServerAttribut() ;
665        sendLonLat() ;
666      }
667     
668      this->isChecked = true;
669   }
670   
671  void CDomain::sendServerAttribut(void)
672  {
673    int ni_srv=ni_glo.getValue() ;
674    int ibegin_srv=1 ;
675    int iend_srv=ni_glo.getValue() ;
676     
677    int nj_srv ;
678    int jbegin_srv ;
679    int jend_srv ;
680   
681    CContext* context=CContext::getCurrent() ;
682    CContextClient* client=context->client ;
683    int nbServer=client->serverSize ;
684    int serverRank=client->getServerLeader() ;
685   
686    jend_srv=0 ;
687    for(int i=0;i<=serverRank;i++)
688    {
689      jbegin_srv=jend_srv+1 ;
690      nj_srv=nj_glo.getValue()/nbServer ;
691      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
692      jend_srv=jbegin_srv+nj_srv-1 ;
693    }
694   
695     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
696     if (client->isServerLeader())
697     {
698       CMessage msg ;
699       msg<<this->getId() ;
700       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
701       event.push(client->getServerLeader(),1,msg) ;
702       client->sendEvent(event) ;
703     }
704     else client->sendEvent(event) ;
705  }
706 
707  void CDomain::computeConnectedServer(void)
708  {
709    int ib,ie,in;
710    int jb,je,jn ;
711   
712    int ni_srv=ni_glo.getValue() ;
713    int ibegin_srv=1 ;
714    int iend_srv=ni_glo.getValue() ;
715     
716    int nj_serv,jbegin_srv, jend_srv ;
717    int zoom_ibegin_srv,zoom_iend_srv,zoom_ni_srv ;
718    int zoom_jbegin_srv,zoom_jend_srv,zoom_nj_srv ;
719   
720    ibegin_client=ibegin.getValue() ; iend_client=iend.getValue() ; ni_client=ni.getValue() ;
721    jbegin_client=jbegin.getValue() ; jend_client=jend.getValue() ; nj_client=nj.getValue() ;
722     
723    CContext* context = CContext::getCurrent() ;
724    CContextClient* client=context->client ;
725    int nbServer=client->serverSize ;
726   
727    // compute client zoom indices
728    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
729    zoom_ibegin_client = ibegin_client > zoom_ibegin.getValue() ? ibegin_client : zoom_ibegin.getValue() ;
730    zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
731    zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
732    if (zoom_ni_client<0) zoom_ni_client=0 ;
733
734    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
735    zoom_jbegin_client = jbegin_client > zoom_jbegin.getValue() ? jbegin_client : zoom_jbegin.getValue() ;
736    zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
737    zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
738    if (zoom_nj_client<0) zoom_nj_client=0 ;
739 
740    // find how much client are connected to a server
741    jend_srv=0 ;
742    for(int ns=0;ns<nbServer;ns++)
743    {
744      jbegin_srv=jend_srv+1 ;
745      nj_srv=nj_glo.getValue()/nbServer ;
746      if (ns<nj_glo.getValue()%nbServer) nj_srv++ ;
747      jend_srv=jbegin_srv+nj_srv-1 ;
748     
749      ib = ibegin_client>ibegin_srv ? ibegin_client : ibegin_srv ;
750      ie=  iend_client< iend_srv? iend_client : iend_srv ;
751      in=ie-ib+1 ;
752      if (in<0) in=0 ;
753     
754      jb= jbegin_client>jbegin_srv ? jbegin_client : jbegin_srv ;
755      je= jend_client<jend_srv ? jend_client : jend_srv ;
756      jn=je-jb+1 ;
757      if (jn<0) jn=0 ;
758       
759      if (in>0 && jn>0)
760      {
761        zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
762        zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
763        zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
764        if (zoom_ni_srv<0) zoom_ni_srv=0 ;
765     
766        zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
767        zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
768        zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
769        if (zoom_nj_srv<0) zoom_nj_srv=0 ;
770 
771        if (zoom_ni_srv>0 && zoom_nj_srv>0 && zoom_ni_client>0 && zoom_nj_client>0)
772        {
773          ib = zoom_ibegin_client>zoom_ibegin_srv ? zoom_ibegin_client : zoom_ibegin_srv ;
774          ie=zoom_iend_client<zoom_iend_srv?zoom_iend_client:zoom_iend_srv ;
775          in=ie-ib+1 ;
776          if (in<0) in=0 ;
777       
778          jb=zoom_jbegin_client>zoom_jbegin_srv?zoom_jbegin_client:zoom_jbegin_srv ;
779          je=zoom_jend_client<zoom_jend_srv?zoom_jend_client:zoom_jend_srv ;
780          jn=je-jb+1 ;
781          if (jn<0) jn=0 ;
782        }
783        else
784        {
785          ib=1 ; ie=0 ; in=0 ;
786          jb=1 ; je=0 ; jn=0 ;
787        }
788       
789//          if (in>0 && jn>0)
790//          {
791            connectedServer.push_back(ns) ;
792            ib_srv.push_back(ib) ;
793            ie_srv.push_back(ie) ;
794            in_srv.push_back(in) ;
795            jb_srv.push_back(jb) ;
796            je_srv.push_back(je) ;
797            jn_srv.push_back(jn) ;
798//           }
799       
800      }
801    }
802    int nbConnectedServer=connectedServer.size() ;
803    int* recvCount=new int[client->clientSize] ;
804    int* displ=new int[client->clientSize] ;
805    int* sendBuff=new int[nbConnectedServer] ;
806    valarray<int> nbClient(0,client->serverSize) ;
807   
808    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
809   
810    // get connected server for everybody
811    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
812   
813    displ[0]=0 ;
814    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
815    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
816    int* recvBuff=new int[recvSize] ;
817 
818   
819    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
820    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
821   
822    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
823   
824    delete [] recvCount ;
825    delete [] displ ;
826    delete [] sendBuff ;
827    delete [] recvBuff ;
828  }
829 
830  void CDomain::sendLonLat(void)
831  {
832    CContext* context = CContext::getCurrent() ;
833    CContextClient* client=context->client ;
834    // send lon lat for each connected server
835    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
836   
837    int ib,ie,in ;
838    int jb,je,jn ;
839 
840    list<shared_ptr<CMessage> > list_msg ;   
841    list<CArray<double,1>* > list_indi,list_indj,list_lon,list_lat ;
842
843    for(int ns=0;ns<connectedServer.size();ns++)
844    {
845      ib=ib_srv[ns] ; ie=ie_srv[ns] ; in=in_srv[ns] ;
846      jb=jb_srv[ns] ; je=je_srv[ns] ; jn=jn_srv[ns] ;
847     
848      CArray<double,1> indi(in*jn) ;
849      CArray<double,1> indj(in*jn) ;
850      CArray<double,1> lon(in*jn) ;
851      CArray<double,1> lat(in*jn) ;
852
853         
854      int ind_client,ind_loc ;
855     
856      for(int j=jb;j<=je;j++)
857        for(int i=ib;i<=ie;i++)
858        {
859          ind_client=(i-zoom_ibegin_client)+(j-zoom_jbegin_client)*zoom_ni_client ;
860          ind_loc=(i-ib)+(j-jb)*in ;
861          lon(ind_loc)=lonvalue(ind_client) ;
862          lat(ind_loc)=latvalue(ind_client) ;
863          indi(ind_loc)=i ;
864          indj(ind_loc)=j ;
865        }
866     
867      list_indi.push_back(new CArray<double,1>(indi.copy())) ;
868      list_indj.push_back(new CArray<double,1>(indj.copy())) ;
869      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
870      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
871
872      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
873
874      *list_msg.back()<<this->getId() ;
875      *list_msg.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back()<<*list_lat.back() ;
876      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
877    }
878
879    client->sendEvent(event) ;
880   
881    list<CArray<double,1>* >::iterator it;
882    for(it=list_indi.begin();it!=list_indi.end();it++) delete *it;
883    for(it=list_indj.begin();it!=list_indj.end();it++) delete *it;
884    for(it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
885    for(it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
886   
887  }
888 
889  bool CDomain::dispatchEvent(CEventServer& event)
890   {
891     
892      if (SuperClass::dispatchEvent(event)) return true ;
893      else
894      {
895        switch(event.type)
896        {
897           case EVENT_ID_SERVER_ATTRIBUT :
898             recvServerAttribut(event) ;
899             return true ;
900             break ;
901           case EVENT_ID_LON_LAT :
902             recvLonLat(event) ;
903             return true ;
904             break ;
905           default :
906             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
907                    <<"Unknown Event") ;
908           return false ;
909         }
910      }
911   }
912   
913  void CDomain::recvServerAttribut(CEventServer& event)
914  {
915    CBufferIn* buffer=event.subEvents.begin()->buffer;
916    string domainId ;
917    *buffer>>domainId ;
918    get(domainId)->recvServerAttribut(*buffer) ;
919  }
920 
921  void CDomain::recvServerAttribut(CBufferIn& buffer)
922  {
923    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
924    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
925
926     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
927   
928    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
929    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
930    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
931     
932    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
933    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
934    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
935
936    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
937    {
938      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
939      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
940    }
941   
942    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
943    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
944  }
945   
946  void CDomain::recvLonLat(CEventServer& event)
947  {
948    list<CEventServer::SSubEvent>::iterator it ;
949    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
950    {
951      CBufferIn* buffer=it->buffer;
952      string domainId ;
953      *buffer>>domainId ;
954      get(domainId)->recvLonLat(*buffer) ;
955    }
956  }
957 
958  void CDomain::recvLonLat(CBufferIn& buffer)
959  {
960    CArray<double,1> indi ;
961    CArray<double,1> indj ;
962    CArray<double,1> lon ;
963    CArray<double,1> lat ;
964     
965    buffer>>indi>>indj>>lon>>lat ;
966    int i,j,ind_srv ;
967
968    for(int ind=0;ind<indi.numElements();ind++)
969    {
970      i=indi(ind) ; j=indj(ind) ;
971      ind_srv=(i-zoom_ibegin_srv)+(j-zoom_jbegin_srv)*zoom_ni_srv ;
972      lonvalue_srv(ind_srv)=lon(ind) ;
973      latvalue_srv(ind_srv)=lat(ind) ;
974    }
975  }
976   //----------------------------------------------------------------
977   
978   void CDomain::completeMask(void)
979   {
980      this->local_mask.resize(zoom_ni_loc,zoom_nj_loc);
981   }
982
983   //----------------------------------------------------------------
984
985   CArray<int,2> CDomain::getLocalMask(void) const
986   {
987      return (this->local_mask);
988   }
989   
990   //----------------------------------------------------------------
991   
992   const std::vector<int> & CDomain::getIBeginSub(void) const
993   {
994      return (this->ibegin_sub);
995   }
996   
997   //----------------------------------------------------------------
998   
999   const std::vector<int> & CDomain::getIBeginZoomSub(void) const
1000   {
1001      return (this->ibegin_zoom_sub);
1002   }
1003
1004   const std::vector<int> & CDomain::getNiZoomSub(void) const
1005   {
1006      return (this->ni_zoom_sub);
1007   }
1008               
1009   //----------------------------------------------------------------
1010                     
1011   const std::vector<int> & CDomain::getIEndSub(void) const
1012   {
1013      return (this->iend_sub);
1014   }
1015   
1016   //----------------------------------------------------------------
1017   
1018   const std::vector<int> & CDomain::getJBeginSub(void) const
1019   {
1020      return (this->jbegin_sub);
1021   }
1022   
1023   //----------------------------------------------------------------
1024     
1025   const std::vector<int> & CDomain::getJBeginZoomSub(void) const
1026   {
1027      return (this->jbegin_zoom_sub);
1028   }
1029
1030   const std::vector<int> & CDomain::getNjZoomSub(void) const
1031   {
1032      return (this->nj_zoom_sub);
1033   }
1034                 
1035   
1036   //----------------------------------------------------------------
1037   
1038   const std::vector<int> & CDomain::getJEndSub(void) const
1039   {
1040      return (this->jend_sub);
1041   }
1042   
1043   //----------------------------------------------------------------
1044   
1045   const std::vector<CArray<double, 1>* > & CDomain::getLonValueSub(void) const
1046   {
1047      return (this->lonvalue_sub);
1048   }
1049   
1050   //----------------------------------------------------------------
1051   
1052   const std::vector<CArray<double,1>*> & CDomain::getLatValueSub(void) const
1053   {
1054      return (this->latvalue_sub);
1055   }   
1056   
1057   ///---------------------------------------------------------------
1058
1059} // namespace xios
Note: See TracBrowser for help on using the repository browser.