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

Last change on this file since 1870 was 1870, 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.1 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() const;
197         int getNumberWrittenIndexes() const;
198         int getTotalNumberWrittenIndexes() const;
199         int getOffsetWrittenIndexes() const;
200
201         CDistributionServer* getDistributionServer();
202         CGridTransformation* getTransformations();
203
204         void transformGrid(CGrid* transformGridSrc);
205         
206         void prepareTransformGrid(CGrid* transformGridSrc);
207         bool prepareTransformGrid_done_ = false ;
208
209         void makeTransformGrid(void); 
210         bool makeTransformGrid_done_ = false ;
211         
212         std::vector<std::string> getAuxInputTransformGrid(void) ; 
213
214         void completeGrid(CGrid* transformGridSrc = 0);
215         void doAutoDistribution(CGrid* transformGridSrc);
216         bool isTransformed();
217         void setTransformed();
218         bool isGenerated();
219         void setGenerated();
220         void addTransGridSource(CGrid* gridSrc);
221         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
222         bool hasTransform();
223         size_t getGlobalWrittenSize(void) ;
224         bool checkIfCompleted(void) ;
225
226
227         bool hasMask(void) const;
228         void checkMask(void);
229         void createMask(void);
230         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
231         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
232
233         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
234                                         const std::vector<CAxis*>& axis,
235                                         const std::vector<CScalar*>& scalars,
236                                         const CArray<int,1>& axisDomainOrder);
237
238         void computeGridIndexToFileServer(CContextClient* client) ;
239 
240     private:
241        /** Client-like distribution calculated based on the knowledge of the entire grid */
242       CDistributionClient* clientDistribution_;
243     public: 
244       void computeClientDistribution(void) ;
245     private:
246       bool computeClientDistribution_done_ = false ;
247     public:
248       CDistributionClient* getClientDistribution(void); 
249
250     private:
251       template<int N>
252       void checkGridMask(CArray<bool,N>& gridMask,
253                          const std::vector<CArray<bool,1>* >& domainMasks,
254                          const std::vector<CArray<bool,1>* >& axisMasks,
255                          const CArray<int,1>& axisDomainOrder,
256                          bool createMask = false);
257        template<int N>
258        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
259
260        template<int N>
261        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
262
263        void storeField_arr(const double* const data, CArray<double, 1>& stored) ;
264        void restoreField_arr(const CArray<double, 1>& stored, double* const data) ;
265        void uncompressField_arr(const double* const data, CArray<double, 1>& outData) ;
266        void maskField_arr(const double* const data, CArray<double, 1>& stored) ;
267
268        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
269        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
270        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
271
272        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
273        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
274        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
275
276        CDomainGroup* getVirtualDomainGroup() const;
277        CAxisGroup* getVirtualAxisGroup() const;
278        CScalarGroup* getVirtualScalarGroup() const;
279
280        void checkAttributesAfterTransformation();
281        void setTransformationAlgorithms();
282        void computeIndexByElement(const std::vector<std::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
283                                   const CContextClient* client,
284                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
285        int computeGridGlobalDimension(std::vector<int>& globalDim,
286                                       const std::vector<CDomain*> domains,
287                                       const std::vector<CAxis*> axis,
288                                       const std::vector<CScalar*> scalars,
289                                       const CArray<int,1>& axisDomainOrder);
290        int getDistributedDimension();
291
292        void computeConnectedClients(CContextClient* client);
293        set<CContextClient*> computeConnectedClients_done_ ;
294
295        void computeConnectedClientsScalarGrid(CContextClient* client); 
296        set<CContextClient*> computeConnectedClientsScalarGrid_done_ ;
297
298      public:
299/** Array containing the local index of the grid
300 *  storeIndex_client[local_workflow_grid_index] -> local_model_grid_index.
301 *  Used to store field from model into the worklow, or to return field into models. 
302 *  The size of the array is the number of local index of the workflow grid */       
303        CArray<int, 1> storeIndex_client_;
304        void computeStoreIndex_client(void) ;
305        bool computeStoreIndex_client_done_ = false ;
306        CArray<int, 1>& getStoreIndex_client(void) { if (!computeStoreIndex_client_done_) computeStoreIndex_client() ; return storeIndex_client_ ;}
307
308/** Array containing the grid mask masked defined by the mask_nd grid attribute.       
309  * The corresponding masked field value provided by the model will be replaced by a NaN value
310  * in the workflow.  */
311        CArray<bool, 1> storeMask_client_;
312        void computeStoreMask_client(void) ;
313        bool computeStoreMask_client_done_ = false ;
314        CArray<bool, 1>& getStoreMask_client(void) { if (!computeStoreMask_client_done_) computeStoreMask_client() ; return storeMask_client_ ;}
315
316/** Map containing the indexes on client side that will be sent to each connected server.
317  * storeIndex_toSrv[&contextClient] -> map concerning the contextClient for which the data will be sent (client side)
318  * storeIndex_toSrv[&contextClient][rank] -> array of indexes that will be sent to each "rank" of the connected servers
319  * storeIndex_toSrv[&contextClient][rank](index_of_buffer_sent_to_server) -> local index of the field of the workflow
320  * grid that will be sent to server */
321         std::map<CContextClient*, map<int, CArray<int, 1> > > storeIndex_toSrv_;
322
323
324/** Map containing the indexes on client side that will be received from each connected server.
325  * This map is used to agreggate field data received from server (for reading) into a single array, which will be an entry
326  * point of the worklow on client side.
327  * storeIndex_toSrv[rank] -> array of indexes that will be received from each "rank" of the connected servers
328  * storeIndex_toSrv[rank](index_of_buffer_received_from_server) -> local index of the field in the "workflow grid"
329  * that has been received from server */
330         std::map<int, CArray<int, 1> > storeIndex_fromSrv_; // Support, for now, reading with level-1 server
331
332
333/** Maps storing the number of participating clients for data sent a specific server for a given contextClient, identified
334  * by the servers communicator size. In future must be direcly identified by context.
335  * nbSender_[context_server_size] -> map the number of client sender by connected rank of servers
336  * nbSender_[context_server_size] [rank_server] -> the number of client participating to a send message for a server of rank "rank_server"
337  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
338         std::map<int, std::map<int,int> > nbSenders_;
339
340
341/** Maps storing the number of participating servers for data sent a specific client for a given contextClient.
342  * Symetric of nbSenders_, but for server side which want to send data to client.
343  * nbReadSender_[context_client_size] -> map the number of server sender by connected rank of clients
344  * nbReadSender_[context_client_size] [rank_client] -> the number of serverq participating to a send message for a client of rank "rank_client"
345  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
346         std::map<CContextClient*, std::map<int,int> > nbReadSenders_;
347
348
349// Manh Ha's comment: " A client receives global index from other clients (via recvIndex)
350// then does mapping these index into local index of STORE_CLIENTINDEX
351// In this way, store_clientIndex can be used as an input of a source filter
352// Maybe we need a flag to determine whether a client wants to write. TODO "
353
354      private:
355       /** Map storing received data on server side. This map is the equivalent to the storeIndex_client, but for data received from client
356        * instead that from model. This map is used to concatenate data received from several clients into a single array on server side
357        * which match the local workflow grid.
358        * outLocalIndexStoreOnClient_[client_rank] -> Array of index from client of rank "client_rank"
359        * outLocalIndexStoreOnClient_[client_rank](index of buffer from client) -> local index of the workflow grid
360        * The map is created in CGrid::computeClientIndex and filled upon receiving data in CField::recvUpdateData().
361        * Symetrically it is also used to send data from a server to several client for reading case. */
362        map<int, CArray<size_t, 1>> outLocalIndexStoreOnClient_; 
363      public:
364         void computeOutLocalIndexStoreOnClient(void) ;
365      private:
366         bool computeOutLocalIndexStoreOnClient_done_ = false ; 
367      public:   
368         map<int, CArray<size_t, 1>>& getOutLocalIndexStoreOnClient(void) 
369         { if (!computeOutLocalIndexStoreOnClient_done_) computeOutLocalIndexStoreOnClient(); return outLocalIndexStoreOnClient_ ; }
370
371      public:
372/** Indexes calculated based on server-like distribution.
373 *  They are used for writing/reading data and only calculated for server level that does the writing/reading.
374 *  Along with localIndexToWriteOnClient, these indexes are used to correctly place incoming data.
375 *  size of the array : numberWrittenIndexes_ : number of index written in a compressed way
376 *  localIndexToWriteOnServer_(compressed_written_index) : -> local uncompressed index that will be written in the file */
377         CArray<size_t,1> localIndexToWriteOnServer_;
378
379/** Indexes calculated based on client-like distribution.
380  * They are used for writing/reading data and only calculated for server level that does the writing/reading.
381  * Along with localIndexToWriteOnServer, these indexes are used to correctly place incoming data.
382  * size of the array : numberWrittenIndexes_
383  * localIndexToWriteOnClient_(compressed_written_index) -> local index of the workflow grid*/
384         CArray<size_t,1> localIndexToWriteOnClient_;
385
386      public: 
387        bool isDataDistributed(void) ; 
388      private:
389
390/** Clients that have to send a grid. There can be multiple clients in case of secondary server, otherwise only one client. */
391        std::list<CContextClient*> clients;
392        std::set<CContextClient*> clientsSet;
393
394      private: 
395        /** Map storing received indexes on server side sent by clients. Key = sender rank, value = global index array.
396            Later, the global indexes received will be mapped onto local index computed with the local distribution.
397            outGlobalIndexFromClient_[rank] -> array of global index send by client of rank "rank"
398            outGlobalIndexFromClient_[rank](n) -> global index of datav n sent by client
399        */     
400        map<int, CArray<size_t, 1> > outGlobalIndexFromClient_;
401      public: 
402        map<int, CArray<size_t, 1> >& getOutGlobalIndexFromClient() { return outGlobalIndexFromClient_ ;}
403
404      private:
405        bool isChecked;
406        bool isDomainAxisChecked;
407        bool isIndexSent;
408
409        CDomainGroup* vDomainGroup_;
410        CAxisGroup* vAxisGroup_;
411        CScalarGroup* vScalarGroup_;
412        std::vector<std::string> axisList_, domList_, scalarList_;
413        bool isAxisListSet, isDomListSet, isScalarListSet;
414
415/** Server-like distribution calculated upon receiving indexes */
416        CDistributionServer* serverDistribution_;
417
418        CClientServerMapping* clientServerMap_;
419        size_t writtenDataSize_;
420        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
421
422/** Map storing local ranks of connected receivers. Key = size of receiver's intracomm.
423  * It is calculated in computeConnectedClients(). */
424        std::map<int, std::vector<int> > connectedServerRank_;
425
426/** Map storing the size of data to be send. Key = size of receiver's intracomm
427  * It is calculated in computeConnectedClients(). */
428        std::map<int, std::map<int,size_t> > connectedDataSize_;
429
430/** Ranks of connected receivers in case of reading. It is calculated in recvIndex(). */
431        std::vector<int> connectedServerRankRead_;
432
433/** Size of data to be send in case of reading. It is calculated in recvIndex(). */
434        std::map<int,size_t> connectedDataSizeRead_;
435     
436         //! True if and only if the data defined on the grid can be outputted in a compressed way
437        bool isCompressible_;
438        std::set<std::string> relFilesCompressed;
439
440        bool isTransformed_, isGenerated_;
441        bool computedWrittenIndex_;
442       
443        std::vector<int> axisPositionInGrid_;
444        void computeAxisPositionInGrid(void) ;
445        bool computeAxisPositionInGrid_done_ = false ;
446        std::vector<int>& getAxisPositionInGrid(void) { if (!computeAxisPositionInGrid_done_) computeAxisPositionInGrid() ; return axisPositionInGrid_ ;}
447
448        CGridTransformation* transformations_;
449        bool hasDomainAxisBaseRef_;       
450        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
451        bool hasTransform_;
452
453/** Map storing global indexes of server-like (band-wise) distribution for sending to receivers (client side).
454  * Key = size of receiver's intracomm (i.e. number of servers)
455  * ~ map<int, umap<int, std::vector<size_t> >> globalIndexOnServer_
456  * globalIndexOnServer_[servers_size] -> map for a distribution of size "servers_size" (number of servers)
457  * globalIndexOnServer_[servers_size][server_rank] -> array of global index managed by server of rank "server_rank"
458  * 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.
459  * -> grid masking is not included.
460  */
461//        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
462        std::map<int, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
463
464
465/** List order of axis and domain in a grid, if there is a domain, it will take value 2, axis 1, scalar 0 */
466        std::vector<int> order_;
467
468   }; // class CGrid
469
470   ///--------------------------------------------------------------
471
472   template <int n>
473   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored)
474   TRY
475   {
476//#ifdef __XIOS_DEBUG
477      if (this->getDataSize() != field.numElements())
478         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
479                << "[ Awaiting data of size = " << this->getDataSize() << ", "
480                << "Received data size = "      << field.numElements() << " ] "
481                << "The data array does not have the right size! "
482                << "Grid = " << this->getId())
483//#endif
484      this->storeField_arr(field.dataFirst(), stored);
485   }
486   CATCH
487
488   template <int n>
489   void CGrid::maskField(const CArray<double,n>& field, CArray<double,1>& stored)
490   {
491//#ifdef __XIOS_DEBUG
492      if (this->getDataSize() != field.numElements())
493         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
494                << "[ Awaiting data of size = " << this->getDataSize() << ", "
495                << "Received data size = "      << field.numElements() << " ] "
496                << "The data array does not have the right size! "
497                << "Grid = " << this->getId())
498//#endif
499      this->maskField_arr(field.dataFirst(), stored);
500   }
501
502   template <int n>
503   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field)
504   TRY
505   {
506//#ifdef __XIOS_DEBUG
507      if (this->getDataSize() != field.numElements())
508         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
509                << "[ Size of the data = " << this->getDataSize() << ", "
510                << "Output data size = "   << field.numElements() << " ] "
511                << "The ouput array does not have the right size! "
512                << "Grid = " << this->getId())
513//#endif
514      this->restoreField_arr(stored, field.dataFirst());
515   }
516   CATCH
517
518   /*!
519     This function removes the effect of mask on received data on the server.
520     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
521     \data data received data with masking effect on the server
522     \outData data without masking effect
523   */
524   template <int N>
525   void CGrid::uncompressField(const CArray<double,N>& data, CArray<double,1>& outData)
526   TRY
527   {     
528     uncompressField_arr(data.dataFirst(), outData);
529   }
530   CATCH
531
532   template<int N>
533   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
534                             const std::vector<CArray<bool,1>* >& domainMasks,
535                             const std::vector<CArray<bool,1>* >& axisMasks,
536                             const CArray<int,1>& axisDomainOrder,
537                             bool createMask)
538   TRY
539   {
540     int idx = 0;
541     int numElement = axisDomainOrder.numElements();
542     int dim = domainMasks.size() * 2 + axisMasks.size();
543     std::vector<CDomain*> domainP = this->getDomains();
544     std::vector<CAxis*> axisP = this->getAxis();
545
546     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
547     std::vector<int> currentIndex(dim);
548     int idxDomain = 0, idxAxis = 0;
549    for (int i = 0; i < numElement; ++i)
550    {
551      indexMap[i] = idx;
552      if (2 == axisDomainOrder(i)) {
553          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
554          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
555          idx += 2; ++idxDomain;
556      }
557      else if (1 == axisDomainOrder(i)) {
558//        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
559        eachDimSize[indexMap[i]] = axisP[idxAxis]->n;
560        ++idx; ++idxAxis;
561      }
562      else {};
563    }
564
565    if (!gridMask.isEmpty() && !createMask)
566    {
567      for (int i = 0; i < dim; ++i)
568      {
569        if (gridMask.extent(i) != eachDimSize[i])
570          ERROR("CGrid::checkMask(void)",
571                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
572                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
573                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
574                << "Grid = " << this->getId())
575      }
576    }
577    else {
578        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
579        gridMask = true;
580    }
581
582    int ssize = gridMask.numElements();
583    idx = 0;
584    while (idx < ssize)
585    {
586      for (int i = 0; i < dim-1; ++i)
587      {
588        if (idxLoop[i] == eachDimSize[i])
589        {
590          idxLoop[i] = 0;
591          ++idxLoop[i+1];
592        }
593      }
594
595      // Find out outer index
596      idxDomain = idxAxis = 0;
597      bool maskValue = true;
598      for (int i = 0; i < numElement; ++i)
599      {
600        if (2 == axisDomainOrder(i))
601        {
602          int idxTmp = idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]];
603          if (idxTmp < (*domainMasks[idxDomain]).numElements())
604            maskValue = maskValue && (*domainMasks[idxDomain])(idxTmp);
605          else
606            maskValue = false;
607          ++idxDomain;
608        }
609        else if (1 == axisDomainOrder(i))
610        {
611          int idxTmp = idxLoop[indexMap[i]];
612          if (idxTmp < (*axisMasks[idxAxis]).numElements())
613            maskValue = maskValue && (*axisMasks[idxAxis])(idxTmp);
614          else
615            maskValue = false;
616
617          ++idxAxis;
618        }
619      }
620
621      int maskIndex = idxLoop[0];
622      int mulDim = 1;
623      for (int k = 1; k < dim; ++k)
624      {
625        mulDim *= eachDimSize[k-1];
626        maskIndex += idxLoop[k]*mulDim;
627      }
628      *(gridMask.dataFirst()+maskIndex) &= maskValue;
629
630      ++idxLoop[0];
631      ++idx;
632    }
633   }
634   CATCH_DUMP_ATTR
635
636   template<int N>
637   void CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
638                                  const std::vector<int>& eachDimSize,
639                                  bool newValue)
640   TRY
641   {
642      if (N != eachDimSize.size())
643      {
644        // ERROR("CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
645        //                                  const std::vector<int>& eachDimSize,
646        //                                  bool newValue)",
647        //       << "Dimension size of the mask is different from input dimension size." << std::endl
648        //       << "Mask dimension is " << N << "." << std::endl
649        //       << "Input dimension is " << eachDimSize.size() << "." << std::endl
650        //       << "Grid = " << this->getId())
651      }
652      CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
653      gridMask = newValue;
654   }
655   CATCH_DUMP_ATTR
656                                 
657
658   /*!
659     Modify the current mask of grid, the local index to be modified will take value false
660     \param [in/out] gridMask current mask of grid
661     \param [in] indexToModify local index to modify
662   */
663   template<int N>
664   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
665   TRY
666   {     
667     int num = indexToModify.numElements();
668     for (int idx = 0; idx < num; ++idx)
669     {
670       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
671     }
672   }
673   CATCH_DUMP_ATTR
674
675   ///--------------------------------------------------------------
676
677
678
679   // Declare/Define CGridGroup and CGridDefinition
680   DECLARE_GROUP(CGrid);
681
682   ///--------------------------------------------------------------
683
684} // namespace xios
685
686#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.