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

How to update a volScalarField's values

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree9Likes
  • 2 Post By alexeym
  • 3 Post By dkxls
  • 1 Post By dkxls
  • 1 Post By Kummi
  • 2 Post By SHUBHAM9595

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 23, 2013, 04:09
Default How to update a volScalarField's values
  #1
Member
 
Thomas Vossel
Join Date: Aug 2013
Location: Germany
Posts: 45
Rep Power: 12
ThomasV is on a distinguished road
Hi!

I have a question concerning how to update a volScalarField. Imagine the following problem:

You have a volScalarField T for the temperature (with values applied to it). You now have another volScalarField X which is meant to consist of values depending on the respective temperatures. How do I implement an update for X which looks up the temperature of each element and then updates the value in terms of special conditions. With that I mean a procedure of this sort:

Code:
if (T<100)
{
    X = 1;
}

if (T=100)
{
    X = 2;
}

if (T>100)
{
    X = 3;
}
ThomasV is offline   Reply With Quote

Old   October 23, 2013, 04:26
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,930
Rep Power: 38
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
There are several ways to do it. One of them:

Code:
forAll(X, cellI) {
    if (T[cellI] < 100)
        X[cellI] = 1;
    else if (T[cellI] > 100)
        X[cellI] = 3;
    else
        X[cellI] = 2;
}
ThomasV and Kummi like this.
alexeym is offline   Reply With Quote

Old   October 23, 2013, 04:44
Default
  #3
Senior Member
 
dkxls's Avatar
 
Armin
Join Date: Feb 2011
Location: Helsinki, Finland
Posts: 156
Rep Power: 19
dkxls will become famous soon enough
There are many examples in the source code, see e.g.:
https://github.com/OpenFOAM/OpenFOAM.../hePsiThermo.C

Here a simplified example:
Say you get your temperature field like this:
Code:
    const volScalarField& T = thermo.T();
Then you can update your field X like this:
Code:
    scalarField& TCells = T.internalField();

    forAll(TCells, celli)
    {
        if (T[celli] < 100.0)
            X[celli] = 1;
        else if (T[celli] > 100.0)
            X[celli] = 3;
        else
            X[celli] = 2;
    }
The boundary field you can update in a similar manner, I suggest you have a look at the code I linked above if you need to do this.
ThomasV, Kummi and russel60 like this.
dkxls is offline   Reply With Quote

Old   October 23, 2013, 05:13
Default
  #4
Member
 
Thomas Vossel
Join Date: Aug 2013
Location: Germany
Posts: 45
Rep Power: 12
ThomasV is on a distinguished road
Thanks!

EDIT:
Btw - is there a difference between writing "celli" and "cellI" (i.e. upper- / lowercase letter)?
ThomasV is offline   Reply With Quote

Old   October 23, 2013, 08:15
Default
  #5
Senior Member
 
dkxls's Avatar
 
Armin
Join Date: Feb 2011
Location: Helsinki, Finland
Posts: 156
Rep Power: 19
dkxls will become famous soon enough
Quote:
Originally Posted by ThomasV View Post
Btw - is there a difference between writing "celli" and "cellI" (i.e. upper- / lowercase letter)?
No, there is no difference.
The variable celli is a 'label' (i.e. a integer) defined in forAll, which is a macro that sets up the for loop. So, you can call the variable whatever you want, you just need to be consistent.

Last edited by dkxls; October 23, 2013 at 09:45.
dkxls is offline   Reply With Quote

Old   October 28, 2013, 10:06
Default
  #6
Member
 
Thomas Vossel
Join Date: Aug 2013
Location: Germany
Posts: 45
Rep Power: 12
ThomasV is on a distinguished road
Allright thanks...

