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

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

suite du précédent commit

File size: 6.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)
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, const CAxis* const a_axis)
81         {
82            string new_id = string("___") + a_domain->getId() + string("_") + a_axis->getId() + string("__") ;
83            CGrid* const grid = ObjectTemplate<CGrid>::CreateObject(new_id) ;
84
85            grid->domain_ref = a_domain->getId() ;
86            grid->axis_ref   = a_axis  ->getId() ;
87
88            return (grid);
89         }
90
91         static CGrid* CreateObject(const CDomain* const a_domain)
92         {
93            string new_id = string("___") + a_domain->getId() + string("__") ;
94            CGrid* const grid = ObjectTemplate<CGrid>::CreateObject(new_id) ;
95            grid->domain_ref = a_domain->getId() ;
96
97            return (grid);
98         }
99
100      protected :
101
102         void storeField_arr(const double* const data, Array<double, 1>& stored) const
103         {
104            const int size = storeIndex.size() ;
105            stored.resize(shape(size)) ;
106            for(int i = 0; i < size; i++)
107               stored(i) = data[storeIndex(i)] ;
108         }
109
110      private:
111
112         bool hasAxis ;
113
114         CAxis   * axis ;
115         CDomain * domain ;
116
117         Array<int, 1> storeIndex ;
118         Array<int, 1> out_i_index ;
119         Array<int, 1> out_j_index ;
120         Array<int, 1> out_l_index ;
121
122   }; // class CGrid
123
124   void CGrid::computeIndex(void)
125   {
126      int ni = domain->ni ;
127      int nj = domain->nj ;
128      int size = (hasAxis) ? (int)axis->size : 1 ;
129      int data_dim = domain->data_dim ;
130      int data_n_index = domain->data_n_index ;
131      int data_ibegin  = domain->data_ibegin ;
132      int data_jbegin  = (data_dim == 2) ? (int)domain->data_jbegin : -1;
133
134      Array<int, 1>& data_i_index =* domain->data_i_index ;
135      Array<int, 1>& data_j_index =* domain->data_j_index ;
136      Array<bool, 2>& mask =* domain->mask ;
137      int i, j, l, n ;
138      int count, indexCount ;
139
140      for(indexCount=0, l=0; l<size ; l++)
141      {
142         for(n=0; n<data_n_index; n++)
143         {
144            if (data_dim == 1)
145            {
146               i = (data_i_index(n) + data_ibegin) % ni ;
147               j = (data_i_index(n) + data_ibegin) / ni ;
148               //cout<<i<<" "<<j<<" "<<mask(i,j)<<endl ;
149            }
150            else
151            {
152               i = data_i_index(n) + data_ibegin ;
153               j = data_j_index(n) + data_jbegin ;
154               //cout<<i<<" "<<j<<" "<<mask(i,j)<<endl ;
155            }
156
157            if (i>=0 && i<ni && j>=0 && j<nj && mask(i,j) ) indexCount++ ;
158         }
159      }
160
161      storeIndex.resize(indexCount) ;
162      out_l_index.resize(indexCount) ;
163      out_i_index.resize(indexCount) ;
164      out_j_index.resize(indexCount) ;
165
166      for(count=0, indexCount=0, l=0; l<size; l++)
167      {
168         for(n=0; n<data_n_index; n++, count++)
169         {
170            if (data_dim == 1)
171            {
172               i = (data_i_index(n) + data_ibegin) % ni ;
173               j = (data_i_index(n) + data_ibegin) / ni ;
174            }
175            else // (dat_dim == 2)
176            {
177               i = data_i_index(n) + data_ibegin ;
178               j = data_j_index(n) + data_jbegin ;
179            }
180
181            if (i>=0 && i<ni && j>=0 && j<nj && mask(i,j))
182            {
183               storeIndex(indexCount) = count ;
184               out_l_index(indexCount) = l ;
185               out_i_index(indexCount) = i ;
186               out_j_index(indexCount) = j ;
187               indexCount++ ;
188            }
189         }
190      }
191   }
192
193   template<>
194      void CGrid::outputField(const Array<double, 1>& stored, Array<double, 2>& outField) const
195   {
196      for(int n = 0; n < storeIndex.size(); n++)
197         outField(out_i_index(n), out_j_index(n)) = stored(n) ;
198   }
199
200   template<>
201      void CGrid::outputField(const Array<double, 1>& stored, Array<double, 3>& outField) const
202   {
203      for(int n = 0; n < storeIndex.size(); n++)
204         outField(out_i_index(n), out_j_index(n), out_l_index(n)) = stored(n) ;
205   }
206} // namespace XMLIOSERVER
207
208
209DECLARE_GROUP(Grid)
210
211
212
213#endif // __GRID__
214
Note: See TracBrowser for help on using the repository browser.