CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

value of previous time step

Register Blogs Community New Posts Updated Threads Search

Like Tree17Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 30, 2011, 05:21
Unhappy value of previous time step
  #1
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Hi
In OpenFoam is it possible to know the old time step value of any variable.
I need to store a vectorField and use it in the next time step.
I had a look in the forum and found the commands:

fieldName.storeOldTime(); // to store an old time field
fieldName.oldTime(); // to recover it

I tried to compile these and got the error:

#
error: 'class Foam::vectorField' has no member named 'storeOldTime'
error: 'class Foam::vectorField' has no member named 'storeOldTime'
#

My question is, how to handle with those?

Luca
hua1015 likes this.
lulo is offline   Reply With Quote

Old   September 30, 2011, 07:42
Default
  #2
Senior Member
 
Nima Samkhaniani
Join Date: Sep 2009
Location: Tehran, Iran
Posts: 1,267
Blog Entries: 1
Rep Power: 25
nimasam is on a distinguished road
did u try ?

fieldName.old();
nimasam is offline   Reply With Quote

Old   September 30, 2011, 10:07
Default
  #3
Senior Member
 
Ben K
Join Date: Feb 2010
Location: Ottawa, Canada
Posts: 140
Rep Power: 19
benk is on a distinguished road
Quote:
Originally Posted by lulo View Post
Hi
In OpenFoam is it possible to know the old time step value of any variable.
I need to store a vectorField and use it in the next time step.

Take a look at this post: http://www.cfd-online.com/Forums/ope...tml#post298878

If I remember correctly, the times are stored in an array and should be accessible through:
Code:
runTime.times()[indexNumber].value()
benk is offline   Reply With Quote

Old   September 30, 2011, 10:14
Default
  #4
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
I still get the same kind of error.
I probably use a wrong sintax.
If I wanted to store the vectorField a (for instance) for then using it in the next time step calculation, what should I write?

I tried these and all gave errors:

#
a.old();
a.oldTime();
a.StoreOldTime();
#
lulo is offline   Reply With Quote

Old   September 30, 2011, 10:18
Default
  #5
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Quote:
Originally Posted by benk View Post
Take a look at this post: http://www.cfd-online.com/Forums/ope...tml#post298878

If I remember correctly, the times are stored in an array and should be accessible through:
Code:
runTime.times()[indexNumber].value()
This will give you the fields of the previous output time, that is usually different from the previous time step.
mm.abdollahzadeh likes this.
Bernhard is offline   Reply With Quote

Old   September 30, 2011, 10:36
Default
  #6
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
Use U.oldTime(). You don't need to tell it to store the old time... it figures that out on its own based on how many .oldTime() requests the GeometricField gets in a timestep. You can ask it how many old times it is storing: .nOldTimes() (I think). The first timestep, even if it is resuming in the middle of an old run, all the .oldTime() are the same.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   September 30, 2011, 11:22
Default
  #7
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Thanks to all for replying

I tried U.oldTime() and it works but when I apply it to my vectorField 'myField' it gives:
error: 'class Foam::vectorField' has no member named 'oldTime()'

I am solving a system of equations by iteration and I want to give the current solution as first guess for the next time step.
the_ichthyologist likes this.
lulo is offline   Reply With Quote

Old   September 30, 2011, 11:29
Default
  #8
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
Oh, it's a vectorField, not a volVectorField? Yeah, vectorFields are just vectorLists with math. There's no fancy oldTime archiving. In that case, just create a vectorField like a temp variable.

Code:
// Initialize
vectorField oldVectors(myVectors.size());
// Save old vectors:
oldVectors = myVectors;
// etc..
You only need to create a "oldVectors" object if myVectors doesn't still have the previous iteration in it when starting the first iteration of the new timestep. When I put adaptive timestep control into mine, that's when I had to start keep old versions.
mm.abdollahzadeh likes this.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   October 3, 2011, 05:19
Default
  #9
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Thanks for the help

I have tried the suggested:
Code:
// Initialize
vectorField oldVectors(myVectors.size());
// Save old vectors:
oldVectors = myVectors;
// etc..
I had no success.
I changed my vectorField to a volVectorField so that I could use fieldName.oldTime().
When I compile I do not know how to initialize my variable:

Code:
...
xn.oldTime();

for (int mm=0; mm < counter; mm++)    // iteration process
            {
            xn = ...     // in this loop the volVectorField xn is created by an iterative process
            }

