Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
399 changes: 399 additions & 0 deletions other/materials_designer/workflows/run_bandgap_workflow_max.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,399 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"# Bandgap Workflow Example\n",
" This notebook demonstrates how to build and run a bandgap workflow for a material.\n",
" Example of building and running a bandgap workflow for twisted MoS2 interface from specific_examples.\n",
"\n",
"## Process Overview\n",
"### 1. Set up the environment and parameters.\n",
"### 2. Log in to get the API token\n",
"### 3. Load the target material.\n",
"### 4. Import workflow builder and related analyzers.\n",
"### 5. Analyze material to get parameters for the workflow configuration.\n",
"### 6. Create the workflow configuration.\n",
"### 7. Create a job with material and workflow configuration.\n",
"### 8. Submit the job to the server.\n",
"### 9. Monitor the job status and retrieve results.\n",
"### 10. Display the results."
]
},
{
"cell_type": "markdown",
"id": "1",
"metadata": {},
"source": [
"## 1. Set up the environment and parameters"
]
},
{
"cell_type": "markdown",
"id": "2",
"metadata": {},
"source": [
"## 2. Log in to get the API token"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.api import ApiClient\n",
"\n",
"# Log in to get the API token\n",
"auth_config = await ApiClient().login()"
]
},
{
"cell_type": "markdown",
"id": "4",
"metadata": {},
"source": [
"## 3. Load the target material"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"from utils.visualize import visualize_materials as visualize\n",
"from utils.jupyterlite import load_material_from_folder\n",
"\n",
"material = load_material_from_folder(\"/uploads\", \"MoS2_twisted_interface_60_degrees.json\")\n",
"visualize(material)"
]
},
{
"cell_type": "markdown",
"id": "6",
"metadata": {},
"source": [
"## 5. Create workflow and set its parameters\n",
"### 5.1. Get list of applications and select one"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.standata.applications import Applications\n",
"\n",
"# Get Applications list (with versions, build)\n",
"apps_list = Applications.list_all()\n",
"# returns apps_list[0] = [{\"name\" : \"espresso\", \"version\": \"7.2\", \"build\": \"GNU\"}]\n",
"\n",
"app = Applications.get_by_name_first_match(\"espresso\")\n",
"# returns name, version, build config"
]
},
{
"cell_type": "markdown",
"id": "8",
"metadata": {},
"source": [
"### 5.2. Create workflow from standard workflows and preview it"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.standata.workflows import Workflows\n",
"from mat3ra.wode.workflows import Workflow\n",
"from utils.visualize import visualize_workflow\n",
"\n",
"# Search WF by name and application\n",
"workflow_config = Workflows.filter_by_application(app).get_by_name_first_match(\"band_gap\")\n",
"workflow = Workflow.create(workflow_config)\n",
"\n",
"# View workflow to understand its structure\n",
"visualize_workflow(workflow)"
]
},
{
"cell_type": "markdown",
"id": "10",
"metadata": {},
"source": [
"### 5.3. Add relaxation subworkflow"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"workflow.add_relaxation()\n",
"visualize_workflow(workflow)\n",
"# Relaxation subworkflow is added as the first subworkflow"
]
},
{
"cell_type": "markdown",
"id": "12",
"metadata": {},
"source": [
"### 5.4. Change subworkflow details (Model subtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.standata.model_tree import ModelTreeStandata\n",
"\n",
"swf_0 = workflow.subworkflows[0] # relaxation subworkflow\n",
"swf_1 = workflow.subworkflows[1] # band structure subworkflow\n",
"\n",
"# Change model subtype for relaxation subworkflow\n",
"# For preview:\n",
"subtypes = ModelTreeStandata.get_subtypes_by_model_type(\"dft\") # [\"gga\", \"lda\"] as enum\n",
"functionals = ModelTreeStandata.get_functionals_by_subtype(\"dft\", \"lda\") # [\"pz\", ...] as enum\n",
"\n",
"model = ModelTreeStandata.get_model_by_parameters(\n",
" model_type=\"dft\",\n",
" subtype=subtypes.LDA,\n",
" functional=functionals.PZ,\n",
")\n",
"swf_0.model = model\n",
"swf_1.model = model"
]
},
{
"cell_type": "markdown",
"id": "14",
"metadata": {},
"source": [
"### 5.5. Modify workflow units found in preview"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.wode.context_providers import PointsGridFormDataProvider\n",
"\n",
"# Values from publication\n",
"kgrid_scf = [6, 6, 1]\n",
"kgrid_nscf = [12, 12, 1]\n",
"kgrid_relax = kgrid_scf\n",
"\n",
"kgrid_context_provider = PointsGridFormDataProvider(material=material)\n",
"\n",
"new_context_relax = kgrid_context_provider.get_data(dimensions=kgrid_relax)\n",
"new_context_scf = kgrid_context_provider.get_data(dimensions=kgrid_scf)\n",
"new_context_nscf = kgrid_context_provider.get_data(dimensions=kgrid_nscf)\n",
"\n",
"# Get workflow's specific unit that needs to be modified\n",
"# Option 1: search is done by unit name regex across the entire workflow\n",
"unit_to_modify_relax = workflow.get_unit_by_name(name_regex=\"relax\")\n",
"unit_to_modify_relax.context.add_context(new_context_relax)\n",
"\n",
"# Option 2: search is done by unit name within a specific subworkflow\n",
"unit_to_modify_scf = workflow.subworkflows[1].get_unit_by_name(name=\"pw_scf\")\n",
"unit_to_modify_scf.context.add_context(new_context_scf)\n",
"unit_to_modify_nscf = workflow.subworkflows[1].get_unit_by_name(name=\"pw_nscf\")\n",
"unit_to_modify_nscf.context.add_context(new_context_nscf)\n",
"\n",
"# Set the modified unit back to the workflow\n",
"# Option 1: direct set by unit object, replacing the existing one\n",
"workflow.set_unit(unit_to_modify_relax)\n",
"\n",
"# Option 2: set by unit flowchart id and new unit object\n",
"workflow.set_unit(unit_flowchart_id=unit_to_modify_scf.flowchart_id, new_unit=unit_to_modify_scf)\n",
"workflow.set_unit(unit_flowchart_id=unit_to_modify_nscf.flowchart_id, new_unit=unit_to_modify_nscf)"
]
},
{
"cell_type": "markdown",
"id": "16",
"metadata": {},
"source": [
"## 6. Create the compute configuration\n",
"### 6.1. View available clusters and providers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17",
"metadata": {},
"outputs": [],
"source": [
"# List available compute providers and clusters\n",
"from mat3ra.ide.compute import ComputeProvider, ComputeCluster\n",
"\n",
"providers = ComputeProvider.list_all()\n",
"clusters = ComputeCluster.list_all()"
]
},
{
"cell_type": "markdown",
"id": "18",
"metadata": {},
"source": [
"### 6.2. Create compute configuration"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.ide.compute import ComputeConfiguration, QueueTypesEnum\n",
"\n",
"compute_config = ComputeConfiguration(\n",
" queue=QueueTypesEnum.OR8,\n",
" nodes=1,\n",
" ppn=8,\n",
" cluster=clusters[0], # select first available cluster\n",
")"
]
},
{
"cell_type": "markdown",
"id": "20",
"metadata": {},
"source": [
"## 7. Create the job with material and workflow configuration"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.jode.job import create_job\n",
"\n",
"job = create_job(\n",
" workflow=workflow,\n",
" material=material,\n",
" compute=compute_config,\n",
" auth_config=auth_config\n",
")\n"
]
},
{
"cell_type": "markdown",
"id": "22",
"metadata": {},
"source": [
"## 8. Submit the job and monitor the status"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23",
"metadata": {},
"outputs": [],
"source": [
"from mat3ra.prode import PropertyEnum\n",
"\n",
"job.run()\n",
"job.wait_for_complete()\n",
"# job.check_status()\n",
"# job.get_current_output()"
]
},
{
"cell_type": "markdown",
"id": "24",
"metadata": {},
"source": [
"## 9. Retrieve results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"metadata": {},
"outputs": [],
"source": [
"# AFTER Finished\n",
"# A class from Prode to handle results\n",
"results = job.get_results(PropertyEnum.BAND_GAP, PropertyEnum.BAND_STRUCTURE)"
]
},
{
"cell_type": "markdown",
"id": "26",
"metadata": {},
"source": [
"## 10. Display results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"metadata": {},
"outputs": [],
"source": [
"# Visual library that can visualize any property defined in Prode\n",
"from mat3ra.prove import visualize_property\n",
"\n",
"visualize_property(results.band_structure)\n",
"print(results.band_gap)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Comment on lines +391 to +413
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fix Python version metadata inconsistency.

The notebook metadata shows conflicting Python versions:

  • Kernel specifies "Python 3" (line 223)
  • Language info specifies version "2.7.6" and "ipython2" (lines 230, 237)

Since the code uses Python 3 features (await/async), the language_info should be updated to reflect Python 3.

Apply this diff to fix the metadata:

   "codemirror_mode": {
     "name": "ipython",
-    "version": 2
+    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
-  "pygments_lexer": "ipython2",
-  "version": "2.7.6"
+  "pygments_lexer": "ipython3",
+  "version": "3.8.0"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
🤖 Prompt for AI Agents
In other/materials_designer/specific_examples/run_bandgap_workflow.ipynb around
lines 220 to 242, the notebook metadata is inconsistent (kernel is "Python 3"
but language_info claims Python 2.7.6 and ipython2); update language_info to
match Python 3 by changing "version" to a Python 3.x string (e.g., "3.8" or
"3.10"), set codemirror_mode "version" to 3, and change "pygments_lexer" from
"ipython2" to "ipython3" (leave kernelspec as-is), so the metadata consistently
reflects Python 3.

Loading