|
| 1 | +/*---------------------------------------------------------------------------*\ |
| 2 | + ========= | |
| 3 | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| 4 | + \\ / O peration | Website: https://openfoam.org |
| 5 | + \\ / A nd | Copyright (C) 2020 OpenFOAM Foundation |
| 6 | + \\/ M anipulation | |
| 7 | +------------------------------------------------------------------------------- |
| 8 | +License |
| 9 | + This file is part of OpenFOAM. |
| 10 | +
|
| 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 |
| 13 | + the Free Software Foundation, either version 3 of the License, or |
| 14 | + (at your option) any later version. |
| 15 | +
|
| 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 |
| 19 | + for more details. |
| 20 | +
|
| 21 | + You should have received a copy of the GNU General Public License |
| 22 | + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +
|
| 24 | +\*---------------------------------------------------------------------------*/ |
| 25 | + |
| 26 | +#include "disengagement.H" |
| 27 | +#include "addToRunTimeSelectionTable.H" |
| 28 | +#include "fvCFD.H" |
| 29 | +#include "fixedValueFvPatchField.H" |
| 30 | + |
| 31 | +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // |
| 32 | + |
| 33 | +namespace Foam |
| 34 | +{ |
| 35 | +namespace functionObjects |
| 36 | +{ |
| 37 | + defineTypeNameAndDebug(disengagement, 0); |
| 38 | + addToRunTimeSelectionTable(functionObject, disengagement, dictionary); |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +//- Add an element to the back of a list and shift all elements left. |
| 43 | +// Removes first element. This allows to only keep the information needed. |
| 44 | +template<class Type> |
| 45 | +void add_to_list_like_static_queue(List<Type>& list, const Type& value) |
| 46 | +{ |
| 47 | + for (label i = 0; i < list.size() - 1; i++) |
| 48 | + { |
| 49 | + //- Movel all elements |
| 50 | + list[i] = list[i+1]; |
| 51 | + } |
| 52 | + |
| 53 | + //- emplace last element |
| 54 | + list[list.size()-1] = value; |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // |
| 60 | + |
| 61 | +Foam::functionObjects::disengagement::disengagement |
| 62 | +( |
| 63 | + const word& name, |
| 64 | + const Time& runTime, |
| 65 | + const dictionary& dict |
| 66 | +) |
| 67 | +: |
| 68 | + fvMeshFunctionObject(name, runTime, dict), |
| 69 | + logFiles(obr_,name), |
| 70 | + phases_(mesh_.lookupObject<phaseSystem>("phaseProperties").phases()), |
| 71 | + phaseName_(dict.lookup<word>("phase")), |
| 72 | + inletPhaseName_(dict.lookup<word>("inletPhase")), |
| 73 | + inletPatch_(dict.lookup<word>("inlet")), |
| 74 | + tolerance_(dict.lookup<scalar>("tolerance")), |
| 75 | + nsamples_(dict.lookup<label>("nsamples")), |
| 76 | + direction_(dict.lookup<vector>("direction")), |
| 77 | + disengage_(dict.lookup<bool>("disengage")), |
| 78 | + phase_com_(2*nsamples_,{0.,-1.}), |
| 79 | + disengaged_(false) |
| 80 | +{ |
| 81 | + //- Check that U has fixedValue |
| 82 | + volVectorField& U = const_cast<volVectorField&>(mesh_.lookupObject<volVectorField>("U." + inletPhaseName_)); |
| 83 | + label patchI = mesh_.boundaryMesh().findPatchID(inletPatch_); |
| 84 | + volVectorField::Boundary& UBf = const_cast<volVectorField::Boundary&>(U.boundaryFieldRef()); |
| 85 | + |
| 86 | + if (!isA<fixedValueFvPatchVectorField>(UBf[patchI])) |
| 87 | + { |
| 88 | + FatalIOErrorInFunction(dict) |
| 89 | + << "Incorrect boundary condition for U." << phaseName_ << " at patch " << inletPatch_ << "\n" |
| 90 | + << "You must use fixedValue with this functionObject." |
| 91 | + << exit(FatalIOError); |
| 92 | + } |
| 93 | + |
| 94 | + functionObject::read(dict); |
| 95 | + resetName(typeName); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // |
| 100 | + |
| 101 | +Foam::functionObjects::disengagement::~disengagement() |
| 102 | +{} |
| 103 | + |
| 104 | + |
| 105 | +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // |
| 106 | + |
| 107 | +void Foam::functionObjects::disengagement::writeFileHeader(const label i) |
| 108 | +{ |
| 109 | + if (Pstream::master()) |
| 110 | + { |
| 111 | + writeHeader(file(), "phase_com"); |
| 112 | + writeCommented(file(), "time"); |
| 113 | + writeTabbed(file(), "phase_com"); |
| 114 | + writeTabbed(file(), "disengaged"); |
| 115 | + |
| 116 | + file() << endl; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +bool Foam::functionObjects::disengagement::execute() |
| 121 | +{ |
| 122 | + const volScalarField& alpha = mesh_.lookupObject<volScalarField>("alpha." + phaseName_); |
| 123 | + |
| 124 | + //- Compute phase_com |
| 125 | + scalar volume = gSum(fvc::volumeIntegrate(alpha)); |
| 126 | + volScalarField hcoord = mesh_.C()&direction_; |
| 127 | + scalar phase_com = gSum(fvc::volumeIntegrate(hcoord*alpha))/volume; |
| 128 | + Pair<scalar> holddata(mesh_.time().value(), phase_com); |
| 129 | + |
| 130 | + //- See function at the beginning of file |
| 131 | + add_to_list_like_static_queue(phase_com_,holddata); |
| 132 | + |
| 133 | + //- Skip rest if no disengagement is required |
| 134 | + if (!disengage_) return true; |
| 135 | + |
| 136 | + //- Stop if already disengaged |
| 137 | + if (disengaged_ ) return true; |
| 138 | + |
| 139 | + //- Do the check only if the list is complete |
| 140 | + if (phase_com_[0].second() > 0.) |
| 141 | + { |
| 142 | + scalar phase_com_mean_long(0.); |
| 143 | + scalar phase_com_mean_short(0.); |
| 144 | + scalar t0_long(phase_com_[0].first()); |
| 145 | + scalar deltat_long(0.); |
| 146 | + scalar deltat_short(0.); |
| 147 | + |
| 148 | + //- The first (oldest) sample is skipped to have a well-defined dt |
| 149 | + for (int i = 1; i < 2*nsamples_; i++) |
| 150 | + { |
| 151 | + scalar dt = phase_com_[i].first() - t0_long; |
| 152 | + t0_long = phase_com_[i].first(); |
| 153 | + phase_com_mean_long += dt*phase_com_[i].second(); |
| 154 | + deltat_long += dt; |
| 155 | + |
| 156 | + //- Compute average on the most recent samples |
| 157 | + if (i >= nsamples_) |
| 158 | + { |
| 159 | + phase_com_mean_short += dt*phase_com_[i].second(); |
| 160 | + deltat_short += dt; |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + phase_com_mean_long /= deltat_long; |
| 166 | + phase_com_mean_short /= deltat_short; |
| 167 | + |
| 168 | + if (phase_com_mean_long < 1e-16) return true; |
| 169 | + |
| 170 | + if (mag(phase_com_mean_long - phase_com_mean_short)/phase_com_mean_long < tolerance_) |
| 171 | + { |
| 172 | + if(!disengaged_) |
| 173 | + { |
| 174 | + Info << "functionObject::disengagement: Disengaging!\n"; |
| 175 | + } |
| 176 | + |
| 177 | + disengaged_ = true; |
| 178 | + |
| 179 | + //- Get boundary condition |
| 180 | + volVectorField& U = const_cast<volVectorField&>(mesh_.lookupObject<volVectorField>("U." + inletPhaseName_)); |
| 181 | + |
| 182 | + label patchI = mesh_.boundaryMesh().findPatchID(inletPatch_); |
| 183 | + |
| 184 | + fvPatchVectorField& UBf = const_cast<fvPatchVectorField&>(U.boundaryFieldRef()[patchI]); |
| 185 | + |
| 186 | + //- Stop the flow from entering |
| 187 | + UBf[patchI] == vector(0,0,0); |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + return true; |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | +bool Foam::functionObjects::disengagement::write() |
| 198 | +{ |
| 199 | + Info << logFiles::names(); |
| 200 | + logFiles::write(); |
| 201 | + |
| 202 | + label dis = 0; |
| 203 | + if(disengaged_) dis = 1; |
| 204 | + |
| 205 | + if (Pstream::master()) |
| 206 | + { |
| 207 | + file() << phase_com_[2*nsamples_ -1].first(); |
| 208 | + file() << tab; |
| 209 | + file() << phase_com_[2*nsamples_ -1].second(); |
| 210 | + file() << tab; |
| 211 | + file() << dis; |
| 212 | + file() << endl; |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + return true; |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +// ************************************************************************* // |
0 commit comments