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

Writing a new variable as output

Register Blogs Community New Posts Updated Threads Search

Like Tree8Likes
  • 2 Post By kuria
  • 4 Post By clapointe
  • 2 Post By clapointe

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 30, 2018, 05:30
Default Writing a new variable as output
  #1
Member
 
K
Join Date: Mar 2018
Posts: 34
Rep Power: 8
kuria is on a distinguished road
Hey everyone!

I am new to OpenFoam and I am learning about interFoam. Currently I am trying to write the data for a new variable that can be read in Paraview.
For example: Say I want to write the rho variable data for the user defined writeInterval.
I edited the CreateFields.H file as (the part dealing with rho variable)
volScalarField rho
(
IOobject
(
"rho",
runTime.timeName(),
mesh,
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE
),
alpha1*rho1 + alpha2*rho2
);
rho.oldTime();
// writing a new variable
rho.write();

But this doesnot seem to work, any suggestions on how to address this problem?

Thank you!
Kummi and ARTisticCFD like this.
kuria is offline   Reply With Quote

Old   April 3, 2018, 23:23
Default
  #2
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 15
clapointe is on a distinguished road
I am not sure why interfoam doesn't automatically write rho, but an easy to understand work around is to define a new field, say rhoWrite, that's written automatically. Then you just need to compute/update rhoWrite with rho before the solver writes data with runTime.write(). So, add

Code:
volScalarField rhoWrite
(
    IOobject
    (
        "rhoWrite",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    alpha1*rho1 + alpha2*rho2
);
to createFields.H after alpha/rho are defined. Then add

Code:
rhoWrite = alpha1*rho1 + alpha2*rho2;
to interFoam.C before runTime.write() and recompile. Beware that I've not tested the previous suggestions, but I've done similar things in the past.

Caelan.
clapointe is offline   Reply With Quote

Old   April 12, 2018, 06:41
Default
  #3
Member
 
K
Join Date: Mar 2018
Posts: 34
Rep Power: 8
kuria is on a distinguished road
Hey Caelan

Great! That worked

Thank you

-Kurian
kuria is offline   Reply With Quote

Old   September 5, 2019, 00:26
Default
  #4
New Member
 
Aashay Tinaikar
Join Date: May 2019
Location: Boston
Posts: 19
Rep Power: 7
ARTisticCFD is on a distinguished road
Hii Kuria and Clapointe,

Thanks for posting the question and giving a very working answer. I was facing an issue where the scalar field variable was not written at output. Precisely, I just added a variable in a manner given in the wiki page
https://openfoamwiki.net/index.php/H...ure_to_icoFoam

I found out that the new variable is written when I specify fixedValue velocity condition but not when I change it to flowRateInletVelocity. Therefore, I suppose it might have some bug.

Quick fix: Add variable_Name.write() just before runTime.write()
ARTisticCFD is offline   Reply With Quote

Old   September 6, 2019, 18:57
Default
  #5
New Member
 
Aashay Tinaikar
Join Date: May 2019
Location: Boston
Posts: 19
Rep Power: 7
ARTisticCFD is on a distinguished road
Quote:
Originally Posted by ARTisticCFD View Post
Quick fix: Add variable_Name.write() just before runTime.write()
Adding only this line forces the solver to write the variable at each iteration irrespective of out output controls. Therefore, better to write it in an "if" statement


if(runtime.outputTime())
{
variable_Name.write();
}

Thanks. Hope this helps.
ARTisticCFD is offline   Reply With Quote

Old   February 9, 2021, 18:30
Default
  #6
New Member
 
Vishal
Join Date: Sep 2020
Posts: 6
Rep Power: 6
vishalgarg474 is on a distinguished road
Hey Guys, is there a possibility where the changes should not be done in the solver but in the controlDict file to write a new variable?


I am trying to write drag coefficients in the multiphaseEulerFoam. I can not make changes in the original solver due to some permission rights. Is there a way that I can add lines in a case directory to write it?


Thank you
vishalgarg474 is offline   Reply With Quote

Old   February 9, 2021, 18:44
Default
  #7
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 15
clapointe is on a distinguished road
Yep, this is possible via function objects compiled at runtime. You can do something like the following (which is added to controlDict) :

Code:
functions
{
    vorticity
    {
       libs ("libutilityFunctionObjects.so");

       type coded;
       name vorticity;

       codeExecute
       #{
          const volVectorField& U = mesh().lookupObject<volVectorField>("U");

          volScalarField enst = 0.5*magSqr(fvc::curl(U));

          static autoPtr<volScalarField> pField;
          if(!pField.valid())
          {
              pField.set
              (
                  new volScalarField
                  (
                      IOobject
                      (
                          "enstrophy",
                          mesh().time().timeName(),
                          U.mesh(),
                          IOobject::NO_READ,
                          IOobject::AUTO_WRITE
                      ),
                      enst
                  )
              );
          }

          volScalarField& enstrophy = pField();

          enstrophy.checkIn();

          enstrophy = enst;
      #};
    }
}
Note that I based this off some thread that I cannot remember, but was asking a similar question -- so this may not be the most efficient way to do it, but it works. Can't remember which version I've used this in... likely 7.

Caelan
Edee and hhu_lulu like this.
clapointe is offline   Reply With Quote

Old   May 12, 2021, 14:01
Default
  #8
New Member
 
sujata
Join Date: Dec 2019
Posts: 10
Rep Power: 7
sujata is on a distinguished road
Quote:
Originally Posted by vishalgarg474 View Post
Hey Guys, is there a possibility where the changes should not be done in the solver but in the controlDict file to write a new variable?


I am trying to write drag coefficients in the multiphaseEulerFoam. I can not make changes in the original solver due to some permission rights. Is there a way that I can add lines in a case directory to write it?


Thank you
Did your problem resolve? If yes then how have you done it?
sujata 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
[Other] refineWallLayer Error Yuby OpenFOAM Meshing & Mesh Conversion 2 November 11, 2021 12:04
[mesh manipulation] How to write cellSet for different regions in constant/polyMesh/sets Struggle_Achieve OpenFOAM Meshing & Mesh Conversion 3 June 17, 2019 10:29
Need help in writing UDF for an oscillating airfoil to get Output parameters ned.musab FLUENT 0 October 27, 2017 12:21
writing a scalar in the output anishtain4 OpenFOAM 6 January 25, 2013 05:16
Env variable not set gruber2 OpenFOAM Installation 5 December 30, 2005 05:27


All times are GMT -4. The time now is 14:12.