source: XMLIO_V2/dev/dev_rv/src/XMLIO/grid.hpp @ 124

Last change on this file since 124 was 124, checked in by hozdoba, 14 years ago

Commit pour sauvegarde - diverses corrections de bogues et améliorations du code.

File size: 7.5 KB
Line 
1#ifndef __GRID__
2#define __GRID__
3
4using XMLIOSERVER::XML::XMLNode;
5using XMLIOSERVER::XML::THashAttributes;
6
7namespace XMLIOSERVER
8{
9   class CGrid : public ObjectTemplate<CGrid>, public GridAttribut
10   {
11      public:
12
13         CGrid(void) : ObjectTemplate<CGrid>(), GridAttribut(), hasAxis(false), axis(NULL), domain(NULL)
14         { /* Ne rien faire de plus */ }
15
16         CGrid(const string& _id) : ObjectTemplate<CGrid>(_id), GridAttribut(), hasAxis(false), axis(NULL), domain(NULL)
17         { /* Ne rien faire de plus */ }
18
19         static string GetName(void) { return ("grid"); }
20
21         const CAxis* getRelAxis(void) const { return (axis); }
22         const CDomain* getRelDomain(void) const { return (domain); }
23
24         inline void solveReference(void) ;
25         inline void solveDomainRef(void) ;
26         inline void solveAxisRef(void) ;
27
28         inline void computeIndex(void);
29
30         bool _hasAxis(void) const { return (hasAxis); }
31
32         inline void storeField(const Array<double, 1>& field, Array<double, 1>& stored) const;
33         inline void storeField(const Array<double, 2>& field, Array<double, 1>& stored) const;
34         inline void storeField(const Array<double, 3>& field, Array<double, 1>& stored) const;
35         inline void storeField(const double* const data, Array<double, 1>& stored) const;
36
37         inline void outputField(const Array<double,1>& stored, Array<double, 2>& outField) const;
38         inline void outputField(const Array<double,1>& stored, Array<double, 3>& outField) const;
39
40         inline static CGrid* CreateObject(const CDomain* const a_domain, const CAxis* const a_axis);
41         inline static CGrid* CreateObject(const CDomain* const a_domain);
42
43         virtual ~CGrid(void)
44         { /* Ne rien faire de plus */ }
45
46      private:
47
48         bool hasAxis ;
49
50         CAxis* axis ;
51         CDomain* domain ;
52
53         Array<int, 1> storeIndex ;
54         Array<int, 1> out_i_index ;
55         Array<int, 1> out_j_index ;
56         Array<int, 1> out_l_index ;
57
58   }; // class CGrid
59
60   CGrid* CGrid::CreateObject(const CDomain* const a_domain, const CAxis* const a_axis)
61   {
62      string new_id = string("___") + a_domain->getId() + string("_") + a_axis->getId() + string("__") ;
63      CGrid* const grid = ObjectTemplate<CGrid>::CreateObject(new_id) ;
64
65      grid->domain_ref = a_domain->getId() ;
66      grid->axis_ref   = a_axis  ->getId() ;
67
68      return (grid);
69   }
70
71   CGrid* CGrid::CreateObject(const CDomain* const a_domain)
72   {
73      string new_id = string("___") + a_domain->getId() + string("__") ;
74      CGrid* const grid = ObjectTemplate<CGrid>::CreateObject(new_id) ;
75
76      grid->domain_ref = a_domain->getId() ;
77
78      return (grid);
79   }
80
81   void CGrid::solveReference(void)
82   {
83
84      static bool isReferenceSolved = false;
85      if (isReferenceSolved) return;
86
87      // Résolution de chacune des références et indexation.
88      solveDomainRef() ;
89      solveAxisRef() ;
90      computeIndex() ;
91
92      isReferenceSolved = true ;
93   }
94
95   void CGrid::solveDomainRef(void)
96   {
97      if (domain_ref.hasValue())
98      {
99         if (CDomain::HasObject(domain_ref))
100         {
101           domain = CDomain::GetObject(domain_ref) ;
102           domain->check() ;
103         }
104         else ERROR("Référence au domaine incorrecte") ;
105      }
106      else ERROR("Domaine non défini") ;
107   }
108
109   void CGrid::solveAxisRef(void)
110   {
111      if (axis_ref.hasValue())
112      {
113         hasAxis = true ;
114         if (CAxis::HasObject(axis_ref)) axis = CAxis::GetObject(axis_ref) ;
115         else ERROR("Référence a l'axe incorrecte") ;
116      }
117      else hasAxis = false ; // hasAxis est normalement déjà à false(?).
118   }
119
120   void CGrid::computeIndex(void)
121   {
122
123      int ni = domain->ni ;
124      int nj = domain->nj ;
125      int size = (hasAxis) ? (int)axis->size : 1 ;
126      int data_dim = domain->data_dim ;
127      int data_n_index = domain->data_n_index ;
128      int data_ibegin  = domain->data_ibegin ;
129      int data_jbegin  = (data_dim == 2) ? (int)domain->data_jbegin : -1;
130
131      Array<int, 1>& data_i_index =* domain->data_i_index ;
132      Array<int, 1>& data_j_index =* domain->data_j_index ;
133      Array<bool, 2>& mask =* domain->mask ;
134      int i, j, l, n ;
135      int count, indexCount ;
136
137      for(indexCount=0, l=0; l<size ; l++)
138      {
139         for(n=0; n<data_n_index; n++)
140         {
141            if (data_dim == 1)
142            {
143               i = (data_i_index(n) + data_ibegin) % ni ;
144               j = (data_i_index(n) + data_ibegin) / ni ;
145               //cout<<i<<" "<<j<<" "<<mask(i,j)<<endl ;
146            }
147            else
148            {
149               i = data_i_index(n) + data_ibegin ;
150               j = data_j_index(n) + data_jbegin ;
151               //cout<<i<<" "<<j<<" "<<mask(i,j)<<endl ;
152            }
153
154            if (i>=0 && i<ni && j>=0 && j<nj && mask(i,j) ) indexCount++ ;
155         }
156      }
157
158      storeIndex.resize(indexCount) ;
159      out_l_index.resize(indexCount) ;
160      out_i_index.resize(indexCount) ;
161      out_j_index.resize(indexCount) ;
162
163      for(count=0, indexCount=0, l=0; l<size; l++)
164      {
165         for(n=0; n<data_n_index; n++, count++)
166         {
167            if (data_dim == 1)
168            {
169               i = (data_i_index(n) + data_ibegin) % ni ;
170               j = (data_i_index(n) + data_ibegin) / ni ;
171            }
172            else // (dat_dim == 2)
173            {
174               i = data_i_index(n) + data_ibegin ;
175               j = data_j_index(n) + data_jbegin ;
176            }
177
178            if (i>=0 && i<ni && j>=0 && j<nj && mask(i,j))
179            {
180               storeIndex(indexCount) = count ;
181               out_l_index(indexCount) = l ;
182               out_i_index(indexCount) = i ;
183               out_j_index(indexCount) = j ;
184               indexCount++ ;
185            }
186         }
187      }
188
189      /** Pour tests
190
191      cout << "Out of CGrid::ComputeIndex" << endl ;
192
193      cout << "storeIndex : " << endl ;
194      cout << storeIndex << endl ;
195
196      cout << "out_i_index : " << endl ;
197      cout << out_i_index << endl ;
198
199      cout << "out_j_index : " << endl ;
200      cout << out_j_index << endl ;
201
202      cout << "out_l_index : " << endl ;
203      cout << out_l_index << endl ;
204
205      **/
206
207   }
208
209   void CGrid::storeField(const Array<double, 1>& field, Array<double, 1>& stored) const
210   {
211      storeField(field.dataFirst(), stored) ;
212      //cout<<"Stored 1"<<stored<<endl ;
213   }
214
215   void CGrid::storeField(const Array<double, 2>& field, Array<double, 1>& stored) const
216   {
217      storeField(field.dataFirst(), stored) ;
218      //cout<<"Stored 2"<<stored<<endl ;
219   }
220
221   void CGrid::storeField(const Array<double, 3>& field, Array<double, 1>& stored) const
222   {
223      storeField(field.dataFirst(), stored) ;
224      //cout<<"Stored 3"<<stored<<endl ;
225   }
226
227   void CGrid::storeField(const double* const data, Array<double, 1>& stored) const
228   {
229      int size = storeIndex.size() ;
230      //cout << "size " << size << endl ;
231
232      stored.resize(shape(size)) ;
233      //cout << "Stored " << stored << endl ;
234
235      for(int i = 0; i < size; i++) stored(i) = data[storeIndex(i)] ;
236      //cout << "Stored " << stored << endl ;
237   }
238
239   void CGrid::outputField(const Array<double, 1>& stored, Array<double, 2>& outField) const
240   {
241      for(int n = 0; n < storeIndex.size(); n++)
242         outField(out_i_index(n), out_j_index(n)) = stored(n) ;
243   }
244
245   void CGrid::outputField(const Array<double, 1>& stored, Array<double, 3>& outField) const
246   {
247      for(int n = 0; n < storeIndex.size(); n++)
248         outField(out_i_index(n), out_j_index(n), out_l_index(n)) = stored(n) ;
249   }
250} // namespace XMLIOSERVER
251
252
253DECLARE_GROUP(Grid)
254
255
256
257#endif // __GRID__
258
Note: See TracBrowser for help on using the repository browser.