I'd like to add another more "practical" question here. Things now work fine and I define a temperature field which then is analyzed to determine the solid fraction in the respective cells. What I'd like to know is how to write the new volScalarField for the fraction solid into the "0 folder". I create the fraction solid field like this:
Code:
    volScalarField fracSol
    (
        IOobject
        (
            "fracSol",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        scalar(0)
    );
Afterwards it becomes filled with the actual data determined by the respective temperatures of the cells. This way I get my fracSol file written in every timestep. Every timestep except for the initial one that is...

I now would like to know how I also can write the calculated fracSol into the 0 folder i.e. the fraction solid field for the initial temperatures. This also would come in handy when postprocessing the project in ParaFoam as there now is no fracSol field in time step 0 and it is a bit tedious to go to timestep 1 and activate the field plus once again any time you accidentally rewind to timestep 0 as it then gets unloaded (because there's no data for it in step 0)...

I already tried to simply put a runtime.write() command before progressing time in the time loop but this didn't do the trick. I guess one usually would approach this problem with the setfields utility but as far as I know one here is limited to the premade functions like defining a block in which the variable get set to a certain value...
ThomasV is offline   Reply With Quote

Old   October 28, 2013, 10:31
Default
  #7
Senior Member
 
dkxls's Avatar
 
Armin
Join Date: Feb 2011
Location: Helsinki, Finland
Posts: 156
Rep Power: 19
dkxls will become famous soon enough
you can force the writing of a field at any time with
Code:
fracSol.write();
You just should take care that you don't do it in the time-loop, as the field would then be written every time-step.
ThomasV likes this.
dkxls is offline   Reply With Quote

Old   October 28, 2013, 11:07
Default
  #8
Member
 
Thomas Vossel
Join Date: Aug 2013
Location: Germany
Posts: 45
Rep Power: 12
ThomasV is on a distinguished road
Thanks - if I can come up with enough self-motivation after having finished my master thesis I might write a beginners tutorial so people won't have such a hard time figuring out such "mundane" things like this...
ThomasV is offline   Reply With Quote

Old   December 21, 2018, 11:39
Default
  #9
Senior Member
 
Kumaresh
Join Date: Oct 2016
Posts: 347
Rep Power: 11
Kummi is on a distinguished road
Send a message via Yahoo to Kummi
Hello Foamers,
My problem is almost similar. I have two volScalarFields T (temperature) and k (thermal conductivity). volScalarField k depends upon volScalarField T with the following expression:
Quote:
forAll( TCells, celli )
{
k[celli] = 1.833e-03* Foam::sqrt(TCells[celli]);
}

I need to update the value (k) thermal conductivity for every time step based on Temperature (T). I have followed the above comments and compiled the new FOAM successfully. However, k returns constant value for every time step after solving the expression.
For verification, I am hereby mentioning the source (.C) and createFields.H files.
Quote:
SOURCE FILE

while (simple.loop())
{
Info<< "Time = " << runTime.timeName() << nl << endl;

while (simple.correctNonOrthogonal())
{
{
solve
(
fvm::ddt(rho * Cp, T)
- fvm::laplacian(k, T)
);
scalarField& TCells = T.primitiveFieldRef();
forAll( TCells, celli )
{
k[celli] = 1.833e-03* Foam::sqrt(TCells[celli]);
}
}
}
runTime.write();

Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
<< " ClockTime = " << runTime.elapsedClockTime() << " s"
<< nl << endl;
}
Quote:
createField FILE
Info<< "Reading field T\n" << endl;

volScalarField T
(
IOobject
(
"T",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

Info<< "Reading field k\n" << endl;

volScalarField k
(
IOobject
(
"k",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh,
dimensionSet(1,1,-3,-1,0,0,0)
);


Info<< "Reading transportProperties\n" << endl;

IOdictionary transportProperties
(
IOobject
(
"transportProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::AUTO_WRITE
)
);

Info<< "Reading density rho\n" << endl;

dimensionedScalar rho
(
transportProperties.lookup("rho")
);

Info<< "Reading thermal capacity Cp\n" << endl;

dimensionedScalar Cp
(
transportProperties.lookup("Cp")
);
Please give me a clue on how to update value of k depending upon T. Kindly share your ideas please. Your thoughts are highly helpful !!!
Thank you
massive_turbulence likes this.
Kummi is offline   Reply With Quote

Old   March 10, 2020, 12:51
Default
  #10
Member
 
MNM
Join Date: Aug 2017
Posts: 65
Rep Power: 8
SHUBHAM9595 is on a distinguished road
Quote:
Please give me a clue on how to update value of k depending upon T.
I know it's too late but might help someone else. You just have to assign k in the last section after solving TEqn so that for each time step you can have updated k based on the most recent T.


Code:
   fvScalarMatrix TEqn
    (
        fvm::ddt(T)
      +	fvm::div(phi, T)
      - fvm::laplacian(alpha, T)
     ==
        (1/cp)*(tau && gradU)
    );

TEqn.solve();

k = 1.833e-03* Foam::sqrt(T);
Just make sure you have
Code:
IOobject::AUTO_WRITE
for "k" while declaring it in createFields.H
tonnykz and Liangyuan like this.
SHUBHAM9595 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Retrieving boundary patch values adjacent to a given cell brooksmoses OpenFOAM Post-Processing 2 December 8, 2008 10:00
strange node values @ solid/fluid interface - help JB FLUENT 2 November 1, 2008 12:04
Plotting raw data values Wilesco Siemens 0 January 5, 2006 05:34
Mass Flux values and calculations Cb Siemens 1 January 22, 2005 09:21
node based or cell centered Ts values acboge FLUENT 0 February 6, 2004 06:41


All times are GMT -4. The time now is 04:00.