Skip to content

Commit 1169500

Browse files
committed
Fixed tutorial 9
1 parent 27fb98e commit 1169500

File tree

7 files changed

+98
-224
lines changed

7 files changed

+98
-224
lines changed

OFtutorial9_runtimePostprocessingUtility/IOpipeCalc.H

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pipeCalc.C
2-
pipeCalcFunctionObject.C
32

43
LIB = $(FOAM_USER_LIBBIN)/libpipeCalc

OFtutorial9_runtimePostprocessingUtility/pipeCalc.C

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,78 @@ License
2424
\*---------------------------------------------------------------------------*/
2525

2626
#include "pipeCalc.H"
27-
27+
#include "addToRunTimeSelectionTable.H"
2828
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
2929

3030
namespace Foam
3131
{
32-
defineTypeNameAndDebug(pipeCalc, 0);
32+
namespace functionObjects
33+
{
34+
defineTypeNameAndDebug(pipeCalc, 0);
35+
addToRunTimeSelectionTable(functionObject, pipeCalc, dictionary);
36+
}
3337
}
3438

3539
// * * * * * * * * * * * * * * * * Protected members * * * * * * * * * * * * * * //
3640

37-
// NOTE: this method first gets declared in functionObjectFile.H, from which this
38-
// class is derived. This method gets called automatically when the base object
39-
// function gets called too.
41+
// NOTE: this returns a list of file names which match indices of the enum
42+
// defined in the header of this class. These names are used to create matching
43+
// files by the logFiles object.
44+
Foam::wordList Foam::functionObjects::pipeCalc::createFileNames
45+
(
46+
const dictionary& dict
47+
) const
48+
{
49+
DynamicList<word> names(1);
50+
51+
// use type of the utility as specified in the dict as the top-level dir name
52+
const word objectType(dict.lookup("type"));
53+
54+
// Name for file(MAIN_FILE=0)
55+
names.append(objectType);
56+
57+
return names;
58+
}
59+
60+
// NOTE: this method first gets declared in logFiles.H, from which this
61+
// class is derived. This method gets called automatically when the base object's
62+
// write() function gets called too.
4063
// The purpose of the function is to add the header to the output data file.
41-
void Foam::pipeCalc::writeFileHeader(const label i)
64+
void Foam::functionObjects::pipeCalc::writeFileHeader(const label i)
4265
{
43-
writeHeader(file(), "Flow rate through face zone");
44-
writeHeaderValue(file(), "Face zone name", faceZoneName_);
45-
writeCommented(file(), "Time [s] | Flow rate [m3s-1]");
46-
file() << endl;
66+
// Find the correct file to write to from the enum defined in the header.
67+
switch (fileID(i))
68+
{
69+
case MAIN_FILE:
70+
{
71+
writeHeader(file(i), "Flow rate through face zone");
72+
writeHeaderValue(file(i), "Face zone name", faceZoneName_);
73+
writeCommented(file(i), "Time [s] | Flow rate [m3s-1]");
74+
file() << endl;
75+
break; // exit the case structure
76+
}
77+
default:
78+
{
79+
FatalErrorInFunction
80+
<< "Unhandled file index: " << i
81+
<< abort(FatalError);
82+
}
83+
}
4784
}
4885

