source: XIOS/dev/dev_ym/XIOS_COUPLING/src/node/grid.hpp @ 1871

Last change on this file since 1871 was 1871, checked in by ymipsl, 4 years ago

Some update on XIOS_COUPLING branch...

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:executable set to *
File size: 29.3 KB
Line 
1#ifndef __XIOS_CGrid__
2#define __XIOS_CGrid__
3
4/// XIOS headers ///
5#include "xios_spl.hpp"
6#include "group_factory.hpp"
7
8#include "declare_group.hpp"
9#include "domain.hpp"
10#include "axis.hpp"
11#include "scalar.hpp"
12#include "array_new.hpp"
13#include "attribute_array.hpp"
14#include "distribution_server.hpp"
15#include "client_server_mapping.hpp"
16#include "utils.hpp"
17#include "transformation_enum.hpp"
18
19namespace xios {
20
21   /// ////////////////////// Declarations ////////////////////// ///
22
23   class CGridGroup;
24   class CGridAttributes;
25   class CDomainGroup;
26   class CAxisGroup;
27   class CScalarGroup;
28   class CGrid;
29   class CDistributionClient;
30   class CDistributionServer;
31   class CServerDistributionDescription;
32   class CClientServerMapping;
33   class CGridTransformation;
34
35   ///--------------------------------------------------------------
36
37   // Declare/Define CGridAttribute
38   BEGIN_DECLARE_ATTRIBUTE_MAP(CGrid)
39#  include "grid_attribute.conf"
40   END_DECLARE_ATTRIBUTE_MAP(CGrid)
41
42   ///--------------------------------------------------------------
43
44   class CGrid
45      : public CObjectTemplate<CGrid>
46      , public CGridAttributes
47   {
48         /// typedef ///
49         typedef CObjectTemplate<CGrid>   SuperClass;
50         typedef CGridAttributes SuperClassAttribute;
51
52      public:
53
54         typedef CGridAttributes RelAttributes;
55         typedef CGridGroup      RelGroup;
56
57         enum EEventId
58         {
59           EVENT_ID_INDEX, EVENT_ID_ADD_DOMAIN, EVENT_ID_ADD_AXIS, EVENT_ID_ADD_SCALAR
60         };
61
62         /// Constructeurs ///
63         CGrid(void);
64         explicit CGrid(const StdString& id);
65         CGrid(const CGrid& grid);       // Not implemented yet.
66         CGrid(const CGrid* const grid); // Not implemented yet.
67
68         /// Traitements ///
69//         void solveReference(void);
70
71         void checkEligibilityForCompressedOutput();
72
73
74
75         void checkMaskIndex(bool doCalculateIndex);
76
77 //        virtual void toBinary  (StdOStream& os) const;
78//         virtual void fromBinary(StdIStream& is);
79
80         void addRelFileCompressed(const StdString& filename);
81
82         /// Tests ///
83         bool isCompressible(void) const;
84         bool isWrittenCompressed(const StdString& filename) const;
85
86      public:
87
88         /// Accesseurs ///
89         StdSize getDimension(void);
90
91         StdSize  getDataSize(void) ;
92
93         /// Entrees-sorties de champs
94         template <int n>
95         void inputField(const CArray<double,n>& field, CArray<double,1>& stored) ;
96         template <int n>
97         void maskField(const CArray<double,n>& field, CArray<double,1>& stored) ;
98         template <int n>
99         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) ; 
100         template <int n>
101         void uncompressField(const CArray<double,n>& data, CArray<double,1>& outData) ; 
102
103         virtual void parse(xml::CXMLNode& node);
104
105         /// Destructeur ///
106         virtual ~CGrid(void);
107
108      public:
109
110         /// Accesseurs statiques ///
111         static StdString GetName(void);
112         static StdString GetDefName(void);
113
114         static ENodeType GetType(void);
115
116         /// Instanciateurs Statiques ///
117         static CGrid* createGrid(CDomain* domain);
118         static CGrid* createGrid(CDomain* domain, CAxis* axis);
119         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
120                                  const CArray<int,1>& axisDomainOrder = CArray<int,1>());
121         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
122                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
123         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
124                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder);
125         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
126                                     const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
127         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
128         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
129
130      public:           
131         void computeIndexServer(void);
132         void computeIndex(void);
133         void computeIndexScalarGrid();
134         void computeWrittenIndex();
135         void solveDomainAxisRef(bool areAttributesChecked);
136         void checkElementsAttributes(void) ;
137
138         void solveDomainRef(bool checkAtt);
139         void solveAxisRef(bool checkAtt);
140         void solveScalarRef(bool checkAtt);
141         void solveElementsRefInheritance(bool apply = true);
142        // void solveTransformations();
143         void solveDomainAxisBaseRef();
144
145         CDomain* addDomain(const std::string& id=StdString());
146         CAxis* addAxis(const std::string& id=StdString());
147         CScalar* addScalar(const std::string& id=StdString());
148
149      public:
150         void sendGridToFileServer(CContextClient* client) ;
151      private:
152         std::set<CContextClient*> sendGridToFileServer_done_ ;
153     
154      public:
155         void sendAddDomain(const std::string& id,CContextClient* contextClient);
156         void sendAddAxis(const std::string& id,CContextClient* contextClient);
157         void sendAddScalar(const std::string& id,CContextClient* contextClient);
158         void sendAllDomains(CContextClient* contextClient);
159         void sendAllAxis(CContextClient* contextClient);
160         void sendAllScalars(CContextClient* contextClient);
161
162         static void recvAddDomain(CEventServer& event);
163         void recvAddDomain(CBufferIn& buffer);
164         static void recvAddAxis(CEventServer& event);
165         void recvAddAxis(CBufferIn& buffer);
166         static void recvAddScalar(CEventServer& event);
167         void recvAddScalar(CBufferIn& buffer);
168
169         static bool dispatchEvent(CEventServer& event);
170         static void recvIndex(CEventServer& event);
171         void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers, CContextServer* server);
172         void sendIndex(void);
173         void sendIndexScalarGrid();
174
175         void setContextClient(CContextClient* contextClient);
176
177         void computeDomConServer();
178         std::map<int, int> getDomConServerSide();
179         std::map<int, StdSize> getAttributesBufferSize(CContextClient* client, bool bufferForWriting = false);
180         std::map<int, StdSize> getDataBufferSize(CContextClient* client, const std::string& id = "", bool bufferForWriting = false);
181         std::vector<StdString> getDomainList();
182         std::vector<StdString> getAxisList();
183         std::vector<StdString> getScalarList();
184         std::vector<CDomain*> getDomains();
185         std::vector<CAxis*> getAxis();
186         std::vector<CScalar*> getScalars();
187         CDomain* getDomain(int domainIndex);
188         CAxis* getAxis(int axisIndex);
189         CScalar* getScalar(int scalarIndex);
190         std::vector<int> getAxisOrder();
191         std::vector<int> getGlobalDimension();
192         bool isScalarGrid() const;         
193
194         bool doGridHaveDataToWrite();
195         bool doGridHaveDataDistributed(CContextClient* client = 0);
196         size_t getWrittenDataSize() ;
197         int getNumberWrittenIndexes() const;
198         int getTotalNumberWrittenIndexes() const;
199         int getOffsetWrittenIndexes() const;
200
201         CGridTransformation* getTransformations();
202
203         void transformGrid(CGrid* transformGridSrc);
204         
205         void prepareTransformGrid(CGrid* transformGridSrc);
206         bool prepareTransformGrid_done_ = false ;
207
208         void makeTransformGrid(void); 
209         bool makeTransformGrid_done_ = false ;
210         
211         std::vector<std::string> getAuxInputTransformGrid(void) ; 
212
213         void completeGrid(CGrid* transformGridSrc = 0);
214         void doAutoDistribution(CGrid* transformGridSrc);
215         bool isTransformed();
216         void setTransformed();
217         bool isGenerated();
218         void setGenerated();
219         void addTransGridSource(CGrid* gridSrc);
220         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
221         bool hasTransform();
222         size_t getGlobalWrittenSize(void) ;
223         bool checkIfCompleted(void) ;
224
225
226         bool hasMask(void) const;
227         void checkMask(void);
228         void createMask(void);
229         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
230         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
231
232         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
233                                         const std::vector<CAxis*>& axis,
234                                         const std::vector<CScalar*>& scalars,
235                                         const CArray<int,1>& axisDomainOrder);
236
237         void computeGridIndexToFileServer(CContextClient* client) ;
238 
239     private:
240        /** Client-like distribution calculated based on the knowledge of the entire grid */
241       CDistributionClient* clientDistribution_;
242     public: 
243       void computeClientDistribution(void) ;
244     private:
245       bool computeClientDistribution_done_ = false ;
246     public:
247       CDistributionClient* getClientDistribution(void); 
248
249     private:   
250       /** Server-like distribution calculated upon receiving indexes */
251       CDistributionServer* serverDistribution_;
252       void computeServerDistribution(void) ;
253       bool computeServerDistribution_done_=false ;
254     public: 
255       CDistributionServer* getServerDistribution(void) { if (computeServerDistribution_done_) computeServerDistribution() ; return serverDistribution_ ;}
256
257
258     private:
259       template<int N>
260       void checkGridMask(CArray<bool,N>& gridMask,
261                          const std::vector<CArray<bool,1>* >& domainMasks,
262                          const std::vector<CArray<bool,1>* >& axisMasks,
263                          const CArray<int,1>& axisDomainOrder,
264                          bool createMask = false);
265        template<int N>
266        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
267
268        template<int N>
269        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
270
271        void storeField_arr(const double* const data, CArray<double, 1>& stored) ;
272        void restoreField_arr(const CArray<double, 1>& stored, double* const data) ;
273        void uncompressField_arr(const double* const data, CArray<double, 1>& outData) ;
274        void maskField_arr(const double* const data, CArray<double, 1>& stored) ;
275
276        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
277        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
278        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
279
280        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
281        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
282        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
283
284        CDomainGroup* getVirtualDomainGroup() const;
285        CAxisGroup* getVirtualAxisGroup() const;
286        CScalarGroup* getVirtualScalarGroup() const;
287
288        void checkAttributesAfterTransformation();
289        void setTransformationAlgorithms();
290        void computeIndexByElement(const std::vector<std::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
291                                   const CContextClient* client,
292                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
293        int computeGridGlobalDimension(std::vector<int>& globalDim,
294                                       const std::vector<CDomain*> domains,
295                                       const std::vector<CAxis*> axis,
296                                       const std::vector<CScalar*> scalars,
297                                       const CArray<int,1>& axisDomainOrder);
298        int getDistributedDimension();
299
300        void computeConnectedClients(CContextClient* client);
301        set<CContextClient*> computeConnectedClients_done_ ;
302
303        void computeConnectedClientsScalarGrid(CContextClient* client); 
304        set<CContextClient*> computeConnectedClientsScalarGrid_done_ ;
305
306      public:
307/** Array containing the local index of the grid
308 *  storeIndex_client[local_workflow_grid_index] -> local_model_grid_index.
309 *  Used to store field from model into the worklow, or to return field into models. 
310 *  The size of the array is the number of local index of the workflow grid */       
311        CArray<int, 1> storeIndex_client_;
312        void computeStoreIndex_client(void) ;
313        bool computeStoreIndex_client_done_ = false ;
314        CArray<int, 1>& getStoreIndex_client(void) { if (!computeStoreIndex_client_done_) computeStoreIndex_client() ; return storeIndex_client_ ;}
315
316/** Array containing the grid mask masked defined by the mask_nd grid attribute.       
317  * The corresponding masked field value provided by the model will be replaced by a NaN value
318  * in the workflow.  */
319        CArray<bool, 1> storeMask_client_;
320        void computeStoreMask_client(void) ;
321        bool computeStoreMask_client_done_ = false ;
322        CArray<bool, 1>& getStoreMask_client(void) { if (!computeStoreMask_client_done_) computeStoreMask_client() ; return storeMask_client_ ;}
323
324/** Map containing the indexes on client side that will be sent to each connected server.
325  * storeIndex_toSrv[&contextClient] -> map concerning the contextClient for which the data will be sent (client side)
326  * storeIndex_toSrv[&contextClient][rank] -> array of indexes that will be sent to each "rank" of the connected servers
327  * storeIndex_toSrv[&contextClient][rank](index_of_buffer_sent_to_server) -> local index of the field of the workflow
328  * grid that will be sent to server */
329         std::map<CContextClient*, map<int, CArray<int, 1> > > storeIndex_toSrv_;
330
331
332/** Map containing the indexes on client side that will be received from each connected server.
333  * This map is used to agreggate field data received from server (for reading) into a single array, which will be an entry
334  * point of the worklow on client side.
335  * storeIndex_toSrv[rank] -> array of indexes that will be received from each "rank" of the connected servers
336  * storeIndex_toSrv[rank](index_of_buffer_received_from_server) -> local index of the field in the "workflow grid"
337  * that has been received from server */
338         std::map<int, CArray<int, 1> > storeIndex_fromSrv_; // Support, for now, reading with level-1 server
339
340
341/** Maps storing the number of participating clients for data sent a specific server for a given contextClient, identified
342  * by the servers communicator size. In future must be direcly identified by context.
343  * nbSender_[context_server_size] -> map the number of client sender by connected rank of servers
344  * nbSender_[context_server_size] [rank_server] -> the number of client participating to a send message for a server of rank "rank_server"
345  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
346         std::map<int, std::map<int,int> > nbSenders_;
347
348
349/** Maps storing the number of participating servers for data sent a specific client for a given contextClient.
350  * Symetric of nbSenders_, but for server side which want to send data to client.
351  * nbReadSender_[context_client_size] -> map the number of server sender by connected rank of clients
352  * nbReadSender_[context_client_size] [rank_client] -> the number of serverq participating to a send message for a client of rank "rank_client"
353  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
354         std::map<CContextClient*, std::map<int,int> > nbReadSenders_;
355
356
357// Manh Ha's comment: " A client receives global index from other clients (via recvIndex)
358// then does mapping these index into local index of STORE_CLIENTINDEX
359// In this way, store_clientIndex can be used as an input of a source filter
360// Maybe we need a flag to determine whether a client wants to write. TODO "
361
362      private:
363       /** Map storing received data on server side. This map is the equivalent to the storeIndex_client, but for data received from client
364        * instead that from model. This map is used to concatenate data received from several clients into a single array on server side
365        * which match the local workflow grid.
366        * outLocalIndexStoreOnClient_[client_rank] -> Array of index from client of rank "client_rank"
367        * outLocalIndexStoreOnClient_[client_rank](index of buffer from client) -> local index of the workflow grid
368        * The map is created in CGrid::computeClientIndex and filled upon receiving data in CField::recvUpdateData().
369        * Symetrically it is also used to send data from a server to several client for reading case. */
370        map<int, CArray<size_t, 1>> outLocalIndexStoreOnClient_; 
371      public:
372         void computeOutLocalIndexStoreOnClient(void) ;
373      private:
374         bool computeOutLocalIndexStoreOnClient_done_ = false ; 
375      public:   
376         map<int, CArray<size_t, 1>>& getOutLocalIndexStoreOnClient(void) 
377         { if (!computeOutLocalIndexStoreOnClient_done_) computeOutLocalIndexStoreOnClient(); return outLocalIndexStoreOnClient_ ; }
378
379      public:
380/** Indexes calculated based on server-like distribution.
381 *  They are used for writing/reading data and only calculated for server level that does the writing/reading.
382 *  Along with localIndexToWriteOnClient, these indexes are used to correctly place incoming data.
383 *  size of the array : numberWrittenIndexes_ : number of index written in a compressed way
384 *  localIndexToWriteOnServer_(compressed_written_index) : -> local uncompressed index that will be written in the file */
385         CArray<size_t,1> localIndexToWriteOnServer_;
386
387/** Indexes calculated based on client-like distribution.
388  * They are used for writing/reading data and only calculated for server level that does the writing/reading.
389  * Along with localIndexToWriteOnServer, these indexes are used to correctly place incoming data.
390  * size of the array : numberWrittenIndexes_
391  * localIndexToWriteOnClient_(compressed_written_index) -> local index of the workflow grid*/
392         CArray<size_t,1> localIndexToWriteOnClient_;
393
394      public: 
395        bool isDataDistributed(void) ; 
396      private:
397
398/** Clients that have to send a grid. There can be multiple clients in case of secondary server, otherwise only one client. */
399        std::list<CContextClient*> clients;
400        std::set<CContextClient*> clientsSet;
401
402      private: 
403        /** Map storing received indexes on server side sent by clients. Key = sender rank, value = global index array.
404            Later, the global indexes received will be mapped onto local index computed with the local distribution.
405            outGlobalIndexFromClient_[rank] -> array of global index send by client of rank "rank"
406            outGlobalIndexFromClient_[rank](n) -> global index of datav n sent by client
407        */     
408        map<int, CArray<size_t, 1> > outGlobalIndexFromClient_;
409      public: 
410        map<int, CArray<size_t, 1> >& getOutGlobalIndexFromClient() { return outGlobalIndexFromClient_ ;}
411
412      private:
413        bool isChecked;
414        bool isDomainAxisChecked;
415        bool isIndexSent;
416
417        CDomainGroup* vDomainGroup_;
418        CAxisGroup* vAxisGroup_;
419        CScalarGroup* vScalarGroup_;
420        std::vector<std::string> axisList_, domList_, scalarList_;
421        bool isAxisListSet, isDomListSet, isScalarListSet;
422
423        CClientServerMapping* clientServerMap_;
424        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
425
426/** Map storing local ranks of connected receivers. Key = size of receiver's intracomm.
427  * It is calculated in computeConnectedClients(). */
428        std::map<int, std::vector<int> > connectedServerRank_;
429
430/** Map storing the size of data to be send. Key = size of receiver's intracomm
431  * It is calculated in computeConnectedClients(). */
432        std::map<int, std::map<int,size_t> > connectedDataSize_;
433
434/** Ranks of connected receivers in case of reading. It is calculated in recvIndex(). */
435        std::vector<int> connectedServerRankRead_;
436
437/** Size of data to be send in case of reading. It is calculated in recvIndex(). */
438        std::map<int,size_t> connectedDataSizeRead_;
439     
440         //! True if and only if the data defined on the grid can be outputted in a compressed way
441        bool isCompressible_;
442        std::set<std::string> relFilesCompressed;
443
444        bool isTransformed_, isGenerated_;
445        bool computedWrittenIndex_;
446       
447        std::vector<int> axisPositionInGrid_;
448        void computeAxisPositionInGrid(void) ;
449        bool computeAxisPositionInGrid_done_ = false ;
450        std::vector<int>& getAxisPositionInGrid(void) { if (!computeAxisPositionInGrid_done_) computeAxisPositionInGrid() ; return axisPositionInGrid_ ;}
451
452        CGridTransformation* transformations_;
453        bool hasDomainAxisBaseRef_;       
454        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
455        bool hasTransform_;
456
457/** Map storing global indexes of server-like (band-wise) distribution for sending to receivers (client side).
458  * Key = size of receiver's intracomm (i.e. number of servers)
459  * ~ map<int, umap<int, std::vector<size_t> >> globalIndexOnServer_
460  * globalIndexOnServer_[servers_size] -> map for a distribution of size "servers_size" (number of servers)
461  * globalIndexOnServer_[servers_size][server_rank] -> array of global index managed by server of rank "server_rank"
462  * globalIndexOnServer_[servers_size][server_rank][n] -> global index of data to be send to the server by client based on sub element of the grid.
463  * -> grid masking is not included.
464  */
465//        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
466        std::map<int, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
467
468
469/** List order of axis and domain in a grid, if there is a domain, it will take value 2, axis 1, scalar 0 */
470        std::vector<int> order_;
471
472   }; // class CGrid
473
474   ///--------------------------------------------------------------
475
476   template <int n>
477   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored)
478   TRY
479   {
480//#ifdef __XIOS_DEBUG
481      if (this->getDataSize() != field.numElements())
482         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
483                << "[ Awaiting data of size = " << this->getDataSize() << ", "
484                << "Received data size = "      << field.numElements() << " ] "
485                << "The data array does not have the right size! "
486                << "Grid = " << this->getId())
487//#endif
488      this->storeField_arr(field.dataFirst(), stored);
489   }
490   CATCH
491
492   template <int n>
493   void CGrid::maskField(const CArray<double,n>& field, CArray<double,1>& stored)
494   {
495//#ifdef __XIOS_DEBUG
496      if (this->getDataSize() != field.numElements())
497         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
498                << "[ Awaiting data of size = " << this->getDataSize() << ", "
499                << "Received data size = "      << field.numElements() << " ] "
500                << "The data array does not have the right size! "
501                << "Grid = " << this->getId())
502//#endif
503      this->maskField_arr(field.dataFirst(), stored);
504   }
505
506   template <int n>
507   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field)
508   TRY
509   {
510//#ifdef __XIOS_DEBUG
511      if (this->getDataSize() != field.numElements())
512         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
513                << "[ Size of the data = " << this->getDataSize() << ", "
514                << "Output data size = "   << field.numElements() << " ] "
515                << "The ouput array does not have the right size! "
516                << "Grid = " << this->getId())
517//#endif
518      this->restoreField_arr(stored, field.dataFirst());
519   }
520   CATCH
521
522   /*!
523     This function removes the effect of mask on received data on the server.
524     This function only serve for the checking purpose. TODO: Something must be done to seperate mask and data_index from each other in received data
525     \data data received data with masking effect on the server
526     \outData data without masking effect
527   */
528   template <int N>
529   void CGrid::uncompressField(const CArray<double,N>& data, CArray<double,1>& outData)
530   TRY
531   {     
532     uncompressField_arr(data.dataFirst(), outData);
533   }
534   CATCH
535
536   template<int N>
537   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
538                             const std::vector<CArray<bool,1>* >& domainMasks,
539                             const std::vector<CArray<bool,1>* >& axisMasks,
540                             const CArray<int,1>& axisDomainOrder,
541                             bool createMask)
542   TRY
543   {
544     int idx = 0;
545     int numElement = axisDomainOrder.numElements();
546     int dim = domainMasks.size() * 2 + axisMasks.size();
547     std::vector<CDomain*> domainP = this->getDomains();
548     std::vector<CAxis*> axisP = this->getAxis();
549
550     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
551     std::vector<int> currentIndex(dim);
552     int idxDomain = 0, idxAxis = 0;
553    for (int i = 0; i < numElement; ++i)
554    {
555      indexMap[i] = idx;
556      if (2 == axisDomainOrder(i)) {
557          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
558          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
559          idx += 2; ++idxDomain;
560      }
561      else if (1 == axisDomainOrder(i)) {
562//        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
563        eachDimSize[indexMap[i]] = axisP[idxAxis]->n;
564        ++idx; ++idxAxis;
565      }
566      else {};
567    }
568
569    if (!gridMask.isEmpty() && !createMask)
570    {
571      for (int i = 0; i < dim; ++i)
572      {
573        if (gridMask.extent(i) != eachDimSize[i])
574          ERROR("CGrid::checkMask(void)",
575                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
576                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
577                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
578                << "Grid = " << this->getId())
579      }
580    }
581    else {
582        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
583        gridMask = true;
584    }
585
586    int ssize = gridMask.numElements();
587    idx = 0;
588    while (idx < ssize)
589    {
590      for (int i = 0; i < dim-1; ++i)
591      {
592        if (idxLoop[i] == eachDimSize[i])
593        {
594          idxLoop[i] = 0;
595          ++idxLoop[i+1];
596        }
597      }
598
599      // Find out outer index
600      idxDomain = idxAxis = 0;
601      bool maskValue = true;
602      for (int i = 0; i < numElement; ++i)
603      {
604        if (2 == axisDomainOrder(i))
605        {
606          int idxTmp = idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]];
607          if (idxTmp < (*domainMasks[idxDomain]).numElements())
608            maskValue = maskValue && (*domainMasks[idxDomain])(idxTmp);
609          else
610            maskValue = false;
611          ++idxDomain;
612        }
613        else if (1 == axisDomainOrder(i))
614        {
615          int idxTmp = idxLoop[indexMap[i]];
616          if (idxTmp < (*axisMasks[idxAxis]).numElements())
617            maskValue = maskValue && (*axisMasks[idxAxis])(idxTmp);
618          else
619            maskValue = false;
620
621          ++idxAxis;
622        }
623      }
624
625      int maskIndex = idxLoop[0];
626      int mulDim = 1;
627      for (int k = 1; k < dim; ++k)
628      {
629        mulDim *= eachDimSize[k-1];
630        maskIndex += idxLoop[k]*mulDim;
631      }
632      *(gridMask.dataFirst()+maskIndex) &= maskValue;
633
634      ++idxLoop[0];
635      ++idx;
636    }
637   }
638   CATCH_DUMP_ATTR
639
640   template<int N>
641   void CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
642                                  const std::vector<int>& eachDimSize,
643                                  bool newValue)
644   TRY
645   {
646      if (N != eachDimSize.size())
647      {
648        // ERROR("CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
649        //                                  const std::vector<int>& eachDimSize,
650        //                                  bool newValue)",
651        //       << "Dimension size of the mask is different from input dimension size." << std::endl
652        //       << "Mask dimension is " << N << "." << std::endl
653        //       << "Input dimension is " << eachDimSize.size() << "." << std::endl
654        //       << "Grid = " << this->getId())
655      }
656      CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
657      gridMask = newValue;
658   }
659   CATCH_DUMP_ATTR
660                                 
661
662   /*!
663     Modify the current mask of grid, the local index to be modified will take value false
664     \param [in/out] gridMask current mask of grid
665     \param [in] indexToModify local index to modify
666   */
667   template<int N>
668   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
669   TRY
670   {     
671     int num = indexToModify.numElements();
672     for (int idx = 0; idx < num; ++idx)
673     {
674       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
675     }
676   }
677   CATCH_DUMP_ATTR
678
679   ///--------------------------------------------------------------
680
681
682
683   // Declare/Define CGridGroup and CGridDefinition
684   DECLARE_GROUP(CGrid);
685
686   ///--------------------------------------------------------------
687
688} // namespace xios
689
690#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.