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

displacementInterpolationFvMotionSolver.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 "displacementInterpolationFvMotionSolver.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "SortableList.H"
00030 #include "IOList.H"
00031 #include "Tuple2.H"
00032 #include "mapPolyMesh.H"
00033 #include "interpolateXY.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 namespace Foam
00038 {
00039     defineTypeNameAndDebug(displacementInterpolationFvMotionSolver, 0);
00040 
00041     addToRunTimeSelectionTable
00042     (
00043         fvMotionSolver,
00044         displacementInterpolationFvMotionSolver,
00045         dictionary
00046     );
00047 
00048     template<>
00049     const word IOList<Tuple2<scalar, vector> >::typeName("scalarVectorTable");
00050 }
00051 
00052 
00053 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00054 
00055 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00056 
00057 Foam::displacementInterpolationFvMotionSolver::
00058 displacementInterpolationFvMotionSolver
00059 (
00060     const polyMesh& mesh,
00061     Istream& is
00062 )
00063 :
00064     displacementFvMotionSolver(mesh, is),
00065     dynamicMeshCoeffs_
00066     (
00067         IOdictionary
00068         (
00069             IOobject
00070             (
00071                 "dynamicMeshDict",
00072                 mesh.time().constant(),
00073                 mesh,
00074                 IOobject::MUST_READ,
00075                 IOobject::NO_WRITE,
00076                 false
00077             )
00078         ).subDict(typeName + "Coeffs")
00079     )
00080 {
00081     // Get zones and their interpolation tables for displacement
00082     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00083 
00084     List<Pair<word> > faceZoneToTable
00085     (
00086         dynamicMeshCoeffs_.lookup("interpolationTables")
00087     );
00088 
00089     const faceZoneMesh& fZones = mesh.faceZones();
00090 
00091     times_.setSize(fZones.size());
00092     displacements_.setSize(fZones.size());
00093 
00094     forAll(faceZoneToTable, i)
00095     {
00096         const word& zoneName = faceZoneToTable[i][0];
00097         label zoneI = fZones.findZoneID(zoneName);
00098 
00099         if (zoneI == -1)
00100         {
00101             FatalErrorIn
00102             (
00103                 "displacementInterpolationFvMotionSolver::"
00104                 "displacementInterpolationFvMotionSolver(const polyMesh&,"
00105                 "Istream&)"
00106             )   << "Cannot find zone " << zoneName << endl
00107                 << "Valid zones are " << mesh.faceZones().names()
00108                 << exit(FatalError);
00109         }
00110 
00111         const word& tableName = faceZoneToTable[i][1];
00112 
00113         IOList<Tuple2<scalar, vector> > table
00114         (
00115             IOobject
00116             (
00117                 tableName,
00118                 mesh.time().constant(),
00119                 "tables",
00120                 mesh,
00121                 IOobject::MUST_READ,
00122                 IOobject::NO_WRITE,
00123                 false
00124             )
00125         );
00126 
00127         // Copy table
00128         times_[zoneI].setSize(table.size());
00129         displacements_[zoneI].setSize(table.size());
00130 
00131         forAll(table, j)
00132         {
00133             times_[zoneI][j] = table[j].first();
00134             displacements_[zoneI][j] = table[j].second();
00135         }
00136     }
00137 
00138 
00139 
00140     // Sort points into bins according to position relative to faceZones
00141     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00142     // Done in all three directions.
00143 
00144     for (direction dir = 0; dir < vector::nComponents; dir++)
00145     {
00146         // min and max coordinates of all faceZones
00147         SortableList<scalar> zoneCoordinates(2*faceZoneToTable.size());
00148 
00149         forAll(faceZoneToTable, i)
00150         {
00151             const word& zoneName = faceZoneToTable[i][0];
00152             label zoneI = fZones.findZoneID(zoneName);
00153             const faceZone& fz = fZones[zoneI];
00154 
00155             scalar minCoord = VGREAT;
00156             scalar maxCoord = -VGREAT;
00157 
00158             forAll(fz().meshPoints(), localI)
00159             {
00160                 label pointI = fz().meshPoints()[localI];
00161                 const scalar coord = points0()[pointI][dir];
00162                 minCoord = min(minCoord, coord);
00163                 maxCoord = max(maxCoord, coord);
00164             }
00165 
00166             zoneCoordinates[2*i] = returnReduce(minCoord, minOp<scalar>());
00167             zoneCoordinates[2*i+1] = returnReduce(maxCoord, maxOp<scalar>());
00168 
00169             if (debug)
00170             {
00171                 Pout<< "direction " << dir << " : "
00172                     << "zone " << zoneName
00173                     << " ranges from coordinate " << zoneCoordinates[2*i]
00174                     << " to " << zoneCoordinates[2*i+1]
00175                     << endl;
00176             }
00177         }
00178         zoneCoordinates.sort();
00179 
00180         // Slightly tweak min and max face zone so points sort within
00181         zoneCoordinates[0] -= SMALL;
00182         zoneCoordinates[zoneCoordinates.size()-1] += SMALL;
00183 
00184         // Check if we have static min and max mesh bounds
00185         const scalarField meshCoords = points0().component(dir);
00186 
00187         scalar minCoord = gMin(meshCoords);
00188         scalar maxCoord = gMax(meshCoords);
00189 
00190         if (debug)
00191         {
00192             Pout<< "direction " << dir << " : "
00193                 << "mesh ranges from coordinate " << minCoord << " to "
00194                 << maxCoord << endl;
00195         }
00196 
00197         // Make copy of zoneCoordinates; include min and max of mesh
00198         // if necessary. Mark min and max with zoneI=-1.
00199         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00200 
00201         labelList& rangeZone = rangeToZone_[dir];
00202         labelListList& rangePoints = rangeToPoints_[dir];
00203         List<scalarField>& rangeWeights = rangeToWeights_[dir];
00204 
00205         scalarField rangeToCoord(zoneCoordinates.size());
00206         rangeZone.setSize(zoneCoordinates.size());
00207         label rangeI = 0;
00208 
00209         if (minCoord < zoneCoordinates[0])
00210         {
00211             label sz = rangeZone.size();
00212             rangeToCoord.setSize(sz+1);
00213             rangeZone.setSize(sz+1);
00214             rangeToCoord[rangeI] = minCoord-SMALL;
00215             rangeZone[rangeI] = -1;
00216 
00217             if (debug)
00218             {
00219                 Pout<< "direction " << dir << " : "
00220                     << "range " << rangeI << " at coordinate "
00221                     << rangeToCoord[rangeI] << " from min of mesh "
00222                     << rangeZone[rangeI] << endl;
00223             }
00224             rangeI = 1;
00225         }
00226         forAll(zoneCoordinates, i)
00227         {
00228             rangeToCoord[rangeI] = zoneCoordinates[i];
00229             rangeZone[rangeI] = zoneCoordinates.indices()[i]/2;
00230 
00231             if (debug)
00232             {
00233                 Pout<< "direction " << dir << " : "
00234                     << "range " << rangeI << " at coordinate "
00235                     << rangeToCoord[rangeI]
00236                     << " from zone " << rangeZone[rangeI] << endl;
00237             }
00238             rangeI++;
00239         }
00240         if (maxCoord > zoneCoordinates[zoneCoordinates.size()-1])
00241         {
00242             label sz = rangeToCoord.size();
00243             rangeToCoord.setSize(sz+1);
00244             rangeZone.setSize(sz+1);
00245             rangeToCoord[sz] = maxCoord+SMALL;
00246             rangeZone[sz] = -1;
00247 
00248             if (debug)
00249             {
00250                 Pout<< "direction " << dir << " : "
00251                     << "range " << rangeI << " at coordinate "
00252                     << rangeToCoord[sz] << " from max of mesh "
00253                     << rangeZone[sz] << endl;
00254             }
00255         }
00256 
00257 
00258         // Sort the points
00259         // ~~~~~~~~~~~~~~~
00260 
00261         // Count all the points inbetween rangeI and rangeI+1
00262         labelList nRangePoints(rangeToCoord.size(), 0);
00263 
00264         forAll(meshCoords, pointI)
00265         {
00266             label rangeI = findLower(rangeToCoord, meshCoords[pointI]);
00267 
00268             if (rangeI == -1 || rangeI == rangeToCoord.size()-1)
00269             {
00270                 FatalErrorIn
00271                 (
00272                     "displacementInterpolationFvMotionSolver::"
00273                     "displacementInterpolationFvMotionSolver"
00274                     "(const polyMesh&, Istream&)"
00275                 )   << "Did not find point " << points0()[pointI]
00276                     << " coordinate " << meshCoords[pointI]
00277                     << " in ranges " << rangeToCoord
00278                     << abort(FatalError);
00279             }
00280             nRangePoints[rangeI]++;
00281         }
00282 
00283         if (debug)
00284         {
00285             for (label rangeI = 0; rangeI < rangeToCoord.size()-1; rangeI++)
00286             {
00287                 // Get the two zones bounding the range
00288                 Pout<< "direction " << dir << " : "
00289                     << "range from " << rangeToCoord[rangeI]
00290                     << " to " << rangeToCoord[rangeI+1]
00291                     << " contains " << nRangePoints[rangeI]
00292                     << " points." << endl;
00293             }
00294         }
00295 
00296         // Sort
00297         rangePoints.setSize(nRangePoints.size());
00298         rangeWeights.setSize(nRangePoints.size());
00299         forAll(rangePoints, rangeI)
00300         {
00301             rangePoints[rangeI].setSize(nRangePoints[rangeI]);
00302             rangeWeights[rangeI].setSize(nRangePoints[rangeI]);
00303         }
00304         nRangePoints = 0;
00305         forAll(meshCoords, pointI)
00306         {
00307             label rangeI = findLower(rangeToCoord, meshCoords[pointI]);
00308             label& nPoints = nRangePoints[rangeI];
00309             rangePoints[rangeI][nPoints] = pointI;
00310             rangeWeights[rangeI][nPoints] =
00311                 (meshCoords[pointI]-rangeToCoord[rangeI])
00312               / (rangeToCoord[rangeI+1]-rangeToCoord[rangeI]);
00313             nPoints++;
00314         }
00315     }
00316 }
00317 
00318 
00319 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00320 
00321 Foam::displacementInterpolationFvMotionSolver::
00322 ~displacementInterpolationFvMotionSolver()
00323 {}
00324 
00325 
00326 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00327 
00328 Foam::tmp<Foam::pointField>
00329 Foam::displacementInterpolationFvMotionSolver::curPoints() const
00330 {
00331     if (mesh().nPoints() != points0().size())
00332     {
00333         FatalErrorIn
00334         (
00335             "displacementInterpolationFvMotionSolver::curPoints() const"
00336         )   << "The number of points in the mesh seems to have changed." << endl
00337             << "In constant/polyMesh there are " << points0().size()
00338             << " points; in the current mesh there are " << mesh().nPoints()
00339             << " points." << exit(FatalError);
00340     }
00341 
00342     tmp<pointField> tcurPoints(new pointField(points0()));
00343     pointField& curPoints = tcurPoints();
00344 
00345     // Interpolate the diplacement of the face zones.
00346     vectorField zoneDisp(displacements_.size(), vector::zero);
00347     forAll(zoneDisp, zoneI)
00348     {
00349         if (times_[zoneI].size())
00350         {
00351             zoneDisp[zoneI] = interpolateXY
00352             (
00353                 mesh().time().value(),
00354                 times_[zoneI],
00355                 displacements_[zoneI]
00356             );
00357         }
00358     }
00359     if (debug)
00360     {
00361         Pout<< "Zone displacements:" << zoneDisp << endl;
00362     }
00363 
00364 
00365     // Interpolate the point location
00366     for (direction dir = 0; dir < vector::nComponents; dir++)
00367     {
00368         const labelList& rangeZone = rangeToZone_[dir];
00369         const labelListList& rangePoints = rangeToPoints_[dir];
00370         const List<scalarField>& rangeWeights = rangeToWeights_[dir];
00371 
00372         for (label rangeI = 0; rangeI < rangeZone.size()-1; rangeI++)
00373         {
00374             const labelList& rPoints = rangePoints[rangeI];
00375             const scalarField& rWeights = rangeWeights[rangeI];
00376 
00377             // Get the two zones bounding the range
00378             label minZoneI = rangeZone[rangeI];
00379             //vector minDisp =
00380             //    (minZoneI == -1 ? vector::zero : zoneDisp[minZoneI]);
00381             scalar minDisp = (minZoneI == -1 ? 0.0 : zoneDisp[minZoneI][dir]);
00382             label maxZoneI = rangeZone[rangeI+1];
00383             //vector maxDisp =
00384             //    (maxZoneI == -1 ? vector::zero : zoneDisp[maxZoneI]);
00385             scalar maxDisp = (maxZoneI == -1 ? 0.0 : zoneDisp[maxZoneI][dir]);
00386 
00387             forAll(rPoints, i)
00388             {
00389                 label pointI = rPoints[i];
00390                 scalar w = rWeights[i];
00391                 //curPoints[pointI] += (1.0-w)*minDisp+w*maxDisp;
00392                 curPoints[pointI][dir] += (1.0-w)*minDisp+w*maxDisp;
00393             }
00394         }
00395     }
00396     return tcurPoints;
00397 }
00398 
00399 
00400 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd