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

C++ and openFoam

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Goab
  • 1 Post By marupio

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 2, 2012, 11:55
Default C++ and openFoam
  #1
New Member
 
Jorge Meneses
Join Date: Feb 2012
Posts: 6
Rep Power: 14
Goab is on a distinguished road
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!!!!!
Kummi likes this.
Goab is offline   Reply With Quote

Old   May 2, 2012, 18:40
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
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
Kummi likes this.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   May 3, 2012, 07:53
Default
  #3
New Member
 
Jorge Meneses
Join Date: Feb 2012
Posts: 6
Rep Power: 14
Goab is on a distinguished road
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

Last edited by Goab; May 3, 2012 at 08:04. Reason: Additional information
Goab is offline   Reply With Quote

Old   May 3, 2012, 09:27
Default
  #4
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
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.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   May 5, 2012, 07:20
Default
  #5
New Member
 
Jorge Meneses
Join Date: Feb 2012
Posts: 6
Rep Power: 14
Goab is on a distinguished road
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
Goab is offline   Reply With Quote

Reply

Tags
c++, force, simplefoam


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
New OpenFOAM Forum Structure jola OpenFOAM 2 October 19, 2011 06:55
Cross-compiling OpenFOAM 1.7.0 on Linux for Windows 32 and 64bits with Mingw-w64 wyldckat OpenFOAM Announcements from Other Sources 3 September 8, 2010 06:25
Modified OpenFOAM Forum Structure and New Mailing-List pete Site News & Announcements 0 June 29, 2009 05:56
64bitrhel5 OF installation instructions mirko OpenFOAM Installation 2 August 12, 2008 18:07
Adventure of fisrst openfoam installation on Ubuntu 710 jussi OpenFOAM Installation 0 April 24, 2008 14:25


All times are GMT -4. The time now is 09:56.