Skip to content

Commit

Permalink
Move python documentation things from opm-simulators to here
Browse files Browse the repository at this point in the history
Get dune.module from opm-simulators as well so we can get the version
Trigger workflow on all changes, since this repo contains only documentation
  • Loading branch information
lisajulia committed Jul 10, 2024
0 parents commit f14085e
Show file tree
Hide file tree
Showing 13 changed files with 1,071 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/python_sphinx_docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Build Python Sphinx Docs and push to gh-pages

on:
push:
branches:
- '**'
pull_request_target:
types: closed
branches: main
repository_dispatch:
types: [docstrings_common_updated, docstrings_simulators_updated]
permissions:
contents: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all tags and branches
- name: Get docstrings_common.json from master branch of opm-common
run: |
curl -L -o python/docstrings_common.json https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json
- name: Get docstrings_common.json from master branch of opm-simulators
run: |
curl -L -o python/docstrings_simulators.json https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json
- name: Get dune.module from master branch of opm-simulators
run: |
curl -L -o dune.module https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install poetry
uses: OPM/actions-poetry@master
- name: Install python dependencies
run: |
cd python/sphinx_docs
poetry install
- name: Build documentation
run: |
cd python
mkdir gh-pages
touch gh-pages/.nojekyll
cd sphinx_docs
# Currently we build only docs for the HEAD of the master/main branch
# Later we can add release tags to the list to get the docs for the releases
# For example: -b "main, release/2024.04/final" will build the docs for
# the main branch and the release/2024.04/final tag
# For the releases, we create snapshots of the docstrings_common.json and docstrings_simulators.json
# and take the ones tracked by git, only for the main, we take the current ones we fetched
# in steps 2 and 3 of this workflow
branch_name=${{ github.ref }}
if [[ "$branch_name" == refs/heads/* ]]; then
branch_name=${branch_name#refs/heads/}
fi
if [ "$branch_name" == "main" ]; then
poetry run sphinx-versioned -m main -b "main" --force --git-root ../../
else
poetry run sphinx-versioned -m main -b "main, ${branch_name}" --force --git-root ../../
fi
- name: Copy documentation to gh-pages
run: |
cp -r python/sphinx_docs/docs/_build/* python/gh-pages
- name: Deploy documentation for PR merge to main or push to main
if: github.ref == 'refs/heads/main'
uses: OPM/github-pages-deploy-action@releases/v4
with:
branch: gh-pages
folder: python/gh-pages
- name: Deploy documentation for other branches (on forks)
if: github.event_name == 'push' && github.ref != 'refs/heads/main'
uses: OPM/github-pages-deploy-action@releases/v4
with:
branch: gh-pages-${{ github.ref }}
folder: python/gh-pages
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Files necessary to build the documentation locally, but these are tracked by other github repositories
dune.module
python/docstrings_common.json
python/docstrings_simulators.json

# Python sphinx build
python/sphinx_docs/docs/_build*

# Python cache directories
**/__pycache__/

# Editor autosave files
*~
*.swp
7 changes: 7 additions & 0 deletions python/sphinx_docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: docs

# Build the documentation locally for the current branch
# NOTE: You need to commit your changes before running this command
docs:
@CURRENT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
sphinx-versioned -m $$CURRENT_BRANCH -b $$CURRENT_BRANCH --git-root ../../
23 changes: 23 additions & 0 deletions python/sphinx_docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Python scripts for building opm-simulators sphinx documentation

## Installation of the python scripts
- Requires python3 >= 3.10

### Using poetry
For development it is recommended to use poetry:

- Install [poetry](https://python-poetry.org/docs/)
- Then run:
```
$ poetry install
$ poetry shell
```

### Installation into virtual environment
If you do not plan to change the code, you can do a regular installation into a VENV:

```
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install .
```
4 changes: 4 additions & 0 deletions python/sphinx_docs/docs/common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OPM Common Python Documentation
===============================

.. opm_common_docstrings::
56 changes: 56 additions & 0 deletions python/sphinx_docs/docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Configuration file for the Sphinx documentation builder.

project = "OPM Python Documentation"
copyright = "2024 Equinor ASA"
author = "Håkon Hægland"

# Function to extract release version from dune.module file
def extract_opm_simulators_release():
version_file_path = '../../../dune.module'
with open(version_file_path, 'r') as file:
for line in file:
if line.startswith('Version:'):
version_string = line.split(':')[1].strip()
return version_string
return "unknown" # Fallback version

release = extract_opm_simulators_release()

# -- General configuration ---------------------------------------------------
import os
import sys

# For regular Python packages, the path to the package is usually added to sys.path
# here such that autodoc can find the modules. However, our Python module
# opm.simulators.BlackOilSimulator is not generated yet. Since it is a pybind11
# extension module, it needs to be compiled as part of the opm-simulators build
# process (which requires building opm-common first and so on). The full compilation
# of opm-simulators requres time and storage resources, so we do not want to
# do this as part of the documentation build. Instead, we will use a sphinx extension
# (Python script) to generate documentation from a JSON input file. (This JSON file
# is also is also used to generate a C++ header file with docstrings. The header file
# is generated when opm-simulators is built and is then included
# in the pybind11 binding code for the BlackOilSimulator class. In this way, the
# opm.simulators.BlackOilSimulator Python module will also have access to the docstrings.)
sys.path.insert(0, os.path.abspath("../src"))
# Our sphinx extension that will use the docstrings.json file to generate documentation
extensions = ["opm_simulators_docs.sphinx_ext_docstrings"]
# Path to docstrings.json
opm_simulators_docstrings_path = os.path.abspath('../../docstrings_simulators.json')
opm_common_docstrings_path = os.path.abspath('../../docstrings_common.json')

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# See: https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
html_context = {
"display_github": True,
"github_user": "OPM",
"github_repo": "opm-simulators",
"github_version": "master",
"conf_py_path": "/python/docs/",
}
54 changes: 54 additions & 0 deletions python/sphinx_docs/docs/embedded.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
OPM Embedded Python Documentation
=================================

The PYACTION keyword is a flow specific keyword which allows for executing embedded Python
code in the SCHEDULE section. The embedded Python code will then be executed at the end of each successful timestep.

The PYACTION keyword is inspired
by the ACTIONX keyword, but instead of a .DATA formatted condition you
are allowed to implement the condition with a general Python script. The
ACTIONX keywords are very clearly separated in a condition part and an
action part in the form of a list of keywords which are effectively injected in
the SCHEDULE section when the condition evaluates to true.
This is not so for PYACTION where there is one Python script in which both
conditions can be evaluated and changes applied.

In order to enable the PYACTION keyword:

1. OPM flow must be compiled with the cmake switches -DOPM ENABLE EMBEDDED PYTHON=ON and -DOPM ENABLE PYTHON=ON, the default is to build with these switches set to OFF.

2. The keyword PYACTION must be added to the SCHEDULE section:

.. code-block:: python
<PYACTION\_NAME> <SINGLE/UNLIMITED> /
<pythonscript> / -- path to the python script, relative to the location of the DATA-file
3. You need to provide the Python script.


To interact with the simulator in the embedded Python code, you can access four variables from the simulator:

.. code-block:: python
# Python module opm_embedded
import opm_embedded
# The current EclipseState
ecl_state = opm_embedded.current_ecl_state
# The current Schedule
schedule = opm_embedded.current_schedule
# The current SummaryState
summary_state = opm_embedded.current_summary_state
# The current report step
report_step = opm_embedded.current_report_step
- current_ecl_state: An instance of the `EclipseState <common.html#opm.io.ecl_state.EclipseState>`_ class - this is a representation of all static properties in the model, ranging from porosity to relperm tables. The content of the ecl state is immutable - you are not allowed to change the static properties at runtime.

- current_schedule: An instance of the `Schedule <common.html#opm.io.schedule.Schedule>`_ class - this is a representation of all the content from the SCHEDULE section, notably all well and group information and the timestepping.

- current_report_step: This is an integer for the report step we are currently working on. Observe that the PYACTION is called for every simulator timestep, i.e. it will typically be called multiple times with the same value for the report step argument.

- current_summary_state: An instance of the `SummaryState <common.html#opm.io.sim.SummaryState>`_ class, this is where the current summary results of the simulator are stored. The SummaryState class has methods to get hold of well, group and general variables.

See also: PYACTION in the `reference manual <https://opm-project.org/?page_id=955>`_ for more information.
22 changes: 22 additions & 0 deletions python/sphinx_docs/docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Welcome to the Python documentation for OPM Flow!
=================================================

There are two Python APIs within OPM Flow:
- running flow from Python code using the Python bindings
- running a Python script embedded in a simulation

.. toctree::
:maxdepth: 1
:caption: Contents:

introduction
common
simulators
embedded

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
7 changes: 7 additions & 0 deletions python/sphinx_docs/docs/introduction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Introduction
============

Documentation for the OPM Python interfaces.

#TODO: expand on the introduction, add information about installation and requirements for example. Some example code would also be nice.

4 changes: 4 additions & 0 deletions python/sphinx_docs/docs/simulators.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OPM Simulators Python Documentation
===================================

.. opm_simulators_docstrings::
Loading

0 comments on commit f14085e

Please sign in to comment.