source: XMLIO_V2/dev/dev_rv/src/xmlio/node/grid.cpp @ 199

Last change on this file since 199 was 199, checked in by hozdoba, 13 years ago
File size: 15.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   void CGrid::solveReference(void)
96   {
97      if (this->isChecked) return;
98      this->solveDomainRef() ;
99      this->solveAxisRef() ;
100      if (this->storeIndex.size() == 1)
101      {
102         this->computeIndex() ;
103      }
104      else
105      {
106         this->computeIndexServer();
107      }
108      this->isChecked = true;
109   }
110
111   //---------------------------------------------------------------
112
113   void CGrid::solveDomainRef(void)
114   {
115      if (!domain_ref.isEmpty())
116      {
117         if (CObjectFactory::HasObject<CDomain>(domain_ref.getValue()))
118         {
119            this->domain = CObjectFactory::GetObject<CDomain>(domain_ref.getValue()) ;
120            domain->checkAttributes() ;
121         }
122         else ERROR("CGrid::solveDomainRef(void)",
123                     << "Référence au domaine incorrecte") ;
124      }
125      else ERROR("CGrid::solveDomainRef(void)",
126                  << "Domaine non défini") ;
127   }
128
129   //---------------------------------------------------------------
130
131   void CGrid::solveAxisRef(void)
132   {
133      if (!axis_ref.isEmpty())
134      {
135         this->withAxis = true ;
136         if (CObjectFactory::GetObject<CAxis>(axis_ref.getValue()))
137         {
138            this->axis = CObjectFactory::GetObject<CAxis>(axis_ref.getValue()) ;
139            axis->checkAttributes() ;
140         }
141         else ERROR("CGrid::solveAxisRef(void)",
142                    << "Référence a l'axe incorrecte") ;
143      }
144      else withAxis = false ;
145   }
146
147   //---------------------------------------------------------------
148
149   void CGrid::computeIndex(void)
150   { 
151      const int ni   = domain->ni.getValue() ,
152                nj   = domain->nj.getValue() ,
153                size = (this->hasAxis()) ? axis->size.getValue() : 1 ;
154
155      /*std::cout << ni   << " : "
156                  << nj   << " : "
157                  << size << std::endl;*/
158
159      const int data_dim     = domain->data_dim.getValue() ,
160                data_n_index = domain->data_n_index.getValue() ,
161                data_ibegin  = domain->data_ibegin.getValue() ,
162                data_jbegin  = (data_dim == 2)
163                             ? domain->data_jbegin.getValue() : -1;
164
165      ARRAY(int, 1) data_i_index = domain->data_i_index.getValue() ,
166                    data_j_index = domain->data_j_index.getValue() ;
167
168      /*std::cout << data_n_index  << " : "
169                  << data_i_index  << " : "
170                  << data_j_index  << std::endl; */
171
172      ARRAY(bool, 2) mask = domain->mask.getValue() ;
173
174      int indexCount = 0;
175
176      for(int l = 0; l < size ; l++)
177      {
178         for(int n = 0, i = 0, j = 0; n < data_n_index; n++)
179         {
180            int temp_i = (*data_i_index)[n] + data_ibegin,
181                temp_j = (data_dim == 1) ? -1
182                       : (*data_j_index)[n] + data_jbegin;
183            i = (data_dim == 1) ? (temp_i - 2) % ni
184                                : (temp_i - 1) ;
185            j = (data_dim == 1) ? (temp_i - 2) / ni
186                                : (temp_j - 1) ;
187
188            if ((i >= 0 && i < ni) &&
189                (j >= 0 && j < nj) && (*mask)[i][j])
190               indexCount++ ;
191         }
192      }
193     
194      //std::cout << indexCount  << std::endl;
195      ARRAY_ASSIGN(this->storeIndex[0] , int, 1, [indexCount]);
196      ARRAY_ASSIGN(this->out_l_index[0], int, 1, [indexCount]);
197      ARRAY_ASSIGN(this->out_i_index[0], int, 1, [indexCount]);
198      ARRAY_ASSIGN(this->out_j_index[0], int, 1, [indexCount]);
199
200      for(int count = 0, indexCount = 0,  l = 0; l < size; l++)
201      {
202         for(int n = 0, i = 0, j = 0; n < data_n_index; n++, count++)
203         {
204            int temp_i = (*data_i_index)[n] + data_ibegin,
205                temp_j = (data_dim == 1) ? -1
206                       : (*data_j_index)[n] + data_jbegin;
207            i = (data_dim == 1) ? (temp_i - 2) % ni
208                                : (temp_i - 1) ;
209            j = (data_dim == 1) ? (temp_i - 2) / ni
210                                : (temp_j - 1) ;
211
212            if ((i >= 0 && i < ni) &&
213                (j >= 0 && j < nj) && (*mask)[i][j])
214            {
215               (*this->storeIndex[0]) [indexCount] = count ;
216               (*this->out_l_index[0])[indexCount] = l ;
217               (*this->out_i_index[0])[indexCount] = i ;
218               (*this->out_j_index[0])[indexCount] = j ;
219               indexCount++ ;
220            }
221         }
222      }
223   }
224
225   //----------------------------------------------------------------
226
227   boost::shared_ptr<CGrid>
228      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain)
229   {
230      StdString new_id = StdString("__") + domain->getId() + StdString("__") ;
231      boost::shared_ptr<CGrid> grid =
232         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
233      grid->domain_ref.setValue(domain->getId());
234      return (grid);
235   }
236
237   boost::shared_ptr<CGrid>
238      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain, boost::shared_ptr<CAxis> axis)
239   {
240      StdString new_id = StdString("__") + domain->getId() +
241                         StdString("_") + axis->getId() + StdString("__") ;
242      boost::shared_ptr<CGrid> grid =
243         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
244      grid->domain_ref.setValue(domain->getId());
245      grid->axis_ref.setValue(axis->getId());
246      return (grid);
247   }
248
249   //----------------------------------------------------------------
250
251   template <>
252      void CGrid::outputField
253         (const ARRAY(double, 1) stored,  ARRAY(double, 3) field) const
254   {
255      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
256         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]][(*out_l_index[0])[n]] = (*stored)[n] ;
257   }
258
259   //---------------------------------------------------------------
260
261   template <>
262      void CGrid::outputField
263         (const ARRAY(double, 1) stored,  ARRAY(double, 2) field) const
264   {
265      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
266         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]] = (*stored)[n] ;
267   }
268
269   //---------------------------------------------------------------
270
271   template <>
272      void CGrid::outputField
273         (const ARRAY(double, 1) stored,  ARRAY(double, 1) field) const
274   {
275      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
276         (*field)[(*out_i_index[0])[n]] = (*stored)[n] ;
277   }
278
279   //----------------------------------------------------------------
280
281   void CGrid::storeField_arr
282      (const double * const data, ARRAY(double, 1) stored) const
283   {
284      const StdSize size = storeIndex[0]->num_elements() ;
285      stored->resize(boost::extents[size]) ;
286      for(StdSize i = 0; i < size; i++)
287         (*stored)[i] = data[(*storeIndex[0])[i]] ;
288   }
289   
290   //---------------------------------------------------------------
291   
292   void CGrid::toBinary  (StdOStream & os) const
293   {
294      SuperClass::toBinary(os);
295     
296      os.write (reinterpret_cast<const char*>(&this->isChecked), sizeof(bool)); 
297     
298      if (this->isChecked)
299      {
300         this->storeIndex [0]->toBinary(os);
301         this->out_i_index[0]->toBinary(os);
302         this->out_j_index[0]->toBinary(os);
303         this->out_l_index[0]->toBinary(os);
304      }
305   }
306
307   //---------------------------------------------------------------
308   
309   void CGrid::fromBinary(StdIStream & is)
310   {
311      bool hasIndex = false;
312      SuperClass::fromBinary(is);           
313      is.read (reinterpret_cast<char*>(&hasIndex), sizeof(bool)); 
314     
315      if (hasIndex)
316      {
317         ARRAY_CREATE(storeIndex_ , int, 1, [0]);
318         ARRAY_CREATE(out_l_index_, int, 1, [0]);
319         ARRAY_CREATE(out_i_index_, int, 1, [0]);
320         ARRAY_CREATE(out_j_index_, int, 1, [0]);
321         
322         storeIndex_ ->fromBinary(is);
323         out_i_index_->fromBinary(is);
324         out_j_index_->fromBinary(is);
325         out_l_index_->fromBinary(is);
326         
327         this->storeIndex .push_back(storeIndex_);
328         this->out_i_index.push_back(out_i_index_);
329         this->out_j_index.push_back(out_j_index_);
330         this->out_l_index.push_back(out_l_index_);
331      }
332   }
333   
334   //---------------------------------------------------------------
335   
336   void CGrid::computeIndexServer(void)
337   {
338      ARRAY(int, 1) storeIndex_srv   =  this->storeIndex[0];
339      ARRAY(int, 1) out_i_index_srv  =  this->out_i_index[0];
340      ARRAY(int, 1) out_j_index_srv  =  this->out_j_index[0];
341      ARRAY(int, 1) out_l_index_srv  =  this->out_l_index[0];     
342     
343      const std::vector<int> & ibegin = this->domain->getIBeginSub();
344      const std::vector<int> & jbegin = this->domain->getJBeginSub();
345     
346      const int ibegin_srv = this->domain->ibegin.getValue();
347      const int jbegin_srv = this->domain->jbegin.getValue();
348      const int zoom_ni_srv   = this->domain->zoom_ni.getValue();
349      const int zoom_nj_srv   = this->domain->zoom_nj.getValue();
350     
351      const int ibegin_zoom_srv = this->domain->zoom_ibegin_loc.getValue();
352      const int jbegin_zoom_srv = this->domain->zoom_jbegin_loc.getValue();
353           
354      StdSize dn = 0;     
355      for (StdSize j = 1; j < this->out_i_index.size(); j++)
356         dn += this->out_i_index[j]->size();
357         
358      ARRAY_ASSIGN(storeIndex_srv , int, 1, [dn]);
359      ARRAY_ASSIGN(out_i_index_srv, int, 1, [dn]);
360      ARRAY_ASSIGN(out_j_index_srv, int, 1, [dn]);
361      ARRAY_ASSIGN(out_l_index_srv, int, 1, [dn]);
362     
363      for (StdSize i = 0, dn = 0; i < ibegin.size(); i++)
364      {
365         ARRAY(int, 1) storeIndex_cl   =  this->storeIndex [i + 1];
366         ARRAY(int, 1) out_i_index_cl  =  this->out_i_index[i + 1];
367         ARRAY(int, 1) out_j_index_cl  =  this->out_j_index[i + 1];
368         ARRAY(int, 1) out_l_index_cl  =  this->out_l_index[i + 1];
369         
370         const int ibegin_cl = ibegin[i];
371         const int jbegin_cl = jbegin[i];
372         
373         for (StdSize n = dn, m = 0; n < (dn + storeIndex_cl->size()); n++, m++)
374         {
375            (*storeIndex_srv)[n]  = (*storeIndex_cl)[m]  + dn; // Faux mais inutile dans tous les cas.
376            (*out_i_index_srv)[n] = (*out_i_index_cl)[m] 
377                                  + (ibegin_cl - 1) - (ibegin_srv - 1) - (ibegin_zoom_srv - 1);
378            (*out_j_index_srv)[n] = (*out_j_index_cl)[m]
379                                  + (jbegin_cl - 1) - (jbegin_srv - 1) - (jbegin_zoom_srv - 1);
380            (*out_l_index_srv)[n] = (*out_l_index_cl)[m];
381         }
382                 
383         dn += storeIndex_cl->size();                 
384      }
385           
386      if (storeIndex_srv->size() != 0)
387      {
388         const int ibegin_t = 
389            *std::min_element(out_i_index_srv->begin(), out_i_index_srv->end());
390         const int iend_t   =
391            *std::max_element(out_i_index_srv->begin(), out_i_index_srv->end());
392         const int jbegin_t =   
393            *std::min_element(out_j_index_srv->begin(), out_j_index_srv->end());
394         const int jend_t   =
395            *std::max_element(out_j_index_srv->begin(), out_j_index_srv->end());
396           
397         if (ibegin_t < 0) 
398         {
399            std::cout << "Erreur 1 " << std::endl;
400         }
401         if (jbegin_t < 0) 
402         {
403            std::cout << "Erreur 2 " <<  std::endl;
404         }
405         if (iend_t >= zoom_ni_srv)
406         {
407            std::cout << "Erreur 3 " << std::endl;
408         }
409         if (jend_t >= zoom_nj_srv) 
410         {
411            std::cout << "Erreur 4 " << std::endl;
412         }
413      }
414     
415      //~ StdOFStream ofs(this->getId().c_str());
416      //~ for (StdSize h = 0; h < storeIndex_srv->size(); h++)
417      //~ {
418        //~ ofs << "(" << (*storeIndex_srv)[h]  << ";"
419            //~ << (*out_i_index_srv)[h] << ","
420            //~ << (*out_j_index_srv)[h] << ","
421            //~ << (*out_l_index_srv)[h] << ")" << std::endl;
422      //~ }
423      //~ ofs.close();
424      this->storeIndex.resize(1);
425      this->out_i_index.resize(1);
426      this->out_j_index.resize(1);
427      this->out_l_index.resize(1);
428     
429   }
430   
431   //----------------------------------------------------------------
432   
433   void CGrid::inputFieldServer
434         (const std::deque<ARRAY(double, 1)> storedClient,
435                           ARRAY(double, 1)  storedServer) const
436   {
437      if ((this->storeIndex.size()-1 ) != storedClient.size())
438         ERROR("CGrid::inputFieldServer(...)",
439                << "[ Nombre de tableau attendu = " << (this->storeIndex.size()-1) << ", "
440                << "[ Nombre de tableau reçu = "    << storedClient.size() << "] "
441                << "Les données d'un client sont manquantes !") ;
442      if (storedServer.get() != NULL)
443         storedServer->resize(boost::extents[this->storeIndex.size()-1]);
444      else 
445         ARRAY_ASSIGN(storedServer, double, 1, [this->storeIndex.size()-1]);
446         
447      for (StdSize i = 0, n = 0; i < storedClient.size(); i++)
448         for (StdSize j = 0; j < storedClient[i]->num_elements(); j++)
449            (*storedServer)[n++] = (*storedClient[i])[j];
450   }
451
452   ///---------------------------------------------------------------
453
454} // namespace tree
455} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.