Skip to content

Commit

Permalink
add example run results
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Nov 21, 2024
1 parent f6cc15b commit 224024b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
24 changes: 23 additions & 1 deletion doc/examples/geopt_compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from ase.build import molecule
from ase.constraints import FixAtoms
from sparc import SPARC
nh3 = molecule("NH3", cell=(8, 8, 8), pbc=False)
nh3 = molecule("NH3", cell=(8, 8, 8), pbc=False, center=True)
# Fix the N center
nh3.constraints = [FixAtoms([0])]
nh3.rattle()
Expand Down Expand Up @@ -98,3 +98,25 @@ def optimize_ase_bfgs_socket():
print(f"Final fmax: {np.max(np.abs(f_fin))} eV/Ang")
print(f"N steps: {nsteps}")
```

Possible outputs
```{raw}
SPARC internal LBFGS:
Final energy: -330.8388527992713 eV
Final fmax: 0.014610782237434465 eV/Ang
N steps: 23
```

```{raw}
ASE LBFGS
Final energy: -330.8460713860338 eV
Final fmax: 0.008090338967301871 eV/Ang
N steps: 21
```

```{raw}
ASE LBFGS (socket mode)
Final energy: -330.8356141220578 eV
Final fmax: 0.015431850436823448 eV/Ang
N steps: 18
```
9 changes: 9 additions & 0 deletions doc/examples/simple_dft.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ if __name__ == "__main__":
main()
```

The output from the above example may look like:
```{raw}
Fitted volume (Ang^3), energy (eV), modulus (eV/Ang^3)
65.97840834969949 -253.07755156337953 2.9095110471623173
Optimal cell length (cubic): 4.040799280428726 Ang
Energy calculated by SPARC: -253.0552324051582 eV
Energy diff 0.02231915822133601 eV
```

```{note}
This example uses file I/O mode for demonstration purpose only. Consider choosing the [socket mode](../advanced_socket.md) if you need more flexibility.
```
21 changes: 11 additions & 10 deletions examples/simple_examples/ex1-ase-optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import numpy as np
from ase.build import molecule
from ase.constraints import FixAtoms
from ase.optimize.bfgs import BFGS
from ase.optimize.lbfgs import LBFGS

from sparc import SPARC

nh3 = molecule("NH3", cell=(8, 8, 8), pbc=False)
nh3.center()
# Fix the N center
nh3.constraints = [FixAtoms([0])]
nh3.rattle()
Expand All @@ -20,7 +21,7 @@
def optimize_sparc_internal():
atoms = nh3.copy()
calc = SPARC(
h=0.25,
h=0.18,
kpts=(1, 1, 1),
xc="pbe",
convergence={"forces": 0.02},
Expand All @@ -41,14 +42,14 @@ def optimize_sparc_internal():

def optimize_ase_bfgs():
atoms = nh3.copy()
calc = SPARC(h=0.25, kpts=(1, 1, 1), xc="pbe", directory="ex1-ase")
calc = SPARC(h=0.18, kpts=(1, 1, 1), xc="pbe", directory="ex1-ase")
atoms.calc = calc
opt = BFGS(atoms)
opt = LBFGS(atoms)
opt.run(fmax=0.02)
e_fin = atoms.get_potential_energy()
f_fin = atoms.get_forces()
nsteps = opt.nsteps
print("ASE LBFGS")
print("ASE LBFGS (file I/O mode)")
print(f"Final energy: {e_fin} eV")
print(f"Final fmax: {np.max(np.abs(f_fin))} eV/Ang")
print(f"N steps: {nsteps}")
Expand All @@ -57,7 +58,7 @@ def optimize_ase_bfgs():
def optimize_ase_bfgs_socket():
atoms = nh3.copy()
calc = SPARC(
h=0.25,
h=0.18,
kpts=(1, 1, 1),
xc="pbe",
print_forces=True,
Expand All @@ -66,18 +67,18 @@ def optimize_ase_bfgs_socket():
)
atoms.calc = calc
with calc:
opt = BFGS(atoms)
opt = LBFGS(atoms)
opt.run(fmax=0.02)
e_fin = atoms.get_potential_energy()
f_fin = atoms.get_forces()
nsteps = opt.nsteps
print("ASE LBFGS")
print("ASE LBFGS (socket mode)")
print(f"Final energy: {e_fin} eV")
print(f"Final fmax: {np.max(np.abs(f_fin))} eV/Ang")
print(f"N steps: {nsteps}")


if __name__ == "__main__":
optimize_sparc_internal()
optimize_ase_bfgs()
# optimize_sparc_internal()
# optimize_ase_bfgs()
optimize_ase_bfgs_socket()

0 comments on commit 224024b

Please sign in to comment.