xn.storeOldTime();
...
This gives me the error:
error: 'xn' was not declared in this scope

I want to use the results of the iteration as first guess for the next time step.
lulo is offline   Reply With Quote

Old   October 4, 2011, 09:12
Default
  #10
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
> Had no success.
What was the error? Was it a compile error, or did it not hold the data. This is a simple vector field copy we're doing - it shouldn't be too hard.

The constructors for volVectorField are listed in GeometricField.H.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   October 5, 2011, 05:49
Default
  #11
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Hi.
The solver compiles whitout problems.
What I can see is that every time step the volVectorField with the initial guesses is not updated.

Code:
volVectorField xn      //initialize the volVectorField with the initial guesses being 0,0,0
(
IOobject
(
"xn",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
mesh,
dimensionedVector
(
"xn",
dimensionSet(0,0,0,0,0,0,0),
vector::zero
)
);

xn.oldTime();       // this should overwrite the xn with the results obtained in the previous step 
                          // I THINK THIS IS NOT WORKING!!

for (int mm=0; mm < counter; mm++)    // iteration process
            {
            xn = ...     // in this loop the volVectorField xn is created by an iterative process
            }

xn.storeOldTime();           // This should store the xn results to be used as initial guesses 
                                      // for the next time step
...
I am sure I am doing something wrong using this OldTime() and storeOldTime() function.
I just do not know how to use them properly.

Thanks in advance
mm.abdollahzadeh likes this.
lulo is offline   Reply With Quote

Old   October 5, 2011, 11:57
Default
  #12
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
Yes, oldTime works much simpler than you think. It is all totally automatic in the background. You never need to call storeOldTime()... and the xn.oldTime() immediately gives you the value from the previous timestep. runTime is clever enough to count how many .oldTime()'s you use, and automatically stores that many in its database.

I think you are probably using a local object. I was always confused as to why xn wouldn't *already* have the previous timestep's value at the start of the next timestep. If you are using a local object, it is erased and reconstructed at every timestep. In this case, oldTime() won't work, and neither would a simpler vectorField copy we talked about either. Where are you constructing your xn object? Will the program encounter the constructor once and only once throughout multiple timestep iterations? If not, move the constructor above your while(runTime.loop())... possibly even into createFields.H. If this is the problem, that's probably why it didn't work when you were using a standard vectorField. I'd recommend returning to the vectorField framework if possible.
__________________
~~~
Follow me on twitter @DavidGaden

Last edited by marupio; October 5, 2011 at 11:58. Reason: "If so" changed to "If not" oops
marupio is offline   Reply With Quote

Old   October 6, 2011, 18:03
Default
  #13
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Hi

Thanks a lot!!
I moved the volVectorField constructor in createFields.H and now the value at the next time step is overwritten correctly.

Why do you suggest to return to the vectorField? I could do that but it will take me time due to my poor programming knowledge.

How should the constructor for the vectorField in createFields.H?

Code:
vectorField xn = vector::zero.boundaryField()[bottom_patch];
I would like to create the vectorField 'xn' of the initial guesses being (0.05,0.001,0.001) and the number of vectors should be the same as the number of cells of my 'bottom_patch'.

I tried also with the volVectorField but without success:
Code:
volVectorField xn     
(
IOobject
(
"xn",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
mesh,
dimensionedVector
(
"xn",
dimensionSet(0,0,0,0,0,0,0),
//vector::zero   // THIS WORKS BUT I DON'T WANT ZERO VECTORS
uniform (0.05,0.001,0.001) // THIS IS NOT COMPILING
)
);
Thanks

Luca
lulo is offline   Reply With Quote

Old   October 6, 2011, 19:23
Default
  #14
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
I don't think you even need to use the .oldTime because the previous values will still be in the vectorField at the start of the next timestep. I suggest using a vectorField if you are not doing any math on the boundary because you are carrying a lot of unnecessary extra memory in the boundary field, as well as performing unnecessary operations there. It's your perogative.

As for the compile error, that's probably because it doesn't know what a uniform is. Try replacing the word 'uniform' with the word 'vector'.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   October 7, 2011, 12:39
Default
  #15
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
Thanks

I undersatand the memory issue. So I am trying to use a vectorField. I want to define the vectorField in createFields.H. I tried different ways:

Code:
                vectorField xn = vector::zero.boundaryField()[bottom_patch];

		vectorField xn
		(
		IOobject
			(
			"xn",
			runTime.timeName(),
			mesh,
			IOobject::NO_READ,
			IOobject::AUTO_WRITE
			),
			mesh.boundaryMesh().findPatchID(bottom_patch),
		);

		vectorField xn
		(
		IOobject
			(
			"xn",
			runTime.timeName(),
			mesh,
			IOobject::NO_READ,
			IOobject::AUTO_WRITE
			),
			mesh.boundaryMesh()[bottom_patch],
		);
I simply want to iniitialize a vectorField with the number of vectors being equal to the number of cells of my ´bottom_patch´.

Any idea??
lulo is offline   Reply With Quote

Old   October 8, 2011, 12:25
Default
  #16
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
You could use this constructor:

Code:
vectorField nameOfVectorField(sizeOfVectorField);
 
// or
 
vectorField nameOfVectorField(sizeOfVectorField, initialValueForAllCells);
To know the size of the bottom patch, you could use: U.boundaryField()[patchNumber].size(). The tricky part is patchNumber, as you need to figure out what index number OpenFOAM assigned to the patch you want - "bottom_patch". Not sure how to do this. I think you need to loop through all the patches and compare .name() with "boundary_patch". You only need to initialize the values (second constructor above) if you are not assigning a value to them first off.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   October 14, 2011, 12:49
Default
  #17
Member
 
luca
Join Date: Feb 2011
Posts: 34
Rep Power: 15
lulo is on a distinguished road
I have been trying to use the vectorField without success. So I will keep my volVectorField.

Many thanks for your help!!
lulo is offline   Reply With Quote

Old   February 7, 2013, 20:16
Default
  #18
Member
 
Martin
Join Date: Dec 2011
Location: Latvia
Posts: 54
Rep Power: 14
latvietis is on a distinguished road
Greetings!

Sorry for bringing up such an old thread but the title is rather corresponding to my issue.

I have a dependent variable like g=g(a). But my solution has really bad convergence and to improve it there is an idea to use weighted combination of values from different time steps. It should look something like this where k is iteration step.

g(k+1)=g(a(k)+a(k-1))

How I can get these values in OF?

Is something like this correct? I mean is a.oldTime value 'more previous' than simply value of a?

Code:
g=g(a+a.oldTime())
latvietis is offline   Reply With Quote

Old   May 21, 2013, 13:08
Default
  #19
New Member
 
Pietro Danilo Tomaselli
Join Date: Oct 2012
Location: Lyngby, DTU
Posts: 9
Rep Power: 14
kobelak is on a distinguished road
Hi all!

I have to modify a solver and I'd like to do some calculations with values of U of all the previous time steps, not only the time step immediately earlier the current one. For instance, which is the way "to call" the value of U at (i-5) time step, if i is the current one (in run time)?

Any hints to do this?

Thanks in advance

Danilo
kobelak is offline   Reply With Quote

Old   November 5, 2013, 05:47
Default
  #20
Member
 
Thomas Vossel
Join Date: Aug 2013
Location: Germany
Posts: 45
Rep Power: 13
ThomasV is on a distinguished road
Hi!

I tried the .oldTime() method but it didn't work for me - I always got the very same values for both my new volScalarField itself as well as for the fieldname.oldTime() expression...

I once saw a .prevIter() method in one of OpenFoam's solvers so I tried it that way. The compiler then asked me to do a field.storePrevIter() command too and this actually worked. So maybe someone should clarify if the oldTime method got deprecated in the meantime or not. What works is this:

Code:
fieldname.storePrevIter();

<< DO THE CALCULATIONS / UPDATES ON YOUR FIELD AFTERWARDS >>

fieldname.prevIter();
ThomasV is offline   Reply With Quote

Reply

Tags
field, store, time step


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading forces from previous time step within solver SD@TUB OpenFOAM Programming & Development 5 April 24, 2023 12:51
Time step size and max iterations per time step pUl| FLUENT 31 October 23, 2020 23:50
Superlinear speedup in OpenFOAM 13 msrinath80 OpenFOAM Running, Solving & CFD 18 March 3, 2015 06:36
UDF: Previous time step macros NOT working jpapg FLUENT 0 April 30, 2011 22:35
Convergence moving mesh lr103476 OpenFOAM Running, Solving & CFD 30 November 19, 2007 15:09


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