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

dif222Foam.C

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

Application
    dif222Foam

Description
    Solves training examples 2.2.2 problem.

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

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

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

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::laplacian(DT, T)
             ==
                fvOptions(T)
            );

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

        runTime.write();
    }
    
    // t = runTime.value()
    scalar my_t2 = 0.2;
    
    volScalarField T_ex(T);
    using std::sqrt;
    my_t = 0.2;
    forAll(T_ex,celli)
    {
        scalar xx = mesh.C()[celli].x();
        scalar yy = mesh.C()[celli].y();
        scalar r = xx*xx + yy*yy ;
        T_ex[celli] = (1/(1+4*my_t2))*exp(-((my_mu*r)/(1+4*my_t2)));
    }
    
    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 L_infy = 0;
    scalar up_max = 0;
    scalar low_max = 0;
    
    forAll(T,celli)
    {
    	my_tmp = mag(T_ex[celli]-T[celli]);
    	if (up_max<=my_tmp)
    	{
    	    up_max = my_tmp;
    	}
    	else {}
    	my_tmp = mag(T_ex[celli]);
    	if (low_max<=my_tmp)
    	{
    	    low_max = my_tmp;
    	}
    	else {}
    }
    L_infy = up_max / low_max;
    
    Info << "L_infy error = " << L_infy << endl;
    
    scalar dampRate = 0;
    scalar end_max = 0;
    
    forAll(T,celli)
    {
        my_tmp = T[celli];
        if (end_max<=my_tmp)
        {
            end_max = my_tmp;
        }
        else {}
    }
    dampRate = 1 - end_max / ini_max;
    
    Info << "Damping Rate = " << 100*dampRate << " %"<< endl;

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

    return 0;
}


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

createFields.H

Info<< "Reading field T\n" << endl;

volScalarField T
(
    IOobject
    (
        "T",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::AUTO_WRITE
    ),
    mesh
);


Info<< "Reading field U\n" << endl;

volVectorField U
(
    IOobject
    (
        "U",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::AUTO_WRITE
    ),
    mesh
);


// Info<< "Reading field X\n" << endl;
// 
// volVectorField X
// (
//     IOobject
//     (
//         "X",
//         runTime.timeName(),
//         mesh,
//         IOobject::NO_READ,
//         IOobject::AUTO_WRITE
//     ),
//     mesh
// );


Info<< "Reading transportProperties\n" << endl;

IOdictionary transportProperties
(
    IOobject
    (
        "transportProperties",
        runTime.constant(),
        mesh,
        IOobject::MUST_READ_IF_MODIFIED,
        IOobject::NO_WRITE
    )
);


Info<< "Reading diffusivity DT\n" << endl;

dimensionedScalar DT
(
    transportProperties.lookup("DT")
);


using std::sqrt;

// forAll(X,celli)
// {
//     scalar xx = mesh.C()[celli].x();
//     scalar yy = mesh.C()[celli].y();
//     X[celli].x() = xx;
//     X[celli].y() = yy;
//     X[celli].z() = 0;
// }
scalar my_mu = 1;
scalar my_t = 0;

using Foam::exp;

scalar my_tmp = 0;

forAll(T,celli)
{
    scalar xx = mesh.C()[celli].x();
    scalar yy = mesh.C()[celli].y();
    scalar r = xx*xx + yy*yy ;
    T[celli] = (1/(1+4*my_t))*exp(-((my_mu*r)/(1+4*my_t)));
}
T.correctBoundaryConditions();
T.write();

scalar ini_max = 0;
forAll(T,celli)
{
    my_tmp = T[celli];
    if (ini_max<=my_tmp)
    {
        ini_max = my_tmp;
    }
    else {}
}

#include "createPhi.H"

#include "createFvOptions.H"

T

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  7
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       volScalarField;
    object      T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 1 0 0 0];

internalField   uniform 0;

boundaryField
{
    upperWall
    {
        type            fixedValue;
        value		 0;
    }
    lowerWall
    {
        type            fixedValue;
        value		 0;
    }
    leftWall
    {
        type            fixedValue;
        value		 0;
    }
    rightWall
    {
        type            fixedValue;
        value		 0;
    }
    frontAndBack
    {
        type            empty;
    }
}

}

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

boundary

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  7
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       polyBoundaryMesh;
    location    "constant/polyMesh";
    object      boundary;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

5
(
    frontAndBack
    {
        type            empty;
        physicalType    empty;
        nFaces          1600;
        startFace       1160;
    }
    lowerWall
    {
        type            patch;
        physicalType    patch;
        nFaces          20;
        startFace       2760;
    }
    rightWall
    {
        type            patch;
        physicalType    patch;
        nFaces          20;
        startFace       2780;
    }
    upperWall
    {
        type            patch;
        physicalType    patch;
        nFaces          20;
        startFace       2800;
    }
    leftWall
    {
        type            patch;
        physicalType    patch;
        nFaces          20;
        startFace       2820;
    }
)

// ************************************************************************* //
2.2.2.linear 题目有误,r的表达式错误,缺少根号 时间步长在一定范围内,对误差影响不大
grids L1Err order L2Err order L3Err order
20*20 0.0207604 1.00923705 0.0198801 1.70077868 0.027724 2.442700616
20*20*2 0.0146329 2.014701463 0.0110262 2.02366649 0.0118903 1.876539103
40*40 0.00513748 3.020165877 0.00488916 2.3465543 0.00755025 1.310377591

猜你喜欢

转载自blog.csdn.net/joshua_shi_t/article/details/121425656