Skip to content

Commit 9670b86

Browse files
author
Lisa Julia Nebel
committed
Move python documentation things from opm-simulators to here
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
1 parent 8a333d8 commit 9670b86

13 files changed

Lines changed: 1067 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Build Python Sphinx Docs and push to gh-pages
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request_target:
8+
types: closed
9+
branches: master
10+
repository_dispatch:
11+
types: [docstrings_common_updated, docstrings_simulators_updated]
12+
permissions:
13+
contents: write
14+
jobs:
15+
build:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout source code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0 # Fetch all history for all tags and branches
22+
- name: Get docstrings_common.json from master branch of opm-common
23+
run: |
24+
curl -L -o python/docstrings_common.json https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json
25+
- name: Get docstrings_common.json from master branch of opm-simulators
26+
run: |
27+
curl -L -o python/docstrings_simulators.json https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json
28+
- name: Get dune.module from master branch of opm-simulators
29+
run: |
30+
curl -L -o dune.module https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module
31+
- name: Set up Python
32+
uses: actions/setup-python@v5
33+
with:
34+
python-version: "3.11"
35+
- name: Install poetry
36+
uses: OPM/actions-poetry@master
37+
- name: Install python dependencies
38+
run: |
39+
cd python/sphinx_docs
40+
poetry install
41+
- name: Build documentation
42+
run: |
43+
cd python
44+
mkdir gh-pages
45+
touch gh-pages/.nojekyll
46+
cd sphinx_docs
47+
# Currently we build only docs for the HEAD of the master branch
48+
# Later we can add release tags to the list to get the docs for the releases
49+
# For example: -b "master, release/2024.04/final" will build the docs for
50+
# the master branch and the release/2024.04/final tag
51+
# For the releases, we create snapshots of the docstrings_common.json and docstrings_simulators.json
52+
# and take the ones tracked by git, only for the master, we take the current ones we fetched
53+
# in steps 2 and 3 of this workflow
54+
55+
if [ "${{ github.ref_name }}" == "master" ]; then
56+
poetry run sphinx-versioned -m master -b "master" --force --git-root ../../
57+
else
58+
poetry run sphinx-versioned -m master -b "master ${{ github.ref_name }}" --force --git-root ../../
59+
fi
60+
- name: Copy documentation to gh-pages
61+
run: |
62+
cp -r python/sphinx_docs/docs/_build/* python/gh-pages
63+
64+
- name: Deploy documentation for PR merge to master or push to master
65+
if: github.ref == 'refs/heads/master'
66+
uses: OPM/github-pages-deploy-action@releases/v4
67+
with:
68+
branch: gh-pages
69+
folder: python/gh-pages
70+
- name: Deploy documentation for other branches (on forks)
71+
if: github.event_name == 'push' && github.ref != 'refs/heads/master'
72+
uses: OPM/github-pages-deploy-action@releases/v4
73+
with:
74+
branch: gh-pages-${{ github.ref_name }}
75+
folder: python/gh-pages

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Files necessary to build the documentation locally, but these are tracked by other github repositories
2+
dune.module
3+
python/docstrings_common.json
4+
python/docstrings_simulators.json
5+
6+
# Python sphinx build
7+
python/sphinx_docs/docs/_build*
8+
9+
# Python cache directories
10+
**/__pycache__/
11+
12+
# Editor autosave files
13+
*~
14+
*.swp

python/sphinx_docs/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: docs
2+
3+
# Build the documentation locally for the current branch
4+
# NOTE: You need to commit your changes before running this command
5+
docs:
6+
@CURRENT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
7+
sphinx-versioned -m $$CURRENT_BRANCH -b $$CURRENT_BRANCH --git-root ../../

python/sphinx_docs/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Python scripts for building opm-simulators sphinx documentation
2+
3+
## Installation of the python scripts
4+
- Requires python3 >= 3.10
5+
6+
### Using poetry
7+
For development it is recommended to use poetry:
8+
9+
- Install [poetry](https://python-poetry.org/docs/)
10+
- Then run:
11+
```
12+
$ poetry install
13+
$ poetry shell
14+
```
15+
16+
### Installation into virtual environment
17+
If you do not plan to change the code, you can do a regular installation into a VENV:
18+
19+
```
20+
$ python -m venv .venv
21+
$ source .venv/bin/activate
22+
$ pip install .
23+
```

python/sphinx_docs/docs/common.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
OPM Common Python Documentation
2+
===============================
3+
4+
.. opm_common_docstrings::

python/sphinx_docs/docs/conf.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
3+
project = "OPM Python Documentation"
4+
copyright = "2024 Equinor ASA"
5+
author = "Håkon Hægland"
6+
7+
# Function to extract release version from dune.module file
8+
def extract_opm_simulators_release():
9+
version_file_path = '../../../dune.module'
10+
with open(version_file_path, 'r') as file:
11+
for line in file:
12+
if line.startswith('Version:'):
13+
version_string = line.split(':')[1].strip()
14+
return version_string
15+
return "unknown" # Fallback version
16+
17+
release = extract_opm_simulators_release()
18+
19+
# -- General configuration ---------------------------------------------------
20+
import os
21+
import sys
22+
23+
# For regular Python packages, the path to the package is usually added to sys.path
24+
# here such that autodoc can find the modules. However, our Python module
25+
# opm.simulators.BlackOilSimulator is not generated yet. Since it is a pybind11
26+
# extension module, it needs to be compiled as part of the opm-simulators build
27+
# process (which requires building opm-common first and so on). The full compilation
28+
# of opm-simulators requres time and storage resources, so we do not want to
29+
# do this as part of the documentation build. Instead, we will use a sphinx extension
30+
# (Python script) to generate documentation from a JSON input file. (This JSON file
31+
# is also is also used to generate a C++ header file with docstrings. The header file
32+
# is generated when opm-simulators is built and is then included
33+
# in the pybind11 binding code for the BlackOilSimulator class. In this way, the
34+
# opm.simulators.BlackOilSimulator Python module will also have access to the docstrings.)
35+
sys.path.insert(0, os.path.abspath("../src"))
36+
# Our sphinx extension that will use the docstrings.json file to generate documentation
37+
extensions = ["opm_simulators_docs.sphinx_ext_docstrings"]
38+
# Path to docstrings.json
39+
opm_simulators_docstrings_path = os.path.abspath('../../docstrings_simulators.json')
40+
opm_common_docstrings_path = os.path.abspath('../../docstrings_common.json')
41+
42+
templates_path = ["_templates"]
43+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
44+
45+
46+
# -- Options for HTML output -------------------------------------------------
47+
# See: https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
48+
html_theme = "sphinx_rtd_theme"
49+
html_static_path = ["_static"]
50+
html_context = {
51+
"display_github": True,
52+
"github_user": "OPM",
53+
"github_repo": "opm-simulators",
54+
"github_version": "master",
55+
"conf_py_path": "/python/docs/",
56+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
OPM Embedded Python Documentation
2+
=================================
3+
4+
The PYACTION keyword is a flow specific keyword which allows for executing embedded Python
5+
code in the SCHEDULE section. The embedded Python code will then be executed at the end of each successful timestep.
6+
7+
The PYACTION keyword is inspired
8+
by the ACTIONX keyword, but instead of a .DATA formatted condition you
9+
are allowed to implement the condition with a general Python script. The
10+
ACTIONX keywords are very clearly separated in a condition part and an
11+
action part in the form of a list of keywords which are effectively injected in
12+
the SCHEDULE section when the condition evaluates to true.
13+
This is not so for PYACTION where there is one Python script in which both
14+
conditions can be evaluated and changes applied.
15+
16+
In order to enable the PYACTION keyword:
17+
18+
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.
19+
20+
2. The keyword PYACTION must be added to the SCHEDULE section:
21+
22+
.. code-block:: python
23+
24+
<PYACTION\_NAME> <SINGLE/UNLIMITED> /
25+
<pythonscript> / -- path to the python script, relative to the location of the DATA-file
26+
27+
3. You need to provide the Python script.
28+
29+
30+
To interact with the simulator in the embedded Python code, you can access four variables from the simulator:
31+
32+
.. code-block:: python
33+
34+
# Python module opm_embedded
35+
import opm_embedded
36+
# The current EclipseState
37+
ecl_state = opm_embedded.current_ecl_state
38+
# The current Schedule
39+
schedule = opm_embedded.current_schedule
40+
# The current SummaryState
41+
summary_state = opm_embedded.current_summary_state
42+
# The current report step
43+
report_step = opm_embedded.current_report_step
44+
45+
46+
- 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.
47+
48+
- 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.
49+
50+
- 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.
51+
52+
- 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.
53+
54+
See also: PYACTION in the `reference manual <https://opm-project.org/?page_id=955>`_ for more information.

python/sphinx_docs/docs/index.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Welcome to the Python documentation for OPM Flow!
2+
=================================================
3+
4+
There are two Python APIs within OPM Flow:
5+
- running flow from Python code using the Python bindings
6+
- running a Python script embedded in a simulation
7+
8+
.. toctree::
9+
:maxdepth: 1
10+
:caption: Contents:
11+
12+
introduction
13+
common
14+
simulators
15+
embedded
16+
17+
Indices and tables
18+
==================
19+
20+
* :ref:`genindex`
21+
* :ref:`modindex`
22+
* :ref:`search`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Introduction
2+
============
3+
4+
Documentation for the OPM Python interfaces.
5+
6+
#TODO: expand on the introduction, add information about installation and requirements for example. Some example code would also be nice.
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
OPM Simulators Python Documentation
2+
===================================
3+
4+
.. opm_simulators_docstrings::

0 commit comments

Comments
 (0)