Skip to content

Commit

Permalink
Add workgraph.py
Browse files Browse the repository at this point in the history
  • Loading branch information
superstar54 committed Aug 25, 2024
1 parent 3dbb585 commit 755e03a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 88 deletions.
78 changes: 78 additions & 0 deletions docs/gallery/concept/autogen/workgraph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
WorkGraph Example
=================
This example demonstrates how to create, configure, and use a
:class:`~aiida_workgraph.workgraph.WorkGraph` object, which 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:
11 changes: 5 additions & 6 deletions docs/gallery/tutorial/autogen/zero_to_hero.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@
# In AiiDA, we can define two `calcfunction` to do the `add` and `mutiply`:
#

from aiida.engine import calcfunction
from aiida_workgraph import task

#
@calcfunction

@task.calcfunction()
def add(x, y):
return x + y


#
@calcfunction
@task.calcfunction()
def multiply(x, y):
return x * y

Expand All @@ -58,7 +57,7 @@ def multiply(x, y):
# --------------------
# Three steps:
#
# - create a empty `WorkGraph`
# - create a empty WorkGraph
# - add tasks: `add` and `multiply`.
# - link the output of the `add` task to the `x` input of the `multiply` task.
#
Expand Down
82 changes: 0 additions & 82 deletions docs/source/concept/workgraph.rst

This file was deleted.

0 comments on commit 755e03a

Please sign in to comment.