GeometricField.C 35 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2015-2019 OpenCFD Ltd.
Henry's avatar
Henry committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "GeometricField.H"
#include "Time.H"
#include "demandDrivenData.H"
29
#include "dictionary.H"
30
#include "localIOdictionary.H"
Henry's avatar
Henry committed
31 32 33 34
#include "data.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

35 36 37 38 39 40 41 42
#define checkField(gf1, gf2, op)                                               \
if ((gf1).mesh() != (gf2).mesh())                                              \
{                                                                              \
    FatalErrorInFunction                                                       \
        << "different mesh for fields "                                        \
        << (gf1).name() << " and " << (gf2).name()                             \
        << " during operation " <<  op                                         \
        << abort(FatalError);                                                  \
Henry's avatar
Henry committed
43 44 45 46 47 48 49 50 51 52 53
}


// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::readFields
(
    const dictionary& dict
)
{
54
    Internal::readField(dict, "internalField");
Henry's avatar
Henry committed
55 56 57

    boundaryField_.readField(*this, dict.subDict("boundaryField"));

58
    Type refLevel;
Henry's avatar
Henry committed
59

60 61 62
    if (dict.readIfPresent("referenceLevel", refLevel))
    {
        Field<Type>::operator+=(refLevel);
Henry's avatar
Henry committed
63 64 65

        forAll(boundaryField_, patchi)
        {
66
            boundaryField_[patchi] == boundaryField_[patchi] + refLevel;
Henry's avatar
Henry committed
67 68 69 70 71 72 73 74
        }
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::readFields()
{
75
    const localIOdictionary dict
Henry's avatar
Henry committed
76 77 78 79
    (
        IOobject
        (
            this->name(),
80 81
            this->instance(),
            this->local(),
Henry's avatar
Henry committed
82
            this->db(),
83
            IOobject::MUST_READ,
Henry's avatar
Henry committed
84 85 86
            IOobject::NO_WRITE,
            false
        ),
87
        typeName
Henry's avatar
Henry committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    );

    this->close();

    readFields(dict);
}


template<class Type, template<class> class PatchField, class GeoMesh>
bool Foam::GeometricField<Type, PatchField, GeoMesh>::readIfPresent()
{
    if
    (
        this->readOpt() == IOobject::MUST_READ
     || this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
    )
    {
105 106
        WarningInFunction
            << "read option IOobject::MUST_READ or MUST_READ_IF_MODIFIED"
Henry's avatar
Henry committed
107 108 109
            << " suggests that a read constructor for field " << this->name()
            << " would be more appropriate." << endl;
    }
110 111 112
    else if
    (
        this->readOpt() == IOobject::READ_IF_PRESENT
113
     && this->template typeHeaderOk<GeometricField<Type, PatchField, GeoMesh>>
114 115 116 117
        (
            true
        )
    )
Henry's avatar
Henry committed
118 119 120 121 122 123
    {
        readFields();

        // Check compatibility between field and mesh
        if (this->size() != GeoMesh::size(this->mesh()))
        {
124 125
            FatalIOErrorInFunction(this->readStream(typeName))
                << "   number of field elements = " << this->size()
Henry's avatar
Henry committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                << " number of mesh elements = "
                << GeoMesh::size(this->mesh())
                << exit(FatalIOError);
        }

        readOldTimeIfPresent();

        return true;
    }

    return false;
}


template<class Type, template<class> class PatchField, class GeoMesh>
bool Foam::GeometricField<Type, PatchField, GeoMesh>::readOldTimeIfPresent()
{
    // Read the old time field if present
    IOobject field0
    (
        this->name()  + "_0",
        this->time().timeName(),
        this->db(),
        IOobject::READ_IF_PRESENT,
        IOobject::AUTO_WRITE,
        this->registerObject()
    );

154 155
    if
    (
156
        field0.template typeHeaderOk<GeometricField<Type, PatchField, GeoMesh>>
157 158 159 160
        (
            true
        )
    )
Henry's avatar
Henry committed
161
    {
162 163
        DebugInFunction
            << "Reading old time level for field" << nl << this->info() << endl;
Henry's avatar
Henry committed
164 165 166 167 168 169 170

        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            field0,
            this->mesh()
        );

171 172 173 174 175
        // Ensure the old time field oriented flag is set to the parent's state
        // Note: required for backwards compatibility in case of restarting from
        // an old run where the oriented state may not have been set
        field0Ptr_->oriented() = this->oriented();

Henry's avatar
Henry committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        field0Ptr_->timeIndex_ = timeIndex_ - 1;

        if (!field0Ptr_->readOldTimeIfPresent())
        {
            field0Ptr_->oldTime();
        }

        return true;
    }

    return false;
}


// * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensionSet& ds,
    const word& patchFieldType
)
:
201
    Internal(io, mesh, ds, false),
