00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "hierarchGeomDecomp.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "PstreamReduceOps.H"
00030 #include "SortableList.H"
00031
00032
00033
00034 namespace Foam
00035 {
00036 defineTypeNameAndDebug(hierarchGeomDecomp, 0);
00037
00038 addToRunTimeSelectionTable
00039 (
00040 decompositionMethod,
00041 hierarchGeomDecomp,
00042 dictionary
00043 );
00044
00045 addToRunTimeSelectionTable
00046 (
00047 decompositionMethod,
00048 hierarchGeomDecomp,
00049 dictionaryMesh
00050 );
00051 }
00052
00053
00054
00055 void Foam::hierarchGeomDecomp::setDecompOrder()
00056 {
00057 word order(geomDecomDict_.lookup("order"));
00058
00059 if (order.size() != 3)
00060 {
00061 FatalIOErrorIn
00062 (
00063 "hierarchGeomDecomp::hierarchGeomDecomp"
00064 "(const dictionary& decompositionDict)",
00065 decompositionDict_
00066 ) << "number of characters in order (" << order << ") != 3"
00067 << exit(FatalIOError);
00068 }
00069
00070 for (label i = 0; i < 3; i++)
00071 {
00072 if (order[i] == 'x')
00073 {
00074 decompOrder_[i] = 0;
00075 }
00076 else if (order[i] == 'y')
00077 {
00078 decompOrder_[i] = 1;
00079 }
00080 else if (order[i] == 'z')
00081 {
00082 decompOrder_[i] = 2;
00083 }
00084 else
00085 {
00086 FatalIOErrorIn
00087 (
00088 "hierarchGeomDecomp::hierarchGeomDecomp"
00089 "(const dictionary& decompositionDict)",
00090 decompositionDict_
00091 ) << "Illegal decomposition order " << order << endl
00092 << "It should only contain x, y or z" << exit(FatalError);
00093 }
00094 }
00095 }
00096
00097
00098 Foam::label Foam::hierarchGeomDecomp::findLower
00099 (
00100 const List<scalar>& l,
00101 const scalar t,
00102 const label initLow,
00103 const label initHigh
00104 )
00105 {
00106 if (initHigh <= initLow)
00107 {
00108 return initLow;
00109 }
00110
00111 label low = initLow;
00112 label high = initHigh;
00113
00114 while ((high - low) > 1)
00115 {
00116 label mid = (low + high)/2;
00117
00118 if (l[mid] < t)
00119 {
00120 low = mid;
00121 }
00122 else
00123 {
00124 high = mid;
00125 }
00126 }
00127
00128
00129
00130 label tIndex = -1;
00131
00132 if (l[high-1] < t)
00133 {
00134 tIndex = high;
00135 }
00136 else
00137 {
00138 tIndex = low;
00139 }
00140
00141 return tIndex;
00142 }
00143
00144
00145
00146
00147 void Foam::hierarchGeomDecomp::findBinary
00148 (
00149 const label sizeTol,
00150 const List<scalar>& values,
00151 const label minIndex,
00152 const scalar minValue,
00153 const scalar maxValue,
00154 const scalar wantedSize,
00155
00156 label& mid,
00157
00158 scalar& midValue
00159 )
00160 {
00161 label low = minIndex;
00162 scalar lowValue = minValue;
00163
00164 scalar highValue = maxValue;
00165
00166 label high = values.size();
00167
00168
00169 label lowPrev = -1;
00170 label midPrev = -1;
00171 label highPrev = -1;
00172
00173
00174 while (true)
00175 {
00176 label size = returnReduce(mid-minIndex, sumOp<label>());
00177
00178 if (debug)
00179 {
00180 Pout<< "low:" << low << " lowValue:" << lowValue
00181 << " high:" << high << " highValue:" << highValue
00182 << " mid:" << mid << " midValue:" << midValue << nl
00183 << "globalSize:" << size << " wantedSize:" << wantedSize
00184 << " sizeTol:" << sizeTol << endl;
00185 }
00186
00187 if (wantedSize < size - sizeTol)
00188 {
00189 high = mid;
00190 highValue = midValue;
00191 }
00192 else if (wantedSize > size + sizeTol)
00193 {
00194 low = mid;
00195 lowValue = midValue;
00196 }
00197 else
00198 {
00199 break;
00200 }
00201
00202
00203 midValue = 0.5*(lowValue+highValue);
00204 mid = findLower(values, midValue, low, high);
00205
00206
00207 bool hasNotChanged = (mid == midPrev) && (low == lowPrev) && (high == highPrev);
00208 if (returnReduce(hasNotChanged, andOp<bool>()))
00209 {
00210 WarningIn("hierarchGeomDecomp::findBinary(..)")
00211 << "unable to find desired deomposition split, making do!"
00212 << endl;
00213 break;
00214 }
00215
00216 midPrev = mid;
00217 lowPrev = low;
00218 highPrev = high;
00219 }
00220 }
00221
00222
00223
00224 void Foam::hierarchGeomDecomp::sortComponent
00225 (
00226 const label sizeTol,
00227 const pointField& points,
00228 const labelList& current,
00229 const direction componentIndex,
00230 const label mult,
00231 labelList& finalDecomp
00232 )
00233 {
00234
00235 label compI = decompOrder_[componentIndex];
00236
00237 if (debug)
00238 {
00239 Pout<< "sortComponent : Sorting slice of size " << current.size()
00240 << " in component " << compI << endl;
00241 }
00242
00243
00244 SortableList<scalar> sortedCoord(current.size());
00245
00246 forAll(current, i)
00247 {
00248 label pointI = current[i];
00249
00250 sortedCoord[i] = points[pointI][compI];
00251 }
00252 sortedCoord.sort();
00253
00254 label globalCurrentSize = returnReduce(current.size(), sumOp<label>());
00255
00256 scalar minCoord = returnReduce
00257 (
00258 (
00259 sortedCoord.size()
00260 ? sortedCoord[0]
00261 : GREAT
00262 ),
00263 minOp<scalar>()
00264 );
00265
00266 scalar maxCoord = returnReduce
00267 (
00268 (
00269 sortedCoord.size()
00270 ? sortedCoord[sortedCoord.size()-1]
00271 : -GREAT
00272 ),
00273 maxOp<scalar>()
00274 );
00275
00276 if (debug)
00277 {
00278 Pout<< "sortComponent : minCoord:" << minCoord
00279 << " maxCoord:" << maxCoord << endl;
00280 }
00281
00282
00283 label leftIndex = 0;
00284
00285 scalar leftCoord = minCoord;
00286
00287
00288 for (label bin = 0; bin < n_[compI]; bin++)
00289 {
00290
00291
00292
00293
00294
00295
00296 label localSize = -1;
00297
00298
00299 scalar rightCoord = -GREAT;
00300
00301 if (bin == n_[compI]-1)
00302 {
00303
00304 localSize = current.size()-leftIndex;
00305 rightCoord = maxCoord;
00306 }
00307 else if (Pstream::nProcs() == 1)
00308 {
00309
00310 localSize = label(current.size()/n_[compI]);
00311 rightCoord = sortedCoord[leftIndex+localSize];
00312 }
00313 else
00314 {
00315
00316
00317
00318
00319 label rightIndex = current.size();
00320 rightCoord = maxCoord;
00321
00322
00323 findBinary
00324 (
00325 sizeTol,
00326 sortedCoord,
00327 leftIndex,
00328 leftCoord,
00329 maxCoord,
00330 globalCurrentSize/n_[compI],
00331
00332 rightIndex,
00333 rightCoord
00334 );
00335 localSize = rightIndex - leftIndex;
00336 }
00337
00338 if (debug)
00339 {
00340 Pout<< "For component " << compI << ", bin " << bin
00341 << " copying" << nl
00342 << "from " << leftCoord << " at local index "
00343 << leftIndex << nl
00344 << "to " << rightCoord << " localSize:"
00345 << localSize << nl
00346 << endl;
00347 }
00348
00349
00350
00351 labelList slice(localSize);
00352
00353 forAll(slice, i)
00354 {
00355 label pointI = current[sortedCoord.indices()[leftIndex+i]];
00356
00357
00358 finalDecomp[pointI] += bin*mult;
00359
00360
00361 slice[i] = pointI;
00362 }
00363
00364
00365 if (componentIndex < 2)
00366 {
00367 string oldPrefix;
00368 if (debug)
00369 {
00370 oldPrefix = Pout.prefix();
00371 Pout.prefix() = " " + oldPrefix;
00372 }
00373
00374 sortComponent
00375 (
00376 sizeTol,
00377 points,
00378 slice,
00379 componentIndex+1,
00380 mult*n_[compI],
00381 finalDecomp
00382 );
00383
00384 if (debug)
00385 {
00386 Pout.prefix() = oldPrefix;
00387 }
00388 }
00389
00390
00391 leftIndex += localSize;
00392 leftCoord = rightCoord;
00393 }
00394 }
00395
00396
00397
00398
00399 Foam::hierarchGeomDecomp::hierarchGeomDecomp
00400 (
00401 const dictionary& decompositionDict
00402 )
00403 :
00404 geomDecomp(decompositionDict, typeName),
00405 decompOrder_()
00406 {
00407 setDecompOrder();
00408 }
00409
00410
00411 Foam::hierarchGeomDecomp::hierarchGeomDecomp
00412 (
00413 const dictionary& decompositionDict,
00414 const polyMesh&
00415 )
00416 :
00417 geomDecomp(decompositionDict, hierarchGeomDecomp::typeName),
00418 decompOrder_()
00419 {
00420 setDecompOrder();
00421 }
00422
00423
00424
00425
00426 Foam::labelList Foam::hierarchGeomDecomp::decompose
00427 (
00428 const pointField& points
00429 )
00430 {
00431
00432 labelList finalDecomp(points.size(), 0);
00433
00434
00435 labelList slice(points.size());
00436 forAll(slice, i)
00437 {
00438 slice[i] = i;
00439 }
00440
00441 pointField rotatedPoints = rotDelta_ & points;
00442
00443
00444
00445
00446
00447 label allSize = points.size();
00448 reduce(allSize, sumOp<label>());
00449
00450 const label sizeTol = max(1, label(1E-3*allSize/nProcessors_));
00451
00452
00453 sortComponent
00454 (
00455 sizeTol,
00456 rotatedPoints,
00457 slice,
00458 0,
00459 1,
00460 finalDecomp
00461 );
00462
00463 return finalDecomp;
00464 }
00465
00466
00467