transformPoints.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  transformPoints
26 
27 Description
28  Transform (translate, rotate, scale) the mesh points, and optionally also
29  any vector and tensor fields.
30 
31 Usage
32  \b transformPoints "<transformations>" [OPTION]
33 
34  Supported transformations:
35  - \par translate=<translation vector>
36  Translational transformation by given vector
37  - \par rotate=(<n1 vector> <n2 vector>)
38  Rotational transformation from unit vector n1 to n2
39  - \par Rx=<angle [deg] about x-axis>
40  Rotational transformation by given angle about x-axis
41  - \par Ry=<angle [deg] about y-axis>
42  Rotational transformation by given angle about y-axis
43  - \par Rz=<angle [deg] about z-axis>
44  Rotational transformation by given angle about z-axis
45  - \par Ra=<axis vector> <angle [deg] about axis>
46  Rotational transformation by given angle about given axis
47  - \par scale=<x-y-z scaling vector>
48  Anisotropic scaling by the given vector in the x, y, z
49  coordinate directions
50 
51  Options:
52  - \par -rotateFields \n
53  Additionally transform vector and tensor fields.
54  - \par -pointSet <name> \n
55  Only transform points in the given point set.
56 
57  Example usage:
58  transformPoints \
59  "translate=(-0.05 -0.05 0), \
60  Rz=45, \
61  translate=(0.05 0.05 0)"
62 
63 See also
64  Foam::transformer
65  surfaceTransformPoints
66 
67 \*---------------------------------------------------------------------------*/
68 
69 #include "argList.H"
70 #include "fvMesh.H"
71 #include "volFields.H"
72 #include "surfaceFields.H"
73 #include "ReadFields.H"
74 #include "pointFields.H"
75 #include "pointSet.H"
76 #include "transformField.H"
78 #include "unitConversion.H"
79 
80 using namespace Foam;
81 
82 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
83 
84 template<class GeoField>
85 void readAndRotateFields
86 (
87  PtrList<GeoField>& flds,
88  const fvMesh& mesh,
89  const tensor& T,
90  const IOobjectList& objects
91 )
92 {
93  ReadFields(mesh, objects, flds);
94  forAll(flds, i)
95  {
96  Info<< "Transforming " << flds[i].name() << endl;
97  dimensionedTensor dimT("t", flds[i].dimensions(), T);
98  transform(flds[i], dimT, flds[i]);
99  }
100 }
101 
102 
103 void rotateFields(const argList& args, const Time& runTime, const tensor& T)
104 {
105  #include "createNamedMesh.H"
106 
107  // Read objects in time directory
108  IOobjectList objects(mesh, runTime.name());
109 
110  // Read vol fields.
112  readAndRotateFields(vsFlds, mesh, T, objects);
114  readAndRotateFields(vvFlds, mesh, T, objects);
116  readAndRotateFields(vstFlds, mesh, T, objects);
118  readAndRotateFields(vsymtFlds, mesh, T, objects);
120  readAndRotateFields(vtFlds, mesh, T, objects);
121 
122  // Read surface fields.
124  readAndRotateFields(ssFlds, mesh, T, objects);
126  readAndRotateFields(svFlds, mesh, T, objects);
128  readAndRotateFields(sstFlds, mesh, T, objects);
130  readAndRotateFields(ssymtFlds, mesh, T, objects);
132  readAndRotateFields(stFlds, mesh, T, objects);
133 
134  mesh.write();
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140 int main(int argc, char *argv[])
141 {
143  (
144  "Transforms a mesh by translation, rotation and/or scaling.\n"
145  "The <transformations> are listed comma-separated in a string "
146  "and executed in sequence.\n\n"
147  "transformations:\n"
148  " translate=<vector> "
149  "translation by vector, e.g. (1 2 3)\n"
150  " rotate=(<n1> <n2>) "
151  "rotation from unit vector n1 to n2\n"
152  " Rx=<angle> "
153  "rotation by given angle [deg], e.g. 90, about x-axis\n"
154  " Ry=<angle> "
155  "rotation by given angle [deg] about y-axis\n"
156  " Rz=<angle> "
157  "rotation by given angle [deg] about z-axis\n"
158  " Ra=<axis vector> <angle> "
159  "rotation by given angle [deg] about specified axis\n"
160  " scale=<vector> "
161  "scale by factors from vector in x, y, z directions,\n"
162  " "
163  "e.g. (0.001 0.001 0.001) to scale from mm to m\n\n"
164  "example:\n"
165  " transformPoints "
166  "\"translate=(1.2 0 0), Rx=90, translate=(-1.2 0 0)\""
167  );
168 
169  argList::validArgs.append("transformations");
170 
172  (
173  "rotateFields",
174  "transform vector and tensor fields"
175  );
176 
178  (
179  "pointSet",
180  "pointSet",
181  "Point set to limit the transformation to"
182  );
183 
184  #include "addRegionOption.H"
185  #include "addAllRegionsOption.H"
186  #include "setRootCase.H"
187  #include "createTime.H"
188 
189  const string transformationString(args[1]);
190 
191  #include "createTransforms.H"
192 
193  #include "setRegionNames.H"
194 
195  const bool doRotateFields = args.optionFound("rotateFields");
196 
197  word pointSetName = word::null;
198  const bool doPointSet = args.optionReadIfPresent("pointSet", pointSetName);
199 
200  if (doRotateFields && doPointSet)
201  {
203  << "Rotation of fields across the entire mesh, and limiting the "
204  << "transformation of points to a set, cannot be done "
205  << "simultaneously" << exit(FatalError);
206  }
207 
208  forAll(regionNames, regioni)
209  {
210  const word& regionName = regionNames[regioni];
211 
212  const word& regionDir =
214  ? word::null
215  : regionName;
216 
217  const fileName meshDir(regionDir/polyMesh::meshSubDir);
218 
220  (
221  IOobject
222  (
223  "points",
224  runTime.findInstance(meshDir, "points"),
225  meshDir,
226  runTime,
229  false
230  )
231  );
232 
233  if (doPointSet)
234  {
235  const labelList setPointIDs
236  (
237  pointSet
238  (
239  IOobject
240  (
241  pointSetName,
242  runTime.findInstance(meshDir/"sets", word::null),
243  polyMesh::meshSubDir/"sets",
244  runTime,
247  false
248  )
249  ).toc()
250  );
251 
252  pointField setPoints(UIndirectList<point>(points, setPointIDs));
253 
254  transforms.transformPosition(setPoints, setPoints);
255 
256  UIndirectList<point>(points, setPointIDs) = setPoints;
257  }
258  else
259  {
261  }
262 
263  if (doRotateFields)
264  {
265  rotateFields(args, runTime, transforms.T());
266  }
267 
268  // Set the precision of the points data to 10
270 
271  Info<< "Writing points into directory " << points.path() << nl << endl;
272  points.write();
273  }
274 
275  Info<< "End\n" << endl;
276 
277  return 0;
278 }
279 
280 
281 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
A primitive field of type <Type> with automated input and output.
Definition: IOField.H:53
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: Time.C:647
A List with indirect addressing.
Definition: UIndirectList.H:60
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
Generic dimensioned Type class.
const word & name() const
Return const reference to name.
A class for handling file names.
Definition: fileName.H:82
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:101
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1736
A set of point labels.
Definition: pointSet.H:51
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:268
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:271
vector transformPosition(const vector &v) const
Transform the given position.
Definition: transformerI.H:153
const tensor & T() const
Return the transformation tensor.
Definition: transformerI.H:94
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
Foam::word regionName
transformer transforms
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
const pointField & points
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
wordList ReadFields(const Mesh &mesh, const IOobjectList &objects, PtrList< GeoField > &fields, const bool syncPar=true)
Read all fields of the specified type.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
dimensionSet transform(const dimensionSet &)
Definition: dimensionSet.C:483
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
error FatalError
static const char nl
Definition: Ostream.H:260
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
PtrList< surfaceScalarField > ssFlds
PtrList< surfaceTensorField > stFlds
PtrList< surfaceSymmTensorField > ssymtFlds
PtrList< surfaceVectorField > svFlds
objects
PtrList< surfaceSphericalTensorField > sstFlds
PtrList< volSphericalTensorField > vstFlds
Definition: readVolFields.H:9
PtrList< volScalarField > vsFlds
Definition: readVolFields.H:3
PtrList< volTensorField > vtFlds
Definition: readVolFields.H:15
PtrList< volSymmTensorField > vsymtFlds
Definition: readVolFields.H:12
PtrList< volVectorField > vvFlds
Definition: readVolFields.H:6
const Foam::wordList regionNames(args.optionFound("allRegions") ? runTime .controlDict().subDict("regionSolvers").toc() :wordList(1, args.optionFound("region") ? args.optionRead< word >("region") :polyMesh::defaultRegion))
Foam::argList args(argc, argv)
Foam::surfaceFields.
Spatial transformation functions for primitive fields.
Spatial transformation functions for FieldFields.
Unit conversion functions.