Introduction

pyRat is set a set of bindings for Rat C++ libraries. It uses the pybind11 library in order to interface the C++ functions, classes and other objects defined in Rat to the Python programming language.

Note

This is a very broad topic which will not be completely covered in this documentation but feel free to explore it.

For instance it allows to bridge Rat C++ code like this:

solenoid.cpp
// Rat-Template solenoid example:
// <https://gitlab.com/Project-Rat/rat-template/-/blob/master/solenoid.cpp?ref_type=heads>
// (adapted for consiceness)

// DESCRIPTION
// Minimalist example of a solenoid model

#include "rat/common/log.hh"
#include "rat/models/pathcircle.hh"
#include "rat/models/crossrectangle.hh"
#include "rat/models/modelcoil.hh"
#include "rat/models/calcmesh.hh"
int main(int argc, char *argv[]){

    // Input parameters
    const rat::fltp time = 0; // output time [s]
    const rat::fltp radius = 40e-3; // coil inner radius [m]
    const rat::fltp dcoil = 10e-3; // thickness of the coil [m]
    const rat::fltp wcable = 12e-3; // width of the cable [m]
    const rat::fltp delem = 1e-3; // element size [m]
    const arma::uword num_sections = 4; // number of coil sections
    const rat::fltp operating_current = 200; // operating current in [A]
    const arma::uword num_turns = 100; // number of turns

    // Model creation
    const rat::mdl::ShPathCirclePr circle = rat::mdl::PathCircle::create(radius, num_sections, delem);
    circle->set_offset(dcoil);
    const rat::mdl::ShCrossRectanglePr rectangle = rat::mdl::CrossRectangle::create(0, dcoil, 0, wcable, delem);
    const rat::mdl::ShModelCoilPr coil = rat::mdl::ModelCoil::create(circle, rectangle);
    coil->set_number_turns(num_turns);
    coil->set_operating_current(operating_current);

    // Mesh calculation
    const rat::mdl::ShCalcMeshPr mesh_calculation = rat::mdl::CalcMesh::create(coil);
    std::string output_path_argument = "./data";
    const rat::cmn::ShLogPr lg = rat::cmn::Log::create(rat::cmn::Log::LogoType::RAT);
    mesh_calculation->calculate_write({time},output_path_argument,lg);
}

To Python pyRat Python code like this:

solenoid.py
from pyrat import rat
if __name__ == "__main__":

    # Input parameters
    time = 0  # output time [s]
    radius = 40e-3  # coil inner radius [m]
    dcoil = 10e-3  # thickness of the coil [m]
    wcable = 12e-3  # width of the cable [m]
    delem = 1e-3  # element size [m]
    num_sections = 4  # number of coil sections
    operating_current = 200  # operating current in [A]
    num_turns = 100  # number of turns

    # Model creation
    circle = rat.mdl.PathCircle(radius, num_sections, delem)
    rectangle = rat.mdl.CrossRectangle(0, dcoil, 0, wcable, delem)
    coil = rat.mdl.ModelCoil(path_circle, cross_rectangle)
    solenoid.number_turns = num_turns
    solenoid.operating_current = operating_current

    # Mesh calculation
    calc_mesh = rat.mdl.CalcMesh(solenoid)
    output_path_argument = "./data"
    lg = rat.cmn.Log()
    calc_mesh.calculate_write([time], output_dir, lg)

Obviously a lot is going on under the hood to make this work, and it is the job of pyRat with the help of the pybind11 framework.

The bridges can be summarized by the following diagram:

        ---
align: center
alt: Python to C++ communication
name: python_to_cpp_communication
config:
    theme: 'neutral'
---

    flowchart TB
        PT(Python code: solenoid.py)
        PRPM(pyRat Python module: pyrat.rat)

        subgraph Rat["Rat C++ libraries"]
            direction LR
            RCMN(rat-common.so)
            MID("(other libraries)")
            RMDL(rat-models.so)

            RCMN --- MID --- RMDL
        end

        PT <==>|Python| PRPM
        PRPM <==>|C++| Rat