4986
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50-
Foam::pipeCalc::pipeCalc
87+
Foam::functionObjects::pipeCalc::pipeCalc
5188
(
5289
const word& name,
53-
const objectRegistry& obr,
54-
const dictionary& dict,
55-
const bool loadFromFiles
90+
const Time& runTime,
91+
const dictionary& dict
5692
)
5793
:
5894
// NOTE: call the base class constructor
59-
functionObjectFile(obr, name, type()),
95+
fvMeshFunctionObject(name, runTime, dict),
96+
logFiles(obr_, name),
97+
6098
name_(name),
61-
obr_(obr),
62-
// NOTE: the instance only gets given a reference to the object registry
63-
// automaitcally (this keeps track of what objects, such as fields or
64-
// meshes) are registered in the simulation. The following line casts
65-
// (converts) the reference to a const fvMesh reference, which allows
66-
// the grid to be accessed but not modified.
67-
mesh_(refCast<const fvMesh>(obr_)),
6899
active_(true),
69100
UName_("U"),
70101
// NOTE: Read the face zone to integrate over. Get its name from the dict, find
@@ -73,27 +104,13 @@ Foam::pipeCalc::pipeCalc
73104
faceZoneLabel_( mesh_.faceZones().findZoneID(faceZoneName_) ),
74105
faces_( mesh_.faceZones()[faceZoneLabel_] )
75106
{
76-
// Check if the available mesh is an fvMesh, otherwise deactivate
77-
if (!isA<fvMesh>(obr_))
78-
{
79-
active_ = false;
80-
WarningIn
81-
(
82-
"pipeCalc::pipeCalc"
83-
"("
84-
"const word&, "
85-
"const objectRegistry&, "
86-
"const dictionary&, "
87-
"const bool"
88-
")"
89-
) << "No fvMesh available, deactivating " << name_ << nl
90-
<< endl;
91-
}
92-
93107
// NOTE: calls the separate .read() method to import the controls from the dict.
94108
// dict reference is passed automatically by the OpenFOAM runtime object manager.
95109
read(dict);
96110

111+
// built-in logFiles method for creating file streams.
112+
resetNames(createFileNames(dict));
113+
97114
if (active_)
98115
{
99116
// Any extra initialisation goes here, if necessary
@@ -106,40 +123,43 @@ Foam::pipeCalc::pipeCalc
106123

107124
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
108125

109-
Foam::pipeCalc::~pipeCalc()
126+
Foam::functionObjects::pipeCalc::~pipeCalc()
110127
{}
111128

112129
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113130

114-
void Foam::pipeCalc::read(const dictionary& dict)
131+
bool Foam::functionObjects::pipeCalc::read(const dictionary& dict)
115132
{
116133
if (active_)
117134
{
118135
UName_ = dict.lookupOrDefault<word>("UName", "U");
119136
}
137+
return true;
120138
}
121139

122-
void Foam::pipeCalc::execute()
140+
bool Foam::functionObjects::pipeCalc::execute()
123141
{
124142
if (active_)
125143
{
126144
// This gets called before write, should put things on which other
127145
// function objects might depend on here (for instance field calculations)
128146
}
147+
return true;
129148
}
130149

131-
void Foam::pipeCalc::end()
150+
bool Foam::functionObjects::pipeCalc::end()
132151
{
133152
if (active_)
134153
{
135154
execute();
136155
}
156+
return true;
137157
}
138158

139-
void Foam::pipeCalc::timeSet()
159+
void Foam::functionObjects::pipeCalc::timeSet()
140160
{}
141161

142-
void Foam::pipeCalc::write()
162+
bool Foam::functionObjects::pipeCalc::write()
143163
{
144164
if (active_)
145165
{
@@ -180,12 +200,13 @@ void Foam::pipeCalc::write()
180200
// Call the base class method which checks if the output file exists
181201
// and creates it, if necessary. That also calls the .writeFileHeader()
182202
// method of the derived class.
183-
functionObjectFile::write();
203+
logFiles::write();
184204

185205
// Add the entry for this time step that has just been computed.
186-
file() << obr_.time().value() << tab << flowRate << endl;
206+
file(MAIN_FILE) << obr_.time().value() << tab << flowRate << endl;
187207
}
188208
}
209+
return true;
189210
}
190211

191212
// ************************************************************************* //

OFtutorial9_runtimePostprocessingUtility/pipeCalc.H

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ License
2424
Class
2525
Foam::pipeCalc
2626
27-
Group
28-
grpUtilitiesFunctionObjects
29-
3027
Description
3128
This function object calculates and outputs various quantities relevant
3229
to flows through pipes and similar geometries.
3330
3431
SourceFiles
3532
pipeCalc.C
36-
IOpipeCalc.H
3733
3834
\*---------------------------------------------------------------------------*/
3935

@@ -47,42 +43,35 @@ SourceFiles
4743
#include "volFields.H"
4844
#include "dictionary.H"
4945
#include "fvc.H"
50-
#include "functionObjectFile.H"
5146
#include "writer.H"
47+
#include "fvMeshFunctionObject.H"
48+
#include "logFiles.H"
49+
#include "addToRunTimeSelectionTable.H"
5250

5351
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
5452

5553
namespace Foam
5654
{
57-
58-
// Forward declaration of classes
59-
class objectRegistry;
60-
class dictionary;
61-
class polyMesh;
62-
class mapPolyMesh;
55+
namespace functionObjects
56+
{
6357

6458
/*---------------------------------------------------------------------------*\
6559
Class pipeCalc Declaration
6660
\*---------------------------------------------------------------------------*/
6761

6862
class pipeCalc
6963
:
70-
// NOTE: derive from the base functionObjectFile in order to provide a ready
71-
// interface for dealing with output files
72-
public functionObjectFile
64+
// NOTE: derive from the base fvMeshFunctionObject and logFiles in order to
65+
// provide a ready interface for dealing with output files
66+
public fvMeshFunctionObject,
67+
public logFiles
7368
{
7469
private:
7570
// Private data
7671

7772
//- Name of this set of pipeCalc objects
7873
word name_;
7974

80-
//- Reference to the database
81-
const objectRegistry& obr_;
82-
83-
// reference to the mesh
84-
const fvMesh& mesh_;
85-
8675
//- On/off switch
8776
bool active_;
8877

@@ -108,6 +97,16 @@ private:
10897
void operator=(const pipeCalc&);
10998

11099
protected:
100+
// NOTE: define a list of files this object writes to; the underlying logFiles
101+
// object will handle output to correct streams automatically.
102+
enum fileID
103+
{
104+
MAIN_FILE = 0
105+
};
106+
107+
// NOTE: Create file names for each of the output files
108+
wordList createFileNames(const dictionary& dict) const;
109+
111110
// NOTE: this first gets declared in the baseline object and gets used to
112111
// write the header in the output file.
113112
virtual void writeFileHeader(const label i);
@@ -119,14 +118,12 @@ public:
119118

120119
// Constructors
121120

122-
//- Construct for given objectRegistry and dictionary.
123-
// Allow the possibility to load fields from files
121+
//- Construct for given Time and dictionary.
124122
pipeCalc
125123
(
126124
const word& name,
127-
const objectRegistry&,
128-
const dictionary&,
129-
const bool loadFromFiles = false
125+
const Time& runTime,
126+
const dictionary& dict
130127
);
131128

132129
//- Destructor
@@ -138,19 +135,19 @@ public:
138135
virtual const word& name() const { return name_; }
139136

140137
//- Read the pipeCalc data
141-
virtual void read(const dictionary&);
138+
virtual bool read(const dictionary&);
142139

143140
//- Execute, currently does nothing
144-
virtual void execute();
141+
virtual bool execute();
145142

146143
//- Execute at the final time-loop, currently does nothing
147-
virtual void end();
144+
virtual bool end();
148145

149146
//- Called when time was set at the end of the Time::operator++
150147
virtual void timeSet();
151148

152-
//- Calculate the pipeCalc and write
153-
virtual void write();
149+
// NOTE: Key method which implements the object's actual functionality
150+
virtual bool write();
154151

155152
//- Update for changes of mesh
156153
virtual void updateMesh(const mapPolyMesh&) {}
@@ -161,6 +158,7 @@ public:
161158

162159
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
163160

161+
} // End namespace functionObjectFile
164162
} // End namespace Foam
165163

166164
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

0 commit comments

Comments
 (0)