ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / meshTools / coordinateSystems / sphericalCS.C
blobf28c87c8c1ec0f49f78068e74e7bbedb8c78015a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "sphericalCS.H"
28 #include "one.H"
29 #include "mathematicalConstants.H"
30 #include "addToRunTimeSelectionTable.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     defineTypeNameAndDebug(sphericalCS, 0);
37     addToRunTimeSelectionTable(coordinateSystem, sphericalCS, dictionary);
38     addToRunTimeSelectionTable(coordinateSystem, sphericalCS, origRotation);
42 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
44 Foam::sphericalCS::sphericalCS(const bool inDegrees)
46     coordinateSystem(),
47     inDegrees_(inDegrees)
51 Foam::sphericalCS::sphericalCS
53     const coordinateSystem& cs,
54     const bool inDegrees
57     coordinateSystem(cs),
58     inDegrees_(inDegrees)
62 Foam::sphericalCS::sphericalCS
64     const word& name,
65     const coordinateSystem& cs,
66     const bool inDegrees
69     coordinateSystem(name, cs),
70     inDegrees_(inDegrees)
74 Foam::sphericalCS::sphericalCS
76     const word& name,
77     const point& origin,
78     const coordinateRotation& cr,
79     const bool inDegrees
82     coordinateSystem(name, origin, cr),
83     inDegrees_(inDegrees)
87 Foam::sphericalCS::sphericalCS
89     const word& name,
90     const point& origin,
91     const vector& axis,
92     const vector& dirn,
93     const bool inDegrees
96     coordinateSystem(name, origin, axis, dirn),
97     inDegrees_(inDegrees)
101 Foam::sphericalCS::sphericalCS
103     const word& name,
104     const dictionary& dict
107     coordinateSystem(name, dict),
108     inDegrees_(dict.lookupOrDefault("degrees", true))
112 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
114 bool Foam::sphericalCS::inDegrees() const
116     return inDegrees_;
120 bool& Foam::sphericalCS::inDegrees()
122     return inDegrees_;
126 Foam::vector Foam::sphericalCS::localToGlobal
128     const vector& local,
129     bool translate
130 ) const
132     scalar r = local.x();
133     const scalar theta
134     (
135         local.y()
136        *(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
137     );
138     const scalar phi
139     (
140         local.z()
141        *(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
142     );
144     return coordinateSystem::localToGlobal
145     (
146         vector(r*cos(theta)*sin(phi), r*sin(theta)*sin(phi), r*cos(phi)),
147         translate
148     );
152 Foam::tmp<Foam::vectorField> Foam::sphericalCS::localToGlobal
154     const vectorField& local,
155     bool translate
156 ) const
158     const scalarField r(local.component(vector::X));
159     const scalarField theta
160     (
161         local.component(vector::Y)
162        *(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
163     );
164     const scalarField phi
165     (
166         local.component(vector::Z)
167        *(inDegrees_ ? constant::mathematical::pi/180.0 : 1.0)
168     );
170     vectorField lc(local.size());
171     lc.replace(vector::X, r*cos(theta)*sin(phi));
172     lc.replace(vector::Y, r*sin(theta)*sin(phi));
173     lc.replace(vector::Z, r*cos(phi));
175     return coordinateSystem::localToGlobal(lc, translate);
179 Foam::vector Foam::sphericalCS::globalToLocal
181     const vector& global,
182     bool translate
183 ) const
185     const vector lc = coordinateSystem::globalToLocal(global, translate);
186     const scalar r = mag(lc);
188     return vector
189     (
190         r,
191         atan2
192         (
193             lc.y(), lc.x()
194         )*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0),
195         acos
196         (
197             lc.z()/(r + SMALL)
198         )*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0)
199     );
203 Foam::tmp<Foam::vectorField> Foam::sphericalCS::globalToLocal
205     const vectorField& global,
206     bool translate
207 ) const
209     const vectorField lc(coordinateSystem::globalToLocal(global, translate));
210     const scalarField r(mag(lc));
212     tmp<vectorField> tresult(new vectorField(lc.size()));
213     vectorField& result = tresult();
215     result.replace
216     (
217         vector::X, r
219     );
221     result.replace
222     (
223         vector::Y,
224         atan2
225         (
226             lc.component(vector::Y),
227             lc.component(vector::X)
228         )*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0)
229     );
231     result.replace
232     (
233         vector::Z,
234         acos
235         (
236             lc.component(vector::Z)/(r + SMALL)
237         )*(inDegrees_ ? 180.0/constant::mathematical::pi : 1.0)
238     );
240     return tresult;
244 // ************************************************************************* //