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

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

XIOS coupling branch
Some updates.

First coupling test is beginning to work...

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