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

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

Mise à jour

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