0%

simpleControl.H

代码意义解释

总述

主要的函数为loop()函数,检查当前时间步是否满足收敛条件,如满足,返回迭代终止,否则存储当前时间步中场的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool Foam::simpleControl::loop()
{
read(); //读取controlDict中的数据,如nNonOrthCorr_,momentumPredictor_,transonic_等

Time& time = const_cast<Time&>(mesh_.time());

if (initialised_)
{
if (criteriaSatisfied()) //满足收敛条件,局部残差,总体残差
{
Info<< nl << algorithmName_ << " solution converged in "
<< time.timeName() << " iterations" << nl << endl;

// Set to finalise calculation
time.writeAndEnd(); //Write the objects now (not at end of iteration) and end the run
}
else
{
storePrevIterFields(); //把当前迭代步的场存储
}
}
else
{
initialised_ = true;
storePrevIterFields();
}


return time.loop();
}

数据结构

函数名 表达式 意义
protected 初始化标志
bool initialised_;
read void read() 读取controls
criteriaSatisfied bool criteriaSatisfied(); 如果所有的收敛检查都满足,返回true
simpleControl simpleControl(const simpleControl&);
= void operator=(const simpleControl&);
public
simpleControl simpleControl(fvMesh& mesh);
~simpleControl
loop virtual bool loop(); 求解控制

源代码+补充代码

public solutionControl.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 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/>.

Class
Foam::solutionControl

Description
Base class for solution control classes

\*---------------------------------------------------------------------------*/

#ifndef solutionControl_H
#define solutionControl_H

#include "fvMesh.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
Class solutionControl Declaration
\*---------------------------------------------------------------------------*/

class solutionControl
:
public IOobject
{
public:

struct fieldData
{
wordRe name;
scalar absTol;
scalar relTol;
scalar initialResidual;
};


protected:

// Protected data

//- Reference to the mesh database
fvMesh& mesh_;

//- List of residual data per field
List<fieldData> residualControl_;

//- The dictionary name, e.g. SIMPLE, PIMPLE
const word algorithmName_;


// Solution controls

//- Maximum number of non-orthogonal correctors
label nNonOrthCorr_;

//- Flag to indicate to solve for momentum
bool momentumPredictor_;

//- Flag to indicate to solve using transonic algorithm
bool transonic_;


// Evolution

//- Current corrector loop index
label corr_;

//- Current non-orthogonal corrector loop index
label corrNonOrtho_;


// Protected Member Functions

//- Read controls from fvSolution dictionary
virtual void read(const bool absTolOnly);

//- Return index of field in residualControl_ if present
virtual label applyToField
(
const word& fieldName,
const bool useRegEx = true
) const;

//- Return true if all convergence checks are satisfied
virtual bool criteriaSatisfied() = 0;

//- Store previous iteration fields
virtual void storePrevIterFields() const;

//- Store previous iteration field for vol<Type>Fields
template<class Type>
void storePrevIter() const;

//- Disallow default bitwise copy construct
solutionControl(const solutionControl&);

//- Disallow default bitwise assignment
void operator=(const solutionControl&);


public:


// Static Data Members

//- Run-time type information
TypeName("solutionControl");


// Constructors

//- Construct from mesh
solutionControl(fvMesh& mesh, const word& algorithmName);


//- Destructor
virtual ~solutionControl();


// Member Functions

// Access

//- Return the solution dictionary
inline const dictionary& dict() const;

//- Current corrector loop index
inline label corr() const;

//- Current non-orthogonal corrector index
inline label corrNonOrtho() const;


// Solution control

//- Maximum number of non-orthogonal correctors
inline label nNonOrthCorr() const;

//- Helper function to identify final non-orthogonal iteration
inline bool finalNonOrthogonalIter() const;

//- Flag to indicate to solve for momentum
inline bool momentumPredictor() const;

//- Flag to indicate to solve using transonic algorithm
inline bool transonic() const;


// Evolution

//- Main control loop
virtual bool loop() = 0;

//- Non-orthogonal corrector loop
inline bool correctNonOrthogonal();
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#ifdef NoRepository
#include "solutionControlTemplates.C"
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#include "solutionControlI.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //

simpleControl.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef simpleControl_H
#define simpleControl_H

#include "solutionControl.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
Class simpleControl Declaration
\*---------------------------------------------------------------------------*/

class simpleControl
:
public solutionControl
{

protected:

// Protected Data

//- Initialised flag
bool initialised_;


// Protected Member Functions

//- Read controls from fvSolution dictionary
void read();

//- Return true if all convergence checks are satisfied
bool criteriaSatisfied();

//- Disallow default bitwise copy construct
simpleControl(const simpleControl&);

//- Disallow default bitwise assignment
void operator=(const simpleControl&);


public:


// Static Data Members

//- Run-time type information
TypeName("simpleControl");


// Constructors

//- Construct from mesh
simpleControl(fvMesh& mesh);


//- Destructor
virtual ~simpleControl();


// Member Functions

// Solution control

//- Loop loop
virtual bool loop();
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

simpleControl.C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 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/>.

\*---------------------------------------------------------------------------*/

#include "simpleControl.H"
#include "Time.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
defineTypeNameAndDebug(simpleControl, 0);
}


// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //

void Foam::simpleControl::read()
{
solutionControl::read(true);
}


bool Foam::simpleControl::criteriaSatisfied()
{
if (residualControl_.empty())
{
return false;
}

bool achieved = true;
bool checked = false; // safety that some checks were indeed performed

const dictionary& solverDict = mesh_.solverPerformanceDict();
forAllConstIter(dictionary, solverDict, iter)
{
const word& variableName = iter().keyword();
const label fieldI = applyToField(variableName);
if (fieldI != -1)
{
const List<solverPerformance> sp(iter().stream());
const scalar residual = sp.first().initialResidual();

checked = true;

bool absCheck = residual < residualControl_[fieldI].absTol;
achieved = achieved && absCheck;

if (debug)
{
Info<< algorithmName_ << " solution statistics:" << endl;

Info<< " " << variableName << ": tolerance = " << residual
<< " (" << residualControl_[fieldI].absTol << ")"
<< endl;
}
}
}

return checked && achieved;
}


// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

Foam::simpleControl::simpleControl(fvMesh& mesh)
:
solutionControl(mesh, "SIMPLE"),
initialised_(false)
{
read();

Info<< nl;

if (residualControl_.empty())
{
Info<< algorithmName_ << ": no convergence criteria found. "
<< "Calculations will run for " << mesh_.time().endTime().value()
<< " steps." << nl << endl;
}
else
{
Info<< algorithmName_ << ": convergence criteria" << nl;
forAll(residualControl_, i)
{
Info<< " field " << residualControl_[i].name << token::TAB
<< " tolerance " << residualControl_[i].absTol
<< nl;
}
Info<< endl;
}
}


// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //

Foam::simpleControl::~simpleControl()
{}


// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //

bool Foam::simpleControl::loop()
{
read();

Time& time = const_cast<Time&>(mesh_.time());

if (initialised_)
{
if (criteriaSatisfied())
{
Info<< nl << algorithmName_ << " solution converged in "
<< time.timeName() << " iterations" << nl << endl;

// Set to finalise calculation
time.writeAndEnd();
}
else
{
storePrevIterFields();
}
}
else
{
initialised_ = true;
storePrevIterFields();
}


return time.loop();
}


// ************************************************************************* //