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

Last change on this file since 181 was 181, checked in by hozdoba, 14 years ago
File size: 10.0 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   { /* Ne rien faire de plus */ }
26
27   ///---------------------------------------------------------------
28
29   StdString CGrid::GetName(void)    { return (StdString("grid")); }
30   StdString CGrid::GetDefName(void) { return (CGrid::GetName()); }
31   ENodeType CGrid::GetType(void)    { return (eGrid); }
32
33   //----------------------------------------------------------------
34
35   const std::deque<ARRAY(int, 1)> & CGrid::getStoreIndex(void) const
36   { 
37      return (this->storeIndex );
38   }
39
40   //---------------------------------------------------------------
41
42   const std::deque<ARRAY(int, 1)> & CGrid::getOutIIndex(void)  const
43   { 
44      return (this->out_i_index ); 
45   }
46
47   //---------------------------------------------------------------
48
49   const std::deque<ARRAY(int, 1)> & CGrid::getOutJIndex(void)  const
50   { 
51      return (this->out_j_index ); 
52   }
53
54   //---------------------------------------------------------------
55
56   const std::deque<ARRAY(int, 1)> & CGrid::getOutLIndex(void)  const
57   { 
58      return (this->out_l_index ); 
59   }
60
61   //---------------------------------------------------------------
62
63   const boost::shared_ptr<CAxis>   CGrid::getRelAxis  (void) const
64   { 
65      return (this->axis ); 
66   }
67
68   //---------------------------------------------------------------
69
70   const boost::shared_ptr<CDomain> CGrid::getRelDomain(void) const
71   { 
72      return (this->domain ); 
73   }
74
75   //---------------------------------------------------------------
76
77   bool CGrid::hasAxis(void) const 
78   { 
79      return (this->withAxis); 
80   }
81
82   //---------------------------------------------------------------
83
84   void CGrid::solveReference(void)
85   {
86      if (this->isChecked) return;
87      this->solveDomainRef() ;
88      this->solveAxisRef() ;
89      if (this->storeIndex.size() == 1)
90         this->computeIndex() ;
91      this->isChecked = true;
92   }
93
94   //---------------------------------------------------------------
95
96   void CGrid::solveDomainRef(void)
97   {
98      if (!domain_ref.isEmpty())
99      {
100         if (CObjectFactory::HasObject<CDomain>(domain_ref.getValue()))
101         {
102            this->domain = CObjectFactory::GetObject<CDomain>(domain_ref.getValue()) ;
103            domain->checkAttributes() ;
104         }
105         else ERROR("CGrid::solveDomainRef(void)",
106                     << "Référence au domaine incorrecte") ;
107      }
108      else ERROR("CGrid::solveDomainRef(void)",
109                  << "Domaine non défini") ;
110   }
111
112   //---------------------------------------------------------------
113
114   void CGrid::solveAxisRef(void)
115   {
116      if (!axis_ref.isEmpty())
117      {
118         this->withAxis = true ;
119         if (CObjectFactory::GetObject<CAxis>(axis_ref.getValue()))
120         {
121            this->axis = CObjectFactory::GetObject<CAxis>(axis_ref.getValue()) ;
122            axis->checkAttributes() ;
123         }
124         else ERROR("CGrid::solveAxisRef(void)",
125                    << "Référence a l'axe incorrecte") ;
126      }
127      else withAxis = false ;
128   }
129
130   //---------------------------------------------------------------
131
132   void CGrid::computeIndex(void)
133   {   
134      const int ni   = domain->ni.getValue() ,
135                nj   = domain->nj.getValue() ,
136                size = (this->hasAxis()) ? axis->size.getValue() : 1 ;
137
138      /*std::cout << ni   << " : "
139                  << nj   << " : "
140                  << size << std::endl;*/
141
142      const int data_dim     = domain->data_dim.getValue() ,
143                data_n_index = domain->data_n_index.getValue() ,
144                data_ibegin  = domain->data_ibegin.getValue() ,
145                data_jbegin  = (data_dim == 2)
146                             ? domain->data_jbegin.getValue() : -1;
147
148      ARRAY(int, 1) data_i_index = domain->data_i_index.getValue() ,
149                    data_j_index = domain->data_j_index.getValue() ;
150
151      /*std::cout << data_n_index  << " : "
152                  << data_i_index  << " : "
153                  << data_j_index  << std::endl; */
154
155      ARRAY(bool, 2) mask = domain->mask.getValue() ;
156
157      int indexCount = 0;
158
159      for(int l = 0; l < size ; l++)
160      {
161         for(int n = 0, i = 0, j = 0; n < data_n_index; n++)
162         {
163            int temp_i = (*data_i_index)[n] + data_ibegin,
164                temp_j = (data_dim == 1) ? -1
165                       : (*data_j_index)[n] + data_jbegin;
166            i = (data_dim == 1) ? (temp_i - 2) % ni
167                                : (temp_i - 1) ;
168            j = (data_dim == 1) ? (temp_i - 2) / ni
169                                : (temp_j - 1) ;
170
171            if ((i >= 0 && i < ni) &&
172                (j >= 0 && j < nj) && (*mask)[i][j])
173               indexCount++ ;
174         }
175      }
176     
177      //std::cout << indexCount  << std::endl;
178      ARRAY_ASSIGN(this->storeIndex[0] , int, 1, [indexCount]);
179      ARRAY_ASSIGN(this->out_l_index[0], int, 1, [indexCount]);
180      ARRAY_ASSIGN(this->out_i_index[0], int, 1, [indexCount]);
181      ARRAY_ASSIGN(this->out_j_index[0], int, 1, [indexCount]);
182
183      for(int count = 0, indexCount = 0,  l = 0; l < size; l++)
184      {
185         for(int n = 0, i = 0, j = 0; n < data_n_index; n++, count++)
186         {
187            int temp_i = (*data_i_index)[n] + data_ibegin,
188                temp_j = (data_dim == 1) ? -1
189                       : (*data_j_index)[n] + data_jbegin;
190            i = (data_dim == 1) ? (temp_i - 2) % ni
191                                : (temp_i - 1) ;
192            j = (data_dim == 1) ? (temp_i - 2) / ni
193                                : (temp_j - 1) ;
194
195            if ((i >= 0 && i < ni) &&
196                (j >= 0 && j < nj) && (*mask)[i][j])
197            {
198               (*this->storeIndex[0]) [indexCount] = count ;
199               (*this->out_l_index[0])[indexCount] = l ;
200               (*this->out_i_index[0])[indexCount] = i ;
201               (*this->out_j_index[0])[indexCount] = j ;
202               indexCount++ ;
203            }
204         }
205      }
206   }
207
208   //----------------------------------------------------------------
209
210   boost::shared_ptr<CGrid>
211      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain)
212   {
213      StdString new_id = StdString("__") + domain->getId() + StdString("__") ;
214      boost::shared_ptr<CGrid> grid =
215         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
216      grid->domain_ref.setValue(domain->getId());
217      return (grid);
218   }
219
220   boost::shared_ptr<CGrid>
221      CGrid::CreateGrid(boost::shared_ptr<CDomain> domain, boost::shared_ptr<CAxis> axis)
222   {
223      StdString new_id = StdString("__") + domain->getId() +
224                         StdString("_") + axis->getId() + StdString("__") ;
225      boost::shared_ptr<CGrid> grid =
226         CGroupFactory::CreateChild(CObjectFactory::GetObject<CGridGroup> ("grid_definition"), new_id);
227      grid->domain_ref.setValue(domain->getId());
228      grid->axis_ref.setValue(axis->getId());
229      return (grid);
230   }
231
232   //----------------------------------------------------------------
233
234   template <>
235      void CGrid::outputField
236         (const ARRAY(double, 1) stored,  ARRAY(double, 3) field) const
237   {
238      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
239         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]][(*out_l_index[0])[n]] = (*stored)[n] ;
240   }
241
242   //---------------------------------------------------------------
243
244   template <>
245      void CGrid::outputField
246         (const ARRAY(double, 1) stored,  ARRAY(double, 2) field) const
247   {
248      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
249         (*field)[(*out_i_index[0])[n]][(*out_j_index[0])[n]] = (*stored)[n] ;
250   }
251
252   //---------------------------------------------------------------
253
254   template <>
255      void CGrid::outputField
256         (const ARRAY(double, 1) stored,  ARRAY(double, 1) field) const
257   {
258      for(StdSize n = 0; n < storeIndex[0]->num_elements(); n++)
259         (*field)[(*out_i_index[0])[n]] = (*stored)[n] ;
260   }
261
262   //----------------------------------------------------------------
263
264   void CGrid::storeField_arr
265      (const double * const data, ARRAY(double, 1) stored) const
266   {
267      const StdSize size = storeIndex[0]->num_elements() ;
268      stored->resize(boost::extents[size]) ;
269      for(StdSize i = 0; i < size; i++)
270         (*stored)[i] = data[(*storeIndex[0])[i]] ;
271   }
272   
273   //---------------------------------------------------------------
274   
275   void CGrid::toBinary  (StdOStream & os) const
276   {
277      SuperClass::toBinary(os);
278      this->storeIndex[0]->toBinary(os);
279      this->out_i_index[0]->toBinary(os);
280      this->out_j_index[0]->toBinary(os);
281      this->out_l_index[0]->toBinary(os);
282   }
283
284   //---------------------------------------------------------------
285   
286   void CGrid::fromBinary(StdIStream & is)
287   {
288      SuperClass::fromBinary(is);
289     
290      ARRAY_CREATE(storeIndex_ , int, 1, [1]);
291      ARRAY_CREATE(out_l_index_, int, 1, [1]);
292      ARRAY_CREATE(out_i_index_, int, 1, [1]);
293      ARRAY_CREATE(out_j_index_, int, 1, [1]);
294     
295      storeIndex_ ->fromBinary(is);
296      out_i_index_->fromBinary(is);
297      out_j_index_->fromBinary(is);
298      out_l_index_->fromBinary(is);
299     
300      this->storeIndex .push_back(storeIndex_);
301      this->out_i_index.push_back(out_i_index_);
302      this->out_j_index.push_back(out_j_index_);
303      this->out_l_index.push_back(out_l_index_);
304   }
305
306   ///---------------------------------------------------------------
307
308} // namespace tree
309} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.