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

dif221Foam.C

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

Application
    dif221Foam

Description
    Solves training examples 2.2.1 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 t = 0.2;
    
    volScalarField T_ex(T);
    using std::sqrt;
    
    forAll(T_ex,celli)
    {
        scalar xx = mesh.C()[celli].x();
        scalar yy = mesh.C()[celli].y();
        T_ex[celli] = 0.25*(-Foam::erf((-1-xx)/(2*sqrt(t)))+Foam::erf((1-xx)/(2*sqrt(t))))*(-Foam::erf((-1-yy)/(2*sqrt(t)))+Foam::erf((1-yy)/(2*sqrt(t))));
    }
    
    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;
    scalar my_tmp = 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;

    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;
// }

forAll(T,celli)
{
    scalar xx = mesh.C()[celli].x();
    scalar yy = mesh.C()[celli].y();
    if ((sqrt(xx*xx)<=1)&&(sqrt(yy*yy)<=1))
    {
    	T[celli] = 1;
    }
    else
    {
    	T[celli] = 0;
    }
}
T.correctBoundaryConditions();
T.write();


#include "createPhi.H"

#include "createFvOptions.H"

 fvSolution

/*--------------------------------*- 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       dictionary;
    location    "system";
    object      fvSolution;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

solvers
{
    T
    {
        solver          PBiCGStab;
        preconditioner  DIC;
        tolerance       1e-06;
        relTol          0;
    }
}

SIMPLE
{
    nNonOrthogonalCorrectors 0;
}


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

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            zeroGradient;
    }
    lowerWall
    {
        type            zeroGradient;
    }
    leftWall
    {
        type            zeroGradient;
    }
    rightWall
    {
        type            zeroGradient;
    }
    frontAndBack
    {
        type            empty;
    }
}

}

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

U

/*--------------------------------*- 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       volVectorField;
    object      U;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 1 -1 0 0 0 0];

internalField   uniform (0 0 0);

boundaryField
{
    upperWall
    {
        type            slip;
    }
    lowerWall
    {
        type            slip;
    }
    leftWall
    {
        type            slip;
    }
    rightWall
    {
        type            slip;
    }
    frontAndBack
    {
        type            empty;
    }
}

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

X

/*--------------------------------*- 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       volVectorField;
    object      X;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 0 0 0 0 0 0];

internalField   uniform (0 0 0);

boundaryField
{
    upperWall
    {
        type            slip;
    }
    lowerWall
    {
        type            slip;
    }
    leftWall
    {
        type            slip;
    }
    rightWall
    {
        type            slip;
    }
    frontAndBack
    {
        type            empty;
    }
}

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

20tr.geo



Point(1) = {-5.0, -5.0, 0, 1e22};
Point(2) = {5.0, -5.0, 0, 1e22};
Point(3) = {5.0, 5.0, 0, 1e22};
Point(4) = {-5.0, 5.0, 0, 1e22};
//+
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};
//+
Line Loop(1) = {1, 2, 3, 4};
Plane Surface(1) = {1};

Transfinite Line {3,1} = 21 Using Progression 1;
Transfinite Line {2,4} = 21 Using Progression 1;

Transfinite Surface {1};

// Recombine Surface {1};

Extrude {0, 0, 0.1} {
  Surface{1}; Layers{1}; Recombine;
}


Physical Surface("frontAndBack") = {26, 1};
Physical Surface("leftWall") = {25};
Physical Surface("rightWall") = {17};
Physical Surface("upperWall") = {21};
Physical Surface("lowerWall") = {13};
Physical Volume("box") = {1};

dif221cr.m

clearvars; clc; % dif221cr.m

L1_Err40 = 0.0124226; L2_Err40 = 0.010474; Lfy_Err40 = 0.00952007;
L1_Err80 = 0.00308746; L2_Err80 = 0.00256125; Lfy_Err80 = 0.00232223;
L1_Err20 = 0.0585821; L2_Err20 = 0.0491155; Lfy_Err20 = 0.0380739;
L1_Err20T = 0.0254147; L2_Err20T = 0.0204783; Lfy_Err20T = 0.0199421;
Delta40 = 40*40; Delta80 = 80*80; Delta20 = 20*20; Delta20T = 20*20*2; n = 2;
NL14080 = cal_N_func(L1_Err40, L1_Err80, Delta40, Delta80, n);
NL24080 = cal_N_func(L2_Err40, L2_Err80, Delta40, Delta80, n);
NLfy4080 = cal_N_func(Lfy_Err40, Lfy_Err80, Delta40, Delta80, n);
NL12040 = cal_N_func(L1_Err20, L1_Err40, Delta20, Delta40, n);
NL22040 = cal_N_func(L2_Err20, L2_Err40, Delta20, Delta40, n);
NLfy2040 = cal_N_func(Lfy_Err20, Lfy_Err40, Delta20, Delta40, n);
NL12080 = cal_N_func(L1_Err20, L1_Err80, Delta20, Delta80, n);
NL22080 = cal_N_func(L2_Err20, L2_Err80, Delta20, Delta80, n);
NLfy2080 = cal_N_func(Lfy_Err20, Lfy_Err80, Delta20, Delta80, n);
NL1QT20 = cal_N_func(L1_Err20, L1_Err20T, Delta20, Delta20T, n);
NL2QT20 = cal_N_func(L2_Err20, L2_Err20T, Delta20, Delta20T, n);
NLfyQT20 = cal_N_func(Lfy_Err20, Lfy_Err20T, Delta20, Delta20T, n);

dif221T20.m

clearvars; clc; % dif221T20.m % edit

dataPath = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\data\2.2.1\';
targetPath = 'D:\SJTU_senior_1st_total\Sundry\grad\hw\hw1\figs\2.2.1\';
targetName = 'T20.png'; % edit
targetName = [targetPath, targetName];
tria100nameY025 = '20trX00.xlsx'; % edit
tria100nameY025 = [dataPath, tria100nameY025];
tria100matY025 = importdata(tria100nameY025);
% tria100exNameY025 = '80triaX050ex.xlsx'; % edit
% tria100exNameY025 = [dataPath, tria100exNameY025];
% tria100matexY025 = importdata(tria100exNameY025);
yExt = tria100matY025(:,1); xExt = zeros(size(yExt,1),1); runTime = 0.2;
phiExt = zeros(size(xExt,1),1);
for i = 1 : size(phiExt,1)
    phiExt(i,1) = cal_dif221phiExt_func(xExt(i,1), yExt(i,1), runTime);
end
tria100matexY025 = [yExt, phiExt];
figure(1);
y_llim = -0.1; y_rlim = 1.1;
set(gca, 'ylim', [y_llim, y_rlim]); hold on;
x_to_plot = tria100matY025(:,1); y_to_plot = tria100matY025(:,2);
size = 20;
scatter(x_to_plot, y_to_plot, size, 'Black', 'c', 'filled');
hold on;
x_to_plot = tria100matexY025(:,1); y_to_plot = tria100matexY025(:,2);
plot(x_to_plot, y_to_plot, 'Black', 'LineWidth', 1);
title('2.2.1 20 Tria. SFCD X = 0', 'FontSize', 15); % edit
legend('Numerical','Analytic','Location','NW', 'FontSize', 15);
saveas(gcf, targetName); close all;
2.2.1.SFCD 2阶精度
grids L1Err order L2Err order L3Err order
20*20 0.0585821 2.237492755 0.0491155 2.22936588 0.0380739 1.999758269
40*40 0.0124226 2.12298378 0.010474 2.1306292 0.00952007 2.017609792
80*80 0.00308746 2.008474805 0.00256125 2.03189253 0.00232223 2.035461314
20*20*2 0.0254147 2.409593419 0.0204783 2.52416486 0.0199421 1.865970031

 

猜你喜欢

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