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

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

Some update...

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