CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   automatic call a function in a program (http://www.cfd-online.com/Forums/openfoam/65635-automatic-call-function-program.html)

gerado June 22, 2009 03:38

automatic call a function in a program
 
Hi,
I have a program and want to call automatically another function in my program . for example

int main (){

myprogram
gambitToFoam "gambitmesh"
return 0;
}

4xF June 22, 2009 05:54

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.

gerado June 25, 2009 02:36

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 .

olesen June 25, 2009 07:13

Quote:

Originally Posted by 4xF (Post 220034)
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.

gerado June 25, 2009 08:05

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

olesen June 25, 2009 08:10

Quote:

Originally Posted by gerado (Post 220452)
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

gerado June 25, 2009 09:23

Quote:

Originally Posted by olesen (Post 220454)
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

4xF June 25, 2009 09:41

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");

olesen June 25, 2009 09:47

Quote:

Originally Posted by gerado (Post 220468)
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");


gerado June 29, 2009 02:30

hi Mark ,Frank,
thanks for your help . it works

Gerard


All times are GMT -4. The time now is 01:28.