00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "FitData.H"
00028 #include "surfaceFields.H"
00029 #include "volFields.H"
00030 #include "SVD.H"
00031
00032
00033
00034 template<class Form, class ExtendedStencil, class Polynomial>
00035 Foam::FitData<Form, ExtendedStencil, Polynomial>::FitData
00036 (
00037 const fvMesh& mesh,
00038 const ExtendedStencil& stencil,
00039 const bool linearCorrection,
00040 const scalar linearLimitFactor,
00041 const scalar centralWeight
00042 )
00043 :
00044 MeshObject<fvMesh, Form>(mesh),
00045 stencil_(stencil),
00046 linearCorrection_(linearCorrection),
00047 linearLimitFactor_(linearLimitFactor),
00048 centralWeight_(centralWeight),
00049 # ifdef SPHERICAL_GEOMETRY
00050 dim_(2),
00051 # else
00052 dim_(mesh.nGeometricD()),
00053 # endif
00054 minSize_(Polynomial::nTerms(dim_))
00055 {
00056
00057 if (linearLimitFactor <= SMALL || linearLimitFactor > 3)
00058 {
00059 FatalErrorIn("FitData<Polynomial>::FitData(..)")
00060 << "linearLimitFactor requested = " << linearLimitFactor
00061 << " should be between zero and 3"
00062 << exit(FatalError);
00063 }
00064 }
00065
00066
00067
00068
00069 template<class FitDataType, class ExtendedStencil, class Polynomial>
00070 void Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::findFaceDirs
00071 (
00072 vector& idir,
00073 vector& jdir,
00074 vector& kdir,
00075 const label facei
00076 )
00077 {
00078 const fvMesh& mesh = this->mesh();
00079
00080 idir = mesh.faceAreas()[facei];
00081 idir /= mag(idir);
00082
00083 # ifndef SPHERICAL_GEOMETRY
00084 if (mesh.nGeometricD() <= 2)
00085 {
00086 if (mesh.geometricD()[0] == -1)
00087 {
00088 kdir = vector(1, 0, 0);
00089 }
00090 else if (mesh.geometricD()[1] == -1)
00091 {
00092 kdir = vector(0, 1, 0);
00093 }
00094 else
00095 {
00096 kdir = vector(0, 0, 1);
00097 }
00098 }
00099 else
00100 {
00101 const face& f = mesh.faces()[facei];
00102 kdir = mesh.points()[f[0]] - mesh.faceCentres()[facei];
00103 }
00104 # else
00105
00106 kdir = mesh.faceCentres()[facei];
00107 # endif
00108
00109 if (mesh.nGeometricD() == 3)
00110 {
00111
00112 kdir -= (idir & kdir)*idir;
00113
00114 scalar magk = mag(kdir);
00115
00116 if (magk < SMALL)
00117 {
00118 FatalErrorIn("findFaceDirs(..)") << " calculated kdir = zero"
00119 << exit(FatalError);
00120 }
00121 else
00122 {
00123 kdir /= magk;
00124 }
00125 }
00126
00127 jdir = kdir ^ idir;
00128 }
00129
00130
00131 template<class FitDataType, class ExtendedStencil, class Polynomial>
00132 void Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::calcFit
00133 (
00134 scalarList& coeffsi,
00135 const List<point>& C,
00136 const scalar wLin,
00137 const label facei
00138 )
00139 {
00140 vector idir(1,0,0);
00141 vector jdir(0,1,0);
00142 vector kdir(0,0,1);
00143 findFaceDirs(idir, jdir, kdir, facei);
00144
00145
00146 scalarList wts(C.size(), scalar(1));
00147 wts[0] = centralWeight_;
00148 if (linearCorrection_)
00149 {
00150 wts[1] = centralWeight_;
00151 }
00152
00153
00154 point p0 = this->mesh().faceCentres()[facei];
00155
00156
00157
00158
00159
00160 vector d;
00161
00162
00163 scalar scale = 1;
00164
00165
00166 scalarRectangularMatrix B(C.size(), minSize_, scalar(0));
00167
00168 for(label ip = 0; ip < C.size(); ip++)
00169 {
00170 const point& p = C[ip];
00171
00172 d.x() = (p - p0)&idir;
00173 d.y() = (p - p0)&jdir;
00174 # ifndef SPHERICAL_GEOMETRY
00175 d.z() = (p - p0)&kdir;
00176 # else
00177 d.z() = mag(p) - mag(p0);
00178 # endif
00179
00180 if (ip == 0)
00181 {
00182 scale = cmptMax(cmptMag((d)));
00183 }
00184
00185
00186 d /= scale;
00187
00188 Polynomial::addCoeffs
00189 (
00190 B[ip],
00191 d,
00192 wts[ip],
00193 dim_
00194 );
00195 }
00196
00197
00198 for(label i = 0; i < B.n(); i++)
00199 {
00200 B[i][0] *= wts[0];
00201 B[i][1] *= wts[0];
00202 }
00203
00204
00205 label stencilSize = C.size();
00206 coeffsi.setSize(stencilSize);
00207
00208 bool goodFit = false;
00209 for(int iIt = 0; iIt < 8 && !goodFit; iIt++)
00210 {
00211 SVD svd(B, SMALL);
00212
00213 scalar maxCoeff = 0;
00214 label maxCoeffi = 0;
00215
00216 for(label i=0; i<stencilSize; i++)
00217 {
00218 coeffsi[i] = wts[0]*wts[i]*svd.VSinvUt()[0][i];
00219 if (mag(coeffsi[i]) > maxCoeff)
00220 {
00221 maxCoeff = mag(coeffsi[i]);
00222 maxCoeffi = i;
00223 }
00224 }
00225
00226 if (linearCorrection_)
00227 {
00228 goodFit =
00229 (mag(coeffsi[0] - wLin) < linearLimitFactor_*wLin)
00230 && (mag(coeffsi[1] - (1 - wLin)) < linearLimitFactor_*(1 - wLin))
00231 && maxCoeffi <= 1;
00232 }
00233 else
00234 {
00235
00236 goodFit =
00237 (mag(coeffsi[0] - 1.0) < linearLimitFactor_*1.0)
00238 && maxCoeffi <= 1;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 if (!goodFit)
00253
00254 {
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 wts[0] *= 10;
00269 if (linearCorrection_)
00270 {
00271 wts[1] *= 10;
00272 }
00273
00274 for(label j = 0; j < B.m(); j++)
00275 {
00276 B[0][j] *= 10;
00277 B[1][j] *= 10;
00278 }
00279
00280 for(label i = 0; i < B.n(); i++)
00281 {
00282 B[i][0] *= 10;
00283 B[i][1] *= 10;
00284 }
00285 }
00286 }
00287
00288 if (goodFit)
00289 {
00290 if (linearCorrection_)
00291 {
00292
00293 coeffsi[0] -= wLin;
00294 coeffsi[1] -= 1 - wLin;
00295 }
00296 else
00297 {
00298
00299 coeffsi[0] -= 1.0;
00300 }
00301 }
00302 else
00303 {
00304
00305
00306 WarningIn
00307 (
00308 "FitData<Polynomial>::calcFit(..)"
00309 ) << "Could not fit face " << facei
00310 << " Weights = " << coeffsi
00311 << ", reverting to linear." << nl
00312 << " Linear weights " << wLin << " " << 1 - wLin << endl;
00313
00314
00315 coeffsi = 0;
00316 }
00317 }
00318
00319
00320 template<class FitDataType, class ExtendedStencil, class Polynomial>
00321 bool Foam::FitData<FitDataType, ExtendedStencil, Polynomial>::movePoints()
00322 {
00323 calcFit();
00324 return true;
00325 }
00326
00327