[630] | 1 | /*! |
---|
| 2 | \file axis_algorithm_interpolate.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 23 June 2015 |
---|
| 5 | \date 02 Jul 2015 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for interpolation on an axis. |
---|
| 8 | */ |
---|
| 9 | #include "axis_algorithm_interpolate.hpp" |
---|
| 10 | #include <algorithm> |
---|
| 11 | #include "context.hpp" |
---|
| 12 | #include "context_client.hpp" |
---|
| 13 | #include "utils.hpp" |
---|
[827] | 14 | #include "grid.hpp" |
---|
| 15 | #include "distribution_client.hpp" |
---|
[630] | 16 | |
---|
| 17 | namespace xios { |
---|
| 18 | |
---|
| 19 | CAxisAlgorithmInterpolate::CAxisAlgorithmInterpolate(CAxis* axisDestination, CAxis* axisSource, CInterpolateAxis* interpAxis) |
---|
[827] | 20 | : CAxisAlgorithmTransformation(axisDestination, axisSource), coordinate_(), transPosition_() |
---|
[630] | 21 | { |
---|
| 22 | interpAxis->checkValid(axisSource); |
---|
| 23 | order_ = interpAxis->order.getValue(); |
---|
[827] | 24 | if (!interpAxis->coordinate.isEmpty()) |
---|
[630] | 25 | { |
---|
[827] | 26 | coordinate_ = interpAxis->coordinate.getValue(); |
---|
| 27 | this->idAuxInputs_.resize(1); |
---|
| 28 | this->idAuxInputs_[0] = coordinate_; |
---|
[630] | 29 | } |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | /*! |
---|
| 33 | Compute the index mapping between axis on grid source and one on grid destination |
---|
| 34 | */ |
---|
[827] | 35 | void CAxisAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[630] | 36 | { |
---|
| 37 | CContext* context = CContext::getCurrent(); |
---|
| 38 | CContextClient* client=context->client; |
---|
| 39 | int nbClient = client->clientSize; |
---|
[827] | 40 | CArray<bool,1>& axisMask = axisSrc_->mask; |
---|
[666] | 41 | int srcSize = axisSrc_->n_glo.getValue(); |
---|
[827] | 42 | std::vector<CArray<double,1> > vecAxisValue; |
---|
[630] | 43 | |
---|
[827] | 44 | // Fill in axis value from coordinate |
---|
| 45 | fillInAxisValue(vecAxisValue, dataAuxInputs); |
---|
[896] | 46 | std::vector<double> valueSrc(srcSize); |
---|
| 47 | std::vector<double> recvBuff(srcSize); |
---|
| 48 | std::vector<int> indexVec(srcSize); |
---|
[630] | 49 | |
---|
[827] | 50 | for (int idx = 0; idx < vecAxisValue.size(); ++idx) |
---|
| 51 | { |
---|
| 52 | CArray<double,1>& axisValue = vecAxisValue[idx]; |
---|
| 53 | retrieveAllAxisValue(axisValue, axisMask, recvBuff, indexVec); |
---|
| 54 | XIOSAlgorithms::sortWithIndex<double, CVectorStorage>(recvBuff, indexVec); |
---|
[896] | 55 | for (int i = 0; i < srcSize; ++i) valueSrc[i] = recvBuff[indexVec[i]]; |
---|
[912] | 56 | computeInterpolantPoint(valueSrc, indexVec, idx); |
---|
[827] | 57 | } |
---|
[630] | 58 | } |
---|
| 59 | |
---|
| 60 | /*! |
---|
| 61 | Compute the interpolant points |
---|
| 62 | Assume that we have all value of axis source, with these values, need to calculate weight (coeff) of Lagrange polynomial |
---|
| 63 | \param [in] axisValue all value of axis source |
---|
[896] | 64 | \param [in] tranPos position of axis on a domain |
---|
[630] | 65 | */ |
---|
[912] | 66 | void CAxisAlgorithmInterpolate::computeInterpolantPoint(const std::vector<double>& axisValue, |
---|
| 67 | const std::vector<int>& indexVec, |
---|
| 68 | int transPos) |
---|
[630] | 69 | { |
---|
| 70 | std::vector<double>::const_iterator itb = axisValue.begin(), ite = axisValue.end(); |
---|
| 71 | std::vector<double>::const_iterator itLowerBound, itUpperBound, it; |
---|
| 72 | const double sfmax = NumTraits<double>::sfmax(); |
---|
| 73 | |
---|
[666] | 74 | int ibegin = axisDest_->begin.getValue(); |
---|
[630] | 75 | CArray<double,1>& axisDestValue = axisDest_->value; |
---|
| 76 | int numValue = axisDestValue.numElements(); |
---|
| 77 | std::map<int, std::vector<std::pair<int,double> > > interpolatingIndexValues; |
---|
| 78 | |
---|
| 79 | for (int idx = 0; idx < numValue; ++idx) |
---|
| 80 | { |
---|
| 81 | double destValue = axisDestValue(idx); |
---|
| 82 | itLowerBound = std::lower_bound(itb, ite, destValue); |
---|
| 83 | itUpperBound = std::upper_bound(itb, ite, destValue); |
---|
| 84 | if ((ite != itUpperBound) && (sfmax == *itUpperBound)) itUpperBound = ite; |
---|
| 85 | |
---|
| 86 | // If the value is not in the range, that means we'll do extra-polation |
---|
| 87 | if (ite == itLowerBound) // extra-polation |
---|
| 88 | { |
---|
| 89 | itLowerBound = itb; |
---|
| 90 | itUpperBound = itb + order_+1; |
---|
| 91 | } |
---|
| 92 | else if (ite == itUpperBound) // extra-polation |
---|
| 93 | { |
---|
| 94 | itLowerBound = itUpperBound - order_-1; |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | if (itb != itLowerBound) --itLowerBound; |
---|
| 99 | if (ite != itUpperBound) ++itUpperBound; |
---|
| 100 | int order = (order_ + 1) - 2; |
---|
| 101 | bool down = true; |
---|
| 102 | for (int k = 0; k < order; ++k) |
---|
| 103 | { |
---|
| 104 | if ((itb != itLowerBound) && down) |
---|
| 105 | { |
---|
| 106 | --itLowerBound; |
---|
| 107 | down = false; |
---|
| 108 | continue; |
---|
| 109 | } |
---|
| 110 | if ((ite != itUpperBound) && (sfmax != *itUpperBound)) |
---|
| 111 | { |
---|
| 112 | ++itUpperBound; |
---|
| 113 | down = true; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | for (it = itLowerBound; it != itUpperBound; ++it) |
---|
| 119 | { |
---|
| 120 | int index = std::distance(itb, it); |
---|
[912] | 121 | interpolatingIndexValues[idx+ibegin].push_back(make_pair(indexVec[index],*it)); |
---|
[630] | 122 | } |
---|
| 123 | } |
---|
[827] | 124 | computeWeightedValueAndMapping(interpolatingIndexValues, transPos); |
---|
[630] | 125 | } |
---|
| 126 | |
---|
| 127 | /*! |
---|
| 128 | Compute weight (coeff) of Lagrange's polynomial |
---|
| 129 | \param [in] interpolatingIndexValues the necessary axis value to calculate the coeffs |
---|
| 130 | */ |
---|
[827] | 131 | void CAxisAlgorithmInterpolate::computeWeightedValueAndMapping(const std::map<int, std::vector<std::pair<int,double> > >& interpolatingIndexValues, int transPos) |
---|
[630] | 132 | { |
---|
[833] | 133 | TransformationIndexMap& transMap = this->transformationMapping_[transPos]; |
---|
| 134 | TransformationWeightMap& transWeight = this->transformationWeight_[transPos]; |
---|
[630] | 135 | std::map<int, std::vector<std::pair<int,double> > >::const_iterator itb = interpolatingIndexValues.begin(), it, |
---|
| 136 | ite = interpolatingIndexValues.end(); |
---|
[666] | 137 | int ibegin = axisDest_->begin.getValue(); |
---|
[630] | 138 | for (it = itb; it != ite; ++it) |
---|
| 139 | { |
---|
| 140 | int globalIndexDest = it->first; |
---|
| 141 | double localValue = axisDest_->value(globalIndexDest - ibegin); |
---|
| 142 | const std::vector<std::pair<int,double> >& interpVal = it->second; |
---|
| 143 | int interpSize = interpVal.size(); |
---|
[827] | 144 | transMap[globalIndexDest].resize(interpSize); |
---|
| 145 | transWeight[globalIndexDest].resize(interpSize); |
---|
[630] | 146 | for (int idx = 0; idx < interpSize; ++idx) |
---|
| 147 | { |
---|
| 148 | int index = interpVal[idx].first; |
---|
| 149 | double weight = 1.0; |
---|
| 150 | |
---|
| 151 | for (int k = 0; k < interpSize; ++k) |
---|
| 152 | { |
---|
| 153 | if (k == idx) continue; |
---|
| 154 | weight *= (localValue - interpVal[k].second); |
---|
| 155 | weight /= (interpVal[idx].second - interpVal[k].second); |
---|
| 156 | } |
---|
[827] | 157 | transMap[globalIndexDest][idx] = index; |
---|
| 158 | transWeight[globalIndexDest][idx] = weight; |
---|
| 159 | if (!transPosition_.empty()) |
---|
| 160 | { |
---|
| 161 | (this->transformationPosition_[transPos])[globalIndexDest] = transPosition_[transPos]; |
---|
| 162 | } |
---|
[630] | 163 | } |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /*! |
---|
| 168 | Each client retrieves all values of an axis |
---|
| 169 | \param [in/out] recvBuff buffer for receiving values (already allocated) |
---|
| 170 | \param [in/out] indexVec mapping between values and global index of axis |
---|
| 171 | */ |
---|
[827] | 172 | void CAxisAlgorithmInterpolate::retrieveAllAxisValue(const CArray<double,1>& axisValue, const CArray<bool,1>& axisMask, |
---|
| 173 | std::vector<double>& recvBuff, std::vector<int>& indexVec) |
---|
[630] | 174 | { |
---|
| 175 | CContext* context = CContext::getCurrent(); |
---|
| 176 | CContextClient* client=context->client; |
---|
| 177 | int nbClient = client->clientSize; |
---|
| 178 | |
---|
[666] | 179 | int srcSize = axisSrc_->n_glo.getValue(); |
---|
[630] | 180 | int numValue = axisValue.numElements(); |
---|
| 181 | |
---|
| 182 | if (srcSize == numValue) // Only one client or axis not distributed |
---|
| 183 | { |
---|
| 184 | for (int idx = 0; idx < srcSize; ++idx) |
---|
| 185 | { |
---|
| 186 | if (axisMask(idx)) |
---|
| 187 | { |
---|
| 188 | recvBuff[idx] = axisValue(idx); |
---|
| 189 | indexVec[idx] = idx; |
---|
| 190 | } |
---|
[896] | 191 | else |
---|
| 192 | { |
---|
| 193 | recvBuff[idx] = NumTraits<double>::sfmax(); |
---|
| 194 | indexVec[idx] = -1; |
---|
| 195 | } |
---|
[630] | 196 | } |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | else // Axis distributed |
---|
| 200 | { |
---|
| 201 | double* sendValueBuff = new double [numValue]; |
---|
| 202 | int* sendIndexBuff = new int [numValue]; |
---|
| 203 | int* recvIndexBuff = new int [srcSize]; |
---|
| 204 | |
---|
[666] | 205 | int ibegin = axisSrc_->begin.getValue(); |
---|
[630] | 206 | for (int idx = 0; idx < numValue; ++idx) |
---|
| 207 | { |
---|
| 208 | if (axisMask(idx)) |
---|
| 209 | { |
---|
| 210 | sendValueBuff[idx] = axisValue(idx); |
---|
| 211 | sendIndexBuff[idx] = idx + ibegin; |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | { |
---|
| 215 | sendValueBuff[idx] = NumTraits<double>::sfmax(); |
---|
| 216 | sendIndexBuff[idx] = -1; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | int* recvCount=new int[nbClient]; |
---|
| 221 | MPI_Allgather(&numValue,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm); |
---|
| 222 | |
---|
| 223 | int* displ=new int[nbClient]; |
---|
| 224 | displ[0]=0 ; |
---|
| 225 | for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1]; |
---|
| 226 | |
---|
| 227 | // Each client have enough global info of axis |
---|
| 228 | MPI_Allgatherv(sendIndexBuff,numValue,MPI_INT,recvIndexBuff,recvCount,displ,MPI_INT,client->intraComm); |
---|
| 229 | MPI_Allgatherv(sendValueBuff,numValue,MPI_DOUBLE,&(recvBuff[0]),recvCount,displ,MPI_DOUBLE,client->intraComm); |
---|
| 230 | |
---|
| 231 | for (int idx = 0; idx < srcSize; ++idx) |
---|
| 232 | { |
---|
| 233 | indexVec[idx] = recvIndexBuff[idx]; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | delete [] displ; |
---|
| 237 | delete [] recvCount; |
---|
| 238 | delete [] recvIndexBuff; |
---|
| 239 | delete [] sendIndexBuff; |
---|
| 240 | delete [] sendValueBuff; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
[827] | 244 | /*! |
---|
| 245 | Fill in axis value dynamically from a field whose grid is composed of a domain and an axis |
---|
| 246 | \param [in/out] vecAxisValue vector axis value filled in from input field |
---|
| 247 | */ |
---|
| 248 | void CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue, |
---|
| 249 | const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
| 250 | { |
---|
| 251 | if (coordinate_.empty()) |
---|
| 252 | { |
---|
| 253 | vecAxisValue.resize(1); |
---|
| 254 | vecAxisValue[0].resize(axisSrc_->value.numElements()); |
---|
| 255 | vecAxisValue[0] = axisSrc_->value; |
---|
| 256 | this->transformationMapping_.resize(1); |
---|
| 257 | this->transformationWeight_.resize(1); |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | CField* field = CField::get(coordinate_); |
---|
| 262 | CGrid* grid = field->grid; |
---|
| 263 | |
---|
| 264 | std::vector<CDomain*> domListP = grid->getDomains(); |
---|
| 265 | std::vector<CAxis*> axisListP = grid->getAxis(); |
---|
| 266 | if (domListP.empty() || axisListP.empty() || (1 < domListP.size()) || (1 < axisListP.size())) |
---|
| 267 | ERROR("CAxisAlgorithmInterpolate::fillInAxisValue(std::vector<CArray<double,1> >& vecAxisValue)", |
---|
| 268 | << "XIOS only supports dynamic interpolation with coordinate (field) associated with grid composed of a domain and an axis" |
---|
| 269 | << "Coordinate (field) id = " <<field->getId() << std::endl |
---|
| 270 | << "Associated grid id = " << grid->getId()); |
---|
| 271 | |
---|
| 272 | CDomain* dom = domListP[0]; |
---|
| 273 | size_t vecAxisValueSize = dom->i_index.numElements(); |
---|
[913] | 274 | size_t vecAxisValueSizeWithMask = 0; |
---|
| 275 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
| 276 | { |
---|
| 277 | if (dom->mask_1d(idx)) ++vecAxisValueSizeWithMask; |
---|
| 278 | } |
---|
| 279 | |
---|
[862] | 280 | int niGlobDom = dom->ni_glo.getValue(); |
---|
[913] | 281 | vecAxisValue.resize(vecAxisValueSizeWithMask); |
---|
[827] | 282 | if (transPosition_.empty()) |
---|
| 283 | { |
---|
[913] | 284 | size_t indexMask = 0; |
---|
| 285 | transPosition_.resize(vecAxisValueSizeWithMask); |
---|
[827] | 286 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
| 287 | { |
---|
[913] | 288 | if (dom->mask_1d(idx)) |
---|
| 289 | { |
---|
| 290 | transPosition_[indexMask].resize(1); |
---|
| 291 | transPosition_[indexMask][0] = (dom->i_index)(idx) + niGlobDom * (dom->j_index)(idx); |
---|
| 292 | ++indexMask; |
---|
| 293 | } |
---|
| 294 | |
---|
[827] | 295 | } |
---|
| 296 | } |
---|
[913] | 297 | this->transformationMapping_.resize(vecAxisValueSizeWithMask); |
---|
| 298 | this->transformationWeight_.resize(vecAxisValueSizeWithMask); |
---|
| 299 | this->transformationPosition_.resize(vecAxisValueSizeWithMask); |
---|
[827] | 300 | |
---|
[831] | 301 | const CDistributionClient::GlobalLocalDataMap& globalLocalIndexSendToServer = grid->getDistributionClient()->getGlobalLocalDataSendToServer(); |
---|
| 302 | CDistributionClient::GlobalLocalDataMap::const_iterator itIndex, iteIndex = globalLocalIndexSendToServer.end(); |
---|
[827] | 303 | size_t axisSrcSize = axisSrc_->index.numElements(); |
---|
| 304 | std::vector<int> globalDimension = grid->getGlobalDimension(); |
---|
[831] | 305 | |
---|
[913] | 306 | size_t indexMask = 0; |
---|
[827] | 307 | for (size_t idx = 0; idx < vecAxisValueSize; ++idx) |
---|
| 308 | { |
---|
[913] | 309 | if (dom->mask_1d(idx)) |
---|
[827] | 310 | { |
---|
[913] | 311 | size_t axisValueSize = 0; |
---|
| 312 | for (size_t jdx = 0; jdx < axisSrcSize; ++jdx) |
---|
[827] | 313 | { |
---|
[913] | 314 | size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1]; |
---|
| 315 | if (iteIndex != globalLocalIndexSendToServer.find(globalIndex)) |
---|
| 316 | { |
---|
| 317 | ++axisValueSize; |
---|
| 318 | } |
---|
[827] | 319 | } |
---|
| 320 | |
---|
[913] | 321 | vecAxisValue[indexMask].resize(axisValueSize); |
---|
| 322 | axisValueSize = 0; |
---|
| 323 | for (size_t jdx = 0; jdx < axisSrcSize; ++jdx) |
---|
[827] | 324 | { |
---|
[913] | 325 | size_t globalIndex = ((dom->i_index)(idx) + (dom->j_index)(idx)*globalDimension[0]) + (axisSrc_->index)(jdx)*globalDimension[0]*globalDimension[1]; |
---|
| 326 | itIndex = globalLocalIndexSendToServer.find(globalIndex); |
---|
| 327 | if (iteIndex != itIndex) |
---|
| 328 | { |
---|
| 329 | vecAxisValue[indexMask](axisValueSize) = (*dataAuxInputs[0])(itIndex->second); |
---|
| 330 | ++axisValueSize; |
---|
| 331 | } |
---|
[827] | 332 | } |
---|
[913] | 333 | ++indexMask; |
---|
[827] | 334 | } |
---|
| 335 | } |
---|
| 336 | } |
---|
[630] | 337 | } |
---|
[827] | 338 | |
---|
| 339 | } |
---|