Skip to content

Commit

Permalink
Docs: migrate tutorials using sphnix-gallery (#268)
Browse files Browse the repository at this point in the history
To run calculation using QE, this PR
- Installs QE using conda
- Installs pseudo-potential using `aiida-pseudo`

Also, migrate one notebook in `howto` and `built-in task` sections.
  • Loading branch information
superstar54 committed Aug 25, 2024
1 parent 70bf8f9 commit c0c801d
Show file tree
Hide file tree
Showing 39 changed files with 1,734 additions and 4,740 deletions.
3 changes: 3 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ build:
- verdi presto
- verdi daemon start
- verdi status
- aiida-pseudo install sssp -x PBEsol
- verdi group list
- cat /proc/cpuinfo | grep processor | wc -l

conda:
environment: docs/environment.yml
Expand Down
1 change: 1 addition & 0 deletions docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ channels:
dependencies:
- aiida-core
- aiida-core.services
- qe
4 changes: 1 addition & 3 deletions docs/gallery/autogen/quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,7 @@ def multiply(x, y):


try:
query_code = load_code(
"add@localhost"
) # The computer label can also be omitted here
code = load_code("add@localhost") # The computer label can also be omitted here
except NotExistent:
code = InstalledCode(
computer=load_computer("localhost"),
Expand Down
3 changes: 3 additions & 0 deletions docs/gallery/built-in/autogen/GALLERY_HEADER.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
==========
HowTos
==========
141 changes: 141 additions & 0 deletions docs/gallery/built-in/autogen/shelljob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
=======================
ShellJob
=======================
"""

# %%
# Introduction
# ============
# ``ShellJob`` is a built-in task, which uses the `aiida-shell <https://aiida-shell.readthedocs.io/en/latest/>`_ package to run shell commands easily. Run any shell executable without writing a dedicated plugin or parser.
#
# This tutorial is based on the `docs <https://aiida-shell.readthedocs.io/en/latest/howto.html#>`_ of the ``aiida-shell``.
#
# Load the AiiDA profile.

from aiida import load_profile

load_profile()

# %%
# Running a shell command
# ========================
# Run a shell command without any arguments. Here we run the `date` command to show the date.
#


from aiida_workgraph import WorkGraph

wg = WorkGraph(name="test_shell_date")
date_task = wg.add_task("ShellJob", command="date")
wg.submit(wait=True)

# Print out the result:
print("\nResult: ", date_task.outputs["stdout"].value.get_content())

# %%
# Running a shell command with arguments
# =======================================
# To pass arguments to the shell command, pass them as a list of strings to the arguments keyword:
#


wg = WorkGraph(name="test_shell_date_with_arguments")
date_task = wg.add_task("ShellJob", command="date", arguments=["--iso-8601"])
wg.submit(wait=True)

# Print out the result:
print("\nResult: ", date_task.outputs["stdout"].value.get_content())

# %%
# Running a shell command with files as arguments
# ===============================================
# For commands that take arguments that refer to files, pass those files using the nodes keyword. The keyword takes a dictionary of SinglefileData nodes. To specify where on the command line the files should be passed, use placeholder strings in the arguments keyword.
#


from aiida.orm import SinglefileData

wg = WorkGraph(name="test_shell_cat_with_file_arguments")
cat_task = wg.add_task(
"ShellJob",
command="cat",
arguments=["{file_a}", "{file_b}"],
nodes={
"file_a": SinglefileData.from_string("string a"),
"file_b": SinglefileData.from_string("string b"),
},
)
wg.submit(wait=True)

# Print out the result:
print("\nResult: ", cat_task.outputs["stdout"].value.get_content())

# %%
# Create a workflow
# =================
# We want to calculate `(x+y)*z` in two steps using the `expr` command. Each step will invole one `ShellJob`.
#
# If one wanted to run this workflow in AiiDA using CalcJob and WorkChain, one would have to write plugins for `expr` command, and a WorkChain to handle the workflow. With aiida-workgraph, this can be run with the following workgraph:
#

from aiida_workgraph import WorkGraph
from aiida.orm import Int
from aiida_shell.data import PickledData
from aiida import load_profile

load_profile()


def parser(self, dirpath):
from aiida.orm import Int

return {"result": Int((dirpath / "stdout").read_text().strip())}


# Create a workgraph
wg = WorkGraph(name="shell_add_mutiply_workflow")
expr_1 = wg.add_task(
"ShellJob",
name="expr_1",
command="expr",
arguments=["{x}", "+", "{y}"],
nodes={"x": Int(2), "y": Int(3)},
parser=PickledData(parser),
parser_outputs=[{"name": "result"}],
)
expr_2 = wg.add_task(
"ShellJob",
name="expr_2",
command="expr",
arguments=["{result}", "*", "{z}"],
nodes={"z": Int(4), "result": expr_1.outputs["result"]},
parser=PickledData(parser),
parser_outputs=[{"name": "result"}],
)
wg.to_html()


# %%
# Submit the workgraph
#

wg.submit(wait=True, timeout=100)
print("State of WorkGraph : {}".format(wg.state))
print("Result : {}".format(expr_2.node.outputs.result.value))


# %%
# Generate node graph from the AiiDA process
#

from aiida_workgraph.utils import generate_node_graph

generate_node_graph(wg.pk)

# %%
# What's Next
# ===========
# For more examples of ``aiida-shell``, please refer to its `docs <https://aiida-shell.readthedocs.io/en/latest/howto.html#>`_.
#
76 changes: 76 additions & 0 deletions docs/gallery/concept/autogen/workgraph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
WorkGraph
=================
This :class:`~aiida_workgraph.workgraph.WorkGraph` object is a collection of tasks and links.
Create and launch workgraph
----------------------------
"""

# %%
# First, create an empty workgraph:
#

from aiida_workgraph import WorkGraph, task

wg = WorkGraph(name="my_first_workgraph")

# %%
# Define and use tasks
#

# Define a task using a `calcfunction`:
@task.calcfunction()
def add(x, y):
return x + y


# Add tasks to the workgraph
add1 = wg.add_task(add, name="add1")
add2 = wg.add_task(add, name="add2")

# %%
# Add a link between tasks:

wg.add_link(add1.outputs["result"], add2.inputs["x"])
wg.to_html()

# %%
# Submit the workgraph:

wg.submit(inputs={"add1": {"x": 1, "y": 2}, "add2": {"y": 3}}, wait=True)

# %%
# Load workgraph from the AiiDA process
# -------------------------------------
#
# WorkGraph saves its data as an extra attribute in its process, allowing reconstruction of the WorkGraph from the process.

from aiida_workgraph import WorkGraph

wg_loaded = WorkGraph.load(wg.pk)

# %%
# Execute order
# -------------
# The tasks will be executed under the following conditions:
#
# - No input task
# - All input tasks are finished.
# Group outputs
# -------------
# You can output the results of the tasks as the output of the WorkGraph.

wg = WorkGraph("test_workgraph_group_outputs")
wg.add_task(add, "add1", x=2, y=3)
wg.group_outputs = [{"name": "sum", "from": "add1.result"}]
wg.submit(wait=True)
assert wg.process.outputs.sum.value == 5

# %%
# List of all Methods
# ----------------------------
#
# .. autoclass:: aiida_workgraph.workgraph.WorkGraph
# :members:
3 changes: 3 additions & 0 deletions docs/gallery/howto/autogen/GALLERY_HEADER.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
===============
Built-in tasks
===============
Loading

0 comments on commit c0c801d

Please sign in to comment.