FreeFOAM The Cross-Platform CFD Toolkit
coordinateSystem.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/IOstream.H>
27 #include "coordinateSystem.H"
28 #include "coordinateSystems.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(coordinateSystem, 0);
36  defineRunTimeSelectionTable(coordinateSystem, dictionary);
37  defineRunTimeSelectionTable(coordinateSystem, origRotation);
38 }
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 :
44  name_(type()),
45  note_(),
46  origin_(point::zero),
47  R_(),
48  Rtr_(sphericalTensor::I)
49 {}
50 
51 
53 (
54  const word& name,
55  const coordinateSystem& cs
56 )
57 :
58  name_(name),
59  note_(),
60  origin_(cs.origin_),
61  R_(cs.R_),
62  Rtr_(cs.Rtr_)
63 {}
64 
65 
67 (
68  const word& name,
69  const point& origin,
70  const coordinateRotation& cr
71 )
72 :
73  name_(name),
74  note_(),
75  origin_(origin),
76  R_(cr),
77  Rtr_(R_.T())
78 {}
79 
80 
82 (
83  const word& name,
84  const point& origin,
85  const vector& axis,
86  const vector& dirn
87 )
88 :
89  name_(name),
90  note_(),
91  origin_(origin),
92  R_(axis, dirn),
93  Rtr_(R_.T())
94 {}
95 
96 
98 (
99  const word& name,
100  const dictionary& dict
101 )
102 :
103  name_(name),
104  note_(),
105  origin_(point::zero),
106  R_(),
107  Rtr_(sphericalTensor::I)
108 {
109  operator=(dict);
110 }
111 
112 
114 (
115  const dictionary& dict
116 )
117 :
118  name_(type()),
119  note_(),
120  origin_(point::zero),
121  R_(),
122  Rtr_(sphericalTensor::I)
123 {
124  operator=(dict);
125 }
126 
127 
129 (
130  const dictionary& dict,
131  const objectRegistry& obr
132 )
133 :
134  name_(type()),
135  note_(),
136  origin_(point::zero),
137  R_(),
138  Rtr_(sphericalTensor::I)
139 {
140  const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
141 
142  // a simple entry is a lookup into global coordinateSystems
143  if (entryPtr && !entryPtr->isDict())
144  {
145  word csName;
146  entryPtr->stream() >> csName;
147 
148  const coordinateSystems& csLst = coordinateSystems::New(obr);
149 
150  label csId = csLst.find(csName);
151  if (debug)
152  {
153  Info<< "coordinateSystem::coordinateSystem"
154  "(const dictionary&, const objectRegistry&):"
155  << nl << "using global coordinate system: "
156  << csName << "=" << csId << endl;
157  }
158 
159  if (csId < 0)
160  {
162  (
163  "coordinateSystem::coordinateSystem"
164  "(const dictionary&, const objectRegistry&)"
165  ) << "could not find coordinate system: " << csName << nl
166  << "available coordinate systems: " << csLst.toc() << nl << nl
167  << exit(FatalError);
168  }
169 
170  // copy coordinateSystem, but assign the name as the typeName
171  // to avoid strange things in writeDict()
172  operator=(csLst[csId]);
173  name_ = typeName_();
174  }
175  else
176  {
177  operator=(dict);
178  }
179 }
180 
181 
183 :
184  name_(is),
185  note_(),
186  origin_(point::zero),
187  R_(),
188  Rtr_(sphericalTensor::I)
189 {
190  dictionary dict(is);
191  operator=(dict);
192 }
193 
194 
195 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
196 
198 {}
199 
200 
201 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
202 
204 {
205  dictionary dict;
206 
207  dict.add("name", name_);
208 
209  // only write type for derived types
210  if (!ignoreType && type() != typeName_())
211  {
212  dict.add("type", type());
213  }
214 
215  // The note entry is optional
216  if (note_.size())
217  {
218  dict.add("note", note_);
219  }
220 
221  dict.add("origin", origin_);
222  dict.add("e1", e1());
223  dict.add("e3", e3());
224 
225  return dict;
226 }
227 
228 
230 (
231  const vector& local,
232  bool translate
233 ) const
234 {
235  if (translate)
236  {
237  return (R_ & local) + origin_;
238  }
239  else
240  {
241  return (R_ & local);
242  }
243 }
244 
245 
247 (
248  const vectorField& local,
249  bool translate
250 ) const
251 {
252  if (translate)
253  {
254  return (R_ & local) + origin_;
255  }
256  else
257  {
258  return (R_ & local);
259  }
260 }
261 
262 
264 (
265  const vector& global,
266  bool translate
267 ) const
268 {
269  if (translate)
270  {
271  return (Rtr_ & (global - origin_));
272  }
273  else
274  {
275  return (Rtr_ & global);
276  }
277 }
278 
279 
281 (
282  const vectorField& global,
283  bool translate
284 ) const
285 {
286  if (translate)
287  {
288  return (Rtr_ & (global - origin_));
289  }
290  else
291  {
292  return (Rtr_ & global);
293  }
294 }
295 
296 
298 {
299  os << type()
300  << " origin: " << origin() << " e1: " << e1() << " e3: " << e3();
301 }
302 
303 
304 void Foam::coordinateSystem::writeDict(Ostream& os, bool subDict) const
305 {
306  if (subDict)
307  {
308  os << indent << name_ << nl
310  }
311 
312  // only write type for derived types
313  if (type() != typeName_())
314  {
315  os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
316  }
317 
318  // The note entry is optional
319  if (note_.size())
320  {
321  os.writeKeyword("note") << note_ << token::END_STATEMENT << nl;
322  }
323 
324  os.writeKeyword("origin") << origin_ << token::END_STATEMENT << nl;
325  os.writeKeyword("e1") << e1() << token::END_STATEMENT << nl;
326  os.writeKeyword("e3") << e3() << token::END_STATEMENT << nl;
327 
328  if (subDict)
329  {
330  os << decrIndent << indent << token::END_BLOCK << endl;
331  }
332 }
333 
334 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
335 
337 {
338  if (debug)
339  {
340  Pout<< "coordinateSystem::operator=(const dictionary&) : "
341  << "assign from " << rhs << endl;
342  }
343 
344  // allow as embedded sub-dictionary "coordinateSystem"
345  const dictionary& dict =
346  (
347  rhs.found(typeName_())
348  ? rhs.subDict(typeName_())
349  : rhs
350  );
351 
352  // unspecified origin is (0 0 0)
353  origin_ = point::zero;
354  dict.readIfPresent("origin", origin_);
355 
356  // The note entry is optional
357  note_.clear();
358  rhs.readIfPresent("note", note_);
359 
360  // specify via coordinateRotation sub-dictionary
361  if (dict.found("coordinateRotation"))
362  {
363  R_ = coordinateRotation::New(dict.subDict("coordinateRotation"))();
364  }
365  else
366  {
367  // let coordinateRotation constructor extract the axes specification
368  R_ = coordinateRotation(dict);
369  }
370 
371  Rtr_ = R_.T();
372 }
373 
374 
375 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
376 
378 {
379  return (a.origin() != b.origin() || a.R() != b.R() || a.type() != b.type());
380 }
381 
382 
383 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
384 
385 Foam::Ostream& Foam::operator<<(Ostream& os, const coordinateSystem& cs)
386 {
387  cs.write(os);
388  os.check("Ostream& operator<<(Ostream&, const coordinateSystem&");
389  return os;
390 }
391 
392 
393 // ************************ vim: set sw=4 sts=4 et: ************************ //