power.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2024 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 "power.H"
27 #include "momentumTransportModel.H"
28 #include "surfaceInterpolate.H"
29 #include "fvcDiv.H"
30 #include "fvcGrad.H"
31 #include "fvcVolumeIntegrate.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
41 
43  (
45  power,
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * Private Static Data * * * * * * * * * * * * * * //
53 
54 const Foam::wordList Foam::functionObjects::power::fields_
55 {
56  "stressUSf", "divStressU", "divStressDotU", "stressDDotGradU",
57  "pUSf", "divPU", "gradPU", "pDivU",
58  "tauUSf", "divTauU", "divTauDotU", "tauDDotGradU"
59 };
60 
61 
62 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
63 
64 void Foam::functionObjects::power::writeFileHeader(const label i)
65 {
66  file().setf(ios_base::fixed, ios_base::floatfield);
67 
69  (
70  file(),
71  "Domain-integrated power [x " + Foam::name(1.0/factor_) + "]"
72  );
73  writeCommented(file(), "Time");
74 
75  forAll(fields_, fieldi)
76  {
77  // Do not write surface fields (0, 4, 8)
78  if ((fieldi % 4) != 0)
79  {
80  writeTabbed(file(), fields_[fieldi]);
81  }
82  }
83 
84  file() << endl;
85 }
86 
87 
88 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
89 
91 (
92  const word& name,
93  const Time& runTime,
94  const dictionary& dict
95 )
96 :
97  fvMeshFunctionObject(name, runTime, dict),
98  logFiles(obr_, name),
99  writeLocalObjects(obr_, log),
100  factor_(1e9)
101 {
102  read(dict);
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
107 
109 {}
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
118 
119  dict.readIfPresent("factor", factor_);
120 
121  resetName(typeName);
122  resetLocalObjectNames(fields_);
123 
124  return true;
125 }
126 
127 
129 {
130  const momentumTransportModel& transport =
131  mesh_.lookupType<momentumTransportModel>();
132 
133  const volScalarField& p = mesh_.lookupObject<volScalarField>("p");
134  const volVectorField& U = transport.U();
136 
137  // Deviatoric stress tensor (the spherical part is added to the pressure)
138  const surfaceVectorField tauf(-transport.devTau()*mesh().magSf());
139 
140  // Pressure stress
141  const surfaceVectorField pIf(fvc::interpolate(p)*mesh().Sf());
142 
143  // Full deviatoric stress tensor = tau - pI
144  const surfaceVectorField stressf(tauf - pIf);
145 
146  store
147  (
148  fields_[3],
149  store(fields_[1], fvc::div(store(fields_[0], stressf & Uf)))
150  - store(fields_[2], fvc::div(stressf) & U)
151  );
152 
153  store
154  (
155  fields_[7],
156  store(fields_[5], fvc::div(store(fields_[4], -pIf & Uf)))
157  - store(fields_[6], -(fvc::grad(p) & U))
158  );
159 
160  store
161  (
162  fields_[11],
163  store(fields_[9], fvc::div(store(fields_[8], tauf & Uf)))
164  - store(fields_[10], fvc::div(tauf) & U)
165  );
166 
167  return true;
168 }
169 
170 
172 {
173  Log << type() << " " << name() << " write:" << nl;
174 
176 
177  logFiles::write();
178 
179  if (Pstream::master())
180  {
181  writeTime(file());
182  }
183 
184  forAll(fields_, fieldi)
185  {
186  if (mesh_.foundObject<volScalarField>(fields_[fieldi]))
187  {
188  const volScalarField& power =
189  mesh_.lookupObject<volScalarField>(fields_[fieldi]);
190 
191  const scalar integratedPower =
192  factor_*fvc::domainIntegrate(power).value();
193 
194  if (Pstream::master())
195  {
196  Log << " " << fields_[fieldi] << ": "
197  << integratedPower << nl;
198 
199  file()<< tab << integratedPower;
200  }
201  }
202  }
203 
204  if (Pstream::master())
205  {
206  Log << endl;
207  file() << endl;
208  }
209 
210  return true;
211 }
212 
213 
214 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
Macros for easy insertion into run-time selection tables.
Generic GeometricField class.
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:508
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
functionObject base class for creating, maintaining and writing log files e.g. integrated of averaged...
Definition: logFiles.H:60
OFstream & file()
Return access to the file (if only 1)
Definition: logFiles.C:113
virtual bool write()
Write function.
Definition: logFiles.C:173
Calculates the natural logarithm of the specified scalar field.
Definition: log.H:106
const ObjectType & lookupObject(const word &fieldName) const
Lookup object from the objectRegistry.
virtual bool read(const dictionary &)
Read optional controls.
Evaluates and writes the following power fields.
Definition: power.H:113
power(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: power.C:91
virtual ~power()
Destructor.
Definition: power.C:108
virtual bool execute()
Calculate the power field.
Definition: power.C:128
virtual bool write()
Write the power field.
Definition: power.C:171
virtual bool read(const dictionary &)
Read the power data.
Definition: power.C:114
void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:121
void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:131
void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:110
FunctionObject base class for managing a list of objects on behalf of the inheriting function object,...
virtual bool read(const dictionary &)
Read the list of objects to be written.
virtual bool write()
Write function.
Abstract base class for turbulence models (RAS, LES and laminar).
virtual tmp< surfaceVectorField > devTau() const =0
Return the effective surface stress.
const volVectorField & U() const
Access function to velocity field.
A class for handling words, derived from string.
Definition: word.H:62
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
Calculate the divergence of the given field.
Calculate the gradient of the given field.
Volume integrate volField creating a volField.
U
Definition: pEqn.H:72
#define Log
Report write to Foam::Info if the local log switch is true.
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
static tmp< SurfaceField< Type > > interpolate(const VolField< Type > &tvf, const surfaceScalarField &faceFlux, Istream &schemeData)
Interpolate field onto faces using scheme given by Istream.
dimensioned< Type > domainIntegrate(const VolField< Type > &vf)
tmp< VolField< typename outerProduct< vector, Type >::type > > grad(const SurfaceField< Type > &ssf)
Definition: fvcGrad.C:46
tmp< VolField< Type > > div(const SurfaceField< Type > &ssf)
Definition: fvcDiv.C:47
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
static const char tab
Definition: Ostream.H:266
IOstream & fixed(IOstream &io)
Definition: IOstream.H:588
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:267
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict
volScalarField & p