Reputation: 11
I write a code in python and I have a function (f, written in Freefem++) that the inputs come from the code in python. the problem is that I don't know how to call that function written in freefem++, in my python code.
This code is in python and the function "f", assumes to be in Freefem++.
for i in range (popsize):
pop[i][N]=f(pop[i][0],pop[i][1])
Thank you in advance.
Upvotes: 1
Views: 1506
Reputation: 484
The answer of A.G. is good but I want to remark, that you can make use of pipes, that allows you to communicate between two programs without the use of files, which are a bottle neck. See for example here Pipeline (Wikipedia)
Upvotes: 0
Reputation: 13
Freefem++ is written in C++ and to my knowledge, there is no API written in C++ to allow communications between Freefem++ and Python.
What I would advise is the following:
Write your FreeFem++ code and imbed in your code a section to save the data to a temporary file. It is also possible to directly "cout" the result and to read the standard output from Python using the subprocess
module.
Call your FreeFem code from python. Say the code is name "simulation.edp", then from python you could write your code as:
import subprocess
dep_file_path="simulation.edp"
bashCommand = ["Freefem++", edp_file_path]
process = subprocess.Popen(bashCommand, stdout=subprocess.PIPE)
output, error = process.communicate()
print(output)
This way, you should be able to read the output of the FreeFem simulation by executing the python code.
You could try a more direct approach by working in C++ but this path is much harder due to the lack of good documentation on the development of FreeFem++ kernel. Here, you will find some examples of how to interface simple C++ classes with FreeFem++ Link to Githb.
Upvotes: 0