From 263a22a67bf4ed55c37451c78d8563be96291fee Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 7 Nov 2019 17:17:09 +0000 Subject: [PATCH] subCycle: Add special treatment for nSubCycles = 1 Now running sub-cycling with nSubCycles = 1 is as efficient as running the same code without the sub-cycling loop. --- src/OpenFOAM/algorithms/subCycle/subCycle.H | 93 +++++++++++++-------- src/OpenFOAM/db/Time/subCycleTime.C | 19 ++++- 2 files changed, 75 insertions(+), 37 deletions(-) diff --git a/src/OpenFOAM/algorithms/subCycle/subCycle.H b/src/OpenFOAM/algorithms/subCycle/subCycle.H index 144274acd7..414a0f0e48 100644 --- a/src/OpenFOAM/algorithms/subCycle/subCycle.H +++ b/src/OpenFOAM/algorithms/subCycle/subCycle.H @@ -57,7 +57,7 @@ class subCycleField GeometricField& gf0_; //- Copy of the "real" old-time value of the field - GeometricField gf_0_; + tmp gf_0_; public: @@ -68,23 +68,35 @@ public: // Constructors //- Construct from field and number of sub-cycles - subCycleField(GeometricField& gf) + subCycleField + ( + GeometricField& gf, + const label nSubCycles + ) : gf_(gf), - gf0_(gf.oldTime()), - gf_0_(gf0_.name() + "_", gf0_) - {} + gf0_(gf.oldTime()) + { + if (nSubCycles > 1) + { + gf_0_ = GeometricField::New(gf0_.name() + "_", gf0_); + } + } //- Destructor ~subCycleField() { - // Reset the old-time field - gf0_ = gf_0_; + if (gf_0_.valid()) + { + // Reset the old-time field + gf0_ = gf_0_; - // Correct the time index of the field to correspond to the global time - gf_.timeIndex() = time().timeIndex(); - gf0_.timeIndex() = time().timeIndex(); + // Correct the time index of the field + // to correspond to the global time + gf_.timeIndex() = time().timeIndex(); + gf0_.timeIndex() = time().timeIndex(); + } } //- Access to time @@ -136,25 +148,33 @@ public: // Constructors //- Construct from field list and number of sub-cycles - subCycleFields(List& gfPtrs) + subCycleFields + ( + List& gfPtrs, + const label nSubCycles + ) : gfPtrs_(gfPtrs), - gf0Ptrs_(gfPtrs.size()), - gf_0Ptrs_(gfPtrs.size()) + gf0Ptrs_(gfPtrs.size()) { - forAll(gfPtrs_, i) + if (nSubCycles > 1) { - gf0Ptrs_[i] = &gfPtrs_[i]->oldTime(); + gf_0Ptrs_.setSize(gfPtrs.size()); - gf_0Ptrs_.set - ( - i, - new volScalarField + forAll(gfPtrs_, i) + { + gf0Ptrs_[i] = &gfPtrs_[i]->oldTime(); + + gf_0Ptrs_.set ( - gf0Ptrs_[i]->name() + "_", - *gf0Ptrs_[i] - ) - ); + i, + new volScalarField + ( + gf0Ptrs_[i]->name() + "_", + *gf0Ptrs_[i] + ) + ); + } } } @@ -162,15 +182,18 @@ public: //- Destructor ~subCycleFields() { - forAll(gfPtrs_, i) + if (gf_0Ptrs_.size()) { - // Reset the old-time fields - *gf0Ptrs_[i] = gf_0Ptrs_[i]; + forAll(gfPtrs_, i) + { + // Reset the old-time fields + *gf0Ptrs_[i] = gf_0Ptrs_[i]; - // Correct the time index of the fields - // to correspond to the global time - gfPtrs_[i]->timeIndex() = time().timeIndex(); - gf0Ptrs_[i]->timeIndex() = time().timeIndex(); + // Correct the time index of the fields + // to correspond to the global time + gfPtrs_[i]->timeIndex() = time().timeIndex(); + gf0Ptrs_[i]->timeIndex() = time().timeIndex(); + } } } @@ -223,11 +246,15 @@ public: const label nSubCycles ) : - SubCycleField(gf), + SubCycleField(gf, nSubCycles), subCycleTime(const_cast(this->time()), nSubCycles) { - // Update the field time index to correspond to the sub-cycle time - this->updateTimeIndex(); + if (nSubCycles > 1) + { + // Update the field time index to correspond + // to the sub-cycle time + this->updateTimeIndex(); + } } //- Disallow default bitwise copy construction diff --git a/src/OpenFOAM/db/Time/subCycleTime.C b/src/OpenFOAM/db/Time/subCycleTime.C index 7c4ad49058..ccbcb2ef28 100644 --- a/src/OpenFOAM/db/Time/subCycleTime.C +++ b/src/OpenFOAM/db/Time/subCycleTime.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -33,7 +33,10 @@ Foam::subCycleTime::subCycleTime(Time& t, const label nSubCycles) nSubCycles_(nSubCycles), subCycleIndex_(0) { - time_.subCycle(nSubCycles_); + if (nSubCycles_ > 1) + { + time_.subCycle(nSubCycles_); + } } @@ -55,7 +58,10 @@ bool Foam::subCycleTime::end() const void Foam::subCycleTime::endSubCycle() { - time_.endSubCycle(); + if (nSubCycles_ > 1) + { + time_.endSubCycle(); + } } @@ -63,8 +69,13 @@ void Foam::subCycleTime::endSubCycle() Foam::subCycleTime& Foam::subCycleTime::operator++() { - time_++; + if (nSubCycles_ > 1) + { + time_++; + } + subCycleIndex_++; + return *this; }