上海交通大学船舶海洋与建筑工程学院谢彬Numerical TESTs for PDEs解答3.2.2

本文介绍了一个基于OpenFOAM的3.2.2训练示例的求解过程,包括方程离散化、边界条件设置、精确解计算及数值解与精确解之间的误差分析。

adf322Foam.C

/*---------------------------------------------------------------------------*\
	Changed from scalarTransportFoam to adv121Foam

Application
    adf322Foam

Description
    Solves training examples 3.2.2 problem.

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

#include "fvCFD.H"
#include "fvOptions.H"
#include "simpleControl.H"

scalar get_xhat(scalar x0, scalar y0, scalar t){
    using std::sin;
    using std::cos;
    scalar xhat = x0*cos(t) - y0*sin(t);
    return xhat;
}

scalar get_yhat(scalar x0, scalar y0, scalar t){
    using std::sin;
    using std::cos;
    scalar yhat = y0*cos(t) - x0*sin(t);
    return yhat;
}

scalar get_my_r2(scalar x, scalar xhat, scalar y, scalar yhat){
    scalar my_r2 = 0;
    my_r2 = (x-xhat)*(x-xhat) + (y-yhat)*(y-yhat);
    return my_r2;
}

scalar get_phiExt(scalar r2, scalar t){
    scalar epsilon = 1e-3;
    scalar phiExt = 0;
    using Foam::exp;
    phiExt = (1/(4*M_PI*epsilon*t)*exp(-(r2/(4*epsilon*t))));
    return phiExt;
}

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

int main(int argc, char *argv[])
{
    #include "setRootCaseLists.H"
    #include "createTime.H"
    #include "createMesh.H"

    simpleControl simple(mesh);

    #include "createFields.H"

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

    Info<< "\nCalculating scalar transport\n" << endl;

    #include "CourantNo.H"

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

        while (simple.correctNonOrthogonal())
        {
            fvScalarMatrix TEqn
            (
                fvm::ddt(T)
              + fvm::div(phi, T)
              - fvm::laplacian(DT, T)
             ==
                fvOptions(T)
            );

            TEqn.relax();
            fvOptions.constrain(TEqn);
            TEqn.solve();
            fvOptions.correct(T);
        }

        runTime.write();
    }
    
    // t = runTime.value()
    // scalar my_t = 1.0;
    // scalar my_a = 1.0;
    
    volScalarField T_ex(T);
    using std::sqrt;
    
    // Calculate Exact Number
    
    forAll(T_ex,celli)
    {
        scalar xx = mesh.C()[celli].x();
        scalar yy = mesh.C()[celli].y();
        scalar my_r21 = get_my_r2(xx, xhat1, yy, yhat1);
        scalar phiExt1 = get_phiExt(my_r21, my_t1);
        T_ex[celli] = phiExt1;
    }
    
    // Error Analysis
    
    scalar L1=0;
    scalar up = 0;
    scalar low =0;
    
    forAll(T,celli)
    {
        up += mag(T_ex[celli]-T[celli]) * mesh.V()[celli];
        low += mag(T_ex[celli])*mesh.V()[celli];
    }
    L1 = up/low;
    
    Info << "L1 error = " << L1 << endl;
    
    scalar L2 = 0;
    up = 0;
    low = 0;
    forAll(T,celli)
    {
    	up += mag(T_ex[celli]-T[celli])*mag(T_ex[celli]-T[celli])*mesh.V()[celli];
    	low += T_ex[celli]*T_ex[celli]*mesh.V()[celli];
    }
    using std::sqrt;
    L2 = sqrt(up/low);
    
    Info << "L2 error = " << L2 << endl;
    
    scalar L3 = 0;
    scalar up_max = 0;
    scalar low_max = 0;
    
    forAll(T,c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jmsyh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值