Skip to content

Commit 14e75d5

Browse files
committed
update examples with preopt option
1 parent 463ff5e commit 14e75d5

File tree

5 files changed

+90
-9
lines changed

5 files changed

+90
-9
lines changed

examples/0-aspirin-simple.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
This is an example to perform CSP based on the reference crystal.
33
The structures with good matches will be output to *-matched.cif.
44
"""
5-
import warnings
6-
warnings.filterwarnings("ignore", category=UserWarning, module='torchani.aev')
75
from pyxtal.optimize import WFS, DFS, QRS
86
from pyxtal.representation import representation
7+
from pyxtal.molecule import pyxtal_molecule
98
import argparse
109
import os
1110

@@ -19,16 +18,17 @@
1918
help="cpu number, default: 1")
2019
parser.add_argument("-a", "--algo", dest="algo", default='WFS',
2120
help="algorithm, default: WFS")
21+
parser.add_argument("--preopt", dest='preopt', action='store_true',
22+
help="preoptimize the lattice and rotation")
23+
2224
options = parser.parse_args()
2325
smiles, sg, wdir = "CC(=O)OC1=CC=CC=C1C(=O)O", [14], "aspirin-simple"
2426
# Reconstruct the reference structure
2527
x = "81 11.38 6.48 11.24 96.9 1 0 0.23 0.43 0.03 -44.6 25.0 34.4 -76.6 -5.2 171.5 0"
2628
rep = representation.from_string(x, [smiles])
2729
xtal = rep.to_pyxtal()
2830
pmg = xtal.to_pymatgen()
29-
30-
# Check if use_mpi is invoked
31-
use_mpi = "OMPI_COMM_WORLD_SIZE" in os.environ or "SLURM_MPI_TYPE" in os.environ
31+
m1 = pyxtal_molecule('CC(=O)OC1=CC=CC=C1C(=O)O.smi', active_sites=[[11], [12], [20]])
3232

3333
# Sampling
3434
fun = globals().get(options.algo)
@@ -40,7 +40,8 @@
4040
N_pop = options.pop,
4141
N_cpu = options.ncpu,
4242
ff_style = 'gaff',
43-
use_mpi = use_mpi)
43+
molecules = [[m1]] if options.preopt else None,
44+
pre_opt = options.preopt)
4445

4546
go.run(ref_pmg=pmg)
4647
go.print_matches(header='Ref_match')

examples/4-preopt-xxv.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pyxtal.optimize import DFS, WFS
2+
from pyxtal.db import database
3+
from time import time
4+
from pyxtal.molecule import pyxtal_molecule
5+
import warnings
6+
warnings.filterwarnings("ignore", category=UserWarning, module='torchani.aev')
7+
if __name__ == "__main__":
8+
code = 'XAFQAZ'
9+
db = database('data/benchmark.db')
10+
pmg = db.get_pyxtal(code).to_pymatgen()
11+
row = db.get_row(code)
12+
m1 = pyxtal_molecule('OC(=O)c1cc(cc(c1)N(=O)=O)N(=O)=O.smi', active_sites=[[], [0], [15]])
13+
m2 = pyxtal_molecule('Cc1ccc2N3CN(Cc2c1)c1ccc(C)cc1C3.smi', active_sites=[[5], [], []])
14+
go = DFS(m1.smile + '.' + m2.smile,
15+
code+'-00',
16+
[14],
17+
code,
18+
N_gen = 5,
19+
N_pop = 256,
20+
N_cpu = 128,
21+
ff_style = 'gaff',
22+
molecules = [[m1], [m2]],
23+
pre_opt = True,
24+
)
25+
t0 = time()
26+
suc_rate = go.run(ref_pmg=pmg)
27+
if len(go.matches) > 0:
28+
best_rank = go.print_matches()
29+
mytag = f"Succ_rate: {suc_rate:7.4f}% True {best_rank:d}/{go.N_struc:d}"
30+
else:
31+
mytag = f"False 0/{go.N_struc:d}"
32+
eng = go.min_energy
33+
t1 = int((time() - t0)/60)
34+
strs = "Final {:8s} {:12.3f} {:20s}".format(code, eng, mytag)
35+
print(strs)

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ python 3-benchmark.py -g 20 -p 192 -n 192 -a WFS -c FLUANT
5959
```
6060
This will just run one system called `FLUANT` with 20 generations.
6161

62+
### `4-preopt-xxv.py`
63+
This is an example to explore the use of pre_opt option that can preoptimize the randomly built crystal based on `optimize_lattice_rotation` function.
64+
65+
```
66+
python 4-preopt-xxv.py > log-4-xxv
67+
```
68+
6269

6370
## How to run it on the HPC nodes??
6471

tests/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Test Charmm Installation
2-
3-
This is a directory to test the installation of CHARMM
1+
## Test Charmm Installation
42

53
1. add charmm executable to your system PATH.
64
```
@@ -22,3 +20,19 @@ $ charmm < charmm.in
2220

2321
You should see quickly see the output of `NORMAL TERMINATION`.
2422
If you see other kinds of output, please go back to reinstall charmm.
23+
24+
25+
## Test the use of `optimize_lattice_and_rotation()`
26+
In PyXtal_v1.0.5 or higher, we have implemented the optimize_lattice_and_rotation() to allow the optimization of random crystal by
27+
1. cut the empty spacing of the crystal lattice
28+
2. optimize the molecular orientation to encourage the formation of strong H-bond (with the specified Hbond acceptor, hydrogen and donor sites)
29+
30+
After applying this function, one should expect to get a more reasonable structure that can be used for the subsequent CSP calculation. This may be useful to reduce the searching space.
31+
32+
To test if this function is working, run the following commond in this folder.
33+
```
34+
$ python test_opt_lattice_rotation.py
35+
```
36+
37+
38+

tests/test_opt_lattice_rotation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
A quick example to test if the optimize_lattice_and_rotation works in PyXtal
3+
"""
4+
5+
from pyxtal import pyxtal
6+
from pyxtal.molecule import pyxtal_molecule
7+
8+
xtal = pyxtal(molecular=True)
9+
10+
# apsirin
11+
asp = pyxtal_molecule('CC(=O)OC1=CC=CC=C1C(=O)O.smi', active_sites=[[11], [12], [20]])
12+
#xtal.from_random(3, 14, [asp], sites=[["4e"]])
13+
xtal.from_random(3, 14, [asp], [8], sites=[["4e", "4e"]])
14+
xtal.to_file('init_asp.cif'); print("Init: ", xtal.lattice, xtal.get_orientation_energy())
15+
xtal.optimize_lattice_and_rotation()
16+
xtal.to_file('opt_asp.cif'); print("Opt: ", xtal.lattice, xtal.get_orientation_energy())
17+
18+
# XXV
19+
m1 = pyxtal_molecule('OC(=O)c1cc(cc(c1)N(=O)=O)N(=O)=O.smi', active_sites=[[], [0], [15]])
20+
m2 = pyxtal_molecule('Cc1ccc2N3CN(Cc2c1)c1ccc(C)cc1C3.smi', active_sites=[[5], [], []])
21+
xtal.from_random(3, 14, [m1, m2], [4, 4], sites=[["4e"], ["4e"]])
22+
xtal.to_file('init_xxv.cif'); print("Init: ", xtal.lattice, xtal.get_orientation_energy())
23+
xtal.optimize_lattice_and_rotation()
24+
xtal.to_file('opt_xxv.cif'); print("Opt: ", xtal.lattice, xtal.get_orientation_energy())

0 commit comments

Comments
 (0)