OFtutorial07_customLibraries解析

关于编译库和编译类的:编译是将函数文件编译成动态链接,另一个求解器/应用程序/类可以调用该链接后再编译,若编译成动态链接后在对文件直接修改将不会影响链接,需要重新编译。因此,编译类和编译库的区别就在编译库之后可以直接使用库链接

组成

如图

customLibrary

组成

customLibrary.H

#include "fvCFD.H"

// This method simply implements the calculation of the distance of each
// cell centre from x0; it accepts the volScalarField r as a reference to avoid
// passing large amounts of information by value, as this is expensive.
// It returns the maximum value found.
scalar computeR(const fvMesh& mesh, volScalarField& r, dimensionedVector x0);//声明computeR函数

// This computes the velocity field. The reference to the pressure is obtained
// through the mesh object, using the name of the p field only. This assmes a
// default value which may be redefined, if necessary.
void computeU(const fvMesh& mesh, volVectorField& U, word pName = "p");//声明computeU函数

customLibrary.C

#include "customLibrary.H"

scalar computeR(const fvMesh& mesh, volScalarField& r, dimensionedVector x0)//定义computeR函数
{
    r = mag(mesh.C()-x0);//mag()用于求矢量的模
    //reduce()函数的作用主要是在并行计算环境中将分布在多个处理器(或计算节点)上的数据“聚合”或“减少”到一个单一的值。其中第一个参数表示从当前处理器取值,这里取了r的最大值,第二个参数是对全局的已取值筛选条件,这里是取最大值。
    return returnReduce(max(r).value(), maxOp<scalar>());
}

void computeU(const fvMesh& mesh, volVectorField& U, word pName)//定义computeU函数
{
    // This allows a reference to a field to be obtained through the mesh object
    // alone by just knowing the name of the field.
    //通过网格对象和字段名称查找压力场 
    const volScalarField& pField = mesh.lookupObject<volScalarField>(pName);

    // Do the usual
    U = fvc::grad(pField)*dimensionedScalar("tmp", dimTime, 1.);
}

Make

与此前章节介绍类似,不做赘述

OFtutorial7.C

#include "fvCFD.H"

// Include the headers for the custom library.
// The library can implement anything from a simple function to several different
// classes. The main advantage of libraries is that they allow the same code to be
// compiled once and used by many other pieces of code later on.
// NOTE: check how the Make/options changed to make sure the additional code gets
// linked to the current utility.
#include "customLibrary.H"

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"
    #include "createFields.H"
    //dimLength给定物理维度,它指定了向量的每个分量都是长度单位
    const dimensionedVector originVector("x0", dimLength, vector(0.05, 0.05, 0.005));
    scalar f (1.);
    // NOTE: initialise the radius field with zero values and dimensions
    volScalarField r
    (
        IOobject
        (
            "r",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::NO_WRITE
        ),
        mesh,
        dimensionedScalar("r0", dimLength, 0.)
    );
    // NOTE: use the method implemented in the library to calculate r and rFarCell
    const scalar rFarCell = computeR(mesh, r, originVector);

    Info<< "\nStarting time loop\n" << endl;

    while (runTime.loop())
    {
        Info<< "Time = " << runTime.timeName() << nl << endl;

        p = Foam::sin(2.*constant::mathematical::pi*f*runTime.time().value())
            / (r/rFarCell + dimensionedScalar("small", dimLength, 1e-12))
            * dimensionedScalar("tmp", dimensionSet(0, 3, -2, 0, 0), 1.);
        p.correctBoundaryConditions();

        // NOTE: call the library method to calculate U
        computeU(mesh, U);

        runTime.write();
    }

    Info<< "End\n" << endl;

    return 0;
}

Make

options

EXE_INC = \
    -I$(LIB_SRC)/finiteVolume/lnInclude \
    -I$(LIB_SRC)/meshTools/lnInclude \
    -IcustomLibrary/包含customLibrary目录

EXE_LIBS = \
    -lfiniteVolume \
    -lmeshTools \
    -L$(FOAM_USER_LIBBIN) -lcustomLibrary//链接customLibrary库

files

不做赘述

Allwmake

wmake libso customLibrary//编译customLibrary库
wmake

Allwclean

wclean libso customLibrary//解构customLibrary库
wclean

testcase

不做赘述

posted @ 2024-08-13 16:27  ouqiyo  阅读(29)  评论(0)    收藏  举报