source: XMLIO_V2/dev/common/src/xmlio/node/grid.cpp @ 219

Last change on this file since 219 was 219, checked in by hozdoba, 13 years ago

Préparation nouvelle arborescence

File size: 16.4 KB
Line 
1#include "grid.hpp"
2
3#include "attribute_template_impl.hpp"
4#include "object_template_impl.hpp"
5#include "group_template_impl.hpp"
6
7namespace xmlioserver {
8namespace tree {
9
10   /// ////////////////////// Définitions ////////////////////// ///
11
12   CGrid::CGrid(void)
13      : CObjectTemplate<CGrid>(), CGridAttributes()
14      , withAxis(false), isChecked(false), axis(), domain()
15      , storeIndex(1), out_i_index(1), out_j_index(1), out_l_index(1)
16   { /* Ne rien faire de plus */ }
17
18   CGrid::CGrid(const StdString & id)
19      : CObjectTemplate<CGrid>(id), CGridAttributes()
20      , withAxis(false), isChecked(false), axis(), domain()
21      , storeIndex(1), out_i_index(1), out_j_index(1), out_l_index(1)
22   { /* Ne rien faire de plus */ }
23
24   CGrid::~CGrid(void)
25   { 
26      this->axis.reset() ;
27      this->domain.reset() ;
28     
29      for (StdSize i = 0; i < this->storeIndex.size(); i++)
30      {
31         this->storeIndex[i].reset();
32         this->out_i_index[i].reset();
33         this->out_j_index[i].reset();
34         this->out_l_index[i].reset();
35      }
36   }
37
38   ///---------------------------------------------------------------
39
40   StdString CGrid::GetName(void)    { return (StdString("grid")); }
41   StdString CGrid::GetDefName(void) { return (CGrid::GetName()); }
42   ENodeType CGrid::GetType(void)    { return (eGrid); }
43
44   //----------------------------------------------------------------
45
46   const std::deque<ARRAY(int, 1)> & CGrid::getStoreIndex(void) const
47   { 
48      return (this->storeIndex );
49   }
50
51   //---------------------------------------------------------------
52
53   const std::deque<ARRAY(int, 1)> & CGrid::getOutIIndex(void)  const
54   { 
55      return (this->out_i_index ); 
56   }
57
58   //---------------------------------------------------------------
59
60   const std::deque<ARRAY(int, 1)> & CGrid::getOutJIndex(void)  const
61   { 
62      return (this->out_j_index ); 
63   }
64
65   //---------------------------------------------------------------
66
67   const std::deque<ARRAY(int, 1)> & CGrid::getOutLIndex(void)  const
68   { 
69      return (this->out_l_index ); 
70   }
71
72   //---------------------------------------------------------------
73
74   const boost::shared_ptr<CAxis>   CGrid::getRelAxis  (void) const
75   { 
76      return (this->axis ); 
77   }
78
79   //---------------------------------------------------------------
80
81   const boost::shared_ptr<CDomain> CGrid::getRelDomain(void) const
82   { 
83      return (this->domain ); 
84   }
85
86   //---------------------------------------------------------------
87
88   bool CGrid::hasAxis(void) const 
89   { 
90      return (this->withAxis); 
91   }
92
93   //---------------------------------------------------------------
94
95   StdSize CGrid::getDimension(void) const
96   {
97      return ((this->withAxis)?3:2);
98   }
99
100   //---------------------------------------------------------------
101
102   std::vector<StdSize> CGrid::getShape(void) const
103   {
104      std::vector<StdSize> retvalue;
105      retvalue.push_back(this->domain->ni.getValue());
106      retvalue.push_back(this->domain->nj.getValue());
107      if (this->withAxis)
108         retvalue.push_back(this->axis->size.getValue());
109      return (retvalue);
110   }
111   //---------------------------------------------------------------
112   
113   StdSize CGrid::getSize(void) const
114   {
115      StdSize retvalue = 1;
116      std::vector<StdSize> shape_ = this->getShape();
117      for (StdSize s = 0; s < shape_.size(); s++)
118         retvalue *= shape_[s];
119      return (retvalue);
120   }
121
122   //---------------------------------------------------------------
123
124   void CGrid::solveReference(void)
125   {
126      if (this->isChecked) return;
127      this->solveDomainRef() ;
128      this->solveAxisRef() ;
129      if (this->storeIndex.size() == 1)
130      {
131         this->computeIndex() ;
132      }
133      else
134      {
135         this->computeIndexServer();
136      }
137      this->isChecked = true;
138   }
139
140   //---------------------------------------------------------------
141
142   void CGrid::solveDomainRef(void)
143   {
144      if (!domain_ref.isEmpty())
145      {
146         if (CObjectFactory::HasObject<CDomain>(domain_ref.getValue()))
147         {
148            this->domain = CObjectFactory::GetObject<CDomain>(domain_ref.getValue()) ;
149            domain->checkAttributes() ;
150         }
151         else ERROR("CGrid::solveDomainRef(void)",
152                     << "Référence au domaine incorrecte") ;
153      }
154      else ERROR("CGrid::solveDomainRef(void)",
155                  << "Domaine non défini") ;
156   }
157
158   //---------------------------------------------------------------
159
160   void CGrid::solveAxisRef(void)
161   {
162      if (!axis_ref.isEmpty())
163      {
164         this->withAxis = true ;
165         if (CObjectFactory::GetObject<CAxis>(axis_ref.getValue()))
166         {
167            this->axis = CObjectFactory::GetObject<CAxis>(axis_ref.getValue()) ;
168            axis->checkAttributes() ;
169         }
170         else ERROR("CGrid::solveAxisRef(void)",
171                    << "Référence a l'axe incorrecte") ;
172      }
173      else withAxis = false ;
174   }
175
176   //---------------------------------------------------------------
177
178   void CGrid::computeIndex(void)
179   { 
180      const int ni   = domain->ni.getValue() ,
181                nj   = domain->nj.getValue() ,
182                size = (this->hasAxis()) ? axis->size.getValue() : 1 ;
183
184      /*std::cout << ni   << " : "
185                  << nj   << " : "
186                  << size << std::endl;*/
187
188      const int data_dim     = domain->data_dim.getValue() ,
189                data_n_index = domain->data_n_index.getValue() ,
190                data_ibegin  = domain->data_ibegin.getValue() ,
191                data_jbegin  = (data_dim == 2)
192                             ? domain->data_jbegin.getValue() : -1;
193
194      ARRAY(int, 1) data_i_index = domain->data_i_index.getValue() ,
195                    data_j_index = domain->data_j_index.getValue() ;
196
197      /*std::cout << data_n_index  << " : "
198                  << data_i_index  << " : "
199                  << data_j_index  << std::endl; */
200
201      ARRAY(bool, 2) mask = domain->mask.getValue() ;
202
203      int indexCount = 0;
204
205      for(int l = 0; l < size ; l++)
206      {
207         for(int n = 0, i = 0, j = 0; n < data_n_index; n++)
208         {
209            int temp_i = (*data_i_index)[n] + data_ibegin,
210                temp_j = (data_dim == 1) ? -1
211                       : (*data_j_index)[n] + data_jbegin;
212            i = (data_dim == 1) ? (temp_i - 2) % ni
213                                : (temp_i - 1) ;
214            j = (data_dim == 1) ? (temp_i - 2) / ni
215                                : (temp_j - 1) ;
216
217            if ((i >= 0 && i < ni) &&
218                (j >= 0 && j < nj) && (*mask)[i][j])
219               indexCount++ ;
220         }
221      }
222     
223      //std::cout << indexCount  << std::endl;
224      ARRAY_ASSIGN(this->storeIndex[0] , int, 1, [indexCount]);
225      ARRAY_ASSIGN(this->out_l_index[0], int, 1, [indexCount]);
226      ARRAY_ASSIGN(this->out_i_index[0], int, 1, [indexCount]);
227      ARRAY_ASSIGN(this->out_j_index[0], int, 1, [indexCount]);
228
229      for(int count = 0, indexCount = 0,  l = 0; l < size; l++)
230      {
231         for(int n = 0, i = 0, j = 0; n < data_n_index; n++, count++)
232         {
233            int temp_i = (*data_i_index)[n] + data_ibegin,
234                temp_j = (data_dim == 1) ? -1
235                       : (*data_j_index)[n] + data_jbegin;
236            i = (data_dim == 1) ? (temp_i - 2) % ni
237                                : (temp_i - 1) ;
238            j = (data_dim == 1) ? (temp_i - 2) / ni
239                                : (temp_j - 1) ;
240
241            if ((i >= 0 && i < ni) &&
242                (j >= 0 && j < nj) && (*mask)[i][j])
243            {
244               (*this->storeIndex[0]) [indexCount] = count ;
245               (*this->out_l_index[0])[indexCount] = l ;
246               (*this->out_i_index[0])[indexCount] = i ;
247               (*this->out_j_index[0])[indexCount] = j ;
248               indexCount++ ;
249            }
250         }
251      }
252   }
253
254   //----------------------------------------------------------------
255
256   boost::shared_ptr<CGrid>
257      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain)
258   {
259      StdString new_id = StdString("__") + domain->getId() + StdString("__") ;
260      boost::shared_ptr<CGrid> grid =
261         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
262      grid->domain_ref.setValue(domain->getId());
263      return (grid);
264   }
265
266   boost::shared_ptr<CGrid>
267      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain, boost::shared_ptr<CAxis> axis)
268   {
269      StdString new_id = StdString("__") + domain->getId() +
270                         StdString("_") + axis->getId() + StdString("__") ;
271      boost::shared_ptr<CGrid> grid =
272         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
273      grid->domain_ref.setValue(domain->getId());
274      grid->axis_ref.setValue(axis->getId());
275      return (grid);
276   }
277
278   //----------------------------------------------------------------
279
280   template <>
281      void CGrid::outputField
282         (const ARRAY(double, 1) stored,  ARRAY(double, 3) field) const
283   {
284      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
285         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]][(*out_l_index[0])[n]] = (*stored)[n] ;
286   }
287
288   //---------------------------------------------------------------
289
290   template <>
291      void CGrid::outputField
292         (const ARRAY(double, 1) stored,  ARRAY(double, 2) field) const
293   {
294      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
295         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]] = (*stored)[n] ;
296   }
297
298   //---------------------------------------------------------------
299
300   template <>
301      void CGrid::outputField
302         (const ARRAY(double, 1) stored,  ARRAY(double, 1) field) const
303   {
304      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
305         (*field)[(*out_i_index[0])[n]] = (*stored)[n] ;
306   }
307
308   //----------------------------------------------------------------
309
310   void CGrid::storeField_arr
311      (const double * const data, ARRAY(double, 1) stored) const
312   {
313      const StdSize size = storeIndex[0]->num_elements() ;
314      stored->resize(boost::extents[size]) ;
315      for(StdSize i = 0; i < size; i++)
316         (*stored)[i] = data[(*storeIndex[0])[i]] ;
317   }
318   
319   //---------------------------------------------------------------
320   
321   void CGrid::toBinary  (StdOStream & os) const
322   {
323      SuperClass::toBinary(os);
324     
325      os.write (reinterpret_cast<const char*>(&this->isChecked), sizeof(bool)); 
326     
327      if (this->isChecked)
328      {
329         this->storeIndex [0]->toBinary(os);
330         this->out_i_index[0]->toBinary(os);
331         this->out_j_index[0]->toBinary(os);
332         this->out_l_index[0]->toBinary(os);
333      }
334   }
335
336   //---------------------------------------------------------------
337   
338   void CGrid::fromBinary(StdIStream & is)
339   {
340      bool hasIndex = false;
341      SuperClass::fromBinary(is);           
342      is.read (reinterpret_cast<char*>(&hasIndex), sizeof(bool)); 
343     
344      if (hasIndex)
345      {
346         ARRAY_CREATE(storeIndex_ , int, 1, [0]);
347         ARRAY_CREATE(out_l_index_, int, 1, [0]);
348         ARRAY_CREATE(out_i_index_, int, 1, [0]);
349         ARRAY_CREATE(out_j_index_, int, 1, [0]);
350         
351         storeIndex_ ->fromBinary(is);
352         out_i_index_->fromBinary(is);
353         out_j_index_->fromBinary(is);
354         out_l_index_->fromBinary(is);
355         
356         this->storeIndex .push_back(storeIndex_);
357         this->out_i_index.push_back(out_i_index_);
358         this->out_j_index.push_back(out_j_index_);
359         this->out_l_index.push_back(out_l_index_);
360      }
361   }
362   
363   //---------------------------------------------------------------
364   
365   void CGrid::computeIndexServer(void)
366   {
367      ARRAY(int, 1) storeIndex_srv   =  this->storeIndex[0];
368      ARRAY(int, 1) out_i_index_srv  =  this->out_i_index[0];
369      ARRAY(int, 1) out_j_index_srv  =  this->out_j_index[0];
370      ARRAY(int, 1) out_l_index_srv  =  this->out_l_index[0];
371
372      ARRAY(int, 2) local_mask =  this->domain->getLocalMask();
373     
374      const std::vector<int> & ibegin = this->domain->getIBeginSub();
375      const std::vector<int> & jbegin = this->domain->getJBeginSub();
376     
377      const int ibegin_srv = this->domain->ibegin.getValue();
378      const int jbegin_srv = this->domain->jbegin.getValue();
379      const int zoom_ni_srv   = this->domain->zoom_ni_loc.getValue();
380      const int zoom_nj_srv   = this->domain->zoom_nj_loc.getValue();
381     
382      const int ibegin_zoom_srv = this->domain->zoom_ibegin_loc.getValue();
383      const int jbegin_zoom_srv = this->domain->zoom_jbegin_loc.getValue();
384           
385      StdSize dn = 0;     
386      for (StdSize j = 1; j < this->out_i_index.size(); j++)
387         dn += this->out_i_index[j]->size();
388         
389      ARRAY_ASSIGN(storeIndex_srv , int, 1, [dn]);
390      ARRAY_ASSIGN(out_i_index_srv, int, 1, [dn]);
391      ARRAY_ASSIGN(out_j_index_srv, int, 1, [dn]);
392      ARRAY_ASSIGN(out_l_index_srv, int, 1, [dn]);
393     
394      for (StdSize i = 0, dn = 0; i < ibegin.size(); i++)
395      {
396         ARRAY(int, 1) storeIndex_cl   =  this->storeIndex [i + 1];
397         ARRAY(int, 1) out_i_index_cl  =  this->out_i_index[i + 1];
398         ARRAY(int, 1) out_j_index_cl  =  this->out_j_index[i + 1];
399         ARRAY(int, 1) out_l_index_cl  =  this->out_l_index[i + 1];
400         
401         const int ibegin_cl = ibegin[i];
402         const int jbegin_cl = jbegin[i];
403         
404         for (StdSize n = dn, m = 0; n < (dn + storeIndex_cl->size()); n++, m++)
405         {
406            (*storeIndex_srv)[n]  = (*storeIndex_cl)[m]  + dn; // Faux mais inutile dans tous les cas.
407            (*out_i_index_srv)[n] = (*out_i_index_cl)[m] 
408                                  + (ibegin_cl - 1) - (ibegin_srv - 1) - (ibegin_zoom_srv - 1);
409            (*out_j_index_srv)[n] = (*out_j_index_cl)[m]
410                                  + (jbegin_cl - 1) - (jbegin_srv - 1) - (jbegin_zoom_srv - 1);
411            (*out_l_index_srv)[n] = (*out_l_index_cl)[m];
412         }
413                 
414         dn += storeIndex_cl->size();                 
415      }
416           
417      if (storeIndex_srv->size() != 0)
418      {
419         const int ibegin_t = 
420            *std::min_element(out_i_index_srv->begin(), out_i_index_srv->end());
421         const int iend_t   =
422            *std::max_element(out_i_index_srv->begin(), out_i_index_srv->end());
423         const int jbegin_t =   
424            *std::min_element(out_j_index_srv->begin(), out_j_index_srv->end());
425         const int jend_t   =
426            *std::max_element(out_j_index_srv->begin(), out_j_index_srv->end());
427           
428         if ((ibegin_t < 0) || (jbegin_t < 0) ||
429             (iend_t >= zoom_ni_srv) || (jend_t >= zoom_nj_srv))
430         {
431            ERROR("CGrid::computeIndexServer(void)",
432                  <<"Erreur d'indexation de la grille au niveau du serveur") ;
433         }
434      }
435
436      if (storeIndex_srv->size() != 0)
437         for (StdSize u = 0; u < storeIndex_srv->size(); u++)
438            (*local_mask)[(*out_i_index_srv)[u]][(*out_j_index_srv)[u]] = 1;
439
440      //~ StdOFStream ofs(this->getId().c_str());
441      //~ for (StdSize h = 0; h < storeIndex_srv->size(); h++)
442      //~ {
443        //~ ofs << "(" << (*storeIndex_srv)[h]  << ";"
444            //~ << (*out_i_index_srv)[h] << ","
445            //~ << (*out_j_index_srv)[h] << ","
446            //~ << (*out_l_index_srv)[h] << ")" << std::endl;
447      //~ }
448      //~ ofs.close();
449      this->storeIndex.resize(1);
450      this->out_i_index.resize(1);
451      this->out_j_index.resize(1);
452      this->out_l_index.resize(1);
453     
454   }
455   
456   //----------------------------------------------------------------
457   
458   void CGrid::inputFieldServer
459         (const std::deque<ARRAY(double, 1)> storedClient,
460                           ARRAY(double, 1)  storedServer) const
461   {
462      if ((this->storeIndex.size()-1 ) != storedClient.size())
463         ERROR("CGrid::inputFieldServer(...)",
464                << "[ Nombre de tableau attendu = " << (this->storeIndex.size()-1) << ", "
465                << "[ Nombre de tableau reçu = "    << storedClient.size() << "] "
466                << "Les données d'un client sont manquantes !") ;
467      if (storedServer.get() != NULL)
468         storedServer->resize(boost::extents[this->storeIndex.size()-1]);
469      else 
470         ARRAY_ASSIGN(storedServer, double, 1, [this->storeIndex.size()-1]);
471         
472      for (StdSize i = 0, n = 0; i < storedClient.size(); i++)
473         for (StdSize j = 0; j < storedClient[i]->num_elements(); j++)
474            (*storedServer)[n++] = (*storedClient[i])[j];
475   }
476
477   ///---------------------------------------------------------------
478
479} // namespace tree
480} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.