1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "mathematicalConstants.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 const char* const tensor::typeName = "tensor";
41 const char* tensor::componentNames[] =
49 const tensor tensor::zero
57 const tensor tensor::one
65 const tensor tensor::max
67 VGREAT, VGREAT, VGREAT,
68 VGREAT, VGREAT, VGREAT,
69 VGREAT, VGREAT, VGREAT
73 const tensor tensor::min
75 -VGREAT, -VGREAT, -VGREAT,
76 -VGREAT, -VGREAT, -VGREAT,
77 -VGREAT, -VGREAT, -VGREAT
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
83 // Return eigenvalues in ascending order of absolute values
84 vector eigenValues(const tensor& t)
93 mag(t.xy()) + mag(t.xz()) + mag(t.yx())
94 + mag(t.yz()) + mag(t.zx()) + mag(t.zy())
106 scalar a = -t.xx() - t.yy() - t.zz();
108 scalar b = t.xx()*t.yy() + t.xx()*t.zz() + t.yy()*t.zz()
109 - t.xy()*t.yx() - t.xz()*t.zx() - t.yz()*t.zy();
111 scalar c = - t.xx()*t.yy()*t.zz() - t.xy()*t.yz()*t.zx()
112 - t.xz()*t.yx()*t.zy() + t.xz()*t.yy()*t.zx()
113 + t.xy()*t.yx()*t.zz() + t.xx()*t.yz()*t.zy();
115 // If there is a zero root
116 if (mag(c) < 1.0e-100)
118 scalar disc = sqr(a) - 4*b;
122 scalar q = -0.5*sqrt(max(0.0, disc));
130 FatalErrorIn("eigenValues(const tensor&)")
131 << "zero and complex eigenvalues in tensor: " << t
132 << abort(FatalError);
137 scalar Q = (a*a - 3*b)/9;
138 scalar R = (2*a*a*a - 9*a*b + 27*c)/54;
143 // Three different real roots
146 scalar sqrtQ = sqrt(Q);
147 scalar theta = acos(R/(Q*sqrtQ));
149 scalar m2SqrtQ = -2*sqrtQ;
152 i = m2SqrtQ*cos(theta/3) - aBy3;
153 ii = m2SqrtQ*cos((theta + mathematicalConstant::twoPi)/3)
155 iii = m2SqrtQ*cos((theta - mathematicalConstant::twoPi)/3)
160 scalar A = cbrt(R + sqrt(R2 - Q3));
162 // Three equal real roots
166 return vector(root, root, root);
171 WarningIn("eigenValues(const tensor&)")
172 << "complex eigenvalues detected for tensor: " << t
182 // Sort the eigenvalues into ascending order
183 if (mag(i) > mag(ii))
188 if (mag(ii) > mag(iii))
193 if (mag(i) > mag(ii))
198 return vector(i, ii, iii);
202 vector eigenVector(const tensor& t, const scalar lambda)
204 if (mag(lambda) < SMALL)
209 // Construct the matrix for the eigenvector problem
210 tensor A(t - lambda*I);
212 // Calculate the sub-determinants of the 3 components
213 scalar sd0 = A.yy()*A.zz() - A.yz()*A.zy();
214 scalar sd1 = A.xx()*A.zz() - A.xz()*A.zx();
215 scalar sd2 = A.xx()*A.yy() - A.xy()*A.yx();
217 scalar magSd0 = mag(sd0);
218 scalar magSd1 = mag(sd1);
219 scalar magSd2 = mag(sd2);
221 // Evaluate the eigenvector using the largest sub-determinant
222 if (magSd0 > magSd1 && magSd0 > magSd2 && magSd0 > SMALL)
227 (A.yz()*A.zx() - A.zz()*A.yx())/sd0,
228 (A.zy()*A.yx() - A.yy()*A.zx())/sd0
234 else if (magSd1 > magSd2 && magSd1 > SMALL)
238 (A.xz()*A.zy() - A.zz()*A.xy())/sd1,
240 (A.zx()*A.xy() - A.xx()*A.zy())/sd1
246 else if (magSd2 > SMALL)
250 (A.xy()*A.yz() - A.yy()*A.xz())/sd2,
251 (A.yx()*A.xz() - A.xx()*A.yz())/sd2,
265 tensor eigenVectors(const tensor& t)
267 vector evals(eigenValues(t));
270 evs.x() = eigenVector(t, evals.x());
271 evs.y() = eigenVector(t, evals.y());
272 evs.z() = eigenVector(t, evals.z());
278 // Return eigenvalues in ascending order of absolute values
279 vector eigenValues(const symmTensor& t)
288 mag(t.xy()) + mag(t.xz()) + mag(t.xy())
289 + mag(t.yz()) + mag(t.xz()) + mag(t.yz())
301 scalar a = -t.xx() - t.yy() - t.zz();
303 scalar b = t.xx()*t.yy() + t.xx()*t.zz() + t.yy()*t.zz()
304 - t.xy()*t.xy() - t.xz()*t.xz() - t.yz()*t.yz();
306 scalar c = - t.xx()*t.yy()*t.zz() - t.xy()*t.yz()*t.xz()
307 - t.xz()*t.xy()*t.yz() + t.xz()*t.yy()*t.xz()
308 + t.xy()*t.xy()*t.zz() + t.xx()*t.yz()*t.yz();
310 // If there is a zero root
311 if (mag(c) < 1.0e-100)
313 scalar disc = sqr(a) - 4*b;
317 scalar q = -0.5*sqrt(max(0.0, disc));
325 FatalErrorIn("eigenValues(const tensor&)")
326 << "zero and complex eigenvalues in tensor: " << t
327 << abort(FatalError);
332 scalar Q = (a*a - 3*b)/9;
333 scalar R = (2*a*a*a - 9*a*b + 27*c)/54;
338 // Three different real roots
341 scalar sqrtQ = sqrt(Q);
342 scalar theta = acos(R/(Q*sqrtQ));
344 scalar m2SqrtQ = -2*sqrtQ;
347 i = m2SqrtQ*cos(theta/3) - aBy3;
348 ii = m2SqrtQ*cos((theta + mathematicalConstant::twoPi)/3)
350 iii = m2SqrtQ*cos((theta - mathematicalConstant::twoPi)/3)
355 scalar A = cbrt(R + sqrt(R2 - Q3));
357 // Three equal real roots
361 return vector(root, root, root);
366 WarningIn("eigenValues(const symmTensor&)")
367 << "complex eigenvalues detected for symmTensor: " << t
377 // Sort the eigenvalues into ascending order
378 if (mag(i) > mag(ii))
383 if (mag(ii) > mag(iii))
388 if (mag(i) > mag(ii))
393 return vector(i, ii, iii);
397 vector eigenVector(const symmTensor& t, const scalar lambda)
399 if (mag(lambda) < SMALL)
404 // Construct the matrix for the eigenvector problem
405 symmTensor A(t - lambda*I);
407 // Calculate the sub-determinants of the 3 components
408 scalar sd0 = A.yy()*A.zz() - A.yz()*A.yz();
409 scalar sd1 = A.xx()*A.zz() - A.xz()*A.xz();
410 scalar sd2 = A.xx()*A.yy() - A.xy()*A.xy();
412 scalar magSd0 = mag(sd0);
413 scalar magSd1 = mag(sd1);
414 scalar magSd2 = mag(sd2);
416 // Evaluate the eigenvector using the largest sub-determinant
417 if (magSd0 > magSd1 && magSd0 > magSd2 && magSd0 > SMALL)
422 (A.yz()*A.xz() - A.zz()*A.xy())/sd0,
423 (A.yz()*A.xy() - A.yy()*A.xz())/sd0
429 else if (magSd1 > magSd2 && magSd1 > SMALL)
433 (A.xz()*A.yz() - A.zz()*A.xy())/sd1,
435 (A.xz()*A.xy() - A.xx()*A.yz())/sd1
441 else if (magSd2 > SMALL)
445 (A.xy()*A.yz() - A.yy()*A.xz())/sd2,
446 (A.xy()*A.xz() - A.xx()*A.yz())/sd2,
460 tensor eigenVectors(const symmTensor& t)
462 vector evals(eigenValues(t));
465 evs.x() = eigenVector(t, evals.x());
466 evs.y() = eigenVector(t, evals.y());
467 evs.z() = eigenVector(t, evals.z());
473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 } // End namespace Foam
477 // ************************************************************************* //