Formatters module

This module contains all the languages formatters used to convert symbolic expressions into computing language expressions.

Base classes for formatting symbolic equations output

Defines base classes to format tendencies and Jacobian symbolic equations output.

Description of the classes

class layercake.formatters.base.EquationFormatter(lang_translation=None, split_lines=False)[source]

Bases: ABC

Base class for symbolic equations formatting.

Parameters:
  • lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the target language.

  • split_lines (bool, optional) – Split the lines if needed, using the splitter static function. Default to False.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the target language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. Defaults to 0.

Type:

int

split_lines

If True, split the lines if needed, using the splitter static function.

Type:

bool

__call__(tensor, variable='U', tendencies='F')[source]

Convert a model symbolic tendencies terms tensor to a list of symbolic equations in string format.

Parameters:
  • tensor (ImmutableSparseNDimArray) – Symbolic tendencies terms tensor to convert.

  • variable (str, optional) – Name of the state variable to use for the output equations strings. Default to ‘U’.

  • tendencies (str, optional) – Name of the tendencies variable to use for the output equations strings. Default to ‘F’.

abstract property closing_character

Character closing the arrays specification index in the target language. Must be defined in the subclasses.

Type:

str

abstract property opening_character

Character opening the arrays specification index in the target language. Must be defined in the subclasses.

Type:

str

static splitter(line, **kwargs)[source]

Function to split a line of code that is too long.

Parameters:
  • line (str) – Line to be split.

  • **kwargs (dict) – Additional keyword arguments to be passed to the splitter function.

Returns:

The split line.

Return type:

str

class layercake.formatters.base.JacobianEquationFormatter(lang_translation=None, split_lines=False)[source]

Bases: EquationFormatter

Base class for symbolic Jacobian equations formatting.

Parameters:
  • lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the target language.

  • split_lines (bool, optional) – Split the lines if needed, using the splitter static function. Default to False.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the target language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. Defaults to 0.

Type:

int

split_lines

If True, split the lines if needed, using the splitter static function.

Type:

bool

__call__(tensor, variable='U', tendencies='J', split_lines=False)[source]

Convert a model Jacobian symbolic tendencies terms tensor to a list of symbolic equations in string format.

Parameters:
  • tensor (ImmutableSparseNDimArray) – Jacobian symbolic tendencies terms tensor to convert.

  • variable (str) – Name of the state variable to use for the output equations strings. Default to ‘U’.

  • tendencies (str) – Name of the tendencies variable to use for the output equations strings. Default to ‘F’.

  • split_lines (bool, optional) – Split the lines if needed. Default to False.

Classes for formatting symbolic equations output in Fortran

Defines classes to format tendencies and Jacobian symbolic equations output in Fortran.

Description of the classes

class layercake.formatters.fortran.FortranEquationFormatter(lang_translation=None, split_lines=True)[source]

Bases: EquationFormatter

Class for symbolic equations formatting in Fortran.

Parameters:
  • lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Fortran language.

  • split_lines (bool, optional) – Split the lines if needed, using the splitter static function. Default to True.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Fortran language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Fortran the base index is 1.

Type:

int

split_lines

If True, split the lines if needed, using the splitter static function.

Type:

bool

property closing_character

Character closing the arrays specification index in the Fortran language.

Type:

str

property opening_character

Character opening the arrays specification index in the Fortran language.

Type:

str

static splitter(line, **kwargs)[source]

Function to split a line of code that is too long.

Parameters:
  • line (str) – Line to be split.

  • **kwargs (dict) – Additional keyword arguments to be passed to the splitter function.

Returns:

The split line.

Return type:

str

class layercake.formatters.fortran.FortranJacobianEquationFormatter(lang_translation=None, split_lines=True)[source]

Bases: JacobianEquationFormatter

Class for symbolic Jacobian equations formatting in Fortran.

Parameters:
  • lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Fortran language.

  • split_lines (bool, optional) – Split the lines if needed, using the splitter static function. Default to True.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Fortran language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Fortran the base index is 1.

Type:

int

split_lines

If True, split the lines if needed, using the splitter static function.

Type:

bool

property closing_character

Character closing the arrays specification index in the Fortran language.

Type:

str

property opening_character

Character opening the arrays specification index in the Fortran language.

Type:

str

static splitter(line, **kwargs)[source]

Function to split a line of code that is too long.

Parameters:
  • line (str) – Line to be split.

  • **kwargs (dict) – Additional keyword arguments to be passed to the splitter function.

Returns:

The split line.

Return type:

str

layercake.formatters.fortran.fortran_line_splitter(line, line_length=80)[source]

Function to split a line of Fortran code that is too long over multiple lines.

Parameters:
  • line (str) – Line to be split.

  • line_length (int, optional) – Length of the resulting split line. Default is 80.

Returns:

The split line.

Return type:

str

Classes for formatting symbolic equations output in Python

Defines classes to format tendencies and Jacobian symbolic equations output in Python.

Description of the classes

class layercake.formatters.python.PythonEquationFormatter(lang_translation=None)[source]

Bases: EquationFormatter

Class for symbolic equations formatting in Python.

Parameters:

lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Python language.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Python language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Python the index base is 0.

Type:

int

property closing_character

Character closing the arrays specification index in the Python language.

Type:

str

property opening_character

Character opening the arrays specification index in the Python language.

Type:

str

class layercake.formatters.python.PythonJacobianEquationFormatter(lang_translation=None)[source]

Bases: JacobianEquationFormatter

Class for symbolic Jacobian equations formatting in Python.

Parameters:

lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Python language.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Python language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Python the index base is 0.

Type:

int

property closing_character

Character closing the arrays specification index in the Python language.

Type:

str

property opening_character

Character opening the arrays specification index in the Python language.

Type:

str

Classes for formatting symbolic equations output in Julia

Defines classes to format tendencies and Jacobian symbolic equations output in Julia.

Description of the classes

class layercake.formatters.julia.JuliaEquationFormatter(lang_translation=None)[source]

Bases: EquationFormatter

Class for symbolic equations formatting in Julia.

Parameters:

lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Julia language.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Julia language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Julia the index base is 1.

Type:

int

property closing_character

Character closing the arrays specification index in the Julia language.

Type:

str

property opening_character

Character opening the arrays specification index in the Julia language.

Type:

str

class layercake.formatters.julia.JuliaJacobianEquationFormatter(lang_translation=None)[source]

Bases: JacobianEquationFormatter

Class for symbolic Jacobian equations formatting in Julia.

Parameters:

lang_translation (dict(str)) – Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Julia language.

lang_translation

Language translation mapping dictionary, mapping replacements for converting Sympy symbolic output strings to the Julia language.

Type:

dict(str)

index_offset

Number that accesses the first element in an array. In Julia the index base is 1.

Type:

int

property closing_character

Character closing the arrays specification index in the Julia language.

Type:

str

property opening_character

Character opening the arrays specification index in the Julia language.

Type:

str