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

How to get the current time within the controlDict?

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes
  • 1 Post By alexeym
  • 1 Post By duan
  • 3 Post By Tobi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 4, 2014, 13:53
Question How to get the current time within the controlDict?
  #1
New Member
 
duan
Join Date: Apr 2014
Posts: 6
Rep Power: 12
duan is on a distinguished road
Hi,

I am trying to set different writeTimeInterval rather than a constant one during the runTime.

for example:
if (currentRunTime<t0)
writeTimeInterval=value0;
else
writeTimeInterval=value1;

Now I have problems on how to get current time within the controlDict.

Here is my code:

Code:
writeInterval   #codeStream
{
    
    code
    #{
        /*// the sample works    
          scalar start = readScalar(dict.lookup("startTime"));
          scalar end = readScalar(dict.lookup("endTime"));
          label nDumps = 5;
          os << ((end - start)/nDumps);*/
        //==================================================
        //how to get the current time within controlDic???
          
          //Tried but Not work
          //scalar t=this->db().time().value(); 
          //error: invalid use of ‘this’ in non-member function
         
          //scalar t = runTime().value() ; 
          //error: ‘runTime’ was not declared in this scope
          //scalar t = timeDirs.last().value(); 
          //error: ‘timeDirs’ was not declared in this scope
          //scalar t = time().value(); 
          //error: too few arguments to function ‘time_t time(time_t*)
          label t0 = 0.4;
          if (t <= t0) {
                os << 0.05;
          }
          else {
                os << 0.02;
          } 
    #};
};
I also have found some post related:
http://www.cfd-online.com/Forums/ope...ntroldict.html
http://www.openfoam.org/version2.0.0...me-control.php
http://www.cfd-online.com/Forums/ope...me-scalar.html



Thanks in advance.
duan is offline   Reply With Quote

Old   November 4, 2014, 14:36
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,932
Rep Power: 38
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

you'd like just to update writeInterval, depending on current time, you can create controlDict_1 with writeInterval_1, controlDict_2 with writeInterval_2 and use timeActivatedFileUpdate like below:

Code:
functions
{
    fileUpdate1
    {
        type            timeActivatedFileUpdate;
        functionObjectLibs ("libutilityFunctionObjects.so");
        outputControl   timeStep;
        outputInterval  1;
        fileToUpdate    "$FOAM_CASE/system/controlDict";
        timeVsFile
        (
            (-1    "$FOAM_CASE/system/controlDict_1")
            (10    "$FOAM_CASE/system/controlDict_2")
        );
    }
}
I.e. take your controlDict, put code snippet there, copy it to coltrolDict_1 and controlDict_2, modify writeInterval settings in controlDict_* and that's it.
lth likes this.
alexeym is offline   Reply With Quote

Old   November 6, 2014, 07:01
Default
  #3
New Member
 
duan
Join Date: Apr 2014
Posts: 6
Rep Power: 12
duan is on a distinguished road
Hi, @alexeym

Thanks for your quick reply.

It works.
duan is offline   Reply With Quote

Old   November 6, 2014, 07:12
Default
  #4
New Member
 
duan
Join Date: Apr 2014
Posts: 6
Rep Power: 12
duan is on a distinguished road
For alternative,

I also wrote a script, to run the case in 3 steps:
0=>t1 ,with write time interval WI1
t1=>t2 ,with write time interval WI2
t2=>t3 ,with write time interval WI3

It's not a very good solution, but works.
If someone is interested, see the attached file.
Attached Files
File Type: txt runScript.txt (5.0 KB, 131 views)
mohseni_2012 likes this.
duan is offline   Reply With Quote

Old   July 25, 2015, 03:47
Default scalar t = runTime().value()
  #5
Member
 
SM
Join Date: Dec 2010
Posts: 97
Rep Power: 15
canopus is on a distinguished road
Does any one know how to get
Code:
scalar t = runTime().value
in controldict to modify the writeinterval using
Code:
writeInterval #codeStream
?

I am getting the same error as reported here

Code:
system/controlDict.#codeStream:40:13: error: ‘runTime’ was not declared in this scope
canopus is offline   Reply With Quote

Old   July 25, 2015, 11:43
Default
  #6
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
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
It's more like:

Code:
scalar t = U.runTime().value();
You only can call runTime().value() with an object. Or maybe with the Namespace::
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   March 22, 2016, 09:25
Default
  #7
New Member
 
Carles
Join Date: Aug 2015
Posts: 4
Rep Power: 10
CarlesCQL is on a distinguished road
Hi Tobi,

I tried

Code:
 scalar t = U.runTime().value();
and I get:

Code:
 error: ‘U’ was not declared in this scope
I searched for this topic a lot in this forum but I didn't found any solutions.

Can you elaborate a little more about that and the namespace option?

Thanks!
CarlesCQL is offline   Reply With Quote

Old   March 21, 2021, 12:51
Default
  #8
Member
 
Join Date: Mar 2009
Posts: 90
Rep Power: 17
aerogt3 is on a distinguished road
Quote:
Originally Posted by CarlesCQL View Post
Hi Tobi,

I tried

Code:
 scalar t = U.runTime().value();
and I get:

Code:
 error: ‘U’ was not declared in this scope
I searched for this topic a lot in this forum but I didn't found any solutions.

Can you elaborate a little more about that and the namespace option?

Thanks!
Did you ever figure this out?
aerogt3 is offline   Reply With Quote

Old   March 22, 2021, 05:53
Default
  #9
Member
 
Andrea Di Ronco
Join Date: Nov 2016
Location: Milano, Italy
Posts: 57
Rep Power: 9
Diro7 is on a distinguished road
Quote:
Originally Posted by aerogt3 View Post
Did you ever figure this out?
I didn't try myself, but you should be able to access the mesh database, and then the simulation time with something like
Code:
db().time().value()
where db() is a call from a objectRegistry object.

In the codeStream documentation (https://cpp.openfoam.org/v6/classFoa...m.html#details) there should be an example on how to access the database from within a codeStream instance:
Code:
someEntry  #codeStream
{
    code
    #{
        const IOdictionary& d = static_cast<const IOdictionary&>(dict);
        const fvMesh& mesh = refCast<const fvMesh>(d.db());

        ...
    #};
};
I guess you don't need to explicitly use namespaces or to link other libraries, but still I didn't try. Just try to put together the pieces and see if it works

Andrea
Diro7 is offline   Reply With Quote

Old   March 23, 2021, 04:09
Default
  #10
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
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
If you call db() you need an object. It is the same as with U.time().value(). U is an object of e.g., volVectorField.

It is not possible to just call db().time().value(). Probably this works:


Code:
this->db().time().value();

Depending if »this« is a pointer or a reference you have to choose the point ».« operator or the other one »->«.
Diro7, alainislas and allanZHONG like this.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   March 23, 2021, 04:29
Default
  #11
Member
 
Andrea Di Ronco
Join Date: Nov 2016
Location: Milano, Italy
Posts: 57
Rep Power: 9
Diro7 is on a distinguished road
Quote:
Originally Posted by Tobi View Post
If you call db() you need an object. It is the same as with U.time().value(). U is an object of e.g., volVectorField.

It is not possible to just call db().time().value(). Probably this works:


Code:
this->db().time().value();
Depending if »this« is a pointer or a reference you have to choose the point ».« operator or the other one »->«.
You are definitely right, my answer was a little ambiguous.
If you already have some object U like a volScalarField, then it is much easier to just call U.db().

If not, this->db() may do the job in general but since I wasn't sure about what is returned by this in a codeStream context, my suggestion was to look at the documentation and see how to retrieve the simulation database from the current dictionary.

Andrea
Diro7 is offline   Reply With Quote

Old   March 23, 2021, 04:31
Default
  #12
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
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
Does not fit 100 % but here is something similar.


https://www.youtube.com/watch?v=cvWaXBnEz1U&t=1095s
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   March 27, 2024, 05:29
Default
  #13
New Member
 
Join Date: Mar 2024
Posts: 1
Rep Power: 0
sh.yu is on a distinguished road
Hi,

I meet the same issue here.

Does anyone know how to get the current time in the controlDict via codeStream?
sh.yu is offline   Reply With Quote

Reply

Tags
controldict, current run time, write control


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
Current density visualisation (PEM fuel cell add-on module) pchoopanya FLUENT 10 August 21, 2023 14:33
Multiple floating objects CKH OpenFOAM Running, Solving & CFD 14 February 20, 2019 09:08
Superlinear speedup in OpenFOAM 13 msrinath80 OpenFOAM Running, Solving & CFD 18 March 3, 2015 05:36
pisoFoam with k-epsilon turb blows up - Some questions Heroic OpenFOAM Running, Solving & CFD 26 December 17, 2012 03:34
Could anybody help me see this error and give help liugx212 OpenFOAM Running, Solving & CFD 3 January 4, 2006 18:07


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