Henry's avatar
Henry committed
202
    timeIndex_(this->time().timeIndex()),
203 204
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
205 206
    boundaryField_(mesh.boundary(), *this, patchFieldType)
{
207 208
    DebugInFunction
        << "Creating temporary" << nl << this->info() << endl;
Henry's avatar
Henry committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    readIfPresent();
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensionSet& ds,
    const wordList& patchFieldTypes,
    const wordList& actualPatchTypes
)
:
224
    Internal(io, mesh, ds, false),
Henry's avatar
Henry committed
225
    timeIndex_(this->time().timeIndex()),
226 227
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
228 229
    boundaryField_(mesh.boundary(), *this, patchFieldTypes, actualPatchTypes)
{
230 231
    DebugInFunction
        << "Creating temporary" << nl << this->info() << endl;
Henry's avatar
Henry committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245

    readIfPresent();
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensioned<Type>& dt,
    const word& patchFieldType
)
:
246
    Internal(io, mesh, dt, false),
Henry's avatar
Henry committed
247
    timeIndex_(this->time().timeIndex()),
248 249
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
250 251
    boundaryField_(mesh.boundary(), *this, patchFieldType)
{
252 253
    DebugInFunction
        << "Creating temporary" << nl << this->info() << endl;
Henry's avatar
Henry committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

    boundaryField_ == dt.value();

    readIfPresent();
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensioned<Type>& dt,
    const wordList& patchFieldTypes,
    const wordList& actualPatchTypes
)
:
271
    Internal(io, mesh, dt, false),
Henry's avatar
Henry committed
272
    timeIndex_(this->time().timeIndex()),
273 274
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
275 276
    boundaryField_(mesh.boundary(), *this, patchFieldTypes, actualPatchTypes)
{
277 278
    DebugInFunction
        << "Creating temporary" << nl << this->info() << endl;
Henry's avatar
Henry committed
279 280 281 282 283 284 285

    boundaryField_ == dt.value();

    readIfPresent();
}


286 287 288 289 290 291 292 293 294 295
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Internal& diField,
    const PtrList<PatchField<Type>>& ptfl
)
:
    Internal(io, diField),
    timeIndex_(this->time().timeIndex()),
296 297
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
298 299
    boundaryField_(this->mesh().boundary(), *this, ptfl)
{
300 301
    DebugInFunction
        << "Copy construct from components" << nl << this->info() << endl;
302 303

    readIfPresent();
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensionSet& ds,
    const Field<Type>& iField,
    const word& patchFieldType
)
:
    Internal(io, mesh, ds, iField),
    timeIndex_(this->time().timeIndex()),
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
    boundaryField_(mesh.boundary(), *this, patchFieldType)
{
    DebugInFunction
        << "Copy construct from internal field" << nl << this->info() << endl;

    readIfPresent();
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensionSet& ds,
    Field<Type>&& iField,
    const word& patchFieldType
)
:
    Internal(io, mesh, ds, std::move(iField)),
    timeIndex_(this->time().timeIndex()),
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
    boundaryField_(mesh.boundary(), *this, patchFieldType)
{
    DebugInFunction
        << "Move construct from internal field" << nl << this->info() << endl;

    readIfPresent();
350 351 352
}


Henry's avatar
Henry committed
353 354 355 356 357 358 359
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dimensionSet& ds,
    const Field<Type>& iField,
360
    const PtrList<PatchField<Type>>& ptfl
Henry's avatar
Henry committed
361 362
)
:
363
    Internal(io, mesh, ds, iField),
Henry's avatar
Henry committed
364
    timeIndex_(this->time().timeIndex()),
365 366
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
367 368
    boundaryField_(mesh.boundary(), *this, ptfl)
{
369 370 371 372 373 374 375
    DebugInFunction
        << "Copy construct from components" << nl << this->info() << endl;

    readIfPresent();
}


Henry's avatar
Henry committed
376 377 378 379
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
380 381
    const Mesh& mesh,
    const bool readOldTime
Henry's avatar
Henry committed
382 383
)
:
384
    Internal(io, mesh, dimless, false),
Henry's avatar
Henry committed
385
    timeIndex_(this->time().timeIndex()),
386 387
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
388 389 390 391 392 393 394 395
    boundaryField_(mesh.boundary())
{
    readFields();

    // Check compatibility between field and mesh

    if (this->size() != GeoMesh::size(this->mesh()))
    {
396 397
        FatalIOErrorInFunction(this->readStream(typeName))
            << "   number of field elements = " << this->size()
Henry's avatar
Henry committed
398 399 400 401
            << " number of mesh elements = " << GeoMesh::size(this->mesh())
            << exit(FatalIOError);
    }

402 403 404 405
    if (readOldTime)
    {
        readOldTimeIfPresent();
    }
Henry's avatar
Henry committed
406

407 408
    DebugInFunction
        << "Finishing read-construction" << nl << this->info() << endl;
Henry's avatar
Henry committed
409 410 411 412 413 414 415 416 417 418 419
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const Mesh& mesh,
    const dictionary& dict
)
:
420
    Internal(io, mesh, dimless, false),
Henry's avatar
Henry committed
421
    timeIndex_(this->time().timeIndex()),
422 423
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
424 425 426 427 428 429 430 431
    boundaryField_(mesh.boundary())
{
    readFields(dict);

    // Check compatibility between field and mesh

    if (this->size() != GeoMesh::size(this->mesh()))
    {
432 433
        FatalErrorInFunction
            << "   number of field elements = " << this->size()
Henry's avatar
Henry committed
434 435 436 437
            << " number of mesh elements = " << GeoMesh::size(this->mesh())
            << exit(FatalIOError);
    }

438 439
    DebugInFunction
        << "Finishing dictionary-construct" << nl << this->info() << endl;
Henry's avatar
Henry committed
440 441 442 443 444 445 446 447 448
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
:
449
    Internal(gf),
Henry's avatar
Henry committed
450
    timeIndex_(gf.timeIndex()),
451 452
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
453 454
    boundaryField_(*this, gf.boundaryField_)
{
455 456
    DebugInFunction
        << "Copy construct" << nl << this->info() << endl;
Henry's avatar
Henry committed
457 458 459 460 461 462 463 464 465 466 467 468

    if (gf.field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            *gf.field0Ptr_
        );
    }

    this->writeOpt() = IOobject::NO_WRITE;
}

469

Henry's avatar
Henry committed
470 471 472
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
473
    const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf
Henry's avatar
Henry committed
474 475
)
:
476
    Internal(tgf.constCast(), tgf.movable()),
Henry's avatar
Henry committed
477
    timeIndex_(tgf().timeIndex()),
478 479
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
480 481
    boundaryField_(*this, tgf().boundaryField_)
{
482 483
    DebugInFunction
        << "Constructing from tmp" << nl << this->info() << endl;
Henry's avatar
Henry committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497

    this->writeOpt() = IOobject::NO_WRITE;

    tgf.clear();
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
:
498
    Internal(io, gf),
Henry's avatar
Henry committed
499
    timeIndex_(gf.timeIndex()),
500 501
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
502 503
    boundaryField_(*this, gf.boundaryField_)
{
504 505 506
    DebugInFunction
        << "Copy construct, resetting IO params" << nl
        << this->info() << endl;
Henry's avatar
Henry committed
507 508 509 510 511 512 513 514 515 516 517 518

    if (!readIfPresent() && gf.field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            io.name() + "_0",
            *gf.field0Ptr_
        );
    }
}


519 520 521 522
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
523
    const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf
524 525
)
:
526
    Internal(io, tgf.constCast(), tgf.movable()),
527
    timeIndex_(tgf().timeIndex()),
528 529
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
530 531
    boundaryField_(*this, tgf().boundaryField_)
{
532 533 534
    DebugInFunction
        << "Constructing from tmp resetting IO params" << nl
        << this->info() << endl;
535 536 537 538 539 540 541

    tgf.clear();

    readIfPresent();
}


Henry's avatar
Henry committed
542 543 544 545 546 547 548
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const word& newName,
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
:
549
    Internal(newName, gf),
Henry's avatar
Henry committed
550
    timeIndex_(gf.timeIndex()),
551 552
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
553 554
    boundaryField_(*this, gf.boundaryField_)
{
555 556 557
    DebugInFunction
        << "Copy construct, resetting name" << nl
        << this->info() << endl;
Henry's avatar
Henry committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

    if (!readIfPresent() && gf.field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            newName + "_0",
            *gf.field0Ptr_
        );
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const word& newName,
574
    const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf
Henry's avatar
Henry committed
575 576
)
:
577
    Internal(newName, tgf.constCast(), tgf.movable()),
Henry's avatar
Henry committed
578
    timeIndex_(tgf().timeIndex()),
579 580
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
581 582
    boundaryField_(*this, tgf().boundaryField_)
{
583 584 585
    DebugInFunction
        << "Constructing from tmp resetting name" << nl
        << this->info() << endl;
Henry's avatar
Henry committed
586 587 588 589

    tgf.clear();
}

590

Henry's avatar
Henry committed
591 592 593 594 595 596 597 598
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const GeometricField<Type, PatchField, GeoMesh>& gf,
    const word& patchFieldType
)
:
599
    Internal(io, gf),
Henry's avatar
Henry committed
600
    timeIndex_(gf.timeIndex()),
601 602
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
603 604
    boundaryField_(this->mesh().boundary(), *this, patchFieldType)
{
605 606 607
    DebugInFunction
        << "Copy construct, resetting IO params" << nl
        << this->info() << endl;
Henry's avatar
Henry committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    boundaryField_ == gf.boundaryField_;

    if (!readIfPresent() && gf.field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            io.name() + "_0",
            *gf.field0Ptr_
        );
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const GeometricField<Type, PatchField, GeoMesh>& gf,
    const wordList& patchFieldTypes,
    const wordList& actualPatchTypes
)
:
631
    Internal(io, gf),
Henry's avatar
Henry committed
632
    timeIndex_(gf.timeIndex()),
633 634
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
Henry's avatar
Henry committed
635 636 637 638 639 640 641 642
    boundaryField_
    (
        this->mesh().boundary(),
        *this,
        patchFieldTypes,
        actualPatchTypes
    )
{
643 644 645
    DebugInFunction
        << "Copy construct, resetting IO params and patch types" << nl
        << this->info() << endl;
Henry's avatar
Henry committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659

    boundaryField_ == gf.boundaryField_;

    if (!readIfPresent() && gf.field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            io.name() + "_0",
            *gf.field0Ptr_
        );
    }
}


660 661 662 663 664 665 666 667 668
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField
(
    const IOobject& io,
    const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf,
    const wordList& patchFieldTypes,
    const wordList& actualPatchTypes
)
:
669
    Internal(io, tgf.constCast(), tgf.movable()),
670
    timeIndex_(tgf().timeIndex()),
671 672
    field0Ptr_(nullptr),
    fieldPrevIterPtr_(nullptr),
673 674 675 676 677 678 679 680
    boundaryField_
    (
        this->mesh().boundary(),
        *this,
        patchFieldTypes,
        actualPatchTypes
    )
{
681 682 683
    DebugInFunction
        << "Constructing from tmp resetting IO params and patch types" << nl
        << this->info() << endl;
684 685 686 687 688 689 690

    boundaryField_ == tgf().boundaryField_;

    tgf.clear();
}


691 692 693 694
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::tmp<Foam::GeometricField<Type, PatchField, GeoMesh>>
Foam::GeometricField<Type, PatchField, GeoMesh>::clone() const
{
695
    return tmp<GeometricField<Type, PatchField, GeoMesh>>::New(*this);
696 697 698
}


