CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Adjoint method. Backwards in time (https://www.cfd-online.com/Forums/openfoam/197956-adjoint-method-backwards-time.html)

jlorenzo January 22, 2018 11:32

Adjoint method. Backwards in time
 
Hello,

I'm trying to code the adjoint method for the unsteady heat equation in OpenFOAM. I am able to solve the forward problem, but I'm having some trouble with the backwards problem. Actually, I don't know how to tell OpenFoam to solve a problem backwards in time, I've tried something like runTime--, but apparently this doesn't make sense to OF. Any idea on this?

Another option I'm thinking about is to solve the adjoint problem forward in time by means of a change of variable like tau = T - t. However, in this case I have to reverse the contents in the time folders, so that data files in folder 0 go to folder T, data files in folder 1 go to folder T - 1, and so on... How could I achieve this?

Thank you.

jlorenzo January 23, 2018 11:28

I have tried the second option, reading the volScalarFields in reverse direction and saving the result. This way I can solve the adjoint problem in forward direction. However, probably there are more elegant ways of doing so, I would appreciate if someone could give some hints.

By the way, maybe this thread should go in the "Programming & Development" sub-forum, but I don't know how to move it.

writeAdjointSourceTerm.H
Code:

// Reverse source term in the adjoint equation
instantList Times = runTime.times();
scalar nTimes = Times.size() - 1;

for (label i = 1; i <= nTimes; i++)
{
    runTime.setTime(Times[nTimes - i + 1], nTimes - i + 1);

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

    volScalarField Td
    (
        IOobject
        (
        "Td",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::NO_WRITE
        ),
        mesh
    );

    runTime.setTime(Times[i], i);

    volScalarField g
    (
        IOobject
        (
        "g",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
        ),
        T - Td
    );

    g.write();
}


Chuangxin May 10, 2018 17:59

Did you try using a negative time step? I find it works.


Quote:

Originally Posted by jlorenzo (Post 679070)
I have tried the second option, reading the volScalarFields in reverse direction and saving the result. This way I can solve the adjoint problem in forward direction. However, probably there are more elegant ways of doing so, I would appreciate if someone could give some hints.

By the way, maybe this thread should go in the "Programming & Development" sub-forum, but I don't know how to move it.

writeAdjointSourceTerm.H
Code:

// Reverse source term in the adjoint equation
instantList Times = runTime.times();
scalar nTimes = Times.size() - 1;

for (label i = 1; i <= nTimes; i++)
{
    runTime.setTime(Times[nTimes - i + 1], nTimes - i + 1);

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

    volScalarField Td
    (
        IOobject
        (
        "Td",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::NO_WRITE
        ),
        mesh
    );

    runTime.setTime(Times[i], i);

    volScalarField g
    (
        IOobject
        (
        "g",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
        ),
        T - Td
    );

    g.write();
}




All times are GMT -4. The time now is 11:26.