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

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

XIOS_COUPLING branch : more comments and cleaning

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