Module pytop.utils

Utilites for pytop, including: bridging between fenics variables to numpy array, or vice versa.

Functions

def create_initialized_fenics_function(fields: list[typing.Callable[[typing.Iterable], float]], function_space: dolfin.function.functionspace.FunctionSpace) ‑> fenics_adjoint.types.function.Function

Return a fenics function defined on the functionspace with values assigned. Elements of fields are assumed to be the following pyfunction:

value1 = lambda x: f(x[0], x[1], ..., x[n]) # n is the dimension of the Function space.
value2 = lambda x: g(x[0], x[1], ..., x[n]) 
create_initialized_fenics_function([value1, value2], functionspace) # The rank of the functionspace and length of the values must be the same.

if the element is not a function but a constant value, it is assumed to be a constant value.

create_initialized_fenics_function([1.0, 1.0], functionspace)

Args: (list, Function) values: list of values to be assigned. functionspace: fenics function space.

Raises

TypeError
if the input is not a list.

Returns: (Function) fenics function.

def fenics_function_to_np_array(fenics_variable: fenics_adjoint.types.constant.Constant | fenics_adjoint.types.function.Function | dolfin.cpp.la.GenericVector) ‑> numpy.ndarray

Convert fenics variable to numpy array.

Args: (Constant | Function | GenericVector) fenics_variable: fenics values to be converted.

Raises

TypeError
if the input is not a fenics vector.

Returns: (np.ndarray) numpy array.

def from_pygmsh(mesh: meshio._mesh.Mesh, planation: bool = False, save_for: Optional[str] = None, mpi_comm: Optional[MPI_Communicator] = None) ‑> fenics_adjoint.types.mesh.Mesh

Convert a pygmesh mesh to a fenics mesh.

Args: (pygmesh.Mesh) mesh: pygmesh mesh. planation (Optional): whether to planate the mesh. save_for (Optional): path to save the mesh. mpi_comm (Optional): MPI communicator.

Returns: (Mesh) fenics mesh.

def import_external_mesh(mesh_file: str, planation=False, mpi_comm: Optional[MPI_Communicator] = None) ‑> fenics_adjoint.types.mesh.Mesh

Import a mesh from a file. Time series XDMF files are not supported yet.

Args: (str) mesh_file: path to the mesh file. planation (Optional): whether to planate the mesh. mpi_comm (Optional): MPI communicator.

Returns: (Mesh) fenics mesh.

This function used the meshio library to read the mesh file and then convert it to a fenics mesh.

def make_noiman_boundary_domains(mesh: fenics_adjoint.types.mesh.Mesh, subdomains: Iterable[dolfin.cpp.mesh.SubDomain], initialize=False) ‑> ufl.measure.Measure

Create a Measure object for the boundary domains.

Args: (Mesh, Iterable[SubDomain], bool) mesh: fenics mesh. subdomains: list of subdomains. initialize: whether to initialize the boundary markers.

Returns: (Measure) Measure object for the boundary domains.

def np_array_to_fenics_function(np_array: numpy.ndarray, fenics_function: fenics_adjoint.types.function.Function) ‑> fenics_adjoint.types.function.Function

Convert numpy array to fenics variable.

Args: (np.ndarray, Function)

np_array: numpy array to be converted.
fenics_function: fenics function to be assigned.

Raises

TypeError
if the input is not a numpy array.
ValueError
if the input numpy array is not of the same size as the fenics vector.

Returns: (Function) fenics variable.

def read_fenics_function_from_file(file_name: str, fenics_function_space: dolfin.function.functionspace.FunctionSpace, fenics_function_name: Optional[str] = None) ‑> fenics_adjoint.types.function.Function
def save_fenics_function_to_file(fenics_variable: fenics_adjoint.types.function.Function, file_name: str, fenics_function_name: Optional[str] = None, with_xml: Optional[bool] = False, with_vtu: Optional[bool] = False, mpi_comm: Optional[MPI_Communicator] = None) ‑> None

Save a fenics variable to a xdmf file.

Args: (Function, str) fenics_variable: fenics variable to be saved. file_name: path to the file. fenics_function_name (Optional): name of the fenics function. with_xml (Optional): whether to save the xml file. with_vtu (Optional): whether to save the vtu file. mpi_comm (Optional): MPI communicator.

def set_fields_to_fenics_function(fields: list[typing.Callable[[typing.Iterable], float]], function: fenics_adjoint.types.function.Function) ‑> None

Set values for a fenics function. Elements of fields are assumed to be the followig pyfunction:

value1 = lambda x: f(x[0], x[1], ..., x[n]) # n is the dimension of the Function space.
value2 = lambda x: g(x[0], x[1], ..., x[n]) 
set_fields_to_fenics_function([value1, value2], function_space) # The rank of the function_space and length of the values must be the same.

if the element is not a function but a constant value, it is assumed to be a constant value.

set_fields_to_fenics_function([1.0, 1.0], function_space)

Args: (list, Function) values: list of values to be assigned. function: fenics function to be assigned.

Raises

TypeError
if the input is not a list.

Classes

class MPI_Communicator

MPI communicator for parallel computing.

Attributes

comm_world : MPI_Comm
MPI communicator.
rank : int
Rank of the process.
size : int
Number of processes.
Expand source code
@dataclass
class MPI_Communicator:
    '''MPI communicator for parallel computing.
    
    Attributes:
        comm_world (MPI_Comm): MPI communicator.
        rank (int): Rank of the process.
        size (int): Number of processes.
    '''
    comm_world = MPI.comm_world
    rank = MPI.comm_world.rank
    size = MPI.comm_world.size

Class variables

var comm_world
var rank
var size