OpenFoam列表的创建与循环器
提示:这里的程序仅仅是对$FOAM_APP/test/circulator文件的注释与添加
前言
提示:在OpenFOAM中最常用的数据类型之一便是列表,因对OpenFOAM的熟悉程度较,在此仅仅说一下撸代码撸到的一个类型
- face 是一个继承于labelList的类,在OpenFOAM中,将labelList类看做是存放整数列表的类。
- 在OpenFOAM中对列表的创建、旋转、修改内部值以及列表的比较。
- 对列表中的数据处理,除去for循环之外的,另一种circulator的类。优点是,可以在循环的时候可以看到当前元素的前一个和后一个元素,提前做出判断。
`
一、程序注释
代码如下:
#include "List.H"
#include "ListOps.H"
#include "face.H"
#include "Circulator.H"
#include "ConstCirculator.H"
using namespace Foam;
int main(int argc, char *argv[])
{
Info<< "Test the implementation of a circular iterator(迭代循环器)" << nl << endl;
Info<< "Test const circulator. First go forwards, then backwards."
<< nl << endl;
Info<<"identity(4)创建的数据类型:"<<typeid(identity(4)).name()<<
" "<< identity(4)<<endl;
Info<<"identity(8)创建的数据类型:"<<typeid(identity(8)).name()<<
" "<< identity(8)<<endl;
face f(identity(4));
Info <<f<<endl;
ConstCirculator<face> cStart(f);
Info<<cStart.size()<<endl;
if (cStart.size()) do
{
Info<< "Iterate forwards over face (prev/curr/next) : "
<< cStart.prev() << " / " << cStart() << " / " << cStart.next()
<< endl;
} while (cStart.circulate(CirculatorBase::direction::clockwise));
if (cStart.size()) do
{
Info<< "Iterate backwards over face : " << cStart() << endl;
} while (cStart.circulate(CirculatorBase::direction::anticlockwise));
Info<< nl << nl << "Test non-const(可以改变参量的) circulator" << nl << endl;
Circulator<face> cStart2(f);
Info<< "输出先前的标签列表对象f中的量个值: " << f[0]<<" "<<f[2] << endl;
if (cStart2.size()) do
{
Info<< "Iterate forwards over face (prev/curr/next) : "
<< cStart2.prev() << " / " << cStart2() << " / " << cStart2.next()
<< endl;
} while (cStart2.circulate(CirculatorBase::direction::clockwise));
if (cStart2.size()) do
{
Info<< "Iterate forwards over face, adding 1 to each element : "
<< cStart2();
cStart2() += 1;
Info<< " -> " << cStart2() << endl;
} while (cStart2.circulate(CirculatorBase::direction::clockwise));
Info<< "Face after : " << f << endl;
Info<< nl << nl << "Compare two faces: " << endl;
face a(identity(5));
Info<< "Compare " << a << " and " << a << " Match = " << face::compare(a, a)
<< endl;
face b(reverseList(a));
Info<< "Compare " << a << " and " << b << " Match = " << face::compare(a, b)
<< endl;
face c(a);
c[4] = 3;
Info<< "Compare " << a << " and " << c << " Match = " << face::compare(a, c)
<< endl;
face d(rotateList(a, 2));
Info<< "Compare " << a << " and " << d << " Match = " << face::compare(a, d)
<< endl;
face g(labelList(5, 1));
face h(g);
Info<< "Compare " << g[2] << " and " << h << " Match = " << face::compare(g, h)
<< endl;
g[0] = 2;
h[3] = 2;
Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
<< endl;
g[4] = 3;
h[4] = 3;
Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
<< endl;
face face1(identity(1));
Info<< "Compare " << face1 << " and " << face1
<< " Match = " << face::compare(face1, face1) << endl;
face face2(identity(1)+1);
Info<< "Compare " << face1 << " and " << face2
<< " Match = " << face::compare(face1, face2) << endl;
Info<< nl << nl << "Zero face" << nl << endl;
face fZero;
Info<<"fZero创建了一个:"<<fZero<<endl;
Circulator<face> cZero(fZero);
if (cZero.size()) do
{
Info<<"进去了"<<endl;
Info<< "Iterate forwards over face : " << cZero() << endl;
Info<<"结束了"<<endl;
} while (cZero.circulate(CirculatorBase::direction::clockwise));
fZero = face(identity(5));
cZero = Circulator<face>(fZero);
do
{
Info<< "Iterate forwards over face : " << cZero() << endl;
} while (cZero.circulate(CirculatorBase::direction::clockwise));
Info<< nl << nl << "Simultaneously go forwards/backwards over face " << f
<< nl << endl;
ConstCirculator<face> circForward(f);
ConstCirculator<face> circBackward(f);
Info<<f<<endl;
if (circForward.size() && circBackward.size()) do
{
Info<< "Iterate over face forwards : " << circForward()
<< ", backwards : " << circBackward() << endl;
}
while
(
circForward.circulate(CirculatorBase::direction::clockwise),
circBackward.circulate(CirculatorBase::direction::anticlockwise)
);
Info<< "\nEnd\n" << endl;
return 0;
}
二、结果
代码如下(示例):
tsing@tsing:~/OpenFOAM/OpenFOAM-9/applications/test/Circulator$ Test-Circulator
Test the implementation of a circular iterator(迭代循环器)
Test const circulator. First go forwards, then backwards.
identity(4)创建的数据类型:N4Foam4ListIiEE 4(0 1 2 3)
identity(8)创建的数据类型:N4Foam4ListIiEE 8(0 1 2 3 4 5 6 7)
4(0 1 2 3)
4
Iterate forwards over face (prev/curr/next) : 3 / 0 / 1
Iterate forwards over face (prev/curr/next) : 0 / 1 / 2
Iterate forwards over face (prev/curr/next) : 1 / 2 / 3
Iterate forwards over face (prev/curr/next) : 2 / 3 / 0
Iterate backwards over face : 0
Iterate backwards over face : 3
Iterate backwards over face : 2
Iterate backwards over face : 1
Test non-const(可以改变参量的) circulator
输出先前的标签列表对象f中的量个值: 0 2
Iterate forwards over face (prev/curr/next) : 3 / 0 / 1
Iterate forwards over face (prev/curr/next) : 0 / 1 / 2
Iterate forwards over face (prev/curr/next) : 1 / 2 / 3
Iterate forwards over face (prev/curr/next) : 2 / 3 / 0
Iterate forwards over face, adding 1 to each element : 0 -> 1
Iterate forwards over face, adding 1 to each element : 1 -> 2
Iterate forwards over face, adding 1 to each element : 2 -> 3
Iterate forwards over face, adding 1 to each element : 3 -> 4
Face after : 4(1 2 3 4)
Compare two faces:
Compare 5(0 1 2 3 4) and 5(0 1 2 3 4) Match = 1
Compare 5(0 1 2 3 4) and 5(4 3 2 1 0) Match = -1
Compare 5(0 1 2 3 4) and 5(0 1 2 3 3) Match = 0
Compare 5(0 1 2 3 4) and 5(3 4 0 1 2) Match = 1
Compare 1 and 5{1} Match = 1
Compare 5(2 1 1 1 1) and 5(1 1 1 2 1) Match = 1
Compare 5(2 1 1 1 3) and 5(1 1 1 2 3) Match = -1
Compare 1(0) and 1(0) Match = 1
Compare 1(0) and 1(1) Match = 0
Zero face
fZero创建了一个:0()
Iterate forwards over face : 0
Iterate forwards over face : 1
Iterate forwards over face : 2
Iterate forwards over face : 3
Iterate forwards over face : 4
Simultaneously go forwards/backwards over face 4(1 2 3 4)
4(1 2 3 4)
Iterate over face forwards : 1, backwards : 1
Iterate over face forwards : 2, backwards : 4
Iterate over face forwards : 3, backwards : 3
Iterate over face forwards : 4, backwards : 2
End
三、官网对circulator类的解释
代码如下
Class
Foam::ConstCirculator
Description
Walks over a container as if it were circular. The container must have the
following members defined:
- value_type
- size_type
- difference_type
- const_iterator
- const_reference
Examples:
\code
face f(identity(5));
ConstCirculator<face> circ(f);
if (circ.size()) do
{
Info<< "Iterate forwards over face : " << circ() << endl;
} while (circ.circulate(CirculatorBase::direction::clockwise));
\endcode
\code
face f(identity(5));
ConstCirculator<face> circClockwise(f);
ConstCirculator<face> circAnticlockwise(f);
if (circClockwise.size() && circAnticlockwise.size()) do
{
Info<< "Iterate forward over face :" << circClockwise() << endl;
Info<< "Iterate backward over face:" << circAnticlockwise() << endl;
}
while
(
circClockwise.circulate(CirculatorBase::direction::clockwise),
circAnticlockwise.circulate
(
CirculatorBase::direction::anticlockwise
)
);
\endcode
|