Skip to content

Commit

Permalink
update examples with preopt option
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhu2017 committed Nov 8, 2024
1 parent 463ff5e commit 14e75d5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
13 changes: 7 additions & 6 deletions examples/0-aspirin-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
This is an example to perform CSP based on the reference crystal.
The structures with good matches will be output to *-matched.cif.
"""
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='torchani.aev')
from pyxtal.optimize import WFS, DFS, QRS
from pyxtal.representation import representation
from pyxtal.molecule import pyxtal_molecule
import argparse
import os

Expand All @@ -19,16 +18,17 @@
help="cpu number, default: 1")
parser.add_argument("-a", "--algo", dest="algo", default='WFS',
help="algorithm, default: WFS")
parser.add_argument("--preopt", dest='preopt', action='store_true',
help="preoptimize the lattice and rotation")

options = parser.parse_args()
smiles, sg, wdir = "CC(=O)OC1=CC=CC=C1C(=O)O", [14], "aspirin-simple"
# Reconstruct the reference structure
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"
rep = representation.from_string(x, [smiles])
xtal = rep.to_pyxtal()
pmg = xtal.to_pymatgen()

# Check if use_mpi is invoked
use_mpi = "OMPI_COMM_WORLD_SIZE" in os.environ or "SLURM_MPI_TYPE" in os.environ
m1 = pyxtal_molecule('CC(=O)OC1=CC=CC=C1C(=O)O.smi', active_sites=[[11], [12], [20]])

# Sampling
fun = globals().get(options.algo)
Expand All @@ -40,7 +40,8 @@
N_pop = options.pop,
N_cpu = options.ncpu,
ff_style = 'gaff',
use_mpi = use_mpi)
molecules = [[m1]] if options.preopt else None,
pre_opt = options.preopt)

go.run(ref_pmg=pmg)
go.print_matches(header='Ref_match')
Expand Down
35 changes: 35 additions & 0 deletions examples/4-preopt-xxv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pyxtal.optimize import DFS, WFS
from pyxtal.db import database
from time import time
from pyxtal.molecule import pyxtal_molecule
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='torchani.aev')
if __name__ == "__main__":
code = 'XAFQAZ'
db = database('data/benchmark.db')
pmg = db.get_pyxtal(code).to_pymatgen()
row = db.get_row(code)
m1 = pyxtal_molecule('OC(=O)c1cc(cc(c1)N(=O)=O)N(=O)=O.smi', active_sites=[[], [0], [15]])
m2 = pyxtal_molecule('Cc1ccc2N3CN(Cc2c1)c1ccc(C)cc1C3.smi', active_sites=[[5], [], []])
go = DFS(m1.smile + '.' + m2.smile,
code+'-00',
[14],
code,
N_gen = 5,
N_pop = 256,
N_cpu = 128,
ff_style = 'gaff',
molecules = [[m1], [m2]],
pre_opt = True,
)
t0 = time()
suc_rate = go.run(ref_pmg=pmg)
if len(go.matches) > 0:
best_rank = go.print_matches()
mytag = f"Succ_rate: {suc_rate:7.4f}% True {best_rank:d}/{go.N_struc:d}"
else:
mytag = f"False 0/{go.N_struc:d}"
eng = go.min_energy
t1 = int((time() - t0)/60)
strs = "Final {:8s} {:12.3f} {:20s}".format(code, eng, mytag)
print(strs)
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ python 3-benchmark.py -g 20 -p 192 -n 192 -a WFS -c FLUANT
```
This will just run one system called `FLUANT` with 20 generations.

### `4-preopt-xxv.py`
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.

```
python 4-preopt-xxv.py > log-4-xxv
```


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

Expand Down
20 changes: 17 additions & 3 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Test Charmm Installation

This is a directory to test the installation of CHARMM
## Test Charmm Installation

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

You should see quickly see the output of `NORMAL TERMINATION`.
If you see other kinds of output, please go back to reinstall charmm.


## Test the use of `optimize_lattice_and_rotation()`
In PyXtal_v1.0.5 or higher, we have implemented the optimize_lattice_and_rotation() to allow the optimization of random crystal by
1. cut the empty spacing of the crystal lattice
2. optimize the molecular orientation to encourage the formation of strong H-bond (with the specified Hbond acceptor, hydrogen and donor sites)

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.

To test if this function is working, run the following commond in this folder.
```
$ python test_opt_lattice_rotation.py
```



24 changes: 24 additions & 0 deletions tests/test_opt_lattice_rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
A quick example to test if the optimize_lattice_and_rotation works in PyXtal
"""

from pyxtal import pyxtal
from pyxtal.molecule import pyxtal_molecule

xtal = pyxtal(molecular=True)

# apsirin
asp = pyxtal_molecule('CC(=O)OC1=CC=CC=C1C(=O)O.smi', active_sites=[[11], [12], [20]])
#xtal.from_random(3, 14, [asp], sites=[["4e"]])
xtal.from_random(3, 14, [asp], [8], sites=[["4e", "4e"]])
xtal.to_file('init_asp.cif'); print("Init: ", xtal.lattice, xtal.get_orientation_energy())
xtal.optimize_lattice_and_rotation()
xtal.to_file('opt_asp.cif'); print("Opt: ", xtal.lattice, xtal.get_orientation_energy())

# XXV
m1 = pyxtal_molecule('OC(=O)c1cc(cc(c1)N(=O)=O)N(=O)=O.smi', active_sites=[[], [0], [15]])
m2 = pyxtal_molecule('Cc1ccc2N3CN(Cc2c1)c1ccc(C)cc1C3.smi', active_sites=[[5], [], []])
xtal.from_random(3, 14, [m1, m2], [4, 4], sites=[["4e"], ["4e"]])
xtal.to_file('init_xxv.cif'); print("Init: ", xtal.lattice, xtal.get_orientation_energy())
xtal.optimize_lattice_and_rotation()
xtal.to_file('opt_xxv.cif'); print("Opt: ", xtal.lattice, xtal.get_orientation_energy())

0 comments on commit 14e75d5

Please sign in to comment.