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

How can write a field in openFOAM

Register Blogs Community New Posts Updated Threads Search

Like Tree34Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 14, 2009, 06:11
Default How can write a field in openFOAM
  #1
Member
 
Sven Degner
Join Date: Mar 2009
Location: Zürich
Posts: 55
Rep Power: 17
sven82 is on a distinguished road
hi,

I will write out a variable of my own turb. model,
but its doesn't work.
First of all I define a ScalarField in my Header,

volScalarField hybrid_;

for the next step I edit the code file with a new Object

hybrid_
(
IOobject
(
"hybrid_",
runTime_.timeName(),
mesh_,
IOobject::NO_READ,
IOobject::AUTO_WRITE
),
mesh_,
dimensionedScalar("hybrid", dimless, 0.0)
),

and follow of the definition in the memberfunction

hybrid_ = tanh ( pow( max( scalar(0) , ( I_ / X_ ) - 1/2 ) , 3) );

// function = tanh( ( max( 0 , l/x - 1/2 ) )³ )

the compile shows now errors and the case run fine for me,
except !!
the code cant write out the values of my defined variable (hybrid_).
When I add the line hybrid_.write() its works,
but for every timestep and not for the defined writecontrol!

Hope everyone got a idea !

Thanks
Sven
Jorg likes this.
sven82 is offline   Reply With Quote

Old   May 14, 2009, 16:27
Default
  #2
Senior Member
 
su_junwei's Avatar
 
su junwei
Join Date: Mar 2009
Location: Xi'an China
Posts: 151
Rep Power: 20
su_junwei is on a distinguished road
Send a message via MSN to su_junwei
Hi Sven

you can test the following code

if(runTime_.outputTime())
{
hybrid_.write();
}

Junwei
su_junwei is offline   Reply With Quote

Old   May 15, 2009, 04:44
Default
  #3
Member
 
Sven Degner
Join Date: Mar 2009
Location: Zürich
Posts: 55
Rep Power: 17
sven82 is on a distinguished road
thanks junwai,

it works

Sven
sven82 is offline   Reply With Quote

Old   July 26, 2012, 10:44
Default
  #4
Senior Member
 
Samuele Z
Join Date: Oct 2009
Location: Mozzate - Co - Italy
Posts: 520
Rep Power: 19
samiam1000 is on a distinguished road
Dear friends,

I am trying to solve the same problem you solved.

Could you help me?

Where should I add that code?

And is it necessary to re-compile the code? Can't I do the same thing in the case-dir, editing the dictionaries (e.g. controlDict)?

Thanks a lot for help,
Samuele
samiam1000 is offline   Reply With Quote

Old   July 26, 2012, 17:52
Default
  #5
Senior Member
 
Jens Höpken
Join Date: Apr 2009
Location: Duisburg, Germany
Posts: 159
Rep Power: 17
jhoepken is on a distinguished road
Send a message via Skype™ to jhoepken
First of all, if you would like to change something in the code, you will - of course - have to recompile the respective parts. If you would just like to write a certain field to disk, that is not written automatically, I guess that there is a functionObject to do that.

Otherwise you will have to put something like this in your solver/library
Code:
volScalarField foo = mesh.V();
foo.write();
Have a look at the programmer's guide.
jhoepken is offline   Reply With Quote

Old   July 26, 2012, 17:53
Default
  #6
Senior Member
 
Jens Höpken
Join Date: Apr 2009
Location: Duisburg, Germany
Posts: 159
Rep Power: 17
jhoepken is on a distinguished road
Send a message via Skype™ to jhoepken
And it would be great if you could just post your question once!
jhoepken is offline   Reply With Quote

Old   July 27, 2012, 04:25
Default
  #7
Senior Member
 
Samuele Z
Join Date: Oct 2009
Location: Mozzate - Co - Italy
Posts: 520
Rep Power: 19
samiam1000 is on a distinguished road
Hi,

