Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / coordinateSystems / coordinateSystem.C
blobcfb6922f7dafc1a6322a0c6bf69bbb2b27b1bb6b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "IOstream.H"
27 #include "coordinateSystem.H"
28 #include "coordinateSystems.H"
29 #include "boundBox.H"
30 #include "addToRunTimeSelectionTable.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     defineTypeNameAndDebug(coordinateSystem, 0);
37     defineRunTimeSelectionTable(coordinateSystem, dictionary);
38     defineRunTimeSelectionTable(coordinateSystem, origRotation);
41 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
43 Foam::coordinateSystem::coordinateSystem()
45     name_(type()),
46     note_(),
47     origin_(point::zero),
48     R_(),
49     Rtr_(sphericalTensor::I)
53 Foam::coordinateSystem::coordinateSystem
55     const word& name,
56     const coordinateSystem& cs
59     name_(name),
60     note_(),
61     origin_(cs.origin_),
62     R_(cs.R_),
63     Rtr_(cs.Rtr_)
67 Foam::coordinateSystem::coordinateSystem
69     const word& name,
70     const point& origin,
71     const coordinateRotation& cr
74     name_(name),
75     note_(),
76     origin_(origin),
77     R_(cr),
78     Rtr_(R_.T())
82 Foam::coordinateSystem::coordinateSystem
84     const word& name,
85     const point& origin,
86     const vector& axis,
87     const vector& dirn
90     name_(name),
91     note_(),
92     origin_(origin),
93     R_(axis, dirn),
94     Rtr_(R_.T())
98 Foam::coordinateSystem::coordinateSystem
100     const word& name,
101     const dictionary& dict
104     name_(name),
105     note_(),
106     origin_(point::zero),
107     R_(),
108     Rtr_(sphericalTensor::I)
110     operator=(dict);
114 Foam::coordinateSystem::coordinateSystem
116     const dictionary& dict
119     name_(type()),
120     note_(),
121     origin_(point::zero),
122     R_(),
123     Rtr_(sphericalTensor::I)
125     operator=(dict);
129 Foam::coordinateSystem::coordinateSystem
131     const dictionary& dict,
132     const objectRegistry& obr
135     name_(type()),
136     note_(),
137     origin_(point::zero),
138     R_(),
139     Rtr_(sphericalTensor::I)
141     const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
143     // a simple entry is a lookup into global coordinateSystems
144     if (entryPtr && !entryPtr->isDict())
145     {
146         word csName;
147         entryPtr->stream() >> csName;
149         const coordinateSystems& csLst = coordinateSystems::New(obr);
151         label csId = csLst.find(csName);
152         if (debug)
153         {
154             Info<< "coordinateSystem::coordinateSystem"
155                 "(const dictionary&, const objectRegistry&):"
156                 << nl << "using global coordinate system: "
157                 << csName << "=" << csId << endl;
158         }
160         if (csId < 0)
161         {
162             FatalErrorIn
163             (
164                 "coordinateSystem::coordinateSystem"
165                 "(const dictionary&, const objectRegistry&)"
166             )   << "could not find coordinate system: " << csName << nl
167                 << "available coordinate systems: " << csLst.toc() << nl << nl
168                 << exit(FatalError);
169         }
171         // copy coordinateSystem, but assign the name as the typeName
172         // to avoid strange things in writeDict()
173         operator=(csLst[csId]);
174         name_ = typeName_();
175     }
176     else
177     {
178         operator=(dict);
179     }
183 Foam::coordinateSystem::coordinateSystem(Istream& is)
185     name_(is),
186     note_(),
187     origin_(point::zero),
188     R_(),
189     Rtr_(sphericalTensor::I)
191     dictionary dict(is);
192     operator=(dict);
196 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
198 Foam::coordinateSystem::~coordinateSystem()
202 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
204 Foam::coordinateSystem::spanInfo Foam::coordinateSystem::spanLimited() const
206     return spanInfo(Pair<bool>(false, false));
210 Foam::boundBox Foam::coordinateSystem::spanBounds() const
212     return boundBox::greatBox;
216 Foam::dictionary Foam::coordinateSystem::dict(bool ignoreType) const
218     dictionary dict;
220     dict.add("name", name_);
222     // only write type for derived types
223     if (!ignoreType && type() != typeName_())
224     {
225         dict.add("type", type());
226     }
228     // The note entry is optional
229     if (note_.size())
230     {
231         dict.add("note", note_);
232     }
234     dict.add("origin", origin_);
235     dict.add("e1", e1());
236     dict.add("e3", e3());
238     return dict;
242 Foam::vector Foam::coordinateSystem::localToGlobal
244     const vector& local,
245     bool translate
246 ) const
248     if (translate)
249     {
250         return (R_ & local) + origin_;
251     }
252     else
253     {
254         return (R_ & local);
255     }
259 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::localToGlobal
261     const vectorField& local,
262     bool translate
263 ) const
265     if (translate)
266     {
267         return (R_ & local) + origin_;
268     }
269     else
270     {
271         return (R_ & local);
272     }
276 Foam::vector Foam::coordinateSystem::globalToLocal
278     const vector& global,
279     bool translate
280 ) const
282     if (translate)
283     {
284         return (Rtr_ & (global - origin_));
285     }
286     else
287     {
288         return (Rtr_ & global);
289     }
293 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::globalToLocal
295     const vectorField& global,
296     bool translate
297 ) const
299     if (translate)
300     {
301         return (Rtr_ & (global - origin_));
302     }
303     else
304     {
305         return (Rtr_ & global);
306     }
310 void Foam::coordinateSystem::write(Ostream& os) const
312     os  << type()
313         << " origin: " << origin()
314         << " e1: " << e1() << " e3: " << e3();
318 void Foam::coordinateSystem::writeDict(Ostream& os, bool subDict) const
320     if (subDict)
321     {
322         os  << indent << nl
323             << indent << token::BEGIN_BLOCK << incrIndent << nl;
324     }
326     // only write type for derived types
327     if (type() != typeName_())
328     {
329         os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
330     }
332     // The note entry is optional
333     if (note_.size())
334     {
335         os.writeKeyword("note") << note_ << token::END_STATEMENT << nl;
336     }
338     os.writeKeyword("name")   << name_    << token::END_STATEMENT << nl;
339     os.writeKeyword("origin") << origin_  << token::END_STATEMENT << nl;
340     os.writeKeyword("e1")     << e1()     << token::END_STATEMENT << nl;
341     os.writeKeyword("e3")     << e3()     << token::END_STATEMENT << nl;
343     if (subDict)
344     {
345         os << decrIndent << indent << token::END_BLOCK << endl;
346     }
349 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
351 void Foam::coordinateSystem::operator=(const dictionary& rhs)
353     if (debug)
354     {
355         Pout<< "coordinateSystem::operator=(const dictionary&) : "
356             << "assign from " << rhs << endl;
357     }
359     // allow as embedded sub-dictionary "coordinateSystem"
360     const dictionary& dict =
361     (
362         rhs.found(typeName_())
363       ? rhs.subDict(typeName_())
364       : rhs
365     );
367     // unspecified origin is (0 0 0)
368     origin_ = point::zero;
369     dict.readIfPresent("origin", origin_);
371     // The note entry is optional
372     note_.clear();
373     rhs.readIfPresent("note", note_);
375     // specify via coordinateRotation sub-dictionary
376     if (dict.found("coordinateRotation"))
377     {
378         R_  = coordinateRotation::New(dict.subDict("coordinateRotation"))();
379     }
380     else
381     {
382         // let coordinateRotation constructor extract the axes specification
383         R_ = coordinateRotation(dict);
384     }
386     Rtr_ = R_.T();
390 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
392 bool Foam::operator!=(const coordinateSystem& a, const coordinateSystem& b)
394     return (a.origin() != b.origin() || a.R() != b.R() || a.type() != b.type());
398 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
400 Foam::Ostream& Foam::operator<<(Ostream& os, const coordinateSystem& cs)
402     cs.write(os);
403     os.check("Ostream& operator<<(Ostream&, const coordinateSystem&");
404     return os;
408 // ************************************************************************* //