CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM

automatic call a function in a program

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   June 22, 2009, 03:38
Default automatic call a function in a program
  #1
New Member
 
Gerard Mouokue
Join Date: Mar 2009
Location: Darmstadt, Hessen, Germany
Posts: 16
Rep Power: 8
gerado is on a distinguished road
Hi,
I have a program and want to call automatically another function in my program . for example

int main (){

myprogram
gambitToFoam "gambitmesh"
return 0;
}
gerado is offline   Reply With Quote

Old   June 22, 2009, 05:54
Default
  #2
4xF
New Member
 
Frank Albina
Join Date: Mar 2009
Location: Switzerland
Posts: 14
Rep Power: 8
4xF is on a distinguished road
Send a message via Skype™ to 4xF
Hi Gerard!

in your case, wouldn't it be more adequate to change your program into a shell or python script? If you just manipulate data an/or files, this is the most portable and efficient way of doing it.

Else, you still have the call to system() from <stdlib.h>, but you will need to make sure you find the executable, i.e. you might need to input the full path. This has the drawback that you will need to recompile your code every time you change the executable (here gambitToFoam) is in another location.

The shell/python/perl/etc... scripts on the other can get the path to the executable from the environment variables. It's up to you to decide which strategy suits you best.
4xF is offline   Reply With Quote

Old   June 25, 2009, 02:36
Default
  #3
New Member
 
Gerard Mouokue
Join Date: Mar 2009
Location: Darmstadt, Hessen, Germany
Posts: 16
Rep Power: 8
gerado is on a distinguished road
Hi Frank ,
I have a c++ code and don't want to transform the code in a script . my problem is that I want at some place of my program to call the function GambitToFoam without having to tipp it myself .
gerado is offline   Reply With Quote

Old   June 25, 2009, 07:13
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: http://olesenm.github.io/
Posts: 777
Rep Power: 18
olesen will become famous soon enough
Quote:
Originally Posted by 4xF View Post
Else, you still have the call to system() from <stdlib.h>, but you will need to make sure you find the executable, i.e. you might need to input the full path. This has the drawback that you will need to recompile your code every time you change the executable (here gambitToFoam) is in another location.
Actually according to the man page, "/bin/sh -c command" is called. Thus there's no need to resolve the path beforehand.
There is also a Foam::system(const string&) available from OSspecific, so you can also call it with C++ strings.
olesen is offline   Reply With Quote

Old   June 25, 2009, 08:05
Default
  #5
New Member
 
Gerard Mouokue
Join Date: Mar 2009
Location: Darmstadt, Hessen, Germany
Posts: 16
Rep Power: 8
gerado is on a distinguished road
hi Mark,
thanks for your advice. the problem it is not possible with Foam::system(const string&) to solve my problem.
I want to call this function
fluentMeshToFoam file.msh
where file.msh is the fluent mesh and fluentMeshToFoam convert it to OpenFoam
gerado is offline   Reply With Quote

Old   June 25, 2009, 08:10
Default
  #6
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: http://olesenm.github.io/
Posts: 777
Rep Power: 18
olesen will become famous soon enough
Quote:
Originally Posted by gerado View Post
hi Mark,
thanks for your advice. the problem it is not possible with Foam::system(const string&) to solve my problem.
I want to call this function
fluentMeshToFoam file.msh
where file.msh is the fluent mesh and fluentMeshToFoam convert it to OpenFoam

What problem does the system report when you call the function?
Code:
#include "OSspecific.H"

   system("fluentMeshToFoam file.msh")
/mark
olesen is offline   Reply With Quote

Old   June 25, 2009, 09:23
Default
  #7
New Member
 
Gerard Mouokue
Join Date: Mar 2009
Location: Darmstadt, Hessen, Germany
Posts: 16
Rep Power: 8
gerado is on a distinguished road
Quote:
Originally Posted by olesen View Post
What problem does the system report when you call the function?
Code:
#include "OSspecific.H"

   system("fluentMeshToFoam file.msh")
/mark
when I run my code I have
Fehler: »fluentMeshToFoam« wurde in diesem Gültigkeitsbereich nicht definiert

that means fluentMeshToFoam ist not defined in this scope
gerado is offline   Reply With Quote

Old   June 25, 2009, 09:41
Default
  #8
4xF
New Member
 
Frank Albina
Join Date: Mar 2009
Location: Switzerland
Posts: 14
Rep Power: 8
4xF is on a distinguished road
Send a message via Skype™ to 4xF
This is not very surprising: Your environment variables are not loaded, so the OpenFOAM paths are not available and so is the executable fluentMeshToFoam... What I said earlier: Make sure that the path is set before calling the executable. If I were you, I'd try:
system("<myPathToTheOFInstallationDir>/etc/bashrc; fluentMeshToFoam file.mesh");
4xF is offline   Reply With Quote

Old   June 25, 2009, 09:47
Default
  #9
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: http://olesenm.github.io/
Posts: 777
Rep Power: 18
olesen will become famous soon enough
Quote:
Originally Posted by gerado View Post
when I run my code I have
Fehler: »fluentMeshToFoam« wurde in diesem Gültigkeitsbereich nicht definiert

that means fluentMeshToFoam ist not defined in this scope
Check that fluentMeshToFoam can even be found *before* starting your program. In general you can send just about anything to system(). For example:

Code:
    system("echo PATH=$PATH | sed -e 's/:/\\n/g'");
    system("echo look for fluentMeshToFoam; which fluentMeshToFoam");
olesen is offline   Reply With Quote

Old   June 29, 2009, 02:30
Default
  #10
New Member
 
Gerard Mouokue
Join Date: Mar 2009
Location: Darmstadt, Hessen, Germany
Posts: 16
Rep Power: 8
gerado is on a distinguished road
hi Mark ,Frank,
thanks for your help . it works

Gerard
gerado is offline   Reply With Quote

Reply

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile problem ivanyao OpenFOAM Running, Solving & CFD 1 October 12, 2012 09:31
BlockMesh FOAM warning gaottino OpenFOAM Native Meshers: blockMesh 7 July 19, 2010 14:11
Error with Wmake skabilan OpenFOAM Installation 3 July 28, 2009 00:35
Problem with compile the setParabolicInlet ivanyao OpenFOAM Running, Solving & CFD 6 September 5, 2008 20:50
Axisymmetrical mesh Rasmus Gjesing (Gjesing) OpenFOAM Native Meshers: blockMesh 10 April 2, 2007 14:00


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