---
# System prepended metadata

title: '[Blueline: granules] diffusion granule interface to Fortran '
tags: [cycle 19]

---

# [Blueline:granules] Diffusion granule interface to Fortran

- Shaped by: Abishek, Magdalena
- Appetite (FTEs, weeks):
- Developers: Abishek, Sam? 
<!-- Filled in at the betting table unless someone is specifically required here -->

## Problem
<!-- The raw idea, a use case, or something we’ve seen that motivates us to work on this -->
We have entire components of the model ported to Python and want make them available for a Fortran driven model.

A simple example is the [Diffusion](https://github.com/C2SM/icon4py/blob/main/model/atmosphere/diffusion/src/icon4py/model/atmosphere/diffusion/diffusion.py), so this is taken as example case.



## Appetite
<!-- Explain how much time we want to spend and how that constrains the solution -->
Full cycle.

## Solution
<!-- The core elements we came up with, presented in a form that’s easy for people to immediately understand -->

### Previous work
There have been some experiments towards this goal with using [CFFI](https://cffi.readthedocs.io/en/stable/embedding.html) which can be used to embed python in C. The code can be found in [py2f](https://github.com/C2SM/icon4py/tree/main/tools/src/icon4pytools/py2f) module in `icon4py/tools`.

`py2f` does generate C headers and a Fortran interface for the generated CFFI code and compiles it, as a base it takes a (hand written) wrapper (`diffusion_wrapper.py`) for the python granule that it is supposed to generate which unpacks all arguments of the granule into  simple fields that are then exposed to Fortran code. 

There are some internal tests for the code generation, but it has never been integration-tested with Fortran code.

The goal of this project would be to
- Write a small Fortran Code that calls the generated granule functions and validate its results.
- improve on the code generation, 
- reduce hand labour, so generate the wrapper file as well 
- Spread knowledge to new developers (Abishek, Sam?)

### Sources
- [CFFI](https://cffi.readthedocs.io/en/stable/embedding.html)

- There are also some preliminary studies and findings on ways to call python fromm Fortran in this [repository](
https://github.com/halungge/python_from_fortran/blob/master/README.md), the conclusion for which was to use CFFI.
- Fortran testing framework used in ICON-C [FortUTF](https://artemis-beta.github.io/FortUTF/usage/)  and some [code samples](https://github.com/C2SM/libfortran-support/blob/master/test/fortran/test_octree.f90) (Jonas Jucker has experience with it)

## Rabbit holes
<!-- Details about the solution worth calling out to avoid problems -->

## No-gos
<!-- Anything specifically excluded from the concept: functionality or use cases we intentionally aren’t covering to fit the ## appetite or make the problem tractable -->

## Progress
<!-- Don't fill during shaping. This area is for collecting TODOs during building. As first task during building add a preliminary list of coarse-grained tasks for the project and refine them with finer-grained items when it makes sense as you work on them. -->

- [x] Task
    - [x] Subtask A
    - [x] Subtask X
- [ ] Task 2
    - [x] Subtask H
    - [ ] Subtask J
- [ ] Discovered Task 3
    - [ ] Subtask L
    - [ ] Subtask S
- [ ] Task 4

