setAlphaField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2017 DHI
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10  Copyright (c) 2017-2020, German Aerospace Center (DLR)
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 Application
29  setAlphaField
30 
31 Description
32  Uses cutCellIso to create a volume fraction field from either a cylinder,
33  a sphere or a plane.
34 
35  Original code supplied by Johan Roenby, DHI (2016)
36  Modification Henning Scheufler, DLR (2019)
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #include "fvCFD.H"
41 
42 #include "triSurface.H"
43 #include "triSurfaceTools.H"
44 
45 #include "implicitFunction.H"
46 
47 #include "cutCellIso.H"
48 #include "OBJstream.H"
49 
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 void isoFacesToFile
54 (
55  const DynamicList<List<point>>& faces,
56  const word& fileName
57 )
58 {
59  // Writing isofaces to OBJ file for inspection in paraview
60  OBJstream os(fileName + ".obj");
61 
62  if (Pstream::parRun())
63  {
64  // Collect points from all the processors
65  List<DynamicList<List<point>>> allProcFaces(Pstream::nProcs());
66  allProcFaces[Pstream::myProcNo()] = faces;
67  Pstream::gatherList(allProcFaces);
68 
69  if (Pstream::master())
70  {
71  Info<< "Writing file: " << fileName << endl;
72 
73  for (const DynamicList<List<point>>& procFaces : allProcFaces)
74  {
75  for (const List<point>& facePts : procFaces)
76  {
77  os.write(face(identity(facePts.size())), facePts);
78  }
79  }
80  }
81  }
82  else
83  {
84  Info<< "Writing file: " << fileName << endl;
85 
86  for (const List<point>& facePts : faces)
87  {
88  os.write(face(identity(facePts.size())), facePts);
89  }
90  }
91 }
92 
93 
94 int main(int argc, char *argv[])
95 {
96  argList::addNote
97  (
98  "Uses cutCellIso to create a volume fraction field from an "
99  "implicit function."
100  );
101 
102  #include "addDictOption.H"
103  #include "addRegionOption.H"
104  #include "setRootCase.H"
105  #include "createTime.H"
106  #include "createNamedMesh.H"
107 
108  const word dictName("setAlphaFieldDict");
109  #include "setSystemMeshDictionaryIO.H"
110 
111  IOdictionary setAlphaFieldDict(dictIO);
112 
113  Info<< "Reading " << setAlphaFieldDict.name() << endl;
114 
115  const word fieldName = setAlphaFieldDict.get<word>("field");
116  const bool invert = setAlphaFieldDict.getOrDefault("invert", false);
117  const bool writeOBJ = setAlphaFieldDict.getOrDefault("writeOBJ", false);
118 
119  Info<< "Reading field " << fieldName << nl << endl;
121  (
122  IOobject
123  (
124  fieldName,
125  runTime.timeName(),
126  mesh,
127  IOobject::MUST_READ,
128  IOobject::AUTO_WRITE
129  ),
130  mesh
131  );
132 
133  autoPtr<implicitFunction> func = implicitFunction::New
134  (
135  setAlphaFieldDict.get<word>("type"),
136  setAlphaFieldDict
137  );
138 
139  scalarField f(mesh.nPoints(), Zero);
140 
141  forAll(f, pi)
142  {
143  f[pi] = func->value(mesh.points()[pi]);
144  };
145 
146  cutCellIso cutCell(mesh, f);
147 
148  DynamicList<List<point>> facePts;
149 
150  DynamicList<triSurface> surface;
151 
152  surfaceScalarField cellToCellDist(mag(mesh.delta()));
153 
154  forAll(alpha1, cellI)
155  {
156  label cellStatus = cutCell.calcSubCell(cellI, 0.0);
157 
158  if (cellStatus == -1)
159  {
160  alpha1[cellI] = 1;
161  }
162  else if (cellStatus == 1)
163  {
164  alpha1[cellI] = 0;
165  }
166  else if (cellStatus == 0)
167  {
168  if (mag(cutCell.faceArea()) != 0)
169  {
170  alpha1[cellI] = max(min(cutCell.VolumeOfFluid(), 1), 0);
171  if (writeOBJ && (mag(cutCell.faceArea()) >= 1e-14))
172  {
173  facePts.append(cutCell.facePoints());
174  }
175  }
176  }
177  }
178 
179  if (writeOBJ)
180  {
181  isoFacesToFile(facePts, fieldName + "0");
182  }
183 
184  ISstream::defaultPrecision(18);
185 
186  if (invert)
187  {
188  alpha1 = scalar(1) - alpha1;
189  }
190 
191  alpha1.correctBoundaryConditions();
192 
193  Info<< "Writing new alpha field " << alpha1.name() << endl;
194  alpha1.write();
195 
196  const scalarField& alpha = alpha1.internalField();
197 
198  Info<< "sum(alpha*V):" << gSum(mesh.V()*alpha)
199  << ", 1-max(alpha1): " << 1 - gMax(alpha)
200  << " min(alpha1): " << gMin(alpha) << endl;
201 
202  Info<< "\nEnd\n" << endl;
203 
204  return 0;
205 }
206 
207 
208 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::meshTools::writeOBJ
void writeOBJ(Ostream &os, const point &pt)
Write obj representation of a point.
Definition: meshTools.C:203
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:212
dictName
const word dictName("blockMeshDict")
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:594
alpha1
const volScalarField & alpha1
Definition: setRegionFluidFields.H:8
triSurface.H
setSystemMeshDictionaryIO.H
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
addDictOption.H
cutCellIso.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
addRegionOption.H
Foam::volScalarField
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:57
Foam::func
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
createNamedMesh.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
implicitFunction.H
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
setRootCase.H
Foam::constant::mathematical::pi
constexpr scalar pi(M_PI)
Foam::nl
constexpr char nl
Definition: Ostream.H:385
f
labelList f(nPoints)
Foam::surfaceScalarField
GeometricField< scalar, fvsPatchField, surfaceMesh > surfaceScalarField
Definition: surfaceFieldsFwd.H:54
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
createTime.H
fvCFD.H
Foam::gMin
Type gMin(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:593
triSurfaceTools.H
Foam::gMax
Type gMax(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:592
OBJstream.H