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

Accessing fields calculated in a solver within a class

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By Neka
  • 1 Post By Neka
  • 1 Post By Neka

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 17, 2017, 11:19
Default Accessing fields calculated in a solver within a class
  #1
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
Dear OF users,

I use foam-extend-3.2 version. I would like to define an additional function in the class multiComponentMixture.
This function contains a volScalarField calculated in a solver at each time step (in my case the field is rho).
How can I access rho from within my new function?
For example if the function looks like:


Code:
Template<class ThermoType>
void Foam::multiComponentMixture<ThermoType>::myFunction()
{
    const volScalarField& Rho = db().lookupObject<volScalarField>(“rho”)
 
    forAll (Z_, n)
    {
        Z_[n] = Rho*some calculation;
    }
}
The way I try to access rho here is not working, because db() hasn’t been declared. I guess the way I do it is also wrong.

Can anybody help me? Is it even possible to access a “fresh” rho field of the last time step out of multiComponentMixture?

Thanks in advance,

Alex
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   January 18, 2017, 11:31
Default
  #2
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
I think I should precise my problem a bit more.

In several threads I’ve read, that the procedure above (the way I try to do it) works for BC classes, but I work here with a mixture class (multiComponentMixture).

As far as I see my problem at the moment, in my custom mixture I have no direct access to the field rho, which is declared in the solver in createFields.H and recalculated each time step in the solver via rho = thermo.rho().

In my custom mixture I don’t want to change rho, I just need the current rho values of the last time step in my function. So, I somehow need to lookup rho from mesh, database, …? But if I try something like this I get the error that db or mesh or whatever I try, is not declared in this scope. So how can I declare it in my mixture class?
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   January 19, 2017, 06:27
Default
  #3
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
Ok, I think I resolved the problem.

As I assumed, I have somehow to declare the database. In my custom mixture class I have mass fraction fields Y[n]. I declared the db() via these mass fraction fields. I hope I didn’t mess up my code and everything will work fine.

This is how I modified my function depicted above:

Code:
 
Template<class ThermoType>
void Foam::multiComponentMixture<ThermoType>::myFunction()
{
    const objectRegistry& db = Y_[0].db();
    const volScalarField& Rho = db.lookupObject<volScalarField>(“rho”)

    forAll (Z_, n)
    {
        Z_[n] = Rho*some calculation;
    }
}

My custom mixture class compiles fine and the solver compiles to. But I need to run the test case first to be sure, that it really works.
I hope, that the rho field that I get is the latest (from the last time step) and not from the initial conditions.

I don’t understand completely what I’ve done here. It would be nice if someone from advanced users would say something about it.
Zhiheng Wang likes this.
Neka is offline   Reply With Quote

Old   January 24, 2017, 07:07
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Neka View Post
Ok, I think I resolved the problem.

...
I don’t understand completely what I’ve done here. It would be nice if someone from advanced users would say something about it.
What you've come up with looks good. You have some volume field in your class (Y_), from which you have obtained the objectRegistry and used that to locate another field that is also registered on that database. The rho field that you will get is the reference to the density field at the current time-level.
olesen is offline   Reply With Quote

Old   January 26, 2017, 13:00
Default
  #5
Member
 
Alexander Nekris
Join Date: Feb 2015
Location: France
Posts: 32
Rep Power: 11
Neka is on a distinguished road
Hello Mark,

thank you for your reply.

It is good to know, that it refers to the current time-level.
Neka is offline   Reply With Quote

Old   March 8, 2017, 19:29
Default
  #6
Member
 
Zhiheng Wang
Join Date: Mar 2016
Posts: 72
Rep Power: 10
Zhiheng Wang is on a distinguished road
Use thermo.rho().internalField()[I]
Or thermo.rho().boundaryField()[I]

Sent from my Lenovo K50a40 using CFD Online Forum mobile app
Zhiheng Wang is offline   Reply With Quote

Old   March 8, 2017, 19:30
Default
  #7
Member
 
Zhiheng Wang
Join Date: Mar 2016
Posts: 72
Rep Power: 10
Zhiheng Wang is on a distinguished road
It's thermo.rho().boundaryField()[patchId][i]

Sent from my Lenovo K50a40 using CFD Online Forum mobile app
Zhiheng Wang is offline   Reply With Quote

Old   July 12, 2017, 15:06
Default
  #8
Member
 
Hooman
Join Date: Apr 2011
Posts: 35
Rep Power: 15
hooman.4028 is on a distinguished road
Quote:
Originally Posted by Neka View Post
Ok, I think I resolved the problem.

As I assumed, I have somehow to declare the database. In my custom mixture class I have mass fraction fields Y[n]. I declared the db() via these mass fraction fields. I hope I didn’t mess up my code and everything will work fine.

This is how I modified my function depicted above:

Code:
 
Template<class ThermoType>
void Foam::multiComponentMixture<ThermoType>::myFunction()
{
    const objectRegistry& db = Y_[0].db();
    const volScalarField& Rho = db.lookupObject<volScalarField>(“rho”)

    forAll (Z_, n)
    {
        Z_[n] = Rho*some calculation;
    }
}
My custom mixture class compiles fine and the solver compiles to. But I need to run the test case first to be sure, that it really works.
I hope, that the rho field that I get is the latest (from the last time step) and not from the initial conditions.

I don’t understand completely what I’ve done here. It would be nice if someone from advanced users would say something about it.
Hello,

I have kind of the same problem. I am adding a new model to simpleFoam (model M). I need to access a volume scalar field which will be calculated right before the very first iteration, let's call it A. How can I access this A field in my M.c file? Because obviously field A does not exist yet!

Thank you very much!
hooman.4028 is offline   Reply With Quote

Reply

Tags
access, class, field, solver


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
dsmcFoam setup hherbol OpenFOAM Pre-Processing 1 November 19, 2021 02:52
The udf.h headers are unable to open- in VISUAL STUDIO 13 sanjeetlimbu Fluent UDF and Scheme Programming 4 May 2, 2016 06:38
oopenFoam error cyndy M OpenFOAM Pre-Processing 7 March 30, 2016 09:03
deviation in calculated force between CFX solver and CFD post murx CFX 2 April 9, 2014 21:03
Errors running allwmake in OpenFOAM141dev with WM_COMPILE_OPTION%3ddebug unoder OpenFOAM Installation 11 January 30, 2008 21:30


All times are GMT -4. The time now is 13:38.