Skip to content

Commit

Permalink
Default values of variables is not stored in db
Browse files Browse the repository at this point in the history
we avoid unnecessary or redundant storing.
  • Loading branch information
mikibonacci committed Nov 20, 2024
1 parent 1441d46 commit 76a2ab7
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 116 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def example_structure_dict():
"positions": [0.0, 0.0, 0.0],
"masses": 63.546,
"charges": 1.0,
"magmoms": [0.0,0.0,0.0],
#"magmoms": [0.0,0.0,0.0],
"weights": (1,)
}
],
Expand Down
98 changes: 29 additions & 69 deletions docs/01-structuredata.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@
" 'positions': [[0.0, 0.0, 0.0],\n",
" [3.84, 1.3576450198781713, 1.9200000000000006]],\n",
" 'kinds': ['Si', 'Si'],\n",
" 'weights': [(1.0,), (1.0,)],\n",
" 'weights': None,\n",
" 'masses': [28.0855, 28.0855],\n",
" 'charges': [0.0, 0.0],\n",
" 'magmoms': [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],\n",
" 'charges': None,\n",
" 'magmoms': None,\n",
" 'hubbard': {'parameters': [],\n",
" 'projectors': 'ortho-atomic',\n",
" 'formulation': 'dudarev'},\n",
Expand Down Expand Up @@ -188,20 +188,21 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Useful methods are provided to help the user modify the structure. For example, we can modify a site just using the `update_site` method of the structure class:"
"Useful methods are provided to help the user modify the structure. As a rule, sites cannot be modified directly (these are derived from the list of properties). In general, we can modify a site just using the `update_site` method of the structure class:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial charge on the first site: 0.0\n",
"Updated charge on the first site: 1.0\n"
"Initial charge on the first site: 1.0\n",
"Updated charge on the first site: 1.0\n",
"Charges in the system: [1, 0]\n"
]
}
],
Expand All @@ -214,18 +215,34 @@
" site_index=0, \n",
" charges=+1)\n",
"\n",
"print(\"Updated charge on the first site: \", mutable_structure.properties.sites[0].charges)"
"print(\"Updated charge on the first site: \", mutable_structure.properties.sites[0].charges)\n",
"print(\"Charges in the system: \", mutable_structure.properties.charges)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To visualize the *supported* properties, use the `get_supported_properties` method of the structure class.\n",
"To visualize the *defined* properties for the structure, you can use the `get_defined_properties`.\n",
":::\n",
"Other methods to update specific properties are: `set_pbc`, `set_cell`, `set_charges`, `set_magmoms`, `set_automatic_kinds`, `set_site_property` and so on. You can see them using tab-completion."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Print defined and supported set of properties\n",
"\n",
"we can also provide the dictionary containing the sites as list of dictionaries, each of them describing a site. This is useful when we want to provide additional information for each site, such as the occupancy or the magnetic moment. In this case, the dictionary should have the following structure:\n",
"To print the *supported* properties, use the `get_supported_properties` method of the structure class.\n",
"List of the *defined* properties for the structure can be accessed using the `get_defined_properties`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### An additional way to define a structure: a sites list\n",
"\n",
"We can also provide the dictionary containing the sites as list of dictionaries, each of them describing a site. This is useful when we want to provide additional information for each site, such as the occupancy or the magnetic moment. In this case, the dictionary should have the following structure:\n",
" \n",
"```python\n",
"structure_dict = {\n",
Expand Down Expand Up @@ -733,63 +750,6 @@
"sites_with_kinds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to Query StructureData using properties\n",
"\n",
"Thanks to the additional computed properties in our `StructureData` (*formula*, *symbols*, *kinds*, *masses*, *charges*, *magmoms*, *positions*, *cell_volume*, *dimensionality*), we can easily query for a structure:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2635\n"
]
},
{
"data": {
"text/plain": [
"[[<StructureData: uuid: 8851c51a-0560-404b-abba-82359ee5a595 (pk: 2029)>],\n",
" [<StructureData: uuid: 3f5390e2-816b-450c-b67c-b87dfe11872f (pk: 2242)>],\n",
" [<StructureData: uuid: 8eeeef7a-3a40-4735-bafb-00d78b97202d (pk: 2030)>],\n",
" [<StructureData: uuid: 0b54bd97-919a-4959-bfc2-92f4fcccff10 (pk: 2635)>],\n",
" [<StructureData: uuid: fcc156e7-b633-4513-a2af-cf130cbc6ddd (pk: 2031)>],\n",
" [<StructureData: uuid: 3d91cbd3-e4aa-4ddf-a49f-94c4b0fe9ec7 (pk: 2243)>],\n",
" [<StructureData: uuid: abf28cab-f8a9-4084-b603-967b907eb08c (pk: 2244)>],\n",
" [<StructureData: uuid: 8310cd8a-6d95-4b32-9752-a5186bd0929e (pk: 2245)>],\n",
" [<StructureData: uuid: 8762ed75-09b1-4b8b-97a7-136233d9ceaa (pk: 2246)>],\n",
" [<StructureData: uuid: 1123f488-a8f5-46aa-8b07-65a3bc395db7 (pk: 2247)>],\n",
" [<StructureData: uuid: 1ee9c471-786f-424b-ad6a-6a9626380231 (pk: 1649)>],\n",
" [<StructureData: uuid: d3cba3d3-b21a-43fa-8397-b734cc006a37 (pk: 1650)>]]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from aiida.orm import QueryBuilder\n",
"\n",
"stored = StructureData.from_mutable(mutable_structure).store()\n",
"print(stored.pk)\n",
"\n",
"qb = QueryBuilder()\n",
"qb.append(StructureData, \n",
" filters={'attributes.formula': 'Fe2'},\n",
" )\n",
"\n",
"qb.all()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion docs/06-how_to_deal_with_kinds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
":::{note} Why do we need to define kinds?\n",
":class: dropdown\n",
"Some codes, like Quantum ESPRESSO, distinguish the atoms using the concept of `kind`. \n",
"The concept of `element_wise_element_wise_element_wise_element_wise_kind` allows you to specify the distinct atoms (even of the same element) and their associated properties in your simulation, e.g. two iron atoms with different magnetic momenta (see below examples).\n",
"This concept allows you to specify distinct atoms (even of the same element) and their associated properties in your simulation, e.g. two iron atoms with different magnetic momenta (see below examples).\n",
":::"
]
},
Expand Down
152 changes: 152 additions & 0 deletions docs/07-how-to-query.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to efficiently query properties\n",
"\n",
"In the following we presents first how `StructureData` nodes are store in the AiiDA database and then how to user the `QueryBuilder` to find them.\n",
"\n",
"## How properties are store in the AiiDA database\n",
"\n",
"In the AiiDA database, we store only properties which are defined to be different from the default value. For example, if we provide all the charges to be zero, the `charges` properties will not be stored in the database. This means that the only structures which are in the database and contains also a charges entry, will be about systems with charged different from zero (NB can also be overall neutral).\n",
"\n",
"For a given structure, you can print the list of all the properties stored in the database (and so, queryable), by calling the `get_defined_properties` method and providing `exclude_computed=False` as input (i.e. returning also properties which are computed/derived from the user-defined ones):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'positions', 'dimensionality', 'symbols', 'cell_volume', 'cell', 'kinds', 'masses', 'formula', 'sites'}\n"
]
}
],
"source": [
"from aiida import load_profile\n",
"from aiida_atomistic import StructureData\n",
"\n",
"load_profile()\n",
"\n",
"structure = StructureData(**{\n",
" 'pbc': [True, True, True],\n",
" 'cell': [[2.75, 2.75, 0.0], [0.0, 2.75, 2.75], [2.75, 0.0, 2.75]],\n",
" 'symbols': ['Si', 'Si'],\n",
" 'charges': [0.0, 0.0],\n",
" 'kinds': ['Si', 'Si'],\n",
" 'positions': [\n",
" [0.0, 0.0, 0.0],\n",
" [3.84, 1.3576450198781713, 1.9200]\n",
" ],\n",
"})\n",
"\n",
"print(\"Properties stored in the database are:\")\n",
"print(structure.get_defined_properties(exclude_computed=False))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Where actually the only properties stored in the AiiDA database are:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'positions', 'symbols', 'cell', 'kinds', 'masses'}\n"
]
}
],
"source": [
"print(structure.get_defined_properties())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The only exception will be the `sites` property, which is contained in this list but not stored in the database: this is computed on-the-fly and it does not contains additional information meant to be in the database.\n",
"\n",
":::{note} \n",
":class: dropdown\n",
"To explicitely see how data are stored and represented in the database, you can access the `structure.base.attributes.all` dictionary. \n",
"As you can see, not `sites` entry is present.\n",
"\n",
"```python\n",
"print(structure.base.attributes.all.keys())\n",
"```\n",
"\n",
"returns: \n",
"\n",
"```bash\n",
"dict_keys(['cell', 'symbols', 'positions', 'kinds', 'masses', 'cell_volume', 'dimensionality', 'formula'])\n",
"```\n",
":::"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to Query StructureData using properties\n",
"\n",
"In the following we present how to perform advanced query in you database to retrieve `StructureData` with given properties.\n",
"\n",
"Thanks to the additional computed properties in our `StructureData` (*formula*, *symbols*, *kinds*, *masses*, *charges*, *magmoms*, *positions*, *cell_volume*, *dimensionality*), we can easily query for a structure:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from aiida.orm import QueryBuilder\n",
"\n",
"stored = StructureData.from_mutable(mutable_structure).store()\n",
"print(stored.pk)\n",
"\n",
"qb = QueryBuilder()\n",
"qb.append(StructureData, \n",
" filters={'attributes.formula': 'Fe2'},\n",
" )\n",
"\n",
"qb.all()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions docs/myst.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ project:
- file: 03-tutorial_qe.ipynb
- file: 05-EoS_workgraph.ipynb
- file: 06-how_to_deal_with_kinds.ipynb
- file: 07-how-to-query.ipynb
- file: 04-plugin_migration_guide.ipynb


Expand Down
15 changes: 15 additions & 0 deletions src/aiida_atomistic/data/structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@
"""

__version__ = "0.1.0a0"

_MASS_THRESHOLD = 1.0e-3
# Threshold to check if the sum is one or not
_SUM_THRESHOLD = 1.0e-6
# Default cell
_DEFAULT_CELL = [[0.0, 0.0, 0.0]] * 3
_DEFAULT_PBC = [True, True, True]

_DEFAULT_VALUES = {
"masses": 0,
"charges": 0,
"magmoms": [0, 0, 0],
"hubbard": None,
"weights": (1,)
}
Loading

0 comments on commit 76a2ab7

Please sign in to comment.