Skip to content

BYSF-1/PTF-Python-To-Fortran

Repository files navigation

PTF

PTF is a Python-to-Fortran translator for scientific and numerical computing.

It is not a general Python compiler. It is a focused CLI and library for turning numerical Python and NumPy source into inspectable Fortran, compiling it locally, and optionally running the generated program.

Positioning

The full positioning note lives in PROJECT_POSITIONING.md.

Short version:

Python is fast to write. Fortran is fast to run. PTF connects the two for numerical and scientific code.

What PTF does

  • translates scientific Python into Fortran using the local Array Compiler checkout as the source of truth
  • applies explicit Fortran dialect profiles for Fortran 95, 2003, 2008, and 2018
  • emits diagnostics when code still relies on features newer than the selected dialect
  • compiles translated Fortran with a local compiler such as gfortran
  • optionally runs the resulting executable and keeps the runtime output visible
  • saves generated .f90 artifacts for downstream use

What PTF does not do

  • translate arbitrary Python application code
  • promise support for all Python syntax or all third-party packages
  • replace normal Python development
  • hide unsupported constructs behind vague or unsafe output

Repository layout

Quick start

python -m venv .venv
source .venv/bin/activate
pip install -e .

On Windows PowerShell:

.venv\Scripts\Activate.ps1

Translate an example file:

ptf translate examples\formatted_array_output.py --show-fortran

Build and run:

ptf build examples\formatted_array_output.py --run

Read from standard input:

type examples\formatted_array_output.py | ptf translate --stdin --name formatted_array_output.py

CLI overview

List bundled examples:

ptf examples

Translate to Fortran:

ptf translate examples\polynomial_roots.py --dialect fortran-2008

Compile and run:

ptf build examples\weighted_rng_choice.py --dialect fortran-2018 --run

Structured JSON output:

ptf build examples\formatted_array_output.py --run --json

Runtime configuration

By default, PTF uses the bundled translator in vendor/array-compiler.

PTF reads these optional environment variables:

PTF_ARRAY_COMPILER_ROOT="./vendor/array-compiler"
PTF_PYTHON=python
PTF_FORTRAN_COMPILER="D:/msys64/ucrt64/bin/gfortran.exe"

For compatibility with the earlier web prototype, ARRAY_COMPILER_ROOT, ARRAY_COMPILER_PYTHON, and ARRAY_COMPILER_COMPILER are also accepted.

Environment and software requirements

See docs/environment-and-software.md.

Package strategy

PTF should be treated as a scientific-subset translator.

  • numpy is the primary first-class package target
  • supported package features should be mapped explicitly to runtime/helper behavior
  • unsupported imports should fail clearly and early

Tests

python -m unittest discover -s tests -v

Submission-ready status

This repository is structured to be pushed directly as a GitHub project:

About

PTF translates numerical Python and NumPy-style code into inspectable Fortran, with local compile-and-run support for scientific computing workflows.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages