Module pyfreefem.io

PyFreeFEM input/output functions.

exception ExecException(message, returncode, stdout, stderr, mix)

An exception raised when a FreeFem++ subprocess fails

display(message, level=0, debug=0, color=None, attr=None, end='\n', flag=None)

A function for displaying messages with a flexible tunable level of verbosity.

Parameters:
  • message – Text to be printed.

  • level (int) – Level of importance of the message which will be actually printed if debug >= level or if flag is stdout or stderr.

  • debug (int) – Desired level of verbosity; the higher and the more information will be displayed.

  • color – Color of the displayed message. Follows the convention of the colored package.

  • attr

    Formatting of the displayed message. Follows the convention of the colored package.

  • end – character appended to the end of a message. Will remove the final line carriage return if end=''.

  • flag – an indicator to use to indicate if the argument message comes from stdout or stderr pipe.

Note

For advanced projects, it can be useful to override pyfreefem.io.display(), for instance to redirect messages to a file.

exec2(cmd, debug=0, level=1, silent=True)

Interface with subprocess.Popen. Execute a shell command cmd and pass standard output and standard errors to the function pyfreefem.io.display().

Parameters:
  • cmd (str) – A shell command. For instance cmd="FreeFem++ script.edp -v 1 -wg".

  • debug (int) – An input debug parameter, tuning the desired level of verbosity. The command cmd will be displayed if debug>=level

  • level (int) – Degree of importance of the executed command. The command cmd will be displayed if debug>=level

  • silent (boolean) – If set to True, no standard output will be displayed.

Note

By default, the standard output is displayed whatever the value of level and debug because pyfreefem.io.display() prints stdout and stderr by watching its flag argument. Use a custom pyfreefem.io.display() function to change this behaviour.

readFF2DArray(filepath, complex_array=False)

Read a matrix file generated by a FreeFEM script (a real[int,int]). If the matrix stored by the FreeFEM script is a sparse matrix, then pyfreefem.io.readFFMatrix() returns a scipy.sparse.csc_matrix.

If it is a dense matrix , then pyfreefem.io.readFFMatrix() returns a numpy.ndarray.

Parameters:

filepath – file name of a FreeFEM matrix stored with ifstream.

Returns:

A numpy.ndarray matrix if ffmatrix contains a dense matrix, or a scipy.sparse.csc_matrix if ffmatrix contains a sparse matrix.

readFFArray(ffarray: str, binary=False, integer=False, complex_array=False)

Read an FreeFEM array stored in a file ffarray.

For instance, if the following .edp file is executed:

freefem
real[int] table = [1, 2, 3, 4, 5];
{
    ifstream f("file.gp");
    f << table;
}

Then

python
readFFArray("file.gp") 

returns the numpy array [1, 2, 3, 4, 5].

Parameters:

ffarray – file name of a FreeFEM array stored with ifstream.

Returns:

numpy array identical to ffarray.

readFFMatrix(ffmatrix: str)

Read a matrix file generated by a FreeFEM script. For instance, if the following .edp file is run:

freefem
matrix table = [[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]];
{
    ifstream f("file.gp");
    f << table;
}

Then

python
readFFMatrix("file.gp") 

returns the numpy matrix [[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]].

If the matrix stored by the FreeFEM script is a sparse matrix, then pyfreefem.io.readFFMatrix() returns a scipy.sparse.csc_matrix.

If it is a dense matrix, then pyfreefem.io.readFFMatrix() returns a numpy.ndarray.

Parameters:

ffmatrix – file name of a FreeFEM matrix stored with ifstream.

Returns:

A numpy.ndarray matrix if ffmatrix contains a dense matrix, or a scipy.sparse.csc_matrix if ffmatrix contains a sparse matrix.

readFFMatrixBinary(filename, complex=False)

Matrix binary file reader from MatrixMarket plugin. Supports real and complex sparse matrices.

readPetscMat(filepath)

Read a sparse matrix stored by PetsC ObjectView

writeFF2DArray(A, filename, complex=False)

Save a dense matrix into a FreeFEM matrix file (a real[int,int]) that can be read with ofstream.

Parameters:
writeFFArray(A: ndarray, fileName: str, binary=False, integer=False, complex=False)

Store a numpy array into a FreeFEM array file that can be read with ofstream.

Parameters:
  • A – a numpy.array data structure

  • fileName – an output file name to save the array A.

writeFFMatrix(A, fileName)

Save a dense or a sparse matrix into a FreeFEM matrix file that can be read with ofstream.

Parameters: