Report patch name instead of index in debug
[foam-extend-3.2.git] / src / foam / coordinateSystems / sphericalCS.C
blob7e8aafd7bdbb98e5970a14c7527bff6b6083eeb5
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 "sphericalCS.H"
28 #include "one.H"
29 #include "mathematicalConstants.H"
30 #include "boundBox.H"
31 #include "addToRunTimeSelectionTable.H"
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 namespace Foam
37     defineTypeNameAndDebug(sphericalCS, 0);
38     addToRunTimeSelectionTable(coordinateSystem, sphericalCS, dictionary);
39     addToRunTimeSelectionTable(coordinateSystem, sphericalCS, origRotation);
43 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
45 Foam::sphericalCS::sphericalCS(const bool inDegrees)
47     coordinateSystem(),
48     inDegrees_(inDegrees)
52 Foam::sphericalCS::sphericalCS
54     const coordinateSystem& cs,
55     const bool inDegrees
58     coordinateSystem(cs),
59     inDegrees_(inDegrees)
63 Foam::sphericalCS::sphericalCS
65     const word& name,
66     const coordinateSystem& cs,
67     const bool inDegrees
70     coordinateSystem(name, cs),
71     inDegrees_(inDegrees)
75 Foam::sphericalCS::sphericalCS
77     const word& name,
78     const point& origin,
79     const coordinateRotation& cr,
80     const bool inDegrees
83     coordinateSystem(name, origin, cr),
84     inDegrees_(inDegrees)
88 Foam::sphericalCS::sphericalCS
90     const word& name,
91     const point& origin,
92     const vector& axis,
93     const vector& dirn,
94     const bool inDegrees
97     coordinateSystem(name, origin, axis, dirn),
98     inDegrees_(inDegrees)
102 Foam::sphericalCS::sphericalCS
104     const word& name,
105     const dictionary& dict
108     coordinateSystem(name, dict),
109     inDegrees_(dict.lookupOrDefault<Switch>("degrees", true))
113 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
115 Foam::coordinateSystem::spanInfo Foam::sphericalCS::spanLimited() const
117     spanInfo b(Pair<bool>(true, true));
119     // Upper bound or r is unlimited
120     b[0] = Pair<bool>(true, false);
122     return b;
126 Foam::boundBox Foam::sphericalCS::spanBounds() const
128     return boundBox
129     (
130         vector
131         (
132             0,
133             ( inDegrees_ ? -180.0 : -mathematicalConstant::pi ),
134             ( inDegrees_ ? -90.0 : -mathematicalConstant::pi/2 )
135         ),
136         vector
137         (
138             GREAT,
139             ( inDegrees_ ? 180.0 : mathematicalConstant::pi ),
140             ( inDegrees_ ? 90.0 : mathematicalConstant::pi/2 )
141         )
142     );
146 bool Foam::sphericalCS::inDegrees() const
148     return inDegrees_;
152 Foam::Switch& Foam::sphericalCS::inDegrees()
154     return inDegrees_;
158 Foam::vector Foam::sphericalCS::localToGlobal
160     const vector& local,
161     bool translate
162 ) const
164     scalar r = local.x();
165     const scalar theta
166     (
167         local.y()
168        *( inDegrees_ ? mathematicalConstant::pi/180.0 : 1.0 )
169     );
170     const scalar phi
171     (
172         local.z()
173        *( inDegrees_ ? mathematicalConstant::pi/180.0 : 1.0 )
174     );
176     return coordinateSystem::localToGlobal
177     (
178         vector(r*cos(theta)*sin(phi), r*sin(theta)*sin(phi), r*cos(phi)),
179         translate
180     );
184 Foam::tmp<Foam::vectorField> Foam::sphericalCS::localToGlobal
186     const vectorField& local,
187     bool translate
188 ) const
190     const scalarField r = local.component(vector::X);
191     const scalarField theta
192     (
193         local.component(vector::Y)
194       * ( inDegrees_ ? mathematicalConstant::pi/180.0 : 1.0 )
195     );
196     const scalarField phi
197     (
198         local.component(vector::Z)
199       * ( inDegrees_ ? mathematicalConstant::pi/180.0 : 1.0 )
200     );
202     vectorField lc(local.size());
203     lc.replace(vector::X, r*cos(theta)*sin(phi));
204     lc.replace(vector::Y, r*sin(theta)*sin(phi));
205     lc.replace(vector::Z, r*cos(phi));
207     return coordinateSystem::localToGlobal(lc, translate);
211 Foam::vector Foam::sphericalCS::globalToLocal
213     const vector& global,
214     bool translate
215 ) const
217     const vector lc = coordinateSystem::globalToLocal(global, translate);
218     const scalar r = mag(lc);
220     return vector
221     (
222         r,
223         atan2
224         (
225             lc.y(), lc.x()
226         ) * ( inDegrees_ ? 180.0/mathematicalConstant::pi : 1.0 ),
227         acos
228         (
229             lc.z()/(r + SMALL)
230         ) * ( inDegrees_ ? 180.0/mathematicalConstant::pi : 1.0 )
231     );
235 Foam::tmp<Foam::vectorField> Foam::sphericalCS::globalToLocal
237     const vectorField& global,
238     bool translate
239 ) const
241     const vectorField lc = coordinateSystem::globalToLocal(global, translate);
242     const scalarField r = mag(lc);
244     tmp<vectorField> tresult(new vectorField(lc.size()));
245     vectorField& result = tresult();
247     result.replace
248     (
249         vector::X, r
251     );
253     result.replace
254     (
255         vector::Y,
256         atan2
257         (
258             lc.component(vector::Y),
259             lc.component(vector::X)
260         ) * ( inDegrees_ ? 180.0/mathematicalConstant::pi : 1.0 )
261     );
263     result.replace
264     (
265         vector::Z,
266         acos
267         (
268             lc.component(vector::Z)/(r + SMALL)
269         ) * ( inDegrees_ ? 180.0/mathematicalConstant::pi : 1.0 )
270     );
272     return tresult;
276 void Foam::sphericalCS::write(Ostream& os) const
278     coordinateSystem::write(os);
279     os << "inDegrees: " << inDegrees() << endl;
283 void Foam::sphericalCS::writeDict(Ostream& os, bool subDict) const
285     if (subDict)
286     {
287         os  << indent << nl
288             << indent << token::BEGIN_BLOCK << incrIndent << nl;
289     }
291     coordinateSystem::writeDict(os, false);
292     os.writeKeyword("inDegrees") << inDegrees_ << token::END_STATEMENT << nl;
294     if (subDict)
295     {
296         os << decrIndent << indent << token::END_BLOCK << endl;
297     }
301 // ************************************************************************* //