CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Running, Solving & CFD (
-   -   coupling OpenFOAM with other programs (

Martin123 May 26, 2009 11:30

coupling OpenFOAM with other programs

I am Martin and new to OpenFOAM. I want to solve the Laplace-Equation
grad(k(x)*grad u(x))=0 with OpenFOAM and interconnect k(x) with data coming from outside.

Is there any possibility to read in and write out dynamically on run-time the k(x) values into a VolScalarField (I'd guess) ? And to access maybe the nearest node for a given coordinate? Or at least to iterate over all nodes, get the coordinates and get/set k(x)?

Any help is appreciated.


henrik May 28, 2009 21:02

Dear Martin,

the bottom line is that you can do anything because you have the source code. If you have the source code for the other code, then merging the two codes into a single executable is easier & faster than file IO while avoiding syncronisation problems. However, this is major surgery and you need to know what you are doing. Concerning your questions:


I would simply create a (temporary) field from an IOobject. Look at any createField.H and place the code inside of the time loop.

However, should work, too.



@finding cell numbers:


vector pos;
label cellI = mesh.findCell(pos);
Info << field[cellI] << endl;

@iterate cell centers:


forAll(mesh.C(), cellI)
    vector pos;
    if ( mag(mesh.C()[cellI] - pos) < SMALL)
        field[cellI] = newValue;

Hope this helps.


Martin123 June 2, 2009 05:26

Dear Henrik,

thank you very much for you help, this is exactly what I was looking for.

I followed your suggestion and created and temporary field during the runtime-loop; unfortunately, OF claims that it cannot read my temporary field

volScalarField epsilon

cannot open file

file: /home/dw9203/projects/ofoprj/cavity/0.005/epsilon at line 0.

From function regIOobject::readStream(const word&)
in file db/regIOobject/regIOobjectRead.C at line 66.

FOAM exiting

But it should however not read from disk, I want to specify the field values during run-time. Could you enlighten this?

Martin123 June 3, 2009 18:13

Thanks, now it works setting values on run-time. A different constructor for volscalarfield made it and I can set now values during run-time. Great!

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