1 | /*! |
---|
2 | \file axis_algorithm_extract.cpp |
---|
3 | \brief Algorithm for extracting an axis. |
---|
4 | */ |
---|
5 | #include "axis_algorithm_extract.hpp" |
---|
6 | #include "axis.hpp" |
---|
7 | #include "grid.hpp" |
---|
8 | #include "grid_transformation_factory_impl.hpp" |
---|
9 | #include "extract_axis.hpp" |
---|
10 | |
---|
11 | namespace xios { |
---|
12 | shared_ptr<CGenericAlgorithmTransformation> CAxisAlgorithmExtract::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
13 | CTransformation<CAxis>* transformation, |
---|
14 | int elementPositionInGrid, |
---|
15 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
16 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
17 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
18 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
19 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
20 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
21 | TRY |
---|
22 | { |
---|
23 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
24 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
25 | |
---|
26 | CExtractAxis* extractAxis = dynamic_cast<CExtractAxis*> (transformation); |
---|
27 | int axisDstIndex = elementPositionInGridDst2AxisPosition[elementPositionInGrid]; |
---|
28 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
29 | |
---|
30 | return make_shared<CAxisAlgorithmExtract>(isSource, axisListDestP[axisDstIndex], axisListSrcP[axisSrcIndex], extractAxis); |
---|
31 | } |
---|
32 | CATCH |
---|
33 | |
---|
34 | bool CAxisAlgorithmExtract::dummyRegistered_ = CAxisAlgorithmExtract::registerTrans(); |
---|
35 | bool CAxisAlgorithmExtract::registerTrans() |
---|
36 | TRY |
---|
37 | { |
---|
38 | return CGridTransformationFactory<CAxis>::registerTransformation(TRANS_EXTRACT_AXIS, create); |
---|
39 | } |
---|
40 | CATCH |
---|
41 | |
---|
42 | CAxisAlgorithmExtract::CAxisAlgorithmExtract(bool isSource, CAxis* axisDestination, CAxis* axisSource, CExtractAxis* extractAxis) |
---|
43 | : CAlgorithmTransformationTransfer(isSource), axisDest_(axisDestination), axisSrc_(axisSource) |
---|
44 | TRY |
---|
45 | { |
---|
46 | axisDestination->axis_type.reset(); |
---|
47 | axisDestination->n_glo.reset(); |
---|
48 | axisDestination->index.reset(); |
---|
49 | axisDestination->n.reset(); |
---|
50 | axisDestination->begin.reset(); |
---|
51 | |
---|
52 | axisDestination->mask.reset(); |
---|
53 | axisDestination->data_index.reset(); |
---|
54 | axisDestination->data_n.reset(); |
---|
55 | axisDestination->data_begin.reset(); |
---|
56 | |
---|
57 | axisDestination->value.reset(); |
---|
58 | axisDestination->label.reset(); |
---|
59 | axisDestination->bounds.reset(); |
---|
60 | |
---|
61 | extractAxis->checkValid(axisSource); |
---|
62 | extractBegin_ = extractAxis->begin.getValue(); |
---|
63 | extractN_ = extractAxis->n.getValue(); |
---|
64 | extractEnd_ = extractBegin_ + extractN_ - 1; |
---|
65 | |
---|
66 | if (extractN_ > axisSource->n_glo.getValue()) |
---|
67 | { |
---|
68 | ERROR("CAxisAlgorithmExtract::CAxisAlgorithmExtract(CAxis* axisDestination, CAxis* axisSource, CExtractAxis* extractAxis)", |
---|
69 | << "Extract size is greater than global size of source axis" |
---|
70 | << "Global size of source axis " <<axisSource->getId() << " is " << axisSource->n_glo.getValue() << std::endl |
---|
71 | << "Extract size is " << extractN_ ); |
---|
72 | } |
---|
73 | |
---|
74 | int idxSrc, nDest = 0, beginDestLoc, beginDestGlo = 0 ; |
---|
75 | int indGloDest, indGloSrc, iSrc; |
---|
76 | for (int i = 0; i < axisSrc_->n.getValue(); i++) |
---|
77 | { |
---|
78 | idxSrc = axisSrc_->index(i); |
---|
79 | if ((idxSrc >= extractBegin_) && (idxSrc <= extractEnd_)) |
---|
80 | { |
---|
81 | if (nDest == 0) beginDestLoc = i; |
---|
82 | ++nDest; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | axisDest_->n_glo.setValue(extractN_); |
---|
87 | axisDest_->index.resize(nDest); |
---|
88 | if (nDest==0) |
---|
89 | { |
---|
90 | axisDest_->n.setValue( 0 ); |
---|
91 | axisDest_->begin.setValue( 0 ); |
---|
92 | } |
---|
93 | |
---|
94 | if (axisSrc_->hasValue) axisDest_->value.resize(nDest); |
---|
95 | if (axisSrc_->hasLabel) axisDest_->label.resize(nDest); |
---|
96 | if (axisSrc_->hasBounds) axisDest_->bounds.resize(2,nDest); |
---|
97 | |
---|
98 | auto& transMap = this->transformationMapping_; |
---|
99 | |
---|
100 | // Set attributes required to define domainDestination->localElement_ and associated views, full and workflow) |
---|
101 | CArray<size_t,1> sourceGlobalIdx = axisSource->getLocalElement()->getGlobalIndex(); |
---|
102 | int indexSize = sourceGlobalIdx.numElements(); |
---|
103 | |
---|
104 | CArray<int,1> sourceWorkflowIdx = axisSource->getLocalView(CElementView::WORKFLOW)->getIndex(); |
---|
105 | int srcWorkflowSize = sourceWorkflowIdx.numElements(); |
---|
106 | axisDest_->data_index.resize(nDest); |
---|
107 | axisDest_->data_index = -1; |
---|
108 | |
---|
109 | int idxMin = INT_MAX; |
---|
110 | for (int countSrc = 0; countSrc < indexSize ; ++countSrc) |
---|
111 | { |
---|
112 | if ( sourceGlobalIdx(countSrc) < idxMin ) |
---|
113 | idxMin = sourceGlobalIdx(countSrc); |
---|
114 | } |
---|
115 | |
---|
116 | int countDest(0); // increment of the position in destination domain |
---|
117 | for (int countSrc = 0; countSrc < indexSize ; ++countSrc) |
---|
118 | { |
---|
119 | int idxSrc = sourceGlobalIdx(countSrc); |
---|
120 | if ( (idxSrc >= extractBegin_) && (idxSrc <= extractEnd_) ) |
---|
121 | { |
---|
122 | axisDest_->index(countDest) = idxSrc-extractBegin_; |
---|
123 | |
---|
124 | // ------------------ define transformation only if in the WF ------------------ |
---|
125 | int iIdxSrc2 = (countSrc+idxMin)%axisSource->n_glo; |
---|
126 | int convert_locally_global_idx = (iIdxSrc2-idxMin) ; |
---|
127 | bool concerned_by_WF(false); |
---|
128 | for ( int i = 0 ; i<sourceWorkflowIdx.numElements() ; ++i ) |
---|
129 | { |
---|
130 | if (sourceWorkflowIdx(i)==convert_locally_global_idx) |
---|
131 | { |
---|
132 | concerned_by_WF = true; |
---|
133 | break; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if (concerned_by_WF) |
---|
138 | { |
---|
139 | transformationMapping_[idxSrc-extractBegin_]=sourceGlobalIdx(countSrc); |
---|
140 | axisDest_->data_index( countDest ) = countDest; |
---|
141 | } |
---|
142 | // ----------------------------------------------------------------------------- |
---|
143 | |
---|
144 | if (axisSrc_->hasValue) |
---|
145 | { |
---|
146 | axisDest_->value(countDest) = axisSrc_->value(countSrc); |
---|
147 | } |
---|
148 | if (axisSrc_->hasLabel) |
---|
149 | { |
---|
150 | axisDest_->label(countDest) = axisSrc_->label(countSrc); |
---|
151 | } |
---|
152 | if (axisSrc_->hasBounds) |
---|
153 | { |
---|
154 | axisDest_->bounds(0,countDest) = axisSrc_->bounds(0,countSrc); |
---|
155 | axisDest_->bounds(1,countDest) = axisSrc_->bounds(1,countSrc); |
---|
156 | } |
---|
157 | |
---|
158 | // if point i has been identified as extracted, increment position in destination domain for the next point |
---|
159 | countDest++; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | axisDestination->checkAttributes() ; |
---|
164 | |
---|
165 | this->computeAlgorithm(axisSource->getLocalView(CElementView::WORKFLOW), axisDestination->getLocalView(CElementView::WORKFLOW)) ; |
---|
166 | } |
---|
167 | CATCH |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | } |
---|