thanks for answering and sorry for the double question.

Samuele
samiam1000 is offline   Reply With Quote

Old   August 29, 2013, 06:58
Default
  #8
New Member
 
Join Date: Feb 2012
Posts: 25
Rep Power: 14
Marshak is on a distinguished road
I am having the same problem to solve. Can someone post the correct code because from above posts it is not clear what is the right way to code?
Marshak is offline   Reply With Quote

Old   August 29, 2013, 07:00
Default
  #9
Senior Member
 
Dr. Alexander Vakhrushev
Join Date: Mar 2009
Posts: 256
Blog Entries: 1
Rep Power: 19
makaveli_lcf is on a distinguished road
Send a message via ICQ to makaveli_lcf
Different variants are correct :-)
__________________
Best regards,

Dr. Alexander VAKHRUSHEV

Christian Doppler Laboratory for "Metallurgical Applications of Magnetohydrodynamics"

Simulation and Modelling of Metallurgical Processes
Department of Metallurgy
University of Leoben

http://smmp.unileoben.ac.at
makaveli_lcf is offline   Reply With Quote

Old   January 31, 2014, 16:23
Default Unable to write field
  #10
New Member
 
Daniel Duque
Join Date: Jan 2011
Location: ETSIN, Madrid
Posts: 28
Rep Power: 15
dduque is on a distinguished road
Hi every one.
I have tried to use these ideas, and other, but right now I am failing to write down a field. I try something as simple as modifying interFoam, and in createFields.H :

