Graphical front-end

A small Streamlit application is included as an optional extra. It offers buttons, drop-down menus and editable tables for the .model language instead of a bare text file: you describe the model, the application compiles it while you edit and shows

  • the generated Fortran module (ready to download), and

  • a typeset preview of the function and of every derivative.

Installation

The GUI is kept out of the default installation. Install it with:

pip install 'thermodiff[gui]'

Usage

Start the application with:

thermodiff-gui

Your browser opens on a form with the model name, the Fortran use statements and editable tables for the variables, the parameters and the intermediate expressions. A drop-down menu loads the bundled examples (Peng-Robinson, ideal gas enthalpy, mole fraction and a minimal template) and a button downloads the generated .f90 file.

Errors are reported with the offending line of the assembled .model file, so the strict validation of the command line compiler is available here too.

Python API

The widgets live in thermodiff.gui.app, which is a Streamlit script. Everything that can be used or tested without a running Streamlit server is collected in thermodiff.gui.logic:

Streamlit independent logic behind the model GUI.

Everything in this module is plain Python: the GUI only wires widgets to these helpers. Keeping it free of any Streamlit import means the helpers can be unit tested without installing the optional gui extra.

DEFAULT_PRESET = 'standard'

Preset selected by default in the GUI.

PRESET_HELP = {'fastest': 'Fastest generated code; slower generation, last-bit differences.', 'minimal': 'Plain generator output, no extra optimisations.', 'standard': 'Recommended: chain rule, symmetry and sharing.'}

One-line description of each code-generation preset.

assemble_model(model_name, uses, variables, parameters, expressions, function)[source]

Build the text of a .model file from structured parts.

Parameters:
  • model_name (str) – Name of the model. Empty falls back to model.

  • uses (list of str) – use statements, without the leading keyword.

  • variables (list of dict) – Rows with name and dims keys.

  • parameters (list of dict) – Rows with name and dims keys.

  • expressions (list of dict) – Rows with lhs and rhs keys.

  • function (str) – Body of the function section.

Returns:

The assembled .model text.

Return type:

str

build_from_parts(model_name, uses, variables, parameters, expressions, function, optional_derivatives=False, preset='standard')[source]

Assemble structured parts and compile them in one step.

Parameters:
Returns:

The build_result() dictionary.

Return type:

dict

build_result(text, filename='<editor>', optional_derivatives=False, preset='standard')[source]

Compile .model text and collect everything the GUI shows.

This never raises: compilation problems are reported in the error entry of the result.

Parameters:
  • text (str) – Contents of the .model file.

  • filename (str, optional) – Name used in error messages.

  • optional_derivatives (bool, optional) – Declare the outputs as Fortran optional arguments so the generated subroutine computes only the derivatives the caller passes.

  • preset ({"minimal", "standard", "fastest"}, optional) – Code-generation preset, see PRESET_HELP.

Returns:

With keys text, code (Fortran or None), latex (preview or None), warnings and error.

Return type:

dict

parse_parts(text)[source]

Split .model text back into GUI friendly rows.

This is the inverse of assemble_model(), used to load the bundled examples into the editor widgets.

Parameters:

text (str) – Contents of a .model file.

Returns:

With keys model_name, uses, variables, parameters, expressions and function. The keys match the parameters of assemble_model(), so assemble_model(**parts) rebuilds the text.

Return type:

dict

Raises:

ModelError – If the text is malformed and cannot be split into sections.

annotated_source(text, line=None)[source]

Return text with line numbers, marking line with an arrow.

Parameters:
  • text (str) – Source text.

  • line (int, optional) – Line number to highlight.

Returns:

A single string ready to be shown in a code block.

Return type:

str

error_context(error, text)[source]

Return the source line a compilation error points at.

Parameters:
  • error (dict) – The error entry of a build_result() dictionary.

  • text (str) – Source text the error refers to.

Returns:

The offending line, without its line number, or None when the error has no location.

Return type:

str or None