PhysicsNeMo Sym Utilities#
Input / Ouput#
Defines helper Plotter class for adding plots to tensorboard summaries
- class physicsnemo.sym.utils.io.plotter.DeepONetValidatorPlotter(n_examples: int = 1)[source]#
Bases:
_Plotter
DeepONet validation plotter for structured data
- class physicsnemo.sym.utils.io.plotter.GridValidatorPlotter(n_examples: int = 1)[source]#
Bases:
_Plotter
Grid validation plotter for structured data
- class physicsnemo.sym.utils.io.plotter.InferencerPlotter[source]#
Bases:
_Plotter
Default plotter class for inferencer
- class physicsnemo.sym.utils.io.plotter.ValidatorPlotter[source]#
Bases:
_Plotter
Default plotter class for validator
Helper functions for generating vtk files
- class physicsnemo.sym.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,
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 physicsnemo.sym.utils.io.vtk.VTKPolyData(
- points: array,
- line_index: array = None,
- poly_index: Tuple[array, array] = None,
- export_map: Dict[str, List[str]] = {},
- file_name: str = 'vtk_output',
- file_dir: str = '.',
- init_vtk: bool = True,
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 vertices 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 physicsnemo.sym.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,
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 physicsnemo.sym.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,
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 physicsnemo.sym.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,
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 physicsnemo.sym.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,
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 vertices 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
- physicsnemo.sym.utils.io.vtk.grid_to_vtk(
- var_dict: Dict[str, array],
- file_path: str,
- batch_index: int = 0,
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
- physicsnemo.sym.utils.io.vtk.var_to_polyvtk(
- var_dict: Dict[str, array],
- file_path: str,
- coordinates=['x', 'y', 'z'],
Helper method for nodes to export their 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
- physicsnemo.sym.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
Sympy#
simple Sympy helper functions
- physicsnemo.sym.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:
y – y=slope*x+intercept
- Return type:
Sympy Expr
- physicsnemo.sym.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:
y – y=factor*(x-inter_1)*(x-+inter_2)
- Return type:
Sympy Expr
- physicsnemo.sym.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:
y – y=factor*(x-inter_1)*(x-+inter_2)
- Return type:
Sympy Expr
Helper functions for converting sympy equations to numpy
- physicsnemo.sym.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 physicsnemo.sym.utils.sympy.torch_printer.CustomDerivativePrinter(settings=None)[source]#
Bases:
StrPrinter
- class physicsnemo.sym.utils.sympy.torch_printer.SympyToTorch(
- sympy_expr,
- name: str,
- freeze_terms: List[int] = [],
- detach_names: List[str] = [],
Bases:
Module
- forward(
- var: Dict[str, Tensor],
Define 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.
- physicsnemo.sym.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 physicsnemo.sym.utils.vpinn.integral.Quad_Rect(
- v,
- n,
- region_type=True,
- scheme_fcn=quadpy.c2.get_good_scheme,
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