Code:
    volScalarField surfForce
    (
        IOobject
        (
            "surfForce",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        alpha1
    );
The name "surfForce" makes no sense, I just tried to write down this complicated field and made my request as simple as possible. The thing is, the field is written down correctly in the first time step, and it is identical to the alpha1 field (the one from time step zero, actually). But in subsequent times, it does not change at all, even if it is written. Meanwhile alpha1 keeps changing (where is it told that this field be written, I wonder?).

Thanks in advance. Best,

Daniel
dduque is offline   Reply With Quote

Old   February 2, 2014, 09:16
Default
  #11
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,981
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
Greetings to all!

@Daniel: OpenFOAM's C++ already seems voodoo'ish enough already, so it would be even more complicated if all it took to bind one field to another, would be to reference it only once at construction

In other words, I suggest that you give a quick study to this tutorial: http://openfoamwiki.net/index.php/Ho...ure_to_icoFoam - You'll notice that the "T" field is changed on every iteration, not just at the start.

Best regards,
Bruno
dduque likes this.
__________________
wyldckat is offline   Reply With Quote

Old   February 2, 2014, 10:30
Default
  #12
New Member
 
Daniel Duque
Join Date: Jan 2011
Location: ETSIN, Madrid
Posts: 28
Rep Power: 15
dduque is on a distinguished road
Of course! I know that tutorial almost by heart, but somehow the fact that a whole equation was solved for T had obscured the obvious fact that T is changed. I only have to repeat the field=alpha1 (or whatever) at each iteration then.

Thanks a lot!

Daniel
dduque is offline   Reply With Quote

Old   May 14, 2014, 12:30
Default
  #13
New Member
 
Christoph Wenzel
Join Date: May 2014
Location: Germany
Posts: 21
Rep Power: 12
ChrisWe is on a distinguished road
I actually try to program my own solver and there are some variables that I want to write down in an additional file. When I try this for the turbulent kinetic energy that works fine. Therefore I added in the createFields.H the following code:

Code:
volScalarField k_output
(
    IOobject
    (
        "k_output",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    turbulence->k()
);
and in the .c file of my solver the following code:

Code:
...
#include "createFields.H"
...
while (runTime.run())
{
...
k_output = turbulence->k();
...
}
All works perfect, OpenFOAM creates a new output-file called k_output where k is stored.


Now I want to do the same with a variable (volTensorField) that I created by myself. In this case the variable is called alphaEff.
Therefore I added the same lines like before in the createField.H file for my own TensorField:

Code:
volTensorField alphaEff_output
   (
      IOobject
      (
        "alphaEff_output",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
        ),
        alphaEff       (i also tried alphaEff_output together with (**) )
  );
and in the .c file of my solver:

Code:
...
#include "createFields.H"
...
while (runTime.run())
{
...
    volTensorField alphaEff
       (
          IOobject
          (
            "alphaEff",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
            ),
            turbulence->nu()/Pr*I + DT
       );
...
alphaEff_output=alphaEff;  (**) (only when using alphaEff_output)
...
}
Now, I've got some problems, because OpenFOAM doesn't know alphaEff in the createFields.H file.

I think that the answer is extremely easy, but I'm sitting now about two days in front of my PC without any progress. Maybe one of you knows the answer for my little problem



(the idea of
Code:
if(runTime_.outputTime())
   {
    alphaEff.write();
  }
works, but then the probes-function doesn't work for alphaEff).
Kummi likes this.
ChrisWe is offline   Reply With Quote

Old   May 14, 2014, 12:51
Default
  #14
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

Why would you like to define alphaEff inside 'while (runTime.run())' loop and not outside?

Also you can define it just like:

Code:
volTensorField alphaEff
(
    IOobject
    (
        "alphaEff",
        runTime.timeName(),
        mesh,
        IOobject::NO_READ,
        IOobject::AUTO_WRITE
    ),
    mesh,
    <dimensions of alphaEff>
);
as anyway you recalculate it every time step (if my guess is correct).

It will be automatically written every write time.
ansubru likes this.
alexeym is offline   Reply With Quote

Old   May 15, 2014, 06:45
Default
  #15
New Member
 
Christoph Wenzel
Join Date: May 2014
Location: Germany
Posts: 21
Rep Power: 12
ChrisWe is on a distinguished road
Thank you very much for your rapid answer, but this solution doesn't work at all.

When I tried your idea, OpenFoam needed an INPUT-File in the 0-folder and a MUST_READ in the IOobject because of the <mesh> entry. After changing this, all worked.

So for all who have the same problem, this was my solution:

In the solver-folder, I added the following IOobject into the <createfields.H> file:

Code:
volTensorField alphaEff
   (
      IOobject
      (
        "alphaEff",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::AUTO_WRITE
      ),
      mesh
);
This file requires an additional INPUT-File in the boundary 0-folder that is called alphaEff and looks like the other boundary-files.

In the .c file of the solver, I only have to update alphaEff in the time-loop, in my case:

Code:
alphaEff=turbulence->nu()/Pr*I + DT;
With this solution, its possible to take probes from alphaEff.
The only ugly part of this solution is the one, that you have to create an additional Input-File for alphaEff.

Maybe someone knows a solution, where I don't need this file.
When I try to create an IOobject with NO_READ, OpenFoam doesn't work anymore.

Last edited by ChrisWe; May 15, 2014 at 08:04.
ChrisWe is offline   Reply With Quote

Old   May 15, 2014, 06:54
Default
  #16
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
I don't know what modifications you've done to proposed solution (I guess you've ignored the line with dimensions), but here's a piece of working code:

Code:
...
    flux_
    (
        IOobject
        (
            "heatFlux",
            mesh.time().timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        dimEnergy/dimTime/dimVolume
    )
...
or we can use another way to initialize field to zero:

Code:
...
    flux_
    (
        IOobject
        (
            "heatFlux",
            mesh.time().timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        mesh,
        dimensionedScalar("flux", dimEnergy/dimTime/dimVolume, 0.0)
    )
...
There's no heatFlux file in 0 folder and field flux_ is written every writeInterval.

In your case it should be tensor, not scalar.
Lisandro Maders and ChrisWe like this.
alexeym is offline   Reply With Quote

Old   May 15, 2014, 08:42
Default
  #17
New Member
 
Christoph Wenzel
Join Date: May 2014
Location: Germany
Posts: 21
Rep Power: 12
ChrisWe is on a distinguished road
Ahhhh, now its working without defining an Input-File for alphaEff. Thank you very much for your patience and your helpfulness!!!

In my version before, I had to replace your NO_READ with MUST_READ because I thought, that the dimensions aren't necessary at all. But I was wrong, NO_READ only works together with the dimensions in this case:

Code:
      mesh,
      dimensionedTensor("alphaEff", dimArea/dimTime, tensor::zero)
granzer likes this.
ChrisWe is offline   Reply With Quote

Old   July 21, 2014, 08:26
Default
  #18
Member
 
Vignesh
Join Date: Oct 2012
Location: Darmstadt, Germany
Posts: 66
Rep Power: 14
vigneshTG is on a distinguished road
Dear All,

I am working on VOF method and using interFoam solver. I would like to write the whole surface tension term in UEqn.H (fvc::interpolate(interface.sigmaK())*fvc::snGrad(a lpha1)) to the output. I tried creating a volVectorField in CreateFields.H as below
Code:
//*******************************************************************************//
volVectorField ST
    (
        IOobject
        (
            "ST",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
        fvc::interpolate(interface.sigmaK())*fvc::snGrad(alpha1)
    );
//*******************************************************************************//
But I get error as below
Code:
In file included from interFoam.C:60:0:
createFields.H: In function 'int main(int, char**)':
createFields.H:40:26: error: 'interface' was not declared in this scope
createFields.H:40:58: error: 'alpha1' was not declared in this scope
/opt/openfoam222/src/finiteVolume/lnInclude/readTimeControls.H:38:8: warning: unused variable 'maxDeltaT' [-Wunused-variable]
make: *** [Make/linux64GccDPOpt/interFoam.o] Error 1
Can anyone help me how to rectify this Or is there any other way to write the surface tension term to the output.
Kummi likes this.
__________________
Thanks and Regards

Vignesh
vigneshTG is offline   Reply With Quote

Old   July 21, 2014, 09:16
Default
  #19
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

where exactly did you put the code? Obviously it should be placed after definition of alpha1 (line 36) and interface (line 76).

From error output I can see that expression with alpha1 and interface is at the line 40, so I guess definition of volScalarField started before definition of alpha1 and interface (hence the error). Also

Code:
fvc::interpolate(interface.sigmaK())*fvc::snGrad(alpha1)
will give you surface field, you need to reconstruct it (see fvc::reconstruct in UEqn.H) to get volume field.
alexeym is offline   Reply With Quote

Old   July 21, 2014, 09:52
Default
  #20
Member
 
Vignesh
Join Date: Oct 2012
Location: Darmstadt, Germany
Posts: 66
Rep Power: 14
vigneshTG is on a distinguished road
Thanks Alex !! You pointed it out, i wrote the definition of surface tension term before alpha definition. Now i corrected it and it works !!

Thank you very much !!
linyanx likes this.
__________________
Thanks and Regards

Vignesh
vigneshTG 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
Critical errors during OpenFoam installation in OpenSuse 11.0 amscosta OpenFOAM 5 May 1, 2009 15:06
Problem installing OpenFOAM 1.5 installation on RHEL 4. vwsj84 OpenFOAM Installation 4 April 23, 2009 05:48
2009 OpenFOAM Summer School in Zagreb, Croatia hjasak OpenFOAM Announcements from Other Sources 0 March 27, 2009 13:08
Adventure of fisrst openfoam installation on Ubuntu 710 jussi OpenFOAM Installation 0 April 24, 2008 15:25
OpenFOAM Training and Workshop Hrvoje Jasak Main CFD Forum 0 October 7, 2005 08:14


All times are GMT -4. The time now is 16:21.