CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Accessing old fields: lookupObject at user defined time (https://www.cfd-online.com/Forums/openfoam-programming-development/221062-accessing-old-fields-lookupobject-user-defined-time.html)

em17 October 2, 2019 12:41

Accessing old fields: lookupObject at user defined time
 
Hi all,

I would like to look up an object (e.g. U) from a user-defined time previously. I cannot hard code this as the number will change from simulation to simulation.

If I am to define this number N, in a constant/*Properties file, then I would like to define something like lookupObject<volVectorField>("U") (at time: current time - N).
For example, say the current time is 1s, then I would like to lookup the velocity field at 0.7s by using current time (1) minus user-defined time, N (in this case, N=0.3). Does anyone know how I can do this?

Thank you,
Emily

frobaux October 3, 2019 11:05

Hello,

You can do it by using the function

Code:

runtime.timeName()
for your example:
Code:

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

should work as intended

em17 October 3, 2019 11:53

Hi,

Thanks for your reply. I should have mentioned that I am trying to write a functionObject so I don't think I can use runTime. I can however use some variant of obr_.time().something . Do you know if can get the equivalent of runTime.timeName(0.7) using obr_.time()?

Thanks

frobaux October 4, 2019 04:34

Well, I'm sorry I don't know. I'm not familiar at all with the functionObjects.
What is the type of obr_?
The function Foam::Time::timeName() is pretty simple, maybe it would be possible to recode it. The only problem would be the label "precision", which describe the maximum number of decimals.
Note that this precision is set at the begining then updated if needed in order to discernate the differents results.



Code:

Foam::word Foam::Time::timeName(const scalar t, const int precision)
{

  std::ostringstream buf;
    buf.setf(ios_base::fmtflags(format_), ios_base::floatfield);
    buf.precision(precision);
    buf << t;
    return buf.str();
}


em17 October 7, 2019 07:10

I figured out how to do it using obr_.time().timeName(0.7) in case anyone else comes across this problem.

Thanks again for your help.


All times are GMT -4. The time now is 08:06.