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

How to get the value of latestTime?

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes
  • 1 Post By reza2031
  • 2 Post By randolph
  • 1 Post By Tobi
  • 1 Post By randolph

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 21, 2018, 13:09
Default How to get the value of latestTime?
  #1
New Member
 
Reza Nouri
Join Date: Oct 2012
Location: Tennessee
Posts: 26
Rep Power: 14
reza2031 is on a distinguished road
Send a message via Skype™ to reza2031
Hello,

Somehow, I need to use the value of latestTime in my code.

More explanation: I have set the stratTime to latestTime in controlDict.
In controlDict:
Code:
startTime latestTime;
I know that OpenFOAM calculates the latest written time in case directory automatically. Let's say the latest time folder is 10, how can I get the value 10 in the code?


Bests,
Reza
reza2031 is offline   Reply With Quote

Old   September 24, 2018, 05:49
Default
  #2
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi,

so in order to get the fields from the latest time step (as real time or incremental), you have to store them such as:

Code:
T.storeOldTime();

// To get the old time

T.oldTime()
However, this works if you are within a run. If you start your calculation at t=10s, you do not have access to the last time step (lets say 9.8s - if dt = 0.2). There is no functionality in FOAM because it does not make sense. E.g. generally, dt is not the same as writeTime.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   September 24, 2018, 10:40
Default
  #3
New Member
 
Reza Nouri
Join Date: Oct 2012
Location: Tennessee
Posts: 26
Rep Power: 14
reza2031 is on a distinguished road
Send a message via Skype™ to reza2031
Thank you Tobi for the reply but it is not I was looking for. I am going to explain more. In my solver, I have these lines:
Code:
scalar eTime( readScalar(runTime.controlDict().lookup("endTime"))/nPoints );
scalar staTime( readScalar(runTime.controlDict().lookup("startTime")));
When the start time is 0 or when it's a scalar, everything is just fine. But, I am looking for a way to deal with "latestTime" in controlDict. OpenFOAM, somehow calculates the latest found directory in the case folder.
I checked Time.C and found this part:
Code:
{                               
    controlDict_.lookup("startTime") >> startTime_;                           
}                           
else                           
{                               
    // Search directory for valid time directories                               
    instantList timeDirs = findTimes(path(), constant());                        
                             
    if (startFrom == "firstTime")                               
    {                                   
        if (timeDirs.size())                                   
        {                                       
             if (timeDirs[0].name() == constant() && timeDirs.size() >= 2)
             {                                           
                   startTime_ = timeDirs[1].value();                                       
             }                                       
             else                                       
             {                                           
                   startTime_ = timeDirs[0].value();                                       
             }                                   
         }                               
    }                               
    else if (startFrom == "latestTime")                               
    {                                   
         if (timeDirs.size())                                   
         {                                       
               startTime_ = timeDirs.last().value();                                   
         }                               
    }
 }
OpenFOAM must save this "latest time" as a scalar. So, there should be a way to read the latest time as a scalar.




Reza
Michael@UW likes this.
reza2031 is offline   Reply With Quote

Old   September 24, 2018, 12:04
Default
  #4
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
You want to read the time value? I still did not get your point.
Getting the time value of the "latest Time" is explained in the code you wrote:


Code:
latestTime = timeDirs.last().value()
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   September 24, 2018, 12:55
Default
  #5
New Member
 
Reza Nouri
Join Date: Oct 2012
Location: Tennessee
Posts: 26
Rep Power: 14
reza2031 is on a distinguished road
Send a message via Skype™ to reza2031
That piece of code is from Time.C (source code for time)
https://github.com/OpenFOAM/OpenFOAM...db/Time/Time.C

I can write the below line in my code (It's a solver based on pisoFoam):
Code:
scalar latestTime = timeDirs.last().value();
I will get the error of declaration :
Code:
error: ‘timeDirs’ was not declared in this scope
Even if I include Time.H, it the same error.
So, how can I use this part of the Time.C and use it in my code?
I want to continue a case from a previous state. The problem is that I use the startTime as a scalar in my code to do some some calculation.So I need to change the startTime every time I want to resume a case from a previous time.

I am looking to do it automatically, writing latestTime in controlDict but my code should somehow reads the latesTime as a scalar, otherwise I get this error:
Code:
--> FOAM FATAL IO ERROR: 
wrong token type - expected Scalar, found on line 22 the word 'LatestTime'
Which is obvious as I have this line in my code:
Code:
scalar staTime( readScalar(runTime.controlDict().lookup("startTime")));
Any idea?


Reza
reza2031 is offline   Reply With Quote

Old   September 24, 2018, 13:01
Default
  #6
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Got it now. Yes it should be possible. The problem you have is related to the fact, that you do not have the object timeDir in your code. You should have access to that object via runTime.whateverFunction(); to get the function, doxygen is the proper way to look at.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   January 31, 2019, 15:10
Default
  #7
Senior Member
 
Reviewer #2
Join Date: Jul 2015
Location: Knoxville, TN
Posts: 141
Rep Power: 11
randolph is on a distinguished road
db().time().startTime().value()
randolph is offline   Reply With Quote

Old   April 15, 2020, 12:24
Default question
  #8
New Member
 
Ningyi Li
Join Date: Jun 2018
Location: Germany
Posts: 6
Rep Power: 8
lny200912 is on a distinguished road
Hi, Reza

Did you solve this problem? I also want to know how to get the value of latestTime in the controlDict as one scalar in the main programming?


Best regards,
Ningyi
lny200912 is offline   Reply With Quote

Old   May 5, 2020, 00:16
Default
  #9
Senior Member
 
Join Date: Jan 2019
Posts: 125
Blog Entries: 1
Rep Power: 0
Michael@UW is on a distinguished road
Quote:
Originally Posted by reza2031 View Post
That piece of code is from Time.C (source code for time)
https://github.com/OpenFOAM/OpenFOAM...db/Time/Time.C

I can write the below line in my code (It's a solver based on pisoFoam):
Code:
scalar latestTime = timeDirs.last().value();
I will get the error of declaration :
Code:
error: ‘timeDirs’ was not declared in this scope
Even if I include Time.H, it the same error.
So, how can I use this part of the Time.C and use it in my code?
I want to continue a case from a previous state. The problem is that I use the startTime as a scalar in my code to do some some calculation.So I need to change the startTime every time I want to resume a case from a previous time.

I am looking to do it automatically, writing latestTime in controlDict but my code should somehow reads the latesTime as a scalar, otherwise I get this error:
Code:
--> FOAM FATAL IO ERROR: 
wrong token type - expected Scalar, found on line 22 the word 'LatestTime'
Which is obvious as I have this line in my code:
Code:
scalar staTime( readScalar(runTime.controlDict().lookup("startTime")));
Any idea?


Reza


Can you just add the line to access timeDirs?

instantList timeDirs = findTimes(path(), constant());

scalar latestTime = timeDirs.last().value();

latestTime is just a word for the if else control.
Michael@UW is offline   Reply With Quote

Old   May 10, 2020, 13:21
Default
  #10
Senior Member
 
Franco
Join Date: Nov 2019
Location: Compiègne, France
Posts: 129
Rep Power: 7
otaolafr is on a distinguished road
hello, not sure if the correct place to ask, I am looking to copy the last folder time from my case to use it as the 0 folder in the next (it is a two-step case)where the first I calculate the steady-state velocity and pressure fields and in the second doing a scalarTransport in transient so I can measure the RTD (residence time distribution) of steady-state flow in micromixer.
so I want to use simpleFoam to simulate the flow and when the residuals are low enought (from the residualControl in fvSoluion) copy the "lastTime folder" and renamed to a second case folder where I will do a scalarTransport.
but as the lastTime is controlled by the residualControl I not going to be able to know from the beginning of the simulation when it is going to be converged.

best regards,
franco
otaolafr is offline   Reply With Quote

Old   May 10, 2020, 13:28
Default
  #11
Senior Member
 
Reviewer #2
Join Date: Jul 2015
Location: Knoxville, TN
Posts: 141
Rep Power: 11
randolph is on a distinguished road
Franco,

I think you have two solutions:

1. just simply use the bash command with some regular expression to copy the last time step folder to your new case.

2. use the mapFields with sourceTime option to map the solution from the flow solution to your new case.

Thanks,
Rdf
randolph is offline   Reply With Quote

Old   May 10, 2020, 13:34
Default
  #12
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi all, I guess the first option is sufficient as the mesh should be the same.

Tobi
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   May 10, 2020, 14:02
Default
  #13
Senior Member
 
Franco
Join Date: Nov 2019
Location: Compiègne, France
Posts: 129
Rep Power: 7
otaolafr is on a distinguished road
Quote:
Originally Posted by randolph View Post
Franco,

I think you have two solutions:

1. just simply use the bash command with some regular expression to copy the last time step folder to your new case.

2. use the mapFields with sourceTime option to map the solution from the flow solution to your new case.

Thanks,
Rdf
hi randolph, and tobi
thanks for the answers, yes as tobi said, i think the first option is better as it is going to be exactly the same mesh, but my issue is how to setup a command to do it (as i wanted to integrated in an Allrun file) sorry as linux is new to me i am learning at the same time:
i wanted to have this structure of folder:
Simulation
------AllrunSimulation
+----sim1
---------+----0
---------+----lastTime
---------+----constant
---------+----system
----------Allrunsim1
----------Allclean
+----sim2
---------+----constant
---------+----system
----------Allrunsim2
----------Allclean
and for my AllrunSimulation file have a code like this:
Quote:
cd ./sim1
./Allclean
./Allrunsim1
cd ..
./sim2/Allclean
cp -r ./sim1/lastTime ./sim2/0
./sim2/Allrunsim2
my issue is that i dont know how to "select" the lastTime automatically.. (what i think you mean by the option 1)..
thanks for the help,
franco
otaolafr is offline   Reply With Quote

Old   May 10, 2020, 14:19
Default
  #14
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
You want to start the first guy and after it finish, the second calculation should start automatically?
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   May 10, 2020, 14:23
Default
  #15
Senior Member
 
Franco
Join Date: Nov 2019
Location: Compiègne, France
Posts: 129
Rep Power: 7
otaolafr is on a distinguished road
Quote:
Originally Posted by Tobi View Post
You want to start the first guy and after it finish, the second calculation should start automatically?
is what i wanted to do at least....
run first case,
when finish copy the last folder to the second case (as 0 folder)
and run the second case.

as my idea is to leave it running during the nights, it would be my "ideal" case so it does not need intervention in middle of it.
franco
otaolafr is offline   Reply With Quote

Old   May 10, 2020, 14:41
Default
  #16
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
What about some bash magic?



Code:
ls -1 | sort -n | tail -n 1
Tobi
otaolafr likes this.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   May 10, 2020, 15:29
Default
  #17
Senior Member
 
Franco
Join Date: Nov 2019
Location: Compiègne, France
Posts: 129
Rep Power: 7
otaolafr is on a distinguished road
Quote:
Originally Posted by Tobi View Post
What about some bash magic?



Code:
ls -1 | sort -n | tail -n 1
Tobi
Looool literally magic 😂😂😂 thanks to bother helping newbies like me, 🙂 I will have a look to understand it
otaolafr is offline   Reply With Quote

Old   May 10, 2020, 20:10
Default
  #18
Senior Member
 
Reviewer #2
Join Date: Jul 2015
Location: Knoxville, TN
Posts: 141
Rep Power: 11
randolph is on a distinguished road
Franco,

If you want go with the second solution, you can run this command in your transport case folder

mapFields <origional case with flow solution> -sourceTime latestTime -consistent

The argument latestTime will automatically parse the latest solution in that folder.

Thank,
Rdf
otaolafr likes this.
randolph is offline   Reply With Quote

Old   May 11, 2020, 05:20
Default
  #19
Senior Member
 
Franco
Join Date: Nov 2019
Location: Compiègne, France
Posts: 129
Rep Power: 7
otaolafr is on a distinguished road
Quote:
Originally Posted by randolph View Post
Franco,

If you want go with the second solution, you can run this command in your transport case folder

mapFields <origional case with flow solution> -sourceTime latestTime -consistent

The argument latestTime will automatically parse the latest solution in that folder.

Thank,
Rdf
thanks rdf! in case that i need a thinner mesh for the second case it will be a good solution, thanks a lot man

Quote:
Originally Posted by Tobi View Post
What about some bash magic?



Code:
ls -1 | sort -n | tail -n 1
Tobi
hello tobi, sorry to bother you, but when i do this in the bash it works correctly but when i do it from an allrun file it begings coping...; i think the Ubuntu subsystem? not sure... (but at least a lot of files... and not the correct ones...)
Quote:
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory

lastestTime=ls -1 | sort -n | tail -n 1;
cp -r $lastestTime/. ./testFolder/
directly in the terminal ls -1 | sort -n | tail -n 1 gives the correct answer but not from the allrun test.
any idea? thanks.

hello, i just came back to give an answer to my problem, for anyone that could need it and also so no one waste time in answering it
:credits to https://unix.stackexchange.com/quest...shell-variable

to stock the correct output the syntaxis is
Quote:
lastestTime=$(ls -1 | sort -n | tail -n 1);
also works:
Quote:
lastestTime=$( bash <<EOF
ls -1
sort -n
tail -n 1
EOF
)
and to use it as a folder name for the cp command,
Quote:
cp -r "$lasT" ./testFolder/
best regards!

Last edited by otaolafr; May 13, 2020 at 04:40. Reason: find the answer to the issue i was having
otaolafr is offline   Reply With Quote

Reply


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
How to make sure to continue running from a complete time dir with latestTime ? eric-meng OpenFOAM 4 May 22, 2018 05:52
dirvergence of simulations after resuming pimpleDyMFoam with latestTime rcastilla OpenFOAM Running, Solving & CFD 3 October 9, 2017 10:01
Restarting a case using latestTime in parallel arieljeds OpenFOAM Running, Solving & CFD 2 May 11, 2016 12:30
pimpleDyMFoam won't restart from latestTime xixchubbsxix OpenFOAM Running, Solving & CFD 1 September 30, 2015 06:26
DieselFoam does not restart from latestTime lord_kossity OpenFOAM Bugs 8 February 21, 2009 14:54


All times are GMT -4. The time now is 21:09.