source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/scalar_algorithm/scalar_algorithm_reduce_scalar.cpp @ 2291

Last change on this file since 2291 was 2291, checked in by ymipsl, 2 years ago

Improve reduction transformation

  • make the difference between reduction over geometry or reduction between process.
  • geometrical reduction :

domain -> axis
axis -> scalar
domain -> scalar

  • reduction across processes for redondant geometrical cell :

axis -> axis
scalar -> scalar

Reduction can be local (only for the geometrical cell owned by current process) or global, using the "local" attribute (bool) over the reduction.

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.6 KB
Line 
1/*!
2   \file scalar_algorithm_reduce_scalar.cpp
3 
4   \brief Algorithm for reduce an scalar to a scalar
5 */
6#include "scalar_algorithm_reduce_scalar.hpp"
7#include "scalar.hpp"
8#include "reduce_scalar_to_scalar.hpp"
9#include "grid.hpp"
10#include "grid_transformation_factory_impl.hpp"
11#include "reduction.hpp"
12#include "grid_algorithm_reduce.hpp"
13
14
15
16namespace xios {
17shared_ptr<CGenericAlgorithmTransformation> CScalarAlgorithmReduceScalar::create(bool isSource, CGrid* gridDst, CGrid* gridSrc,
18                                                                     CTransformation<CScalar>* transformation,
19                                                                     int elementPositionInGrid,
20                                                                     std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
21                                                                     std::map<int, int>& elementPositionInGridSrc2AxisPosition,
22                                                                     std::map<int, int>& elementPositionInGridSrc2DomainPosition,
23                                                                     std::map<int, int>& elementPositionInGridDst2ScalarPosition,
24                                                                     std::map<int, int>& elementPositionInGridDst2AxisPosition,
25                                                                     std::map<int, int>& elementPositionInGridDst2DomainPosition)
26TRY
27{
28  std::vector<CScalar*> scalarListDestP = gridDst->getScalars();
29  std::vector<CScalar*> scalarListSrcP  = gridSrc->getScalars();
30
31  CReduceScalarToScalar* reduceScalar = dynamic_cast<CReduceScalarToScalar*> (transformation);
32  int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid];
33  int scalarSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid];
34
35  return make_shared<CScalarAlgorithmReduceScalar>(isSource, scalarListDestP[scalarDstIndex], scalarListSrcP[scalarSrcIndex], reduceScalar);
36}
37CATCH
38
39bool CScalarAlgorithmReduceScalar::dummyRegistered_ = CScalarAlgorithmReduceScalar::registerTrans();
40bool CScalarAlgorithmReduceScalar::registerTrans()
41TRY
42{
43  return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_SCALAR_TO_SCALAR, create);
44}
45CATCH
46
47CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(bool isSource, CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)
48 : CAlgorithmTransformationReduce(isSource)
49TRY
50{
51  scalarDestination->checkAttributes() ;
52  if (algo->operation.isEmpty())
53    ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
54           << "Operation must be defined."
55           << "Scalar source " <<scalarSource->getId() << std::endl
56           << "Scalar destination " << scalarDestination->getId());
57  StdString op;
58  switch (algo->operation)
59  {
60    case CReduceScalarToScalar::operation_attr::sum:
61      operator_ = EReduction::sum;
62      break;
63    case CReduceScalarToScalar::operation_attr::min:
64      operator_ = EReduction::min;
65      break;
66    case CReduceScalarToScalar::operation_attr::max:
67      operator_ = EReduction::max;
68      break;
69    case CReduceScalarToScalar::operation_attr::average:
70      operator_ = EReduction::average;
71      break;
72    default:
73        ERROR("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",
74         << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl
75         << "Scalar source " <<scalarSource->getId() << std::endl
76         << "Scalar destination " << scalarDestination->getId());
77
78  }
79 
80  auto& transMap = this->transformationMapping_;
81 
82  CArray<size_t,1> dstGlobalIndex ;
83  scalarDestination->getLocalView(CElementView::WORKFLOW)->getGlobalIndexView(dstGlobalIndex) ;
84  size_t nbIdx = dstGlobalIndex.numElements();
85
86  for (size_t idx = 0; idx < nbIdx; ++idx)
87  {
88    size_t globalIdx = dstGlobalIndex(idx);
89    transMap[globalIdx].resize(1);
90    transMap[globalIdx][0]=globalIdx ;     
91  }
92
93  this->computeAlgorithm(scalarSource->getLocalView(CElementView::WORKFLOW), scalarDestination->getLocalView(CElementView::WORKFLOW)) ; 
94}
95CATCH
96
97shared_ptr<CGridAlgorithm> CScalarAlgorithmReduceScalar::createGridAlgorithm(CGrid* gridSrc, CGrid* gridDst, int pos)
98{
99  auto algo=make_shared<CGridAlgorithmReduce>(gridSrc, gridDst, pos, shared_from_this(), operator_) ;
100  algo->computeAlgorithm(false) ;
101  return algo ; 
102}
103
104CScalarAlgorithmReduceScalar::~CScalarAlgorithmReduceScalar()
105TRY
106{
107}
108CATCH
109
110
111}
Note: See TracBrowser for help on using the repository browser.