source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/domain_algorithm/domain_algorithm_extract.cpp @ 2270

Last change on this file since 2270 was 2270, checked in by ymipsl, 3 years ago

Tracking memory leak :
Tranformations and algorithms are now managed with shared_ptr.

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.8 KB
Line 
1#include "domain_algorithm_extract.hpp"
2#include "extract_domain.hpp"
3#include "domain.hpp"
4#include "grid.hpp"
5#include "grid_transformation_factory_impl.hpp"
6#include "attribute_template.hpp"
7
8namespace xios {
9shared_ptr<CGenericAlgorithmTransformation> CDomainAlgorithmExtract::create(bool isSource, CGrid* gridDst, CGrid* gridSrc,
10                                                             CTransformation<CDomain>* transformation,
11                                                             int elementPositionInGrid,
12                                                             std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
13                                                             std::map<int, int>& elementPositionInGridSrc2AxisPosition,
14                                                             std::map<int, int>& elementPositionInGridSrc2DomainPosition,
15                                                             std::map<int, int>& elementPositionInGridDst2ScalarPosition,
16                                                             std::map<int, int>& elementPositionInGridDst2AxisPosition,
17                                                             std::map<int, int>& elementPositionInGridDst2DomainPosition)
18TRY
19{
20  std::vector<CDomain*> domainListDestP = gridDst->getDomains();
21  std::vector<CDomain*> domainListSrcP  = gridSrc->getDomains();
22
23  CExtractDomain* extractDomain = dynamic_cast<CExtractDomain*> (transformation);
24  int domainDstIndex = elementPositionInGridDst2DomainPosition[elementPositionInGrid];
25  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
26
27  return make_shared<CDomainAlgorithmExtract>(isSource, domainListDestP[domainDstIndex], domainListSrcP[domainSrcIndex], extractDomain);
28}
29CATCH
30
31bool CDomainAlgorithmExtract::dummyRegistered_ = CDomainAlgorithmExtract::registerTrans();
32bool CDomainAlgorithmExtract::registerTrans()
33TRY
34{
35  return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_EXTRACT_DOMAIN, create);
36}
37CATCH
38
39CDomainAlgorithmExtract::CDomainAlgorithmExtract(bool isSource, CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain)
40: CAlgorithmTransformationTransfer(isSource), domainSrc_(domainSource), domainDest_(domainDestination)
41TRY
42{
43   // Reset geometrical attributes to avoid incompatible (user/domainSource) attributs
44   //   attributs will be defined using domainSource and/or transformation attributs
45   domainDestination->type.reset();
46   domainDestination->ni_glo.reset();
47   domainDestination->nj_glo.reset();
48   
49   domainDestination->i_index.reset();   // defined using domainSource->getLocalElement()
50   domainDestination->j_index.reset();   // "
51   domainDestination->ibegin.reset();    // will be computed in domainDestination->checkDomain() (from checkAttributes())
52   domainDestination->ni.reset();        // "
53   domainDestination->jbegin.reset();    // "
54   domainDestination->nj.reset();        // "
55   
56   domainDestination->mask_1d.reset();   // defined scanning domainSource->getFullView() & domainSource->getWorkflowView() differencies
57   domainDestination->mask_2d.reset();   //   in all case domainDestination->mask_1d used as reference 
58
59   // domainDestination->data_* attributes will be computed in :
60   domainDestination->data_dim.reset();     // domainDestination->checkDomainData() (from checkAttributes())
61   domainDestination->data_ni.reset();
62   domainDestination->data_nj.reset();
63   domainDestination->data_ibegin.reset();
64   domainDestination->data_jbegin.reset();
65   domainDestination->data_i_index.reset(); // domainDestination->checkCompression() (from checkAttributes())
66   domainDestination->data_j_index.reset();
67
68   // Next attributes will be set using domainSource->attributes
69   domainDestination->lonvalue_1d.reset();
70   domainDestination->latvalue_1d.reset();
71   domainDestination->lonvalue_2d.reset();
72   domainDestination->latvalue_2d.reset();
73   domainDestination->nvertex.reset();
74   domainDestination->bounds_lon_1d.reset();
75   domainDestination->bounds_lat_1d.reset();
76   domainDestination->bounds_lon_2d.reset();
77   domainDestination->bounds_lat_2d.reset();
78   domainDestination->area.reset();
79   domainDestination->radius.reset();
80   
81 
82  extractDomain->checkValid(domainSource);
83  extractIBegin_ = extractDomain->ibegin.getValue();
84  extractJBegin_ = extractDomain->jbegin.getValue();
85
86  extractNi_  = extractDomain->ni.getValue();
87  extractNj_  = extractDomain->nj.getValue();
88
89  extractIEnd_ = extractIBegin_ + extractNi_ - 1;
90  extractJEnd_ = extractJBegin_ + extractNj_ - 1;
91
92  if (extractNi_ > domainSource->ni_glo.getValue())
93  {
94    ERROR("CDomainAlgorithmExtract::CDomainAlgorithmExtract(CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain)",
95           << "Extract size is greater than size of domain source"
96           << "Size ni_glo of domain source " <<domainSource->getId() << " is " << domainSource->ni_glo.getValue()  << std::endl
97           << "Extract size is " << extractNi_ );
98  }
99
100  if (extractNj_ > domainSource->nj_glo.getValue())
101  {
102    ERROR("CDomainAlgorithmExtract::CDomainAlgorithmExtract(CDomain* domainDestination, CDomain* domainSource, CExtractDomain* extractDomain)",
103           << "Extract size is greater than size of domain source"
104           << "Size nj_glo of domain source " <<domainSource->getId() << " is " << domainSource->nj_glo.getValue()  << std::endl
105           << "Extract size is " << extractNj_ );
106  }
107
108  // Calculate the size of local domain
109  int ind, indLocSrc, indLocDest, iIdxSrc, jIdxSrc, destIBegin = -1, destJBegin = -1, niDest = 0, njDest = 0 ;
110  int indGloDest, indGloSrc, niGloSrc = domainSrc_->ni_glo, iSrc, jSrc;
111  for (int j = 0; j < domainSrc_->nj.getValue(); j++)
112  {
113    for (int i = 0; i < domainSrc_->ni.getValue(); i++)
114    {
115      ind = j*domainSrc_->ni + i;
116      iIdxSrc = domainSrc_->i_index(ind);
117      if ((iIdxSrc >= extractIBegin_) && (iIdxSrc <= extractIEnd_))
118      {
119        jIdxSrc = domainSrc_->j_index(ind);
120        if ((jIdxSrc >= extractJBegin_) && (jIdxSrc <= extractJEnd_))
121        {
122          if ((niDest == 0) && (njDest == 0))
123          {
124            destIBegin = i;
125            destJBegin = j;
126          }
127          if (i == destIBegin) ++njDest;
128        }
129        if (j == destJBegin) ++niDest;
130
131      }
132    }
133  }
134
135   
136  // Set attributes for this transformation
137  domainDest_->type = domainSrc_ -> type ;
138  domainDest_->ni_glo.setValue(extractNi_);
139  domainDest_->nj_glo.setValue(extractNj_);
140  domainDest_->i_index.resize(niDest*njDest);
141  domainDest_->j_index.resize(niDest*njDest);
142
143  // Resize lon/lat, bounds, area arrays to local domain dimensions
144  if (!domainSrc_->lonvalue_1d.isEmpty())
145  {
146    if (domainDest_->type == CDomain::type_attr::rectilinear)
147    {
148      domainDest_->lonvalue_1d.resize(niDest);
149      domainDest_->latvalue_1d.resize(njDest);
150    }
151    else if (domainDest_->type == CDomain::type_attr::unstructured)
152    {
153      domainDest_->lonvalue_1d.resize(niDest);
154      domainDest_->latvalue_1d.resize(niDest);
155    }
156  }
157  else if (!domainSrc_->lonvalue_2d.isEmpty())
158  {
159    domainDest_->lonvalue_2d.resize(niDest,njDest);
160    domainDest_->latvalue_2d.resize(niDest,njDest);
161  }
162  if (domainSrc_->hasBounds)
163  {
164    if (!domainSrc_->bounds_lon_2d.isEmpty())
165    {
166      domainDest_->bounds_lon_2d.resize(domainDest_->nvertex, niDest, njDest);
167      domainDest_->bounds_lon_2d.resize(domainDest_->nvertex, niDest, njDest);
168    }
169    else if (!domainSrc_->bounds_lon_1d.isEmpty())
170    {
171      domainDest_->bounds_lon_1d.resize(domainDest_->nvertex, niDest);
172      domainDest_->bounds_lon_1d.resize(domainDest_->nvertex, niDest);
173    }
174  }
175  if (domainSrc_->hasArea) domainDest_->area.resize(niDest,njDest);
176
177  // Set attributes required to define domainDestination->localElement_ and associated views, full and workflow)
178  CArray<size_t,1> sourceGlobalIdx = domainSource->getLocalElement()->getGlobalIndex();
179  int indexSize = sourceGlobalIdx.numElements();
180  domainDest_->data_i_index.resize(niDest*njDest);
181  domainDestination->data_i_index = -1; 
182  domainDest_->data_j_index.resize(niDest*njDest);
183  domainDestination->data_j_index = 0; 
184
185  CArray<int,1> sourceWorkflowIdx = domainSource->getLocalView(CElementView::WORKFLOW)->getIndex();
186  int srcWorkflowSize = sourceWorkflowIdx.numElements();
187
188  int iIdxSrcMin = INT_MAX;
189  int jIdxSrcMin = INT_MAX;
190  int IdxMin = INT_MAX;
191  for (int countSrc = 0; countSrc < indexSize ; ++countSrc)
192  {
193    if ( sourceGlobalIdx(countSrc)%domainSource->ni_glo < iIdxSrcMin )
194      iIdxSrcMin = sourceGlobalIdx(countSrc)%domainSource->ni_glo;
195    if ( sourceGlobalIdx(countSrc)/domainSource->ni_glo < jIdxSrcMin )
196      jIdxSrcMin = sourceGlobalIdx(countSrc)/domainSource->ni_glo;
197    if ( sourceGlobalIdx(countSrc) < IdxMin )
198      IdxMin = sourceGlobalIdx(countSrc);
199  }
200 
201  int countDest(0); // increment of the position in destination domain
202  for (int countSrc = 0; countSrc < indexSize ; ++countSrc)
203  {
204    int iIdxSrc = sourceGlobalIdx(countSrc)%domainSource->ni_glo;
205    int jIdxSrc = sourceGlobalIdx(countSrc)/domainSource->ni_glo;
206    // check that point countSrc concerned by extract
207    if ( (iIdxSrc >= extractIBegin_) && (iIdxSrc <= extractIEnd_)
208         && (jIdxSrc >= extractJBegin_) && (jIdxSrc <= extractJEnd_) )
209    {
210      // if concerned, convert source the global indexation in the extracted frame
211      domainDest_->i_index(countDest) = iIdxSrc-extractIBegin_;
212      domainDest_->j_index(countDest) = jIdxSrc-extractJBegin_;
213
214      // ------------------ define transformation only if in the WF ------------------
215      // countSrc+IdxMin is the global position (not index) considering the distributed memory
216      //     - can be compared to the workflow view
217      int iIdxSrc2 = (countSrc+IdxMin)%domainSource->ni_glo;
218      int jIdxSrc2 = (countSrc+IdxMin)/domainSource->ni_glo;
219      int convert_locally_global_idx = (jIdxSrc2-jIdxSrcMin)*domainSource->ni + (iIdxSrc2-iIdxSrcMin) ;
220      bool concerned_by_WF(false);
221      for ( int i = 0 ; i<sourceWorkflowIdx.numElements() ; ++i )
222      {
223        if (sourceWorkflowIdx(i)==convert_locally_global_idx)
224        {     
225          concerned_by_WF = true;
226          break;
227        }
228      }
229      if (concerned_by_WF)
230      {
231        transformationMapping_[extractNi_*(jIdxSrc-extractJBegin_)+iIdxSrc-extractIBegin_]=sourceGlobalIdx(countSrc);
232        domainDest_->data_i_index( countDest ) = countDest;
233      }
234      // -----------------------------------------------------------------------------
235
236      int iIdxDestLocal = countDest%niDest;
237      int jIdxDestLocal = countDest/niDest;
238      int iIdxSrcLocal  = countSrc%domainSource->ni;
239      int jIdxSrcLocal  = countSrc/domainSource->ni;
240
241      // area
242      if (!domainSrc_->area.isEmpty())
243      {
244        domainDest_->area(iIdxDestLocal,jIdxDestLocal) = domainSrc_->area(iIdxSrcLocal,jIdxSrcLocal);
245      }
246
247      // bounds
248      if (!domainDest_->bounds_lon_1d.isEmpty())
249      {
250        for (int n = 0; n < domainSrc_->nvertex; ++n)
251        {
252          domainDest_->bounds_lon_1d(n, countDest) = domainSrc_->bounds_lon_1d(n,countSrc);
253          domainDest_->bounds_lat_1d(n, countDest) = domainSrc_->bounds_lat_1d(n,countSrc);
254        }
255      }
256      else if (!domainDest_->bounds_lon_2d.isEmpty())
257      {
258        for (int n = 0; n < domainSrc_->nvertex; ++n)
259        {
260          domainDest_->bounds_lon_2d(n, iIdxDestLocal, jIdxDestLocal) = domainSrc_->bounds_lon_2d(n, iIdxSrcLocal, jIdxSrcLocal);
261          domainDest_->bounds_lat_2d(n, iIdxDestLocal, jIdxDestLocal) = domainSrc_->bounds_lat_2d(n, iIdxSrcLocal, jIdxSrcLocal);
262        }
263      }
264
265      // lon/lat
266      if (!domainDest_->lonvalue_1d.isEmpty())
267      {
268        if (domainDest_->type == CDomain::type_attr::rectilinear)
269        {
270          // i : scan nbr of points in src
271          domainDest_->lonvalue_1d(iIdxDestLocal) = domainSrc_->lonvalue_1d(iIdxSrcLocal);
272          domainDest_->latvalue_1d(jIdxDestLocal) = domainSrc_->latvalue_1d(jIdxSrcLocal);
273        }
274        else if (domainDest_->type == CDomain::type_attr::unstructured)
275        {
276          domainDest_->lonvalue_1d(countDest) = domainSrc_->lonvalue_1d(countSrc);
277          domainDest_->latvalue_1d(countDest) = domainSrc_->latvalue_1d(countSrc);
278        }
279      }
280      else if (!domainDest_->lonvalue_2d.isEmpty())
281      {
282        if (domainDest_->type == CDomain::type_attr::curvilinear)
283        {
284          domainDest_->lonvalue_2d(iIdxDestLocal, jIdxDestLocal) = domainSrc_->lonvalue_2d(iIdxSrcLocal,jIdxSrcLocal);
285          domainDest_->latvalue_2d(iIdxDestLocal, jIdxDestLocal) = domainSrc_->latvalue_2d(iIdxSrcLocal,jIdxSrcLocal);
286        }
287      }
288     
289      // if point i has been identified as extracted, increment position in destination domain for the next point
290      countDest++;
291    }
292
293  }
294 
295  domainDestination->checkAttributes() ;
296  this->computeAlgorithm(domainSource->getLocalView(CElementView::WORKFLOW), domainDestination->getLocalView(CElementView::WORKFLOW)) ;
297}
298CATCH
299
300
301}
Note: See TracBrowser for help on using the repository browser.