Module pyrat¶
The pyRat Python module pyrat
is split into several submodules.
Python and C++¶
The pyrat.rat submodule is the only submodule using C++.
All other submodules are written in pure Python
code within the pyrat/src/pyrat directory and are
mostly helpers and tools for comfort using pyrat.rat.
CLI : pyrat.cli¶
The pyrat command (also python -m pyrat) shows
versions and generates the pyRat examples defined in the
pyrat/src/pyrat/examples directory.
pyrat --version # logo (in a terminal), pyRat, RAT and CUDA versions
pyrat -e # list the examples
pyrat -e moon # generate examples/rat/dm/moon.py
pyrat -e mdl moon # every Rat-Models example, and moon
pyrat -e mdl/canted # every example under mdl/canted
pyrat -e "*cct*" # a glob, quoted so that the shell leaves it alone
pyrat -e rat-template # generate examples/rat/rat-template/rat_template.py
pyrat -e all # every example
A pattern is an example name (with or without .py;
- and _ are interchangeable), a directory (every
example under it), or a glob matched anywhere under the
examples. --examples is the long form of -e.
Examples are generated in the current directory under the same layout as in the package, with the input files they read (CSV tables, JSON geometries), so run them from their own directory:
examples/
└── rat/
├── dm/ ← Rat-Distmesh
├── fmm/ ← Rat-MLFMM
├── mat/ ← Rat-Materials
├── mdl/ ← Rat-Models
│ ├── asbuild/
│ ├── cabling/
│ ├── canted/
│ ├── data/ ← input files of some examples
│ ├── dmesher/
│ ├── nlsolver/
│ ├── other/
│ ├── pm/
│ └── simple/
├── nl/ ← Rat-NL
└── rat-template/
Color : pyrat.col¶
The pyrat.col color submodule can be
used for simple message formatting, mimicking
terminal color definitions from the
rat-common/include/typedefs.hh header
file with some additional escape sequences.
from pyrat.col import *
print(f"{KYEL}WARNING:{KNRM} Colors are nice.")
Log : pyrat.log¶
pyRat logs with Python’s standard logging module, to
the "pyrat" logger. Like any library, it is silent until
the application shows it:
import pyrat.log
log = pyrat.log.enable() # print pyRat messages, INFO and up, to stderr
log.info("Mesh ready") # pyRat INFO: Mesh ready
log = pyrat.log.get("my_script") # a logger of your own, shown the same way
log.warning("Coarse mesh") # pyRat [my_script] WARNING: Coarse mesh
enable(level, stream) can be called again to change the
level or the stream. An application that configures
logging itself does not need it: the "pyrat" logger
follows its settings.
Messages from RAT itself go through the rat.cmn.Log
passed to its calls, which writes to Python’s sys.stdout:
they show in Jupyter and can be captured like any print
(contextlib.redirect_stdout, pytest).
Note
pyRat 2.0.0 removed pyrat.log.config(), the
HINT and RESULT levels and the logs/ file:
use pyrat.log.enable() and pyrat.log.get(name).
Meta : pyrat.meta¶
The pyrat.meta metadata submodule gathers
information about the pyrat Python module:
VERSION, CUDA_VERSION (the CUDA major version
RAT was built with, None without CUDA), license,
URLs, and the ASCII logos (print_logo).
Typing : pyrat.typ¶
The pyrat.typ typing submodule defines the NumPy array
types that signatures and stubs use: Vec[T], Mat[T],
Cub[T], VecN[N, T] (a vector of fixed length N)
and Field[T]. They can type hint scripts as well:
import numpy as np
from pyrat.typ import Mat
def centre(coords: Mat[np.float64]) -> Mat[np.float64]:
return coords.mean(axis=1, keepdims=True)
Rat : pyrat.rat¶
The pyrat.rat core module contains the
binding to Rat C++ libraries made with pybind11.
It is of course the heart of the magnetic simulations
and bridges Python code to the original C++ code.
It holds one submodule per Rat library: cmn
(Rat-Common), math, dm (Rat-Distmesh), mat
(Rat-Materials), fmm (Rat-MLFMM), nl (Rat-NL) and
mdl (Rat-Models), plus arma for Armadillo internals
(e.g. arma.arma_rng to seed its random generator).
Errors¶
Errors raised by RAT are
pyrat.rat.RatError, aRuntimeError.Armadillo size and argument errors are
ValueError, out-of-bounds errorsIndexError.Reading a write-only property raises
AttributeError, sohasattr()and IDE inspection work.
Python behaviour¶
RAT nodes (
rat.cmn.Nodeand every class deriving from it) can be pickled and copied (copy.copy,copy.deepcopy) through RAT’s serializer: models can be sent tomultiprocessingorProcessPoolExecutorworkers.Long calls (
setup,calculate*,create_meshes, solvers, …) release the GIL while they run: other Python threads (Jupyter, GUIs, thread pools) keep running. Ctrl+C takes effect once the call returns.A Python subclass of a RAT class can override its methods, but RAT (C++) keeps calling its own implementation: a
RuntimeWarningsays so.Vector-shaped fields (1 x N or N x 1) come back as flat lists; matrices come back as NumPy arrays.