VandV_pdf.pdf

Embed Size (px)

Citation preview

  • 8/10/2019 VandV_pdf.pdf

    1/44

  • 8/10/2019 VandV_pdf.pdf

    2/44

    We will learn:

    The difference between validation and vericationWhat is the MMSHow to implement a new solver in openFOAM (can be useful!)How to run a set of simulations efciently using Linux tools:

    sedforgrep

    How to sample our openFOAM results and transfer them to MatlabWhat can we do in real life to (try to) ensure that our CFD results are

    corect

  • 8/10/2019 VandV_pdf.pdf

    3/44

    Lets begin !

  • 8/10/2019 VandV_pdf.pdf

    4/44

    By default, everything is wrong

  • 8/10/2019 VandV_pdf.pdf

    5/44

    until you can prove it is correct

    (or x it)

  • 8/10/2019 VandV_pdf.pdf

    6/44

    NUMERICALMODEL

    REALITY PHYSICALMODEL

    Qualification

    VerificationValidation

    1: Write a set of

    equations (usuallyODE/PDE) thatimplement physicallaws

    2: Two steps-Write a computer code to solve theequations-Choose a mesh, time step, residuals..and solve numerically the equations

    0: The reality(inntely subtle)

  • 8/10/2019 VandV_pdf.pdf

    7/44

    NUMERICALMODEL

    REALITY PHYSICALMODEL

    Qualification

    VerificationValidation

    Qualify the physicaland numericalmodels for their use

    in PREDICTIONS

    Check that thenumerical solutionof the equations iscorrect

    Check that the

    physical model isaccurate enough

  • 8/10/2019 VandV_pdf.pdf

    8/44

    NUMERICALMODEL

    REALITY PHYSICALMODEL

    Qualification

    VerificationValidation

    Holy Grial ofcomputational

    enginering !!

    SOLVE THEEQUATIONS RIGHT

    SOLVE THERIGHT EQUATIONS

  • 8/10/2019 VandV_pdf.pdf

    9/44

    NUMERICALMODEL

    REALITY PHYSICALMODEL

    Qualification

    VerificationValidation

    MATHEMATICAL

    process

    EXPERIMENTALprocess

  • 8/10/2019 VandV_pdf.pdf

    10/44

    Reality

    Ideal world

    Imperfect,cheap copy

    Mathematical reasoning

    Validation and Verication in Platos mind

    Discovering (actually,remembering) the ideal world

    Never ! Are you joking ??

    If you want to understand the stars, dont observe them,think about them

  • 8/10/2019 VandV_pdf.pdf

    11/44

    MMS: Method of Manufactured Solutions

    DP DE (u ) = b dD:

    Problem to be solved with OpenFoam:

    d: = bc u

    n=

    u

    n bc

    Only a few trivial cases have known analytic solutionThen, how can we check our solutions ?

    .. what could be wrong?-the code itself (not to be expected in OpenFoam)-the mesh and/or the time step (most likely)-the model used (e.g, turbulence model)

    Given b and the b.c., nd u

  • 8/10/2019 VandV_pdf.pdf

    12/44

    MMS inverts the process to generate arbitrarily complexsolutions of our problem:

    Generate u and then obtain (by derivation) b

    Exemple: Conduction heat transfer equation

    fvm::ddt(T) - fvm::laplacian( D, T) == B

    T

    t D

    2 T

    x2

    + 2 T

    y2

    + 2 T

    z2

    = B

    T

    t D2

    T = B D: thermal diffusivity

    OpenFoam representation:

  • 8/10/2019 VandV_pdf.pdf

    13/44

    We choose our domain to be [0:1]^3

    To keep things simple, we choose T to beonly a function of x, and thermal diffusivityto be 1. Example:

    T=-4*x*(x-1)*(x-3/4)*(x-1/4) but in general, it can be (has to be) function of x,y,z

    Then, by analitic derivation:

    B=8*(x - 1)*(x - 1/4) + 8*(x - 1)*(x - 3/4) +8*(x - 1/4)*(x - 3/4) + 8*x*(x - 1) + 8*x*(x - 1/4) +8*x*(x - 3/4)

  • 8/10/2019 VandV_pdf.pdf

    14/44

    For lazy people.. use Matlab : (generaB)clear allclose allsyms x y z t T BDT=1 % difusividad termica (demasiado elevada, sin sentido sico)

    T=-4*x*(x-1)*(x-3/4)*(x-1/4)

    % ecuacion que vamos a resolver en OpenFOAM:% fvm::ddt(T) - fvm::laplacian(DT, T) == B

    % por tanto, este debe ser el termino fuente ..B = diff(T,t) - DT*(diff(T,x,2)+diff(T,y,2)+diff(T,z,2) )

    % Si queremos examinar los valores numericos de T o B, podemos hacerlo

    % creando funciones a partir de los resultados simbolicosfB=@(x,y,z,t) eval(char(B));fT=@(x,y,z,t) eval(char(T));

    fB(0,0.5,0.5,0.5)

    fT(0,0.5,0.5,0.5)

  • 8/10/2019 VandV_pdf.pdf

    15/44

    Another example, again with thermal diffusivity equal to 1

    T=-4*x*(x-1)*(x-3/4)*(x-1/4)*(x-7/10)*(x-8/10)*x^2

    B = diff(T,t) - DT*(diff(T,x,2)+diff(T,y,2)+diff(T,z,2) )

    B=simplify(B)

    this yields:

    B=(x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/50

  • 8/10/2019 VandV_pdf.pdf

    16/44

    B=(x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/50

  • 8/10/2019 VandV_pdf.pdf

    17/44

    T=-4*x*(x-1)*(x-3/4)*(x-1/4)*(x-7/10)*(x-8/10)*x^2

  • 8/10/2019 VandV_pdf.pdf

    18/44

    Now, we will use openFOAM to solve the problem:

    T t D

    2 T x 2 +

    2 T y 2 +

    2 T z 2 = B

    whereB= (x*(11200*x^5 - 29400*x^4 + 28485*x^3 - 12355*x^2 + 2271*x - 126))/5 the domain is a 1x1x1 cube and:T=0 in the x boundariesdT/dn=0 in the y,z boundaries (to make it 1d)

    Laplace equation,can be solved with the available laplacianFoam, but tointroduce a RHS, we need to write a new solver

    2 T = 0

  • 8/10/2019 VandV_pdf.pdf

    19/44

    A new solver in this case is surprisingly easy, it will only beabout 50 lines of C++ code !

    very easy (with the help of a friend) :)Copy laplacianFoam source code in a separate folder

    In createFields.h, add the following:

    Info

  • 8/10/2019 VandV_pdf.pdf

    20/44

    In laplacianFoam.C change the following:

    1-Assign a value for B to each domain cell, as a functionof x,yz, and t2-Change the PDE equation to be solved

    in the original laplacianFoam, the equation is:

    solve(

    fvm::ddt(T) - fvm::laplacian(DT, T)

    );

    This is C++ !

  • 8/10/2019 VandV_pdf.pdf

    21/44

    while (simple.loop()){

    Info

  • 8/10/2019 VandV_pdf.pdf

    22/44

    W l d t t d iti f B ld i 0 f ld

  • 8/10/2019 VandV_pdf.pdf

    23/44

    We also need to create a denition for B eld in 0 folderThis is a bit nonsense as B will be assigned later, but it is the easiersolution

    FoamFile{version 2.0;format ascii;class volScalarField;object T;

    }

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //dimensions [0 0 -1 1 0 0 0];internalField uniform 0;boundaryField{

    adiabatic{

    type zeroGradient;}cold{

    type xedValue;value uniform 0;

    }

    }

    K/s

    T

    t D

    2 T

    x2

    + 2 T

    y2

    + 2 T

    z2

    = B

    K/s K/m^2 K/sm^2/s

    I t t/t tP ti if th l diff i it 1

  • 8/10/2019 VandV_pdf.pdf

    24/44

    In constant/transportProperties, we specify thermal diffusivity=1

    DT DT [ 0 2 -1 0 0 0 0 ] 1.0;

    m^2/s

    I constant/pol Mesh/blockMeshDict if

  • 8/10/2019 VandV_pdf.pdf

    25/44

    In constant/polyMesh/blockMeshDict, we specify:

    blocks(

    hex (0 1 2 3 4 5 6 7) ( 10 10 10 ) simpleGrading (1 1 1));

    In system/controlDict we specify:

  • 8/10/2019 VandV_pdf.pdf

    26/44

    In system/controlDict , we specify:

    application laplacianFoam;

    startFrom startTime;

    startTime 0;

    stopAt endTime;

    endTime 1e6;

    deltaT 1e6;

  • 8/10/2019 VandV_pdf.pdf

    27/44

    Steps to run a simulation (once the solver is ready):

    1-With paper&pencil (or Matlab if you are as lazy as me)

    -Generate T-Obtain B

    2-Insert B in OpenFoam and solve for T-Edit solver to change B (take care with integers!!)

    -Compile (wmake) and copy (cp) the executable-Change constant/polyMesh/blockMeshDict (to testdifferent meshes)

    -Change system/controlDict (if needed)

    -Change system/sampleDict (if needed)-Generate the new mesh: blockMesh-Run the solver: ./ newlaplacianFoam

  • 8/10/2019 VandV_pdf.pdf

    28/44

    We can see the results in paraview, but it will be better to usethe openFoam sample utility

  • 8/10/2019 VandV_pdf.pdf

    29/44

    specications for sample are in system/sampleDict

    // Fields to sample:elds

    ( T B);

    sets(

    lineX1{

    type uniform;axis x;

    start (0.0 0.5 0.5);end (1.0 0.5 0.5);nPoints 100;

    });

    100 points in line parallel to x axis,from (0, 1/2, 1/2) to (1,1/2,1/2)

  • 8/10/2019 VandV_pdf.pdf

    30/44

    in /postProcessing/sets/1e+06, the le with our sample is created:

    manel@ubuntu:~/Dropbox/CFD/cubeBpar/postProcessing/sets/1e

    +06$ head lineX1_B_T.xy 0 0 00.010101 0.000825318 1.97071e-050.020202 0.00165064 3.94142e-050.030303 0.00247595 5.91213e-050.040404 0.00330127 7.88284e-050.0505051 0.00412659 9.85355e-050.0606061 0.00495191 0.0001182430.0707071 0.00577723 0.00013795

    0.0808081 0.00660255 0.0001576570.0909091 0.00742786 0.000177364

    x B T

    We use Matlab to plot it and compare with the exact solution:

  • 8/10/2019 VandV_pdf.pdf

    31/44

    We use Matlab to plot it and compare with the exact solution:

    clear

    leID = fopen('../cubeBpar/postProcessing/sets/1e+06/lineX1_B_T.xy','r');formatSpec = '%f %f %f';sizeA = [3 Inf];DAT = fscanf(leID,formatSpec,sizeA); % DAT contains: row 1: x positions; row 2: B; row 3: T% Calculamos solucion analitica

    y=0.5; % irrelevante, es unidimensionalz=0.5; % idemt=0.5; % No se usafor i=1:size(DAT,2) % numero de puntos

    xv(i)=DAT(1,i);

    x=xv(i);Ta(i)=-4*x*(x-1)*(x-3/4)*(x-1/4)*(x-7/10)*(x-8/10)*x^2;endplot(xv,Ta,'b');hold onplot(DAT(1,:),DAT(3,:),'r');

    T

  • 8/10/2019 VandV_pdf.pdf

    32/44

    x

    T

    openFoam (10)

    exact

  • 8/10/2019 VandV_pdf.pdf

    33/44

    Steps to compare with the imposed analytical solution(once the numerical solution has been computed):

    1-Obtain a 1d cut of the T eld-sample

    we choose a 1d cut just because our problem is 1d, ingeneral, all the domain has to be sampled

    2-Read it, and compute the exact solution in the samepositions, you can do it with Matlab or octave, gnuplot,

    T

  • 8/10/2019 VandV_pdf.pdf

    34/44

    T

    Not too bad, but is it correct ?How many control volumes do we need for an accurate solution?

    We need to repeat the simulation with increasingly ne meshes ! Can we do it automatically ?Yes, we can !

    x

  • 8/10/2019 VandV_pdf.pdf

    35/44

    FoamFile{

    version 2.0;format ascii;class dictionary;object blockMeshDict;

    } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //convertToMeters 1.0;

    vertices(

    (0 0 0)(1 0 0)(1 1 0)(0 1 0)(0 0 1)

    (1 0 1)(1 1 1)(0 1 1)

    );

    blocks(

    hex (0 1 2 3 4 5 6 7) ( #NX# 10 10) simpleGrading (1 1 1));

    constant/polyMesh/blockMeshDict.NX (arbitrary name)

  • 8/10/2019 VandV_pdf.pdf

    36/44

    We will use the command sed to replace the string #NX#with the number of control volumes that we want, for instance 123:

    cat blockMeshDict.NX | sed "s/#NX#/123/" > blockMeshDict

    Type the leblockMeshDict.NX, butdirect the output to thenext command (symbol|)

    Substitute #NX# by123

    Direct the outputto the leblockMeshDict

  • 8/10/2019 VandV_pdf.pdf

    37/44

    We create a le called (for instance) doit_once , with :

    cd constant/polyMesh/cat blockMeshDict.NX | sed "s/#NX#/ $1 /" > blockMeshDictcd ../..blockMesh./newlaplacianFoam

    samplecp -r postProcessing postProcessing_ $1

    Then, we type:chmod +x doit_once./doit_once 20

    First parameter

    Make it executable

    Run with 20

    Copy theresults to anotherfolder

  • 8/10/2019 VandV_pdf.pdf

    38/44

    for x in 5 10 20 40 80 160 320 640 1280do

    echo $x./doit_once $x

    done

    We create doit_all, with :

    T

  • 8/10/2019 VandV_pdf.pdf

    39/44

    x

    T

    error

  • 8/10/2019 VandV_pdf.pdf

    40/44

    ! x

    error

    ! x2

    Max (T! x-Ta)

  • 8/10/2019 VandV_pdf.pdf

    41/44

    The meaning of it all

    u i +1 u i 1

    2 x

    u

    x

    x i

    = x 2

    3! 3u

    x 3

    x i

    + . . .

    xi 1

    xi

    xi+1

    u (x i + x i ) u (x i ) x i

    u

    x x i=

    x i

    2! 2u

    x 2x i+

    x 2i

    3! 3u

    x 3x i+ . . .

    | {z } Truncation ErrorFirst orderapproximation

    Second orderapproximation

    http://www.rsmas.miami.edu/personal/miskandarani/Courses/MSC321/lectniteDifference.pdf

    http://www.rsmas.miami.edu/personal/miskandarani/Courses/MSC321/lectfiniteDifference.pdf
  • 8/10/2019 VandV_pdf.pdf

    42/44

  • 8/10/2019 VandV_pdf.pdf

    43/44

    T l d

  • 8/10/2019 VandV_pdf.pdf

    44/44

    To conclude

    DO Begin with a similar problem that has a known solution, look in

    literature for experimental results; reproduce it and understand theresuts

    Use AT LEAST two or (better) three different meshes of differentdensities to evaluate how close to the mesh independent solution areyou

    Be specially careful with turbulence models !!

    NEVER DO Never launch a simulation with a model (ie, multiphase, compressible,

    LES..) if you dont understand the equations and the physics behind .

    Never be satised with a single solution. If you can rene the mesh,generate a coarser mesh.Be impressed by the beauty of possibly wrong solutions

    REMEMBER: One day CFD will be reliable and wind tunnels will be closed; until then,