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

coordinateSystem.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "IOstream.H"
00028 #include "coordinateSystem.H"
00029 #include "coordinateSystems.H"
00030 #include "addToRunTimeSelectionTable.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036     defineTypeNameAndDebug(coordinateSystem, 0);
00037     defineRunTimeSelectionTable(coordinateSystem, dictionary);
00038     defineRunTimeSelectionTable(coordinateSystem, origRotation);
00039 }
00040 
00041 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00042 
00043 Foam::coordinateSystem::coordinateSystem()
00044 :
00045     name_(type()),
00046     note_(),
00047     origin_(point::zero),
00048     R_(),
00049     Rtr_(sphericalTensor::I)
00050 {}
00051 
00052 
00053 Foam::coordinateSystem::coordinateSystem
00054 (
00055     const word& name,
00056     const coordinateSystem& cs
00057 )
00058 :
00059     name_(name),
00060     note_(),
00061     origin_(cs.origin_),
00062     R_(cs.R_),
00063     Rtr_(Rtr_)
00064 {}
00065 
00066 
00067 Foam::coordinateSystem::coordinateSystem
00068 (
00069     const word& name,
00070     const point& origin,
00071     const coordinateRotation& cr
00072 )
00073 :
00074     name_(name),
00075     note_(),
00076     origin_(origin),
00077     R_(cr),
00078     Rtr_(R_.T())
00079 {}
00080 
00081 
00082 Foam::coordinateSystem::coordinateSystem
00083 (
00084     const word& name,
00085     const point& origin,
00086     const vector& axis,
00087     const vector& dirn
00088 )
00089 :
00090     name_(name),
00091     note_(),
00092     origin_(origin),
00093     R_(axis, dirn),
00094     Rtr_(R_.T())
00095 {}
00096 
00097 
00098 Foam::coordinateSystem::coordinateSystem
00099 (
00100     const word& name,
00101     const dictionary& dict
00102 )
00103 :
00104     name_(name),
00105     note_(),
00106     origin_(point::zero),
00107     R_(),
00108     Rtr_(sphericalTensor::I)
00109 {
00110     operator=(dict);
00111 }
00112 
00113 
00114 Foam::coordinateSystem::coordinateSystem
00115 (
00116     const dictionary& dict
00117 )
00118 :
00119     name_(type()),
00120     note_(),
00121     origin_(point::zero),
00122     R_(),
00123     Rtr_(sphericalTensor::I)
00124 {
00125     operator=(dict);
00126 }
00127 
00128 
00129 Foam::coordinateSystem::coordinateSystem
00130 (
00131     const dictionary& dict,
00132     const objectRegistry& obr
00133 )
00134 :
00135     name_(type()),
00136     note_(),
00137     origin_(point::zero),
00138     R_(),
00139     Rtr_(sphericalTensor::I)
00140 {
00141     const entry* entryPtr = dict.lookupEntryPtr(typeName_(), false, false);
00142 
00143     // a simple entry is a lookup into global coordinateSystems
00144     if (entryPtr && !entryPtr->isDict())
00145     {
00146         word csName;
00147         entryPtr->stream() >> csName;
00148 
00149         const coordinateSystems& csLst = coordinateSystems::New(obr);
00150 
00151         label csId = csLst.find(csName);
00152         if (debug)
00153         {
00154             Info<< "coordinateSystem::coordinateSystem"
00155                 "(const dictionary&, const objectRegistry&):"
00156                 << nl << "using global coordinate system: "
00157                 << csName << "=" << csId << endl;
00158         }
00159 
00160         if (csId < 0)
00161         {
00162             FatalErrorIn
00163             (
00164                 "coordinateSystem::coordinateSystem"
00165                 "(const dictionary&, const objectRegistry&)"
00166             )   << "could not find coordinate system: " << csName << nl
00167                 << "available coordinate systems: " << csLst.toc() << nl << nl
00168                 << exit(FatalError);
00169         }
00170 
00171         // copy coordinateSystem, but assign the name as the typeName
00172         // to avoid strange things in writeDict()
00173         operator=(csLst[csId]);
00174         name_ = typeName_();
00175     }
00176     else
00177     {
00178         operator=(dict);
00179     }
00180 }
00181 
00182 
00183 Foam::coordinateSystem::coordinateSystem(Istream& is)
00184 :
00185     name_(is),
00186     note_(),
00187     origin_(point::zero),
00188     R_(),
00189     Rtr_(sphericalTensor::I)
00190 {
00191     dictionary dict(is);
00192     operator=(dict);
00193 }
00194 
00195 
00196 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00197 
00198 Foam::coordinateSystem::~coordinateSystem()
00199 {}
00200 
00201 
00202 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00203 
00204 Foam::dictionary Foam::coordinateSystem::dict(bool ignoreType) const
00205 {
00206     dictionary dict;
00207 
00208     dict.add("name", name_);
00209 
00210     // only write type for derived types
00211     if (!ignoreType && type() != typeName_())
00212     {
00213         dict.add("type", type());
00214     }
00215 
00216     // The note entry is optional
00217     if (note_.size())
00218     {
00219         dict.add("note", note_);
00220     }
00221 
00222     dict.add("origin", origin_);
00223     dict.add("e1", e1());
00224     dict.add("e3", e3());
00225 
00226     return dict;
00227 }
00228 
00229 
00230 Foam::vector Foam::coordinateSystem::localToGlobal
00231 (
00232     const vector& local,
00233     bool translate
00234 ) const
00235 {
00236     if (translate)
00237     {
00238         return (R_ & local) + origin_;
00239     }
00240     else
00241     {
00242         return (R_ & local);
00243     }
00244 }
00245 
00246 
00247 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::localToGlobal
00248 (
00249     const vectorField& local,
00250     bool translate
00251 ) const
00252 {
00253     if (translate)
00254     {
00255         return (R_ & local) + origin_;
00256     }
00257     else
00258     {
00259         return (R_ & local);
00260     }
00261 }
00262 
00263 
00264 Foam::vector Foam::coordinateSystem::globalToLocal
00265 (
00266     const vector& global,
00267     bool translate
00268 ) const
00269 {
00270     if (translate)
00271     {
00272         return (Rtr_ & (global - origin_));
00273     }
00274     else
00275     {
00276         return (Rtr_ & global);
00277     }
00278 }
00279 
00280 
00281 Foam::tmp<Foam::vectorField> Foam::coordinateSystem::globalToLocal
00282 (
00283     const vectorField& global,
00284     bool translate
00285 ) const
00286 {
00287     if (translate)
00288     {
00289         return (Rtr_ & (global - origin_));
00290     }
00291     else
00292     {
00293         return (Rtr_ & global);
00294     }
00295 }
00296 
00297 
00298 void Foam::coordinateSystem::write(Ostream& os) const
00299 {
00300     os  << type()
00301         << " origin: " << origin() << " e1: " << e1() << " e3: " << e3();
00302 }
00303 
00304 
00305 void Foam::coordinateSystem::writeDict(Ostream& os, bool subDict) const
00306 {
00307     if (subDict)
00308     {
00309         os  << indent << name_ << nl
00310             << indent << token::BEGIN_BLOCK << incrIndent << nl;
00311     }
00312 
00313     // only write type for derived types
00314     if (type() != typeName_())
00315     {
00316         os.writeKeyword("type") << type() << token::END_STATEMENT << nl;
00317     }
00318 
00319     // The note entry is optional
00320     if (note_.size())
00321     {
00322         os.writeKeyword("note") << note_ << token::END_STATEMENT << nl;
00323     }
00324 
00325     os.writeKeyword("origin") << origin_  << token::END_STATEMENT << nl;
00326     os.writeKeyword("e1")     << e1()     << token::END_STATEMENT << nl;
00327     os.writeKeyword("e3")     << e3()     << token::END_STATEMENT << nl;
00328 
00329     if (subDict)
00330     {
00331         os << decrIndent << indent << token::END_BLOCK << endl;
00332     }
00333 }
00334 
00335 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00336 
00337 void Foam::coordinateSystem::operator=(const dictionary& rhs)
00338 {
00339     if (debug)
00340     {
00341         Pout<< "coordinateSystem::operator=(const dictionary&) : "
00342             << "assign from " << rhs << endl;
00343     }
00344 
00345     // allow as embedded sub-dictionary "coordinateSystem"
00346     const dictionary& dict =
00347     (
00348         rhs.found(typeName_())
00349       ? rhs.subDict(typeName_())
00350       : rhs
00351     );
00352 
00353     // unspecified origin is (0 0 0)
00354     origin_ = point::zero;
00355     dict.readIfPresent("origin", origin_);
00356 
00357     // The note entry is optional
00358     note_.clear();
00359     rhs.readIfPresent("note", note_);
00360 
00361     // specify via coordinateRotation sub-dictionary
00362     if (dict.found("coordinateRotation"))
00363     {
00364         R_  = coordinateRotation::New(dict.subDict("coordinateRotation"))();
00365     }
00366     else
00367     {
00368         // let coordinateRotation constructor extract the axes specification
00369         R_ = coordinateRotation(dict);
00370     }
00371 
00372     Rtr_ = R_.T();
00373 }
00374 
00375 
00376 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00377 
00378 bool Foam::operator!=(const coordinateSystem& a, const coordinateSystem& b)
00379 {
00380     return (a.origin() != b.origin() || a.R() != b.R() || a.type() != b.type());
00381 }
00382 
00383 
00384 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00385 
00386 Foam::Ostream& Foam::operator<<(Ostream& os, const coordinateSystem& cs)
00387 {
00388     cs.write(os);
00389     os.check("Ostream& operator<<(Ostream&, const coordinateSystem&");
00390     return os;
00391 }
00392 
00393 
00394 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd