Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for opm common and embedded #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions cmake/modules/OpmPythonGenerateDocStrings.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
# Set the path to the input docstrings.json file and the output .hpp file
set(PYTHON_DOCSTRINGS_FILE "${PROJECT_SOURCE_DIR}/docs/docstrings.json")
set(DOCSTRINGS_GENERATED_HPP "PyBlackOilSimulatorDoc.hpp")
set(DOCSTRINGS_GENERATED_HEADER_PATH "${PROJECT_BINARY_DIR}/${TEMP_GEN_DIR}/${DOCSTRINGS_GENERATED_HPP}")
set(PYBLACKOIL_DOCSTRINGS_FILE "${PROJECT_SOURCE_DIR}/docs/docstrings_simulators.json")
set(PYBLACKOIL_DOCSTRINGS_GENERATED_HPP "PyBlackOilSimulatorDoc.hpp")
set(PYBLACKOIL_DOCSTRINGS_GENERATED_HEADER_PATH "${PROJECT_BINARY_DIR}/${TEMP_GEN_DIR}/${PYBLACKOIL_DOCSTRINGS_GENERATED_HPP}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, only PYBLACKOIL_DOCSTRINGS_GENERATED_HEADER_PATH and COMMON_DOCSTRINGS_GENERATED_HEADER_PATH needs to be exported to the global scope, right? I like to export only necessary variables, I think it helps with maintenance in the long run. Could we make this a function like in e.g. OpmPythonSetupInstall.cmake ? I realize this was something I forgot to do in the original version of this file..


# Command to run the Python script to generate the .hpp file
set(COMMON_DOCSTRINGS_FILE "${PROJECT_SOURCE_DIR}/docs/docstrings_common.json")
set(COMMON_DOCSTRINGS_GENERATED_HPP "OpmCommonPythonDoc.hpp")
set(COMMON_DOCSTRINGS_GENERATED_HEADER_PATH "${PROJECT_BINARY_DIR}/${TEMP_GEN_DIR}/${COMMON_DOCSTRINGS_GENERATED_HPP}")

# Commands to run the Python script to generate the .hpp file

add_custom_command(
OUTPUT ${PYBLACKOIL_DOCSTRINGS_GENERATED_HEADER_PATH}
COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/helper_scripts/generate_docstring_hpp.py ${PYBLACKOIL_DOCSTRINGS_FILE} "${PYBLACKOIL_DOCSTRINGS_GENERATED_HEADER_PATH}"
PYBLACKOILSIMULATORDOC_HPP "Opm::Pybind::DocStrings"
DEPENDS ${PYBLACKOIL_DOCSTRINGS_FILE}
COMMENT "Generating ${PYBLACKOIL_DOCSTRINGS_GENERATED_HPP} from JSON file"
)
add_custom_command(
OUTPUT ${DOCSTRINGS_GENERATED_HEADER_PATH}
COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/helper_scripts/generate_docstring_hpp.py ${PYTHON_DOCSTRINGS_FILE} "${DOCSTRINGS_GENERATED_HEADER_PATH}"
DEPENDS ${PYTHON_DOCSTRINGS_FILE}
COMMENT "Generating ${DOCSTRINGS_GENERATED_HPP} from JSON file"
OUTPUT ${COMMON_DOCSTRINGS_GENERATED_HEADER_PATH}
COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/helper_scripts/generate_docstring_hpp.py ${COMMON_DOCSTRINGS_FILE} "${COMMON_DOCSTRINGS_GENERATED_HEADER_PATH}"
OPMCOMMONPYTHONDOC_HPP "Opm::Common::DocStrings"
DEPENDS ${COMMON_DOCSTRINGS_FILE}
COMMENT "Generating ${COMMON_DOCSTRINGS_GENERATED_HPP} from JSON file"
)

# Create a custom target for the generated header file
add_custom_target(generate_docstring_hpp DEPENDS ${DOCSTRINGS_GENERATED_HEADER_PATH})
# Create custom targets for the generated header files
add_custom_target(pyblackoil_generate_docstring_hpp DEPENDS ${PYBLACKOIL_DOCSTRINGS_GENERATED_HEADER_PATH})
add_custom_target(common_generate_docstring_hpp DEPENDS ${COMMON_DOCSTRINGS_GENERATED_HEADER_PATH})
5 changes: 3 additions & 2 deletions cmake/modules/OpmPythonSetupOpmSimulatorsBindings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function (build_opm_simulators_pybind11_module)
"${MODULE_VERSION_OBJECT_FILE}"
)
# Make sure the module depends on the generated header file
add_dependencies(simulators generate_docstring_hpp)
add_dependencies(simulators pyblackoil_generate_docstring_hpp)
add_dependencies(simulators common_generate_docstring_hpp)

set_target_properties(
simulators PROPERTIES
Expand All @@ -26,4 +27,4 @@ function (build_opm_simulators_pybind11_module)
target_include_directories(simulators PRIVATE "${PROJECT_BINARY_DIR}/${TEMP_GEN_DIR}")
endfunction()

build_opm_simulators_pybind11_module()
build_opm_simulators_pybind11_module()
4 changes: 4 additions & 0 deletions 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::
5 changes: 3 additions & 2 deletions docs/docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration file for the Sphinx documentation builder.

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

Expand Down Expand Up @@ -37,7 +37,8 @@ def extract_opm_python_release():
# Our sphinx extension that will use the docstrings.json file to generate documentation
extensions = ["opm_python_docs.sphinx_ext_docstrings"]
# Path to docstrings.json
opm_python_docstrings_path = os.path.abspath('../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"]
Expand Down
52 changes: 52 additions & 0 deletions docs/docs/embedded.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan is to remove the -DOPM_ENABLE_PYTHON flag from opm-common and only keep the -DOPM ENABLE EMBEDDED PYTHON flag. When that is done, we should update this sentence.


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

.. code-block:: python

<PYACTION\_NAME> <SINGLE/UNLIMITED> /
<pythonscript> /

3. You need to provide the Python script.
lisajulia marked this conversation as resolved.
Show resolved Hide resolved


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.
lisajulia marked this conversation as resolved.
Show resolved Hide resolved

- 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.
lisajulia marked this conversation as resolved.
Show resolved Hide resolved

- 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.
lisajulia marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 6 additions & 4 deletions docs/docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Welcome to the Python documentation for opm-simulators!
=======================================================
Welcome to the Python documentation for OPM Flow!
=================================================

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

introduction
python

common
simulators
embedded

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

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/introduction.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Introduction
============

Documentation for the ``opm.simulators`` Python module.
Documentation for the OPM Python interfaces.
lisajulia marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 0 additions & 4 deletions docs/docs/python.rst

This file was deleted.

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

.. opm_simulators_docstrings::

74 changes: 0 additions & 74 deletions docs/docstrings.json

This file was deleted.

Loading