Skip to content

Commit 128c991

Browse files
tberkelsunqm
authored andcommitted
Add pyscf-forge page to user guide
1 parent c5c4052 commit 128c991

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
lines changed

source/user/index.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ gpu.rst
5555
```
5656

5757
```{toctree}
58-
:caption: Extensions
58+
:caption: PySCF-forge
59+
:maxdepth: 1
60+
61+
pyscf-forge
62+
```
63+
64+
```{toctree}
65+
:caption: Extensions
5966
:maxdepth: 1
6067
6168
extensions

source/user/pyscf-forge.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Overview
2+
3+
PySCF-forge is a staging ground for new methods or features that are expected
4+
to be made available in the main PySCF distribution. Not all new features are
5+
appropriate for PySCF-forge; experimental or niche features are more
6+
appropriately distributed as [](./extensions).
7+
8+
## How to install PySCF-forge
9+
10+
PySCF-forge is distributed as a
11+
[separate GitHub repository](https://github.com/pyscf/pyscf-forge).
12+
Like PySCF, it can be installed via pip,
13+
```
14+
pip install pyscf-forge
15+
# or
16+
pip install git+https://github.com/pyscf/pyscf-forge
17+
```
18+
or cloned and built from source. As long as PySCF-forge can be found in your
19+
`PYTHONPATH`, its modules can be imported as if they were native to PySCF.
20+
Alternatively, you can define the `PYSCF_EXT_PATH` environment variable to
21+
point to your local `pyscf-forge` directory,
22+
```
23+
export PYSCF_EXT_PATH=/path/to/pyscf-forge
24+
```
25+
PySCF will read the `PYSCF_EXT_PATH` environment variable and load modules from
26+
this path at runtime. For more details of the `PYSCF_EXT_PATH` environment
27+
variable, see the
28+
[Extensions](./extensions.md#environment-variable-pyscf-ext-path) section. For
29+
example,
30+
```python
31+
from pyscf import gto, mcpdft # mcpdft is in pyscf-forge
32+
mol = gto.M(
33+
atom = 'O 0 0 0; O 0 0 1.2',
34+
basis = 'ccpvdz',
35+
spin = 2)
36+
mf = mol.RHF().run ()
37+
# Calling the MC-PDFT method provided by the pyscf-forge modules
38+
mc = mcpdft.CASCI(mf, 'tPBE', 6, 8).run()
39+
```
40+
41+
## How to develop in PySCF-forge
42+
43+
If you are developing new features or modifying the code in PySCF-forge, an
44+
editable installation is recommended. By configuring the package in editable
45+
mode, you can modify existing modules and add new features to PySCF-forge.
46+
After cloning the library to your local repository, there are two ways to
47+
enable the editable installation.
48+
49+
### Method 1. Using pip for editable installation
50+
51+
Install the package with the following pip command,
52+
```
53+
pip install --no-deps -e /path/to/pyscf-forge
54+
```
55+
This command creates a `.pth` file in `~/.local/lib/python3.*/site-packages/`
56+
or other Python runtime paths. It is recommended to use this method with Python
57+
virtual environments.
58+
59+
### Method 2. Setting an environment variable
60+
61+
As described above, you can also define the `PYSCF_EXT_PATH` environment
62+
variable to point to your local PySCF-forge directory,
63+
```
64+
export PYSCF_EXT_PATH=/path/to/pyscf-forge
65+
```
66+
67+
### Adding new features: An example
68+
69+
Suppose you want to create a new module in PySCF-forge that provides a new
70+
feature called `abc`. You can follow these steps to add the module,
71+
72+
1. Install PySCF-forge in editable installation mode.
73+
74+
2. Create a folder named `pyscf-forge/pyscf/abc`. Due to the editable
75+
installation mode, this folder can be readily imported as a regular `pyscf`
76+
module.
77+
```
78+
>>> from pyscf import abc
79+
>>> print(abc)
80+
<module 'pyscf.abc' (namespace)>
81+
82+
>>> print(abc.__path__)
83+
_NamespacePath(['path/to/pyscf-forge/pyscf/abc'])
84+
```
85+
86+
3. Add Python code files to the `pyscf-forge/pyscf/abc` directory. This
87+
process is similar to developing new methods for PySCF. For example, you
88+
can add the following Python files into the `pyscf-forge/pyscf/abc` folder,
89+
```
90+
pyscf-forge
91+
├── ...
92+
└── pyscf
93+
├── ...
94+
└── abc
95+
├── __init__.py
96+
├── rabc.py
97+
└── uabc.py
98+
```
99+
100+
### Path Conflicts
101+
102+
What if you want to add a feature to a module that already exists in PySCF?
103+
For example, if you want to add a new method, like `pp_rpa.py`, to the
104+
`pyscf/tdscf` folder, this could conflict with the existing `pyscf.tdscf`
105+
module in the PySCF core repository. Adding features to existing modules
106+
requires more a slightly more complex configuration.
107+
108+
To import the `pp_rpa` module from the PySCF-forge repository, you will need to
109+
modify the `__init__.py` file of the PySCF core module. Add the following line
110+
of code to modify the `__path__` attribute of the `pyscf.tdscf` module,
111+
```python
112+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
113+
```
114+
This command extends the search path of the `tdscf` module, resulting in the
115+
`__path__` attribute being set to
116+
```
117+
['/path/to/pyscf/pyscf/tdscf',
118+
'/path/to/pyscf-forge/pyscf/tdscf']
119+
```
120+
This configuration allows Python to locate and load the new `pp_rpa.py` module
121+
from the extended directory in PySCF-forge.
122+
123+
Note that the PySCF core `tdscf` folder already contains an `__init__.py` file.
124+
To avoid overwriting the existing `__init__.py` file in PySCF during the
125+
installation of PySCF-forge, you should not add an `__init__.py` file in the
126+
`pyscf-forge/pyscf/tdscf` directory.
127+
128+
The structure of PySCF and PySCF-forge can be organized as follows,
129+
```
130+
pyscf
131+
├── ...
132+
└── pyscf
133+
├── ...
134+
└── tdscf
135+
├── __init__.py // modify the __path__ attribute in pyscf core module
136+
├── rhf.py
137+
├── rks.py
138+
└── ...
139+
140+
pyscf-forge
141+
├── ...
142+
└── pyscf
143+
├── ...
144+
└── tdscf // no __init__.py file in pyscf-forge
145+
└── pp_rpa.py
146+
```
147+
148+
When installing the PySCF-forge wheels using `pip install` in the normal
149+
installation mode, the `pp_rpa.py` file will be added to the `pyscf/tdscf`
150+
folder, integrating seamlessly as part of the regular PySCF module. After this
151+
standard installation, there is no need to adjust the `__path__` attribute, as
152+
all features and modules are located within the same directory.

0 commit comments

Comments
 (0)