Modulus Utilities

Input / Ouput

Defines helper Plotter class for adding plots to tensorboard summaries

class modulus.utils.io.plotter.DeepONetValidatorPlotter(n_examples: int = 1)[source]

Bases: _Plotter

DeepONet validation plotter for structured data

class modulus.utils.io.plotter.GridValidatorPlotter(n_examples: int = 1)[source]

Bases: _Plotter

Grid validation plotter for structured data

class modulus.utils.io.plotter.InferencerPlotter[source]

Bases: _Plotter

Default plotter class for inferencer

class modulus.utils.io.plotter.ValidatorPlotter[source]

Bases: _Plotter

Default plotter class for validator

Helper functions for generating vtk files

class modulus.utils.io.vtk.VTKFromFile(file_path: str, export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', force_legacy: bool = False)[source]

Bases: object

Reads VTK file into memory and constructs corresponding VTK object

Parameters
  • file_path (str) – File directory/name of input vtk file

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • force_legacy (bool, optional) – Force a legacy only read, by default False

class modulus.utils.io.vtk.VTKPolyData(points: array, line_index: Optional[array] = None, poly_index: Optional[Tuple[array, array]] = None, export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', init_vtk: bool = True)[source]

Bases: VTKBase

vtkPolyData wrapper class

Parameters
  • points (np.array) – Array of point locations [npoints, (1,2 or 3)]

  • line_index (np.array, optional) – Array of line connections [nedges, 2], by default None

  • poly_index (Tuple[poly_offsets, poly_connectivity]) – Tuple of polygon offsets and polygon connectivity arrays. Polygon offsets is a 1D array denoting how many points make up a face for each polygon. Polygon connectivity is a 1D array that contains verticies of each polygon face in order, by default None

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • init_vtk (bool, optional) – Initialize new VTK object from parameters (used by VTKFromFile), by default True

class modulus.utils.io.vtk.VTKRectilinearGrid(axis_coords: List[array], export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', init_vtk: bool = True)[source]

Bases: VTKBase

vtkRectilinearGrid wrapper class

Parameters
  • axis_coords (List[np.array]) – List of arrays that define points on each axis

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • init_vtk (bool, optional) – Initialize new VTK object from parameters (used by VTKFromFile), by default True

class modulus.utils.io.vtk.VTKStructuredGrid(points: array, dims: List[int], export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', init_vtk: bool = True)[source]

Bases: VTKBase

vtkStructuredGrid wrapper class

Parameters
  • points (np.array) – Mesh grid of points in ‘ij’ format

  • dims (List[int]) – Number of points in each dimension

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • init_vtk (bool, optional) – Initialize new VTK object from parameters (used by VTKFromFile), by default True

class modulus.utils.io.vtk.VTKUniformGrid(bounds: List[List[int]], npoints: List[int], export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', init_vtk: bool = True)[source]

Bases: VTKBase

vtkUniformGrid wrapper class

Parameters
  • bounds (List[List[int]]) – Domain bounds of each dimension

  • npoints (List[int]) – List of number of points in each dimension

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • init_vtk (bool, optional) – Initialize new VTK object from parameters (used by VTKFromFile), by default True

class modulus.utils.io.vtk.VTKUnstructuredGrid(points: array, cell_index: Tuple[array, array], cell_types: array, export_map: Dict[str, List[str]] = {}, file_name: str = 'vtk_output', file_dir: str = '.', init_vtk: bool = True)[source]

Bases: VTKBase

vtkUnstructuredGrid wrapper class

Parameters
  • points (np.array) – Array of point locations [npoints, (1,2 or 3)]

  • cell_index (Tuple[ np.array, np.array ]) – Tuple of (cell_offsets, cell_connectivity) arrays. Cell offsets is a 1D array denoting how many points make up a face for each cell. Cell connectivity is a 1D array that contains verticies of each cell face in order

  • cell_types (np.array) – Array of cell vtk types: https://vtk.org/doc/nightly/html/vtkCellType_8h_source.html

  • export_map (Dict[str, List[str]], optional) – Export map dictionary with keys that are VTK variables names and values that are lists of output variables. Will use 1 to 1 mapping if none is provided, by default {}

  • file_name (str, optional) – File name of output vtk file, by default “vtk_output”

  • file_dir (str, optional) – File directory of output vtk file, by default “.”

  • init_vtk (bool, optional) – Initialize new VTK object from parameters (used by VTKFromFile), by default True

modulus.utils.io.vtk.grid_to_vtk(var_dict: Dict[str, array], file_path: str, batch_index: int = 0)[source]

Helper method for nodes to export image/grid data to vtkUniformData file. Arrays should be in the numpy ‘ij’ layout (element [0,0] is origin)

Parameters
  • var_dict (Dict[str, np.array]) – Dictionary of variables in the array format [batch, dim, xdim, ydim, zdim]

  • file_path (str) – File directory/name of output vtk file

  • batch_index (int, optional) – Batch index to write to file, by default 0

modulus.utils.io.vtk.var_to_polyvtk(var_dict: Dict[str, array], file_path: str, coordinates=['x', 'y', 'z'])[source]

Helper method for nodes to export thier variables to a vtkPolyData file Should be avoided when possible as other VTK formats can save on memory.

Parameters
  • var_dict (Dict[str, np.array]) – Dictionary of variables in the array format [nstates, dim]

  • file_path (str) – File directory/name of output vtk file

  • coordinates (list, optional) – Variable names that corresponds to point positions, by default [“x”, “y”, “z”]

simple helper functions for reading and saving CSV files

modulus.utils.io.csv_rw.csv_to_dict(filename, mapping=None, delimiter=',')[source]

reads a csv file to a dictionary of columns

Parameters
  • filename (str) – The file name to load from

  • mapping (None, dict) – If None load entire csv file and store every column as a key in the dict. If mapping is not none use this to map keys from CSV to keys in dict.

  • delimiter (str) – The string used for separating values.

Returns

data – numpy arrays have shape [N, 1].

Return type

dict of numpy arrays

modulus.utils.io.csv_rw.dict_to_csv(dictonary, filename)[source]

saves a dict of numpy arrays to csv file

Parameters
  • dictionary (dict) – dictionary of numpy arrays. The numpy arrays have a shape of [N, 1].

  • filename (str) – The file name to save too

Sympy

simple Sympy helper functions

modulus.utils.sympy.functions.line(x, point_x_1, point_y_1, point_x_2, point_y_2)[source]

line function from point intercepts

Parameters
  • x (Sympy Symbol/Exp) – the x in equation y=a*x+b

  • point_x_1 (Sympy Symbol/Exp, float, int) – first intercept x position

  • point_y_1 (Sympy Symbol/Exp, float, int) – first intercept y position

  • point_x_2 (Sympy Symbol/Exp, float, int) – second intercept x position

  • point_y_2 (Sympy Symbol/Exp, float, int) – second intercept y position

Returns

yy=slope*x+intercept

Return type

Sympy Expr

modulus.utils.sympy.functions.parabola(x, inter_1, inter_2, height)[source]

parabola from point intercepts

Parameters
  • x (Sympy Symbol/Exp) – the x in equation y=a*x*2+b*x+c

  • inter_1 (Sympy Symbol/Exp, float, int) – first intercept such that y=0 when x=inter_1

  • inter_2 (Sympy Symbol/Exp, float, int) – second intercept such that y=0 when x=inter_1

  • height (Sympy Symbol/Exp, float, int) – max height of parabola

Returns

yy=factor*(x-inter_1)*(x-+inter_2)

Return type

Sympy Expr

modulus.utils.sympy.functions.parabola2D(x, y, inter_1_x, inter_2_x, inter_1_y, inter_2_y, height)[source]

square parabola from point intercepts

Parameters
  • x (Sympy Symbol/Exp) – the x in equation z=parabola(x)*parabola(y)

  • y (Sympy Symbol/Exp) – the y in equation z=a*x**2+b*y**2+c*xy+d*y+e*x+f

  • inter_1_x (Sympy Symbol/Exp, float, int) – first intercept such that z=0 when x=inter_1_x

  • inter_2_x (Sympy Symbol/Exp, float, int) – second intercept such that z=0 when x=inter_2_x

  • inter_1_y (Sympy Symbol/Exp, float, int) – first intercept such that z=0 when y=inter_1_y

  • inter_2_y (Sympy Symbol/Exp, float, int) – second intercept such that z=0 when y=inter_2_y

  • height (Sympy Symbol/Exp, float, int) – max height of parabola

Returns

yy=factor*(x-inter_1)*(x-+inter_2)

Return type

Sympy Expr

Helper functions for converting sympy equations to numpy

modulus.utils.sympy.numpy_printer.np_lambdify(f, r)[source]

generates a numpy function from a sympy equation

Parameters
  • f (Sympy Exp, float, int, bool or list of the previous) – the equation to convert to a numpy function. If float, int, or bool this gets converted to a constant function of value f. If f is a list then output for each element in list is is concatenated on axis -1.

  • r (list, dict) – A list of the arguments for f. If dict then the keys of the dict are used.

Returns

np_f

Return type

numpy function

Helper functions for converting sympy equations to pytorch

class modulus.utils.sympy.torch_printer.CustomDerivativePrinter(settings=None)[source]

Bases: StrPrinter

class modulus.utils.sympy.torch_printer.SympyToTorch(sympy_expr, name: str, freeze_terms: List[int] = [], detach_names: List[str] = [])[source]

Bases: Module

forward(var: Dict[str, Tensor]) Dict[str, Tensor][source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

modulus.utils.sympy.torch_printer.torch_lambdify(f, r, separable=False)[source]

generates a PyTorch function from a sympy equation

Parameters
  • f (Sympy Exp, float, int, bool) – the equation to convert to torch. If float, int, or bool this gets converted to a constant function of value f.

  • r (list, dict) – A list of the arguments for f. If dict then the keys of the dict are used.

Returns

torch_f

Return type

PyTorch function

Variational PINNs

Helper functions and classes for integration

class modulus.utils.vpinn.integral.Quad_Rect(v, n, region_type=True, scheme_fcn=quadpy.c2.get_good_scheme)[source]

Bases: Quadrature

The points are specified in an array of shape (2, 2, …) such that arr[0][0] is the lower left corner, arr[1][1] the upper right, and set region_type=False. If your c2 has its sides aligned with the coordinate axes, you can use v=[[x0, x1], [y0, y1]], and set region_type=True (default).

Helper functions for and classes for making test functions used in VPINNs