CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (http://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Storing previous time step data (http://www.cfd-online.com/Forums/openfoam-post-processing/85506-storing-previous-time-step-data.html)

mohanamuraly February 28, 2011 05:47

Storing previous time step data
 
Hi

I am writing an acoustics propagation code using OF. I have my compressible code results at given time steps sizes. I need to load the data at time i (current) and i-1 (previous time) to evaluate the time derivative of the source terms in the integral.


For getting the time derivative of pressure I did this
---------------------------------------------------------------------------
volScalarField p_cur
(
IOobject
(
"p",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

and stored the current value in a variable p_old
volScalarField p_old(p_cur);
mesh.readUpdate();

But when I try to do
volScalarField dp_dt = p_cur - p_old;

It always returns exact zero as the time derivative for pressure. Is the constructor making a reference of p_cur and not making a new copy of p_cur in p_old ? If so how do I copy and not reference.

Pavanakumar M

mohanamuraly February 28, 2011 07:30

I finally figured out the way after fiddling with the OF code. The key is the creation of as many time objects as the number of time steps required. For example, a two point stencil in time will require two time objects.

So we make two as follows :

// This stores the current time
Foam::Time runTime
(
Foam::Time::controlDictName,
args.rootPath(),
args.caseName()
);

// This stores the previous time
Foam::Time prevTime
(
Foam::Time::controlDictName,
args.rootPath(),
args.caseName()
);

And then you create the fields p_cur (current) and p_old (previous) as follows:

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

volScalarField p_old
(
IOobject
(
"p",
prevTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

In your code make sure that you update the runTime object first and one iteration delayed update of prevTime object. This will ensure that you maintain the current and previous time step data in your code.

I dont know if using two time objects is ok. But it is working for me.


Best,

Pavanakumar M

mm.abdollahzadeh October 8, 2012 05:21

Quote:

Originally Posted by mohanamuraly (Post 297266)

In your code make sure that you update the runTime object first and one iteration delayed update of prevTime object. M

Dear Friend

I dont know how to update the object at my runTime.

could you give me some idea please

Best
Mahdi

mohanamuraly October 8, 2012 07:00

Mahdi,

This is a very bad idea of using time objects. This will keep three copies of the entire solution at two time levels, which is bad idea. Currently I dropped this idea and decided to parse the entire solutiontime history forming a source array. I then use this source array to find the time derivatives and acoustic integrals. This also makes sense if you want to do parallel noise prediction.

I am anyway attaching the source verbatim from my old git snap-shot for your benefit ... but don't blame me for damages that it might do to you ... and I am not sure even if this works (it should but too lazy to test if this is the correct version in my git repo)

#include<.....>
Foam::Time prevTime
(
Foam::Time::controlDictName,
args.rootPath(),
args.caseName()
);
Foam::Time runTime
(
Foam::Time::controlDictName,
args.rootPath(),
args.caseName()
);
Foam::Time nextTime
(
Foam::Time::controlDictName,
args.rootPath(),
args.caseName()
);

Foam::instantList timeDirs = Foam::timeSelector::select0(runTime, args);

// This is a global counter that tell us how many time steps are currently loaded into memory
int countTimeSnaps = 0;
int prevTimeI,curTimeI,nextTimeI;
// Ffowcs Williams Hawkins Analogy
if(analogyType == "FfowcsWilliamsHawkings"){
forAll(timeDirs, timeI)
{
if( countTimeSnaps == 0 ) // First time
prevTimeI = timeI;
if( countTimeSnaps == 1 ) // Current time
curTimeI = timeI;
if( countTimeSnaps > 1 ) {
// Time step setup
nextTimeI = timeI;
prevTime.setTime(timeDirs[prevTimeI], prevTimeI);
runTime.setTime(timeDirs[curTimeI], curTimeI);
nextTime.setTime(timeDirs[nextTimeI], nextTimeI);
mesh.readUpdate();
prevTimeI = curTimeI;
curTimeI = nextTimeI;
Foam::Info << "********************************************\ n";
Foam::Info << "Previous Time : " << prevTime.timeName() << "\n";
Foam::Info << "Current Time : " << runTime.timeName() << "\n";
Foam::Info << "Next Time : " << nextTime.timeName() << "\n";
Foam::Info << "********************************************\ n";
# include "updateFWHSource.H"
}
++countTimeSnaps;
}
}


Cheers,

Mohanamuraly


All times are GMT -4. The time now is 17:37.