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

transformPoints.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 Application
00026     transformPoints
00027 
00028 Description
00029     Transforms the mesh points in the polyMesh directory according to the
00030     translate, rotate and scale options.
00031 
00032 Usage
00033     Options are:
00034 
00035     -translate vector
00036         Translates the points by the given vector,
00037 
00038     -rotate (vector vector)
00039         Rotates the points from the first vector to the second,
00040 
00041      or -yawPitchRoll (yawdegrees pitchdegrees rolldegrees)
00042      or -rollPitchYaw (rolldegrees pitchdegrees yawdegrees)
00043 
00044     -scale vector
00045         Scales the points by the given vector.
00046 
00047     The any or all of the three options may be specified and are processed
00048     in the above order.
00049 
00050     With -rotateFields (in combination with -rotate/yawPitchRoll/rollPitchYaw)
00051     it will also read & transform vector & tensor fields.
00052 
00053     Note:
00054     yaw (rotation about z)
00055     pitch (rotation about y)
00056     roll (rotation about x)
00057 
00058 \*---------------------------------------------------------------------------*/
00059 
00060 #include "argList.H"
00061 #include "Time.H"
00062 #include "fvMesh.H"
00063 #include "volFields.H"
00064 #include "surfaceFields.H"
00065 #include "ReadFields.H"
00066 #include "pointFields.H"
00067 #include "transformField.H"
00068 #include "transformGeometricField.H"
00069 #include "IStringStream.H"
00070 
00071 using namespace Foam;
00072 using namespace Foam::mathematicalConstant;
00073 
00074 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00075 
00076 template<class GeoField>
00077 void readAndRotateFields
00078 (
00079     PtrList<GeoField>& flds,
00080     const fvMesh& mesh,
00081     const tensor& T,
00082     const IOobjectList& objects
00083 )
00084 {
00085     ReadFields(mesh, objects, flds);
00086     forAll(flds, i)
00087     {
00088         Info<< "Transforming " << flds[i].name() << endl;
00089         dimensionedTensor dimT("t", flds[i].dimensions(), T);
00090         transform(flds[i], dimT, flds[i]);
00091     }
00092 }
00093 
00094 
00095 void rotateFields(const Time& runTime, const tensor& T)
00096 {
00097 #   include "createMesh.H"
00098 
00099     // Read objects in time directory
00100     IOobjectList objects(mesh, runTime.timeName());
00101 
00102     // Read vol fields.
00103 
00104     PtrList<volScalarField> vsFlds;
00105     readAndRotateFields(vsFlds, mesh, T, objects);
00106 
00107     PtrList<volVectorField> vvFlds;
00108     readAndRotateFields(vvFlds, mesh, T, objects);
00109 
00110     PtrList<volSphericalTensorField> vstFlds;
00111     readAndRotateFields(vstFlds, mesh, T, objects);
00112 
00113     PtrList<volSymmTensorField> vsymtFlds;
00114     readAndRotateFields(vsymtFlds, mesh, T, objects);
00115 
00116     PtrList<volTensorField> vtFlds;
00117     readAndRotateFields(vtFlds, mesh, T, objects);
00118 
00119     // Read surface fields.
00120 
00121     PtrList<surfaceScalarField> ssFlds;
00122     readAndRotateFields(ssFlds, mesh, T, objects);
00123 
00124     PtrList<surfaceVectorField> svFlds;
00125     readAndRotateFields(svFlds, mesh, T, objects);
00126 
00127     PtrList<surfaceSphericalTensorField> sstFlds;
00128     readAndRotateFields(sstFlds, mesh, T, objects);
00129 
00130     PtrList<surfaceSymmTensorField> ssymtFlds;
00131     readAndRotateFields(ssymtFlds, mesh, T, objects);
00132 
00133     PtrList<surfaceTensorField> stFlds;
00134     readAndRotateFields(stFlds, mesh, T, objects);
00135 
00136     mesh.write();
00137 }
00138 
00139 
00140 //  Main program:
00141 
00142 int main(int argc, char *argv[])
00143 {
00144     argList::validOptions.insert("translate", "vector");
00145     argList::validOptions.insert("rotate", "(vector vector)");
00146     argList::validOptions.insert("rollPitchYaw", "(roll pitch yaw)");
00147     argList::validOptions.insert("yawPitchRoll", "(yaw pitch roll)");
00148     argList::validOptions.insert("rotateFields", "");
00149     argList::validOptions.insert("scale", "vector");
00150 
00151 #   include "setRootCase.H"
00152 #   include "createTime.H"
00153 
00154     pointIOField points
00155     (
00156         IOobject
00157         (
00158             "points",
00159             runTime.findInstance(polyMesh::meshSubDir, "points"),
00160             polyMesh::meshSubDir,
00161             runTime,
00162             IOobject::MUST_READ,
00163             IOobject::NO_WRITE,
00164             false
00165         )
00166     );
00167 
00168 
00169     if (args.options().empty())
00170     {
00171         FatalErrorIn(args.executable())
00172             << "No options supplied, please use one or more of "
00173                "-translate, -rotate or -scale options."
00174             << exit(FatalError);
00175     }
00176 
00177     if (args.optionFound("translate"))
00178     {
00179         vector transVector(args.optionLookup("translate")());
00180 
00181         Info<< "Translating points by " << transVector << endl;
00182 
00183         points += transVector;
00184     }
00185 
00186     if (args.optionFound("rotate"))
00187     {
00188         Pair<vector> n1n2(args.optionLookup("rotate")());
00189         n1n2[0] /= mag(n1n2[0]);
00190         n1n2[1] /= mag(n1n2[1]);
00191         tensor T = rotationTensor(n1n2[0], n1n2[1]);
00192 
00193         Info<< "Rotating points by " << T << endl;
00194 
00195         points = transform(T, points);
00196 
00197         if (args.optionFound("rotateFields"))
00198         {
00199             rotateFields(runTime, T);
00200         }
00201     }
00202     else if (args.optionFound("rollPitchYaw"))
00203     {
00204         vector v(args.optionLookup("rollPitchYaw")());
00205 
00206         Info<< "Rotating points by" << nl
00207             << "    roll  " << v.x() << nl
00208             << "    pitch " << v.y() << nl
00209             << "    yaw   " << v.z() << endl;
00210 
00211 
00212         // Convert to radians
00213         v *= pi/180.0;
00214 
00215         quaternion R(v.x(), v.y(), v.z());
00216 
00217         Info<< "Rotating points by quaternion " << R << endl;
00218         points = transform(R, points);
00219 
00220         if (args.optionFound("rotateFields"))
00221         {
00222             rotateFields(runTime, R.R());
00223         }
00224     }
00225     else if (args.optionFound("yawPitchRoll"))
00226     {
00227         vector v(args.optionLookup("yawPitchRoll")());
00228 
00229         Info<< "Rotating points by" << nl
00230             << "    yaw   " << v.x() << nl
00231             << "    pitch " << v.y() << nl
00232             << "    roll  " << v.z() << endl;
00233 
00234 
00235         // Convert to radians
00236         v *= pi/180.0;
00237 
00238         scalar yaw = v.x();
00239         scalar pitch = v.y();
00240         scalar roll = v.z();
00241 
00242         quaternion R = quaternion(vector(0, 0, 1), yaw);
00243         R *= quaternion(vector(0, 1, 0), pitch);
00244         R *= quaternion(vector(1, 0, 0), roll);
00245 
00246         Info<< "Rotating points by quaternion " << R << endl;
00247         points = transform(R, points);
00248 
00249         if (args.optionFound("rotateFields"))
00250         {
00251             rotateFields(runTime, R.R());
00252         }
00253     }
00254 
00255     if (args.optionFound("scale"))
00256     {
00257         vector scaleVector(args.optionLookup("scale")());
00258 
00259         Info<< "Scaling points by " << scaleVector << endl;
00260 
00261         points.replace(vector::X, scaleVector.x()*points.component(vector::X));
00262         points.replace(vector::Y, scaleVector.y()*points.component(vector::Y));
00263         points.replace(vector::Z, scaleVector.z()*points.component(vector::Z));
00264     }
00265 
00266     // Set the precision of the points data to 10
00267     IOstream::defaultPrecision(10);
00268 
00269     Info << "Writing points into directory " << points.path() << nl << endl;
00270     points.write();
00271 
00272     return 0;
00273 }
00274 
00275 
00276 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd