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/)
-   -   CPU Time (https://www.cfd-online.com/Forums/openfoam-programming-development/144474-cpu-time.html)

willzyba November 14, 2014 19:09

CPU Time
 
Ever since we upgraded to OpenFOAM 2.3.x I've struggled to understand how I can get the simulation Time.

I'm trying to make an active spring, with a force component that depends on the model time.

In older versions of OpenFOAM I could use
Code:

runTime.elapsedClockTime()
But this no longer works. I'm just not sure how within one of the sizDoFRigidBodyMotion codes, i.e. linearAxialAngularSpring.C, I can get the sytem time.

Thanks in advance for any help

ngj November 15, 2014 02:37

Hi,

I cannot answer your question, but be careful. If the properties changes based on the CPU time and not the simulated time, then you will no longer have a deterministic model. The results will change per simulation, architechture, discretisation, etc.

Kind regards,

Niels

willzyba November 15, 2014 05:05

Agreed, I do want simulation time, which is what I though elapsedClickTime gave.

But at this point I just want anything, I'm going to start using the system clock soon and that will be terrible.

wyldckat November 15, 2014 05:21

1 Attachment(s)
Greetings to all!

This is one of those questions that have seemly pretty direct answers which is to tell you what exact question should be asked and to whom. The answer is: Ask the code documentation - http://www.openfoam.org/docs/cpp/ - about "Time" and it will tell you all about it, as shown below: :D

http://www.cfd-online.com/Forums/att...1&d=1416046606

There you'll be shown that the following methods might be of interest to you:
Code:

time_t    elapsedClockTime () const
time_t    clockTimeIncrement () const
double    elapsedCpuTime () const
double    cpuTimeIncrement () const
virtual scalar    userTimeToTime (const scalar theta) const
virtual scalar    timeToUserTime (const scalar t) const
scalar    timeOutputValue () const
label    timeIndex () const
scalar    deltaTValue () const
scalar    deltaT0Value () const
dimensionedScalar    deltaT () const
dimensionedScalar    deltaT0 () const
bool    outputTime () const

I'm not giving a direct link to the page for the Time class, because the link will change with each new release of OpenFOAM.

Best regards,
Bruno

willzyba November 15, 2014 05:57

I get the documentation. The issue isn't the list of available function within the Time class, its getting access to the Time class.

i.e. How do I get an instance of class Time (rather than just make a new instance) that is linked to the main application and the times it is using.

Once upon a time this was just 'runTime'. But this is no longer accessible within libraries I'm writing - where I'm simply modifying one of the 6DOF restraints.

wyldckat November 15, 2014 08:22

Hi Will,

:eek: Well, this first statement of yours was pretty misleading:
Quote:

Originally Posted by willzyba (Post 519243)
Ever since we upgraded to OpenFOAM 2.3.x I've struggled to understand how I can get the simulation Time.

because it lead me to think that you had this feature working in an older version of OpenFOAM.

The other problem is that you didn't give us the exact context, or better yet, show us what you're seeing. And the thread title is also misleading ;)

OK, so the problem is that you're trying to get access to the time controls from within a class for dynamic meshing. This would conceptually be similar to having this feature in a boundary condition, because the way the accesses to the mesh and fields are done in a similar way.
This leads me to this thread: http://www.cfd-online.com/Forums/ope...tml#post513962 - specifically post #7, where I mention the class "uniformFixedValue", which is the class/BC that is able to handle the table lookup dependant on time. The specific code that does this is here: https://github.com/OpenFOAM/OpenFOAM...chField.C#L117
Quote:

Code:

template<class Type>
uniformFixedValueFvPatchField<Type>::uniformFixedValueFvPatchField
(
    const uniformFixedValueFvPatchField<Type>& ptf,
    const DimensionedField<Type, volMesh>& iF
)
:
    fixedValueFvPatchField<Type>(ptf, iF),
    uniformValue_
    (
        ptf.uniformValue_.valid()
      ? ptf.uniformValue_().clone().ptr()
      : NULL
    )
{
    // For safety re-evaluate
    const scalar t = this->db().time().timeOutputValue();

    if (ptf.uniformValue_.valid())
    {
        fvPatchField<Type>::operator==(uniformValue_->value(t));
    }
}


See anything familiar that you might be looking for? :D

Best regards,
Bruno

willzyba November 15, 2014 12:10

Hi Bruno,

My first posting was correct. The code I showed used to work in OpenFoam 2.2.2, where we used it in a custom class based directly on sixDoFRigidBodyMotion/restraint/linearSpring class.

Using
Code:

this->db().time().timeOutputValue()
also doesn't work. We get the compiler error:
Code:

sixDoFRigidBodyMotion/restraints/CCactiveSpring/CCactiveSpring.C:91:19: error: ‘const class Foam::sixDoFRigidBodyMotionRestraints::CCactiveSpring’ has no member named ‘db’
I'm not surprised, since these 6DOF/restraint classes don't have many base classes, and certainly nothing that links back to the Time. I suspect what happened before was there was a global variable define which was accessible across the all the code. I'm still trying to get my head around how OpenFOAM is structure and the changes that took place from 2.2.2 to 2.3.x and I guess that is the fundamental problem.

Thanks for your help though.

wyldckat November 15, 2014 13:33

Hi Will,

Sorry, I was in too much of a hurry and didn't check the OpenFOAM source code myself. But you didn't mention which was the previous version OpenFOAM you had used ;)

I've taken a look at the source code for OpenFOAM 2.2.x at Github and I believe that you had more modifications in your older code than you remember using, because there is no occurrence in the whole source code of something like this:
Code:

extern Foam::Time runTime;
at least in OpenFOAM 2.2.x.

But as you mentioned, one solution is to rely on a globally shared value between the solver and your classes. This requires you to also create a modified version of your solver, which will have to depend on the new library you're creating, so that the solver can access the global variable in your class/library that has a global pointer for "runTime".

2nd solution would be to access a global instance of the object registry... which would then give you access to "db()" or "thisDb()"... but I haven't found it yet.

3rd solution is to hack the class "sixDoFRigidBodyMotion" to calculate the total time that has passed since it started, and register said time in the class "sixDoFRigidBodyMotionState". Then it would be possible to use the variable "motion" within the "restrain" method to access the current total time for this particular state.

4th solution is to actually reverse engineer the time between two states, based in the position of each state for the previous restraint and the current restraint. The "motion.state()" gives a complete snapshot... er, wait, it's only a partial snapshot... never mind.


5th solution is to overhaul the whole library "sixDoFRigidBodyMotion" to support this requirement you have. It's probably the best and correct way to fix this.

You might also want to open a feature request for this on the OpenFOAM bug tracker, because the current solid motion is only dealing with relative time steps and doesn't even allow to send these time steps to the restrain mechanism.

Best regards,
Bruno

kmou February 18, 2016 12:54

Quote:

Originally Posted by wyldckat (Post 519334)
Hi Will,


I've taken a look at the source code for OpenFOAM 2.2.x at Github and I believe that you had more modifications in your older code than you remember using, because there is no occurrence in the whole source code of something like this:
Code:

extern Foam::Time runTime;
at least in OpenFOAM 2.2.x.

But as you mentioned, one solution is to rely on a globally shared value between the solver and your classes. This requires you to also create a modified version of your solver, which will have to depend on the new library you're creating, so that the solver can access the global variable in your class/library that has a global pointer for "runTime".



Hi Bruno,

I have been trying exactly this working: making the runTime variable global within my solver directory.
It is a modified interFoam solver.
runTime is set inside interFoam.C
and used as we wish, runTime.timeName(), etc...

Within my solver directory, I created a sub-directory which is a personalised boundary condition.
It contains two files: MyBoundaryCondition.C
and myBoundaryCondition.H


I would like to be able to store variables created in the .C file within the time directories of my simulation.
To do so, I would need runTime to be global, or "extern"

but I don't seem to manage to understand where to use "extern Foam::Time runTime;"
or if this is actually a wrong code line to use...

Thank you for your support :)

wyldckat March 28, 2016 13:10

Greetings Camille,

Sorry for the late reply, but only now did I finally manage to look into this.

And I recently went for a debugging session on a feature related to this: http://www.openfoam.org/mantisbt/view.php?id=2030

What happens is that whenever we have access to a field object or the main "mesh" object, we are automatically able to access the "runTime" object indirectly. Funnily enough, I went searching for this very same topic and I ended up on my previous post above :)

In specific, since you have a boundary condition, all you need to do is use the same exact strategy that a lot of other boundary conditions already use. If you run the following command in the terminal/console window:
Code:

grep -r "db()" $FOAM_SRC/finiteVolume/fields/fvPatchFields
you will find a lot of these examples.
For example, the following code within your boundary condition:
Code:

this->db().time()
is the indirect access to the "runTime" object.

Best regards,
Bruno


All times are GMT -4. The time now is 18:28.