CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   C++ and openFoam (https://www.cfd-online.com/Forums/openfoam-programming-development/101380-c-openfoam.html)

Goab May 2, 2012 11:55

C++ and openFoam
 
Hello Foamers,

This might be a pretty basic cuestion, however I've trying to find some information or advise on taht respect with no results.

I have a C++ code in charge of creating a mesh with FEM and when flux passes trought it the solid would deform causing forces to spring which in turn changes the flux, I've been checking the icoFSIFoam and it's been quite helpful.

My problem is that I do need to solve the solid with the already implemented code, meaning that I have to transfered the calculated from C++ to openFoam, plase them on the cell faces and finally, once the flux is solved return U to the C++ code.

I'm using simpleFoam where the UEqn

tmp<fvVectorMatrix> UEqn
(
fvm::div(phi, U)
+ turbulence->divDevReff(U)
+ Force
==
sources(U)
);

Now, in orther to make it work I defined Force as:

Info<< "Reading field Force\n" << endl;
volVectorField Force
(
IOobject
(
"Force",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
mesh
);

Thinking on produce a List from the C++ code and make the volVectorField to read it and I discovered that I had no idea how to do it, I tryied to create a new /Force every timestep but I realized that I don't know how to make OF to read the infomation I'm giving it or pass it to the C++ code.

Thanks a lot!!!!!

marupio May 2, 2012 18:40

Not sure what you want.

You have specified a volVectorField for force, and you are using a dictionary constructor. So it will read its initial values from [case]/[time value]/Force. And it will output to the latest time directory everytime OpenFOAM writes. It can't read from the file again mid-run.

Is there an equation for Force? If so, you can then set it equal to the expression. Eg:

Code:

    Force = k * (x - x.oldTime());
    // where x is a volVectorField of position


Goab May 3, 2012 07:53

Hi Marupio, thanks for your quick reply,

Sorry if I didn't make my intentions clear, the thing is that I'm testing some materials with different properties using the C++ code, reason why I can't use the equation you're proposing, I mean, I have some different equations already solved mostly based on energy.

Now, what I would need to do is to read those forces from C++ every timestep from openFoam and place them accordingly depending on their position within the C++-solid on the internal field (or at the cells) of my simpleFoam mesh, which by the way, is a cube:

vertices
(
(0 0 0)//0
(32 0 0)//1
(32 32 0)//2
(0 32 0)//3
(0 0 32)//4
(32 0 32)//5
(32 32 32)//6
(0 32 32)// 7

);

blocks
(
hex (0 1 2 3 4 5 6 7) (20 20 20) simpleGrading (1 1 1)
);

Once forces are placed, solve U and p normally and sample U at the timestep and export it to C++, to the equations there and again solve the forces.

My first thought was to create a List-like file form C++ and make "Forces" to read it but I don't know how to do it or even if it could work that way.

As far as I know and taking into account that OF is based on C++ it shouldn't be too difficult, however I've been trying it for quite a time and I'm still stuck.


Thanks

marupio May 3, 2012 09:27

Are you trying to communicate back and forth between independent programs? I am confused because you're talking about exporting to C++... but OpenFOAM is in C++. I assume you mean there's another program.

If you want OpenFOAM to reread Forces at ever time step, here's what I'm thinking:

Make Forces a local object in your solver. Do not define it in createFields, rather define it within your run loop. That way, it is rebuilt at every timestep.

In order for this to work, I think you need your solver to output at every time step. Also, you need the Forces file for the next time step to be already available in the next time directory. OpenFOAM creates these time directories during a write, but your "C++" needs to create the directory, and place the Forces file in it, properly formatted as a VolVectorField. If you have trouble with this, you could resort to placing it in your constant directory... but you'd overwrite the previous Forces files... if that's bad, you could rename the old one with name-mangling to Forces_[time] or something.

The key being to move the Forces constructor out of createFields.H and into the main run time loop.

Goab May 5, 2012 07:20

Fantastic, thanks for you advise.

I defined the volVectorField out of the createFields.H and now is reading the Force from a file. You're right I have another program, "forces.cpp" calculating the forces for me. I have a couple more questions though.

1. Is it possible to make both programs at the same time, I mean, I want them to be made in the same solver by adding "#include forces.H" to the forcesSimpleFoam and when I run it in the command line for the case with forcesSimpleFoam for it to run "forces.cpp" as well?.

I've been trying it but with no results yet.

2. I've been trying to export the loop time and store in a variable so both of the programs will write both U and forces within the same directory and work automatically. I tried to put my "foces.cpp" code into the forcesSimpleFoam.C but since the startTime and endTime are dimensionedScalars it wasn't possible to convert them to a double. Is there an easier way?, how can I transform one datatype to another?.

3. Now that the volVectorField is reading, I would like to place those forces on different points, or cells,since as far as I know openFoam works with cells, I thought I might use setCells for this issue, that way I could place the forces in whether a straight line or a sphere however it's not working as I suspected. Any Idea regarding that?.

Thank you very much for helping me


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