Henry's avatar
Henry committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>::~GeometricField()
{
    deleteDemandDrivenData(field0Ptr_);
    deleteDemandDrivenData(fieldPrevIterPtr_);
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
typename
713
Foam::GeometricField<Type, PatchField, GeoMesh>::Internal&
714 715 716 717
Foam::GeometricField<Type, PatchField, GeoMesh>::ref
(
    const bool updateAccessTime
)
Henry's avatar
Henry committed
718
{
719 720 721 722 723
    if (updateAccessTime)
    {
        this->setUpToDate();
        storeOldTimes();
    }
Henry's avatar
Henry committed
724 725 726 727 728 729
    return *this;
}


template<class Type, template<class> class PatchField, class GeoMesh>
typename
730
Foam::GeometricField<Type, PatchField, GeoMesh>::Internal::FieldType&
731 732 733 734
Foam::GeometricField<Type, PatchField, GeoMesh>::primitiveFieldRef
(
    const bool updateAccessTime
)
Henry's avatar
Henry committed
735
{
736 737 738 739 740
    if (updateAccessTime)
    {
        this->setUpToDate();
        storeOldTimes();
    }
Henry's avatar
Henry committed
741 742 743 744 745 746
    return *this;
}


template<class Type, template<class> class PatchField, class GeoMesh>
typename
747
Foam::GeometricField<Type, PatchField, GeoMesh>::Boundary&
748 749 750 751
Foam::GeometricField<Type, PatchField, GeoMesh>::boundaryFieldRef
(
    const bool updateAccessTime
)
Henry's avatar
Henry committed
752
{
753 754 755 756 757
    if (updateAccessTime)
    {
        this->setUpToDate();
        storeOldTimes();
    }
Henry's avatar
Henry committed
758 759 760 761 762 763 764 765 766 767 768
    return boundaryField_;
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::storeOldTimes() const
{
    if
    (
        field0Ptr_
     && timeIndex_ != this->time().timeIndex()
769
     && !this->name().endsWith("_0")
Henry's avatar
Henry committed
770 771 772
    )
    {
        storeOldTime();
773
        timeIndex_ = this->time().timeIndex();
Henry's avatar
Henry committed
774 775 776
    }

    // Correct time index
777
    //timeIndex_ = this->time().timeIndex();
Henry's avatar
Henry committed
778 779
}

780

Henry's avatar
Henry committed
781 782 783 784 785 786 787
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::storeOldTime() const
{
    if (field0Ptr_)
    {
        field0Ptr_->storeOldTime();

788 789
        DebugInFunction
            << "Storing old time field for field" << nl << this->info() << endl;
Henry's avatar
Henry committed
790 791 792 793 794 795 796 797 798 799 800

        *field0Ptr_ == *this;
        field0Ptr_->timeIndex_ = timeIndex_;

        if (field0Ptr_->field0Ptr_)
        {
            field0Ptr_->writeOpt() = this->writeOpt();
        }
    }
}

801

Henry's avatar
Henry committed
802 803 804 805 806 807 808
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::label Foam::GeometricField<Type, PatchField, GeoMesh>::nOldTimes() const
{
    if (field0Ptr_)
    {
        return field0Ptr_->nOldTimes() + 1;
    }
809 810

    return 0;
Henry's avatar
Henry committed
811 812
}

813

Henry's avatar
Henry committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
template<class Type, template<class> class PatchField, class GeoMesh>
const Foam::GeometricField<Type, PatchField, GeoMesh>&
Foam::GeometricField<Type, PatchField, GeoMesh>::oldTime() const
{
    if (!field0Ptr_)
    {
        field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            IOobject
            (
                this->name() + "_0",
                this->time().timeName(),
                this->db(),
                IOobject::NO_READ,
                IOobject::NO_WRITE,
                this->registerObject()
            ),
            *this
        );
Andrew Heather's avatar
Andrew Heather committed
833 834 835 836 837 838 839 840 841 842 843

        if (debug)
        {
            InfoInFunction
                << "created old time field " << field0Ptr_->info() << endl;

            if (debug&2)
            {
                error::printStack(Info);
            }
        }
Henry's avatar
Henry committed
844 845 846 847 848 849 850 851 852
    }
    else
    {
        storeOldTimes();
    }

    return *field0Ptr_;
}

853

Henry's avatar
Henry committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
template<class Type, template<class> class PatchField, class GeoMesh>
Foam::GeometricField<Type, PatchField, GeoMesh>&
Foam::GeometricField<Type, PatchField, GeoMesh>::oldTime()
{
    static_cast<const GeometricField<Type, PatchField, GeoMesh>&>(*this)
        .oldTime();

    return *field0Ptr_;
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::storePrevIter() const
{
    if (!fieldPrevIterPtr_)
    {
870 871 872
        DebugInFunction
            << "Allocating previous iteration field" << nl
            << this->info() << endl;
Henry's avatar
Henry committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

        fieldPrevIterPtr_ = new GeometricField<Type, PatchField, GeoMesh>
        (
            this->name() + "PrevIter",
            *this
        );
    }
    else
    {
        *fieldPrevIterPtr_ == *this;
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
const Foam::GeometricField<Type, PatchField, GeoMesh>&
Foam::GeometricField<Type, PatchField, GeoMesh>::prevIter() const
{
    if (!fieldPrevIterPtr_)
    {
893 894
        FatalErrorInFunction
            << "previous iteration field" << endl << this->info() << endl
Henry's avatar
Henry committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
            << "  not stored."
            << "  Use field.storePrevIter() at start of iteration."
            << abort(FatalError);
    }

    return *fieldPrevIterPtr_;
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::
correctBoundaryConditions()
{
    this->setUpToDate();
    storeOldTimes();
    boundaryField_.evaluate();
}


template<class Type, template<class> class PatchField, class GeoMesh>
bool Foam::GeometricField<Type, PatchField, GeoMesh>::needReference() const
{
    // Search all boundary conditions, if any are
    // fixed-value or mixed (Robin) do not set reference level for solution.

    bool needRef = true;

    forAll(boundaryField_, patchi)
    {
        if (boundaryField_[patchi].fixesValue())
        {
            needRef = false;
            break;
        }
    }

    reduce(needRef, andOp<bool>());

    return needRef;
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::relax(const scalar alpha)
{
940 941
    DebugInFunction
        << "Relaxing" << nl << this->info() << " by " << alpha << endl;
Henry's avatar
Henry committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

    operator==(prevIter() + alpha*(*this - prevIter()));
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::relax()
{
    word name = this->name();

    if
    (
        this->mesh().data::template lookupOrDefault<bool>
        (
            "finalIteration",
            false
        )
    )
    {
        name += "Final";
    }

    if (this->mesh().relaxField(name))
    {
        relax(this->mesh().fieldRelaxationFactor(name));
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::word Foam::GeometricField<Type, PatchField, GeoMesh>::select
(
    bool final
) const
{
    if (final)
    {
        return this->name() + "Final";
    }
    else
    {
        return this->name();
    }
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::writeMinMax
(
    Ostream& os
) const
{
994 995
    MinMax<Type> range = Foam::minMax(*this).value();

Henry's avatar
Henry committed
996
    os  << "min/max(" << this->name() << ") = "
997
        << range.min() << ", " << range.max() << endl;
Henry's avatar
Henry committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
}


template<class Type, template<class> class PatchField, class GeoMesh>
bool Foam::GeometricField<Type, PatchField, GeoMesh>::
writeData(Ostream& os) const
{
    os << *this;
    return os.good();
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
1013
Foam::tmp<Foam::GeometricField<Type, PatchField, GeoMesh>>
Henry's avatar
Henry committed
1014 1015
Foam::GeometricField<Type, PatchField, GeoMesh>::T() const
{
1016
    auto tresult = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
Henry's avatar
Henry committed
1017
    (
1018
        IOobject
Henry's avatar
Henry committed
1019
        (
1020 1021 1022 1023 1024 1025
            this->name() + ".T()",
            this->instance(),
            this->db()
        ),
        this->mesh(),
        this->dimensions()
Henry's avatar
Henry committed
1026 1027
    );

1028 1029
    Foam::T(tresult.ref().primitiveFieldRef(), primitiveField());
    Foam::T(tresult.ref().boundaryFieldRef(), boundaryField());
Henry's avatar
Henry committed
1030

1031
    return tresult;
Henry's avatar
Henry committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
}


template<class Type, template<class> class PatchField, class GeoMesh>
Foam::tmp
<
    Foam::GeometricField
    <
        typename Foam::GeometricField<Type, PatchField, GeoMesh>::cmptType,
        PatchField,
        GeoMesh
    >
>
Foam::GeometricField<Type, PatchField, GeoMesh>::component
(
    const direction d
) const
{
1050
    auto tresult = tmp<GeometricField<cmptType, PatchField, GeoMesh>>::New
Henry's avatar
Henry committed
1051
    (
1052
        IOobject
Henry's avatar
Henry committed
1053
        (
1054 1055 1056 1057 1058 1059
            this->name() + ".component(" + Foam::name(d) + ')',
            this->instance(),
            this->db()
        ),
        this->mesh(),
        this->dimensions()
Henry's avatar
Henry committed
1060 1061
    );

1062 1063
    Foam::component(tresult.ref().primitiveFieldRef(), primitiveField(), d);
    Foam::component(tresult.ref().boundaryFieldRef(), boundaryField(), d);
Henry's avatar
Henry committed
1064

1065
    return tresult;
Henry's avatar
Henry committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::replace
(
    const direction d,
    const GeometricField
    <
        typename GeometricField<Type, PatchField, GeoMesh>::cmptType,
        PatchField,
        GeoMesh
     >& gcf
)
{
1081
    primitiveFieldRef().replace(d, gcf.primitiveField());
1082
    boundaryFieldRef().replace(d, gcf.boundaryField());
Henry's avatar
Henry committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::replace
(
    const direction d,
    const dimensioned<cmptType>& ds
)
{
1093
    primitiveFieldRef().replace(d, ds.value());
1094
    boundaryFieldRef().replace(d, ds.value());
Henry's avatar
Henry committed
1095 1096 1097
}


1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::min
(
    const dimensioned<Type>& dt
)
{
    Foam::min(primitiveFieldRef(), primitiveField(), dt.value());
    Foam::min(boundaryFieldRef(), boundaryField(), dt.value());
}


Henry's avatar
Henry committed
1109 1110 1111 1112 1113 1114
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::max
(
    const dimensioned<Type>& dt
)
{
1115
    Foam::max(primitiveFieldRef(), primitiveField(), dt.value());
1116
    Foam::max(boundaryFieldRef(), boundaryField(), dt.value());
Henry's avatar
Henry committed
1117 1118 1119 1120
}


template<class Type, template<class> class PatchField, class GeoMesh>
1121
void Foam::GeometricField<Type, PatchField, GeoMesh>::clip
Henry's avatar
Henry committed
1122
(
1123
    const dimensioned<MinMax<Type>>& range
Henry's avatar
Henry committed
1124 1125
)
{
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    Foam::clip(primitiveFieldRef(), primitiveField(), range.value());
    Foam::clip(boundaryFieldRef(), boundaryField(), range.value());
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clip
(
    const dimensioned<Type>& minVal,
    const dimensioned<Type>& maxVal
)
{
    MinMax<Type> range(minVal.value(), maxVal.value());

    Foam::clip(primitiveFieldRef(), primitiveField(), range);
    Foam::clip(boundaryFieldRef(), boundaryField(), range);
Henry's avatar
Henry committed
1142 1143 1144
}


1145 1146 1147
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::maxMin
(
1148 1149
    const dimensioned<Type>& minVal,
    const dimensioned<Type>& maxVal
1150 1151
)
{
1152
    this->clip(minVal, maxVal);
1153 1154 1155
}


Henry's avatar
Henry committed
1156 1157 1158
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::negate()
{
1159
    primitiveFieldRef().negate();
1160
    boundaryFieldRef().negate();
Henry's avatar
Henry committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
}


// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::operator=
(
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
{
    if (this == &gf)
    {
1174 1175
        FatalErrorInFunction
            << "attempted assignment to self"
Henry's avatar
Henry committed
1176 1177 1178 1179 1180
            << abort(FatalError);
    }

    checkField(*this, gf, "=");

1181
    // Only assign field contents not ID
Henry's avatar
Henry committed
1182

1183
    ref() = gf();
1184
    boundaryFieldRef() = gf.boundaryField();
Henry's avatar
Henry committed
1185 1186 1187 1188 1189 1190
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::operator=
(
1191
    const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf
Henry's avatar
Henry committed
1192 1193
)
{
1194 1195 1196
    const auto& gf = tgf();

    if (this == &gf)
Henry's avatar
Henry committed
1197
    {
1198 1199
        FatalErrorInFunction
            << "attempted assignment to self"
Henry's avatar
Henry committed
1200 1201 1202 1203 1204
            << abort(FatalError);
    }

    checkField(*this, gf, "=");

1205
    // Only assign field contents not ID
Henry's avatar
Henry committed
1206 1207

    this->dimensions() = gf.dimensions();
1208
    this->oriented() = gf.oriented();
Henry's avatar
Henry committed
1209