Download data/src/functionObjects/solvers/phaseScalarTransport/phaseScalarTransport.C from introvoyz042/OpenFOAM-dev: direct link, hf CLI and curl.
- Browser
- Download file 17.7 kB
-
https://huggingface.co/datasets/introvoyz042/OpenFOAM-dev/resolve/main/data/src/functionObjects/solvers/phaseScalarTransport/phaseScalarTransport.C
- Command line
-
hf download hf://datasets/introvoyz042/OpenFOAM-dev/data/src/functionObjects/solvers/phaseScalarTransport/phaseScalarTransport.C
-
curl -L -o phaseScalarTransport.C https://huggingface.co/datasets/introvoyz042/OpenFOAM-dev/resolve/main/data/src/functionObjects/solvers/phaseScalarTransport/phaseScalarTransport.C
17.7 kB
| /*---------------------------------------------------------------------------*\ | |
| ========= | | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | |
| \\ / O peration | Website: https://openfoam.org | |
| \\ / A nd | Copyright (C) 2019-2026 OpenFOAM Foundation | |
| \\/ M anipulation | | |
| ------------------------------------------------------------------------------- | |
| 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/>. | |
| \*---------------------------------------------------------------------------*/ | |
| // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // | |
| namespace Foam | |
| { | |
| namespace functionObjects | |
| { | |
| defineTypeNameAndDebug(phaseScalarTransport, 0); | |
| addToRunTimeSelectionTable | |
| ( | |
| functionObject, | |
| phaseScalarTransport, | |
| dictionary | |
| ); | |
| } | |
| } | |
| // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // | |
| Foam::volScalarField& Foam::functionObjects::phaseScalarTransport::Phi() | |
| { | |
| if (!PhiPtr_.valid()) | |
| { | |
| const surfaceScalarField& phi = | |
| mesh_.lookupObject<surfaceScalarField>(phiName_); | |
| const volScalarField& p = | |
| mesh_.lookupObject<volScalarField>(pName_); | |
| wordList PhiPatchFieldTypes(mesh_.poly().boundary().size()); | |
| forAll(p.boundaryField(), patchi) | |
| { | |
| PhiPatchFieldTypes[patchi] = | |
| p.boundaryField()[patchi].fixesValue() | |
| ? fixedValueFvPatchField<scalar>::typeName | |
| : zeroGradientFvPatchField<scalar>::typeName; | |
| } | |
| PhiPtr_.set | |
| ( | |
| new volScalarField | |
| ( | |
| IOobject | |
| ( | |
| typedName(IOobject::groupName("Phi", phaseName_)), | |
| time_.name(), | |
| mesh_, | |
| IOobject::READ_IF_PRESENT, | |
| IOobject::NO_WRITE | |
| ), | |
| mesh_, | |
| dimensionedScalar(phi.dimensions()/dimensions::length, Zero), | |
| PhiPatchFieldTypes | |
| ) | |
| ); | |
| mesh_.schemes().setFluxRequired(PhiPtr_->name()); | |
| } | |
| return PhiPtr_(); | |
| } | |
| Foam::tmp<Foam::surfaceScalarField> | |
| Foam::functionObjects::phaseScalarTransport::alphaPhi() | |
| { | |
| if (!solveAlphaPhi_) | |
| { | |
| return mesh_.lookupObject<surfaceScalarField>(alphaPhiName_); | |
| } | |
| const volScalarField& alpha = | |
| mesh_.lookupObject<volScalarField>(alphaName_); | |
| const surfaceScalarField& phi = | |
| mesh_.lookupObject<surfaceScalarField>(phiName_); | |
| // Make a crude guess of the phase flux using default interpolation | |
| tmp<surfaceScalarField> tAlphaPhi | |
| ( | |
| new surfaceScalarField | |
| ( | |
| alphaPhiName_, | |
| phi*fvc::interpolate(alpha) | |
| ) | |
| ); | |
| surfaceScalarField& alphaPhi = tAlphaPhi.ref(); | |
| // Get the potential field | |
| volScalarField& Phi(this->Phi()); | |
| // Construct the Phi Laplacian scheme names | |
| const word PhiLaplacianScheme("laplacian(", pName_, ')'); | |
| // Debug writing. Write the material derivative of alpha, before and after | |
| // the solution of the potential and the correction of alphaPhi. Before | |
| // correction the field should be non-zero, and after it should be | |
| // comparable to the solution tolerance. | |
| auto writeDDt = [&](const label i) | |
| { | |
| const volInternalScalarField DDtAlpha | |
| ( | |
| word | |
| ( | |
| "DDt(", | |
| IOobject::groupName | |
| ( | |
| IOobject::member(alpha.name()) + Foam::name(i), | |
| IOobject::group(alpha.name()) | |
| ), | |
| ')' | |
| ), | |
| fvi::ddt(alpha) + fvi::div(alphaPhi) | |
| ); | |
| Info<< type() << ": Writing " << DDtAlpha.name() << endl; | |
| DDtAlpha.write(); | |
| }; | |
| if (debug && time_.writeTime()) | |
| { | |
| writeDDt(0); | |
| } | |
| // Lookup the non-orthogonal solution control | |
| nonOrthogonalSolutionControl& control = | |
| mesh_.lookupObjectRef<nonOrthogonalSolutionControl> | |
| ( | |
| solutionControl::typeName | |
| ); | |
| // Solve for the potential and correct alphaPhi with the resulting flux | |
| if (phi.dimensions() == dimensions::volumetricFlux) | |
| { | |
| while (control.correctNonOrthogonal()) | |
| { | |
| fvScalarMatrix PhiEqn | |
| ( | |
| fvm::laplacian(Phi, PhiLaplacianScheme) | |
| + fvi::ddt(alpha) | |
| + fvi::div(alphaPhi) | |
| ); | |
| PhiEqn.solve(pName_); | |
| if (control.finalNonOrthogonalIter()) | |
| { | |
| alphaPhi += PhiEqn.flux(); | |
| } | |
| } | |
| } | |
| else if (phi.dimensions() == dimensions::massFlux) | |
| { | |
| const volScalarField& rho = | |
| mesh_.lookupObject<volScalarField>(rhoName_); | |
| while (control.correctNonOrthogonal()) | |
| { | |
| fvScalarMatrix PhiEqn | |
| ( | |
| fvm::laplacian(Phi, PhiLaplacianScheme) | |
| + fvi::ddt(rho, alpha) | |
| + fvi::div(alphaPhi) | |
| ); | |
| PhiEqn.solve(pName_); | |
| if (control.finalNonOrthogonalIter()) | |
| { | |
| alphaPhi += PhiEqn.flux(); | |
| } | |
| } | |
| } | |
| else | |
| { | |
| PhiDimensionErrorInFunction(phi); | |
| } | |
| // Debug writing | |
| if (debug && time_.writeTime()) | |
| { | |
| writeDDt(1); | |
| } | |
| return tAlphaPhi; | |
| } | |
| Foam::tmp<Foam::volScalarField> | |
| Foam::functionObjects::phaseScalarTransport::D | |
| ( | |
| const surfaceScalarField& alphaPhi | |
| ) const | |
| { | |
| if (diffusivity_ == scalarTransport::diffusivityType::field) | |
| { | |
| return mesh_.lookupObject<volScalarField>(Dname_); | |
| } | |
| if (diffusivity_ == scalarTransport::diffusivityType::viscosity) | |
| { | |
| const word& nameNoPhase = momentumTransportModel::typeName; | |
| const word namePhase = IOobject::groupName(nameNoPhase, phaseName_); | |
| // Try looking up the phase transport model, then try the mixture | |
| // transport model, then fail with an error relating to the phase | |
| // transport model | |
| const momentumTransportModel& turbulence = | |
| mesh_.foundObject<momentumTransportModel>(namePhase) | |
| ? mesh_.lookupObject<momentumTransportModel>(namePhase) | |
| : mesh_.foundObject<momentumTransportModel>(nameNoPhase) | |
| ? mesh_.lookupObject<momentumTransportModel>(nameNoPhase) | |
| : mesh_.lookupObject<momentumTransportModel>(namePhase); | |
| return volScalarField::New | |
| ( | |
| "D" + s_.name(), | |
| alphal_*turbulence.nu() + alphat_*turbulence.nut() | |
| ); | |
| } | |
| FatalErrorInFunction | |
| << "Diffusivity field requested for non-field diffusivity option '" | |
| << scalarTransport::diffusivityTypeNames_[diffusivity_] << "'" | |
| << exit(FatalError); | |
| return tmp<volScalarField>(); | |
| } | |
| // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // | |
| Foam::functionObjects::phaseScalarTransport::phaseScalarTransport | |
| ( | |
| const word& name, | |
| const Time& runTime, | |
| const dictionary& dict | |
| ) | |
| : | |
| fvMeshFunctionObject(name, runTime, dict), | |
| fieldName_(dict.lookup("field")), | |
| phaseName_(IOobject::group(fieldName_)), | |
| D_("D", dimensions::kinematicViscosity, NaN), | |
| Dname_(word::null), | |
| alphal_("alphal", dimless, NaN), | |
| alphat_("alphat", dimless, NaN), | |
| s_ | |
| ( | |
| IOobject | |
| ( | |
| fieldName_, | |
| time_.name(), | |
| mesh_, | |
| IOobject::MUST_READ, | |
| IOobject::NO_WRITE | |
| ), | |
| mesh_ | |
| ), | |
| PhiPtr_(nullptr) | |
| { | |
| if (phaseName_ == word::null) | |
| { | |
| FatalErrorInFunction | |
| << "Field \"" << fieldName_ << "\" does not have a phase extension " | |
| << "in its name. If it is associated with \"phaseA\" then it " | |
| << "should be named \"" << fieldName_ << ".phaseA\"." | |
| << exit(FatalError); | |
| } | |
| read(dict); | |
| } | |
| // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // | |
| Foam::functionObjects::phaseScalarTransport::~phaseScalarTransport() | |
| {} | |
| // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // | |
| bool Foam::functionObjects::phaseScalarTransport::read(const dictionary& dict) | |
| { | |
| fvMeshFunctionObject::read(dict); | |
| solveAlphaPhi_ = dict.lookupOrDefault<bool>("solveAlphaPhi", false); | |
| alphaName_ = | |
| dict.lookupOrDefault<word> | |
| ( | |
| "alpha", | |
| IOobject::groupName("alpha", phaseName_) | |
| ); | |
| const word defaultAlphaPhiName = | |
| IOobject::groupName("alphaPhi", phaseName_); | |
| alphaPhiName_ = | |
| solveAlphaPhi_ | |
| ? typedName(defaultAlphaPhiName) | |
| : dict.lookupOrDefault<word>("alphaPhi", defaultAlphaPhiName); | |
| phiName_ = dict.lookupOrDefault<word>("phi", "phi"); | |
| rhoName_ = | |
| dict.lookupOrDefault<word> | |
| ( | |
| "rho", | |
| IOobject::groupName("rho", phaseName_) | |
| ); | |
| pName_ = dict.lookupOrDefault<word>("p", "p"); | |
| schemesField_ = dict.lookupOrDefault<word>("schemesField", fieldName_); | |
| solverField_ = dict.lookupOrDefault<word>("solverField", fieldName_); | |
| diffusivity_ = | |
| scalarTransport::diffusivityTypeNames_.read(dict.lookup("diffusivity")); | |
| switch(diffusivity_) | |
| { | |
| case scalarTransport::diffusivityType::none: | |
| break; | |
| case scalarTransport::diffusivityType::constant: | |
| // This is used instead of D_.read(dict) in order to avoid | |
| // successfully parsing a word as the name of the dimensioned type, | |
| // and then mucking up the subsequent error message | |
| D_.value() = dict.lookup<scalar>(D_.name(), D_.dimensions()); | |
| break; | |
| case scalarTransport::diffusivityType::field: | |
| Dname_ = dict.lookup<word>("D"); | |
| break; | |
| case scalarTransport::diffusivityType::viscosity: | |
| alphal_.read(dict); | |
| alphat_.read(dict); | |
| break; | |
| } | |
| residualAlpha_ = dict.lookupOrDefault<scalar>("residualAlpha", rootSmall); | |
| writeAlphaField_ = dict.lookupOrDefault<bool>("writeAlphaField", true); | |
| return true; | |
| } | |
| Foam::wordList Foam::functionObjects::phaseScalarTransport::fields() const | |
| { | |
| return wordList{alphaName_, alphaPhiName_, phiName_, pName_}; | |
| } | |
| bool Foam::functionObjects::phaseScalarTransport::execute() | |
| { | |
| Info<< type() << ": Executing" << endl; | |
| const volScalarField& alpha = | |
| mesh_.lookupObject<volScalarField>(alphaName_); | |
| // Get the phase flux | |
| tmp<surfaceScalarField> tAlphaPhi(this->alphaPhi()); | |
| const surfaceScalarField& alphaPhi = tAlphaPhi(); | |
| const int nCorr = | |
| mesh_.solution().solverDict(solverField_) | |
| .lookupOrDefaultBackwardsCompatible<label> | |
| ( | |
| {"nCorrectors", "nCorr"}, | |
| 0 | |
| ); | |
| // Get the relaxation coefficient | |
| const scalar relaxCoeff = | |
| mesh_.solution().relaxEquation(solverField_) | |
| ? mesh_.solution().equationRelaxationFactor(solverField_) | |
| : 0; | |
| // Models and constraints | |
| const Foam::fvModels& fvModels = Foam::fvModels::New(mesh_); | |
| const Foam::fvConstraints& fvConstraints = Foam::fvConstraints::New(mesh_); | |
| // Solve | |
| if (alphaPhi.dimensions() == dimensions::volume/dimensions::time) | |
| { | |
| for (int i=0; i<=nCorr; i++) | |
| { | |
| fvScalarMatrix fieldEqn | |
| ( | |
| fvm::ddt(alpha, s_) | |
| + fvm::div | |
| ( | |
| alphaPhi, | |
| s_, | |
| word("div(", alphaPhi.name(), ',', schemesField_, ')') | |
| ) | |
| == | |
| fvModels.source(alpha, s_) | |
| - fvm::ddt(residualAlpha_, s_) | |
| + fvi::ddt(residualAlpha_, s_) | |
| ); | |
| if (diffusivity_ == scalarTransport::diffusivityType::constant) | |
| { | |
| auto alphaD = fvc::interpolate(alpha)*D_; | |
| fieldEqn -= | |
| fvm::laplacian | |
| ( | |
| alphaD, | |
| s_, | |
| word("laplacian(", alpha.name(), ',', | |
| D_.name(), ',', schemesField_, ')') | |
| ); | |
| } | |
| else if (diffusivity_ != scalarTransport::diffusivityType::none) | |
| { | |
| tmp<volScalarField> tD(this->D(alphaPhi)); | |
| fieldEqn -= | |
| fvm::laplacian | |
| ( | |
| fvc::interpolate(alpha)*fvc::interpolate(tD()), | |
| s_, | |
| word("laplacian(", alpha.name(), ',', | |
| tD().name(), ',', schemesField_, ')') | |
| ); | |
| } | |
| fieldEqn.relax(relaxCoeff); | |
| fvConstraints.constrain(fieldEqn); | |
| fieldEqn.solve(solverField_); | |
| fvConstraints.constrain(s_); | |
| } | |
| } | |
| else if (alphaPhi.dimensions() == dimensions::mass/dimensions::time) | |
| { | |
| const volScalarField& rho = | |
| mesh_.lookupObject<volScalarField>(rhoName_); | |
| for (int i=0; i<=nCorr; i++) | |
| { | |
| fvScalarMatrix fieldEqn | |
| ( | |
| fvm::ddt(alpha, rho, s_) | |
| + fvm::div | |
| ( | |
| alphaPhi, | |
| s_, | |
| word("div(", alphaPhi.name(), ',', schemesField_, ')') | |
| ) | |
| == | |
| fvModels.source(alpha, rho, s_) | |
| - fvm::ddt(residualAlpha_*rho, s_) | |
| + fvi::ddt(residualAlpha_*rho, s_) | |
| ); | |
| if (diffusivity_ == scalarTransport::diffusivityType::constant) | |
| { | |
| fieldEqn -= | |
| fvm::laplacian | |
| ( | |
| fvc::interpolate(alpha)*fvc::interpolate(rho)*D_, | |
| s_, | |
| word("laplacian(", alpha.name(), ',', rho.name(), ',', | |
| D_.name(), ',', schemesField_, ')') | |
| ); | |
| } | |
| else if (diffusivity_ != scalarTransport::diffusivityType::none) | |
| { | |
| tmp<volScalarField> tD(this->D(alphaPhi)); | |
| fieldEqn -= | |
| fvm::laplacian | |
| ( | |
| fvc::interpolate(alpha)*fvc::interpolate(rho*tD()), | |
| s_, | |
| word("laplacian(", alpha.name(), ',', rho.name(), ',', | |
| tD().name(), ',', schemesField_, ')') | |
| ); | |
| } | |
| fieldEqn.relax(relaxCoeff); | |
| fvConstraints.constrain(fieldEqn); | |
| fieldEqn.solve(solverField_); | |
| fvConstraints.constrain(s_); | |
| } | |
| } | |
| else | |
| { | |
| PhiDimensionErrorInFunction(alphaPhi); | |
| } | |
| // Update the alpha*S field | |
| if (writeAlphaField_) | |
| { | |
| if (!alphaSPtr_.valid()) | |
| { | |
| alphaSPtr_.set | |
| ( | |
| new volScalarField | |
| ( | |
| IOobject | |
| ( | |
| "alpha" | |
| + word(toupper(fieldName_[0])) | |
| + fieldName_(1, fieldName_.size() - 1), | |
| time_.name(), | |
| mesh_, | |
| IOobject::NO_READ, | |
| IOobject::NO_WRITE | |
| ), | |
| mesh_, | |
| dimensionedScalar(s_.dimensions(), Zero) | |
| ) | |
| ); | |
| } | |
| alphaSPtr_() = alpha*s_; | |
| } | |
| else | |
| { | |
| if (alphaSPtr_.valid()) | |
| { | |
| alphaSPtr_().clear(); | |
| } | |
| } | |
| Info<< endl; | |
| return true; | |
| } | |
| bool Foam::functionObjects::phaseScalarTransport::write() | |
| { | |
| s_.write(); | |
| if (alphaSPtr_.valid()) | |
| { | |
| alphaSPtr_->write(); | |
| } | |
| if (PhiPtr_.valid()) | |
| { | |
| PhiPtr_->write(); | |
| } | |
| return true; | |
| } | |
| // ************************************************************************* // | |