OpenFOAM小练习
提示:这里仅仅对OpenFOAM中自带的test/BinSum文件进行了注释和改动
前言
提示:小练习熟悉了一些函数,但也有不清楚的地方
熟悉了Random的类以及以及scalarField等基本类的使用。
一、Test-BinSum.C
对于此程序中的add()函数并不清楚其作用,代码在附录中给出如果有知道的麻烦告知一下。
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2012-2018 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/>.
Application
Test-BinSum
Description
Test BinSum container
\*---------------------------------------------------------------------------*/
#include "List.H"
#include "BinSum.H"
#include "IOstreams.H"
#include "Random.H"
#include "scalarField.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
/*Random是一个类,生成随机函数的,在Random.H<-RandomI.H中*/
Random rnd(0);//构造函数交付初始值
Info<<"rnd.scalar01:"<<rnd.scalar01()<<endl;//成员函数scalar01()生成一个0到1之间大小的scalar类型的数
Info<<"rnd.scalarAB:"<<rnd.scalarAB(3,10)<<endl;//使用成员函数scalarAB()生成一个a-b之间大小的scalar类型的数
/*scalarField也是一个类,用于创建标量场(浮点数类型)*/
scalarField samples(10);//创建名为samples的标量场,也属于构造函数赋值
forAll(samples, i)
{
samples[i] = rnd.scalar01(); //对标量场赋值
Info<<"samples"<<i<<"="<<samples[i]<<nl;
}
/*创建三个scalar(float)类型的值*/
const scalar min = 0;
const scalar max = 1;
const scalar delta = 0.1;
/*BinSum也是一个类,其父类是list类
//- Construct with given size initialising all elements to zero
List(const label, const zero);*/
BinSum<scalar, scalarField> count(min, max, delta);
BinSum<scalar, scalarField> sum(min, max, delta);
Info<<count<<endl;
Info<<sum<<endl;
/*BinSum中的两种add()函数没有看懂内核程序是干啥的,内核程序里面运用了运算符重载等*/
forAll(samples, i)
{
count.add(samples[i], 3);
sum.add(samples[i], samples[i]);
}
Info<< "sum : " << sum << endl;
Info<< "count : " << count << endl;
Info<< "average: " << sum/count << endl;
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //
运行结果
tsing@tsing:~/OpenFOAM/OpenFOAM-9/applications/test/BinSum$ Test-BinSum
rnd.scalar01:0.170828
rnd.scalarAB:8.24931
samples0=0.0963717
samples1=0.870465
samples2=0.577304
samples3=0.785799
samples4=0.692194
samples5=0.368766
samples6=0.873904
samples7=0.745095
samples8=0.446046
samples9=0.353728
10{0}
10{0}
sum : 10(0.0963717 0 0 0.722494 0.446046 0.577304 0.692194 1.53089 1.74437 0)
count : 10(3 0 0 6 3 3 3 6 6 0)
average: 10(0.0321239 -nan -nan 0.120416 0.148682 0.192435 0.230731 0.255149 0.290728 -nan)
End
二、附录
1.BinSum.H
代码如下(示例):
\*---------------------------------------------------------------------------*/
#ifndef BinSum_H
#define BinSum_H
#include "ops.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class BinSum Declaration
\*---------------------------------------------------------------------------*/
template
<
class IndexType,
class List,
class CombineOp = plusEqOp<typename List::value_type>
>
class BinSum
:
public List
{
// Private Data
const IndexType min_;
const IndexType max_;
const IndexType delta_;
//- Sum < lowest bin
typename List::value_type lowSum_;
//- Sum of >= highest bin
typename List::value_type highSum_;
public:
// Constructors
//- Construct given min, max, delta
BinSum
(
const IndexType min,
const IndexType max,
const IndexType delta
);
//- Construct given min, max, delta and data
BinSum
(
const IndexType min,
const IndexType max,
const IndexType delta,
const UList<IndexType>& indexVals,
const List& vals,
const CombineOp& cop = plusEqOp<typename List::value_type>()
);
// Access
//- Return the delta
inline IndexType delta() const
{
return delta_;
}
//- Return the sum of all added elements < min
inline const IndexType& lowSum() const
{
return lowSum_;
}
//- Return the sum of all added elements >= max
inline const IndexType& highSum() const
{
return highSum_;
}
void add
(
const IndexType& indexVal,
const typename List::const_reference val,
const CombineOp& cop = plusEqOp<typename List::value_type>()
);
void add
(
const UList<IndexType>& indexVals,
const List& vals,
const CombineOp& cop = plusEqOp<typename List::value_type>()
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "BinSum.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
2.BinSum.C
代码如下(示例):
#include "BinSum.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class IndexType, class List, class CombineOp>
Foam::BinSum<IndexType, List, CombineOp>::BinSum
(
const IndexType min,
const IndexType max,
const IndexType delta
)
:
List(ceil((max-min)/delta), Zero),
min_(min),
max_(max),
delta_(delta),
lowSum_(Zero),
highSum_(Zero)
{}
template<class IndexType, class List, class CombineOp>
Foam::BinSum<IndexType, List, CombineOp>::BinSum
(
const IndexType min,
const IndexType max,
const IndexType delta,
const UList<IndexType>& indexVals,
const List& vals,
const CombineOp& cop
)
:
List(ceil((max-min)/delta), Zero),
min_(min),
max_(max),
delta_(delta),
lowSum_(Zero),
highSum_(Zero)
{
forAll(indexVals, i)
{
add(indexVals[i], vals[i], cop);
}
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class IndexType, class List, class CombineOp>
void Foam::BinSum<IndexType, List, CombineOp>::add
(
const IndexType& indexVal,
const typename List::const_reference val,
const CombineOp& cop
)
{
if (indexVal < min_)
{
cop(lowSum_, val);
}
else if (indexVal >= max_)
{
cop(highSum_, val);
}
else
{
label index = (indexVal-min_)/delta_;
cop(this->operator[](index), val);
}
}
template<class IndexType, class List, class CombineOp>
void Foam::BinSum<IndexType, List, CombineOp>::add
(
const UList<IndexType>& indexVals,
const List& vals,
const CombineOp& cop
)
{
forAll(indexVals, i)
{
add(indexVals[i], vals[i], cop);
}
}
// ************************************************************************* //
附录中的程序来自https://cpp.openfoam.org/v9/BinSum_8C_source.html
总结
随机函数的使用;父类的查找;函数的重载;构造函数和成员函数的使用;OpenFOAM中五个基本文件的函数内容