OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

hierarchGeomDecomp.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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     // high and low can still differ by one. Choose best.
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 // Find position in values so between minIndex and this position there
00146 // are wantedSize elements.
00147 void Foam::hierarchGeomDecomp::findBinary
00148 (
00149     const label sizeTol,
00150     const List<scalar>& values,
00151     const label minIndex,       // index of previous value
00152     const scalar minValue,      // value at minIndex
00153     const scalar maxValue,      // global max of values
00154     const scalar wantedSize,    // wanted size
00155 
00156     label& mid,                 // index where size of bin is
00157                                 // wantedSize (to within sizeTol)
00158     scalar& midValue            // value at mid
00159 )
00160 {
00161     label low = minIndex;
00162     scalar lowValue = minValue;
00163 
00164     scalar highValue = maxValue;
00165     // (one beyond) index of highValue
00166     label high = values.size();
00167 
00168     // Safeguards to avoid infinite loop.
00169     label lowPrev = -1;
00170     label midPrev = -1;
00171     label highPrev = -1;
00172 
00173     //while (low <= high)
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         // Update mid, midValue
00203         midValue = 0.5*(lowValue+highValue);
00204         mid = findLower(values, midValue, low, high);
00205 
00206         // Safeguard if same as previous.
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 // Sort points into bins according to one component. Recurses to next component.
00224 void Foam::hierarchGeomDecomp::sortComponent
00225 (
00226     const label sizeTol,
00227     const pointField& points,
00228     const labelList& current,       // slice of points to decompose
00229     const direction componentIndex, // index in decompOrder_
00230     const label mult,               // multiplication factor for finalDecomp
00231     labelList& finalDecomp
00232 )
00233 {
00234     // Current component
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     // Storage for sorted component compI
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     // starting index (in sortedCoord) of bin (= local)
00283     label leftIndex = 0;
00284     // starting value of bin (= global since coordinate)
00285     scalar leftCoord = minCoord;
00286 
00287     // Sort bins of size n
00288     for (label bin = 0; bin < n_[compI]; bin++)
00289     {
00290         // Now we need to determine the size of the bin (dx). This is
00291         // determined by the 'pivot' values - everything to the left of this
00292         // value goes in the current bin, everything to the right into the next
00293         // bins.
00294 
00295         // Local number of elements
00296         label localSize = -1;     // offset from leftOffset
00297 
00298         // Value at right of bin (leftIndex+localSize-1)
00299         scalar rightCoord = -GREAT;
00300 
00301         if (bin == n_[compI]-1)
00302         {
00303             // Last bin. Copy all.
00304             localSize = current.size()-leftIndex;
00305             rightCoord = maxCoord;                  // note: not used anymore
00306         }
00307         else if (Pstream::nProcs() == 1)
00308         {
00309             // No need for binary searching of bin size
00310             localSize = label(current.size()/n_[compI]);
00311             rightCoord = sortedCoord[leftIndex+localSize];
00312         }
00313         else
00314         {
00315             // For the current bin (starting at leftCoord) we want a rightCoord
00316             // such that the sum of all sizes are globalCurrentSize/n_[compI].
00317             // We have to iterate to obtain this.
00318 
00319             label rightIndex = current.size();
00320             rightCoord = maxCoord;
00321 
00322             // Calculate rightIndex/rightCoord to have wanted size
00323             findBinary
00324             (
00325                 sizeTol,
00326                 sortedCoord,
00327                 leftIndex,
00328                 leftCoord,
00329                 maxCoord,
00330                 globalCurrentSize/n_[compI],  // wanted size
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         // Copy localSize elements starting from leftIndex.
00351         labelList slice(localSize);
00352 
00353         forAll(slice, i)
00354         {
00355             label pointI = current[sortedCoord.indices()[leftIndex+i]];
00356 
00357             // Mark point into correct bin
00358             finalDecomp[pointI] += bin*mult;
00359 
00360             // And collect for next sorting action
00361             slice[i] = pointI;
00362         }
00363 
00364         // Sort slice in next component
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],     // Multiplier to apply to decomposition.
00381                 finalDecomp
00382             );
00383 
00384             if (debug)
00385             {
00386                 Pout.prefix() = oldPrefix;
00387             }
00388         }
00389 
00390         // Step to next bin.
00391         leftIndex += localSize;
00392         leftCoord = rightCoord;
00393     }
00394 }
00395 
00396 
00397 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00425 
00426 Foam::labelList Foam::hierarchGeomDecomp::decompose
00427 (
00428     const pointField& points
00429 )
00430 {
00431     // construct a list for the final result
00432     labelList finalDecomp(points.size(), 0);
00433 
00434     // Start off with every point sorted onto itself.
00435     labelList slice(points.size());
00436     forAll(slice, i)
00437     {
00438         slice[i] = i;
00439     }
00440 
00441     pointField rotatedPoints = rotDelta_ & points;
00442 
00443 
00444     // Calculate tolerance of cell distribution. For large cases finding
00445     // distibution to the cell exact would cause too many iterations so allow
00446     // some slack.
00447     label allSize = points.size();
00448     reduce(allSize, sumOp<label>());
00449 
00450     const label sizeTol = max(1, label(1E-3*allSize/nProcessors_));
00451 
00452     // Sort recursive
00453     sortComponent
00454     (
00455         sizeTol,
00456         rotatedPoints,
00457         slice,
00458         0,              // Sort first component in decompOrder.
00459         1,              // Offset for different x bins.
00460         finalDecomp
00461     );
00462 
00463     return finalDecomp;
00464 }
00465 
00466 
00467 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd