How PyFreeFEM works¶
PyFreeFEM operates through the FreeFemRunner class
which is used to
read
.edpscripts from source files or code written in python strings:pythonfrom pyfreefem import FreeFemRunner # Loading the script "foo.edp" runner = FreeFemRunner("foo.edp") # Loading directly FreeFEM code script = """mesh Th=square(10,10);""" runner = FreeFemRunner(script)execute
.edpscripts with the methodexecute():
# Execute the script
runner.execute()
PyFreeFEM
offers a flexible interfacing between FreeFEM and Python by
supporting special instructions that can be added in .edp scripts.
For instance,
the following code which defines a function returning
a square mesh with an input number N of boundary elements:
from pyfreefem import FreeFemRunner
def square(N):
code = """
IMPORT "io.edp"
DEFAULT (N,10)
mesh Th=square($N,$N);
exportMesh(Th);"""
runner=FreeFemRunner(code,{'N':N})
return runner.execute()['Th']
Calling the method execute() does the
following steps:
A temporary running directory is created (for instance
/tmp/pyfreefem_l2zm1dqj/), which is removed oncerunneris destroyed.The code is parsed into a valid
.edpfileinclude("/tmp/pyfreefem_l2zm1dqj/io.edp") mesh Th=square(30,30); exportMesh(Th);which is saved to the temporary directory. These parsing operations are achieved with the
Preprocessormodule.Macro files such as
io.edpare also stored in the temporary directory, as well as imported python data structures.Finally, the
.edpscript is executed in a Popen subprocess with the commandFreeFEM++.