New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
domain.cpp in vendors/XIOS/current/src/node – NEMO

source: vendors/XIOS/current/src/node/domain.cpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.5 KB
Line 
1#include "domain.hpp"
2
3#include "attribute_template_impl.hpp"
4#include "object_template_impl.hpp"
5#include "group_template_impl.hpp"
6
7#include "xmlioserver_spl.hpp"
8#include "event_client.hpp"
9#include "event_server.hpp"
10#include "buffer_in.hpp"
11
12namespace xios {
13   
14   /// ////////////////////// Définitions ////////////////////// ///
15
16   CDomain::CDomain(void)
17      : CObjectTemplate<CDomain>(), CDomainAttributes()
18      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[0][0])), relFiles()
19      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
20      , ibegin_zoom_sub(), jbegin_zoom_sub(), ni_zoom_sub(), nj_zoom_sub()
21      , lonvalue_sub(), latvalue_sub(),lonvalue_srv(new CArray<double,1>())
22      , latvalue_srv(new CArray<double,1>())
23   { /* Ne rien faire de plus */ }
24
25   CDomain::CDomain(const StdString & id)
26      : CObjectTemplate<CDomain>(id), CDomainAttributes()
27      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[0][0])), relFiles()
28      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
29      , ibegin_zoom_sub(), jbegin_zoom_sub(),ni_zoom_sub(), nj_zoom_sub()
30      , lonvalue_sub(), latvalue_sub(),lonvalue_srv(new CArray<double,1>())
31      , latvalue_srv(new CArray<double,1>())
32   { /* Ne rien faire de plus */ }
33
34   CDomain::~CDomain(void)
35   { 
36      this->local_mask.reset();
37      for (StdSize i = 0; i < this->lonvalue_sub.size(); i++)
38      {
39         this->lonvalue_sub[i].reset();
40         this->latvalue_sub[i].reset();
41      }     
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_)->clear()
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         ARRAY(bool, 2) mask_ = mask.getValue();
264         unsigned int niu = ni.getValue(), nju = nj.getValue();
265         if ((mask_->shape()[0] != niu) ||
266             (mask_->shape()[1] != nju))
267            ERROR("CDomain::checkAttributes(void)",
268                  <<"Le masque n'a pas la même taille que le domaine local") ;
269                 
270         for (int i = 0; i < ni.getValue(); i++)
271         {
272            for (int j = 0; j < nj.getValue(); j++)
273            {
274               if (i < ibegin_mask && i > iend_mask &&
275                   j < jbegin_mask && j > jend_mask )
276                     (*mask_)[i][j] = false;
277            }
278         }
279      }
280      else // (!mask.hasValue())
281      { // Si aucun masque n'est défini,
282        // on en crée un nouveau qui valide l'intégralité du domaine.
283         ARRAY_CREATE(__arr, bool, 2, [ni.getValue()][nj.getValue()]);
284         for (int i = 0; i < ni.getValue(); i++)
285         {
286            for (int j = 0; j < nj.getValue(); j++)
287            {
288               if (i >= ibegin_mask && i <= iend_mask &&
289                   j >= jbegin_mask && j <= jend_mask )
290                     (*__arr)[i][j] = true;
291               else  (*__arr)[i][j] = false;
292            }
293         }
294               
295         mask.setValue(__arr);
296         __arr.reset();
297      }
298   }
299
300
301   //----------------------------------------------------------------
302
303   void CDomain::checkDomainData(void)
304   {     
305      if (!data_dim.isEmpty() &&
306         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
307      {
308         ERROR("CDomain::checkAttributes(void)",
309               << "Dimension des données non comptatible (doit être 1 ou 2) !") ;
310      }
311      else if (data_dim.isEmpty())
312      {
313         ERROR("CDomain::checkAttributes(void)",
314               << "Dimension des données non définie !") ;
315      }
316
317      if (data_ibegin.isEmpty())
318         data_ibegin.setValue(0) ;
319      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
320         data_jbegin.setValue(0) ;
321
322      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
323      {
324         ERROR("CDomain::checkAttributes(void)",
325               << "Dimension des données négative (data_ni).") ;
326      }
327      else if (data_ni.isEmpty())
328      {
329         data_ni.setValue((data_dim.getValue() == 1)
330                           ? (ni.getValue() * nj.getValue())
331                           : ni.getValue());
332      }
333
334      if (data_dim.getValue() == 2)
335      {
336         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
337         {
338            ERROR("CDomain::checkAttributes(void)",
339                  << "Dimension des données négative (data_nj).") ;
340         }
341         else if (data_nj.isEmpty())
342            data_nj.setValue(nj.getValue()) ;
343      }
344
345   }
346
347   //----------------------------------------------------------------
348
349   void CDomain::checkCompression(void)
350   {
351      if (!data_i_index.isEmpty())
352      {
353         int ssize = data_i_index.getValue()->size();
354         if (!data_n_index.isEmpty() &&
355            (data_n_index.getValue() != ssize))
356         {
357            ERROR("CDomain::checkAttributes(void)",
358                  <<"Dimension data_i_index incompatible avec data_n_index.") ;
359         }
360         else if (data_n_index.isEmpty())
361            data_n_index.setValue(ssize) ;
362
363         if (data_dim.getValue() == 2)
364         {
365            if (!data_j_index.isEmpty() &&
366               (data_j_index.getValue()->size() != data_i_index.getValue()->size()))
367            {
368               ERROR("CDomain::checkAttributes(void)",
369                     <<"Dimension data_j_index incompatible avec data_i_index.") ;
370            }
371            else if (data_j_index.isEmpty())
372            {
373               ERROR("CDomain::checkAttributes(void)",
374                     <<"La donnée data_j_index doit être renseignée !") ;
375            }
376         }
377      }
378      else
379      {
380         if (!data_n_index.isEmpty() ||
381            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
382            ERROR("CDomain::checkAttributes(void)", << "data_i_index non défini") ;
383      }
384
385      if (data_n_index.isEmpty())
386      { // -> bloc re-vérifié OK
387         if (data_dim.getValue() == 1)
388         {
389            const int dni = data_ni.getValue();
390            ARRAY_CREATE(__arri, int, 1, [dni]);
391            data_n_index.setValue(dni);
392            for (int i = 0; i < dni; i++)
393               (*__arri)[i] = i+1 ;
394            data_i_index.setValue(__arri) ;
395         }
396         else   // (data_dim == 2)
397         {
398            const int dni = data_ni.getValue() * data_nj.getValue();
399           
400            ARRAY_CREATE(__arri, int, 1, [dni]);
401            ARRAY_CREATE(__arrj, int, 1, [dni]);               
402            data_n_index.setValue(dni);
403           
404            //for(int count = 0, i = 0; i  < data_ni.getValue(); i++)
405            //for(int j = 0; j < data_nj.getValue(); j++, count++)
406           
407            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
408            {
409               for(int i = 0; i < data_ni.getValue(); i++, count++)
410               {
411                  (*__arri)[count] = i+1 ;
412                  (*__arrj)[count] = j+1 ;
413               }
414            }
415            data_i_index.setValue(__arri) ;
416            data_j_index.setValue(__arrj) ;           
417            __arri.reset();
418            __arrj.reset();
419         }
420      }
421   }
422
423   //----------------------------------------------------------------
424   
425   void CDomain::completeLonLatClient(void)
426   {
427      ARRAY_CREATE(lonvalue_temp, double, 1, [0]);
428      ARRAY_CREATE(latvalue_temp, double, 1, [0]);
429     
430      const int zoom_ibegin_client  = zoom_ibegin_loc.getValue(),
431                zoom_jbegin_client  = zoom_jbegin_loc.getValue(),
432                zoom_ni_client      = zoom_ni_loc.getValue(),
433                zoom_nj_client      = zoom_nj_loc.getValue();
434               
435      ARRAY(double, 1) lonvalue_ = this->lonvalue.getValue(),
436                       latvalue_ = this->latvalue.getValue();
437               
438      if (this->data_dim.getValue() == 2)
439      {
440         StdSize dm = zoom_ni_client * zoom_nj_client;
441
442         lonvalue_temp->resize(boost::extents[dm]);
443         latvalue_temp->resize(boost::extents[dm]);
444         
445         for (int i = 0; i < zoom_ni_client; i++)
446         {
447            for (int j = 0; j < zoom_nj_client; j++)
448            {
449               (*lonvalue_temp)[i + j * zoom_ni_client] =
450               (*lonvalue_)[(i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue()];             
451               (*latvalue_temp)[i + j * zoom_ni_client] =
452               (*latvalue_)[(i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue()];
453            }
454         }
455         this->lonvalue.setValue(lonvalue_temp);
456         this->latvalue.setValue(latvalue_temp);
457      }
458      else
459      {
460         lonvalue_temp->resize(boost::extents[zoom_ni_client]);
461         latvalue_temp->resize(boost::extents[zoom_nj_client]);
462         
463         for (int i = zoom_ibegin_client - 1; i < (zoom_ni_client - zoom_ibegin_client + 1); i++)
464         {
465            (*lonvalue_temp)[i] = (*lonvalue_)[i]; 
466         }
467         
468         for (int j = zoom_ibegin_client - 1; j < (zoom_nj_client - zoom_jbegin_client + 1); j++)
469         {
470            (*latvalue_temp)[j] = (*latvalue_)[j];
471         }
472         
473         this->lonvalue.setValue(lonvalue_temp);
474         this->latvalue.setValue(latvalue_temp);
475      } 
476   }
477 
478   //----------------------------------------------------------------
479     
480   void CDomain::completeLonLatServer(void)
481   {
482      ARRAY_CREATE(lonvalue_temp, double, 1, [0]);
483      ARRAY_CREATE(latvalue_temp, double, 1, [0]);
484     
485      const int ibegin_serv     = ibegin.getValue(),
486                jbegin_serv     = jbegin.getValue(),
487                zoom_ni_serv    = zoom_ni_loc.getValue(),
488                zoom_nj_serv    = zoom_nj_loc.getValue(),
489                ibegin_zoom_srv = zoom_ibegin_loc.getValue(),
490                jbegin_zoom_srv = zoom_jbegin_loc.getValue();
491                     
492                       
493      if (this->data_dim.getValue() == 2)
494      {
495         StdSize dm = zoom_ni_serv * zoom_nj_serv;     
496         
497         lonvalue_temp->resize(boost::extents[dm]);
498         latvalue_temp->resize(boost::extents[dm]);
499         
500         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
501         {
502            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
503                             latvalue_loc = this->latvalue_sub[k];
504            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
505                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k],
506                      ibegin_cl = ibegin_sub[k] ,
507                      jbegin_cl = jbegin_sub[k] ,
508                      ni_cl = iend_sub[k] - ibegin_sub[k] + 1;
509                     
510            for (int i = 0; i < zoom_ni_cl; i++)
511            {
512               for (int j = 0; j < zoom_nj_cl; j++)
513               {
514                  int ii = i + (ibegin_cl-1) - (ibegin_serv - 1) + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1);
515                  int jj = j + (jbegin_cl-1) - (jbegin_serv - 1) + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1);
516                  (*lonvalue_temp)[ii + jj * zoom_ni_serv] =
517                  (*lonvalue_loc)[i + j * zoom_ni_cl];
518                  (*latvalue_temp)[ii + jj * zoom_ni_serv] = 
519                  (*latvalue_loc)[i + j * zoom_ni_cl];
520               }
521            }
522         }
523         this->lonvalue.setValue(lonvalue_temp);
524         this->latvalue.setValue(latvalue_temp);
525      }
526      else
527      {
528         lonvalue_temp->resize(boost::extents[zoom_ni_serv]);
529         latvalue_temp->resize(boost::extents[zoom_nj_serv]);
530         
531         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
532         {
533            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
534                             latvalue_loc = this->latvalue_sub[k];
535            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
536                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k];
537                     
538            for (int i = 0; i < zoom_ni_cl; i++)
539               (*lonvalue_temp)[i /*- (ibegin_serv - 1)*/ + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1)] =
540               (*lonvalue_loc)[i];
541               
542            for (int j = 0; j < zoom_nj_cl; j++)
543               (*latvalue_temp)[j /*- (jbegin_serv - 1)*/ + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1)] =
544               (*latvalue_loc)[j];
545         }       
546         this->lonvalue.setValue(lonvalue_temp);
547         this->latvalue.setValue(latvalue_temp);
548      }
549   }
550
551   //----------------------------------------------------------------
552
553   void CDomain::checkZoom(void)
554   {
555      // Résolution et vérification des données globales de zoom.
556      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
557          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
558      {
559         if (this->zoom_ni.isEmpty()     && this->zoom_nj.isEmpty() &&
560             this->zoom_ibegin.isEmpty() && this->zoom_jbegin.isEmpty())
561         {
562            ERROR("CDomain::checkZoom(void)",
563                  <<"Les attributs définissant un zoom doivent tous être définis") ;
564         }
565         else
566         {
567            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
568            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
569               
570            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
571                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
572               ERROR("CDomain::checkZoom(void)",
573                     << "Zoom mal défini,"
574                     << " vérifiez les valeurs zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
575         }
576      }
577      else
578      {
579         this->zoom_ni.setValue(this->ni_glo.getValue()); 
580         this->zoom_nj.setValue(this->nj_glo.getValue());
581         this->zoom_ibegin.setValue(1);
582         this->zoom_jbegin.setValue(1);
583      }
584      // Résolution des données locales de zoom.
585      {
586         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
587         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
588         
589         if ((zoom_ibegin.getValue() > iend.getValue()) || 
590             (zoom_iend < ibegin.getValue()))
591         {
592            zoom_ni_loc.setValue(0);
593            zoom_ibegin_loc.setValue(zoom_ibegin.getValue());
594         }
595         else
596         {
597            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
598                                 ? zoom_ibegin.getValue()
599                                 : ibegin.getValue();
600            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
601                                 ? zoom_iend
602                                 : iend.getValue();
603            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
604           
605            zoom_ni_loc.setValue(zoom_ni_loc_);
606            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
607         }
608         
609         if ((zoom_jbegin.getValue() > jend.getValue()) || 
610             (zoom_jend < jbegin.getValue()))
611         {
612            zoom_nj_loc.setValue(0);
613            zoom_jbegin_loc.setValue(zoom_jbegin.getValue());
614         }
615         else
616         {
617            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
618                                 ? zoom_jbegin.getValue()
619                                 : jbegin.getValue();
620            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
621                                 ? zoom_jend
622                                 : jend.getValue();
623            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
624           
625            zoom_nj_loc.setValue(zoom_nj_loc_);
626            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
627         }
628      }
629   }
630
631   //----------------------------------------------------------------
632
633   void CDomain::checkAttributes(void)
634   {
635      if (this->isChecked) return;
636      CContext* context=CContext::getCurrent() ;
637
638      this->checkGlobalDomain();
639      this->checkLocalIDomain();
640      this->checkLocalJDomain();
641     
642      this->checkZoom();
643     
644      if (context->hasClient)
645      { // Côté client uniquement
646         this->checkMask();
647         this->checkDomainData();
648         this->checkCompression();
649         
650         this->ibegin_sub.push_back(this->ibegin.getValue());
651         this->jbegin_sub.push_back(this->jbegin.getValue());
652         this->iend_sub.push_back(this->iend.getValue());
653         this->jend_sub.push_back(this->jend.getValue()); 
654
655         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
656         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
657         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
658         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
659     
660         this->latvalue_sub.push_back(this->latvalue.getValue());
661         this->lonvalue_sub.push_back(this->lonvalue.getValue()); 
662
663
664//         if (!this->isEmpty())
665//         {
666            this->completeLonLatClient();
667//         }
668         this->completeMask();
669
670      }
671      else
672      { // Côté serveur uniquement
673//         if (!this->isEmpty())
674// ne sert plus //   this->completeLonLatServer();
675      }
676   
677      if (context->hasClient)
678      {
679        computeConnectedServer() ;
680        sendServerAttribut() ;
681        sendLonLat() ;
682      }
683     
684      this->isChecked = true;
685   }
686   
687  void CDomain::sendServerAttribut(void)
688  {
689    int ni_srv=ni_glo.getValue() ;
690    int ibegin_srv=1 ;
691    int iend_srv=ni_glo.getValue() ;
692     
693    int nj_srv ;
694    int jbegin_srv ;
695    int jend_srv ;
696   
697    CContext* context=CContext::getCurrent() ;
698    CContextClient* client=context->client ;
699    int nbServer=client->serverSize ;
700    int serverRank=client->getServerLeader() ;
701   
702    jend_srv=0 ;
703    for(int i=0;i<=serverRank;i++)
704    {
705      jbegin_srv=jend_srv+1 ;
706      nj_srv=nj_glo.getValue()/nbServer ;
707      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
708      jend_srv=jbegin_srv+nj_srv-1 ;
709    }
710   
711     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
712     if (client->isServerLeader())
713     {
714       CMessage msg ;
715       msg<<this->getId() ;
716       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
717       event.push(client->getServerLeader(),1,msg) ;
718       client->sendEvent(event) ;
719     }
720     else client->sendEvent(event) ;
721  }
722 
723  void CDomain::computeConnectedServer(void)
724  {
725    int ib,ie,in;
726    int jb,je,jn ;
727   
728    int ni_srv=ni_glo.getValue() ;
729    int ibegin_srv=1 ;
730    int iend_srv=ni_glo.getValue() ;
731     
732    int nj_serv,jbegin_srv, jend_srv ;
733    int zoom_ibegin_srv,zoom_iend_srv,zoom_ni_srv ;
734    int zoom_jbegin_srv,zoom_jend_srv,zoom_nj_srv ;
735   
736    ibegin_client=ibegin.getValue() ; iend_client=iend.getValue() ; ni_client=ni.getValue() ;
737    jbegin_client=jbegin.getValue() ; jend_client=jend.getValue() ; nj_client=nj.getValue() ;
738     
739    CContext* context = CContext::getCurrent() ;
740    CContextClient* client=context->client ;
741    int nbServer=client->serverSize ;
742   
743    // compute client zoom indices
744    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
745    zoom_ibegin_client = ibegin_client > zoom_ibegin.getValue() ? ibegin_client : zoom_ibegin.getValue() ;
746    zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
747    zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
748    if (zoom_ni_client<0) zoom_ni_client=0 ;
749
750    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
751    zoom_jbegin_client = jbegin_client > zoom_jbegin.getValue() ? jbegin_client : zoom_jbegin.getValue() ;
752    zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
753    zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
754    if (zoom_nj_client<0) zoom_nj_client=0 ;
755 
756    // find how much client are connected to a server
757    jend_srv=0 ;
758    for(int ns=0;ns<nbServer;ns++)
759    {
760      jbegin_srv=jend_srv+1 ;
761      nj_srv=nj_glo.getValue()/nbServer ;
762      if (ns<nj_glo.getValue()%nbServer) nj_srv++ ;
763      jend_srv=jbegin_srv+nj_srv-1 ;
764     
765      ib = ibegin_client>ibegin_srv ? ibegin_client : ibegin_srv ;
766      ie=  iend_client< iend_srv? iend_client : iend_srv ;
767      in=ie-ib+1 ;
768      if (in<0) in=0 ;
769     
770      jb= jbegin_client>jbegin_srv ? jbegin_client : jbegin_srv ;
771      je= jend_client<jend_srv ? jend_client : jend_srv ;
772      jn=je-jb+1 ;
773      if (jn<0) jn=0 ;
774       
775      if (in>0 && jn>0)
776      {
777        zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
778        zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
779        zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
780        if (zoom_ni_srv<0) zoom_ni_srv=0 ;
781     
782        zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
783        zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
784        zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
785        if (zoom_nj_srv<0) zoom_nj_srv=0 ;
786 
787        if (zoom_ni_srv>0 && zoom_nj_srv>0 && zoom_ni_client>0 && zoom_nj_client>0)
788        {
789          ib = zoom_ibegin_client>zoom_ibegin_srv ? zoom_ibegin_client : zoom_ibegin_srv ;
790          ie=zoom_iend_client<zoom_iend_srv?zoom_iend_client:zoom_iend_srv ;
791          in=ie-ib+1 ;
792          if (in<0) in=0 ;
793       
794          jb=zoom_jbegin_client>zoom_jbegin_srv?zoom_jbegin_client:zoom_jbegin_srv ;
795          je=zoom_jend_client<zoom_jend_srv?zoom_jend_client:zoom_jend_srv ;
796          jn=je-jb+1 ;
797          if (jn<0) jn=0 ;
798        }
799        else
800        {
801          ib=1 ; ie=0 ; in=0 ;
802          jb=1 ; je=0 ; jn=0 ;
803        }
804       
805//          if (in>0 && jn>0)
806//          {
807            connectedServer.push_back(ns) ;
808            ib_srv.push_back(ib) ;
809            ie_srv.push_back(ie) ;
810            in_srv.push_back(in) ;
811            jb_srv.push_back(jb) ;
812            je_srv.push_back(je) ;
813            jn_srv.push_back(jn) ;
814//           }
815       
816      }
817    }
818    int nbConnectedServer=connectedServer.size() ;
819    int* recvCount=new int[client->clientSize] ;
820    int* displ=new int[client->clientSize] ;
821    int* sendBuff=new int[nbConnectedServer] ;
822    valarray<int> nbClient(0,client->serverSize) ;
823   
824    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
825   
826    // get connected server for everybody
827    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
828   
829    displ[0]=0 ;
830    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
831    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
832    int* recvBuff=new int[recvSize] ;
833 
834   
835    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
836    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
837   
838    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
839   
840    delete [] recvCount ;
841    delete [] displ ;
842    delete [] sendBuff ;
843    delete [] recvBuff ;
844  }
845 
846  void CDomain::sendLonLat(void)
847  {
848    CContext* context = CContext::getCurrent() ;
849    CContextClient* client=context->client ;
850    // send lon lat for each connected server
851    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
852   
853    ARRAY(double, 1) lonvalue_client = lonvalue.getValue(),
854                     latvalue_client = latvalue.getValue();
855   
856    int ib,ie,in ;
857    int jb,je,jn ;
858 
859    list<shared_ptr<CMessage> > list_msg ;   
860    list<ARRAY(double,1)> list_indi,list_indj,list_lon,list_lat ;
861
862    for(int ns=0;ns<connectedServer.size();ns++)
863    {
864      ib=ib_srv[ns] ; ie=ie_srv[ns] ; in=in_srv[ns] ;
865      jb=jb_srv[ns] ; je=je_srv[ns] ; jn=jn_srv[ns] ;
866     
867      ARRAY_CREATE(indi,double,1,[in*jn]) ;
868      ARRAY_CREATE(indj,double,1,[in*jn]) ;
869      ARRAY_CREATE(lon,double,1,[in*jn]) ;
870      ARRAY_CREATE(lat,double,1,[in*jn]) ;
871
872         
873      int ind_client,ind_loc ;
874     
875      for(int j=jb;j<=je;j++)
876        for(int i=ib;i<=ie;i++)
877        {
878          ind_client=(i-zoom_ibegin_client)+(j-zoom_jbegin_client)*zoom_ni_client ;
879          ind_loc=(i-ib)+(j-jb)*in ;
880          (*lon)[ind_loc]=(*lonvalue_client)[ind_client] ;
881          (*lat)[ind_loc]=(*latvalue_client)[ind_client] ;
882          (*indi)[ind_loc]=i ;
883          (*indj)[ind_loc]=j ;
884        }
885     
886      list_indi.push_back(indi) ; list_indj.push_back(indj) ;
887      list_lon.push_back(lon) ; list_lat.push_back(lat) ;
888      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
889
890      *list_msg.back()<<this->getId() ;
891      *list_msg.back()<<list_indi.back()<<list_indj.back()<<list_lon.back()<<list_lat.back() ;
892      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
893    }
894
895    client->sendEvent(event) ;
896  }
897 
898  bool CDomain::dispatchEvent(CEventServer& event)
899   {
900     
901      if (SuperClass::dispatchEvent(event)) return true ;
902      else
903      {
904        switch(event.type)
905        {
906           case EVENT_ID_SERVER_ATTRIBUT :
907             recvServerAttribut(event) ;
908             return true ;
909             break ;
910           case EVENT_ID_LON_LAT :
911             recvLonLat(event) ;
912             return true ;
913             break ;
914           default :
915             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
916                    <<"Unknown Event") ;
917           return false ;
918         }
919      }
920   }
921   
922  void CDomain::recvServerAttribut(CEventServer& event)
923  {
924    CBufferIn* buffer=event.subEvents.begin()->buffer;
925    string domainId ;
926    *buffer>>domainId ;
927    get(domainId)->recvServerAttribut(*buffer) ;
928  }
929 
930  void CDomain::recvServerAttribut(CBufferIn& buffer)
931  {
932    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
933    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
934
935     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
936   
937    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
938    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
939    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
940     
941    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
942    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
943    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
944
945    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
946    {
947      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
948      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
949    }
950   
951    lonvalue_srv->resize(extents[zoom_ni_srv*zoom_nj_srv]) ;
952    latvalue_srv->resize(extents[zoom_ni_srv*zoom_nj_srv]) ;
953  }
954   
955  void CDomain::recvLonLat(CEventServer& event)
956  {
957    list<CEventServer::SSubEvent>::iterator it ;
958    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
959    {
960      CBufferIn* buffer=it->buffer;
961      string domainId ;
962      *buffer>>domainId ;
963      get(domainId)->recvLonLat(*buffer) ;
964    }
965  }
966 
967  void CDomain::recvLonLat(CBufferIn& buffer)
968  {
969    ARRAY_CREATE(indi,double,1,[0]) ;
970    ARRAY_CREATE(indj,double,1,[0]) ;
971    ARRAY_CREATE(lon,double,1,[0]) ;
972    ARRAY_CREATE(lat,double,1,[0]) ;
973   
974    buffer>>indi>>indj>>lon>>lat ;
975    int i,j,ind_srv ;
976
977    for(int ind=0;ind<indi->num_elements();ind++)
978    {
979      i=(*indi)[ind] ; j=(*indj)[ind] ;
980      ind_srv=(i-zoom_ibegin_srv)+(j-zoom_jbegin_srv)*zoom_ni_srv ;
981      (*lonvalue_srv)[ind_srv]=(*lon)[ind] ;
982      (*latvalue_srv)[ind_srv]=(*lat)[ind] ;
983    }
984  }
985   //----------------------------------------------------------------
986   
987   void CDomain::completeMask(void)
988   {
989      this->local_mask->resize(boost::extents[zoom_ni_loc.getValue()][zoom_nj_loc.getValue()]);
990   }
991
992   //----------------------------------------------------------------
993
994   ARRAY(int, 2) CDomain::getLocalMask(void) const
995   {
996      return (this->local_mask);
997   }
998   
999   //----------------------------------------------------------------
1000   
1001   const std::vector<int> & CDomain::getIBeginSub(void) const
1002   {
1003      return (this->ibegin_sub);
1004   }
1005   
1006   //----------------------------------------------------------------
1007   
1008   const std::vector<int> & CDomain::getIBeginZoomSub(void) const
1009   {
1010      return (this->ibegin_zoom_sub);
1011   }
1012
1013   const std::vector<int> & CDomain::getNiZoomSub(void) const
1014   {
1015      return (this->ni_zoom_sub);
1016   }
1017               
1018   //----------------------------------------------------------------
1019                     
1020   const std::vector<int> & CDomain::getIEndSub(void) const
1021   {
1022      return (this->iend_sub);
1023   }
1024   
1025   //----------------------------------------------------------------
1026   
1027   const std::vector<int> & CDomain::getJBeginSub(void) const
1028   {
1029      return (this->jbegin_sub);
1030   }
1031   
1032   //----------------------------------------------------------------
1033     
1034   const std::vector<int> & CDomain::getJBeginZoomSub(void) const
1035   {
1036      return (this->jbegin_zoom_sub);
1037   }
1038
1039   const std::vector<int> & CDomain::getNjZoomSub(void) const
1040   {
1041      return (this->nj_zoom_sub);
1042   }
1043                 
1044   
1045   //----------------------------------------------------------------
1046   
1047   const std::vector<int> & CDomain::getJEndSub(void) const
1048   {
1049      return (this->jend_sub);
1050   }
1051   
1052   //----------------------------------------------------------------
1053   
1054   const std::vector<ARRAY(double, 1)> & CDomain::getLonValueSub(void) const
1055   {
1056      return (this->lonvalue_sub);
1057   }
1058   
1059   //----------------------------------------------------------------
1060   
1061   const std::vector<ARRAY(double, 1)> & CDomain::getLatValueSub(void) const
1062   {
1063      return (this->latvalue_sub);
1064   }   
1065   
1066   ///---------------------------------------------------------------
1067
1068} // namespace xios
Note: See TracBrowser for help on using the repository browser.