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

Add support for Jupyter plugin exercise2 #3872

Merged
merged 4 commits into from
Aug 23, 2020
Merged
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
21 changes: 15 additions & 6 deletions doc/sphinx/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ are required:
.. code-block:: bash

sudo apt install python3-matplotlib python3-scipy ipython3 jupyter-notebook
sudo pip3 install 'pint>=0.9'
pip3 install --user 'pint>=0.9' 'jupyter_contrib_nbextensions==0.5.1' \
'sphinx>=1.6.7,!=2.1.0,!=3.0.0' 'sphinxcontrib-bibtex>=0.3.5'
jupyter contrib nbextension install --user
jupyter nbextension enable rubberband/main
jupyter nbextension enable exercise2/main

Nvidia GPU acceleration
"""""""""""""""""""""""
Expand Down Expand Up @@ -829,10 +833,10 @@ to actually write a simulation script for |es|.
Running an interactive notebook
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Running the Jupyter interpreter requires using the ``ipypresso`` script, which
Running a Jupyter session requires using the ``ipypresso`` script, which
is also located in the build directory (its name comes from the IPython
interpreter, today known as Jupyter). To run the tutorials, you will need
to start the Jupyter interpreter in notebook mode:
to start a Jupyter session:

.. code-block:: bash

Expand All @@ -858,11 +862,16 @@ will exit the Python interpreter and Jupyter will notify you that the current
Python kernel stopped. If a cell takes too long to execute, you may interrupt
it with the stop button.

To close the Jupyter notebook, go to the terminal where it was started and use
Solutions cells are created using the ``exercise2`` plugin from nbextensions.
To prevent solution code cells from running when clicking on "Run All", these
code cells need to be converted to Markdown cells and fenced with `````python``
and ```````.

To close the Jupyter session, go to the terminal where it was started and use
the keyboard shortcut Ctrl+C twice.

When starting the Jupyter interpreter in notebook mode, you may see the
following warning in the terminal:
When starting a Jupyter session, you may see the following warning in the
terminal:

.. code-block:: none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
"\n",
"We will simulate a planar Poiseuille flow using a square box, two walls\n",
"with normal vectors $\\left(\\pm 1, 0, 0 \\right)$, and an external force density\n",
"applied to every node.\n",
"\n",
"Use the data to fit a parabolic function. Can you confirm the analytic solution?"
"applied to every node."
]
},
{
Expand All @@ -52,6 +50,7 @@
"import logging\n",
"import sys\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"import espressomd\n",
Expand Down Expand Up @@ -102,9 +101,54 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"source": [
"The solution is available at <tt>/doc/tutorials/04-lattice_boltzmann/scripts/04-lattice_boltzmann_part4_solution.py</tt>"
"Use the data to fit a parabolic function. Can you confirm the analytic solution?"
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "hidden"
},
"source": [
"```python\n",
"# Extract fluid velocity along the x-axis\n",
"fluid_velocities = np.zeros((lbf.shape[0], 2))\n",
"for x in range(lbf.shape[0]):\n",
" # Average over the node in y direction\n",
" v_tmp = np.zeros(lbf.shape[1])\n",
" for y in range(lbf.shape[1]):\n",
" v_tmp[y] = lbf[x, y, 0].velocity[1]\n",
" fluid_velocities[x, 0] = (x + 0.5) * AGRID\n",
" fluid_velocities[x, 1] = np.average(v_tmp)\n",
"\n",
"def poiseuille_flow(x, force_density, dynamic_viscosity, height):\n",
" return force_density / (2.0 * dynamic_viscosity) * \\\n",
" (height**2.0 / 4.0 - x**2.0)\n",
"\n",
"# Note that the LB viscosity is not the dynamic viscosity but the\n",
"# kinematic viscosity (mu=LB_viscosity * density)\n",
"x_values = np.linspace(0.0, BOX_L, lbf.shape[0])\n",
"HEIGHT = BOX_L - 2.0 * AGRID\n",
"# analytical curve\n",
"y_values = poiseuille_flow(x_values - (HEIGHT / 2.0 + AGRID), FORCE_DENSITY[1],\n",
" VISCOSITY * DENSITY, HEIGHT)\n",
"# velocity is zero outside the walls\n",
"y_values[np.nonzero(x_values < WALL_OFFSET)] = 0.0\n",
"y_values[np.nonzero(x_values > BOX_L - WALL_OFFSET)] = 0.0\n",
"\n",
"fig1 = plt.figure(num=None, figsize=(10, 6), dpi=80, facecolor='w', edgecolor='k')\n",
"plt.plot(x_values, y_values, '-', linewidth=2, label='analytical')\n",
"plt.plot(fluid_velocities[:, 0], fluid_velocities[:, 1], 'o', label='simulation')\n",
"plt.xlabel('Position on the $x$-axis', fontsize=16)\n",
"plt.ylabel('Fluid velocity in $y$-direction', fontsize=16)\n",
"plt.legend()\n",
"plt.show()\n",
"```"
]
},
{
Expand Down
28 changes: 3 additions & 25 deletions doc/tutorials/04-lattice_boltzmann/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,13 @@ configure_tutorial_target(
04-lattice_boltzmann_part2.ipynb 04-lattice_boltzmann_part3.ipynb
04-lattice_boltzmann_part4.ipynb figures/latticeboltzmann-grid.png
figures/latticeboltzmann-momentumexchange.png
scripts/04-lattice_boltzmann_part3_solution.py
scripts/04-lattice_boltzmann_part4_solution.py)

add_custom_command(
OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/scripts/04-lattice_boltzmann_part4_solution_cut.py"
DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/04-lattice_boltzmann_part4_solution.py"
DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/04-lattice_boltzmann_part4_solution_cut.cmake"
COMMAND
${CMAKE_COMMAND} -P
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/04-lattice_boltzmann_part4_solution_cut.cmake"
)
scripts/04-lattice_boltzmann_part3_solution.py)

nb_export(TARGET tutorial_04 SUFFIX "1" FILE "04-lattice_boltzmann_part1.ipynb"
HTML_RUN)
nb_export(TARGET tutorial_04 SUFFIX "2" FILE "04-lattice_boltzmann_part2.ipynb"
HTML_RUN)
nb_export(TARGET tutorial_04 SUFFIX "3" FILE "04-lattice_boltzmann_part3.ipynb"
HTML_RUN)
nb_export(
TARGET
tutorial_04
SUFFIX
"4"
FILE
"04-lattice_boltzmann_part4.ipynb"
HTML_RUN
ADD_SCRIPTS
"${CMAKE_CURRENT_BINARY_DIR}/scripts/04-lattice_boltzmann_part4_solution_cut.py"
)
nb_export(TARGET tutorial_04 SUFFIX "4" FILE "04-lattice_boltzmann_part4.ipynb"
HTML_RUN)

This file was deleted.

This file was deleted.

32 changes: 21 additions & 11 deletions doc/tutorials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,39 @@ function(NB_EXPORT)
set(PY_FILE "${NB_FILE_BASE}.py")

if(${NB_EXPORT_HTML_RUN})
set(HTML_FILE_DEPENDS "${NB_FILE_BASE}.run${NB_FILE_EXT}")
set(NB_FILE_RUN "${NB_FILE_BASE}.run${NB_FILE_EXT}")
add_custom_command(
OUTPUT "${HTML_FILE_DEPENDS}"
OUTPUT ${NB_FILE_RUN}
DEPENDS
"${NB_FILE};${NB_EXPORT_ADD_SCRIPTS};${CMAKE_BINARY_DIR}/doc/tutorials/html_runner.py;${CMAKE_BINARY_DIR}/testsuite/scripts/importlib_wrapper.py"
COMMAND
"${CMAKE_BINARY_DIR}/pypresso"
"${CMAKE_BINARY_DIR}/doc/tutorials/html_runner.py" "--input"
"${NB_FILE}" "--output" "${HTML_FILE_DEPENDS}" "--substitutions"
${NB_EXPORT_VAR_SUBST} "--scripts" ${NB_EXPORT_ADD_SCRIPTS})
${CMAKE_BINARY_DIR}/pypresso
${CMAKE_BINARY_DIR}/doc/tutorials/html_runner.py --execute --exercise2
--input ${NB_FILE} --output ${NB_FILE_RUN} --substitutions
${NB_EXPORT_VAR_SUBST} --scripts ${NB_EXPORT_ADD_SCRIPTS})
else()
set(HTML_FILE_DEPENDS "${NB_FILE}")
set(NB_FILE_RUN ${NB_FILE})
endif()

add_custom_command(
OUTPUT ${HTML_FILE} DEPENDS ${HTML_FILE_DEPENDS};${NB_EXPORT_ADD_SCRIPTS}
OUTPUT ${HTML_FILE}
DEPENDS ${NB_FILE_RUN};${NB_EXPORT_ADD_SCRIPTS}
COMMAND
${CMAKE_BINARY_DIR}/pypresso
${CMAKE_BINARY_DIR}/doc/tutorials/html_runner.py --exercise2 --input
${NB_FILE_RUN} --output ${NB_FILE_RUN}~
COMMAND ${IPYTHON_EXECUTABLE} nbconvert --to "html" --output ${HTML_FILE}
${HTML_FILE_DEPENDS})
${NB_FILE_RUN}~)

add_custom_command(
OUTPUT ${PY_FILE} DEPENDS ${NB_FILE}
OUTPUT ${PY_FILE}
DEPENDS ${NB_FILE}
COMMAND
${CMAKE_BINARY_DIR}/pypresso
${CMAKE_BINARY_DIR}/doc/tutorials/html_runner.py --exercise2 --input
${NB_FILE} --output ${NB_FILE}~
COMMAND ${IPYTHON_EXECUTABLE} nbconvert --to "python" --output ${PY_FILE}
${NB_FILE})
${NB_FILE}~)

add_custom_target("${NB_EXPORT_TARGET}_html" DEPENDS ${HTML_FILE}
${DEPENDENCY_OF_TARGET})
Expand Down
Loading