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 "timeVaryingMappedFixedValueFvPatchField.H"
00028 #include "Time.H"
00029 #include "triSurfaceTools.H"
00030 #include "triSurface.H"
00031 #include "vector2D.H"
00032 #include "OFstream.H"
00033 #include "long.H"
00034 #include "AverageIOField.H"
00035
00036
00037
00038 namespace Foam
00039 {
00040
00041
00042
00043 template<class Type>
00044 timeVaryingMappedFixedValueFvPatchField<Type>::
00045 timeVaryingMappedFixedValueFvPatchField
00046 (
00047 const fvPatch& p,
00048 const DimensionedField<Type, volMesh>& iF
00049 )
00050 :
00051 fixedValueFvPatchField<Type>(p, iF),
00052 setAverage_(false),
00053 referenceCS_(NULL),
00054 nearestVertex_(0),
00055 nearestVertexWeight_(0),
00056 sampleTimes_(0),
00057 startSampleTime_(-1),
00058 startSampledValues_(0),
00059 startAverage_(pTraits<Type>::zero),
00060 endSampleTime_(-1),
00061 endSampledValues_(0),
00062 endAverage_(pTraits<Type>::zero)
00063 {
00064 if (debug)
00065 {
00066 Pout<< "timeVaryingMappedFixedValue :"
00067 << " construct from fvPatch and internalField" << endl;
00068 }
00069 }
00070
00071
00072 template<class Type>
00073 timeVaryingMappedFixedValueFvPatchField<Type>::
00074 timeVaryingMappedFixedValueFvPatchField
00075 (
00076 const timeVaryingMappedFixedValueFvPatchField<Type>& ptf,
00077 const fvPatch& p,
00078 const DimensionedField<Type, volMesh>& iF,
00079 const fvPatchFieldMapper& mapper
00080 )
00081 :
00082 fixedValueFvPatchField<Type>(ptf, p, iF, mapper),
00083 setAverage_(ptf.setAverage_),
00084 referenceCS_(NULL),
00085 nearestVertex_(0),
00086 nearestVertexWeight_(0),
00087 sampleTimes_(0),
00088 startSampleTime_(-1),
00089 startSampledValues_(0),
00090 startAverage_(pTraits<Type>::zero),
00091 endSampleTime_(-1),
00092 endSampledValues_(0),
00093 endAverage_(pTraits<Type>::zero)
00094 {
00095 if (debug)
00096 {
00097 Pout<< "timeVaryingMappedFixedValue"
00098 << " : construct from mappedFixedValue and mapper" << endl;
00099 }
00100 }
00101
00102
00103 template<class Type>
00104 timeVaryingMappedFixedValueFvPatchField<Type>::
00105 timeVaryingMappedFixedValueFvPatchField
00106 (
00107 const fvPatch& p,
00108 const DimensionedField<Type, volMesh>& iF,
00109 const dictionary& dict
00110 )
00111 :
00112 fixedValueFvPatchField<Type>(p, iF),
00113 setAverage_(readBool(dict.lookup("setAverage"))),
00114 referenceCS_(NULL),
00115 nearestVertex_(0),
00116 nearestVertexWeight_(0),
00117 sampleTimes_(0),
00118 startSampleTime_(-1),
00119 startSampledValues_(0),
00120 startAverage_(pTraits<Type>::zero),
00121 endSampleTime_(-1),
00122 endSampledValues_(0),
00123 endAverage_(pTraits<Type>::zero)
00124 {
00125 if (debug)
00126 {
00127 Pout<< "timeVaryingMappedFixedValue : construct from dictionary"
00128 << endl;
00129 }
00130
00131 if (dict.found("value"))
00132 {
00133 fvPatchField<Type>::operator==(Field<Type>("value", dict, p.size()));
00134 }
00135 else
00136 {
00137 updateCoeffs();
00138 }
00139 }
00140
00141
00142 template<class Type>
00143 timeVaryingMappedFixedValueFvPatchField<Type>::
00144 timeVaryingMappedFixedValueFvPatchField
00145 (
00146 const timeVaryingMappedFixedValueFvPatchField<Type>& ptf
00147 )
00148 :
00149 fixedValueFvPatchField<Type>(ptf),
00150 setAverage_(ptf.setAverage_),
00151 referenceCS_(ptf.referenceCS_),
00152 nearestVertex_(ptf.nearestVertex_),
00153 nearestVertexWeight_(ptf.nearestVertexWeight_),
00154 sampleTimes_(ptf.sampleTimes_),
00155 startSampleTime_(ptf.startSampleTime_),
00156 startSampledValues_(ptf.startSampledValues_),
00157 startAverage_(ptf.startAverage_),
00158 endSampleTime_(ptf.endSampleTime_),
00159 endSampledValues_(ptf.endSampledValues_),
00160 endAverage_(ptf.endAverage_)
00161 {
00162 if (debug)
00163 {
00164 Pout<< "timeVaryingMappedFixedValue : copy construct"
00165 << endl;
00166 }
00167 }
00168
00169
00170
00171 template<class Type>
00172 timeVaryingMappedFixedValueFvPatchField<Type>::
00173 timeVaryingMappedFixedValueFvPatchField
00174 (
00175 const timeVaryingMappedFixedValueFvPatchField<Type>& ptf,
00176 const DimensionedField<Type, volMesh>& iF
00177 )
00178 :
00179 fixedValueFvPatchField<Type>(ptf, iF),
00180 setAverage_(ptf.setAverage_),
00181 referenceCS_(ptf.referenceCS_),
00182 nearestVertex_(ptf.nearestVertex_),
00183 nearestVertexWeight_(ptf.nearestVertexWeight_),
00184 sampleTimes_(ptf.sampleTimes_),
00185 startSampleTime_(ptf.startSampleTime_),
00186 startSampledValues_(ptf.startSampledValues_),
00187 startAverage_(ptf.startAverage_),
00188 endSampleTime_(ptf.endSampleTime_),
00189 endSampledValues_(ptf.endSampledValues_),
00190 endAverage_(ptf.endAverage_)
00191 {
00192 if (debug)
00193 {
00194 Pout<< "timeVaryingMappedFixedValue :"
00195 << " copy construct, resetting internal field" << endl;
00196 }
00197 }
00198
00199
00200
00201
00202 template<class Type>
00203 void timeVaryingMappedFixedValueFvPatchField<Type>::autoMap
00204 (
00205 const fvPatchFieldMapper& m
00206 )
00207 {
00208 fixedValueFvPatchField<Type>::autoMap(m);
00209 if (startSampledValues_.size())
00210 {
00211 startSampledValues_.autoMap(m);
00212 endSampledValues_.autoMap(m);
00213 }
00214 }
00215
00216
00217 template<class Type>
00218 void timeVaryingMappedFixedValueFvPatchField<Type>::rmap
00219 (
00220 const fvPatchField<Type>& ptf,
00221 const labelList& addr
00222 )
00223 {
00224 fixedValueFvPatchField<Type>::rmap(ptf, addr);
00225
00226 const timeVaryingMappedFixedValueFvPatchField<Type>& tiptf =
00227 refCast<const timeVaryingMappedFixedValueFvPatchField<Type> >(ptf);
00228
00229 startSampledValues_.rmap(tiptf.startSampledValues_, addr);
00230 endSampledValues_.rmap(tiptf.endSampledValues_, addr);
00231 }
00232
00233
00234 template<class Type>
00235 void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
00236 {
00237
00238
00239 pointIOField samplePoints
00240 (
00241 IOobject
00242 (
00243 "points",
00244 this->db().time().constant(),
00245 "boundaryData"/this->patch().name(),
00246 this->db(),
00247 IOobject::MUST_READ,
00248 IOobject::AUTO_WRITE,
00249 false
00250 )
00251 );
00252
00253 const fileName samplePointsFile = samplePoints.filePath();
00254
00255 if (debug)
00256 {
00257 Info<< "timeVaryingMappedFixedValueFvPatchField :"
00258 << " Read " << samplePoints.size() << " sample points from "
00259 << samplePointsFile << endl;
00260 }
00261
00262
00263
00264 if (samplePoints.size() < 3)
00265 {
00266 FatalErrorIn
00267 (
00268 "timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()"
00269 ) << "Only " << samplePoints.size() << " points read from file "
00270 << samplePoints.objectPath() << nl
00271 << "Need at least three non-colinear samplePoints"
00272 << " to be able to interpolate."
00273 << "\n on patch " << this->patch().name()
00274 << " of field " << this->dimensionedInternalField().name()
00275 << " in file " << this->dimensionedInternalField().objectPath()
00276 << exit(FatalError);
00277 }
00278
00279 const point& p0 = samplePoints[0];
00280
00281
00282 vector e1;
00283 label index1 = -1;
00284
00285 for (label i = 1; i < samplePoints.size(); i++)
00286 {
00287 e1 = samplePoints[i] - p0;
00288
00289 scalar magE1 = mag(e1);
00290
00291 if (magE1 > SMALL)
00292 {
00293 e1 /= magE1;
00294 index1 = i;
00295 break;
00296 }
00297 }
00298
00299
00300 label index2 = -1;
00301 vector e2;
00302 vector n;
00303
00304 for (label i = index1+1; i < samplePoints.size(); i++)
00305 {
00306 e2 = samplePoints[i] - p0;
00307
00308 scalar magE2 = mag(e2);
00309
00310 if (magE2 > SMALL)
00311 {
00312 e2 /= magE2;
00313
00314 n = e1^e2;
00315
00316 scalar magN = mag(n);
00317
00318 if (magN > SMALL)
00319 {
00320 index2 = i;
00321 n /= magN;
00322 break;
00323 }
00324 }
00325 }
00326
00327 if (index2 == -1)
00328 {
00329 FatalErrorIn
00330 (
00331 "timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()"
00332 ) << "Cannot find points that make valid normal." << nl
00333 << "Need at least three sample points which are not in a line."
00334 << "\n on patch " << this->patch().name()
00335 << " of field " << this->dimensionedInternalField().name()
00336 << " in file " << this->dimensionedInternalField().objectPath()
00337 << exit(FatalError);
00338 }
00339
00340 if (debug)
00341 {
00342 Info<< "timeVaryingMappedFixedValueFvPatchField :"
00343 << " Used points " << p0 << ' ' << samplePoints[index1]
00344 << ' ' << samplePoints[index2]
00345 << " to define coordinate system with normal " << n << endl;
00346 }
00347
00348 referenceCS_.reset
00349 (
00350 new coordinateSystem
00351 (
00352 "reference",
00353 p0,
00354 n,
00355 e1
00356 )
00357 );
00358
00359 tmp<vectorField> tlocalVertices
00360 (
00361 referenceCS().localPosition(samplePoints)
00362 );
00363 const vectorField& localVertices = tlocalVertices();
00364
00365
00366 List<vector2D> localVertices2D(localVertices.size());
00367 forAll(localVertices, i)
00368 {
00369 localVertices2D[i][0] = localVertices[i][0];
00370 localVertices2D[i][1] = localVertices[i][1];
00371 }
00372
00373 triSurface s(triSurfaceTools::delaunay2D(localVertices2D));
00374
00375 tmp<pointField> localFaceCentres
00376 (
00377 referenceCS().localPosition
00378 (
00379 this->patch().patch().faceCentres()
00380 )
00381 );
00382
00383 if (debug)
00384 {
00385 Pout<< "readSamplePoints :"
00386 <<" Dumping triangulated surface to triangulation.stl" << endl;
00387 s.write(this->db().time().path()/"triangulation.stl");
00388
00389 OFstream str(this->db().time().path()/"localFaceCentres.obj");
00390 Pout<< "readSamplePoints :"
00391 << " Dumping face centres to " << str.name() << endl;
00392
00393 forAll(localFaceCentres(), i)
00394 {
00395 const point& p = localFaceCentres()[i];
00396 str<< "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl;
00397 }
00398 }
00399
00400
00401 triSurfaceTools::calcInterpolationWeights
00402 (
00403 s,
00404 localFaceCentres,
00405 nearestVertex_,
00406 nearestVertexWeight_
00407 );
00408
00409
00410
00411
00412
00413 const fileName samplePointsDir = samplePointsFile.path();
00414
00415 sampleTimes_ = Time::findTimes(samplePointsDir);
00416
00417 if (debug)
00418 {
00419 Info<< "timeVaryingMappedFixedValueFvPatchField : In directory "
00420 << samplePointsDir << " found times " << timeNames(sampleTimes_)
00421 << endl;
00422 }
00423 }
00424
00425
00426 template<class Type>
00427 wordList timeVaryingMappedFixedValueFvPatchField<Type>::timeNames
00428 (
00429 const instantList& times
00430 )
00431 {
00432 wordList names(times.size());
00433
00434 forAll(times, i)
00435 {
00436 names[i] = times[i].name();
00437 }
00438 return names;
00439 }
00440
00441
00442 template<class Type>
00443 void timeVaryingMappedFixedValueFvPatchField<Type>::findTime
00444 (
00445 const fileName& instance,
00446 const fileName& local,
00447 const scalar timeVal,
00448 label& lo,
00449 label& hi
00450 ) const
00451 {
00452 lo = startSampleTime_;
00453 hi = -1;
00454
00455 for (label i = startSampleTime_+1; i < sampleTimes_.size(); i++)
00456 {
00457 if (sampleTimes_[i].value() > timeVal)
00458 {
00459 break;
00460 }
00461 else
00462 {
00463 lo = i;
00464 }
00465 }
00466
00467 if (lo == -1)
00468 {
00469 FatalErrorIn("findTime")
00470 << "Cannot find starting sampling values for current time "
00471 << timeVal << nl
00472 << "Have sampling values for times "
00473 << timeNames(sampleTimes_) << nl
00474 << "In directory "
00475 << this->db().time().constant()/"boundaryData"/this->patch().name()
00476 << "\n on patch " << this->patch().name()
00477 << " of field " << this->dimensionedInternalField().name()
00478 << " in file " << this->dimensionedInternalField().objectPath()
00479 << exit(FatalError);
00480 }
00481
00482 if (lo < sampleTimes_.size()-1)
00483 {
00484 hi = lo+1;
00485 }
00486
00487
00488 if (debug)
00489 {
00490 if (hi == -1)
00491 {
00492 Pout<< "findTime : Found time " << timeVal << " after"
00493 << " index:" << lo << " time:" << sampleTimes_[lo].value()
00494 << endl;
00495 }
00496 else
00497 {
00498 Pout<< "findTime : Found time " << timeVal << " inbetween"
00499 << " index:" << lo << " time:" << sampleTimes_[lo].value()
00500 << " and index:" << hi << " time:" << sampleTimes_[hi].value()
00501 << endl;
00502 }
00503 }
00504 }
00505
00506
00507 template<class Type>
00508 void timeVaryingMappedFixedValueFvPatchField<Type>::checkTable()
00509 {
00510
00511 if (startSampleTime_ == -1 && endSampleTime_ == -1)
00512 {
00513 readSamplePoints();
00514 }
00515
00516
00517 label lo = -1;
00518 label hi = -1;
00519
00520 findTime
00521 (
00522 this->db().time().constant(),
00523 "boundaryData"/this->patch().name(),
00524 this->db().time().value(),
00525 lo,
00526 hi
00527 );
00528
00529
00530
00531 if (lo != startSampleTime_)
00532 {
00533 startSampleTime_ = lo;
00534
00535 if (startSampleTime_ == endSampleTime_)
00536 {
00537
00538 if (debug)
00539 {
00540 Pout<< "checkTable : Setting startValues to (already read) "
00541 << "boundaryData"
00542 /this->patch().name()
00543 /sampleTimes_[startSampleTime_].name()
00544 << endl;
00545 }
00546 startSampledValues_ = endSampledValues_;
00547 startAverage_ = endAverage_;
00548 }
00549 else
00550 {
00551 if (debug)
00552 {
00553 Pout<< "checkTable : Reading startValues from "
00554 << "boundaryData"
00555 /this->patch().name()
00556 /sampleTimes_[lo].name()
00557 << endl;
00558 }
00559
00560
00561
00562 AverageIOField<Type> vals
00563 (
00564 IOobject
00565 (
00566 this->dimensionedInternalField().name(),
00567 this->db().time().constant(),
00568 "boundaryData"
00569 /this->patch().name()
00570 /sampleTimes_[startSampleTime_].name(),
00571 this->db(),
00572 IOobject::MUST_READ,
00573 IOobject::AUTO_WRITE,
00574 false
00575 )
00576 );
00577
00578 startAverage_ = vals.average();
00579 startSampledValues_ = interpolate(vals);
00580 }
00581 }
00582
00583 if (hi != endSampleTime_)
00584 {
00585 endSampleTime_ = hi;
00586
00587 if (endSampleTime_ == -1)
00588 {
00589
00590 if (debug)
00591 {
00592 Pout<< "checkTable : Clearing endValues" << endl;
00593 }
00594 endSampledValues_.clear();
00595 }
00596 else
00597 {
00598 if (debug)
00599 {
00600 Pout<< "checkTable : Reading endValues from "
00601 << "boundaryData"
00602 /this->patch().name()
00603 /sampleTimes_[endSampleTime_].name()
00604 << endl;
00605 }
00606
00607 AverageIOField<Type> vals
00608 (
00609 IOobject
00610 (
00611 this->dimensionedInternalField().name(),
00612 this->db().time().constant(),
00613 "boundaryData"
00614 /this->patch().name()
00615 /sampleTimes_[endSampleTime_].name(),
00616 this->db(),
00617 IOobject::MUST_READ,
00618 IOobject::AUTO_WRITE,
00619 false
00620 )
00621 );
00622 endAverage_ = vals.average();
00623 endSampledValues_ = interpolate(vals);
00624 }
00625 }
00626 }
00627
00628
00629 template<class Type>
00630 tmp<Field<Type> > timeVaryingMappedFixedValueFvPatchField<Type>::interpolate
00631 (
00632 const Field<Type>& sourceFld
00633 ) const
00634 {
00635 tmp<Field<Type> > tfld(new Field<Type>(nearestVertex_.size()));
00636 Field<Type>& fld = tfld();
00637
00638 forAll(fld, i)
00639 {
00640 const FixedList<label, 3>& verts = nearestVertex_[i];
00641 const FixedList<scalar, 3>& w = nearestVertexWeight_[i];
00642
00643 if (verts[2] == -1)
00644 {
00645 if (verts[1] == -1)
00646 {
00647
00648 fld[i] = sourceFld[verts[0]];
00649 }
00650 else
00651 {
00652
00653 fld[i] =
00654 w[0]*sourceFld[verts[0]]
00655 + w[1]*sourceFld[verts[1]];
00656 }
00657 }
00658 else
00659 {
00660 fld[i] =
00661 w[0]*sourceFld[verts[0]]
00662 + w[1]*sourceFld[verts[1]]
00663 + w[2]*sourceFld[verts[2]];
00664 }
00665 }
00666 return tfld;
00667 }
00668
00669
00670 template<class Type>
00671 void timeVaryingMappedFixedValueFvPatchField<Type>::updateCoeffs()
00672 {
00673 if (this->updated())
00674 {
00675 return;
00676 }
00677
00678 checkTable();
00679
00680
00681
00682 Type wantedAverage;
00683
00684 if (endSampleTime_ == -1)
00685 {
00686
00687 if (debug)
00688 {
00689 Pout<< "updateCoeffs : Sampled, non-interpolated values"
00690 << " from start time:"
00691 << sampleTimes_[startSampleTime_].name() << nl;
00692 }
00693
00694 this->operator==(startSampledValues_);
00695 wantedAverage = startAverage_;
00696 }
00697 else
00698 {
00699 scalar start = sampleTimes_[startSampleTime_].value();
00700 scalar end = sampleTimes_[endSampleTime_].value();
00701
00702 scalar s = (this->db().time().value()-start)/(end-start);
00703
00704 if (debug)
00705 {
00706 Pout<< "updateCoeffs : Sampled, interpolated values"
00707 << " between start time:"
00708 << sampleTimes_[startSampleTime_].name()
00709 << " and end time:" << sampleTimes_[endSampleTime_].name()
00710 << " with weight:" << s << endl;
00711 }
00712
00713 this->operator==((1-s)*startSampledValues_ + s*endSampledValues_);
00714 wantedAverage = (1-s)*startAverage_ + s*endAverage_;
00715 }
00716
00717
00718
00719 if (setAverage_)
00720 {
00721 const Field<Type>& fld = *this;
00722
00723 Type averagePsi =
00724 gSum(this->patch().magSf()*fld)
00725 /gSum(this->patch().magSf());
00726
00727 if (debug)
00728 {
00729 Pout<< "updateCoeffs :"
00730 << " actual average:" << averagePsi
00731 << " wanted average:" << wantedAverage
00732 << endl;
00733 }
00734
00735 if (mag(averagePsi) < VSMALL)
00736 {
00737
00738 const Type offset = wantedAverage - averagePsi;
00739 if (debug)
00740 {
00741 Pout<< "updateCoeffs :"
00742 << " offsetting with:" << offset << endl;
00743 }
00744 this->operator==(fld+offset);
00745 }
00746 else
00747 {
00748 const scalar scale = mag(wantedAverage)/mag(averagePsi);
00749
00750 if (debug)
00751 {
00752 Pout<< "updateCoeffs :"
00753 << " scaling with:" << scale << endl;
00754 }
00755 this->operator==(scale*fld);
00756 }
00757 }
00758
00759 if (debug)
00760 {
00761 Pout<< "updateCoeffs : set fixedValue to min:" << gMin(*this)
00762 << " max:" << gMax(*this) << endl;
00763 }
00764
00765 fixedValueFvPatchField<Type>::updateCoeffs();
00766 }
00767
00768
00769 template<class Type>
00770 void timeVaryingMappedFixedValueFvPatchField<Type>::write(Ostream& os) const
00771 {
00772 fvPatchField<Type>::write(os);
00773 os.writeKeyword("setAverage") << setAverage_ << token::END_STATEMENT << nl;
00774 this->writeEntry("value", os);
00775 }
00776
00777
00778
00779
00780 }
00781
00782