source: XIOS/trunk/src/transformation/axis_algorithm_transformation.cpp @ 668

Last change on this file since 668 was 668, checked in by mhnguyen, 6 years ago

Implementing some code factoring

+) Replace some slow searching function by faster ones

Test
+) On Curie
+) test_client and test_complete are correct

File size: 5.6 KB
Line 
1/*!
2   \file axis_algorithm_transformation.hpp
3   \author Ha NGUYEN
4   \since 14 May 2015
5   \date 29 June 2015
6
7   \brief Interface for all axis transformation algorithms.
8 */
9
10#include "axis_algorithm_transformation.hpp"
11#include "axis_algorithm_inverse.hpp"
12#include "axis_algorithm_zoom.hpp"
13
14namespace xios {
15
16CAxisAlgorithmTransformation::CAxisAlgorithmTransformation(CAxis* axisDestination, CAxis* axisSource)
17 : CGenericAlgorithmTransformation(), axisDest_(axisDestination), axisSrc_(axisSource)
18{
19  axisDestGlobalSize_ = axisDestination->n_glo.getValue();
20  int niDest = axisDestination->n.getValue();
21  int ibeginDest = axisDestination->begin.getValue();
22
23  for (int idx = 0; idx < niDest; ++idx)
24    if ((axisDestination->mask)(idx)) axisDestGlobalIndex_.push_back(ibeginDest+idx);
25}
26
27CAxisAlgorithmTransformation::~CAxisAlgorithmTransformation()
28{
29}
30
31void CAxisAlgorithmTransformation::computeIndexSourceMapping()
32{
33}
34
35/*!
36  Compute an array of global index from a global index on a axis
37  \param[in] axisDestGlobalIndex global index on an axis of destination grid
38  \param[in] axisSrcGlobalIndex global index on an axis of source grid (which are needed by one index on axis destination)
39  \param[in] axisPositionInGrid position of the axis in the grid
40  \param[in] gridDestGlobalDim dimension size of destination grid (it should share the same size for all dimension, maybe except the axis on which transformation is performed)
41  \param[in] globalIndexGridDestSendToServer global index of destination grid which are to be sent to server(s), this array is already acsending sorted
42  \param[in/out] globalIndexDestGrid array of global index (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated
43  \param[in/out] globalIndexSrcGrid array of global index of source grid (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated
44*/
45void CAxisAlgorithmTransformation::computeGlobalGridIndexFromGlobalIndexElement(int axisDestGlobalIndex,
46                                                                          const std::vector<int>& axisSrcGlobalIndex,
47                                                                          int axisPositionInGrid,
48                                                                          const std::vector<int>& gridDestGlobalDim,
49                                                                          const std::vector<int>& gridSrcGlobalDim,
50                                                                          const std::vector<size_t>& globalIndexGridDestSendToServer,
51                                                                          CArray<size_t,1>& globalIndexDestGrid,
52                                                                          std::vector<std::vector<size_t> >& globalIndexSrcGrid)
53{
54  int globalDim = gridDestGlobalDim.size();
55
56  std::vector<size_t> currentIndex(globalDim);
57  std::vector<int> gridAxisGlobalDim(globalDim);
58  std::vector<int> idxLoop(globalDim, 0);
59
60  size_t ssize = 1, idx = 0, realGlobalIndexSize = 0;
61  for (int i = 0; i< globalDim; ++i)
62  {
63    if (axisPositionInGrid == i) gridAxisGlobalDim[i] = 1;
64    else gridAxisGlobalDim[i] = gridDestGlobalDim[i];
65    ssize *= gridAxisGlobalDim[i];
66  }
67
68  std::vector<size_t>::const_iterator itbArr = globalIndexGridDestSendToServer.begin(), itArr,
69                                      iteArr = globalIndexGridDestSendToServer.end();
70
71  while (idx < ssize)
72  {
73    for (int i = 0; i < globalDim-1; ++i)
74    {
75      if (idxLoop[i] == gridAxisGlobalDim[i])
76      {
77        idxLoop[i] = 0;
78        ++idxLoop[i+1];
79      }
80    }
81
82    for (int i = 0; i < globalDim; ++i) currentIndex[i] = idxLoop[i];
83    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
84
85    size_t globIndex = currentIndex[0];
86    size_t mulDim = 1;
87    for (int k = 1; k < globalDim; ++k)
88    {
89      mulDim *= gridDestGlobalDim[k-1];
90      globIndex += (currentIndex[k])*mulDim;
91    }
92
93    if (std::binary_search(itbArr, iteArr, globIndex)) ++realGlobalIndexSize;
94    ++idxLoop[0];
95    ++idx;
96  }
97
98  if (globalIndexDestGrid.numElements() != realGlobalIndexSize)
99    globalIndexDestGrid.resize(realGlobalIndexSize);
100
101  if (realGlobalIndexSize != globalIndexSrcGrid.size()) globalIndexSrcGrid.resize(realGlobalIndexSize);
102  for (int i = 0; i < globalIndexSrcGrid.size(); ++i)
103    if (globalIndexSrcGrid[i].size() != axisSrcGlobalIndex.size())
104      globalIndexSrcGrid[i].resize(axisSrcGlobalIndex.size());
105
106  size_t realGlobalIndex = 0;
107  idx = 0;
108  idxLoop.assign(globalDim, 0);
109  while (idx < ssize)
110  {
111    for (int i = 0; i < globalDim-1; ++i)
112    {
113      if (idxLoop[i] == gridAxisGlobalDim[i])
114      {
115        idxLoop[i] = 0;
116        ++idxLoop[i+1];
117      }
118    }
119
120    for (int i = 0; i < globalDim; ++i) currentIndex[i] = idxLoop[i];
121    currentIndex[axisPositionInGrid] = axisDestGlobalIndex;
122
123    size_t globIndex = currentIndex[0];
124    size_t mulDim = 1;
125    for (int k = 1; k < globalDim; ++k)
126    {
127      mulDim *= gridDestGlobalDim[k-1];
128      globIndex += (currentIndex[k])*mulDim;
129    }
130
131    if (std::binary_search(itbArr, iteArr, globIndex))
132    {
133      globalIndexDestGrid(realGlobalIndex) = globIndex;
134      for (int i = 0; i < globalIndexSrcGrid[realGlobalIndex].size(); ++i)
135      {
136        currentIndex[axisPositionInGrid] = axisSrcGlobalIndex[i];
137        globIndex = currentIndex[0];
138        mulDim = 1;
139        for (int k = 1; k < globalDim; ++k)
140        {
141          mulDim *= gridDestGlobalDim[k-1];
142          globIndex += (currentIndex[k])*mulDim;
143        }
144        (globalIndexSrcGrid[realGlobalIndex])[i] = globIndex;
145      }
146      ++realGlobalIndex;
147    }
148
149    ++idxLoop[0];
150    ++idx;
151  }
152}
153}
Note: See TracBrowser for help on using the repository browser.