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-2025 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 -pointZone <name> \n
55  Only transform points in the given pointZone.
56  - \par -pointSet <name> \n
57  Only transform points in the given point set.
58 
59  Example usage:
60  transformPoints \
61  "translate=(-0.05 -0.05 0), \
62  Rz=45, \
63  translate=(0.05 0.05 0)"
64 
65 See also
66  Foam::transformer
67  surfaceTransformPoints
68 
69 \*---------------------------------------------------------------------------*/
70 
71 #include "argList.H"
72 #include "fvMesh.H"
73 #include "volFields.H"
74 #include "surfaceFields.H"
75 #include "ReadFields.H"
76 #include "pointFields.H"
77 #include "pointSet.H"
78 #include "transformField.H"
80 
81 using namespace Foam;
82 
83 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
84 
85 template<class GeoField>
86 void readAndRotateFields
87 (
88  PtrList<GeoField>& flds,
89  const fvMesh& mesh,
90  const tensor& T,
91  const IOobjectList& objects
92 )
93 {
94  ReadFields(mesh, objects, flds);
95  forAll(flds, i)
96  {
97  Info<< "Transforming " << flds[i].name() << endl;
98  dimensionedTensor dimT("t", flds[i].dimensions(), T);
99  transform(flds[i], dimT, flds[i]);
100  }
101 }
102 
103 
104 void rotateFields
105 (
106  const argList& args,
107  const Time& runTime,
108  const tensor& T
109 )
110 {
112 
113  // Read objects in time directory
114  IOobjectList objects(mesh, runTime.name());
115 
116  // Read vol fields.
118  readAndRotateFields(vsFlds, mesh, T, objects);
120  readAndRotateFields(vvFlds, mesh, T, objects);
122  readAndRotateFields(vstFlds, mesh, T, objects);
124  readAndRotateFields(vsymtFlds, mesh, T, objects);
126  readAndRotateFields(vtFlds, mesh, T, objects);
127 
128  // Read surface fields.
130  readAndRotateFields(ssFlds, mesh, T, objects);
132  readAndRotateFields(svFlds, mesh, T, objects);
134  readAndRotateFields(sstFlds, mesh, T, objects);
136  readAndRotateFields(ssymtFlds, mesh, T, objects);
138  readAndRotateFields(stFlds, mesh, T, objects);
139 
140  mesh.write();
141 }
142 
143 
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
145 
146 int main(int argc, char *argv[])
147 {
149  (
150  "Transforms a mesh by translation, rotation and/or scaling.\n"
151  "The <transformations> are listed comma-separated in a string "
152  "and executed in sequence.\n\n"
153  "transformations:\n"
154  " translate=<vector> "
155  "translation by vector, e.g. (1 2 3)\n"
156  " rotate=(<n1> <n2>) "
157  "rotation from unit vector n1 to n2\n"
158  " Rx=<angle> "
159  "rotation by given angle [deg], e.g. 90, about x-axis\n"
160  " Ry=<angle> "
161  "rotation by given angle [deg] about y-axis\n"
162  " Rz=<angle> "
163  "rotation by given angle [deg] about z-axis\n"
164  " Ra=<axis vector> <angle> "
165  "rotation by given angle [deg] about specified axis\n"
166  " scale=<vector> "
167  "scale by factors from vector in x, y, z directions,\n"
168  " "
169  "e.g. (0.001 0.001 0.001) to scale from mm to m\n\n"
170  "example:\n"
171  " transformPoints "
172  "\"translate=(1.2 0 0), Rx=90, translate=(-1.2 0 0)\""
173  );
174 
175  argList::validArgs.append("transformations");
176 
178  (
179  "rotateFields",
180  "transform vector and tensor fields"
181  );
182 
184  (
185  "pointZone",
186  "pointZone",
187  "pointZone to limit the transformation to"
188  );
189 
191  (
192  "pointSet",
193  "pointSet",
194  "Point set to limit the transformation to"
195  );
196 
197  #include "addMeshOption.H"
198  #include "addRegionOption.H"
199  #include "addAllRegionsOption.H"
200  #include "setRootCase.H"
201  #include "setMeshPath.H"
202  #include "createTime.H"
203 
204  const string transformationString(args[1]);
205 
206  #include "createTransforms.H"
207 
208  #include "setRegionNames.H"
209 
210  const bool doRotateFields = args.optionFound("rotateFields");
211 
212  word pointZoneName = word::null;
213  const bool doPointZone =
214  args.optionReadIfPresent("pointZone", pointZoneName);
215 
216  word pointSetName = word::null;
217  const bool doPointSet = args.optionReadIfPresent("pointSet", pointSetName);
218 
219  if (doRotateFields && (doPointZone || doPointSet))
220  {
222  << "Rotation of fields across the entire mesh, and limiting the "
223  << "transformation of points to a set, cannot be done "
224  << "simultaneously" << exit(FatalError);
225  }
226 
227  forAll(regionNames, regioni)
228  {
229  const word& regionName = regionNames[regioni];
230 
231  const word& regionDir =
233  ? word::null
234  : regionName;
235 
236  const fileName meshDir(meshPath/regionDir/polyMesh::meshSubDir);
237 
239  (
240  IOobject
241  (
242  "points",
243  runTime.findInstance(meshDir, "points"),
244  meshDir,
245  runTime,
248  false
249  )
250  );
251 
252  if (doPointZone || doPointSet)
253  {
254  labelList zonePointIDs;
255 
256  if (doPointZone)
257  {
258  const polyMesh mesh
259  (
260  IOobject
261  (
262  regionName,
263  runTime.name(),
264  meshPath,
265  runTime,
267  )
268  );
269 
270  zonePointIDs = mesh.pointZones()[pointZoneName];
271  }
272  else
273  {
274  zonePointIDs = pointSet
275  (
276  IOobject
277  (
278  pointSetName,
279  runTime.findInstance(meshDir/"sets", word::null),
280  polyMesh::meshSubDir/"sets",
281  runTime,
284  false
285  )
286  ).toc();
287  }
288 
289  pointField zonePoints(UIndirectList<point>(points, zonePointIDs));
290  transforms.transformPosition(zonePoints, zonePoints);
291  UIndirectList<point>(points, zonePointIDs) = zonePoints;
292  }
293  else
294  {
296  }
297 
298  // Set the precision of the points data to 10
300 
301  Info<< "Writing points into directory " << points.path() << nl << endl;
302 
303  points.write();
304 
305  if (doRotateFields)
306  {
307  rotateFields(args, runTime, transforms.T());
308  }
309  }
310 
311  Info<< "End\n" << endl;
312 
313  return 0;
314 }
315 
316 
317 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:227
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:473
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:255
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:96
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1785
A set of point labels.
Definition: pointSet.H:51
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const pointZoneList & pointZones() const
Return point zones.
Definition: polyMesh.H:437
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:270
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:273
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::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
transformer transforms
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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.
const HashTable< dimensionSet > & dimensions()
Get the table of dimension sets.
Definition: dimensionSets.C:96
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
const word & regionName(const solver &region)
Definition: solver.H:218
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
messageStream Info
dimensionSet transform(const dimensionSet &)
Definition: dimensionSet.C:501
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
error FatalError
static const char nl
Definition: Ostream.H:267
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
word meshPath
Definition: setMeshPath.H:1
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.