Download data/src/functionObjects/field/regionSizeDistribution/regionSizeDistribution.C from introvoyz042/OpenFOAM-dev: direct link, hf CLI and curl.
- Browser
- Download file 22.1 kB
-
https://huggingface.co/datasets/introvoyz042/OpenFOAM-dev/resolve/main/data/src/functionObjects/field/regionSizeDistribution/regionSizeDistribution.C
- Command line
-
hf download hf://datasets/introvoyz042/OpenFOAM-dev/data/src/functionObjects/field/regionSizeDistribution/regionSizeDistribution.C
-
curl -L -o regionSizeDistribution.C https://huggingface.co/datasets/introvoyz042/OpenFOAM-dev/resolve/main/data/src/functionObjects/field/regionSizeDistribution/regionSizeDistribution.C
22.1 kB
| /*---------------------------------------------------------------------------*\ | |
| ========= | | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | |
| \\ / O peration | Website: https://openfoam.org | |
| \\ / A nd | Copyright (C) 2013-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(regionSizeDistribution, 0); | |
| addToRunTimeSelectionTable | |
| ( | |
| functionObject, | |
| regionSizeDistribution, | |
| dictionary | |
| ); | |
| } | |
| //- Plus op for FixedList<scalar> | |
| template<class T, unsigned Size> | |
| class ListPlusEqOp | |
| { | |
| public: | |
| void operator() | |
| ( | |
| FixedList<T, Size>& x, | |
| const FixedList<T, Size>& y | |
| ) const | |
| { | |
| forAll(x, i) | |
| { | |
| x[i] += y[i]; | |
| } | |
| } | |
| }; | |
| } | |
| // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // | |
| void Foam::functionObjects::regionSizeDistribution::writeAlphaFields | |
| ( | |
| const fvRegionSplit& regions, | |
| const Map<label>& patchRegions, | |
| const Map<scalar>& regionVolume, | |
| const volScalarField& alpha | |
| ) const | |
| { | |
| const scalar maxDropletVol = 1.0/6.0*pow(maxDiam_, 3); | |
| // Split alpha field | |
| // ~~~~~~~~~~~~~~~~~ | |
| // Split into | |
| // - liquidCore : region connected to inlet patches | |
| // - per region a volume : for all other regions | |
| // - backgroundAlpha : remaining alpha | |
| // Construct field | |
| volScalarField liquidCore | |
| ( | |
| IOobject | |
| ( | |
| alphaName_ + "_liquidCore", | |
| time_.name(), | |
| obr_, | |
| IOobject::NO_READ | |
| ), | |
| alpha, | |
| fvPatchField<scalar>::calculatedType() | |
| ); | |
| volScalarField backgroundAlpha | |
| ( | |
| IOobject | |
| ( | |
| alphaName_ + "_background", | |
| time_.name(), | |
| obr_, | |
| IOobject::NO_READ | |
| ), | |
| alpha, | |
| fvPatchField<scalar>::calculatedType() | |
| ); | |
| // Knock out any cell not in patchRegions | |
| forAll(liquidCore, celli) | |
| { | |
| label regionI = regions[celli]; | |
| if (patchRegions.found(regionI)) | |
| { | |
| backgroundAlpha[celli] = 0; | |
| } | |
| else | |
| { | |
| liquidCore[celli] = 0; | |
| scalar regionVol = regionVolume[regionI]; | |
| if (regionVol < maxDropletVol) | |
| { | |
| backgroundAlpha[celli] = 0; | |
| } | |
| } | |
| } | |
| liquidCore.correctBoundaryConditions(); | |
| backgroundAlpha.correctBoundaryConditions(); | |
| Info<< indent << "Volume of liquid-core = " | |
| << fvc::domainIntegrate(liquidCore).value() | |
| << endl; | |
| Info<< indent << "Volume of background = " | |
| << fvc::domainIntegrate(backgroundAlpha).value() | |
| << endl; | |
| Info<< indent | |
| << "Writing liquid-core field to " << liquidCore.name() << endl; | |
| liquidCore.write(); | |
| Info<< indent | |
| << "Writing background field to " << backgroundAlpha.name() << endl; | |
| backgroundAlpha.write(); | |
| } | |
| Foam::Map<Foam::label> | |
| Foam::functionObjects::regionSizeDistribution::findPatchRegions | |
| ( | |
| const fvRegionSplit& regions | |
| ) const | |
| { | |
| // Mark all regions starting at patches | |
| // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| // Count number of patch faces (just for initial sizing) | |
| const labelHashSet patchIDs(mesh_.poly().boundary().patchSet(patchNames_)); | |
| label nPatchFaces = 0; | |
| forAllConstIter(labelHashSet, patchIDs, iter) | |
| { | |
| nPatchFaces += mesh_.poly().boundary()[iter.key()].size(); | |
| } | |
| Map<label> patchRegions(nPatchFaces); | |
| forAllConstIter(labelHashSet, patchIDs, iter) | |
| { | |
| const polyPatch& pp = mesh_.poly().boundary()[iter.key()]; | |
| // Collect all regions on the patch | |
| const labelList& faceCells = pp.faceCells(); | |
| forAll(faceCells, i) | |
| { | |
| patchRegions.insert | |
| ( | |
| regions[faceCells[i]], | |
| Pstream::myProcNo() // dummy value | |
| ); | |
| } | |
| } | |
| // Make sure all the processors have the same set of regions | |
| Pstream::mapCombineGather(patchRegions, minEqOp()); | |
| Pstream::mapCombineScatter(patchRegions); | |
| return patchRegions; | |
| } | |
| template<class Type> | |
| Foam::tmp<Foam::Field<Type>> | |
| Foam::functionObjects::regionSizeDistribution::divide | |
| ( | |
| const Field<Type>& num, | |
| const scalarField& denom | |
| ) | |
| { | |
| tmp<Field<Type>> tresult(new Field<Type>(num.size())); | |
| Field<Type>& result = tresult.ref(); | |
| forAll(denom, i) | |
| { | |
| if (denom[i] != 0) | |
| { | |
| result[i] = num[i]/denom[i]; | |
| } | |
| else | |
| { | |
| result[i] = Zero; | |
| } | |
| } | |
| return tresult; | |
| } | |
| template<class Type> | |
| void Foam::functionObjects::regionSizeDistribution::generateFields | |
| ( | |
| const word& fieldName, // name of field | |
| const labelList& indices, // index of bin for each region | |
| const Field<Type>& sortedField, // per region field data | |
| const scalarField& binCount, // per bin number of regions | |
| wordList& fieldNames, | |
| PtrList<Field<Type>>& fields | |
| ) const | |
| { | |
| if (Pstream::master()) | |
| { | |
| // Calculate per-bin sum | |
| Field<Type> binSum(nBins_, Zero); | |
| forAll(sortedField, i) | |
| { | |
| binSum[indices[i]] += sortedField[i]; | |
| } | |
| // Calculate per-bin average | |
| Field<Type> binAvg(divide(binSum, binCount)); | |
| // Append | |
| fields.setSize(fieldNames.size()); | |
| fieldNames.append(fieldName + "_sum"); | |
| fields.append(binSum); | |
| fieldNames.append(fieldName + "_avg"); | |
| fields.append(binAvg); | |
| } | |
| } | |
| void Foam::functionObjects::regionSizeDistribution::generateFields | |
| ( | |
| const word& fieldName, // name of field | |
| const labelList& indices, // index of bin for each region | |
| const scalarField& sortedField, // per region field data | |
| const scalarField& binCount, // per bin number of regions | |
| wordList& fieldNames, | |
| PtrList<scalarField>& fields | |
| ) const | |
| { | |
| if (Pstream::master()) | |
| { | |
| // Calculate per-bin sum | |
| scalarField binSum(nBins_, Zero); | |
| forAll(sortedField, i) | |
| { | |
| binSum[indices[i]] += sortedField[i]; | |
| } | |
| // Calculate per-bin average | |
| scalarField binAvg(divide(binSum, binCount)); | |
| // Calculate per-bin deviation | |
| scalarField binSqrSum(nBins_, Zero); | |
| forAll(sortedField, i) | |
| { | |
| binSqrSum[indices[i]] += Foam::sqr(sortedField[i]); | |
| } | |
| scalarField binDev | |
| ( | |
| sqrt(divide(binSqrSum, binCount) - Foam::sqr(binAvg)) | |
| ); | |
| // Append | |
| fields.setSize(fieldNames.size()); | |
| fieldNames.append(fieldName + "_sum"); | |
| fields.append(binSum); | |
| fieldNames.append(fieldName + "_avg"); | |
| fields.append(binAvg); | |
| fieldNames.append(fieldName + "_dev"); | |
| fields.append(binDev); | |
| } | |
| } | |
| template<class Type> | |
| void Foam::functionObjects::regionSizeDistribution::generateFields | |
| ( | |
| const word& fieldName, // name of field | |
| const Field<Type>& cellField, // per cell field data | |
| const fvRegionSplit& regions, // per cell the region(=droplet) | |
| const labelList& sortedRegions, // valid regions in sorted order | |
| const scalarField& sortedNormalisation, | |
| const labelList& indices, // index of bin for each region | |
| const scalarField& binCount, // per bin number of regions | |
| wordList& fieldNames, | |
| PtrList<Field<Type>>& fields | |
| ) const | |
| { | |
| // Sum on a per-region basis. Parallel reduced. | |
| Map<Type> regionField(regionSum(regions, cellField)); | |
| // Extract in region order | |
| Field<Type> sortedField | |
| ( | |
| sortedNormalisation*extractData(sortedRegions, regionField) | |
| ); | |
| // Generate fields | |
| generateFields | |
| ( | |
| fieldName, // name of field | |
| indices, // index of bin for each region | |
| sortedField, // per region field data | |
| binCount, // per bin number of regions | |
| fieldNames, | |
| fields | |
| ); | |
| } | |
| // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // | |
| Foam::functionObjects::regionSizeDistribution::regionSizeDistribution | |
| ( | |
| const word& name, | |
| const Time& runTime, | |
| const dictionary& dict | |
| ) | |
| : | |
| fvMeshFunctionObject(name, runTime, dict), | |
| file_(obr_, name), | |
| alphaName_(dict.lookup("alpha")), | |
| patchNames_(dict.lookup("patches")) | |
| { | |
| read(dict); | |
| } | |
| // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // | |
| Foam::functionObjects::regionSizeDistribution::~regionSizeDistribution() | |
| {} | |
| // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // | |
| bool Foam::functionObjects::regionSizeDistribution::read(const dictionary& dict) | |
| { | |
| dict.lookup("alpha") >> alphaName_; | |
| dict.lookup("patches") >> patchNames_; | |
| dict.lookup("threshold") >> threshold_; | |
| dict.lookup("maxDiameter") >> maxDiam_; | |
| minDiam_ = 0.0; | |
| dict.readIfPresent("minDiameter", minDiam_); | |
| dict.lookup("nBins") >> nBins_; | |
| dict.lookup("fields") >> fields_; | |
| formatterPtr_ = setWriter::New(dict.lookup("setFormat"), dict); | |
| return true; | |
| } | |
| Foam::wordList Foam::functionObjects::regionSizeDistribution::fields() const | |
| { | |
| wordList fields(fields_); | |
| fields.append(alphaName_); | |
| return fields; | |
| } | |
| bool Foam::functionObjects::regionSizeDistribution::execute() | |
| { | |
| return true; | |
| } | |
| bool Foam::functionObjects::regionSizeDistribution::write() | |
| { | |
| Info<< indent << type() << " " << name() << " write:" << nl; | |
| autoPtr<volScalarField> alphaPtr; | |
| if (obr_.foundObject<volScalarField>(alphaName_)) | |
| { | |
| Info<< indent << "Looking up field " << alphaName_ << endl; | |
| } | |
| else | |
| { | |
| Info<< indent << "Reading field " << alphaName_ << endl; | |
| alphaPtr.reset | |
| ( | |
| new volScalarField | |
| ( | |
| IOobject | |
| ( | |
| alphaName_, | |
| time_.name(), | |
| mesh_, | |
| IOobject::MUST_READ, | |
| IOobject::NO_WRITE | |
| ), | |
| mesh_ | |
| ) | |
| ); | |
| } | |
| const volScalarField& alpha = | |
| ( | |
| alphaPtr.valid() | |
| ? alphaPtr() | |
| : obr_.lookupObject<volScalarField>(alphaName_) | |
| ); | |
| Info<< indent << "Volume of alpha = " | |
| << fvc::domainIntegrate(alpha).value() | |
| << endl; | |
| const scalar meshVol = gSum(mesh_.V().primitiveField()); | |
| const scalar maxDropletVol = 1.0/6.0*pow(maxDiam_, 3); | |
| const scalar delta = (maxDiam_-minDiam_)/nBins_; | |
| Info<< indent << "Mesh volume = " << meshVol << endl; | |
| Info<< indent << "Maximum droplet diameter = " << maxDiam_ << endl; | |
| Info<< indent << "Maximum droplet volume = " << maxDropletVol << endl; | |
| fvRegionSplit regions(mesh_, alpha, threshold_); | |
| Info<< indent << "Determined " << regions.nRegions() | |
| << " disconnected regions" << endl; | |
| if (debug) | |
| { | |
| volScalarField region | |
| ( | |
| IOobject | |
| ( | |
| "region", | |
| time_.name(), | |
| mesh_, | |
| IOobject::NO_READ, | |
| IOobject::NO_WRITE | |
| ), | |
| mesh_, | |
| dimensionedScalar(dimless, 0) | |
| ); | |
| Info<< indent << "Dumping region as volScalarField to " << region.name() | |
| << endl; | |
| forAll(regions, celli) | |
| { | |
| region[celli] = regions[celli]; | |
| } | |
| region.correctBoundaryConditions(); | |
| region.write(); | |
| } | |
| // Determine regions connected to supplied patches | |
| Map<label> patchRegions(findPatchRegions(regions)); | |
| // Sum all regions | |
| const scalarField alphaVol | |
| ( | |
| alpha.primitiveField()*mesh_.V().primitiveField() | |
| ); | |
| Map<scalar> allRegionVolume(regionSum(regions, mesh_.V().primitiveField())); | |
| Map<scalar> allRegionAlphaVolume(regionSum(regions, alphaVol)); | |
| Map<label> allRegionNumCells | |
| ( | |
| regionSum | |
| ( | |
| regions, | |
| labelField(mesh_.nCells(), 1.0) | |
| ) | |
| ); | |
| if (debug) | |
| { | |
| Info<< indent << "All regions:" << endl; | |
| Info<< indent << "" << tab << "Region" | |
| << tab << "Volume(mesh)" | |
| << tab << "Volume(" << alpha.name() << "):" | |
| << tab << "nCells" | |
| << endl; | |
| scalar meshSumVol = 0.0; | |
| scalar alphaSumVol = 0.0; | |
| label nCells = 0; | |
| Map<scalar>::const_iterator vIter = allRegionVolume.begin(); | |
| Map<scalar>::const_iterator aIter = allRegionAlphaVolume.begin(); | |
| Map<label>::const_iterator numIter = allRegionNumCells.begin(); | |
| for | |
| ( | |
| ; | |
| vIter != allRegionVolume.end() | |
| && aIter != allRegionAlphaVolume.end(); | |
| ++vIter, ++aIter, ++numIter | |
| ) | |
| { | |
| Info<< indent << "" << tab << vIter.key() | |
| << tab << vIter() | |
| << tab << aIter() | |
| << tab << numIter() | |
| << endl; | |
| meshSumVol += vIter(); | |
| alphaSumVol += aIter(); | |
| nCells += numIter(); | |
| } | |
| Info<< indent << "" << tab << "Total:" | |
| << tab << meshSumVol | |
| << tab << alphaSumVol | |
| << tab << nCells | |
| << endl; | |
| } | |
| { | |
| Info<< indent << "Patch connected regions (liquid core):" << endl; | |
| Info<< indent << "" << tab << "Region" | |
| << tab << "Volume(mesh)" | |
| << tab << "Volume(" << alpha.name() << "):" | |
| << endl; | |
| forAllConstIter(Map<label>, patchRegions, iter) | |
| { | |
| label regionI = iter.key(); | |
| Info<< indent << "" << tab << iter.key() | |
| << tab << allRegionVolume[regionI] | |
| << tab << allRegionAlphaVolume[regionI] << endl; | |
| } | |
| } | |
| { | |
| Info<< indent << "Background regions:" << endl; | |
| Info<< indent << "" << tab << "Region" | |
| << tab << "Volume(mesh)" | |
| << tab << "Volume(" << alpha.name() << "):" | |
| << endl; | |
| Map<scalar>::const_iterator vIter = allRegionVolume.begin(); | |
| Map<scalar>::const_iterator aIter = allRegionAlphaVolume.begin(); | |
| for | |
| ( | |
| ; | |
| vIter != allRegionVolume.end() | |
| && aIter != allRegionAlphaVolume.end(); | |
| ++vIter, ++aIter | |
| ) | |
| { | |
| if | |
| ( | |
| !patchRegions.found(vIter.key()) | |
| && vIter() >= maxDropletVol | |
| ) | |
| { | |
| Info<< indent << "" << tab << vIter.key() | |
| << tab << vIter() | |
| << tab << aIter() << endl; | |
| } | |
| } | |
| } | |
| // Split alpha field | |
| // ~~~~~~~~~~~~~~~~~ | |
| // Split into | |
| // - liquidCore : region connected to inlet patches | |
| // - per region a volume : for all other regions | |
| // - backgroundAlpha : remaining alpha | |
| writeAlphaFields(regions, patchRegions, allRegionVolume, alpha); | |
| // Extract droplet-only allRegionVolume, i.e. delete liquid core | |
| // (patchRegions) and background regions from maps. | |
| // Note that we have to use mesh volume (allRegionVolume) and not | |
| // allRegionAlphaVolume since background might not have alpha in it. | |
| forAllIter(Map<scalar>, allRegionVolume, vIter) | |
| { | |
| label regionI = vIter.key(); | |
| if | |
| ( | |
| patchRegions.found(regionI) | |
| || vIter() >= maxDropletVol | |
| ) | |
| { | |
| allRegionVolume.erase(vIter); | |
| allRegionAlphaVolume.erase(regionI); | |
| allRegionNumCells.erase(regionI); | |
| } | |
| } | |
| if (allRegionVolume.size()) | |
| { | |
| // Construct mids of bins for plotting | |
| scalarField xBin(nBins_); | |
| scalar x = 0.5*delta; | |
| forAll(xBin, i) | |
| { | |
| xBin[i] = x; | |
| x += delta; | |
| } | |
| // Get in region order the alpha*volume and diameter | |
| // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| const labelList sortedRegions = allRegionAlphaVolume.sortedToc(); | |
| scalarField sortedVols | |
| ( | |
| extractData | |
| ( | |
| sortedRegions, | |
| allRegionAlphaVolume | |
| ) | |
| ); | |
| // Calculate the diameters | |
| scalarField sortedDiameters(sortedVols.size()); | |
| forAll(sortedDiameters, i) | |
| { | |
| sortedDiameters[i] = Foam::cbrt | |
| ( | |
| sortedVols[i] | |
| *6/constant::mathematical::pi | |
| ); | |
| } | |
| // Determine the bin index for all the diameters | |
| labelList indices(sortedDiameters.size()); | |
| forAll(sortedDiameters, i) | |
| { | |
| indices[i] = (sortedDiameters[i]-minDiam_)/delta; | |
| } | |
| // Calculate the counts per diameter bin | |
| scalarField binCount(nBins_, 0.0); | |
| forAll(sortedDiameters, i) | |
| { | |
| binCount[indices[i]] += 1.0; | |
| } | |
| // Write to log | |
| { | |
| Info<< indent << "Bins:" << endl; | |
| Info<< indent << "" << tab << "Bin" | |
| << tab << "Min diameter" | |
| << tab << "Count:" | |
| << endl; | |
| scalar diam = 0.0; | |
| forAll(binCount, binI) | |
| { | |
| Info<< indent << "" << tab << binI | |
| << tab << diam | |
| << tab << binCount[binI] << endl; | |
| diam += delta; | |
| } | |
| Info<< endl; | |
| } | |
| // Declare fields and field names | |
| wordList fieldNames; | |
| FOR_ALL_FIELD_TYPES(DeclareTypeFields); | |
| // Add the bin count | |
| fieldNames.append("binCount"); | |
| // Add the volumes | |
| generateFields | |
| ( | |
| "volume", | |
| indices, | |
| sortedVols, | |
| binCount, | |
| fieldNames, | |
| scalarFields | |
| ); | |
| // Add other sampled fields | |
| forAll(fields_, fieldi) | |
| { | |
| bool found = false; | |
| FOR_ALL_FIELD_TYPES(GenerateTypeFields); | |
| if (!found) cannotFindObject(fields_[fieldi]); | |
| } | |
| // Expand all field lists | |
| FOR_ALL_FIELD_TYPES(TypeFieldsExpand) | |
| // Write | |
| formatterPtr_().write | |
| ( | |
| file_.baseTimeDir(), | |
| typeName, | |
| coordSet(true, "diameter", xBin), | |
| fieldNames | |
| FOR_ALL_FIELD_TYPES(TypeFieldsParameter) | |
| ); | |
| } | |
| return true; | |
| } | |
| // ************************************************************************* // | |